LCOV - code coverage report
Current view: top level - src - unstrip.c (source / functions) Coverage Total Hit
Test: elfutils-0.197 Lines: 71.0 % 1258 893
Test Date: 2026-11-10 23:31:53 Functions: 85.7 % 42 36
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 55.6 % 1017 565

             Branch data     Line data    Source code
       1                 :             : /* Combine stripped files with separate symbols and debug information.
       2                 :             :    Copyright (C) 2007-2012, 2014, 2015 Red Hat, Inc.
       3                 :             :    This file is part of elfutils.
       4                 :             :    Written by Roland McGrath <roland@redhat.com>, 2007.
       5                 :             : 
       6                 :             :    This file is free software; you can redistribute it and/or modify
       7                 :             :    it under the terms of the GNU General Public License as published by
       8                 :             :    the Free Software Foundation; either version 3 of the License, or
       9                 :             :    (at your option) any later version.
      10                 :             : 
      11                 :             :    elfutils is distributed in the hope that it will be useful, but
      12                 :             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :             :    GNU General Public License for more details.
      15                 :             : 
      16                 :             :    You should have received a copy of the GNU General Public License
      17                 :             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      18                 :             : 
      19                 :             : /* TODO:
      20                 :             : 
      21                 :             :   * SHX_XINDEX
      22                 :             : 
      23                 :             :   * prelink vs .debug_* linked addresses
      24                 :             : 
      25                 :             :  */
      26                 :             : 
      27                 :             : #ifdef HAVE_CONFIG_H
      28                 :             : # include <config.h>
      29                 :             : #endif
      30                 :             : 
      31                 :             : #include <argp.h>
      32                 :             : #include <assert.h>
      33                 :             : #include <errno.h>
      34                 :             : #include <fcntl.h>
      35                 :             : #include <fnmatch.h>
      36                 :             : #include <locale.h>
      37                 :             : #include <stdbool.h>
      38                 :             : #include <stdio.h>
      39                 :             : #include <stdio_ext.h>
      40                 :             : #include <inttypes.h>
      41                 :             : #include <stdlib.h>
      42                 :             : #include <string.h>
      43                 :             : #include <unistd.h>
      44                 :             : #include <sys/stat.h>
      45                 :             : 
      46                 :             : #include <gelf.h>
      47                 :             : #include <libebl.h>
      48                 :             : #include <libdwfl.h>
      49                 :             : #include "system.h"
      50                 :             : #include "libdwelf.h"
      51                 :             : #include "libeu.h"
      52                 :             : #include "printversion.h"
      53                 :             : 
      54                 :             : /* Name and version of program.  */
      55                 :             : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
      56                 :             : 
      57                 :             : /* Bug report address.  */
      58                 :             : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
      59                 :             : 
      60                 :             : /* Definitions of arguments for argp functions.  */
      61                 :             : static const struct argp_option options[] =
      62                 :             : {
      63                 :             :   /* Group 2 will follow group 1 from dwfl_standard_argp.  */
      64                 :             :   { "match-file-names", 'f', NULL, 0,
      65                 :             :     N_("Match MODULE against file names, not module names"), 2 },
      66                 :             :   { "ignore-missing", 'i', NULL, 0, N_("Silently skip unfindable files"), 0 },
      67                 :             : 
      68                 :             :   { NULL, 0, NULL, 0, N_("Output options:"), 0 },
      69                 :             :   { "output", 'o', "FILE", 0, N_("Place output into FILE"), 0 },
      70                 :             :   { "output-directory", 'd', "DIRECTORY",
      71                 :             :     0, N_("Create multiple output files under DIRECTORY"), 0 },
      72                 :             :   { "module-names", 'm', NULL, 0, N_("Use module rather than file names"), 0 },
      73                 :             :   { "all", 'a', NULL, 0,
      74                 :             :     N_("Create output for modules that have no separate debug information"),
      75                 :             :     0 },
      76                 :             :   { "relocate", 'R', NULL, 0,
      77                 :             :     N_("Apply relocations to section contents in ET_REL files"), 0 },
      78                 :             :   { "list-only", 'n', NULL, 0,
      79                 :             :     N_("Only list module and file names, build IDs"), 0 },
      80                 :             :  { "force", 'F', NULL, 0,
      81                 :             :     N_("Force combining files even if some ELF headers don't seem to match"),
      82                 :             :    0 },
      83                 :             :   { NULL, 0, NULL, 0, NULL, 0 }
      84                 :             : };
      85                 :             : 
      86                 :             : struct arg_info
      87                 :             : {
      88                 :             :   const char *output_file;
      89                 :             :   const char *output_dir;
      90                 :             :   Dwfl *dwfl;
      91                 :             :   char **args;
      92                 :             :   bool list;
      93                 :             :   bool all;
      94                 :             :   bool ignore;
      95                 :             :   bool modnames;
      96                 :             :   bool match_files;
      97                 :             :   bool relocate;
      98                 :             :   bool force;
      99                 :             : };
     100                 :             : 
     101                 :             : /* Handle program arguments.  */
     102                 :             : static error_t
     103                 :         340 : parse_opt (int key, char *arg, struct argp_state *state)
     104                 :             : {
     105                 :         340 :   struct arg_info *info = state->input;
     106                 :             : 
     107   [ +  +  -  -  :         340 :   switch (key)
          -  -  -  +  -  
                -  +  + ]
     108                 :             :     {
     109                 :          58 :     case ARGP_KEY_INIT:
     110                 :          58 :       state->child_inputs[0] = &info->dwfl;
     111                 :          58 :       break;
     112                 :             : 
     113                 :          40 :     case 'o':
     114         [ -  + ]:          40 :       if (info->output_file != NULL)
     115                 :             :         {
     116                 :           0 :           argp_error (state, _("-o option specified twice"));
     117                 :           0 :           return EINVAL;
     118                 :             :         }
     119                 :          40 :       info->output_file = arg;
     120                 :          40 :       break;
     121                 :             : 
     122                 :           0 :     case 'd':
     123         [ #  # ]:           0 :       if (info->output_dir != NULL)
     124                 :             :         {
     125                 :           0 :           argp_error (state, _("-d option specified twice"));
     126                 :           0 :           return EINVAL;
     127                 :             :         }
     128                 :           0 :       info->output_dir = arg;
     129                 :           0 :       break;
     130                 :             : 
     131                 :           0 :     case 'm':
     132                 :           0 :       info->modnames = true;
     133                 :           0 :       break;
     134                 :           0 :     case 'f':
     135                 :           0 :       info->match_files = true;
     136                 :           0 :       break;
     137                 :           0 :     case 'a':
     138                 :           0 :       info->all = true;
     139                 :           0 :       break;
     140                 :           0 :     case 'i':
     141                 :           0 :       info->ignore = true;
     142                 :           0 :       break;
     143                 :          10 :     case 'n':
     144                 :          10 :       info->list = true;
     145                 :          10 :       break;
     146                 :           0 :     case 'R':
     147                 :           0 :       info->relocate = true;
     148                 :           0 :       break;
     149                 :           0 :     case 'F':
     150                 :           0 :       info->force = true;
     151                 :           0 :       break;
     152                 :             : 
     153                 :          58 :     case ARGP_KEY_ARGS:
     154                 :             :     case ARGP_KEY_NO_ARGS:
     155                 :             :       /* We "consume" all the arguments here.  */
     156                 :          58 :       info->args = &state->argv[state->next];
     157                 :             : 
     158   [ +  +  -  + ]:          58 :       if (info->output_file != NULL && info->output_dir != NULL)
     159                 :             :         {
     160                 :           0 :           argp_error (state, _("only one of -o or -d allowed"));
     161                 :           0 :           return EINVAL;
     162                 :             :         }
     163                 :             : 
     164   [ +  +  +  - ]:          58 :       if (info->list && (info->dwfl == NULL
     165         [ +  - ]:          10 :                          || info->output_dir != NULL
     166         [ -  + ]:          10 :                          || info->output_file != NULL))
     167                 :             :         {
     168                 :           0 :           argp_error (state,
     169                 :           0 :                       _("-n cannot be used with explicit files or -o or -d"));
     170                 :           0 :           return EINVAL;
     171                 :             :         }
     172                 :             : 
     173         [ -  + ]:          58 :       if (info->output_dir != NULL)
     174                 :             :         {
     175                 :           0 :           struct stat st;
     176                 :           0 :           error_t fail = 0;
     177         [ #  # ]:           0 :           if (stat (info->output_dir, &st) < 0)
     178                 :           0 :             fail = errno;
     179         [ #  # ]:           0 :           else if (!S_ISDIR (st.st_mode))
     180                 :             :             fail = ENOTDIR;
     181         [ #  # ]:           0 :           if (fail)
     182                 :             :             {
     183                 :           0 :               argp_failure (state, EXIT_FAILURE, fail,
     184                 :           0 :                             _("output directory '%s'"), info->output_dir);
     185                 :           0 :               return fail;
     186                 :             :             }
     187                 :             :         }
     188                 :             : 
     189         [ +  + ]:          58 :       if (info->dwfl == NULL)
     190                 :             :         {
     191         [ -  + ]:          48 :           if (state->next + 2 != state->argc)
     192                 :             :             {
     193                 :           0 :               argp_error (state, _("exactly two file arguments are required"));
     194                 :           0 :               return EINVAL;
     195                 :             :             }
     196                 :             : 
     197         [ -  + ]:          48 :           if (info->ignore || info->all || info->modnames || info->relocate)
     198                 :             :             {
     199                 :           0 :               argp_error (state, _("\
     200                 :             : -m, -a, -R, and -i options not allowed with explicit files"));
     201                 :           0 :               return EINVAL;
     202                 :             :             }
     203                 :             : 
     204                 :             :           /* Bail out immediately to prevent dwfl_standard_argp's parser
     205                 :             :              from defaulting to "-e a.out".  */
     206                 :             :           return ENOSYS;
     207                 :             :         }
     208   [ -  +  -  + ]:          10 :       else if (info->output_file == NULL && info->output_dir == NULL
     209         [ +  - ]:          10 :                && !info->list)
     210                 :             :         {
     211                 :           0 :           argp_error (state,
     212                 :           0 :                       _("-o or -d is required when using implicit files"));
     213                 :           0 :           return EINVAL;
     214                 :             :         }
     215                 :             :       break;
     216                 :             : 
     217                 :             :     default:
     218                 :             :       return ARGP_ERR_UNKNOWN;
     219                 :             :     }
     220                 :             :   return 0;
     221                 :             : }
     222                 :             : 
     223                 :             : #define ELF_CHECK(call, msg)                                                  \
     224                 :             :   do                                                                          \
     225                 :             :     {                                                                         \
     226                 :             :       if (unlikely (!(call)))                                                 \
     227                 :             :         error_exit (0, msg, elf_errmsg (-1));                                 \
     228                 :             :     } while (0)
     229                 :             : 
     230                 :             : /* Copy INELF to newly-created OUTELF, exit via error for any problems.  */
     231                 :             : static void
     232                 :          40 : copy_elf (Elf *outelf, Elf *inelf)
     233                 :             : {
     234         [ -  + ]:          40 :   ELF_CHECK (gelf_newehdr (outelf, gelf_getclass (inelf)),
     235                 :             :              _("cannot create ELF header: %s"));
     236                 :             : 
     237                 :          40 :   size_t shstrndx;
     238         [ -  + ]:          40 :   ELF_CHECK (elf_getshdrstrndx (inelf, &shstrndx) == 0,
     239                 :             :              _("cannot get shdrstrndx:%s"));
     240                 :             : 
     241                 :          40 :   GElf_Ehdr ehdr_mem;
     242                 :          40 :   GElf_Ehdr *ehdr = gelf_getehdr (inelf, &ehdr_mem);
     243         [ -  + ]:          40 :   ELF_CHECK (ehdr != NULL, _("cannot get ELF header: %s"));
     244         [ +  + ]:          40 :   if (shstrndx < SHN_LORESERVE)
     245                 :          38 :     ehdr->e_shstrndx = shstrndx;
     246                 :             :   else
     247                 :             :     {
     248                 :           2 :       ehdr->e_shstrndx = SHN_XINDEX;
     249                 :           2 :       Elf_Scn *scn0 = elf_getscn (outelf, 0);
     250                 :           2 :       GElf_Shdr shdr0_mem;
     251                 :           2 :       GElf_Shdr *shdr0 = gelf_getshdr (scn0, &shdr0_mem);
     252         [ -  + ]:           2 :       ELF_CHECK (shdr0 != NULL,
     253                 :             :                  _("cannot get new zero section: %s"));
     254                 :           2 :       shdr0->sh_link = shstrndx;
     255         [ -  + ]:           2 :       ELF_CHECK (gelf_update_shdr (scn0, shdr0),
     256                 :             :                  _("cannot update new zero section: %s"));
     257                 :             :     }
     258                 :             : 
     259         [ -  + ]:          40 :   ELF_CHECK (gelf_update_ehdr (outelf, ehdr),
     260                 :             :              _("cannot copy ELF header: %s"));
     261                 :             : 
     262                 :          40 :   size_t phnum;
     263         [ -  + ]:          40 :   ELF_CHECK (elf_getphdrnum (inelf, &phnum) == 0,
     264                 :             :              _("cannot get number of program headers: %s"));
     265                 :             : 
     266         [ +  + ]:          40 :   if (phnum > 0)
     267                 :             :     {
     268         [ -  + ]:          18 :       ELF_CHECK (gelf_newphdr (outelf, phnum),
     269                 :             :                  _("cannot create program headers: %s"));
     270                 :             : 
     271                 :             :       GElf_Phdr phdr_mem;
     272         [ +  + ]:         150 :       for (size_t i = 0; i < phnum; ++i)
     273         [ -  + ]:         132 :         ELF_CHECK (gelf_update_phdr (outelf, i,
     274                 :             :                                      gelf_getphdr (inelf, i, &phdr_mem)),
     275                 :             :                    _("cannot copy program header: %s"));
     276                 :             :     }
     277                 :             : 
     278                 :             :   Elf_Scn *scn = NULL;
     279         [ +  + ]:      132194 :   while ((scn = elf_nextscn (inelf, scn)) != NULL)
     280                 :             :     {
     281                 :      132154 :       Elf_Scn *newscn = elf_newscn (outelf);
     282                 :             : 
     283                 :      132154 :       GElf_Shdr shdr_mem;
     284         [ -  + ]:      132154 :       ELF_CHECK (gelf_update_shdr (newscn, gelf_getshdr (scn, &shdr_mem)),
     285                 :             :                  _("cannot copy section header: %s"));
     286                 :             : 
     287                 :      132154 :       Elf_Data *data = elf_getdata (scn, NULL);
     288         [ -  + ]:      132154 :       ELF_CHECK (data != NULL, _("cannot get section data: %s"));
     289                 :      132154 :       Elf_Data *newdata = elf_newdata (newscn);
     290         [ -  + ]:      132154 :       ELF_CHECK (newdata != NULL, _("cannot copy section data: %s"));
     291                 :      132154 :       *newdata = *data;
     292                 :      132154 :       elf_flagdata (newdata, ELF_C_SET, ELF_F_DIRTY);
     293                 :             :     }
     294                 :          40 : }
     295                 :             : 
     296                 :             : /* Create directories containing PATH.  */
     297                 :             : static void
     298                 :           0 : make_directories (const char *path)
     299                 :             : {
     300                 :           0 :   const char *lastslash = strrchr (path, '/');
     301         [ #  # ]:           0 :   if (lastslash == NULL)
     302                 :             :     return;
     303                 :             : 
     304   [ #  #  #  # ]:           0 :   while (lastslash > path && lastslash[-1] == '/')
     305                 :           0 :     --lastslash;
     306         [ #  # ]:           0 :   if (lastslash == path)
     307                 :             :     return;
     308                 :             : 
     309                 :           0 :   char *dir = strndup (path, lastslash - path);
     310         [ #  # ]:           0 :   if (dir == NULL)
     311                 :           0 :     error(EXIT_FAILURE, errno, _("memory exhausted"));
     312                 :             : 
     313   [ #  #  #  # ]:           0 :   while (mkdir (dir, ACCESSPERMS) < 0 && errno != EEXIST)
     314                 :             :     {
     315         [ #  # ]:           0 :       if (errno == ENOENT)
     316                 :           0 :         make_directories (dir);
     317                 :             :       else
     318                 :           0 :         error_exit (errno, _("cannot create directory '%s'"), dir);
     319                 :             :     }
     320                 :           0 :   free (dir);
     321                 :             : }
     322                 :             : 
     323                 :             : /* Keep track of new section data we are creating, so we can free it
     324                 :             :    when done.  */
     325                 :             : struct data_list
     326                 :             : {
     327                 :             :   void *data;
     328                 :             :   struct data_list *next;
     329                 :             : };
     330                 :             : 
     331                 :             : struct data_list *new_data_list;
     332                 :             : 
     333                 :             : static void
     334                 :          38 : record_new_data (void *data)
     335                 :             : {
     336                 :          38 :   struct data_list *next = new_data_list;
     337                 :          38 :   new_data_list = xmalloc (sizeof (struct data_list));
     338                 :          38 :   new_data_list->data = data;
     339                 :          38 :   new_data_list->next = next;
     340                 :          38 : }
     341                 :             : 
     342                 :             : static void
     343                 :          40 : free_new_data (void)
     344                 :             : {
     345                 :          40 :   struct data_list *list = new_data_list;
     346         [ +  + ]:          78 :   while (list != NULL)
     347                 :             :     {
     348                 :          38 :       struct data_list *next = list->next;
     349                 :          38 :       free (list->data);
     350                 :          38 :       free (list);
     351                 :          38 :       list = next;
     352                 :             :     }
     353                 :          40 :   new_data_list = NULL;
     354                 :          40 : }
     355                 :             : 
     356                 :             : /* The binutils linker leaves gratuitous section symbols in .symtab
     357                 :             :    that strip has to remove.  Older linkers likewise include a
     358                 :             :    symbol for every section, even unallocated ones, in .dynsym.
     359                 :             :    Because of this, the related sections can shrink in the stripped
     360                 :             :    file from their original size.  Older versions of strip do not
     361                 :             :    adjust the sh_size field in the debuginfo file's SHT_NOBITS
     362                 :             :    version of the section header, so it can appear larger.  */
     363                 :             : static bool
     364                 :          14 : section_can_shrink (const GElf_Shdr *shdr)
     365                 :             : {
     366         [ -  + ]:          14 :   switch (shdr->sh_type)
     367                 :             :     {
     368                 :             :     case SHT_SYMTAB:
     369                 :             :     case SHT_DYNSYM:
     370                 :             :     case SHT_HASH:
     371                 :             :     case SHT_GNU_versym:
     372                 :             :       return true;
     373                 :             :     }
     374                 :           0 :   return false;
     375                 :             : }
     376                 :             : 
     377                 :             : /* See if this symbol table has a leading section symbol for every single
     378                 :             :    section, in order.  The binutils linker produces this.  While we're here,
     379                 :             :    update each section symbol's st_value.  */
     380                 :             : static size_t
     381                 :          24 : symtab_count_leading_section_symbols (Elf *elf, Elf_Scn *scn, size_t shnum,
     382                 :             :                                       Elf_Data *newsymdata)
     383                 :             : {
     384                 :          24 :   Elf_Data *data = elf_getdata (scn, NULL);
     385                 :          24 :   Elf_Data *shndxdata = NULL;   /* XXX */
     386                 :             : 
     387         [ +  - ]:         156 :   for (size_t i = 1; i < shnum; ++i)
     388                 :             :     {
     389                 :         156 :       GElf_Sym sym_mem;
     390                 :         156 :       GElf_Word shndx = SHN_UNDEF;
     391                 :         156 :       GElf_Sym *sym = gelf_getsymshndx (data, shndxdata, i, &sym_mem, &shndx);
     392         [ -  + ]:         156 :       ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s"));
     393                 :             : 
     394                 :         156 :       GElf_Shdr shdr_mem;
     395                 :         156 :       GElf_Shdr *shdr = gelf_getshdr (elf_getscn (elf, i), &shdr_mem);
     396         [ -  + ]:         156 :       ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
     397                 :             : 
     398         [ +  - ]:         156 :       if (sym->st_shndx != SHN_XINDEX)
     399                 :         156 :         shndx = sym->st_shndx;
     400                 :             : 
     401   [ +  +  -  + ]:         156 :       if (shndx != i || GELF_ST_TYPE (sym->st_info) != STT_SECTION)
     402                 :          24 :         return i;
     403                 :             : 
     404                 :         132 :       sym->st_value = shdr->sh_addr;
     405         [ +  - ]:         132 :       if (sym->st_shndx != SHN_XINDEX)
     406                 :         132 :         shndx = SHN_UNDEF;
     407         [ -  + ]:         132 :       ELF_CHECK (gelf_update_symshndx (newsymdata, shndxdata, i, sym, shndx),
     408                 :             :                  _("cannot update symbol table: %s"));
     409                 :             :     }
     410                 :             : 
     411                 :             :   return shnum;
     412                 :             : }
     413                 :             : 
     414                 :             : static void
     415                 :      132866 : update_shdr (Elf_Scn *outscn, GElf_Shdr *newshdr)
     416                 :             : {
     417         [ -  + ]:      132866 :   ELF_CHECK (gelf_update_shdr (outscn, newshdr),
     418                 :             :              _("cannot update section header: %s"));
     419                 :      132866 : }
     420                 :             : 
     421                 :             : /* We expanded the output section, so update its header.  */
     422                 :             : static void
     423                 :          12 : update_sh_size (Elf_Scn *outscn, const Elf_Data *data)
     424                 :             : {
     425                 :          12 :   GElf_Shdr shdr_mem;
     426                 :          12 :   GElf_Shdr *newshdr = gelf_getshdr (outscn, &shdr_mem);
     427         [ -  + ]:          12 :   ELF_CHECK (newshdr != NULL, _("cannot get section header: %s"));
     428                 :             : 
     429                 :          12 :   newshdr->sh_size = data->d_size;
     430                 :             : 
     431                 :          12 :   update_shdr (outscn, newshdr);
     432                 :          12 : }
     433                 :             : 
     434                 :             : static inline void
     435                 :       49194 : adjust_reloc (GElf_Xword *info,
     436                 :             :               size_t map[], size_t map_size)
     437                 :             : {
     438                 :       49194 :   size_t ndx = GELF_R_SYM (*info);
     439         [ +  + ]:       49194 :   if (ndx != STN_UNDEF)
     440                 :             :     {
     441         [ -  + ]:       49182 :       if (ndx > map_size)
     442                 :           0 :         error_exit (0, "bad symbol ndx section");
     443                 :       49182 :       *info = GELF_R_INFO (map[ndx - 1], GELF_R_TYPE (*info));
     444                 :             :     }
     445                 :       49194 : }
     446                 :             : 
     447                 :             : /* Update relocation sections using the symbol table.  */
     448                 :             : static void
     449                 :         186 : adjust_relocs (Elf_Scn *outscn, Elf_Scn *inscn, const GElf_Shdr *shdr,
     450                 :             :                size_t map[], size_t map_size, const GElf_Shdr *symshdr)
     451                 :             : {
     452                 :         186 :   Elf_Data *data = elf_getdata (outscn, NULL);
     453                 :             : 
     454   [ +  +  +  +  :         186 :   switch (shdr->sh_type)
                   +  - ]
     455                 :             :     {
     456                 :          32 :     case SHT_REL:
     457         [ -  + ]:          32 :       if (shdr->sh_entsize == 0)
     458                 :           0 :         error_exit (0, "REL section cannot have zero sh_entsize");
     459                 :             : 
     460         [ +  + ]:        3736 :       for (size_t i = 0; i < shdr->sh_size / shdr->sh_entsize; ++i)
     461                 :             :         {
     462                 :        3704 :           GElf_Rel rel_mem;
     463                 :        3704 :           GElf_Rel *rel = gelf_getrel (data, i, &rel_mem);
     464         [ -  + ]:        3704 :           ELF_CHECK (rel != NULL, _("gelf_getrel failed: %s"));
     465                 :        3704 :           adjust_reloc (&rel->r_info, map, map_size);
     466         [ -  + ]:        3704 :           ELF_CHECK (gelf_update_rel (data, i, rel),
     467                 :             :                      _("cannot update relocation: %s"));
     468                 :             :         }
     469                 :             :       break;
     470                 :             : 
     471                 :         128 :     case SHT_RELA:
     472         [ -  + ]:         128 :       if (shdr->sh_entsize == 0)
     473                 :           0 :         error_exit (0, "RELA section cannot have zero sh_entsize");
     474                 :             : 
     475         [ +  + ]:       45618 :       for (size_t i = 0; i < shdr->sh_size / shdr->sh_entsize; ++i)
     476                 :             :         {
     477                 :       45490 :           GElf_Rela rela_mem;
     478                 :       45490 :           GElf_Rela *rela = gelf_getrela (data, i, &rela_mem);
     479         [ -  + ]:       45490 :           ELF_CHECK (rela != NULL, _("gelf_getrela failed: %s"));
     480                 :       45490 :           adjust_reloc (&rela->r_info, map, map_size);
     481         [ -  + ]:       45490 :           ELF_CHECK (gelf_update_rela (data, i, rela),
     482                 :             :                      _("cannot update relocation: %s"));
     483                 :             :         }
     484                 :             :       break;
     485                 :             : 
     486                 :          14 :     case SHT_GROUP:
     487                 :             :       {
     488                 :          14 :         GElf_Shdr shdr_mem;
     489                 :          14 :         GElf_Shdr *newshdr = gelf_getshdr (outscn, &shdr_mem);
     490         [ -  + ]:          14 :         ELF_CHECK (newshdr != NULL, _("cannot get section header: %s"));
     491         [ +  - ]:          14 :         if (newshdr->sh_info != STN_UNDEF)
     492                 :             :           {
     493                 :          14 :             newshdr->sh_info = map[newshdr->sh_info - 1];
     494                 :          14 :             update_shdr (outscn, newshdr);
     495                 :             :           }
     496                 :          14 :         break;
     497                 :             :       }
     498                 :             : 
     499                 :           6 :     case SHT_HASH:
     500                 :             :       /* We must expand the table and rejigger its contents.  */
     501                 :             :       {
     502         [ -  + ]:           6 :         if (shdr->sh_entsize == 0)
     503                 :           0 :           error_exit (0, "HASH section cannot have zero sh_entsize");
     504         [ -  + ]:           6 :         if (symshdr->sh_entsize == 0)
     505                 :           0 :           error_exit (0, "Symbol table cannot have zero sh_entsize");
     506                 :           6 :         const size_t nsym = symshdr->sh_size / symshdr->sh_entsize;
     507                 :           6 :         const size_t onent = shdr->sh_size / shdr->sh_entsize;
     508         [ -  + ]:           6 :         if (data->d_size != shdr->sh_size)
     509                 :           0 :           error_exit (0, "HASH section has inconsistent size");
     510                 :             : 
     511                 :             : #define CONVERT_HASH(Hash_Word)                                               \
     512                 :             :         {                                                                     \
     513                 :             :           const Hash_Word *const old_hash = data->d_buf;                   \
     514                 :             :           const size_t nbucket = old_hash[0];                                 \
     515                 :             :           const size_t nchain = old_hash[1];                                  \
     516                 :             :           const Hash_Word *const old_bucket = &old_hash[2];               \
     517                 :             :           const Hash_Word *const old_chain = &old_bucket[nbucket];        \
     518                 :             :           if (onent != 2 + nbucket + nchain)                                  \
     519                 :             :             error_exit (0, "HASH section has inconsistent entsize");        \
     520                 :             :                                                                               \
     521                 :             :           const size_t nent = 2 + nbucket + nsym;                             \
     522                 :             :           Hash_Word *const new_hash = xcalloc (nent, sizeof new_hash[0]);     \
     523                 :             :           Hash_Word *const new_bucket = &new_hash[2];                             \
     524                 :             :           Hash_Word *const new_chain = &new_bucket[nbucket];                      \
     525                 :             :                                                                               \
     526                 :             :           new_hash[0] = nbucket;                                              \
     527                 :             :           new_hash[1] = nsym;                                                 \
     528                 :             :           for (size_t i = 0; i < nbucket; ++i)                                     \
     529                 :             :             if (old_bucket[i] != STN_UNDEF)                                   \
     530                 :             :               new_bucket[i] = map[old_bucket[i] - 1];                         \
     531                 :             :                                                                               \
     532                 :             :           for (size_t i = 1; i < nchain; ++i)                                      \
     533                 :             :             if (old_chain[i] != STN_UNDEF)                                    \
     534                 :             :               new_chain[map[i - 1]] = map[old_chain[i] - 1];                  \
     535                 :             :                                                                               \
     536                 :             :           record_new_data (new_hash);                                   \
     537                 :             :           data->d_buf = new_hash;                                          \
     538                 :             :           data->d_size = nent * sizeof new_hash[0];                        \
     539                 :             :         }
     540                 :             : 
     541      [ +  -  - ]:           6 :         switch (shdr->sh_entsize)
     542                 :             :           {
     543                 :           6 :           case 4:
     544   [ -  +  +  +  :         432 :             CONVERT_HASH (Elf32_Word);
          +  +  +  +  +  
                      + ]
     545                 :           6 :             break;
     546                 :           0 :           case 8:
     547   [ #  #  #  #  :           0 :             CONVERT_HASH (Elf64_Xword);
          #  #  #  #  #  
                      # ]
     548                 :           0 :             break;
     549                 :           0 :           default:
     550                 :           0 :             abort ();
     551                 :             :           }
     552                 :             : 
     553                 :           6 :         elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY);
     554                 :           6 :         update_sh_size (outscn, data);
     555                 :             : 
     556                 :             : #undef  CONVERT_HASH
     557                 :             :       }
     558                 :           6 :       break;
     559                 :             : 
     560                 :           6 :     case SHT_GNU_versym:
     561                 :             :       /* We must expand the table and move its elements around.  */
     562                 :             :       {
     563         [ -  + ]:           6 :         if (shdr->sh_entsize == 0)
     564                 :           0 :           error_exit (0, "GNU_versym section cannot have zero sh_entsize");
     565         [ -  + ]:           6 :         if (symshdr->sh_entsize == 0)
     566                 :           0 :           error_exit (0, "Symbol table cannot have zero sh_entsize");
     567                 :           6 :         const size_t nent = symshdr->sh_size / symshdr->sh_entsize;
     568                 :           6 :         const size_t onent = shdr->sh_size / shdr->sh_entsize;
     569         [ -  + ]:           6 :         assert (nent >= onent);
     570                 :             : 
     571                 :             :         /* We don't bother using gelf_update_versym because there is
     572                 :             :            really no conversion to be done.  */
     573                 :           6 :         eu_static_assert (sizeof (Elf32_Versym) == sizeof (GElf_Versym));
     574                 :           6 :         eu_static_assert (sizeof (Elf64_Versym) == sizeof (GElf_Versym));
     575                 :           6 :         GElf_Versym *versym = xcalloc (nent, sizeof versym[0]);
     576                 :             : 
     577         [ +  + ]:         210 :         for (size_t i = 1; i < onent; ++i)
     578                 :             :           {
     579                 :         204 :             GElf_Versym *v = gelf_getversym (data, i, &versym[map[i - 1]]);
     580         [ -  + ]:         204 :             ELF_CHECK (v != NULL, _("cannot get symbol version: %s"));
     581                 :             :           }
     582                 :             : 
     583                 :           6 :         record_new_data (versym);
     584                 :           6 :         data->d_buf = versym;
     585                 :           6 :         data->d_size = nent * sizeof versym[0];
     586                 :           6 :         elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY);
     587                 :           6 :         update_sh_size (outscn, data);
     588                 :             :       }
     589                 :           6 :       break;
     590                 :             : 
     591                 :           0 :     default:
     592                 :           0 :       error_exit (0,
     593                 :             :                   _("unexpected section type in [%zu] with sh_link to symtab"),
     594                 :             :                   elf_ndxscn (inscn));
     595                 :             :     }
     596                 :         186 : }
     597                 :             : 
     598                 :             : /* Adjust all the relocation sections in the file.  */
     599                 :             : static void
     600                 :          26 : adjust_all_relocs (Elf *elf, Elf_Scn *symtab, const GElf_Shdr *symshdr,
     601                 :             :                    size_t map[], size_t map_size, bool scn_filter[])
     602                 :             : {
     603                 :          26 :   size_t new_sh_link = elf_ndxscn (symtab);
     604                 :          26 :   Elf_Scn *scn = NULL;
     605         [ +  + ]:         876 :   while ((scn = elf_nextscn (elf, scn)) != NULL)
     606         [ +  + ]:         850 :     if (scn != symtab)
     607                 :             :       {
     608         [ +  + ]:         824 :         if (scn_filter != NULL)
     609                 :             :           {
     610                 :         632 :             size_t ndx = elf_ndxscn (scn);
     611                 :             : 
     612                 :             :             /* Skip relocations that were already mapped during adjust_relocs
     613                 :             :                for the stripped symtab.  This is to avoid mapping a relocation's
     614                 :             :                symbol index from X to Y during the first adjust_relocs and then
     615                 :             :                wrongly remapping it from Y to Z during the second call.  */
     616         [ +  + ]:         632 :             if (scn_filter[ndx])
     617                 :          94 :               continue;
     618                 :             :           }
     619                 :             : 
     620                 :         730 :         GElf_Shdr shdr_mem;
     621                 :         730 :         GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
     622         [ -  + ]:         730 :         ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
     623                 :             : 
     624   [ +  +  +  + ]:         730 :         if (shdr->sh_type != SHT_NOBITS && shdr->sh_link == new_sh_link)
     625                 :          92 :           adjust_relocs (scn, scn, shdr, map, map_size, symshdr);
     626                 :             :       }
     627                 :          26 : }
     628                 :             : 
     629                 :             : /* Return true if SHDR, a symbol table section of ELF, has an sh_entsize
     630                 :             :    equal to the size of one symbol and an sh_size of at least one entry.
     631                 :             :    libelf bounds symbol accesses by sh_size divided by the symbol size,
     632                 :             :    so only then does sh_size / sh_entsize count the symbols it can access.  */
     633                 :             : static bool
     634                 :          74 : valid_symtab_entsize (Elf *elf, const GElf_Shdr *shdr)
     635                 :             : {
     636                 :          74 :   return (shdr->sh_entsize == gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT)
     637   [ +  +  +  + ]:          74 :           && shdr->sh_size >= shdr->sh_entsize);
     638                 :             : }
     639                 :             : 
     640                 :             : /* The original file probably had section symbols for all of its
     641                 :             :    sections, even the unallocated ones.  To match it as closely as
     642                 :             :    possible, add in section symbols for the added sections.  */
     643                 :             : static Elf_Data *
     644                 :           6 : add_new_section_symbols (Elf_Scn *old_symscn, size_t old_shnum,
     645                 :             :                          Elf *elf, bool rel, Elf_Scn *symscn, size_t shnum)
     646                 :           6 : {
     647                 :           6 :   const size_t added = shnum - old_shnum;
     648                 :             : 
     649                 :           6 :   GElf_Shdr shdr_mem;
     650                 :           6 :   GElf_Shdr *shdr = gelf_getshdr (symscn, &shdr_mem);
     651         [ -  + ]:           6 :   ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
     652   [ +  -  -  + ]:           6 :   if (shdr->sh_entsize == 0 || shdr->sh_size < shdr->sh_entsize)
     653                 :           0 :     error_exit (0, _("Symbol table section cannot have zero sh_entsize"
     654                 :             :                      " or a sh_size smaller than sh_entsize"));
     655                 :             : 
     656                 :           6 :   const size_t nsym = shdr->sh_size / shdr->sh_entsize;
     657                 :             :   /* The loop below writes symndx_map[i - 1] for each i < old_shnum, so
     658                 :             :      old_shnum must not exceed nsym.  */
     659         [ -  + ]:           6 :   if (old_shnum > nsym)
     660                 :           0 :     error_exit (0, _("SYMTAB section has too few symbols"));
     661                 :           6 :   size_t symndx_map[nsym - 1];
     662                 :             : 
     663                 :           6 :   shdr->sh_info += added;
     664                 :           6 :   shdr->sh_size += added * shdr->sh_entsize;
     665                 :           6 :   update_shdr (symscn, shdr);
     666                 :             : 
     667                 :           6 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
     668                 :           6 :   Elf_Data *shndxdata = NULL;   /* XXX */
     669                 :             : 
     670                 :           6 :   symdata->d_size = shdr->sh_size;
     671                 :           6 :   symdata->d_buf = xmalloc (symdata->d_size);
     672                 :           6 :   record_new_data (symdata->d_buf);
     673                 :             : 
     674                 :             :   /* Copy the existing section symbols.  */
     675                 :           6 :   Elf_Data *old_symdata = elf_getdata (old_symscn, NULL);
     676         [ +  + ]:         144 :   for (size_t i = 0; i < old_shnum; ++i)
     677                 :             :     {
     678                 :         138 :       GElf_Sym sym_mem;
     679                 :         138 :       GElf_Word shndx = SHN_UNDEF;
     680                 :         138 :       GElf_Sym *sym = gelf_getsymshndx (old_symdata, shndxdata,
     681                 :             :                                         i, &sym_mem, &shndx);
     682         [ -  + ]:         138 :       ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s"));
     683         [ -  + ]:         138 :       ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, i,
     684                 :             :                                        sym, shndx),
     685                 :             :                  _("cannot update symbol table: %s"));
     686                 :             : 
     687         [ +  + ]:         138 :       if (i > 0)
     688                 :         132 :         symndx_map[i - 1] = i;
     689                 :             :     }
     690                 :             : 
     691                 :             :   /* Add in the new section symbols.  */
     692         [ +  + ]:          54 :   for (size_t i = old_shnum; i < shnum; ++i)
     693                 :             :     {
     694                 :          48 :       GElf_Shdr i_shdr_mem;
     695                 :          48 :       GElf_Shdr *i_shdr = gelf_getshdr (elf_getscn (elf, i), &i_shdr_mem);
     696         [ -  + ]:          48 :       ELF_CHECK (i_shdr != NULL, _("cannot get section header: %s"));
     697         [ +  - ]:          48 :       GElf_Sym sym =
     698                 :             :         {
     699         [ +  - ]:          48 :           .st_value = rel ? 0 : i_shdr->sh_addr,
     700                 :             :           .st_info = GELF_ST_INFO (STB_LOCAL, STT_SECTION),
     701                 :             :           .st_shndx = i < SHN_LORESERVE ? i : SHN_XINDEX
     702                 :             :         };
     703         [ -  + ]:          48 :       GElf_Word shndx = i < SHN_LORESERVE ? SHN_UNDEF : i;
     704         [ -  + ]:          48 :       ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, i,
     705                 :             :                                        &sym, shndx),
     706                 :             :                  _("cannot update symbol table: %s"));
     707                 :             :     }
     708                 :             : 
     709                 :             :   /* Now copy the rest of the existing symbols.  */
     710         [ +  + ]:          78 :   for (size_t i = old_shnum; i < nsym; ++i)
     711                 :             :     {
     712                 :          72 :       GElf_Sym sym_mem;
     713                 :          72 :       GElf_Word shndx = SHN_UNDEF;
     714                 :          72 :       GElf_Sym *sym = gelf_getsymshndx (old_symdata, shndxdata,
     715                 :             :                                         i, &sym_mem, &shndx);
     716         [ -  + ]:          72 :       ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s"));
     717         [ -  + ]:          72 :       ELF_CHECK (gelf_update_symshndx (symdata, shndxdata,
     718                 :             :                                        i + added, sym, shndx),
     719                 :             :                  _("cannot update symbol table: %s"));
     720                 :             : 
     721                 :          72 :       symndx_map[i - 1] = i + added;
     722                 :             :     }
     723                 :             : 
     724                 :             :   /* Adjust any relocations referring to the old symbol table.  */
     725                 :           6 :   adjust_all_relocs (elf, symscn, shdr, symndx_map, nsym - 1, NULL);
     726                 :             : 
     727                 :           6 :   return symdata;
     728                 :             : }
     729                 :             : 
     730                 :             : /* This has the side effect of updating STT_SECTION symbols' values,
     731                 :             :    in case of prelink adjustments.  */
     732                 :             : static Elf_Data *
     733                 :          24 : check_symtab_section_symbols (Elf *elf, bool rel, Elf_Scn *scn,
     734                 :             :                               size_t shnum, size_t shstrndx,
     735                 :             :                               Elf_Scn *oscn, size_t oshnum, size_t oshstrndx,
     736                 :             :                               size_t debuglink)
     737                 :             : {
     738                 :          24 :   size_t n = symtab_count_leading_section_symbols (elf, oscn, oshnum,
     739                 :             :                                                    elf_getdata (scn, NULL));
     740                 :             : 
     741         [ -  + ]:          24 :   if (n == oshnum)
     742                 :           0 :     return add_new_section_symbols (oscn, n, elf, rel, scn, shnum);
     743                 :             : 
     744   [ +  -  +  +  :          24 :   if (n == oshstrndx || (n == debuglink && n == oshstrndx - 1))
                   +  - ]
     745                 :           6 :     return add_new_section_symbols (oscn, n, elf, rel, scn, shstrndx);
     746                 :             : 
     747                 :             :   return NULL;
     748                 :             : }
     749                 :             : 
     750                 :             : struct section
     751                 :             : {
     752                 :             :   Elf_Scn *scn;
     753                 :             :   const char *name;
     754                 :             :   const char *sig;
     755                 :             :   Elf_Scn *outscn;
     756                 :             :   Dwelf_Strent *strent;
     757                 :             :   GElf_Shdr shdr;
     758                 :             : };
     759                 :             : 
     760                 :             : static int
     761                 :        1556 : compare_alloc_sections (const struct section *s1, const struct section *s2,
     762                 :             :                         bool rel)
     763                 :             : {
     764         [ +  + ]:        1556 :   if (!rel)
     765                 :             :     {
     766                 :             :       /* Sort by address.  */
     767         [ +  + ]:        1272 :       if (s1->shdr.sh_addr < s2->shdr.sh_addr)
     768                 :             :         return -1;
     769         [ +  - ]:           4 :       if (s1->shdr.sh_addr > s2->shdr.sh_addr)
     770                 :             :         return 1;
     771                 :             :     }
     772                 :             : 
     773                 :             :   /* At the same address, preserve original section order.  */
     774                 :         288 :   return (ssize_t) elf_ndxscn (s1->scn) - (ssize_t) elf_ndxscn (s2->scn);
     775                 :             : }
     776                 :             : 
     777                 :             : static int
     778                 :      395414 : compare_unalloc_sections (const GElf_Shdr *shdr1, const GElf_Shdr *shdr2,
     779                 :             :                           const char *name1, const char *name2,
     780                 :             :                           const char *sig1, const char *sig2)
     781                 :             : {
     782                 :             :   /* Sort by sh_flags as an arbitrary ordering.  */
     783         [ +  + ]:      395414 :   if (shdr1->sh_flags < shdr2->sh_flags)
     784                 :             :     return -1;
     785         [ +  + ]:      395196 :   if (shdr1->sh_flags > shdr2->sh_flags)
     786                 :             :     return 1;
     787                 :             : 
     788                 :             :   /* Sizes should be the same.  */
     789         [ +  + ]:      394838 :   if (shdr1->sh_size < shdr2->sh_size)
     790                 :             :     return -1;
     791         [ +  + ]:         994 :   if (shdr1->sh_size > shdr2->sh_size)
     792                 :             :     return 1;
     793                 :             : 
     794                 :             :   /* Are they both SHT_GROUP sections? Then compare signatures.  */
     795         [ +  + ]:         226 :   if (sig1 != NULL && sig2 != NULL)
     796                 :          22 :     return strcmp (sig1, sig2);
     797                 :             : 
     798                 :             :   /* Sort by name as last resort.  */
     799                 :         204 :   return strcmp (name1, name2);
     800                 :             : }
     801                 :             : 
     802                 :             : static int
     803                 :        2574 : compare_sections (const void *a, const void *b, bool rel)
     804                 :             : {
     805                 :        2574 :   const struct section *s1 = a;
     806                 :        2574 :   const struct section *s2 = b;
     807                 :             : 
     808                 :             :   /* Sort all non-allocated sections last.  */
     809         [ +  + ]:        2574 :   if ((s1->shdr.sh_flags ^ s2->shdr.sh_flags) & SHF_ALLOC)
     810         [ +  + ]:         402 :     return (s1->shdr.sh_flags & SHF_ALLOC) ? -1 : 1;
     811                 :             : 
     812                 :        2172 :   return ((s1->shdr.sh_flags & SHF_ALLOC)
     813                 :        1556 :           ? compare_alloc_sections (s1, s2, rel)
     814         [ +  + ]:        2172 :           : compare_unalloc_sections (&s1->shdr, &s2->shdr,
     815                 :         616 :                                       s1->name, s2->name,
     816                 :         616 :                                       s1->sig, s2->sig));
     817                 :             : }
     818                 :             : 
     819                 :             : static int
     820                 :        1144 : compare_sections_rel (const void *a, const void *b)
     821                 :             : {
     822                 :        1144 :   return compare_sections (a, b, true);
     823                 :             : }
     824                 :             : 
     825                 :             : static int
     826                 :        1430 : compare_sections_nonrel (const void *a, const void *b)
     827                 :             : {
     828                 :        1430 :   return compare_sections (a, b, false);
     829                 :             : }
     830                 :             : 
     831                 :             : 
     832                 :             : struct symbol
     833                 :             : {
     834                 :             :   size_t *map;
     835                 :             : 
     836                 :             :   union
     837                 :             :   {
     838                 :             :     const char *name;
     839                 :             :     Dwelf_Strent *strent;
     840                 :             :   };
     841                 :             :   union
     842                 :             :   {
     843                 :             :     struct
     844                 :             :     {
     845                 :             :       GElf_Addr value;
     846                 :             :       GElf_Xword size;
     847                 :             :       GElf_Word shndx;
     848                 :             :       union
     849                 :             :       {
     850                 :             :         struct
     851                 :             :         {
     852                 :             :           uint8_t info;
     853                 :             :           uint8_t other;
     854                 :             :         } info;
     855                 :             :         int16_t compare;
     856                 :             :       };
     857                 :             :     };
     858                 :             : 
     859                 :             :     /* For a symbol discarded after first sort, this matches its better's
     860                 :             :        map pointer.  */
     861                 :             :     size_t *duplicate;
     862                 :             :   };
     863                 :             : };
     864                 :             : 
     865                 :             : /* Collect input symbols into our internal form.  */
     866                 :             : static void
     867                 :          40 : collect_symbols (Elf *outelf, bool rel, Elf_Scn *symscn, Elf_Scn *strscn,
     868                 :             :                  const size_t nent, const GElf_Addr bias,
     869                 :             :                  const size_t scnmap[], struct symbol *table, size_t *map,
     870                 :             :                  struct section *split_bss)
     871                 :             : {
     872                 :          40 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
     873         [ -  + ]:          40 :   ELF_CHECK (symdata != NULL, _("cannot get symbol section data: %s"));
     874                 :          40 :   Elf_Data *strdata = elf_getdata (strscn, NULL);
     875         [ -  + ]:          40 :   ELF_CHECK (strdata != NULL, _("cannot get string section data: %s"));
     876                 :             :   Elf_Data *shndxdata = NULL;   /* XXX */
     877                 :             : 
     878         [ +  + ]:        3474 :   for (size_t i = 1; i < nent; ++i)
     879                 :             :     {
     880                 :        3434 :       GElf_Sym sym_mem;
     881                 :        3434 :       GElf_Word shndx = SHN_UNDEF;
     882                 :        3434 :       GElf_Sym *sym = gelf_getsymshndx (symdata, shndxdata, i,
     883                 :             :                                         &sym_mem, &shndx);
     884         [ -  + ]:        3434 :       ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s"));
     885         [ +  - ]:        3434 :       if (sym->st_shndx != SHN_XINDEX)
     886                 :        3434 :         shndx = sym->st_shndx;
     887                 :             : 
     888         [ +  - ]:        3434 :       if (sym->st_name >= strdata->d_size
     889         [ -  + ]:        3434 :           || memrchr (strdata->d_buf + sym->st_name, '\0',
     890                 :             :                       strdata->d_size - sym->st_name) == NULL)
     891                 :           0 :         error_exit (0,
     892                 :             :                     _("invalid string offset in symbol [%zu]"), i);
     893                 :             : 
     894                 :        3434 :       struct symbol *s = &table[i - 1];
     895                 :        3434 :       s->map = &map[i - 1];
     896                 :        3434 :       s->name = strdata->d_buf + sym->st_name;
     897                 :        3434 :       s->value = sym->st_value + bias;
     898                 :        3434 :       s->size = sym->st_size;
     899                 :        3434 :       s->shndx = shndx;
     900                 :        3434 :       s->info.info = sym->st_info;
     901                 :        3434 :       s->info.other = sym->st_other;
     902                 :        3434 :       s->duplicate = NULL;
     903                 :             : 
     904   [ +  +  +  +  :        3434 :       if (scnmap != NULL && shndx != SHN_UNDEF && shndx < SHN_LORESERVE)
                   +  + ]
     905                 :         766 :         s->shndx = scnmap[shndx - 1];
     906                 :             : 
     907   [ +  +  +  + ]:        3434 :       if (GELF_ST_TYPE (s->info.info) == STT_SECTION && !rel)
     908                 :         228 :         {
     909                 :             :           /* Update the value to match the output section.  */
     910                 :         228 :           GElf_Shdr shdr_mem;
     911                 :         228 :           GElf_Shdr *shdr = gelf_getshdr (elf_getscn (outelf, s->shndx),
     912                 :             :                                           &shdr_mem);
     913         [ -  + ]:         228 :           ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
     914                 :         228 :           s->value = shdr->sh_addr;
     915                 :             :         }
     916         [ -  + ]:        3206 :       else if (split_bss != NULL
     917         [ #  # ]:           0 :                && s->value < split_bss->shdr.sh_addr
     918         [ #  # ]:           0 :                && s->value >= split_bss[-1].shdr.sh_addr
     919         [ #  # ]:           0 :                && shndx == elf_ndxscn (split_bss->outscn))
     920                 :             :         /* This symbol was in .bss and was split into .dynbss.  */
     921                 :           0 :         s->shndx = elf_ndxscn (split_bss[-1].outscn);
     922                 :             :     }
     923                 :          40 : }
     924                 :             : 
     925                 :             : 
     926                 :             : #define CMP(value)                                                            \
     927                 :             :   if (s1->value < s2->value)                                                 \
     928                 :             :     return -1;                                                                \
     929                 :             :   if (s1->value > s2->value)                                                 \
     930                 :             :     return 1
     931                 :             : 
     932                 :             : /* Symbol comparison used to sort symbols in preparation for deduplication.  */
     933                 :             : static int
     934                 :       21210 : compare_symbols (const void *a, const void *b)
     935                 :             : {
     936                 :       21210 :   const struct symbol *s1 = a;
     937                 :       21210 :   const struct symbol *s2 = b;
     938                 :             : 
     939   [ +  +  +  + ]:       21210 :   CMP (value);
     940   [ +  +  +  + ]:       20362 :   CMP (size);
     941   [ +  +  +  + ]:       15794 :   CMP (shndx);
     942                 :             : 
     943                 :       10684 :   int res = s1->compare - s2->compare;
     944         [ +  + ]:       10684 :   if (res != 0)
     945                 :             :     return res;
     946                 :             : 
     947                 :       10016 :   res = strcmp (s1->name, s2->name);
     948         [ +  + ]:       10016 :   if (res != 0)
     949                 :             :     return res;
     950                 :             : 
     951                 :             :   /* Duplicates still have distinct positions in the symbol index map.
     952                 :             :      Compare map positions to ensure that duplicate symbols are ordered
     953                 :             :      consistently even if the sort function is unstable.  */
     954   [ -  +  -  - ]:        1464 :   CMP (map);
     955                 :           0 :   error_exit (0, _("found two identical index map positions."));
     956                 :             : }
     957                 :             : 
     958                 :             : /* Symbol comparison used to deduplicate symbols found in both the stripped
     959                 :             :    and unstripped input files.
     960                 :             : 
     961                 :             :    Similar to compare_symbols, but does not differentiate symbols based
     962                 :             :    on their position in the symbol index map.  Duplicates can't be found
     963                 :             :    by comparing index map postions because they always have distinct
     964                 :             :    positions in the map.  */
     965                 :             : static int
     966                 :        3410 : compare_symbols_duplicate (const void *a, const void *b)
     967                 :             : {
     968                 :        3410 :   const struct symbol *s1 = a;
     969                 :        3410 :   const struct symbol *s2 = b;
     970                 :             : 
     971   [ +  +  +  - ]:        3410 :   CMP (value);
     972   [ +  +  +  - ]:        3310 :   CMP (size);
     973   [ +  +  +  - ]:        2970 :   CMP (shndx);
     974                 :             : 
     975         [ +  + ]:        2554 :   return (s1->compare - s2->compare) ?: strcmp (s1->name, s2->name);
     976                 :             : }
     977                 :             : 
     978                 :             : /* Compare symbols for output order after slots have been assigned.  */
     979                 :             : static int
     980                 :       22238 : compare_symbols_output (const void *a, const void *b)
     981                 :             : {
     982                 :       22238 :   const struct symbol *s1 = a;
     983                 :       22238 :   const struct symbol *s2 = b;
     984                 :       22238 :   int cmp;
     985                 :             : 
     986                 :             :   /* Sort discarded symbols last.  */
     987                 :       22238 :   cmp = (s1->name == NULL) - (s2->name == NULL);
     988                 :             : 
     989         [ +  + ]:       22238 :   if (cmp == 0)
     990                 :             :     /* Local symbols must come first.  */
     991                 :       18266 :     cmp = ((GELF_ST_BIND (s2->info.info) == STB_LOCAL)
     992                 :       18266 :            - (GELF_ST_BIND (s1->info.info) == STB_LOCAL));
     993                 :             : 
     994         [ +  + ]:       18266 :   if (cmp == 0)
     995                 :             :     /* binutils always puts section symbols first.  */
     996                 :       16260 :     cmp = ((GELF_ST_TYPE (s2->info.info) == STT_SECTION)
     997                 :       16260 :            - (GELF_ST_TYPE (s1->info.info) == STT_SECTION));
     998                 :             : 
     999         [ +  + ]:       20232 :   if (cmp == 0)
    1000                 :             :     {
    1001         [ +  + ]:       14648 :       if (GELF_ST_TYPE (s1->info.info) == STT_SECTION)
    1002                 :             :         {
    1003                 :             :           /* binutils always puts section symbols in section index order.  */
    1004   [ +  +  -  + ]:        1692 :           CMP (shndx);
    1005         [ #  # ]:           0 :           else if (s1 != s2)
    1006                 :           0 :             error_exit (0, _("section symbols in unexpected order"));
    1007                 :             :         }
    1008                 :             : 
    1009                 :             :       /* Nothing really matters, so preserve the original order.  */
    1010   [ +  +  -  + ]:       12956 :       CMP (map);
    1011         [ #  # ]:           0 :       else if (s1 != s2)
    1012                 :           0 :         error_exit (0, _("found two identical symbols"));
    1013                 :             :     }
    1014                 :             : 
    1015                 :             :   return cmp;
    1016                 :             : }
    1017                 :             : 
    1018                 :             : #undef CMP
    1019                 :             : 
    1020                 :             : /* Return true if the flags of the sections match, ignoring the SHF_INFO_LINK
    1021                 :             :    flag if the section contains relocation information.  */
    1022                 :             : static bool
    1023                 :         704 : sections_flags_match (Elf64_Xword sh_flags1, Elf64_Xword sh_flags2,
    1024                 :             :                       Elf64_Word sh_type)
    1025                 :             : {
    1026                 :         704 :   if (sh_type == SHT_REL || sh_type == SHT_RELA)
    1027                 :             :     {
    1028                 :          46 :       sh_flags1 &= ~SHF_INFO_LINK;
    1029                 :          46 :       sh_flags2 &= ~SHF_INFO_LINK;
    1030                 :             :     }
    1031                 :             : 
    1032                 :         704 :   return sh_flags1 == sh_flags2;
    1033                 :             : }
    1034                 :             : 
    1035                 :             : /* Return true iff the flags, size, and name match.  */
    1036                 :             : static bool
    1037                 :         704 : sections_match (const struct section *sections, size_t i,
    1038                 :             :                 const GElf_Shdr *shdr, const char *name)
    1039                 :             : {
    1040                 :        1408 :   return (sections_flags_match (sections[i].shdr.sh_flags, shdr->sh_flags,
    1041         [ +  + ]:         704 :                                 sections[i].shdr.sh_type)
    1042         [ +  + ]:         700 :           && (sections[i].shdr.sh_size == shdr->sh_size
    1043         [ -  + ]:          14 :               || (sections[i].shdr.sh_size < shdr->sh_size
    1044         [ -  + ]:          14 :                   && section_can_shrink (&sections[i].shdr)))
    1045   [ +  +  -  + ]:        1404 :           && !strcmp (sections[i].name, name));
    1046                 :             : }
    1047                 :             : 
    1048                 :             : /* Locate a matching allocated section in SECTIONS.  */
    1049                 :             : static struct section *
    1050                 :         546 : find_alloc_section (const GElf_Shdr *shdr, GElf_Addr bias, const char *name,
    1051                 :             :                     struct section sections[], size_t nalloc)
    1052                 :             : {
    1053                 :         546 :   const GElf_Addr addr = shdr->sh_addr + bias;
    1054                 :         546 :   size_t l = 0, u = nalloc;
    1055         [ +  - ]:        2652 :   while (l < u)
    1056                 :             :     {
    1057                 :        2106 :       size_t i = (l + u) / 2;
    1058         [ +  + ]:        2106 :       if (addr < sections[i].shdr.sh_addr)
    1059                 :             :         u = i;
    1060         [ +  + ]:        1216 :       else if (addr > sections[i].shdr.sh_addr)
    1061                 :         670 :         l = i + 1;
    1062                 :             :       else
    1063                 :             :         {
    1064                 :             :           /* We've found allocated sections with this address.
    1065                 :             :              Find one with matching size, flags, and name.  */
    1066   [ +  +  +  + ]:         554 :           while (i > 0 && sections[i - 1].shdr.sh_addr == addr)
    1067                 :           8 :             --i;
    1068   [ +  -  +  - ]:         550 :           for (; i < nalloc && sections[i].shdr.sh_addr == addr;
    1069                 :           4 :                ++i)
    1070         [ +  + ]:         550 :             if (sections_match (sections, i, shdr, name))
    1071                 :             :               return &sections[i];
    1072                 :             :           break;
    1073                 :             :         }
    1074                 :             :     }
    1075                 :             :   return NULL;
    1076                 :             : }
    1077                 :             : 
    1078                 :             : static inline const char *
    1079                 :      132392 : get_section_name (size_t ndx, const GElf_Shdr *shdr, const Elf_Data *shstrtab)
    1080                 :             : {
    1081         [ -  + ]:      132392 :   if (shdr->sh_name >= shstrtab->d_size)
    1082                 :           0 :     error_exit (0, _("cannot read section [%zu] name: %s"),
    1083                 :             :                 ndx, elf_errmsg (-1));
    1084                 :      132392 :   return shstrtab->d_buf + shdr->sh_name;
    1085                 :             : }
    1086                 :             : 
    1087                 :             : /* Returns the signature of a group section, or NULL if the given
    1088                 :             :    section isn't a group.  */
    1089                 :             : static const char *
    1090                 :      132614 : get_group_sig (Elf *elf, GElf_Shdr *shdr)
    1091                 :             : {
    1092         [ +  + ]:      132614 :   if (shdr->sh_type != SHT_GROUP)
    1093                 :             :     return NULL;
    1094                 :             : 
    1095                 :          28 :   Elf_Scn *symscn = elf_getscn (elf, shdr->sh_link);
    1096         [ -  + ]:          28 :   if (symscn == NULL)
    1097                 :           0 :     error_exit (0, _("bad sh_link for group section: %s"),
    1098                 :             :                 elf_errmsg (-1));
    1099                 :             : 
    1100                 :          28 :   GElf_Shdr symshdr_mem;
    1101                 :          28 :   GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
    1102         [ -  + ]:          28 :   if (symshdr == NULL)
    1103                 :           0 :     error_exit (0, _("couldn't get shdr for group section: %s"),
    1104                 :             :                 elf_errmsg (-1));
    1105                 :             : 
    1106                 :          28 :   Elf_Data *symdata = elf_getdata (symscn, NULL);
    1107         [ -  + ]:          28 :   if (symdata == NULL)
    1108                 :           0 :     error_exit (0, _("bad data for group symbol section: %s"),
    1109                 :             :                 elf_errmsg (-1));
    1110                 :             : 
    1111                 :          28 :   GElf_Sym sym_mem;
    1112                 :          28 :   GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
    1113         [ -  + ]:          28 :   if (sym == NULL)
    1114                 :           0 :     error_exit (0, _("couldn't get symbol for group section: %s"),
    1115                 :             :                 elf_errmsg (-1));
    1116                 :             : 
    1117                 :          28 :   const char *sig = elf_strptr (elf, symshdr->sh_link, sym->st_name);
    1118         [ -  + ]:          28 :   if (sig == NULL)
    1119                 :           0 :     error_exit (0, _("bad symbol name for group section: %s"),
    1120                 :             :                 elf_errmsg (-1));
    1121                 :             : 
    1122                 :             :   return sig;
    1123                 :             : }
    1124                 :             : 
    1125                 :             : static inline bool
    1126                 :           0 : check_match (bool match, Elf_Scn *scn, const char *name)
    1127                 :             : {
    1128         [ #  # ]:           0 :   if (!match)
    1129                 :             :     {
    1130                 :           0 :       error (0, 0, _("cannot find matching section for [%zu] '%s'"),
    1131                 :             :              elf_ndxscn (scn), name);
    1132                 :           0 :       return true;
    1133                 :             :     }
    1134                 :             : 
    1135                 :             :   return false;
    1136                 :             : }
    1137                 :             : 
    1138                 :             : 
    1139                 :             : /* Fix things up when prelink has moved some allocated sections around
    1140                 :             :    and the debuginfo file's section headers no longer match up.
    1141                 :             :    This fills in SECTIONS[0..NALLOC-1].outscn or exits.
    1142                 :             :    If there was a .bss section that was split into two sections
    1143                 :             :    with the new one preceding it in sh_addr, we return that pointer.  */
    1144                 :             : static struct section *
    1145                 :           0 : find_alloc_sections_prelink (Elf *debug, Elf_Data *debug_shstrtab,
    1146                 :             :                              Elf *main, const GElf_Ehdr *main_ehdr,
    1147                 :             :                              Elf_Data *main_shstrtab, GElf_Addr bias,
    1148                 :             :                              struct section *sections,
    1149                 :             :                              size_t nalloc, size_t nsections)
    1150                 :             : {
    1151                 :           0 :   Elf_Scn *undo = NULL;
    1152         [ #  # ]:           0 :   for (size_t i = nalloc; i < nsections; ++i)
    1153                 :             :     {
    1154                 :           0 :       const struct section *sec = &sections[i];
    1155         [ #  # ]:           0 :       if (sec->shdr.sh_type == SHT_PROGBITS
    1156         [ #  # ]:           0 :           && !(sec->shdr.sh_flags & SHF_ALLOC)
    1157         [ #  # ]:           0 :           && !strcmp (sec->name, ".gnu.prelink_undo"))
    1158                 :             :         {
    1159                 :           0 :           undo = sec->scn;
    1160                 :           0 :           break;
    1161                 :             :         }
    1162                 :             :     }
    1163                 :             : 
    1164                 :             :   /* Find the original allocated sections before prelinking.  */
    1165                 :           0 :   struct section *undo_sections = NULL;
    1166                 :           0 :   size_t undo_nalloc = 0;
    1167         [ #  # ]:           0 :   if (undo != NULL)
    1168                 :             :     {
    1169                 :             :       /* Clear assignments that might have been bogus.  */
    1170         [ #  # ]:           0 :       for (size_t i = 0; i < nalloc; ++i)
    1171                 :           0 :         sections[i].outscn = NULL;
    1172                 :             : 
    1173                 :           0 :       Elf_Data *undodata = elf_rawdata (undo, NULL);
    1174         [ #  # ]:           0 :       ELF_CHECK (undodata != NULL,
    1175                 :             :                  _("cannot read '.gnu.prelink_undo' section: %s"));
    1176                 :             : 
    1177                 :           0 :       union
    1178                 :             :       {
    1179                 :             :         Elf32_Ehdr e32;
    1180                 :             :         Elf64_Ehdr e64;
    1181                 :             :       } ehdr;
    1182                 :           0 :       Elf_Data dst =
    1183                 :             :         {
    1184                 :             :           .d_buf = &ehdr,
    1185                 :             :           .d_size = sizeof ehdr,
    1186                 :             :           .d_type = ELF_T_EHDR,
    1187                 :             :           .d_version = EV_CURRENT
    1188                 :             :         };
    1189                 :           0 :       Elf_Data src = *undodata;
    1190                 :           0 :       src.d_size = gelf_fsize (main, ELF_T_EHDR, 1, EV_CURRENT);
    1191                 :           0 :       src.d_type = ELF_T_EHDR;
    1192         [ #  # ]:           0 :       ELF_CHECK (gelf_xlatetom (main, &dst, &src,
    1193                 :             :                                 main_ehdr->e_ident[EI_DATA]) != NULL,
    1194                 :             :                  _("cannot read '.gnu.prelink_undo' section: %s"));
    1195                 :             : 
    1196                 :           0 :       uint_fast16_t phnum;
    1197                 :           0 :       uint_fast16_t shnum;  /* prelink doesn't handle > SHN_LORESERVE.  */
    1198         [ #  # ]:           0 :       if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
    1199                 :             :         {
    1200                 :           0 :           phnum = ehdr.e32.e_phnum;
    1201                 :           0 :           shnum = ehdr.e32.e_shnum;
    1202                 :             :         }
    1203                 :             :       else
    1204                 :             :         {
    1205                 :           0 :           phnum = ehdr.e64.e_phnum;
    1206                 :           0 :           shnum = ehdr.e64.e_shnum;
    1207                 :             :         }
    1208                 :             : 
    1209                 :           0 :       bool class32 = ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32;
    1210         [ #  # ]:           0 :       size_t shsize = class32 ? sizeof (Elf32_Shdr) : sizeof (Elf64_Shdr);
    1211         [ #  # ]:           0 :       if (unlikely (shnum == 0 || shnum > SIZE_MAX / shsize + 1))
    1212                 :           0 :         error_exit (0, _("overflow with shnum = %zu in '%s' section"),
    1213                 :             :                     (size_t) shnum, ".gnu.prelink_undo");
    1214                 :             : 
    1215                 :           0 :       --shnum;
    1216                 :             : 
    1217                 :           0 :       size_t phsize = gelf_fsize (main, ELF_T_PHDR, phnum, EV_CURRENT);
    1218                 :           0 :       src.d_buf += src.d_size + phsize;
    1219                 :           0 :       src.d_size = gelf_fsize (main, ELF_T_SHDR, shnum, EV_CURRENT);
    1220                 :           0 :       src.d_type = ELF_T_SHDR;
    1221         [ #  # ]:           0 :       if ((size_t) (src.d_buf - undodata->d_buf) > undodata->d_size
    1222         [ #  # ]:           0 :           || undodata->d_size - (src.d_buf - undodata->d_buf) != src.d_size)
    1223                 :           0 :         error_exit (0, _("invalid contents in '%s' section"),
    1224                 :             :                     ".gnu.prelink_undo");
    1225                 :             : 
    1226                 :           0 :       const size_t shdr_bytes = shnum * shsize;
    1227                 :           0 :       void *shdr = xmalloc (shdr_bytes);
    1228                 :           0 :       dst.d_buf = shdr;
    1229                 :           0 :       dst.d_size = shdr_bytes;
    1230         [ #  # ]:           0 :       ELF_CHECK (gelf_xlatetom (main, &dst, &src,
    1231                 :             :                                 main_ehdr->e_ident[EI_DATA]) != NULL,
    1232                 :             :                  _("cannot read '.gnu.prelink_undo' section: %s"));
    1233                 :             : 
    1234                 :           0 :       undo_sections = xmalloc (shnum * sizeof undo_sections[0]);
    1235         [ #  # ]:           0 :       for (size_t i = 0; i < shnum; ++i)
    1236                 :             :         {
    1237                 :           0 :           struct section *sec = &undo_sections[undo_nalloc];
    1238                 :           0 :           Elf32_Shdr (*s32)[shnum] = shdr;
    1239                 :           0 :           Elf64_Shdr (*s64)[shnum] = shdr;
    1240         [ #  # ]:           0 :           if (class32)
    1241                 :             :             {
    1242                 :             : #define COPY(field) sec->shdr.field = (*s32)[i].field
    1243                 :           0 :               COPY (sh_name);
    1244                 :           0 :               COPY (sh_type);
    1245                 :           0 :               COPY (sh_flags);
    1246                 :           0 :               COPY (sh_addr);
    1247                 :           0 :               COPY (sh_offset);
    1248                 :           0 :               COPY (sh_size);
    1249                 :           0 :               COPY (sh_link);
    1250                 :           0 :               COPY (sh_info);
    1251                 :           0 :               COPY (sh_addralign);
    1252                 :           0 :               COPY (sh_entsize);
    1253                 :             : #undef  COPY
    1254                 :             :             }
    1255                 :             :           else
    1256                 :           0 :             sec->shdr = (*s64)[i];
    1257         [ #  # ]:           0 :           if (sec->shdr.sh_flags & SHF_ALLOC)
    1258                 :             :             {
    1259                 :           0 :               sec->shdr.sh_addr += bias;
    1260                 :           0 :               sec->name = get_section_name (i + 1, &sec->shdr, main_shstrtab);
    1261                 :           0 :               sec->scn = elf_getscn (main, i + 1); /* Really just for ndx.  */
    1262                 :           0 :               sec->outscn = NULL;
    1263                 :           0 :               sec->strent = NULL;
    1264                 :           0 :               sec->sig = get_group_sig (main, &sec->shdr);
    1265                 :           0 :               ++undo_nalloc;
    1266                 :             :             }
    1267                 :             :         }
    1268                 :           0 :       qsort (undo_sections, undo_nalloc,
    1269                 :             :              sizeof undo_sections[0], compare_sections_nonrel);
    1270                 :           0 :       free (shdr);
    1271                 :             :     }
    1272                 :             : 
    1273                 :           0 :   bool fail = false;
    1274                 :           0 :   Elf_Scn *scn = NULL;
    1275         [ #  # ]:           0 :   while ((scn = elf_nextscn (debug, scn)) != NULL)
    1276                 :             :     {
    1277                 :           0 :       GElf_Shdr shdr_mem;
    1278                 :           0 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1279         [ #  # ]:           0 :       ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
    1280                 :             : 
    1281         [ #  # ]:           0 :       if (!(shdr->sh_flags & SHF_ALLOC))
    1282                 :           0 :         continue;
    1283                 :             : 
    1284                 :           0 :       const char *name = get_section_name (elf_ndxscn (scn), shdr,
    1285                 :             :                                            debug_shstrtab);
    1286                 :             : 
    1287         [ #  # ]:           0 :       if (undo_sections != NULL)
    1288                 :             :         {
    1289                 :           0 :           struct section *sec = find_alloc_section (shdr, 0, name,
    1290                 :             :                                                     undo_sections,
    1291                 :             :                                                     undo_nalloc);
    1292         [ #  # ]:           0 :           if (sec != NULL)
    1293                 :             :             {
    1294                 :           0 :               sec->outscn = scn;
    1295                 :           0 :               continue;
    1296                 :             :             }
    1297                 :             :         }
    1298                 :             : 
    1299                 :             :       /* If there is no prelink info, we are just here to find
    1300                 :             :          the sections to give error messages about.  */
    1301         [ #  # ]:           0 :       for (size_t i = 0; shdr != NULL && i < nalloc; ++i)
    1302         [ #  # ]:           0 :         if (sections[i].outscn == scn)
    1303                 :           0 :           shdr = NULL;
    1304                 :           0 :       fail |= check_match (shdr == NULL, scn, name);
    1305                 :             :     }
    1306                 :             : 
    1307         [ #  # ]:           0 :   if (fail)
    1308                 :           0 :     exit (EXIT_FAILURE);
    1309                 :             : 
    1310                 :             :   /* Now we have lined up output sections for each of the original sections
    1311                 :             :      before prelinking.  Translate those to the prelinked sections.
    1312                 :             :      This matches what prelink's undo_sections does.  */
    1313                 :             :   struct section *split_bss = NULL;
    1314         [ #  # ]:           0 :   for (size_t i = 0; i < undo_nalloc; ++i)
    1315                 :             :     {
    1316                 :           0 :       const struct section *undo_sec = &undo_sections[i];
    1317                 :             : 
    1318                 :           0 :       const char *name = undo_sec->name;
    1319                 :           0 :       scn = undo_sec->scn; /* This is just for elf_ndxscn.  */
    1320                 :             : 
    1321         [ #  # ]:           0 :       for (size_t j = 0; j < nalloc; ++j)
    1322                 :             :         {
    1323                 :           0 :           struct section *sec = &sections[j];
    1324                 :             : #define RELA_SCALED(field) \
    1325                 :             :           (2 * sec->shdr.field == 3 * undo_sec->shdr.field)
    1326         [ #  # ]:           0 :           if (sec->outscn == NULL
    1327         [ #  # ]:           0 :               && sec->shdr.sh_name == undo_sec->shdr.sh_name
    1328         [ #  # ]:           0 :               && sec->shdr.sh_flags == undo_sec->shdr.sh_flags
    1329         [ #  # ]:           0 :               && sec->shdr.sh_addralign == undo_sec->shdr.sh_addralign
    1330         [ #  # ]:           0 :               && (((sec->shdr.sh_type == undo_sec->shdr.sh_type
    1331         [ #  # ]:           0 :                     && sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize
    1332         [ #  # ]:           0 :                     && (sec->shdr.sh_size == undo_sec->shdr.sh_size
    1333         [ #  # ]:           0 :                         || (sec->shdr.sh_size > undo_sec->shdr.sh_size
    1334         [ #  # ]:           0 :                             && main_ehdr->e_type == ET_EXEC
    1335         [ #  # ]:           0 :                             && !strcmp (sec->name, ".dynstr"))))
    1336         [ #  # ]:           0 :                    || (sec->shdr.sh_size == undo_sec->shdr.sh_size
    1337         [ #  # ]:           0 :                        && ((sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize
    1338         [ #  # ]:           0 :                             && undo_sec->shdr.sh_type == SHT_NOBITS)
    1339         [ #  # ]:           0 :                            || undo_sec->shdr.sh_type == SHT_PROGBITS)
    1340         [ #  # ]:           0 :                        && !strcmp (sec->name, ".plt")))
    1341         [ #  # ]:           0 :                   || (sec->shdr.sh_type == SHT_RELA
    1342         [ #  # ]:           0 :                       && undo_sec->shdr.sh_type == SHT_REL
    1343   [ #  #  #  # ]:           0 :                       && RELA_SCALED (sh_entsize) && RELA_SCALED (sh_size))
    1344         [ #  # ]:           0 :                   || (sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize
    1345         [ #  # ]:           0 :                       && (sec->shdr.sh_type == undo_sec->shdr.sh_type
    1346         [ #  # ]:           0 :                           || (sec->shdr.sh_type == SHT_PROGBITS
    1347         [ #  # ]:           0 :                               && undo_sec->shdr.sh_type == SHT_NOBITS))
    1348         [ #  # ]:           0 :                       && sec->shdr.sh_size <= undo_sec->shdr.sh_size
    1349         [ #  # ]:           0 :                       && (!strcmp (sec->name, ".bss")
    1350         [ #  # ]:           0 :                           || !strcmp (sec->name, ".sbss"))
    1351         [ #  # ]:           0 :                       && (sec->shdr.sh_size == undo_sec->shdr.sh_size
    1352         [ #  # ]:           0 :                           || (split_bss = sec) > sections))))
    1353                 :             :             {
    1354                 :           0 :               sec->outscn = undo_sec->outscn;
    1355                 :           0 :               undo_sec = NULL;
    1356                 :           0 :               break;
    1357                 :             :             }
    1358                 :             :         }
    1359                 :             : 
    1360                 :           0 :       fail |= check_match (undo_sec == NULL, scn, name);
    1361                 :             :     }
    1362                 :             : 
    1363                 :           0 :   free (undo_sections);
    1364                 :             : 
    1365         [ #  # ]:           0 :   if (fail)
    1366                 :           0 :     exit (EXIT_FAILURE);
    1367                 :             : 
    1368                 :           0 :   return split_bss;
    1369                 :             : }
    1370                 :             : 
    1371                 :             : /* Create new .shstrtab contents, subroutine of copy_elided_sections.
    1372                 :             :    This can't be open coded there and still use variable-length auto arrays,
    1373                 :             :    since the end of our block would free other VLAs too.  */
    1374                 :             : static Elf_Data *
    1375                 :          48 : new_shstrtab (Elf *unstripped, size_t unstripped_shnum,
    1376                 :             :               Elf_Data *shstrtab, size_t unstripped_shstrndx,
    1377                 :             :               struct section *sections, size_t stripped_shnum,
    1378                 :             :               Dwelf_Strtab *strtab)
    1379                 :          48 : {
    1380         [ +  + ]:          48 :   if (strtab == NULL)
    1381                 :             :     return NULL;
    1382                 :             : 
    1383                 :           2 :   Dwelf_Strent *unstripped_strent[unstripped_shnum];
    1384                 :           2 :   memset (unstripped_strent, 0, sizeof unstripped_strent);
    1385                 :           2 :   for (struct section *sec = sections;
    1386         [ +  + ]:          10 :        sec < &sections[stripped_shnum - 1];
    1387                 :           8 :        ++sec)
    1388         [ +  + ]:           8 :     if (sec->outscn != NULL)
    1389                 :             :       {
    1390         [ +  + ]:           6 :         if (sec->strent == NULL)
    1391                 :             :           {
    1392                 :           4 :             sec->strent = dwelf_strtab_add (strtab, sec->name);
    1393         [ -  + ]:           4 :             ELF_CHECK (sec->strent != NULL,
    1394                 :             :                        _("cannot add section name to string table: %s"));
    1395                 :             :           }
    1396                 :           6 :         unstripped_strent[elf_ndxscn (sec->outscn) - 1] = sec->strent;
    1397                 :             :       }
    1398                 :             : 
    1399                 :             :   /* Add names of sections we aren't touching.  */
    1400         [ +  + ]:          14 :   for (size_t i = 0; i < unstripped_shnum - 1; ++i)
    1401         [ +  + ]:          12 :     if (unstripped_strent[i] == NULL)
    1402                 :             :       {
    1403                 :           6 :         Elf_Scn *scn = elf_getscn (unstripped, i + 1);
    1404                 :           6 :         GElf_Shdr shdr_mem;
    1405                 :           6 :         GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1406         [ -  + ]:           6 :         ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
    1407                 :           6 :         const char *name = get_section_name (i + 1, shdr, shstrtab);
    1408                 :           6 :         unstripped_strent[i] = dwelf_strtab_add (strtab, name);
    1409         [ -  + ]:           6 :         ELF_CHECK (unstripped_strent[i] != NULL,
    1410                 :             :                    _("cannot add section name to string table: %s"));
    1411                 :             :       }
    1412                 :             :     else
    1413                 :           6 :       unstripped_strent[i] = NULL;
    1414                 :             : 
    1415                 :             :   /* Now finalize the string table so we can get offsets.  */
    1416                 :           2 :   Elf_Data *strtab_data = elf_getdata (elf_getscn (unstripped,
    1417                 :             :                                                    unstripped_shstrndx), NULL);
    1418         [ -  + ]:           2 :   ELF_CHECK (elf_flagdata (strtab_data, ELF_C_SET, ELF_F_DIRTY),
    1419                 :             :              _("cannot update section header string table data: %s"));
    1420         [ -  + ]:           2 :   if (dwelf_strtab_finalize (strtab, strtab_data) == NULL)
    1421                 :           0 :     error_exit (0, "Not enough memory to create string table");
    1422                 :             : 
    1423                 :             :   /* Update the sh_name fields of sections we aren't modifying later.  */
    1424         [ +  + ]:          14 :   for (size_t i = 0; i < unstripped_shnum - 1; ++i)
    1425         [ +  + ]:          12 :     if (unstripped_strent[i] != NULL)
    1426                 :             :       {
    1427                 :           6 :         Elf_Scn *scn = elf_getscn (unstripped, i + 1);
    1428                 :           6 :         GElf_Shdr shdr_mem;
    1429                 :           6 :         GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1430         [ -  + ]:           6 :         ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
    1431                 :           6 :         shdr->sh_name = dwelf_strent_off (unstripped_strent[i]);
    1432         [ +  + ]:           6 :         if (i + 1 == unstripped_shstrndx)
    1433                 :           2 :           shdr->sh_size = strtab_data->d_size;
    1434                 :           6 :         update_shdr (scn, shdr);
    1435                 :             :       }
    1436                 :             : 
    1437                 :             :   return strtab_data;
    1438                 :             : }
    1439                 :             : 
    1440                 :             : /* Fill in any SHT_NOBITS sections in UNSTRIPPED by
    1441                 :             :    copying their contents and sh_type from STRIPPED.  */
    1442                 :             : static void
    1443                 :          48 : copy_elided_sections (Elf *unstripped, Elf *stripped,
    1444                 :             :                       const GElf_Ehdr *stripped_ehdr, GElf_Addr bias)
    1445                 :          48 : {
    1446                 :          48 :   size_t unstripped_shstrndx;
    1447         [ -  + ]:          48 :   ELF_CHECK (elf_getshdrstrndx (unstripped, &unstripped_shstrndx) == 0,
    1448                 :             :              _("cannot get section header string table section index: %s"));
    1449                 :             : 
    1450                 :          48 :   size_t stripped_shstrndx;
    1451         [ -  + ]:          48 :   ELF_CHECK (elf_getshdrstrndx (stripped, &stripped_shstrndx) == 0,
    1452                 :             :              _("cannot get section header string table section index: %s"));
    1453                 :             : 
    1454                 :          48 :   size_t unstripped_shnum;
    1455         [ -  + ]:          48 :   ELF_CHECK (elf_getshdrnum (unstripped, &unstripped_shnum) == 0,
    1456                 :             :              _("cannot get section count: %s"));
    1457                 :             : 
    1458                 :          48 :   size_t stripped_shnum;
    1459         [ -  + ]:          48 :   ELF_CHECK (elf_getshdrnum (stripped, &stripped_shnum) == 0,
    1460                 :             :              _("cannot get section count: %s"));
    1461                 :             : 
    1462         [ -  + ]:          48 :   if (unlikely (stripped_shnum > unstripped_shnum))
    1463                 :           0 :     error_exit (0, _("\
    1464                 :             : more sections in stripped file than debug file -- arguments reversed?"));
    1465                 :             : 
    1466         [ -  + ]:          48 :   if (unlikely (stripped_shnum <= 1))
    1467                 :           0 :     error_exit (0, _("no sections in stripped file"));
    1468                 :             : 
    1469                 :             :   /* Used as sanity check for allocated section offset, if the section
    1470                 :             :      offset needs to be preserved.  We want to know the max size of the
    1471                 :             :      ELF file, to check if any existing section offsets are OK.  */
    1472                 :          48 :   int64_t max_off = -1;
    1473         [ +  + ]:          48 :   if (stripped_ehdr->e_type != ET_REL)
    1474                 :             :     {
    1475                 :          24 :       elf_flagelf (stripped, ELF_C_SET, ELF_F_LAYOUT);
    1476                 :          24 :       max_off = elf_update (stripped, ELF_C_NULL);
    1477                 :             :     }
    1478                 :             : 
    1479                 :             :   /* Cache the stripped file's section details.  */
    1480                 :          48 :   struct section sections[stripped_shnum - 1];
    1481                 :          48 :   Elf_Scn *scn = NULL;
    1482         [ +  + ]:        1050 :   while ((scn = elf_nextscn (stripped, scn)) != NULL)
    1483                 :             :     {
    1484                 :        1002 :       size_t i = elf_ndxscn (scn) - 1;
    1485                 :        1002 :       GElf_Shdr *shdr = gelf_getshdr (scn, &sections[i].shdr);
    1486         [ -  + ]:        1002 :       ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
    1487                 :        2004 :       sections[i].name = elf_strptr (stripped, stripped_shstrndx,
    1488                 :        1002 :                                      shdr->sh_name);
    1489         [ -  + ]:        1002 :       if (sections[i].name == NULL)
    1490                 :           0 :         error_exit (0, _("cannot read section [%zu] name: %s"),
    1491                 :             :                     elf_ndxscn (scn), elf_errmsg (-1));
    1492                 :        1002 :       sections[i].scn = scn;
    1493                 :        1002 :       sections[i].outscn = NULL;
    1494                 :        1002 :       sections[i].strent = NULL;
    1495                 :        1002 :       sections[i].sig = get_group_sig (stripped, shdr);
    1496                 :             :     }
    1497                 :             : 
    1498                 :          48 :   const struct section *stripped_symtab = NULL;
    1499                 :             : 
    1500                 :             :   /* Sort the sections, allocated by address and others after.  */
    1501                 :          48 :   qsort (sections, stripped_shnum - 1, sizeof sections[0],
    1502         [ +  + ]:          48 :          stripped_ehdr->e_type == ET_REL
    1503                 :             :          ? compare_sections_rel : compare_sections_nonrel);
    1504                 :          48 :   size_t nalloc = stripped_shnum - 1;
    1505   [ +  +  +  + ]:         350 :   while (nalloc > 0 && !(sections[nalloc - 1].shdr.sh_flags & SHF_ALLOC))
    1506                 :             :     {
    1507                 :         302 :       --nalloc;
    1508         [ +  + ]:         302 :       if (sections[nalloc].shdr.sh_type == SHT_SYMTAB)
    1509                 :          28 :         stripped_symtab = &sections[nalloc];
    1510                 :             :     }
    1511                 :             : 
    1512                 :          48 :   Elf_Data *shstrtab = elf_getdata (elf_getscn (unstripped,
    1513                 :             :                                                 unstripped_shstrndx), NULL);
    1514         [ -  + ]:          48 :   ELF_CHECK (shstrtab != NULL,
    1515                 :             :              _("cannot read section header string table: %s"));
    1516                 :             : 
    1517                 :             :   /* Match each debuginfo section with its corresponding stripped section.  */
    1518                 :             :   bool check_prelink = false;
    1519                 :             :   Elf_Scn *unstripped_symtab = NULL;
    1520                 :             :   size_t unstripped_strndx = 0;
    1521                 :             :   size_t alloc_avail = 0;
    1522                 :             :   scn = NULL;
    1523         [ +  + ]:      132492 :   while ((scn = elf_nextscn (unstripped, scn)) != NULL)
    1524                 :             :     {
    1525                 :      132444 :       GElf_Shdr shdr_mem;
    1526                 :      132444 :       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    1527         [ -  + ]:      132444 :       ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
    1528                 :             : 
    1529         [ +  + ]:      132444 :       if (shdr->sh_type == SHT_SYMTAB)
    1530                 :             :         {
    1531                 :          44 :           unstripped_symtab = scn;
    1532                 :          44 :           unstripped_strndx = shdr->sh_link;
    1533                 :      131576 :           continue;
    1534                 :             :         }
    1535                 :             : 
    1536                 :      132400 :       const size_t ndx = elf_ndxscn (scn);
    1537   [ +  +  +  + ]:      132400 :       if (ndx == unstripped_shstrndx || ndx == unstripped_strndx)
    1538                 :          88 :         continue;
    1539                 :             : 
    1540                 :      132312 :       const char *name = get_section_name (ndx, shdr, shstrtab);
    1541                 :             : 
    1542                 :      132312 :       struct section *sec = NULL;
    1543         [ +  + ]:      132312 :       if (shdr->sh_flags & SHF_ALLOC)
    1544                 :             :         {
    1545         [ +  + ]:         700 :           if (stripped_ehdr->e_type != ET_REL)
    1546                 :             :             {
    1547                 :             :               /* Look for the section that matches.  */
    1548                 :         546 :               sec = find_alloc_section (shdr, bias, name, sections, nalloc);
    1549         [ -  + ]:         546 :               if (sec == NULL)
    1550                 :             :                 {
    1551                 :             :                   /* We couldn't figure it out.  It may be a prelink issue.  */
    1552                 :           0 :                   check_prelink = true;
    1553                 :           0 :                   continue;
    1554                 :             :                 }
    1555                 :             :             }
    1556                 :             :           else
    1557                 :             :             {
    1558                 :             :               /* The sh_addr of allocated sections does not help us,
    1559                 :             :                  but the order usually matches.  */
    1560         [ +  - ]:         154 :               if (likely (sections_match (sections, alloc_avail, shdr, name)))
    1561                 :         154 :                 sec = &sections[alloc_avail++];
    1562                 :             :               else
    1563         [ #  # ]:           0 :                 for (size_t i = alloc_avail + 1; i < nalloc; ++i)
    1564         [ #  # ]:           0 :                   if (sections_match (sections, i, shdr, name))
    1565                 :             :                     {
    1566                 :           0 :                       sec = &sections[i];
    1567                 :           0 :                       break;
    1568                 :             :                     }
    1569                 :             :             }
    1570                 :             :         }
    1571                 :             :       else
    1572                 :             :         {
    1573                 :             :           /* Locate a matching unallocated section in SECTIONS.  */
    1574                 :      131612 :           const char *sig = get_group_sig (unstripped, shdr);
    1575                 :      131612 :           size_t l = nalloc, u = stripped_shnum - 1;
    1576         [ +  + ]:      657854 :           while (l < u)
    1577                 :             :             {
    1578                 :      394798 :               size_t i = (l + u) / 2;
    1579                 :      394798 :               struct section *section = &sections[i];
    1580                 :      394798 :               int cmp = compare_unalloc_sections (shdr, &section->shdr,
    1581                 :             :                                                   name, section->name,
    1582                 :             :                                                   sig, section->sig);
    1583         [ +  + ]:      394798 :               if (cmp < 0)
    1584                 :             :                 u = i;
    1585         [ +  + ]:         908 :               else if (cmp > 0)
    1586                 :         740 :                 l = i + 1;
    1587                 :             :               else
    1588                 :             :                 {
    1589                 :             :                   sec = section;
    1590                 :             :                   break;
    1591                 :             :                 }
    1592                 :             :             }
    1593                 :             : 
    1594         [ +  + ]:      131612 :           if (sec == NULL)
    1595                 :             :             {
    1596                 :             :               /* An additional unallocated section is fine if not SHT_NOBITS.
    1597                 :             :                  We looked it up anyway in case it's an unallocated section
    1598                 :             :                  copied in both files (e.g. SHT_NOTE), and don't keep both.  */
    1599         [ +  - ]:      131444 :               if (shdr->sh_type != SHT_NOBITS)
    1600                 :      131444 :                 continue;
    1601                 :             : 
    1602                 :             :               /* Somehow some old .debug files wound up with SHT_NOBITS
    1603                 :             :                  .comment sections, so let those pass.  */
    1604         [ #  # ]:           0 :               if (!strcmp (name, ".comment"))
    1605                 :           0 :                 continue;
    1606                 :             :             }
    1607                 :             :         }
    1608                 :             : 
    1609         [ -  + ]:         154 :       if (sec == NULL)
    1610                 :           0 :         error_exit (0, _("cannot find matching section for [%zu] '%s'"),
    1611                 :             :                     elf_ndxscn (scn), name);
    1612                 :             : 
    1613                 :         868 :       sec->outscn = scn;
    1614                 :             :     }
    1615                 :             : 
    1616                 :             :   /* If that failed due to changes made by prelink, we take another tack.
    1617                 :             :      We keep track of a .bss section that was partly split into .dynbss
    1618                 :             :      so that collect_symbols can update symbols' st_shndx fields.  */
    1619                 :          48 :   struct section *split_bss = NULL;
    1620         [ -  + ]:          48 :   if (check_prelink)
    1621                 :             :     {
    1622                 :           0 :       Elf_Data *data = elf_getdata (elf_getscn (stripped, stripped_shstrndx),
    1623                 :             :                                     NULL);
    1624         [ #  # ]:           0 :       ELF_CHECK (data != NULL,
    1625                 :             :                  _("cannot read section header string table: %s"));
    1626                 :           0 :       split_bss = find_alloc_sections_prelink (unstripped, shstrtab,
    1627                 :             :                                                stripped, stripped_ehdr,
    1628                 :             :                                                data, bias, sections,
    1629                 :             :                                                nalloc, stripped_shnum - 1);
    1630                 :             :     }
    1631                 :             : 
    1632                 :             :   /* Make sure each main file section has a place to go.  */
    1633                 :          48 :   const struct section *stripped_dynsym = NULL;
    1634                 :          48 :   size_t debuglink = SHN_UNDEF;
    1635                 :          48 :   size_t ndx_sec_num = stripped_shnum - 1;
    1636                 :          48 :   size_t ndx_section[ndx_sec_num];
    1637                 :          48 :   Dwelf_Strtab *strtab = NULL;
    1638                 :          48 :   for (struct section *sec = sections;
    1639         [ +  + ]:        1050 :        sec < &sections[ndx_sec_num];
    1640                 :        1002 :        ++sec)
    1641                 :             :     {
    1642                 :        1002 :       size_t secndx = elf_ndxscn (sec->scn);
    1643                 :             : 
    1644         [ +  + ]:        1002 :       if (sec->outscn == NULL)
    1645                 :             :         {
    1646                 :             :           /* We didn't find any corresponding section for this.  */
    1647                 :             : 
    1648         [ +  + ]:         134 :           if (secndx == stripped_shstrndx)
    1649                 :             :             {
    1650                 :             :               /* We only need one .shstrtab.  */
    1651                 :          48 :               ndx_section[secndx - 1] = unstripped_shstrndx;
    1652                 :          48 :               continue;
    1653                 :             :             }
    1654                 :             : 
    1655         [ +  + ]:          86 :           if (unstripped_symtab != NULL && sec == stripped_symtab)
    1656                 :             :             {
    1657                 :             :               /* We don't need a second symbol table.  */
    1658                 :          24 :               ndx_section[secndx - 1] = elf_ndxscn (unstripped_symtab);
    1659                 :          24 :               continue;
    1660                 :             :             }
    1661                 :             : 
    1662         [ +  + ]:          62 :           if (unstripped_symtab != NULL && stripped_symtab != NULL
    1663         [ +  + ]:          40 :               && secndx == stripped_symtab->shdr.sh_link
    1664         [ +  - ]:          24 :               && unstripped_strndx != 0)
    1665                 :             :             {
    1666                 :             :               /* ... nor its string table.  */
    1667                 :          24 :               ndx_section[secndx - 1] = unstripped_strndx;
    1668                 :          24 :               continue;
    1669                 :             :             }
    1670                 :             : 
    1671         [ +  - ]:          38 :           if (!(sec->shdr.sh_flags & SHF_ALLOC)
    1672         [ +  + ]:          38 :               && !strcmp (sec->name, ".gnu_debuglink"))
    1673                 :             :             {
    1674                 :             :               /* This was created by stripping.  We don't want it.  */
    1675                 :          36 :               debuglink = secndx;
    1676                 :          36 :               ndx_section[secndx - 1] = SHN_UNDEF;
    1677                 :          36 :               continue;
    1678                 :             :             }
    1679                 :             : 
    1680                 :           2 :           sec->outscn = elf_newscn (unstripped);
    1681                 :           2 :           Elf_Data *newdata = elf_newdata (sec->outscn);
    1682   [ +  -  -  + ]:           2 :           ELF_CHECK (newdata != NULL && gelf_update_shdr (sec->outscn,
    1683                 :             :                                                           &sec->shdr),
    1684                 :             :                      _("cannot add new section: %s"));
    1685                 :           2 :           unstripped_shnum++;
    1686                 :             : 
    1687         [ +  - ]:           2 :           if (strtab == NULL)
    1688                 :           2 :             strtab = dwelf_strtab_init (true);
    1689                 :           2 :           sec->strent = dwelf_strtab_add (strtab, sec->name);
    1690         [ -  + ]:           2 :           ELF_CHECK (sec->strent != NULL,
    1691                 :             :                      _("cannot add section name to string table: %s"));
    1692                 :             :         }
    1693                 :             : 
    1694                 :             :       /* Cache the mapping of original section indices to output sections.  */
    1695                 :         870 :       ndx_section[secndx - 1] = elf_ndxscn (sec->outscn);
    1696                 :             :     }
    1697                 :             : 
    1698                 :             :   /* We added some sections, so we need a new shstrtab.  */
    1699                 :          48 :   Elf_Data *strtab_data = new_shstrtab (unstripped, unstripped_shnum,
    1700                 :             :                                         shstrtab, unstripped_shstrndx,
    1701                 :             :                                         sections, stripped_shnum,
    1702                 :             :                                         strtab);
    1703                 :             : 
    1704                 :             :   /* Get the updated section count.  */
    1705         [ -  + ]:          48 :   ELF_CHECK (elf_getshdrnum (unstripped, &unstripped_shnum) == 0,
    1706                 :             :              _("cannot get section count: %s"));
    1707                 :             : 
    1708                 :          48 :   bool placed[unstripped_shnum - 1];
    1709                 :          48 :   memset (placed, 0, sizeof placed);
    1710                 :             : 
    1711                 :             :   /* Now update the output sections and copy in their data.  */
    1712                 :          48 :   GElf_Off offset = 0;
    1713                 :          48 :   for (const struct section *sec = sections;
    1714         [ +  + ]:        1046 :        sec < &sections[stripped_shnum - 1];
    1715                 :         998 :        ++sec)
    1716         [ +  + ]:        1002 :     if (sec->outscn != NULL)
    1717                 :             :       {
    1718                 :         870 :         GElf_Shdr shdr_mem;
    1719                 :         870 :         GElf_Shdr *shdr = gelf_getshdr (sec->outscn, &shdr_mem);
    1720         [ -  + ]:         870 :         ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
    1721                 :             : 
    1722                 :             :         /* In an ET_REL file under --relocate, the sh_addr of SHF_ALLOC
    1723                 :             :            sections will have been set nonzero by relocation.  This
    1724                 :             :            touched the shdrs of whichever file had the symtab.  sh_addr
    1725                 :             :            is still zero in the corresponding shdr.  The relocated
    1726                 :             :            address is what we want to use.  */
    1727         [ +  + ]:         870 :         if (stripped_ehdr->e_type != ET_REL
    1728         [ +  + ]:         294 :             || !(shdr_mem.sh_flags & SHF_ALLOC)
    1729         [ +  - ]:         154 :             || shdr_mem.sh_addr == 0)
    1730                 :         870 :           shdr_mem.sh_addr = sec->shdr.sh_addr;
    1731                 :             : 
    1732                 :         870 :         shdr_mem.sh_type = sec->shdr.sh_type;
    1733                 :         870 :         shdr_mem.sh_size = sec->shdr.sh_size;
    1734                 :         870 :         shdr_mem.sh_info = sec->shdr.sh_info;
    1735                 :         870 :         shdr_mem.sh_link = sec->shdr.sh_link;
    1736                 :             : 
    1737                 :             :         /* Buggy binutils objdump might have stripped the SHF_INFO_LINK
    1738                 :             :            put it back if necessary.  */
    1739         [ +  + ]:         870 :         if ((sec->shdr.sh_type == SHT_REL || sec->shdr.sh_type == SHT_RELA)
    1740         [ +  + ]:         126 :             && sec->shdr.sh_flags != shdr_mem.sh_flags
    1741         [ +  - ]:           4 :             && (sec->shdr.sh_flags & SHF_INFO_LINK) != 0)
    1742                 :           4 :           shdr_mem.sh_flags |= SHF_INFO_LINK;
    1743                 :             : 
    1744         [ +  + ]:         870 :         if (sec->shdr.sh_link != SHN_UNDEF)
    1745                 :             :           {
    1746         [ -  + ]:         264 :             if (sec->shdr.sh_link > ndx_sec_num)
    1747                 :           0 :               error_exit (0,
    1748                 :             :                           "section [%zd] has invalid sh_link %" PRId32,
    1749                 :             :                           elf_ndxscn (sec->scn), sec->shdr.sh_link);
    1750                 :         264 :             shdr_mem.sh_link = ndx_section[sec->shdr.sh_link - 1];
    1751                 :             :           }
    1752   [ +  +  -  +  :         870 :         if (SH_INFO_LINK_P (&sec->shdr) && sec->shdr.sh_info != 0)
                   +  + ]
    1753                 :             :           {
    1754         [ -  + ]:         104 :             if (sec->shdr.sh_info > ndx_sec_num)
    1755                 :           0 :               error_exit (0,
    1756                 :             :                           "section [%zd] has invalid sh_info %" PRId32,
    1757                 :             :                           elf_ndxscn (sec->scn), sec->shdr.sh_info);
    1758                 :         104 :             shdr_mem.sh_info = ndx_section[sec->shdr.sh_info - 1];
    1759                 :             :           }
    1760                 :             : 
    1761         [ +  + ]:         870 :         if (strtab != NULL)
    1762                 :           6 :           shdr_mem.sh_name = dwelf_strent_off (sec->strent);
    1763                 :             : 
    1764                 :         870 :         Elf_Data *indata = elf_getdata (sec->scn, NULL);
    1765         [ -  + ]:         870 :         ELF_CHECK (indata != NULL, _("cannot get section data: %s"));
    1766                 :         870 :         Elf_Data *outdata = elf_getdata (sec->outscn, NULL);
    1767         [ -  + ]:         870 :         ELF_CHECK (outdata != NULL, _("cannot copy section data: %s"));
    1768                 :         870 :         *outdata = *indata;
    1769                 :         870 :         elf_flagdata (outdata, ELF_C_SET, ELF_F_DIRTY);
    1770                 :             : 
    1771                 :             :         /* Preserve the file layout of the allocated sections.  */
    1772   [ +  +  +  + ]:         870 :         if (stripped_ehdr->e_type != ET_REL && (shdr_mem.sh_flags & SHF_ALLOC))
    1773                 :             :           {
    1774   [ +  -  -  + ]:         546 :             if (max_off > 0 && sec->shdr.sh_offset > (Elf64_Off) max_off)
    1775                 :           0 :                 error_exit (0,
    1776                 :             :                             "allocated section offset too large [%zd] %" PRIx64,
    1777                 :             :                             elf_ndxscn (sec->scn), sec->shdr.sh_offset);
    1778                 :             : 
    1779                 :         546 :             shdr_mem.sh_offset = sec->shdr.sh_offset;
    1780                 :         546 :             placed[elf_ndxscn (sec->outscn) - 1] = true;
    1781                 :             : 
    1782                 :        1092 :             const GElf_Off end_offset = (shdr_mem.sh_offset
    1783                 :         546 :                                          + (shdr_mem.sh_type == SHT_NOBITS
    1784         [ +  + ]:         546 :                                             ? 0 : shdr_mem.sh_size));
    1785         [ +  + ]:         546 :             if (end_offset > offset)
    1786                 :         870 :               offset = end_offset;
    1787                 :             :           }
    1788                 :             : 
    1789                 :         870 :         update_shdr (sec->outscn, &shdr_mem);
    1790                 :             : 
    1791         [ +  + ]:         870 :         if (shdr_mem.sh_type == SHT_SYMTAB || shdr_mem.sh_type == SHT_DYNSYM)
    1792                 :             :           {
    1793                 :             :             /* We must adjust all the section indices in the symbol table.  */
    1794                 :             : 
    1795                 :          28 :             Elf_Data *shndxdata = NULL; /* XXX */
    1796                 :             : 
    1797         [ +  + ]:          28 :             if (!valid_symtab_entsize (stripped, &shdr_mem))
    1798                 :           4 :               error_exit (0,
    1799                 :             :                           _("SYMTAB section cannot have an invalid"
    1800                 :             :                             " sh_entsize or sh_size"));
    1801         [ +  + ]:         614 :             for (size_t i = 1; i < shdr_mem.sh_size / shdr_mem.sh_entsize; ++i)
    1802                 :             :               {
    1803                 :         590 :                 GElf_Sym sym_mem;
    1804                 :         590 :                 GElf_Word shndx = SHN_UNDEF;
    1805                 :         590 :                 GElf_Sym *sym = gelf_getsymshndx (outdata, shndxdata,
    1806                 :             :                                                   i, &sym_mem, &shndx);
    1807         [ -  + ]:         590 :                 ELF_CHECK (sym != NULL,
    1808                 :             :                            _("cannot get symbol table entry: %s"));
    1809         [ +  - ]:         590 :                 if (sym->st_shndx != SHN_XINDEX)
    1810                 :         590 :                   shndx = sym->st_shndx;
    1811                 :             : 
    1812         [ +  + ]:         590 :                 if (shndx != SHN_UNDEF && shndx < SHN_LORESERVE)
    1813                 :             :                   {
    1814         [ -  + ]:         234 :                     if (shndx >= stripped_shnum)
    1815                 :           0 :                       error_exit (0,
    1816                 :             :                                   _("symbol [%zu] has invalid section index"), i);
    1817                 :             : 
    1818                 :         234 :                     shndx = ndx_section[shndx - 1];
    1819         [ +  - ]:         234 :                     if (shndx < SHN_LORESERVE)
    1820                 :             :                       {
    1821                 :         234 :                         sym->st_shndx = shndx;
    1822                 :         234 :                         shndx = SHN_UNDEF;
    1823                 :             :                       }
    1824                 :             :                     else
    1825                 :           0 :                       sym->st_shndx = SHN_XINDEX;
    1826                 :             : 
    1827         [ -  + ]:         590 :                     ELF_CHECK (gelf_update_symshndx (outdata, shndxdata,
    1828                 :             :                                                      i, sym, shndx),
    1829                 :             :                                _("cannot update symbol table: %s"));
    1830                 :             :                   }
    1831                 :             :               }
    1832                 :             : 
    1833         [ -  + ]:          24 :             if (shdr_mem.sh_type == SHT_SYMTAB)
    1834                 :           0 :               stripped_symtab = sec;
    1835         [ +  - ]:          24 :             if (shdr_mem.sh_type == SHT_DYNSYM)
    1836                 :          24 :               stripped_dynsym = sec;
    1837                 :             :           }
    1838                 :             : 
    1839         [ +  + ]:         866 :         if (shdr_mem.sh_type == SHT_GROUP)
    1840                 :             :           {
    1841                 :             :             /* We must adjust all the section indices in the group.
    1842                 :             :                Skip the first word, which is the section group flag.
    1843                 :             :                Everything else is a section index.  */
    1844                 :          14 :             Elf32_Word *shndx = (Elf32_Word *) outdata->d_buf;
    1845         [ +  + ]:          44 :             for (size_t i = 1; i < shdr_mem.sh_size / sizeof (Elf32_Word); ++i)
    1846   [ +  -  -  + ]:          30 :               if (shndx[i]  == SHN_UNDEF || shndx[i] >= stripped_shnum)
    1847                 :           0 :                 error_exit (0,
    1848                 :             :                             _("group has invalid section index [%zd]"), i);
    1849                 :             :               else
    1850                 :          30 :                 shndx[i] = ndx_section[shndx[i] - 1];
    1851                 :             :           }
    1852                 :             :       }
    1853                 :             : 
    1854                 :             :   /* We may need to update the symbol table.  */
    1855                 :          44 :   Elf_Data *symdata = NULL;
    1856                 :          44 :   Dwelf_Strtab *symstrtab = NULL;
    1857                 :          44 :   Elf_Data *symstrdata = NULL;
    1858         [ +  - ]:          44 :   if (unstripped_symtab != NULL && (stripped_symtab != NULL
    1859         [ +  + ]:          44 :                                     || check_prelink /* Section adjustments. */
    1860         [ +  - ]:          20 :                                     || (stripped_ehdr->e_type != ET_REL
    1861         [ -  + ]:          20 :                                         && bias != 0)))
    1862                 :          44 :     {
    1863                 :             :       /* Merge the stripped file's symbol table into the unstripped one.  */
    1864         [ +  - ]:          24 :       if (stripped_symtab != NULL
    1865         [ +  + ]:          24 :           && !valid_symtab_entsize (stripped, &stripped_symtab->shdr))
    1866                 :           2 :         error_exit (0,
    1867                 :             :                     _("stripped SYMTAB section cannot have an invalid"
    1868                 :             :                       " sh_entsize or sh_size"));
    1869                 :          44 :       const size_t stripped_nsym = (stripped_symtab == NULL ? 1
    1870                 :          22 :                                     : (stripped_symtab->shdr.sh_size
    1871                 :          22 :                                        / stripped_symtab->shdr.sh_entsize));
    1872                 :             : 
    1873                 :          22 :       GElf_Shdr shdr_mem;
    1874                 :          22 :       GElf_Shdr *shdr = gelf_getshdr (unstripped_symtab, &shdr_mem);
    1875         [ -  + ]:          22 :       ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
    1876         [ +  + ]:          22 :       if (!valid_symtab_entsize (unstripped, shdr))
    1877                 :           2 :         error_exit (0,
    1878                 :             :                     _("unstripped SYMTAB section cannot have an invalid"
    1879                 :             :                       " sh_entsize or sh_size"));
    1880                 :          20 :       const size_t unstripped_nsym = shdr->sh_size / shdr->sh_entsize;
    1881                 :             : 
    1882                 :             :       /* First collect all the symbols from both tables.  */
    1883                 :             : 
    1884                 :          20 :       const size_t total_syms = stripped_nsym - 1 + unstripped_nsym - 1;
    1885                 :          20 :       struct symbol *symbols = xmalloc (total_syms * sizeof (struct symbol));
    1886                 :          20 :       size_t *symndx_map = xmalloc (total_syms * sizeof (size_t));
    1887                 :             : 
    1888         [ +  - ]:          20 :       if (stripped_symtab != NULL)
    1889                 :          40 :         collect_symbols (unstripped, stripped_ehdr->e_type == ET_REL,
    1890                 :          20 :                          stripped_symtab->scn,
    1891                 :          20 :                          elf_getscn (stripped, stripped_symtab->shdr.sh_link),
    1892                 :             :                          stripped_nsym, 0, ndx_section,
    1893                 :             :                          symbols, symndx_map, NULL);
    1894                 :             : 
    1895                 :          20 :       Elf_Scn *unstripped_strtab = elf_getscn (unstripped, shdr->sh_link);
    1896                 :          20 :       collect_symbols (unstripped, stripped_ehdr->e_type == ET_REL,
    1897                 :             :                        unstripped_symtab, unstripped_strtab, unstripped_nsym,
    1898                 :          20 :                        stripped_ehdr->e_type == ET_REL ? 0 : bias, NULL,
    1899                 :          20 :                        &symbols[stripped_nsym - 1],
    1900         [ +  + ]:          20 :                        &symndx_map[stripped_nsym - 1], split_bss);
    1901                 :             : 
    1902                 :             :       /* Next, sort our array of all symbols.  */
    1903                 :          20 :       qsort (symbols, total_syms, sizeof symbols[0], compare_symbols);
    1904                 :             : 
    1905                 :             :       /* Now we can weed out the duplicates.  Assign remaining symbols
    1906                 :             :          new slots, collecting a map from old indices to new.  */
    1907                 :          20 :       size_t nsym = 0;
    1908         [ +  + ]:        1994 :       for (struct symbol *s = symbols; s < &symbols[total_syms]; ++s)
    1909                 :             :         {
    1910                 :             :           /* Skip a section symbol for a removed section.  */
    1911         [ +  + ]:        1974 :           if (s->shndx == SHN_UNDEF
    1912         [ +  + ]:         538 :               && GELF_ST_TYPE (s->info.info) == STT_SECTION)
    1913                 :             :             {
    1914                 :           4 :               s->name = NULL;        /* Mark as discarded. */
    1915                 :           4 :               *s->map = STN_UNDEF;
    1916                 :           4 :               s->duplicate = NULL;
    1917                 :           4 :               continue;
    1918                 :             :             }
    1919                 :             : 
    1920                 :             :           struct symbol *n = s;
    1921                 :        3430 :           while (n + 1 < &symbols[total_syms]
    1922   [ +  +  +  + ]:        3430 :                  && !compare_symbols_duplicate (s, n + 1))
    1923                 :             :             ++n;
    1924                 :             : 
    1925         [ +  + ]:        3430 :           while (s < n)
    1926                 :             :             {
    1927                 :             :               /* This is a duplicate.  Its twin will get the next slot.  */
    1928                 :        1460 :               s->name = NULL;        /* Mark as discarded. */
    1929                 :        1460 :               s->duplicate = n->map;
    1930                 :        1460 :               ++s;
    1931                 :             :             }
    1932                 :             : 
    1933                 :             :           /* Allocate the next slot.  */
    1934                 :        1970 :           *s->map = ++nsym;
    1935                 :             :         }
    1936                 :             : 
    1937                 :             :       /* Now we sort again, to determine the order in the output.  */
    1938                 :          20 :       qsort (symbols, total_syms, sizeof symbols[0], compare_symbols_output);
    1939                 :             : 
    1940         [ +  - ]:          20 :       if (nsym < total_syms)
    1941                 :             :         /* The discarded symbols are now at the end of the table.  */
    1942         [ -  + ]:          20 :         assert (symbols[nsym].name == NULL);
    1943                 :             : 
    1944                 :             :       /* Now a final pass updates the map with the final order,
    1945                 :             :          and builds up the new string table.  */
    1946                 :          20 :       symstrtab = dwelf_strtab_init (true);
    1947         [ +  + ]:        1990 :       for (size_t i = 0; i < nsym; ++i)
    1948                 :             :         {
    1949         [ -  + ]:        1970 :           assert (symbols[i].name != NULL);
    1950         [ -  + ]:        1970 :           assert (*symbols[i].map != 0);
    1951                 :        1970 :           *symbols[i].map = 1 + i;
    1952                 :        1970 :           symbols[i].strent = dwelf_strtab_add (symstrtab, symbols[i].name);
    1953                 :             :         }
    1954                 :             : 
    1955                 :             :       /* Scan the discarded symbols too, just to update their slots
    1956                 :             :          in SYMNDX_MAP to refer to their live duplicates.  */
    1957         [ +  + ]:        1484 :       for (size_t i = nsym; i < total_syms; ++i)
    1958                 :             :         {
    1959         [ -  + ]:        1464 :           assert (symbols[i].name == NULL);
    1960         [ +  + ]:        1464 :           if (symbols[i].duplicate == NULL)
    1961         [ -  + ]:           4 :             assert (*symbols[i].map == STN_UNDEF);
    1962                 :             :           else
    1963                 :             :             {
    1964         [ -  + ]:        1460 :               assert (*symbols[i].duplicate != STN_UNDEF);
    1965                 :        1460 :               *symbols[i].map = *symbols[i].duplicate;
    1966                 :             :             }
    1967                 :             :         }
    1968                 :             : 
    1969                 :             :       /* Now we are ready to write the new symbol table.  */
    1970                 :          20 :       symdata = elf_getdata (unstripped_symtab, NULL);
    1971         [ -  + ]:          20 :       if (symdata == NULL)
    1972                 :           0 :         error_exit (0, "Failed to get data from symbol table: %s",
    1973                 :             :                     elf_errmsg (-1));
    1974                 :          20 :       symstrdata = elf_getdata (unstripped_strtab, NULL);
    1975         [ -  + ]:          20 :       if (symstrdata == NULL)
    1976                 :           0 :         error_exit (0, "Failed to get data from symbol string table: %s",
    1977                 :             :                     elf_errmsg (-1));
    1978                 :          20 :       Elf_Data *shndxdata = NULL;       /* XXX */
    1979                 :             : 
    1980                 :             :       /* If symtab and the section header table share the string table
    1981                 :             :          add the section names to the strtab and then (after finalizing)
    1982                 :             :          fixup the section header sh_names.  Also dispose of the old data.  */
    1983                 :          20 :       Dwelf_Strent *unstripped_strent[unstripped_shnum - 1];
    1984         [ +  + ]:          20 :       if (unstripped_shstrndx == elf_ndxscn (unstripped_strtab))
    1985                 :             :         {
    1986         [ +  + ]:          76 :           for (size_t i = 0; i < unstripped_shnum - 1; ++i)
    1987                 :             :             {
    1988                 :          74 :               Elf_Scn *sec = elf_getscn (unstripped, i + 1);
    1989                 :          74 :               GElf_Shdr mem;
    1990                 :          74 :               GElf_Shdr *hdr = gelf_getshdr (sec, &mem);
    1991                 :          74 :               const char *name = get_section_name (i + 1, hdr, shstrtab);
    1992                 :          74 :               unstripped_strent[i] = dwelf_strtab_add (symstrtab, name);
    1993         [ -  + ]:          74 :               ELF_CHECK (unstripped_strent[i] != NULL,
    1994                 :             :                          _("cannot add section name to string table: %s"));
    1995                 :             :             }
    1996                 :             : 
    1997         [ +  - ]:           2 :           if (strtab != NULL)
    1998                 :             :             {
    1999                 :           0 :               dwelf_strtab_free (strtab);
    2000                 :           0 :               free (strtab_data->d_buf);
    2001                 :           0 :               strtab = NULL;
    2002                 :             :             }
    2003                 :             :         }
    2004                 :             : 
    2005         [ -  + ]:          20 :       if (dwelf_strtab_finalize (symstrtab, symstrdata) == NULL)
    2006                 :           0 :         error_exit (0, "Not enough memory to create symbol table");
    2007                 :             : 
    2008                 :          20 :       elf_flagdata (symstrdata, ELF_C_SET, ELF_F_DIRTY);
    2009                 :             : 
    2010                 :             :       /* And update the section header names if necessary.  */
    2011         [ +  + ]:          20 :       if (unstripped_shstrndx == elf_ndxscn (unstripped_strtab))
    2012                 :             :         {
    2013         [ +  + ]:          76 :           for (size_t i = 0; i < unstripped_shnum - 1; ++i)
    2014                 :             :             {
    2015                 :          74 :               Elf_Scn *sec = elf_getscn (unstripped, i + 1);
    2016                 :          74 :               GElf_Shdr mem;
    2017                 :          74 :               GElf_Shdr *hdr = gelf_getshdr (sec, &mem);
    2018                 :          74 :               shdr->sh_name = dwelf_strent_off (unstripped_strent[i]);
    2019                 :          74 :               update_shdr (sec, hdr);
    2020                 :             :             }
    2021                 :             :         }
    2022                 :             : 
    2023                 :             :       /* Now update the symtab shdr.  Reload symtab shdr because sh_name
    2024                 :             :          might have changed above. */
    2025                 :          20 :       shdr = gelf_getshdr (unstripped_symtab, &shdr_mem);
    2026         [ -  + ]:          20 :       ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
    2027                 :             : 
    2028                 :          20 :       shdr->sh_size = symdata->d_size = (1 + nsym) * shdr->sh_entsize;
    2029                 :          20 :       symdata->d_buf = xmalloc (symdata->d_size);
    2030                 :          20 :       record_new_data (symdata->d_buf);
    2031                 :             : 
    2032                 :          20 :       GElf_Sym sym;
    2033                 :          20 :       memset (&sym, 0, sizeof sym);
    2034         [ -  + ]:          20 :       ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, 0, &sym, SHN_UNDEF),
    2035                 :             :                  _("cannot update symbol table: %s"));
    2036                 :             : 
    2037                 :          20 :       shdr->sh_info = 1;
    2038         [ +  + ]:        1990 :       for (size_t i = 0; i < nsym; ++i)
    2039                 :             :         {
    2040                 :        1970 :           struct symbol *s = &symbols[i];
    2041                 :             : 
    2042                 :             :           /* Fill in the symbol details.  */
    2043                 :        1970 :           sym.st_name = dwelf_strent_off (s->strent);
    2044                 :        1970 :           sym.st_value = s->value; /* Already biased to output address.  */
    2045                 :        1970 :           sym.st_size = s->size;
    2046                 :        1970 :           sym.st_shndx = s->shndx; /* Already mapped to output index.  */
    2047                 :        1970 :           sym.st_info = s->info.info;
    2048                 :        1970 :           sym.st_other = s->info.other;
    2049                 :             : 
    2050                 :             :           /* Keep track of the number of leading local symbols.  */
    2051         [ +  + ]:        1970 :           if (GELF_ST_BIND (sym.st_info) == STB_LOCAL)
    2052                 :             :             {
    2053         [ -  + ]:        1348 :               assert (shdr->sh_info == 1 + i);
    2054                 :        1348 :               shdr->sh_info = 1 + i + 1;
    2055                 :             :             }
    2056                 :             : 
    2057         [ -  + ]:        1970 :           ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, 1 + i,
    2058                 :             :                                            &sym, SHN_UNDEF),
    2059                 :             :                      _("cannot update symbol table: %s"));
    2060                 :             : 
    2061                 :             :         }
    2062                 :          20 :       elf_flagdata (symdata, ELF_C_SET, ELF_F_DIRTY);
    2063                 :          20 :       update_shdr (unstripped_symtab, shdr);
    2064                 :             : 
    2065                 :             :       /* Track which sections are adjusted during the first round
    2066                 :             :          of calls to adjust_relocs.  */
    2067                 :          20 :       bool scn_adjusted[unstripped_shnum];
    2068         [ +  - ]:          20 :       memset (scn_adjusted, 0, sizeof scn_adjusted);
    2069                 :             : 
    2070         [ +  - ]:          20 :       if (stripped_symtab != NULL)
    2071                 :             :         {
    2072                 :             :           /* Adjust any relocations referring to the old symbol table.  */
    2073                 :          20 :           const size_t old_sh_link = elf_ndxscn (stripped_symtab->scn);
    2074                 :          20 :           for (const struct section *sec = sections;
    2075         [ +  + ]:         480 :                sec < &sections[stripped_shnum - 1];
    2076                 :         460 :                ++sec)
    2077   [ +  +  +  + ]:         460 :             if (sec->outscn != NULL && sec->shdr.sh_link == old_sh_link)
    2078                 :             :               {
    2079                 :          94 :                 adjust_relocs (sec->outscn, sec->scn, &sec->shdr,
    2080                 :             :                                symndx_map, total_syms, shdr);
    2081                 :          94 :                 scn_adjusted[elf_ndxscn (sec->outscn)] = true;
    2082                 :             :               }
    2083                 :             :         }
    2084                 :             : 
    2085                 :             :       /* Also adjust references to the other old symbol table.  */
    2086                 :          20 :       adjust_all_relocs (unstripped, unstripped_symtab, shdr,
    2087                 :             :                          &symndx_map[stripped_nsym - 1],
    2088                 :          20 :                          total_syms - (stripped_nsym - 1),
    2089                 :             :                          scn_adjusted);
    2090                 :             : 
    2091                 :          20 :       free (symbols);
    2092                 :          20 :       free (symndx_map);
    2093                 :             :     }
    2094   [ +  -  -  - ]:          20 :   else if (stripped_symtab != NULL && stripped_shnum != unstripped_shnum)
    2095                 :           0 :     check_symtab_section_symbols (unstripped,
    2096                 :           0 :                                   stripped_ehdr->e_type == ET_REL,
    2097                 :           0 :                                   stripped_symtab->scn,
    2098                 :             :                                   unstripped_shnum, unstripped_shstrndx,
    2099                 :           0 :                                   stripped_symtab->outscn,
    2100                 :             :                                   stripped_shnum, stripped_shstrndx,
    2101                 :             :                                   debuglink);
    2102                 :             : 
    2103         [ +  + ]:          40 :   if (stripped_dynsym != NULL)
    2104                 :          24 :     (void) check_symtab_section_symbols (unstripped,
    2105                 :          24 :                                          stripped_ehdr->e_type == ET_REL,
    2106                 :          24 :                                          stripped_dynsym->outscn,
    2107                 :             :                                          unstripped_shnum,
    2108                 :             :                                          unstripped_shstrndx,
    2109                 :          24 :                                          stripped_dynsym->scn, stripped_shnum,
    2110                 :             :                                          stripped_shstrndx, debuglink);
    2111                 :             : 
    2112                 :             :   /* We need to preserve the layout of the stripped file so the
    2113                 :             :      phdrs will match up.  This requires us to do our own layout of
    2114                 :             :      the added sections.  We do manual layout even for ET_REL just
    2115                 :             :      so we can try to match what the original probably had.  */
    2116                 :             : 
    2117                 :          40 :   elf_flagelf (unstripped, ELF_C_SET, ELF_F_LAYOUT);
    2118                 :             : 
    2119         [ +  + ]:          40 :   if (offset == 0)
    2120                 :             :     /* For ET_REL we are starting the layout from scratch.  */
    2121                 :          16 :     offset = gelf_fsize (unstripped, ELF_T_EHDR, 1, EV_CURRENT);
    2122                 :             : 
    2123                 :             :   bool skip_reloc = false;
    2124                 :          80 :   do
    2125                 :             :     {
    2126                 :          80 :       skip_reloc = !skip_reloc;
    2127         [ +  + ]:      264900 :       for (size_t i = 0; i < unstripped_shnum - 1; ++i)
    2128         [ +  + ]:      264820 :         if (!placed[i])
    2129                 :             :           {
    2130                 :      132012 :             scn = elf_getscn (unstripped, 1 + i);
    2131                 :             : 
    2132                 :      132012 :             GElf_Shdr shdr_mem;
    2133                 :      132012 :             GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
    2134         [ -  + ]:      132012 :             ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
    2135                 :             : 
    2136                 :             :             /* We must make sure we have read in the data of all sections
    2137                 :             :                beforehand and marked them to be written out.  When we're
    2138                 :             :                modifying the existing file in place, we might overwrite
    2139                 :             :                this part of the file before we get to handling the section.  */
    2140                 :             : 
    2141         [ -  + ]:      132012 :             ELF_CHECK (elf_flagdata (elf_getdata (scn, NULL),
    2142                 :             :                                      ELF_C_SET, ELF_F_DIRTY),
    2143                 :             :                        _("cannot read section data: %s"));
    2144                 :             : 
    2145         [ +  + ]:      132012 :             if (skip_reloc
    2146         [ +  + ]:      131864 :                 && (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA))
    2147                 :         148 :               continue;
    2148                 :             : 
    2149                 :      131864 :             GElf_Off align = shdr->sh_addralign ?: 1;
    2150                 :      131864 :             offset = (offset + align - 1) & -align;
    2151                 :      131864 :             shdr->sh_offset = offset;
    2152         [ +  + ]:      131864 :             if (shdr->sh_type != SHT_NOBITS)
    2153                 :      131848 :               offset += shdr->sh_size;
    2154                 :             : 
    2155                 :      131864 :             update_shdr (scn, shdr);
    2156                 :             : 
    2157         [ +  + ]:      131864 :             if (unstripped_shstrndx == 1 + i)
    2158                 :             :               {
    2159                 :             :                 /* Place the section headers immediately after
    2160                 :             :                    .shstrtab, and update the ELF header.  */
    2161                 :             : 
    2162                 :          40 :                 GElf_Ehdr ehdr_mem;
    2163                 :          40 :                 GElf_Ehdr *ehdr = gelf_getehdr (unstripped, &ehdr_mem);
    2164         [ -  + ]:          40 :                 ELF_CHECK (ehdr != NULL, _("cannot get ELF header: %s"));
    2165                 :             : 
    2166                 :          40 :                 GElf_Off sh_align = gelf_getclass (unstripped) * 4;
    2167                 :          40 :                 offset = (offset + sh_align - 1) & -sh_align;
    2168                 :          40 :                 ehdr->e_shnum = unstripped_shnum;
    2169                 :          40 :                 ehdr->e_shoff = offset;
    2170                 :          40 :                 offset += unstripped_shnum * ehdr->e_shentsize;
    2171         [ -  + ]:          40 :                 ELF_CHECK (gelf_update_ehdr (unstripped, ehdr),
    2172                 :             :                            _("cannot update ELF header: %s"));
    2173                 :             :               }
    2174                 :             : 
    2175                 :      131864 :             placed[i] = true;
    2176                 :             :           }
    2177                 :             :     }
    2178         [ +  + ]:          80 :   while (skip_reloc);
    2179                 :             : 
    2180                 :          40 :   size_t phnum;
    2181         [ -  + ]:          40 :   ELF_CHECK (elf_getphdrnum (stripped, &phnum) == 0,
    2182                 :             :              _("cannot get number of program headers: %s"));
    2183                 :             : 
    2184         [ +  + ]:          40 :   if (phnum > 0)
    2185         [ +  - ]:          24 :     ELF_CHECK (gelf_newphdr (unstripped, phnum),
    2186                 :             :                _("cannot create program headers: %s"));
    2187                 :             : 
    2188                 :             :   /* Copy each program header from the stripped file.  */
    2189         [ +  + ]:         214 :   for (size_t i = 0; i < phnum; ++i)
    2190                 :             :     {
    2191                 :         174 :       GElf_Phdr phdr_mem;
    2192                 :         174 :       GElf_Phdr *phdr = gelf_getphdr (stripped, i, &phdr_mem);
    2193         [ -  + ]:         174 :       ELF_CHECK (phdr != NULL, _("cannot get program header: %s"));
    2194                 :             : 
    2195         [ -  + ]:         174 :       ELF_CHECK (gelf_update_phdr (unstripped, i, phdr),
    2196                 :             :                  _("cannot update program header: %s"));
    2197                 :             :     }
    2198                 :             : 
    2199                 :             :   /* Finally, write out the file.  */
    2200         [ -  + ]:          40 :   ELF_CHECK (elf_update (unstripped, ELF_C_WRITE) > 0,
    2201                 :             :              _("cannot write output file: %s"));
    2202                 :             : 
    2203         [ -  + ]:          40 :   if (strtab != NULL)
    2204                 :             :     {
    2205                 :           0 :       dwelf_strtab_free (strtab);
    2206                 :           0 :       free (strtab_data->d_buf);
    2207                 :             :     }
    2208                 :             : 
    2209         [ +  + ]:          40 :   if (symstrtab != NULL)
    2210                 :             :     {
    2211                 :          20 :       dwelf_strtab_free (symstrtab);
    2212                 :          20 :       free (symstrdata->d_buf);
    2213                 :             :     }
    2214                 :          40 :   free_new_data ();
    2215                 :          40 : }
    2216                 :             : 
    2217                 :             : /* Process one pair of files, already opened.  */
    2218                 :             : static void
    2219                 :          48 : handle_file (const char *output_file, bool create_dirs,
    2220                 :             :              Elf *stripped, const GElf_Ehdr *stripped_ehdr,
    2221                 :             :              Elf *unstripped)
    2222                 :             : {
    2223                 :          48 :   size_t phnum;
    2224         [ -  + ]:          48 :   ELF_CHECK (elf_getphdrnum (stripped, &phnum) == 0,
    2225                 :             :              _("cannot get number of program headers: %s"));
    2226                 :             : 
    2227                 :             :   /* Determine the address bias between the debuginfo file and the main
    2228                 :             :      file, which may have been modified by prelinking.  */
    2229                 :          48 :   GElf_Addr bias = 0;
    2230         [ +  - ]:          48 :   if (unstripped != NULL)
    2231         [ +  + ]:          80 :     for (size_t i = 0; i < phnum; ++i)
    2232                 :             :       {
    2233                 :          56 :         GElf_Phdr phdr_mem;
    2234                 :          56 :         GElf_Phdr *phdr = gelf_getphdr (stripped, i, &phdr_mem);
    2235         [ -  + ]:          56 :         ELF_CHECK (phdr != NULL, _("cannot get program header: %s"));
    2236         [ +  + ]:          56 :         if (phdr->p_type == PT_LOAD)
    2237                 :             :           {
    2238                 :          24 :             GElf_Phdr unstripped_phdr_mem;
    2239                 :          24 :             GElf_Phdr *unstripped_phdr = gelf_getphdr (unstripped, i,
    2240                 :             :                                                        &unstripped_phdr_mem);
    2241         [ -  + ]:          24 :             ELF_CHECK (unstripped_phdr != NULL,
    2242                 :             :                        _("cannot get program header: %s"));
    2243                 :          24 :             bias = phdr->p_vaddr - unstripped_phdr->p_vaddr;
    2244                 :          24 :             break;
    2245                 :             :           }
    2246                 :             :       }
    2247                 :             : 
    2248                 :             :   /* One day we could adjust all the DWARF data (like prelink itself does).  */
    2249         [ +  - ]:          48 :   if (bias != 0)
    2250                 :             :     {
    2251         [ #  # ]:           0 :       if (output_file == NULL)
    2252                 :           0 :         error (0, 0, _("\
    2253                 :             : DWARF data not adjusted for prelinking bias; consider prelink -u"));
    2254                 :             :       else
    2255                 :           0 :         error (0, 0, _("\
    2256                 :             : DWARF data in '%s' not adjusted for prelinking bias; consider prelink -u"),
    2257                 :             :                output_file);
    2258                 :             :     }
    2259                 :             : 
    2260         [ +  + ]:          48 :   if (output_file == NULL)
    2261                 :             :     /* Modify the unstripped file in place.  */
    2262                 :           8 :     copy_elided_sections (unstripped, stripped, stripped_ehdr, bias);
    2263                 :             :   else
    2264                 :             :     {
    2265         [ -  + ]:          40 :       if (create_dirs)
    2266                 :           0 :         make_directories (output_file);
    2267                 :             : 
    2268                 :             :       /* Copy the unstripped file and then modify it.  */
    2269                 :          80 :       int outfd = open (output_file, O_RDWR | O_CREAT,
    2270         [ +  + ]:          40 :                         (stripped_ehdr->e_type == ET_REL
    2271                 :             :                          ? DEFFILEMODE : ACCESSPERMS));
    2272         [ -  + ]:          40 :       if (outfd < 0)
    2273                 :           0 :         error_exit (errno, _("cannot open '%s'"), output_file);
    2274                 :          40 :       Elf *outelf = elf_begin (outfd, ELF_C_WRITE, NULL);
    2275         [ -  + ]:          40 :       ELF_CHECK (outelf != NULL, _("cannot create ELF descriptor: %s"));
    2276                 :             : 
    2277         [ -  + ]:          40 :       if (unstripped == NULL)
    2278                 :             :         {
    2279                 :             :           /* Actually, we are just copying out the main file as it is.  */
    2280                 :           0 :           copy_elf (outelf, stripped);
    2281         [ #  # ]:           0 :           if (stripped_ehdr->e_type != ET_REL)
    2282                 :           0 :             elf_flagelf (outelf, ELF_C_SET, ELF_F_LAYOUT);
    2283         [ #  # ]:           0 :           ELF_CHECK (elf_update (outelf, ELF_C_WRITE) > 0,
    2284                 :             :                      _("cannot write output file: %s"));
    2285                 :             :         }
    2286                 :             :       else
    2287                 :             :         {
    2288                 :          40 :           copy_elf (outelf, unstripped);
    2289                 :          40 :           copy_elided_sections (outelf, stripped, stripped_ehdr, bias);
    2290                 :             :         }
    2291                 :             : 
    2292                 :          32 :       elf_end (outelf);
    2293                 :          32 :       close (outfd);
    2294                 :             :     }
    2295                 :          40 : }
    2296                 :             : 
    2297                 :             : static int
    2298                 :          96 : open_file (const char *file, bool writable)
    2299                 :             : {
    2300         [ +  + ]:          96 :   int fd = open (file, writable ? O_RDWR : O_RDONLY);
    2301         [ -  + ]:          96 :   if (fd < 0)
    2302                 :           0 :     error_exit (errno, _("cannot open '%s'"), file);
    2303                 :          96 :   return fd;
    2304                 :             : }
    2305                 :             : 
    2306                 :             : /* Warn, and exit if not forced to continue, if some ELF header
    2307                 :             :    sanity check for the stripped and unstripped files failed.  */
    2308                 :             : static void
    2309                 :           0 : warn (const char *msg, bool force,
    2310                 :             :       const char *stripped_file, const char *unstripped_file)
    2311                 :             : {
    2312   [ #  #  #  # ]:           0 :   error (force ? 0 : EXIT_FAILURE, 0, "%s'%s' and '%s' %s%s.",
    2313                 :           0 :          force ? _("WARNING: ") : "",
    2314                 :             :          stripped_file, unstripped_file, msg,
    2315                 :           0 :          force ? "" : _(", use --force"));
    2316                 :           0 : }
    2317                 :             : 
    2318                 :             : /* Handle a pair of files we need to open by name.  */
    2319                 :             : static void
    2320                 :          48 : handle_explicit_files (const char *output_file, bool create_dirs, bool force,
    2321                 :             :                        const char *stripped_file, const char *unstripped_file)
    2322                 :             : {
    2323                 :          48 :   int stripped_fd = open_file (stripped_file, false);
    2324                 :          48 :   Elf *stripped = elf_begin (stripped_fd, ELF_C_READ, NULL);
    2325                 :          48 :   GElf_Ehdr stripped_ehdr;
    2326         [ -  + ]:          48 :   ELF_CHECK (gelf_getehdr (stripped, &stripped_ehdr),
    2327                 :             :              _("cannot create ELF descriptor: %s"));
    2328                 :             : 
    2329                 :          48 :   int unstripped_fd = -1;
    2330                 :          48 :   Elf *unstripped = NULL;
    2331         [ +  - ]:          48 :   if (unstripped_file != NULL)
    2332                 :             :     {
    2333                 :          48 :       unstripped_fd = open_file (unstripped_file, output_file == NULL);
    2334         [ +  + ]:          88 :       unstripped = elf_begin (unstripped_fd,
    2335                 :             :                               (output_file == NULL ? ELF_C_RDWR : ELF_C_READ),
    2336                 :             :                               NULL);
    2337                 :          48 :       GElf_Ehdr unstripped_ehdr;
    2338         [ -  + ]:          48 :       ELF_CHECK (gelf_getehdr (unstripped, &unstripped_ehdr),
    2339                 :             :                  _("cannot create ELF descriptor: %s"));
    2340                 :             : 
    2341         [ -  + ]:          48 :       if (memcmp (stripped_ehdr.e_ident,
    2342                 :             :                   unstripped_ehdr.e_ident, EI_NIDENT) != 0)
    2343                 :           0 :         warn (_("ELF header identification (e_ident) different"), force,
    2344                 :             :               stripped_file, unstripped_file);
    2345                 :             : 
    2346         [ -  + ]:          48 :       if (stripped_ehdr.e_type != unstripped_ehdr.e_type)
    2347                 :           0 :         warn (_("ELF header type (e_type) different"), force,
    2348                 :             :               stripped_file, unstripped_file);
    2349                 :             : 
    2350         [ -  + ]:          48 :       if (stripped_ehdr.e_machine != unstripped_ehdr.e_machine)
    2351                 :           0 :         warn (_("ELF header machine type (e_machine) different"), force,
    2352                 :             :               stripped_file, unstripped_file);
    2353                 :             : 
    2354         [ -  + ]:          48 :       if (stripped_ehdr.e_phnum < unstripped_ehdr.e_phnum)
    2355                 :           0 :         warn (_("stripped program header (e_phnum) smaller than unstripped"),
    2356                 :             :               force, stripped_file, unstripped_file);
    2357                 :             :     }
    2358                 :             : 
    2359                 :          48 :   handle_file (output_file, create_dirs, stripped, &stripped_ehdr, unstripped);
    2360                 :             : 
    2361                 :          40 :   elf_end (stripped);
    2362                 :          40 :   close (stripped_fd);
    2363                 :             : 
    2364                 :          40 :   elf_end (unstripped);
    2365                 :          40 :   close (unstripped_fd);
    2366                 :          40 : }
    2367                 :             : 
    2368                 :             : 
    2369                 :             : /* Handle a pair of files opened implicitly by libdwfl for one module.  */
    2370                 :             : static void
    2371                 :           0 : handle_dwfl_module (const char *output_file, bool create_dirs, bool force,
    2372                 :             :                     Dwfl_Module *mod, bool all, bool ignore, bool relocate)
    2373                 :             : {
    2374                 :           0 :   GElf_Addr bias;
    2375                 :           0 :   Elf *stripped = dwfl_module_getelf (mod, &bias);
    2376         [ #  # ]:           0 :   if (stripped == NULL)
    2377                 :             :     {
    2378         [ #  # ]:           0 :       if (ignore)
    2379                 :           0 :         return;
    2380                 :             : 
    2381                 :           0 :       const char *file;
    2382                 :           0 :       const char *modname = dwfl_module_info (mod, NULL, NULL, NULL,
    2383                 :             :                                               NULL, NULL, &file, NULL);
    2384         [ #  # ]:           0 :       if (file == NULL)
    2385                 :           0 :         error_exit (0,
    2386                 :             :                     _("cannot find stripped file for module '%s': %s"),
    2387                 :             :                     modname, dwfl_errmsg (-1));
    2388                 :             :       else
    2389                 :           0 :         error_exit (0,
    2390                 :             :                     _("cannot open stripped file '%s' for module '%s': %s"),
    2391                 :             :                     modname, file, dwfl_errmsg (-1));
    2392                 :             :     }
    2393                 :             : 
    2394                 :           0 :   Elf *debug = dwarf_getelf (dwfl_module_getdwarf (mod, &bias));
    2395         [ #  # ]:           0 :   if (debug == NULL && !all)
    2396                 :             :     {
    2397         [ #  # ]:           0 :       if (ignore)
    2398                 :           0 :         return;
    2399                 :             : 
    2400                 :           0 :       const char *file;
    2401                 :           0 :       const char *modname = dwfl_module_info (mod, NULL, NULL, NULL,
    2402                 :             :                                               NULL, NULL, NULL, &file);
    2403         [ #  # ]:           0 :       if (file == NULL)
    2404                 :           0 :         error_exit (0,
    2405                 :             :                     _("cannot find debug file for module '%s': %s"),
    2406                 :             :                     modname, dwfl_errmsg (-1));
    2407                 :             :       else
    2408                 :           0 :         error_exit (0,
    2409                 :             :                     _("cannot open debug file '%s' for module '%s': %s"),
    2410                 :             :                     modname, file, dwfl_errmsg (-1));
    2411                 :             :     }
    2412                 :             : 
    2413         [ #  # ]:           0 :   if (debug == stripped)
    2414                 :             :     {
    2415         [ #  # ]:           0 :       if (all)
    2416                 :             :         debug = NULL;
    2417                 :             :       else
    2418                 :             :         {
    2419                 :           0 :           const char *file;
    2420                 :           0 :           const char *modname = dwfl_module_info (mod, NULL, NULL, NULL,
    2421                 :             :                                                   NULL, NULL, &file, NULL);
    2422                 :           0 :           error_exit (0, _("module '%s' file '%s' is not stripped"),
    2423                 :             :                       modname, file);
    2424                 :             :         }
    2425                 :             :     }
    2426                 :             : 
    2427                 :           0 :   GElf_Ehdr stripped_ehdr;
    2428         [ #  # ]:           0 :   ELF_CHECK (gelf_getehdr (stripped, &stripped_ehdr),
    2429                 :             :              _("cannot create ELF descriptor: %s"));
    2430                 :             : 
    2431         [ #  # ]:           0 :   if (stripped_ehdr.e_type == ET_REL)
    2432                 :             :     {
    2433         [ #  # ]:           0 :       if (!relocate)
    2434                 :             :         {
    2435                 :             :           /* We can't use the Elf handles already open,
    2436                 :             :              because the DWARF sections have been relocated.  */
    2437                 :             : 
    2438                 :           0 :           const char *stripped_file = NULL;
    2439                 :           0 :           const char *unstripped_file = NULL;
    2440                 :           0 :           (void) dwfl_module_info (mod, NULL, NULL, NULL, NULL, NULL,
    2441                 :             :                                    &stripped_file, &unstripped_file);
    2442                 :             : 
    2443                 :           0 :           handle_explicit_files (output_file, create_dirs, force,
    2444                 :             :                                  stripped_file, unstripped_file);
    2445                 :           0 :           return;
    2446                 :             :         }
    2447                 :             : 
    2448                 :             :       /* Relocation is what we want!  This ensures that all sections that can
    2449                 :             :          get sh_addr values assigned have them, even ones not used in DWARF.
    2450                 :             :          They might still be used in the symbol table.  */
    2451         [ #  # ]:           0 :       if (dwfl_module_relocations (mod) < 0)
    2452                 :           0 :         error_exit (0,
    2453                 :             :                     _("cannot cache section addresses for module '%s': %s"),
    2454                 :             :                     dwfl_module_info (mod, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
    2455                 :             :                     dwfl_errmsg (-1));
    2456                 :             :     }
    2457                 :             : 
    2458                 :           0 :   handle_file (output_file, create_dirs, stripped, &stripped_ehdr, debug);
    2459                 :             : }
    2460                 :             : 
    2461                 :             : /* Handle one module being written to the output directory.  */
    2462                 :             : static void
    2463                 :           0 : handle_output_dir_module (const char *output_dir, Dwfl_Module *mod, bool force,
    2464                 :             :                           bool all, bool ignore, bool modnames, bool relocate)
    2465                 :             : {
    2466         [ #  # ]:           0 :   if (! modnames)
    2467                 :             :     {
    2468                 :             :       /* Make sure we've searched for the ELF file.  */
    2469                 :           0 :       GElf_Addr bias;
    2470                 :           0 :       (void) dwfl_module_getelf (mod, &bias);
    2471                 :             :     }
    2472                 :             : 
    2473                 :           0 :   const char *file;
    2474                 :           0 :   const char *name = dwfl_module_info (mod, NULL, NULL, NULL,
    2475                 :             :                                        NULL, NULL, &file, NULL);
    2476                 :             : 
    2477   [ #  #  #  # ]:           0 :   if (file == NULL && ignore)
    2478                 :           0 :     return;
    2479                 :             : 
    2480         [ #  # ]:           0 :   char *output_file = xasprintf ("%s/%s", output_dir,
    2481                 :           0 :                                  modnames ? xbasename (name) : file);
    2482                 :             : 
    2483                 :           0 :   handle_dwfl_module (output_file, true, force, mod, all, ignore, relocate);
    2484                 :             : 
    2485                 :           0 :   free (output_file);
    2486                 :             : }
    2487                 :             : 
    2488                 :             : 
    2489                 :             : static void
    2490                 :          54 : list_module (Dwfl_Module *mod)
    2491                 :             : {
    2492                 :             :   /* Make sure we have searched for the files.  */
    2493                 :          54 :   GElf_Addr bias;
    2494                 :          54 :   bool have_elf = dwfl_module_getelf (mod, &bias) != NULL;
    2495                 :          54 :   bool have_dwarf = dwfl_module_getdwarf (mod, &bias) != NULL;
    2496                 :             : 
    2497                 :          54 :   const char *file;
    2498                 :          54 :   const char *debug;
    2499                 :          54 :   Dwarf_Addr start;
    2500                 :          54 :   Dwarf_Addr end;
    2501                 :          54 :   const char *name = dwfl_module_info (mod, NULL, &start, &end,
    2502                 :             :                                        NULL, NULL, &file, &debug);
    2503   [ +  +  -  +  :          54 :   if (file != NULL && debug != NULL && (debug == file || !strcmp (debug, file)))
             -  -  -  - ]
    2504                 :           0 :     debug = ".";
    2505                 :             : 
    2506                 :          54 :   const unsigned char *id;
    2507                 :          54 :   GElf_Addr id_vaddr;
    2508                 :          54 :   int id_len = dwfl_module_build_id (mod, &id, &id_vaddr);
    2509                 :             : 
    2510                 :          54 :   printf ("%#" PRIx64 "+%#" PRIx64 " ", start, end - start);
    2511                 :             : 
    2512         [ +  + ]:          54 :   if (id_len > 0)
    2513                 :             :     {
    2514                 :         920 :       do
    2515                 :         920 :         printf ("%02" PRIx8, *id++);
    2516         [ +  + ]:         920 :       while (--id_len > 0);
    2517         [ +  - ]:          46 :       if (id_vaddr != 0)
    2518                 :          46 :         printf ("@%#" PRIx64, id_vaddr);
    2519                 :             :     }
    2520                 :             :   else
    2521                 :           8 :     putchar ('-');
    2522                 :             : 
    2523   [ +  -  +  + ]:         106 :   printf (" %s %s %s\n",
    2524         [ +  + ]:          54 :           file ?: have_elf ? "." : "-",
    2525         [ +  - ]:          54 :           debug ?: have_dwarf ? "." : "-",
    2526                 :             :           name);
    2527                 :          54 : }
    2528                 :             : 
    2529                 :             : 
    2530                 :             : struct match_module_info
    2531                 :             : {
    2532                 :             :   char **patterns;
    2533                 :             :   Dwfl_Module *found;
    2534                 :             :   bool match_files;
    2535                 :             : };
    2536                 :             : 
    2537                 :             : static int
    2538                 :          54 : match_module (Dwfl_Module *mod,
    2539                 :             :               void **userdata __attribute__ ((unused)),
    2540                 :             :               const char *name,
    2541                 :             :               Dwarf_Addr start __attribute__ ((unused)),
    2542                 :             :               void *arg)
    2543                 :             : {
    2544                 :          54 :   struct match_module_info *info = arg;
    2545                 :             : 
    2546         [ +  - ]:          54 :   if (info->patterns[0] == NULL) /* Match all.  */
    2547                 :             :     {
    2548                 :          54 :     match:
    2549                 :          54 :       info->found = mod;
    2550                 :          54 :       return DWARF_CB_ABORT;
    2551                 :             :     }
    2552                 :             : 
    2553         [ #  # ]:           0 :   if (info->match_files)
    2554                 :             :     {
    2555                 :             :       /* Make sure we've searched for the ELF file.  */
    2556                 :           0 :       GElf_Addr bias;
    2557                 :           0 :       (void) dwfl_module_getelf (mod, &bias);
    2558                 :             : 
    2559                 :           0 :       const char *file;
    2560                 :           0 :       const char *check = dwfl_module_info (mod, NULL, NULL, NULL,
    2561                 :             :                                             NULL, NULL, &file, NULL);
    2562   [ #  #  #  #  :           0 :       if (check == NULL || strcmp (check, name) != 0 || file == NULL)
                   #  # ]
    2563                 :           0 :         return DWARF_CB_OK;
    2564                 :             : 
    2565                 :           0 :       name = file;
    2566                 :             :     }
    2567                 :             : 
    2568         [ #  # ]:           0 :   for (char **p = info->patterns; *p != NULL; ++p)
    2569         [ #  # ]:           0 :     if (fnmatch (*p, name, 0) == 0)
    2570                 :           0 :       goto match;
    2571                 :             : 
    2572                 :             :   return DWARF_CB_OK;
    2573                 :             : }
    2574                 :             : 
    2575                 :             : /* Handle files opened implicitly via libdwfl.  */
    2576                 :             : static void
    2577                 :          10 : handle_implicit_modules (const struct arg_info *info)
    2578                 :             : {
    2579                 :          10 :   struct match_module_info mmi = { info->args, NULL, info->match_files };
    2580                 :          10 :   ptrdiff_t offset = dwfl_getmodules (info->dwfl, &match_module, &mmi, 0);
    2581         [ -  + ]:          10 :   if (offset == 0)
    2582                 :           0 :     error_exit (0, _("no matching modules found"));
    2583                 :             : 
    2584         [ +  - ]:          10 :   if (info->list)
    2585                 :          54 :     do
    2586                 :          54 :       list_module (mmi.found);
    2587                 :         108 :     while ((offset = dwfl_getmodules (info->dwfl, &match_module, &mmi,
    2588         [ +  + ]:          54 :                                       offset)) > 0);
    2589         [ #  # ]:           0 :   else if (info->output_dir == NULL)
    2590                 :             :     {
    2591         [ #  # ]:           0 :       if (dwfl_getmodules (info->dwfl, &match_module, &mmi, offset) != 0)
    2592                 :           0 :         error_exit (0, _("matched more than one module"));
    2593                 :           0 :       handle_dwfl_module (info->output_file, false, info->force, mmi.found,
    2594                 :           0 :                           info->all, info->ignore, info->relocate);
    2595                 :             :     }
    2596                 :             :   else
    2597                 :           0 :     do
    2598                 :           0 :       handle_output_dir_module (info->output_dir, mmi.found, info->force,
    2599                 :           0 :                                 info->all, info->ignore,
    2600                 :           0 :                                 info->modnames, info->relocate);
    2601                 :           0 :     while ((offset = dwfl_getmodules (info->dwfl, &match_module, &mmi,
    2602         [ #  # ]:           0 :                                       offset)) > 0);
    2603                 :          10 : }
    2604                 :             : 
    2605                 :             : int
    2606                 :          58 : main (int argc, char **argv)
    2607                 :             : {
    2608                 :             :   /* We use no threads here which can interfere with handling a stream.  */
    2609                 :          58 :   __fsetlocking (stdin, FSETLOCKING_BYCALLER);
    2610                 :          58 :   __fsetlocking (stdout, FSETLOCKING_BYCALLER);
    2611                 :          58 :   __fsetlocking (stderr, FSETLOCKING_BYCALLER);
    2612                 :             : 
    2613                 :             :   /* Set locale.  */
    2614                 :          58 :   setlocale (LC_ALL, "");
    2615                 :             : 
    2616                 :             :   /* Make sure the message catalog can be found.  */
    2617                 :          58 :   bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
    2618                 :             : 
    2619                 :             :   /* Initialize the message catalog.  */
    2620                 :          58 :   textdomain (PACKAGE_TARNAME);
    2621                 :             : 
    2622                 :             :   /* Parse and process arguments.  */
    2623                 :          58 :   const struct argp_child argp_children[] =
    2624                 :             :     {
    2625                 :             :       {
    2626                 :          58 :         .argp = dwfl_standard_argp (),
    2627                 :             :         .header = N_("Input selection options:"),
    2628                 :             :         .group = 1,
    2629                 :             :       },
    2630                 :             :       { .argp = NULL },
    2631                 :             :     };
    2632                 :          58 :   const struct argp argp =
    2633                 :             :     {
    2634                 :             :       .options = options,
    2635                 :             :       .parser = parse_opt,
    2636                 :             :       .children = argp_children,
    2637                 :             :       .args_doc = N_("STRIPPED-FILE DEBUG-FILE\n[MODULE...]"),
    2638                 :             :       .doc = N_("\
    2639                 :             : Combine stripped files with separate symbols and debug information.\n\
    2640                 :             : \n\
    2641                 :             : The first form puts the result in DEBUG-FILE if -o was not given.\n\
    2642                 :             : \n\
    2643                 :             : MODULE arguments give file name patterns matching modules to process.\n\
    2644                 :             : With -f these match the file name of the main (stripped) file \
    2645                 :             : (slashes are never special), otherwise they match the simple module names.  \
    2646                 :             : With no arguments, process all modules found.\n\
    2647                 :             : \n\
    2648                 :             : Multiple modules are written to files under OUTPUT-DIRECTORY, \
    2649                 :             : creating subdirectories as needed.  \
    2650                 :             : With -m these files have simple module names, otherwise they have the \
    2651                 :             : name of the main file complete with directory underneath OUTPUT-DIRECTORY.\n\
    2652                 :             : \n\
    2653                 :             : With -n no files are written, but one line to standard output for each module:\
    2654                 :             : \n\tSTART+SIZE BUILDID FILE DEBUGFILE MODULENAME\n\
    2655                 :             : START and SIZE are hexadecimal giving the address bounds of the module.  \
    2656                 :             : BUILDID is hexadecimal for the build ID bits, or - if no ID is known; \
    2657                 :             : the hexadecimal may be followed by @0xADDR giving the address where the \
    2658                 :             : ID resides if that is known.  \
    2659                 :             : FILE is the file name found for the module, or - if none was found, \
    2660                 :             : or . if an ELF image is available but not from any named file.  \
    2661                 :             : DEBUGFILE is the separate debuginfo file name, \
    2662                 :             : or - if no debuginfo was found, or . if FILE contains the debug information.\
    2663                 :             : ")
    2664                 :             :     };
    2665                 :             : 
    2666                 :          58 :   int remaining;
    2667                 :          58 :   struct arg_info info = { .args = NULL };
    2668                 :          58 :   error_t result = argp_parse (&argp, argc, argv, 0, &remaining, &info);
    2669         [ +  + ]:          58 :   if (result == ENOSYS)
    2670         [ -  + ]:          48 :     assert (info.dwfl == NULL);
    2671         [ +  - ]:          10 :   else if (result)
    2672                 :             :     return EXIT_FAILURE;
    2673         [ -  + ]:          58 :   assert (info.args != NULL);
    2674                 :             : 
    2675                 :             :   /* Tell the library which version we are expecting.  */
    2676                 :          58 :   elf_version (EV_CURRENT);
    2677                 :             : 
    2678         [ +  + ]:          58 :   if (info.dwfl == NULL)
    2679                 :             :     {
    2680         [ -  + ]:          48 :       assert (result == ENOSYS);
    2681                 :             : 
    2682         [ -  + ]:          48 :       if (info.output_dir != NULL)
    2683                 :             :         {
    2684                 :           0 :           char *file = xasprintf ("%s/%s", info.output_dir, info.args[0]);
    2685                 :           0 :           handle_explicit_files (file, true, info.force,
    2686                 :           0 :                                  info.args[0], info.args[1]);
    2687                 :           0 :           free (file);
    2688                 :             :         }
    2689                 :             :       else
    2690                 :          48 :         handle_explicit_files (info.output_file, false, info.force,
    2691                 :          48 :                                info.args[0], info.args[1]);
    2692                 :             :     }
    2693                 :             :   else
    2694                 :             :     {
    2695                 :             :       /* parse_opt checked this.  */
    2696   [ +  -  +  -  :          10 :       assert (info.output_file != NULL || info.output_dir != NULL || info.list);
                   -  + ]
    2697                 :             : 
    2698                 :          10 :       handle_implicit_modules (&info);
    2699                 :             : 
    2700                 :          10 :       dwfl_end (info.dwfl);
    2701                 :             :     }
    2702                 :             : 
    2703                 :             :   return 0;
    2704                 :             : }
    2705                 :             : 
    2706                 :             : 
    2707                 :             : #include "debugpred.h"
        

Generated by: LCOV version 2.0-1