Merge commit 'origin/master' into pmachata/pending
[platform/upstream/elfutils.git] / src / strip.c
1 /* Discard section not used at runtime from object files.
2    Copyright (C) 2000-2008, 2009 Red Hat, Inc.
3    This file is part of Red Hat elfutils.
4    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
5
6    Red Hat elfutils is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by the
8    Free Software Foundation; version 2 of the License.
9
10    Red Hat elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with Red Hat elfutils; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
19    Red Hat elfutils is an included package of the Open Invention Network.
20    An included package of the Open Invention Network is a package for which
21    Open Invention Network licensees cross-license their patents.  No patent
22    license is granted, either expressly or impliedly, by designation as an
23    included package.  Should you wish to participate in the Open Invention
24    Network licensing program, please visit www.openinventionnetwork.com
25    <http://www.openinventionnetwork.com>.  */
26
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30
31 #include <argp.h>
32 #include <assert.h>
33 #include <byteswap.h>
34 #include <endian.h>
35 #include <error.h>
36 #include <fcntl.h>
37 #include <gelf.h>
38 #include <libelf.h>
39 #include <libintl.h>
40 #include <locale.h>
41 #include <mcheck.h>
42 #include <stdbool.h>
43 #include <stdio.h>
44 #include <stdio_ext.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <sys/param.h>
49 #include <sys/time.h>
50
51 #include <elf-knowledge.h>
52 #include <libebl.h>
53 #include <system.h>
54
55
56 /* Name and version of program.  */
57 static void print_version (FILE *stream, struct argp_state *state);
58 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
59
60 /* Bug report address.  */
61 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
62
63
64 /* Values for the parameters which have no short form.  */
65 #define OPT_REMOVE_COMMENT      0x100
66 #define OPT_PERMISSIVE          0x101
67
68
69 /* Definitions of arguments for argp functions.  */
70 static const struct argp_option options[] =
71 {
72   { NULL, 0, NULL, 0, N_("Output selection:"), 0 },
73   { "output", 'o', "FILE", 0, N_("Place stripped output into FILE"), 0 },
74   { NULL, 'f', "FILE", 0, N_("Extract the removed sections into FILE"), 0 },
75   { NULL, 'F', "FILE", 0, N_("Embed name FILE instead of -f argument"), 0 },
76
77   { NULL, 0, NULL, 0, N_("Output options:"), 0 },
78   { "strip-all", 's', NULL, OPTION_HIDDEN, NULL, 0 },
79   { "strip-debug", 'g', NULL, 0, N_("Remove all debugging symbols"), 0 },
80   { NULL, 'd', NULL, OPTION_ALIAS, NULL, 0 },
81   { NULL, 'S', NULL, OPTION_ALIAS, NULL, 0 },
82   { "preserve-dates", 'p', NULL, 0,
83     N_("Copy modified/access timestamps to the output"), 0 },
84   { "remove-comment", OPT_REMOVE_COMMENT, NULL, 0,
85     N_("Remove .comment section"), 0 },
86   { "remove-section", 'R', "SECTION", OPTION_HIDDEN, NULL, 0 },
87   { "permissive", OPT_PERMISSIVE, NULL, 0,
88     N_("Relax a few rules to handle slightly broken ELF files"), 0 },
89   { NULL, 0, NULL, 0, NULL, 0 }
90 };
91
92 /* Short description of program.  */
93 static const char doc[] = N_("Discard symbols from object files.");
94
95 /* Strings for arguments in help texts.  */
96 static const char args_doc[] = N_("[FILE...]");
97
98 /* Prototype for option handler.  */
99 static error_t parse_opt (int key, char *arg, struct argp_state *state);
100
101 /* Data structure to communicate with argp functions.  */
102 static struct argp argp =
103 {
104   options, parse_opt, args_doc, doc, NULL, NULL, NULL
105 };
106
107
108 /* Print symbols in file named FNAME.  */
109 static int process_file (const char *fname);
110
111 /* Handle one ELF file.  */
112 static int handle_elf (int fd, Elf *elf, const char *prefix,
113                        const char *fname, mode_t mode, struct timeval tvp[2]);
114
115 /* Handle all files contained in the archive.  */
116 static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
117                       struct timeval tvp[2]);
118
119 #define INTERNAL_ERROR(fname) \
120   error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s-%s): %s"),      \
121          fname, __LINE__, PACKAGE_VERSION, __DATE__, elf_errmsg (-1))
122
123
124 /* Name of the output file.  */
125 static const char *output_fname;
126
127 /* Name of the debug output file.  */
128 static const char *debug_fname;
129
130 /* Name to pretend the debug output file has.  */
131 static const char *debug_fname_embed;
132
133 /* If true output files shall have same date as the input file.  */
134 static bool preserve_dates;
135
136 /* If true .comment sections will be removed.  */
137 static bool remove_comment;
138
139 /* If true remove all debug sections.  */
140 static bool remove_debug;
141
142 /* If true relax some ELF rules for input files.  */
143 static bool permissive;
144
145
146 int
147 main (int argc, char *argv[])
148 {
149   int remaining;
150   int result = 0;
151
152   /* Make memory leak detection possible.  */
153   mtrace ();
154
155   /* We use no threads here which can interfere with handling a stream.  */
156   __fsetlocking (stdin, FSETLOCKING_BYCALLER);
157   __fsetlocking (stdout, FSETLOCKING_BYCALLER);
158   __fsetlocking (stderr, FSETLOCKING_BYCALLER);
159
160   /* Set locale.  */
161   setlocale (LC_ALL, "");
162
163   /* Make sure the message catalog can be found.  */
164   bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
165
166   /* Initialize the message catalog.  */
167   textdomain (PACKAGE_TARNAME);
168
169   /* Parse and process arguments.  */
170   if (argp_parse (&argp, argc, argv, 0, &remaining, NULL) != 0)
171     return EXIT_FAILURE;
172
173   /* Tell the library which version we are expecting.  */
174   elf_version (EV_CURRENT);
175
176   if (remaining == argc)
177     /* The user didn't specify a name so we use a.out.  */
178     result = process_file ("a.out");
179   else
180     {
181       /* If we have seen the '-o' or '-f' option there must be exactly one
182          input file.  */
183       if ((output_fname != NULL || debug_fname != NULL)
184           && remaining + 1 < argc)
185         error (EXIT_FAILURE, 0, gettext ("\
186 Only one input file allowed together with '-o' and '-f'"));
187
188       /* Process all the remaining files.  */
189       do
190         result |= process_file (argv[remaining]);
191       while (++remaining < argc);
192     }
193
194   return result;
195 }
196
197
198 /* Print the version information.  */
199 static void
200 print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
201 {
202   fprintf (stream, "strip (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
203   fprintf (stream, gettext ("\
204 Copyright (C) %s Red Hat, Inc.\n\
205 This is free software; see the source for copying conditions.  There is NO\n\
206 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
207 "), "2009");
208   fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
209 }
210
211
212 /* Handle program arguments.  */
213 static error_t
214 parse_opt (int key, char *arg, struct argp_state *state)
215 {
216   switch (key)
217     {
218     case 'f':
219       if (debug_fname != NULL)
220         {
221           error (0, 0, gettext ("-f option specified twice"));
222           return EINVAL;
223         }
224       debug_fname = arg;
225       break;
226
227     case 'F':
228       if (debug_fname_embed != NULL)
229         {
230           error (0, 0, gettext ("-F option specified twice"));
231           return EINVAL;
232         }
233       debug_fname_embed = arg;
234       break;
235
236     case 'o':
237       if (output_fname != NULL)
238         {
239           error (0, 0, gettext ("-o option specified twice"));
240           return EINVAL;
241         }
242       output_fname = arg;
243       break;
244
245     case 'p':
246       preserve_dates = true;
247       break;
248
249     case OPT_REMOVE_COMMENT:
250       remove_comment = true;
251       break;
252
253     case 'R':
254       if (!strcmp (arg, ".comment"))
255         remove_comment = true;
256       else
257         {
258           argp_error (state,
259                       gettext ("-R option supports only .comment section"));
260           return EINVAL;
261         }
262       break;
263
264     case 'g':
265     case 'd':
266     case 'S':
267       remove_debug = true;
268       break;
269
270     case OPT_PERMISSIVE:
271       permissive = true;
272       break;
273
274     case 's':                   /* Ignored for compatibility.  */
275       break;
276
277     default:
278       return ARGP_ERR_UNKNOWN;
279     }
280   return 0;
281 }
282
283
284 static int
285 process_file (const char *fname)
286 {
287   /* If we have to preserve the modify and access timestamps get them
288      now.  We cannot use fstat() after opening the file since the open
289      would change the access time.  */
290   struct stat64 pre_st;
291   struct timeval tv[2];
292  again:
293   if (preserve_dates)
294     {
295       if (stat64 (fname, &pre_st) != 0)
296         {
297           error (0, errno, gettext ("cannot stat input file '%s'"), fname);
298           return 1;
299         }
300
301       /* If we have to preserve the timestamp, we need it in the
302          format utimes() understands.  */
303       TIMESPEC_TO_TIMEVAL (&tv[0], &pre_st.st_atim);
304       TIMESPEC_TO_TIMEVAL (&tv[1], &pre_st.st_mtim);
305     }
306
307   /* Open the file.  */
308   int fd = open (fname, output_fname == NULL ? O_RDWR : O_RDONLY);
309   if (fd == -1)
310     {
311       error (0, errno, gettext ("while opening '%s'"), fname);
312       return 1;
313     }
314
315   /* We always use fstat() even if we called stat() before.  This is
316      done to make sure the information returned by stat() is for the
317      same file.  */
318   struct stat64 st;
319   if (fstat64 (fd, &st) != 0)
320     {
321       error (0, errno, gettext ("cannot stat input file '%s'"), fname);
322       return 1;
323     }
324   /* Paranoid mode on.  */
325   if (preserve_dates
326       && (st.st_ino != pre_st.st_ino || st.st_dev != pre_st.st_dev))
327     {
328       /* We detected a race.  Try again.  */
329       close (fd);
330       goto again;
331     }
332
333   /* Now get the ELF descriptor.  */
334   Elf *elf = elf_begin (fd, output_fname == NULL ? ELF_C_RDWR : ELF_C_READ,
335                         NULL);
336   int result;
337   switch (elf_kind (elf))
338     {
339     case ELF_K_ELF:
340       result = handle_elf (fd, elf, NULL, fname, st.st_mode & ACCESSPERMS,
341                            preserve_dates ? tv : NULL);
342       break;
343
344     case ELF_K_AR:
345       /* It is not possible to strip the content of an archive direct
346          the output to a specific file.  */
347       if (unlikely (output_fname != NULL || debug_fname != NULL))
348         {
349           error (0, 0, gettext ("%s: cannot use -o or -f when stripping archive"),
350                  fname);
351           result = 1;
352         }
353       else
354         result = handle_ar (fd, elf, NULL, fname, preserve_dates ? tv : NULL);
355       break;
356
357     default:
358       error (0, 0, gettext ("%s: File format not recognized"), fname);
359       result = 1;
360       break;
361     }
362
363   if (unlikely (elf_end (elf) != 0))
364     INTERNAL_ERROR (fname);
365
366   close (fd);
367
368   return result;
369 }
370
371
372 /* Maximum size of array allocated on stack.  */
373 #define MAX_STACK_ALLOC (400 * 1024)
374
375 static int
376 handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
377             mode_t mode, struct timeval tvp[2])
378 {
379   size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
380   size_t fname_len = strlen (fname) + 1;
381   char *fullname = alloca (prefix_len + 1 + fname_len);
382   char *cp = fullname;
383   Elf *debugelf = NULL;
384   char *tmp_debug_fname = NULL;
385   int result = 0;
386   size_t shdridx = 0;
387   size_t shstrndx;
388   struct shdr_info
389   {
390     Elf_Scn *scn;
391     GElf_Shdr shdr;
392     Elf_Data *data;
393     Elf_Data *debug_data;
394     const char *name;
395     Elf32_Word idx;             /* Index in new file.  */
396     Elf32_Word old_sh_link;     /* Original value of shdr.sh_link.  */
397     Elf32_Word symtab_idx;
398     Elf32_Word version_idx;
399     Elf32_Word group_idx;
400     Elf32_Word group_cnt;
401     Elf_Scn *newscn;
402     struct Ebl_Strent *se;
403     Elf32_Word *newsymidx;
404   } *shdr_info = NULL;
405   Elf_Scn *scn;
406   size_t cnt;
407   size_t idx;
408   bool changes;
409   GElf_Ehdr newehdr_mem;
410   GElf_Ehdr *newehdr;
411   GElf_Ehdr debugehdr_mem;
412   GElf_Ehdr *debugehdr;
413   struct Ebl_Strtab *shst = NULL;
414   Elf_Data debuglink_crc_data;
415   bool any_symtab_changes = false;
416   Elf_Data *shstrtab_data = NULL;
417
418   /* Create the full name of the file.  */
419   if (prefix != NULL)
420     {
421       cp = mempcpy (cp, prefix, prefix_len);
422       *cp++ = ':';
423     }
424   memcpy (cp, fname, fname_len);
425
426   /* If we are not replacing the input file open a new file here.  */
427   if (output_fname != NULL)
428     {
429       fd = open (output_fname, O_RDWR | O_CREAT, mode);
430       if (unlikely (fd == -1))
431         {
432           error (0, errno, gettext ("cannot open '%s'"), output_fname);
433           return 1;
434         }
435     }
436
437   int debug_fd = -1;
438
439   /* Get the EBL handling.  The -g option is currently the only reason
440      we need EBL so don't open the backend unless necessary.  */
441   Ebl *ebl = NULL;
442   if (remove_debug)
443     {
444       ebl = ebl_openbackend (elf);
445       if (ebl == NULL)
446         {
447           error (0, errno, gettext ("cannot open EBL backend"));
448           result = 1;
449           goto fail;
450         }
451     }
452
453   /* Open the additional file the debug information will be stored in.  */
454   if (debug_fname != NULL)
455     {
456       /* Create a temporary file name.  We do not want to overwrite
457          the debug file if the file would not contain any
458          information.  */
459       size_t debug_fname_len = strlen (debug_fname);
460       tmp_debug_fname = (char *) alloca (debug_fname_len + sizeof (".XXXXXX"));
461       strcpy (mempcpy (tmp_debug_fname, debug_fname, debug_fname_len),
462               ".XXXXXX");
463
464       debug_fd = mkstemp (tmp_debug_fname);
465       if (unlikely (debug_fd == -1))
466         {
467           error (0, errno, gettext ("cannot open '%s'"), debug_fname);
468           result = 1;
469           goto fail;
470         }
471     }
472
473   /* Get the information from the old file.  */
474   GElf_Ehdr ehdr_mem;
475   GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
476   if (ehdr == NULL)
477     INTERNAL_ERROR (fname);
478
479   /* Get the section header string table index.  */
480   if (unlikely (elf_getshstrndx (elf, &shstrndx) < 0))
481     error (EXIT_FAILURE, 0,
482            gettext ("cannot get section header string table index"));
483
484   /* We now create a new ELF descriptor for the same file.  We
485      construct it almost exactly in the same way with some information
486      dropped.  */
487   Elf *newelf;
488   if (output_fname != NULL)
489     newelf = elf_begin (fd, ELF_C_WRITE_MMAP, NULL);
490   else
491     newelf = elf_clone (elf, ELF_C_EMPTY);
492
493   if (unlikely (gelf_newehdr (newelf, gelf_getclass (elf)) == 0)
494       || (ehdr->e_type != ET_REL
495           && unlikely (gelf_newphdr (newelf, ehdr->e_phnum) == 0)))
496     {
497       error (0, 0, gettext ("cannot create new file '%s': %s"),
498              output_fname, elf_errmsg (-1));
499       goto fail;
500     }
501
502   /* Copy over the old program header if needed.  */
503   if (ehdr->e_type != ET_REL)
504     for (cnt = 0; cnt < ehdr->e_phnum; ++cnt)
505       {
506         GElf_Phdr phdr_mem;
507         GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
508         if (phdr == NULL
509             || unlikely (gelf_update_phdr (newelf, cnt, phdr) == 0))
510           INTERNAL_ERROR (fname);
511       }
512
513   if (debug_fname != NULL)
514     {
515       /* Also create an ELF descriptor for the debug file */
516       debugelf = elf_begin (debug_fd, ELF_C_WRITE_MMAP, NULL);
517       if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0)
518           || (ehdr->e_type != ET_REL
519               && unlikely (gelf_newphdr (debugelf, ehdr->e_phnum) == 0)))
520         {
521           error (0, 0, gettext ("cannot create new file '%s': %s"),
522                  debug_fname, elf_errmsg (-1));
523           goto fail_close;
524         }
525
526       /* Copy over the old program header if needed.  */
527       if (ehdr->e_type != ET_REL)
528         for (cnt = 0; cnt < ehdr->e_phnum; ++cnt)
529           {
530             GElf_Phdr phdr_mem;
531             GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
532             if (phdr == NULL
533                 || unlikely (gelf_update_phdr (debugelf, cnt, phdr) == 0))
534               INTERNAL_ERROR (fname);
535           }
536     }
537
538   /* Number of sections.  */
539   size_t shnum;
540   if (unlikely (elf_getshnum (elf, &shnum) < 0))
541     {
542       error (0, 0, gettext ("cannot determine number of sections: %s"),
543              elf_errmsg (-1));
544       goto fail_close;
545     }
546
547   /* Storage for section information.  We leave room for two more
548      entries since we unconditionally create a section header string
549      table.  Maybe some weird tool created an ELF file without one.
550      The other one is used for the debug link section.  */
551   if ((shnum + 2) * sizeof (struct shdr_info) > MAX_STACK_ALLOC)
552     shdr_info = (struct shdr_info *) xcalloc (shnum + 2,
553                                               sizeof (struct shdr_info));
554   else
555     {
556       shdr_info = (struct shdr_info *) alloca ((shnum + 2)
557                                                * sizeof (struct shdr_info));
558       memset (shdr_info, '\0', (shnum + 2) * sizeof (struct shdr_info));
559     }
560
561   /* Prepare section information data structure.  */
562   scn = NULL;
563   cnt = 1;
564   while ((scn = elf_nextscn (elf, scn)) != NULL)
565     {
566       /* This should always be true (i.e., there should not be any
567          holes in the numbering).  */
568       assert (elf_ndxscn (scn) == cnt);
569
570       shdr_info[cnt].scn = scn;
571
572       /* Get the header.  */
573       if (gelf_getshdr (scn, &shdr_info[cnt].shdr) == NULL)
574         INTERNAL_ERROR (fname);
575
576       /* Get the name of the section.  */
577       shdr_info[cnt].name = elf_strptr (elf, shstrndx,
578                                         shdr_info[cnt].shdr.sh_name);
579       if (shdr_info[cnt].name == NULL)
580         {
581           error (0, 0, gettext ("illformed file '%s'"), fname);
582           goto fail_close;
583         }
584
585       /* Mark them as present but not yet investigated.  */
586       shdr_info[cnt].idx = 1;
587
588       /* Remember the shdr.sh_link value.  */
589       shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
590
591       /* Sections in files other than relocatable object files which
592          are not loaded can be freely moved by us.  In relocatable
593          object files everything can be moved.  */
594       if (ehdr->e_type == ET_REL
595           || (shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) == 0)
596         shdr_info[cnt].shdr.sh_offset = 0;
597
598       /* If this is an extended section index table store an
599          appropriate reference.  */
600       if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
601         {
602           assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
603           shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
604         }
605       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
606         {
607           /* Cross-reference the sections contained in the section
608              group.  */
609           shdr_info[cnt].data = elf_getdata (shdr_info[cnt].scn, NULL);
610           if (shdr_info[cnt].data == NULL)
611             INTERNAL_ERROR (fname);
612
613           /* XXX Fix for unaligned access.  */
614           Elf32_Word *grpref = (Elf32_Word *) shdr_info[cnt].data->d_buf;
615           size_t inner;
616           for (inner = 1;
617                inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
618                ++inner)
619             shdr_info[grpref[inner]].group_idx = cnt;
620
621           if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
622             /* If the section group contains only one element and this
623                is n COMDAT section we can drop it right away.  */
624             shdr_info[cnt].idx = 0;
625           else
626             shdr_info[cnt].group_cnt = inner - 1;
627         }
628       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
629         {
630           assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
631           shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
632         }
633
634       /* If this section is part of a group make sure it is not
635          discarded right away.  */
636       if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
637         {
638           assert (shdr_info[cnt].group_idx != 0);
639
640           if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
641             {
642               /* The section group section will be removed.  */
643               shdr_info[cnt].group_idx = 0;
644               shdr_info[cnt].shdr.sh_flags &= ~SHF_GROUP;
645             }
646         }
647
648       /* Increment the counter.  */
649       ++cnt;
650     }
651
652   /* Now determine which sections can go away.  The general rule is that
653      all sections which are not used at runtime are stripped out.  But
654      there are a few exceptions:
655
656      - special sections named ".comment" and ".note" are kept
657      - OS or architecture specific sections are kept since we might not
658        know how to handle them
659      - if a section is referred to from a section which is not removed
660        in the sh_link or sh_info element it cannot be removed either
661   */
662   for (cnt = 1; cnt < shnum; ++cnt)
663     /* Check whether the section can be removed.  */
664     if (ebl_section_strip_p (ebl, ehdr, &shdr_info[cnt].shdr,
665                              shdr_info[cnt].name, remove_comment,
666                              remove_debug))
667       {
668         /* For now assume this section will be removed.  */
669         shdr_info[cnt].idx = 0;
670
671         idx = shdr_info[cnt].group_idx;
672         while (idx != 0)
673           {
674             /* The section group data is already loaded.  */
675             assert (shdr_info[idx].data != NULL);
676
677             /* If the references section group is a normal section
678                group and has one element remaining, or if it is an
679                empty COMDAT section group it is removed.  */
680             bool is_comdat = (((Elf32_Word *) shdr_info[idx].data->d_buf)[0]
681                               & GRP_COMDAT) != 0;
682
683             --shdr_info[idx].group_cnt;
684             if ((!is_comdat && shdr_info[idx].group_cnt == 1)
685                 || (is_comdat && shdr_info[idx].group_cnt == 0))
686               {
687                 shdr_info[idx].idx = 0;
688                 /* Continue recursively.  */
689                 idx = shdr_info[idx].group_idx;
690               }
691             else
692               break;
693           }
694       }
695
696   /* Mark the SHT_NULL section as handled.  */
697   shdr_info[0].idx = 2;
698
699
700   /* Handle exceptions: section groups and cross-references.  We might
701      have to repeat this a few times since the resetting of the flag
702      might propagate.  */
703   do
704     {
705       changes = false;
706
707       for (cnt = 1; cnt < shnum; ++cnt)
708         {
709           if (shdr_info[cnt].idx == 0)
710             {
711               /* If a relocation section is marked as being removed make
712                  sure the section it is relocating is removed, too.  */
713               if ((shdr_info[cnt].shdr.sh_type == SHT_REL
714                    || shdr_info[cnt].shdr.sh_type == SHT_RELA)
715                   && shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
716                 shdr_info[cnt].idx = 1;
717             }
718
719           if (shdr_info[cnt].idx == 1)
720             {
721               /* The content of symbol tables we don't remove must not
722                  reference any section which we do remove.  Otherwise
723                  we cannot remove the section.  */
724               if (debug_fname != NULL
725                   && shdr_info[cnt].debug_data == NULL
726                   && (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM
727                       || shdr_info[cnt].shdr.sh_type == SHT_SYMTAB))
728                 {
729                   /* Make sure the data is loaded.  */
730                   if (shdr_info[cnt].data == NULL)
731                     {
732                       shdr_info[cnt].data
733                         = elf_getdata (shdr_info[cnt].scn, NULL);
734                       if (shdr_info[cnt].data == NULL)
735                         INTERNAL_ERROR (fname);
736                     }
737                   Elf_Data *symdata = shdr_info[cnt].data;
738
739                   /* If there is an extended section index table load it
740                      as well.  */
741                   if (shdr_info[cnt].symtab_idx != 0
742                       && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
743                     {
744                       assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
745
746                       shdr_info[shdr_info[cnt].symtab_idx].data
747                         = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
748                                        NULL);
749                       if (shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
750                         INTERNAL_ERROR (fname);
751                     }
752                   Elf_Data *xndxdata
753                     = shdr_info[shdr_info[cnt].symtab_idx].data;
754
755                   /* Go through all symbols and make sure the section they
756                      reference is not removed.  */
757                   size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1,
758                                               ehdr->e_version);
759
760                   for (size_t inner = 0;
761                        inner < shdr_info[cnt].data->d_size / elsize;
762                        ++inner)
763                     {
764                       GElf_Sym sym_mem;
765                       Elf32_Word xndx;
766                       GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
767                                                         inner, &sym_mem,
768                                                         &xndx);
769                       if (sym == NULL)
770                         INTERNAL_ERROR (fname);
771
772                       size_t scnidx = sym->st_shndx;
773                       if (scnidx == SHN_UNDEF || scnidx >= shnum
774                           || (scnidx >= SHN_LORESERVE
775                               && scnidx <= SHN_HIRESERVE
776                               && scnidx != SHN_XINDEX)
777                           /* Don't count in the section symbols.  */
778                           || GELF_ST_TYPE (sym->st_info) == STT_SECTION)
779                         /* This is no section index, leave it alone.  */
780                         continue;
781                       else if (scnidx == SHN_XINDEX)
782                         scnidx = xndx;
783
784                       if (shdr_info[scnidx].idx == 0)
785                         /* This symbol table has a real symbol in
786                            a discarded section.  So preserve the
787                            original table in the debug file.  */
788                         shdr_info[cnt].debug_data = symdata;
789                     }
790                 }
791
792               /* Cross referencing happens:
793                  - for the cases the ELF specification says.  That are
794                    + SHT_DYNAMIC in sh_link to string table
795                    + SHT_HASH in sh_link to symbol table
796                    + SHT_REL and SHT_RELA in sh_link to symbol table
797                    + SHT_SYMTAB and SHT_DYNSYM in sh_link to string table
798                    + SHT_GROUP in sh_link to symbol table
799                    + SHT_SYMTAB_SHNDX in sh_link to symbol table
800                    Other (OS or architecture-specific) sections might as
801                    well use this field so we process it unconditionally.
802                  - references inside section groups
803                  - specially marked references in sh_info if the SHF_INFO_LINK
804                  flag is set
805               */
806
807               if (shdr_info[shdr_info[cnt].shdr.sh_link].idx == 0)
808                 {
809                   shdr_info[shdr_info[cnt].shdr.sh_link].idx = 1;
810                   changes |= shdr_info[cnt].shdr.sh_link < cnt;
811                 }
812
813               /* Handle references through sh_info.  */
814               if (SH_INFO_LINK_P (&shdr_info[cnt].shdr)
815                   && shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
816                 {
817                   shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
818                   changes |= shdr_info[cnt].shdr.sh_info < cnt;
819                 }
820
821               /* Mark the section as investigated.  */
822               shdr_info[cnt].idx = 2;
823             }
824
825           if (debug_fname != NULL
826               && (shdr_info[cnt].idx == 0 || shdr_info[cnt].debug_data != NULL))
827             {
828               /* This section is being preserved in the debug file.
829                  Sections it refers to must be preserved there too.
830
831                  In this pass we mark sections to be preserved in both
832                  files by setting the .debug_data pointer to the original
833                  file's .data pointer.  Below, we'll copy the section
834                  contents.  */
835
836               inline void check_preserved (size_t i)
837               {
838                 if (i != 0 && shdr_info[i].idx != 0
839                     && shdr_info[i].debug_data == NULL)
840                   {
841                     if (shdr_info[i].data == NULL)
842                       shdr_info[i].data = elf_getdata (shdr_info[i].scn, NULL);
843                     if (shdr_info[i].data == NULL)
844                       INTERNAL_ERROR (fname);
845
846                     shdr_info[i].debug_data = shdr_info[i].data;
847                     changes |= i < cnt;
848                   }
849               }
850
851               check_preserved (shdr_info[cnt].shdr.sh_link);
852               if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
853                 check_preserved (shdr_info[cnt].shdr.sh_info);
854             }
855         }
856     }
857   while (changes);
858
859   /* Copy the removed sections to the debug output file.
860      The ones that are not removed in the stripped file are SHT_NOBITS.  */
861   if (debug_fname != NULL)
862     {
863       for (cnt = 1; cnt < shnum; ++cnt)
864         {
865           scn = elf_newscn (debugelf);
866           if (scn == NULL)
867             error (EXIT_FAILURE, 0,
868                    gettext ("while generating output file: %s"),
869                    elf_errmsg (-1));
870
871           bool discard_section = (shdr_info[cnt].idx > 0
872                                   && shdr_info[cnt].debug_data == NULL
873                                   && shdr_info[cnt].shdr.sh_type != SHT_NOTE
874                                   && cnt != ehdr->e_shstrndx);
875
876           /* Set the section header in the new file.  */
877           GElf_Shdr debugshdr = shdr_info[cnt].shdr;
878           if (discard_section)
879             debugshdr.sh_type = SHT_NOBITS;
880
881           if (unlikely (gelf_update_shdr (scn, &debugshdr) == 0))
882             /* There cannot be any overflows.  */
883             INTERNAL_ERROR (fname);
884
885           /* Get the data from the old file if necessary. */
886           if (shdr_info[cnt].data == NULL)
887             {
888               shdr_info[cnt].data = elf_getdata (shdr_info[cnt].scn, NULL);
889               if (shdr_info[cnt].data == NULL)
890                 INTERNAL_ERROR (fname);
891             }
892
893           /* Set the data.  This is done by copying from the old file.  */
894           Elf_Data *debugdata = elf_newdata (scn);
895           if (debugdata == NULL)
896             INTERNAL_ERROR (fname);
897
898           /* Copy the structure.  This data may be modified in place
899              before we write out the file.  */
900           *debugdata = *shdr_info[cnt].data;
901           if (discard_section)
902             debugdata->d_buf = NULL;
903           else if (shdr_info[cnt].debug_data != NULL)
904             {
905               /* Copy the original data before it gets modified.  */
906               shdr_info[cnt].debug_data = debugdata;
907               debugdata->d_buf = memcpy (xmalloc (debugdata->d_size),
908                                          debugdata->d_buf, debugdata->d_size);
909             }
910         }
911
912       /* Finish the ELF header.  Fill in the fields not handled by
913          libelf from the old file.  */
914       debugehdr = gelf_getehdr (debugelf, &debugehdr_mem);
915       if (debugehdr == NULL)
916         INTERNAL_ERROR (fname);
917
918       memcpy (debugehdr->e_ident, ehdr->e_ident, EI_NIDENT);
919       debugehdr->e_type = ehdr->e_type;
920       debugehdr->e_machine = ehdr->e_machine;
921       debugehdr->e_version = ehdr->e_version;
922       debugehdr->e_entry = ehdr->e_entry;
923       debugehdr->e_flags = ehdr->e_flags;
924       debugehdr->e_shstrndx = ehdr->e_shstrndx;
925
926       if (unlikely (gelf_update_ehdr (debugelf, debugehdr) == 0))
927         {
928           error (0, 0, gettext ("%s: error while creating ELF header: %s"),
929                  debug_fname, elf_errmsg (-1));
930           result = 1;
931           goto fail_close;
932         }
933     }
934
935   /* Mark the section header string table as unused, we will create
936      a new one.  */
937   shdr_info[shstrndx].idx = 0;
938
939   /* We need a string table for the section headers.  */
940   shst = ebl_strtabinit (true);
941   if (shst == NULL)
942     error (EXIT_FAILURE, errno, gettext ("while preparing output for '%s'"),
943            output_fname ?: fname);
944
945   /* Assign new section numbers.  */
946   shdr_info[0].idx = 0;
947   for (cnt = idx = 1; cnt < shnum; ++cnt)
948     if (shdr_info[cnt].idx > 0)
949       {
950         shdr_info[cnt].idx = idx++;
951
952         /* Create a new section.  */
953         shdr_info[cnt].newscn = elf_newscn (newelf);
954         if (shdr_info[cnt].newscn == NULL)
955           error (EXIT_FAILURE, 0, gettext ("while generating output file: %s"),
956                  elf_errmsg (-1));
957
958         assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
959
960         /* Add this name to the section header string table.  */
961         shdr_info[cnt].se = ebl_strtabadd (shst, shdr_info[cnt].name, 0);
962       }
963
964   /* Test whether we are doing anything at all.  */
965   if (cnt == idx)
966     /* Nope, all removable sections are already gone.  */
967     goto fail_close;
968
969   /* Create the reference to the file with the debug info.  */
970   if (debug_fname != NULL)
971     {
972       /* Add the section header string table section name.  */
973       shdr_info[cnt].se = ebl_strtabadd (shst, ".gnu_debuglink", 15);
974       shdr_info[cnt].idx = idx++;
975
976       /* Create the section header.  */
977       shdr_info[cnt].shdr.sh_type = SHT_PROGBITS;
978       shdr_info[cnt].shdr.sh_flags = 0;
979       shdr_info[cnt].shdr.sh_addr = 0;
980       shdr_info[cnt].shdr.sh_link = SHN_UNDEF;
981       shdr_info[cnt].shdr.sh_info = SHN_UNDEF;
982       shdr_info[cnt].shdr.sh_entsize = 0;
983       shdr_info[cnt].shdr.sh_addralign = 4;
984       /* We set the offset to zero here.  Before we write the ELF file the
985          field must have the correct value.  This is done in the final
986          loop over all section.  Then we have all the information needed.  */
987       shdr_info[cnt].shdr.sh_offset = 0;
988
989       /* Create the section.  */
990       shdr_info[cnt].newscn = elf_newscn (newelf);
991       if (shdr_info[cnt].newscn == NULL)
992         error (EXIT_FAILURE, 0,
993                gettext ("while create section header section: %s"),
994                elf_errmsg (-1));
995       assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
996
997       shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
998       if (shdr_info[cnt].data == NULL)
999         error (EXIT_FAILURE, 0, gettext ("cannot allocate section data: %s"),
1000                elf_errmsg (-1));
1001
1002       char *debug_basename = basename (debug_fname_embed ?: debug_fname);
1003       off_t crc_offset = strlen (debug_basename) + 1;
1004       /* Align to 4 byte boundary */
1005       crc_offset = ((crc_offset - 1) & ~3) + 4;
1006
1007       shdr_info[cnt].data->d_align = 4;
1008       shdr_info[cnt].shdr.sh_size = shdr_info[cnt].data->d_size
1009         = crc_offset + 4;
1010       shdr_info[cnt].data->d_buf = xcalloc (1, shdr_info[cnt].data->d_size);
1011
1012       strcpy (shdr_info[cnt].data->d_buf, debug_basename);
1013
1014       /* Cache this Elf_Data describing the CRC32 word in the section.
1015          We'll fill this in when we have written the debug file.  */
1016       debuglink_crc_data = *shdr_info[cnt].data;
1017       debuglink_crc_data.d_buf = ((char *) debuglink_crc_data.d_buf
1018                                   + crc_offset);
1019       debuglink_crc_data.d_size = 4;
1020
1021       /* One more section done.  */
1022       ++cnt;
1023     }
1024
1025   /* Index of the section header table in the shdr_info array.  */
1026   shdridx = cnt;
1027
1028   /* Add the section header string table section name.  */
1029   shdr_info[cnt].se = ebl_strtabadd (shst, ".shstrtab", 10);
1030   shdr_info[cnt].idx = idx;
1031
1032   /* Create the section header.  */
1033   shdr_info[cnt].shdr.sh_type = SHT_STRTAB;
1034   shdr_info[cnt].shdr.sh_flags = 0;
1035   shdr_info[cnt].shdr.sh_addr = 0;
1036   shdr_info[cnt].shdr.sh_link = SHN_UNDEF;
1037   shdr_info[cnt].shdr.sh_info = SHN_UNDEF;
1038   shdr_info[cnt].shdr.sh_entsize = 0;
1039   /* We set the offset to zero here.  Before we write the ELF file the
1040      field must have the correct value.  This is done in the final
1041      loop over all section.  Then we have all the information needed.  */
1042   shdr_info[cnt].shdr.sh_offset = 0;
1043   shdr_info[cnt].shdr.sh_addralign = 1;
1044
1045   /* Create the section.  */
1046   shdr_info[cnt].newscn = elf_newscn (newelf);
1047   if (shdr_info[cnt].newscn == NULL)
1048     error (EXIT_FAILURE, 0,
1049            gettext ("while create section header section: %s"),
1050            elf_errmsg (-1));
1051   assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
1052
1053   /* Finalize the string table and fill in the correct indices in the
1054      section headers.  */
1055   shstrtab_data = elf_newdata (shdr_info[cnt].newscn);
1056   if (shstrtab_data == NULL)
1057     error (EXIT_FAILURE, 0,
1058            gettext ("while create section header string table: %s"),
1059            elf_errmsg (-1));
1060   ebl_strtabfinalize (shst, shstrtab_data);
1061
1062   /* We have to set the section size.  */
1063   shdr_info[cnt].shdr.sh_size = shstrtab_data->d_size;
1064
1065   /* Update the section information.  */
1066   GElf_Off lastoffset = 0;
1067   for (cnt = 1; cnt <= shdridx; ++cnt)
1068     if (shdr_info[cnt].idx > 0)
1069       {
1070         Elf_Data *newdata;
1071
1072         scn = elf_getscn (newelf, shdr_info[cnt].idx);
1073         assert (scn != NULL);
1074
1075         /* Update the name.  */
1076         shdr_info[cnt].shdr.sh_name = ebl_strtaboffset (shdr_info[cnt].se);
1077
1078         /* Update the section header from the input file.  Some fields
1079            might be section indeces which now have to be adjusted.  */
1080         if (shdr_info[cnt].shdr.sh_link != 0)
1081           shdr_info[cnt].shdr.sh_link =
1082             shdr_info[shdr_info[cnt].shdr.sh_link].idx;
1083
1084         if (shdr_info[cnt].shdr.sh_type == SHT_GROUP)
1085           {
1086             assert (shdr_info[cnt].data != NULL);
1087
1088             Elf32_Word *grpref = (Elf32_Word *) shdr_info[cnt].data->d_buf;
1089             for (size_t inner = 0;
1090                  inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
1091                  ++inner)
1092               grpref[inner] = shdr_info[grpref[inner]].idx;
1093           }
1094
1095         /* Handle the SHT_REL, SHT_RELA, and SHF_INFO_LINK flag.  */
1096         if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
1097           shdr_info[cnt].shdr.sh_info =
1098             shdr_info[shdr_info[cnt].shdr.sh_info].idx;
1099
1100         /* Get the data from the old file if necessary.  We already
1101            created the data for the section header string table.  */
1102         if (cnt < shnum)
1103           {
1104             if (shdr_info[cnt].data == NULL)
1105               {
1106                 shdr_info[cnt].data = elf_getdata (shdr_info[cnt].scn, NULL);
1107                 if (shdr_info[cnt].data == NULL)
1108                   INTERNAL_ERROR (fname);
1109               }
1110
1111             /* Set the data.  This is done by copying from the old file.  */
1112             newdata = elf_newdata (scn);
1113             if (newdata == NULL)
1114               INTERNAL_ERROR (fname);
1115
1116             /* Copy the structure.  */
1117             *newdata = *shdr_info[cnt].data;
1118
1119             /* We know the size.  */
1120             shdr_info[cnt].shdr.sh_size = shdr_info[cnt].data->d_size;
1121
1122             /* We have to adjust symbol tables.  The st_shndx member might
1123                have to be updated.  */
1124             if (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM
1125                 || shdr_info[cnt].shdr.sh_type == SHT_SYMTAB)
1126               {
1127                 Elf_Data *versiondata = NULL;
1128                 Elf_Data *shndxdata = NULL;
1129
1130                 size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1,
1131                                             ehdr->e_version);
1132
1133                 if (shdr_info[cnt].symtab_idx != 0)
1134                   {
1135                     assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX);
1136                     /* This section has extended section information.
1137                        We have to modify that information, too.  */
1138                     shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
1139                                              NULL);
1140
1141                     assert ((versiondata->d_size / sizeof (Elf32_Word))
1142                             >= shdr_info[cnt].data->d_size / elsize);
1143                   }
1144
1145                 if (shdr_info[cnt].version_idx != 0)
1146                   {
1147                     assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
1148                     /* This section has associated version
1149                        information.  We have to modify that
1150                        information, too.  */
1151                     versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
1152                                                NULL);
1153
1154                     assert ((versiondata->d_size / sizeof (GElf_Versym))
1155                             >= shdr_info[cnt].data->d_size / elsize);
1156                   }
1157
1158                 shdr_info[cnt].newsymidx
1159                   = (Elf32_Word *) xcalloc (shdr_info[cnt].data->d_size
1160                                             / elsize, sizeof (Elf32_Word));
1161
1162                 bool last_was_local = true;
1163                 size_t destidx;
1164                 size_t inner;
1165                 for (destidx = inner = 1;
1166                      inner < shdr_info[cnt].data->d_size / elsize;
1167                      ++inner)
1168                   {
1169                     Elf32_Word sec;
1170                     GElf_Sym sym_mem;
1171                     Elf32_Word xshndx;
1172                     GElf_Sym *sym = gelf_getsymshndx (shdr_info[cnt].data,
1173                                                       shndxdata, inner,
1174                                                       &sym_mem, &xshndx);
1175                     if (sym == NULL)
1176                       INTERNAL_ERROR (fname);
1177
1178                     if (sym->st_shndx == SHN_UNDEF
1179                         || (sym->st_shndx >= shnum
1180                             && sym->st_shndx != SHN_XINDEX))
1181                       {
1182                         /* This is no section index, leave it alone
1183                            unless it is moved.  */
1184                         if (destidx != inner
1185                             && gelf_update_symshndx (shdr_info[cnt].data,
1186                                                      shndxdata,
1187                                                      destidx, sym,
1188                                                      xshndx) == 0)
1189                           INTERNAL_ERROR (fname);
1190
1191                         shdr_info[cnt].newsymidx[inner] = destidx++;
1192
1193                         if (last_was_local
1194                             && GELF_ST_BIND (sym->st_info) != STB_LOCAL)
1195                           {
1196                             last_was_local = false;
1197                             shdr_info[cnt].shdr.sh_info = destidx - 1;
1198                           }
1199
1200                         continue;
1201                       }
1202
1203                     /* Get the full section index, if necessary from the
1204                        XINDEX table.  */
1205                     if (sym->st_shndx != SHN_XINDEX)
1206                       sec = shdr_info[sym->st_shndx].idx;
1207                     else
1208                       {
1209                         assert (shndxdata != NULL);
1210
1211                         sec = shdr_info[xshndx].idx;
1212                       }
1213
1214                     if (sec != 0)
1215                       {
1216                         GElf_Section nshndx;
1217                         Elf32_Word nxshndx;
1218
1219                         if (sec < SHN_LORESERVE)
1220                           {
1221                             nshndx = sec;
1222                             nxshndx = 0;
1223                           }
1224                         else
1225                           {
1226                             nshndx = SHN_XINDEX;
1227                             nxshndx = sec;
1228                           }
1229
1230                         assert (sec < SHN_LORESERVE || shndxdata != NULL);
1231
1232                         if ((inner != destidx || nshndx != sym->st_shndx
1233                              || (shndxdata != NULL && nxshndx != xshndx))
1234                             && (sym->st_shndx = nshndx,
1235                                 gelf_update_symshndx (shdr_info[cnt].data,
1236                                                       shndxdata,
1237                                                       destidx, sym,
1238                                                       nxshndx) == 0))
1239                           INTERNAL_ERROR (fname);
1240
1241                         shdr_info[cnt].newsymidx[inner] = destidx++;
1242
1243                         if (last_was_local
1244                             && GELF_ST_BIND (sym->st_info) != STB_LOCAL)
1245                           {
1246                             last_was_local = false;
1247                             shdr_info[cnt].shdr.sh_info = destidx - 1;
1248                           }
1249                       }
1250                     else if (debug_fname == NULL
1251                              || shdr_info[cnt].debug_data == NULL)
1252                       /* This is a section symbol for a section which has
1253                          been removed.  */
1254                       assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
1255                   }
1256
1257                 if (destidx != inner)
1258                   {
1259                     /* The size of the symbol table changed.  */
1260                     shdr_info[cnt].shdr.sh_size = newdata->d_size
1261                       = destidx * elsize;
1262                     any_symtab_changes = true;
1263                   }
1264                 else
1265                   {
1266                     /* The symbol table didn't really change.  */
1267                     free (shdr_info[cnt].newsymidx);
1268                     shdr_info[cnt].newsymidx = NULL;
1269                   }
1270               }
1271           }
1272
1273         /* If we have to, compute the offset of the section.  */
1274         if (shdr_info[cnt].shdr.sh_offset == 0)
1275           shdr_info[cnt].shdr.sh_offset
1276             = ((lastoffset + shdr_info[cnt].shdr.sh_addralign - 1)
1277                & ~((GElf_Off) (shdr_info[cnt].shdr.sh_addralign - 1)));
1278
1279         /* Set the section header in the new file.  */
1280         if (unlikely (gelf_update_shdr (scn, &shdr_info[cnt].shdr) == 0))
1281           /* There cannot be any overflows.  */
1282           INTERNAL_ERROR (fname);
1283
1284         /* Remember the last section written so far.  */
1285         GElf_Off filesz = (shdr_info[cnt].shdr.sh_type != SHT_NOBITS
1286                            ? shdr_info[cnt].shdr.sh_size : 0);
1287         if (lastoffset < shdr_info[cnt].shdr.sh_offset + filesz)
1288           lastoffset = shdr_info[cnt].shdr.sh_offset + filesz;
1289       }
1290
1291   /* Adjust symbol references if symbol tables changed.  */
1292   if (any_symtab_changes)
1293     /* Find all relocation sections which use this symbol table.  */
1294     for (cnt = 1; cnt <= shdridx; ++cnt)
1295       {
1296         /* Update section headers when the data size has changed.
1297            We also update the SHT_NOBITS section in the debug
1298            file so that the section headers match in sh_size.  */
1299         inline void update_section_size (const Elf_Data *newdata)
1300         {
1301           GElf_Shdr shdr_mem;
1302           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1303           shdr->sh_size = newdata->d_size;
1304           (void) gelf_update_shdr (scn, shdr);
1305           if (debugelf != NULL)
1306             {
1307               /* libelf will use d_size to set sh_size.  */
1308               Elf_Data *debugdata = elf_getdata (elf_getscn (debugelf,
1309                                                              cnt), NULL);
1310               debugdata->d_size = newdata->d_size;
1311             }
1312         }
1313
1314         if (shdr_info[cnt].idx == 0 && debug_fname == NULL)
1315           /* Ignore sections which are discarded.  When we are saving a
1316              relocation section in a separate debug file, we must fix up
1317              the symbol table references.  */
1318           continue;
1319
1320         const Elf32_Word symtabidx = shdr_info[cnt].old_sh_link;
1321         const Elf32_Word *const newsymidx = shdr_info[symtabidx].newsymidx;
1322         switch (shdr_info[cnt].shdr.sh_type)
1323           {
1324             inline bool no_symtab_updates (void)
1325             {
1326               /* If the symbol table hasn't changed, do not do anything.  */
1327               if (shdr_info[symtabidx].newsymidx == NULL)
1328                 return true;
1329
1330               /* If the symbol table is not discarded, but additionally
1331                  duplicated in the separate debug file and this section
1332                  is discarded, don't adjust anything.  */
1333               return (shdr_info[cnt].idx == 0
1334                       && shdr_info[symtabidx].debug_data != NULL);
1335             }
1336
1337           case SHT_REL:
1338           case SHT_RELA:
1339             if (no_symtab_updates ())
1340               break;
1341
1342             Elf_Data *d = elf_getdata (shdr_info[cnt].idx == 0
1343                                        ? elf_getscn (debugelf, cnt)
1344                                        : elf_getscn (newelf,
1345                                                      shdr_info[cnt].idx),
1346                                        NULL);
1347             assert (d != NULL);
1348             size_t nrels = (shdr_info[cnt].shdr.sh_size
1349                             / shdr_info[cnt].shdr.sh_entsize);
1350
1351             if (shdr_info[cnt].shdr.sh_type == SHT_REL)
1352               for (size_t relidx = 0; relidx < nrels; ++relidx)
1353                 {
1354                   GElf_Rel rel_mem;
1355                   if (gelf_getrel (d, relidx, &rel_mem) == NULL)
1356                     INTERNAL_ERROR (fname);
1357
1358                   size_t symidx = GELF_R_SYM (rel_mem.r_info);
1359                   if (newsymidx[symidx] != symidx)
1360                     {
1361                       rel_mem.r_info
1362                         = GELF_R_INFO (newsymidx[symidx],
1363                                        GELF_R_TYPE (rel_mem.r_info));
1364
1365                       if (gelf_update_rel (d, relidx, &rel_mem) == 0)
1366                         INTERNAL_ERROR (fname);
1367                     }
1368                 }
1369             else
1370               for (size_t relidx = 0; relidx < nrels; ++relidx)
1371                 {
1372                   GElf_Rela rel_mem;
1373                   if (gelf_getrela (d, relidx, &rel_mem) == NULL)
1374                     INTERNAL_ERROR (fname);
1375
1376                   size_t symidx = GELF_R_SYM (rel_mem.r_info);
1377                   if (newsymidx[symidx] != symidx)
1378                     {
1379                       rel_mem.r_info
1380                         = GELF_R_INFO (newsymidx[symidx],
1381                                        GELF_R_TYPE (rel_mem.r_info));
1382
1383                       if (gelf_update_rela (d, relidx, &rel_mem) == 0)
1384                         INTERNAL_ERROR (fname);
1385                     }
1386                 }
1387             break;
1388
1389           case SHT_HASH:
1390             if (no_symtab_updates ())
1391               break;
1392
1393             /* We have to recompute the hash table.  */
1394
1395             assert (shdr_info[cnt].idx > 0);
1396
1397             /* The hash section in the new file.  */
1398             scn = elf_getscn (newelf, shdr_info[cnt].idx);
1399
1400             /* The symbol table data.  */
1401             Elf_Data *symd = elf_getdata (elf_getscn (newelf,
1402                                                       shdr_info[symtabidx].idx),
1403                                           NULL);
1404             assert (symd != NULL);
1405
1406             /* The hash table data.  */
1407             Elf_Data *hashd = elf_getdata (scn, NULL);
1408             assert (hashd != NULL);
1409
1410             if (shdr_info[cnt].shdr.sh_entsize == sizeof (Elf32_Word))
1411               {
1412                 /* Sane arches first.  */
1413                 Elf32_Word *bucket = (Elf32_Word *) hashd->d_buf;
1414
1415                 size_t strshndx = shdr_info[symtabidx].old_sh_link;
1416                 size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1,
1417                                             ehdr->e_version);
1418
1419                 /* Adjust the nchain value.  The symbol table size
1420                    changed.  We keep the same size for the bucket array.  */
1421                 bucket[1] = symd->d_size / elsize;
1422                 Elf32_Word nbucket = bucket[0];
1423                 bucket += 2;
1424                 Elf32_Word *chain = bucket + nbucket;
1425
1426                 /* New size of the section.  */
1427                 hashd->d_size = ((2 + symd->d_size / elsize + nbucket)
1428                                  * sizeof (Elf32_Word));
1429                 update_section_size (hashd);
1430
1431                 /* Clear the arrays.  */
1432                 memset (bucket, '\0',
1433                         (symd->d_size / elsize + nbucket)
1434                         * sizeof (Elf32_Word));
1435
1436                 for (size_t inner = shdr_info[symtabidx].shdr.sh_info;
1437                      inner < symd->d_size / elsize; ++inner)
1438                   {
1439                     GElf_Sym sym_mem;
1440                     GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
1441                     assert (sym != NULL);
1442
1443                     const char *name = elf_strptr (elf, strshndx,
1444                                                    sym->st_name);
1445                     assert (name != NULL);
1446                     size_t hidx = elf_hash (name) % nbucket;
1447
1448                     if (bucket[hidx] == 0)
1449                       bucket[hidx] = inner;
1450                     else
1451                       {
1452                         hidx = bucket[hidx];
1453
1454                         while (chain[hidx] != 0)
1455                           hidx = chain[hidx];
1456
1457                         chain[hidx] = inner;
1458                       }
1459                   }
1460               }
1461             else
1462               {
1463                 /* Alpha and S390 64-bit use 64-bit SHT_HASH entries.  */
1464                 assert (shdr_info[cnt].shdr.sh_entsize
1465                         == sizeof (Elf64_Xword));
1466
1467                 Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
1468
1469                 size_t strshndx = shdr_info[symtabidx].old_sh_link;
1470                 size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1,
1471                                             ehdr->e_version);
1472
1473                 /* Adjust the nchain value.  The symbol table size
1474                    changed.  We keep the same size for the bucket array.  */
1475                 bucket[1] = symd->d_size / elsize;
1476                 Elf64_Xword nbucket = bucket[0];
1477                 bucket += 2;
1478                 Elf64_Xword *chain = bucket + nbucket;
1479
1480                 /* New size of the section.  */
1481                 hashd->d_size = ((2 + symd->d_size / elsize + nbucket)
1482                                  * sizeof (Elf64_Xword));
1483                 update_section_size (hashd);
1484
1485                 /* Clear the arrays.  */
1486                 memset (bucket, '\0',
1487                         (symd->d_size / elsize + nbucket)
1488                         * sizeof (Elf64_Xword));
1489
1490                 for (size_t inner = shdr_info[symtabidx].shdr.sh_info;
1491                      inner < symd->d_size / elsize; ++inner)
1492                   {
1493                     GElf_Sym sym_mem;
1494                     GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
1495                     assert (sym != NULL);
1496
1497                     const char *name = elf_strptr (elf, strshndx,
1498                                                    sym->st_name);
1499                     assert (name != NULL);
1500                     size_t hidx = elf_hash (name) % nbucket;
1501
1502                     if (bucket[hidx] == 0)
1503                       bucket[hidx] = inner;
1504                     else
1505                       {
1506                         hidx = bucket[hidx];
1507
1508                         while (chain[hidx] != 0)
1509                           hidx = chain[hidx];
1510
1511                         chain[hidx] = inner;
1512                       }
1513                   }
1514               }
1515             break;
1516
1517           case SHT_GNU_versym:
1518             /* If the symbol table changed we have to adjust the entries.  */
1519             if (no_symtab_updates ())
1520               break;
1521
1522             assert (shdr_info[cnt].idx > 0);
1523
1524             /* The symbol version section in the new file.  */
1525             scn = elf_getscn (newelf, shdr_info[cnt].idx);
1526
1527             /* The symbol table data.  */
1528             symd = elf_getdata (elf_getscn (newelf, shdr_info[symtabidx].idx),
1529                                 NULL);
1530             assert (symd != NULL);
1531
1532             /* The version symbol data.  */
1533             Elf_Data *verd = elf_getdata (scn, NULL);
1534             assert (verd != NULL);
1535
1536             /* The symbol version array.  */
1537             GElf_Half *verstab = (GElf_Half *) verd->d_buf;
1538
1539             /* Walk through the list and */
1540             size_t elsize = gelf_fsize (elf, verd->d_type, 1,
1541                                         ehdr->e_version);
1542             for (size_t inner = 1; inner < verd->d_size / elsize; ++inner)
1543               if (newsymidx[inner] != 0)
1544                 /* Overwriting the same array works since the
1545                    reordering can only move entries to lower indices
1546                    in the array.  */
1547                 verstab[newsymidx[inner]] = verstab[inner];
1548
1549             /* New size of the section.  */
1550             verd->d_size = gelf_fsize (newelf, verd->d_type,
1551                                        symd->d_size
1552                                        / gelf_fsize (elf, symd->d_type, 1,
1553                                                      ehdr->e_version),
1554                                        ehdr->e_version);
1555             update_section_size (verd);
1556             break;
1557
1558           case SHT_GROUP:
1559             if (no_symtab_updates ())
1560               break;
1561
1562             /* Yes, the symbol table changed.
1563                Update the section header of the section group.  */
1564             scn = elf_getscn (newelf, shdr_info[cnt].idx);
1565             GElf_Shdr shdr_mem;
1566             GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1567             assert (shdr != NULL);
1568
1569             shdr->sh_info = newsymidx[shdr->sh_info];
1570
1571             (void) gelf_update_shdr (scn, shdr);
1572             break;
1573           }
1574       }
1575
1576   /* Now that we have done all adjustments to the data,
1577      we can actually write out the debug file.  */
1578   if (debug_fname != NULL)
1579     {
1580       uint32_t debug_crc;
1581       Elf_Data debug_crc_data =
1582         {
1583           .d_type = ELF_T_WORD,
1584           .d_buf = &debug_crc,
1585           .d_size = sizeof (debug_crc),
1586           .d_version = EV_CURRENT
1587         };
1588
1589       /* Finally write the file.  */
1590       if (unlikely (elf_update (debugelf, ELF_C_WRITE) == -1))
1591         {
1592           error (0, 0, gettext ("while writing '%s': %s"),
1593                  debug_fname, elf_errmsg (-1));
1594           result = 1;
1595           goto fail_close;
1596         }
1597
1598       /* Create the real output file.  First rename, then change the
1599          mode.  */
1600       if (rename (tmp_debug_fname, debug_fname) != 0
1601           || fchmod (debug_fd, mode) != 0)
1602         {
1603           error (0, errno, gettext ("while creating '%s'"), debug_fname);
1604           result = 1;
1605           goto fail_close;
1606         }
1607
1608       /* The temporary file does not exist anymore.  */
1609       tmp_debug_fname = NULL;
1610
1611       /* Compute the checksum which we will add to the executable.  */
1612       if (crc32_file (debug_fd, &debug_crc) != 0)
1613         {
1614           error (0, errno,
1615                  gettext ("while computing checksum for debug information"));
1616           unlink (debug_fname);
1617           result = 1;
1618           goto fail_close;
1619         }
1620
1621       /* Store it in the debuglink section data.  */
1622       if (unlikely (gelf_xlatetof (newelf, &debuglink_crc_data,
1623                                    &debug_crc_data, ehdr->e_ident[EI_DATA])
1624                     != &debuglink_crc_data))
1625         INTERNAL_ERROR (fname);
1626     }
1627
1628   /* Finally finish the ELF header.  Fill in the fields not handled by
1629      libelf from the old file.  */
1630   newehdr = gelf_getehdr (newelf, &newehdr_mem);
1631   if (newehdr == NULL)
1632     INTERNAL_ERROR (fname);
1633
1634   memcpy (newehdr->e_ident, ehdr->e_ident, EI_NIDENT);
1635   newehdr->e_type = ehdr->e_type;
1636   newehdr->e_machine = ehdr->e_machine;
1637   newehdr->e_version = ehdr->e_version;
1638   newehdr->e_entry = ehdr->e_entry;
1639   newehdr->e_flags = ehdr->e_flags;
1640   newehdr->e_phoff = ehdr->e_phoff;
1641   /* We need to position the section header table.  */
1642   const size_t offsize = gelf_fsize (elf, ELF_T_OFF, 1, EV_CURRENT);
1643   newehdr->e_shoff = ((shdr_info[shdridx].shdr.sh_offset
1644                        + shdr_info[shdridx].shdr.sh_size + offsize - 1)
1645                       & ~((GElf_Off) (offsize - 1)));
1646   newehdr->e_shentsize = gelf_fsize (elf, ELF_T_SHDR, 1, EV_CURRENT);
1647
1648   /* The new section header string table index.  */
1649   if (likely (idx < SHN_HIRESERVE) && likely (idx != SHN_XINDEX))
1650     newehdr->e_shstrndx = idx;
1651   else
1652     {
1653       /* The index does not fit in the ELF header field.  */
1654       shdr_info[0].scn = elf_getscn (elf, 0);
1655
1656       if (gelf_getshdr (shdr_info[0].scn, &shdr_info[0].shdr) == NULL)
1657         INTERNAL_ERROR (fname);
1658
1659       shdr_info[0].shdr.sh_link = idx;
1660       (void) gelf_update_shdr (shdr_info[0].scn, &shdr_info[0].shdr);
1661
1662       newehdr->e_shstrndx = SHN_XINDEX;
1663     }
1664
1665   if (gelf_update_ehdr (newelf, newehdr) == 0)
1666     {
1667       error (0, 0, gettext ("%s: error while creating ELF header: %s"),
1668              fname, elf_errmsg (-1));
1669       return 1;
1670     }
1671
1672   /* We have everything from the old file.  */
1673   if (elf_cntl (elf, ELF_C_FDDONE) != 0)
1674     {
1675       error (0, 0, gettext ("%s: error while reading the file: %s"),
1676              fname, elf_errmsg (-1));
1677       return 1;
1678     }
1679
1680   /* The ELF library better follows our layout when this is not a
1681      relocatable object file.  */
1682   elf_flagelf (newelf, ELF_C_SET,
1683                (ehdr->e_type != ET_REL ? ELF_F_LAYOUT : 0)
1684                | (permissive ? ELF_F_PERMISSIVE : 0));
1685
1686   /* Finally write the file.  */
1687   if (elf_update (newelf, ELF_C_WRITE) == -1)
1688     {
1689       error (0, 0, gettext ("while writing '%s': %s"),
1690              fname, elf_errmsg (-1));
1691       result = 1;
1692     }
1693
1694  fail_close:
1695   if (shdr_info != NULL)
1696     {
1697       /* For some sections we might have created an table to map symbol
1698          table indices.  */
1699       if (any_symtab_changes)
1700         for (cnt = 1; cnt <= shdridx; ++cnt)
1701           {
1702             free (shdr_info[cnt].newsymidx);
1703             if (shdr_info[cnt].debug_data != NULL)
1704               free (shdr_info[cnt].debug_data->d_buf);
1705           }
1706
1707       /* Free the memory.  */
1708       if ((shnum + 2) * sizeof (struct shdr_info) > MAX_STACK_ALLOC)
1709         free (shdr_info);
1710     }
1711
1712   /* Free other resources.  */
1713   if (shstrtab_data != NULL)
1714     free (shstrtab_data->d_buf);
1715   if (shst != NULL)
1716     ebl_strtabfree (shst);
1717
1718   /* That was it.  Close the descriptors.  */
1719   if (elf_end (newelf) != 0)
1720     {
1721       error (0, 0, gettext ("error while finishing '%s': %s"), fname,
1722              elf_errmsg (-1));
1723       result = 1;
1724     }
1725
1726   if (debugelf != NULL && elf_end (debugelf) != 0)
1727     {
1728       error (0, 0, gettext ("error while finishing '%s': %s"), debug_fname,
1729              elf_errmsg (-1));
1730       result = 1;
1731     }
1732
1733  fail:
1734   /* Close the EBL backend.  */
1735   if (ebl != NULL)
1736     ebl_closebackend (ebl);
1737
1738   /* Close debug file descriptor, if opened */
1739   if (debug_fd >= 0)
1740     {
1741       if (tmp_debug_fname != NULL)
1742         unlink (tmp_debug_fname);
1743       close (debug_fd);
1744     }
1745
1746   /* If requested, preserve the timestamp.  */
1747   if (tvp != NULL)
1748     {
1749       if (futimes (fd, tvp) != 0)
1750         {
1751           error (0, errno, gettext ("\
1752 cannot set access and modification date of '%s'"),
1753                  output_fname ?: fname);
1754           result = 1;
1755         }
1756     }
1757
1758   /* Close the file descriptor if we created a new file.  */
1759   if (output_fname != NULL)
1760     close (fd);
1761
1762   return result;
1763 }
1764
1765
1766 static int
1767 handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
1768            struct timeval tvp[2])
1769 {
1770   size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
1771   size_t fname_len = strlen (fname) + 1;
1772   char new_prefix[prefix_len + 1 + fname_len];
1773   char *cp = new_prefix;
1774
1775   /* Create the full name of the file.  */
1776   if (prefix != NULL)
1777     {
1778       cp = mempcpy (cp, prefix, prefix_len);
1779       *cp++ = ':';
1780     }
1781   memcpy (cp, fname, fname_len);
1782
1783
1784   /* Process all the files contained in the archive.  */
1785   Elf *subelf;
1786   Elf_Cmd cmd = ELF_C_RDWR;
1787   int result = 0;
1788   while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
1789     {
1790       /* The the header for this element.  */
1791       Elf_Arhdr *arhdr = elf_getarhdr (subelf);
1792
1793       if (elf_kind (subelf) == ELF_K_ELF)
1794         result |= handle_elf (fd, subelf, new_prefix, arhdr->ar_name, 0, NULL);
1795       else if (elf_kind (subelf) == ELF_K_AR)
1796         result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name, NULL);
1797
1798       /* Get next archive element.  */
1799       cmd = elf_next (subelf);
1800       if (unlikely (elf_end (subelf) != 0))
1801         INTERNAL_ERROR (fname);
1802     }
1803
1804   if (tvp != NULL)
1805     {
1806       if (unlikely (futimes (fd, tvp) != 0))
1807         {
1808           error (0, errno, gettext ("\
1809 cannot set access and modification date of '%s'"), fname);
1810           result = 1;
1811         }
1812     }
1813
1814   if (unlikely (close (fd) != 0))
1815     error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
1816
1817   return result;
1818 }
1819
1820
1821 #include "debugpred.h"