Line data Source code
1 : /* Discard section not used at runtime from object files.
2 : Copyright (C) 2000-2012, 2014, 2015, 2016, 2017, 2018 Red Hat, Inc.
3 : This file is part of elfutils.
4 : Written by Ulrich Drepper <drepper@redhat.com>, 2000.
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 : #ifdef HAVE_CONFIG_H
20 : # include <config.h>
21 : #endif
22 :
23 : #include <argp.h>
24 : #include <assert.h>
25 : #include <byteswap.h>
26 : #include <endian.h>
27 : #include <fcntl.h>
28 : #include <fnmatch.h>
29 : #include <gelf.h>
30 : #include <libelf.h>
31 : #include <libintl.h>
32 : #include <locale.h>
33 : #include <stdbool.h>
34 : #include <stdio.h>
35 : #include <stdio_ext.h>
36 : #include <stdlib.h>
37 : #include <string.h>
38 : #include <unistd.h>
39 : #include <sys/stat.h>
40 : #include <sys/time.h>
41 :
42 : #include <elf-knowledge.h>
43 : #include <libebl.h>
44 : #include "libdwelf.h"
45 : #include <libeu.h>
46 : #include <system.h>
47 : #include <printversion.h>
48 :
49 : typedef uint8_t GElf_Byte;
50 :
51 : /* Name and version of program. */
52 : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
53 :
54 : /* Bug report address. */
55 : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
56 :
57 :
58 : /* Values for the parameters which have no short form. */
59 : #define OPT_REMOVE_COMMENT 0x100
60 : #define OPT_PERMISSIVE 0x101
61 : #define OPT_STRIP_SECTIONS 0x102
62 : #define OPT_RELOC_DEBUG 0x103
63 : #define OPT_KEEP_SECTION 0x104
64 : #define OPT_RELOC_DEBUG_ONLY 0x105
65 :
66 :
67 : /* Definitions of arguments for argp functions. */
68 : static const struct argp_option options[] =
69 : {
70 : { NULL, 0, NULL, 0, N_("Output selection:"), 0 },
71 : { "output", 'o', "FILE", 0, N_("Place stripped output into FILE"), 0 },
72 : { NULL, 'f', "FILE", 0, N_("Extract the removed sections into FILE"), 0 },
73 : { NULL, 'F', "FILE", 0, N_("Embed name FILE instead of -f argument"), 0 },
74 :
75 : { NULL, 0, NULL, 0, N_("Output options:"), 0 },
76 : { "strip-all", 's', NULL, OPTION_HIDDEN, NULL, 0 },
77 : { "strip-debug", 'g', NULL, 0, N_("Remove all debugging symbols"), 0 },
78 : { NULL, 'd', NULL, OPTION_ALIAS, NULL, 0 },
79 : { NULL, 'S', NULL, OPTION_ALIAS, NULL, 0 },
80 : { "strip-sections", OPT_STRIP_SECTIONS, NULL, 0,
81 : N_("Remove section headers (not recommended)"), 0 },
82 : { "preserve-dates", 'p', NULL, 0,
83 : N_("Copy modified/access timestamps to the output"), 0 },
84 : { "reloc-debug-sections", OPT_RELOC_DEBUG, NULL, 0,
85 : N_("Resolve all trivial relocations between debug sections if the removed sections are placed in a debug file (only relevant for ET_REL files, operation is not reversable, needs -f)"), 0 },
86 : { "reloc-debug-sections-only", OPT_RELOC_DEBUG_ONLY, NULL, 0,
87 : N_("Similar to --reloc-debug-sections, but resolve all trivial relocations between debug sections in place. No other stripping is performed (operation is not reversable, incompatible with -f, -g, --remove-comment and --remove-section)"), 0 },
88 : { "remove-comment", OPT_REMOVE_COMMENT, NULL, 0,
89 : N_("Remove .comment section"), 0 },
90 : { "remove-section", 'R', "SECTION", 0, N_("Remove the named section. SECTION is an extended wildcard pattern. May be given more than once. Only non-allocated sections can be removed."), 0 },
91 : { "keep-section", OPT_KEEP_SECTION, "SECTION", 0, N_("Keep the named section. SECTION is an extended wildcard pattern. May be given more than once."), 0 },
92 : { "permissive", OPT_PERMISSIVE, NULL, 0,
93 : N_("Relax a few rules to handle slightly broken ELF files"), 0 },
94 : { NULL, 0, NULL, 0, NULL, 0 }
95 : };
96 :
97 : /* Short description of program. */
98 : static const char doc[] = N_("Discard symbols from object files.");
99 :
100 : /* Strings for arguments in help texts. */
101 : static const char args_doc[] = N_("[FILE...]");
102 :
103 : /* Prototype for option handler. */
104 : static error_t parse_opt (int key, char *arg, struct argp_state *state);
105 :
106 : /* Data structure to communicate with argp functions. */
107 : static struct argp argp =
108 : {
109 : options, parse_opt, args_doc, doc, NULL, NULL, NULL
110 : };
111 :
112 :
113 : /* Print symbols in file named FNAME. */
114 : static int process_file (const char *fname);
115 :
116 : /* Handle one ELF file. */
117 : static int handle_elf (int fd, Elf *elf, const char *prefix,
118 : const char *fname, mode_t mode, struct timespec tvp[2]);
119 :
120 : /* Handle all files contained in the archive. */
121 : static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
122 : struct timespec tvp[2]) __attribute__ ((unused));
123 :
124 : static int debug_fd = -1;
125 : static char *tmp_debug_fname = NULL;
126 :
127 : /* Close debug file descriptor, if opened. And remove temporary debug file. */
128 : static void cleanup_debug (void);
129 :
130 : #define INTERNAL_ERROR(fname) \
131 : do { \
132 : cleanup_debug (); \
133 : error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s): %s"), \
134 : fname, __LINE__, PACKAGE_VERSION, elf_errmsg (-1)); \
135 : } while (0)
136 :
137 :
138 : /* Name of the output file. */
139 : static const char *output_fname;
140 :
141 : /* Name of the debug output file. */
142 : static const char *debug_fname;
143 :
144 : /* Name to pretend the debug output file has. */
145 : static const char *debug_fname_embed;
146 :
147 : /* If true output files shall have same date as the input file. */
148 : static bool preserve_dates;
149 :
150 : /* If true .comment sections will be removed. */
151 : static bool remove_comment;
152 :
153 : /* If true remove all debug sections. */
154 : static bool remove_debug;
155 :
156 : /* If true remove all section headers. */
157 : static bool remove_shdrs;
158 :
159 : /* If true relax some ELF rules for input files. */
160 : static bool permissive;
161 :
162 : /* If true perform relocations between debug sections. */
163 : static bool reloc_debug;
164 :
165 : /* If true perform relocations between debug sections only. */
166 : static bool reloc_debug_only;
167 :
168 : /* Sections the user explicitly wants to keep or remove. */
169 : struct section_pattern
170 : {
171 : char *pattern;
172 : struct section_pattern *next;
173 : };
174 :
175 : static struct section_pattern *keep_secs = NULL;
176 : static struct section_pattern *remove_secs = NULL;
177 :
178 : static void
179 10 : add_pattern (struct section_pattern **patterns, const char *pattern)
180 : {
181 10 : struct section_pattern *p = xmalloc (sizeof *p);
182 10 : p->pattern = xstrdup (pattern);
183 10 : p->next = *patterns;
184 10 : *patterns = p;
185 10 : }
186 :
187 : static void
188 212 : free_sec_patterns (struct section_pattern *patterns)
189 : {
190 212 : struct section_pattern *pattern = patterns;
191 434 : while (pattern != NULL)
192 : {
193 10 : struct section_pattern *p = pattern;
194 10 : pattern = p->next;
195 10 : free (p->pattern);
196 10 : free (p);
197 : }
198 212 : }
199 :
200 : static void
201 106 : free_patterns (void)
202 : {
203 106 : free_sec_patterns (keep_secs);
204 106 : free_sec_patterns (remove_secs);
205 106 : }
206 :
207 : static bool
208 : section_name_matches (struct section_pattern *patterns, const char *name)
209 : {
210 399051 : struct section_pattern *pattern = patterns;
211 399302 : while (pattern != NULL)
212 : {
213 269 : if (fnmatch (pattern->pattern, name, FNM_EXTMATCH) == 0)
214 : return true;
215 251 : pattern = pattern->next;
216 : }
217 : return false;
218 : }
219 :
220 :
221 : int
222 106 : main (int argc, char *argv[])
223 : {
224 : int remaining;
225 106 : int result = 0;
226 :
227 : /* We use no threads here which can interfere with handling a stream. */
228 106 : __fsetlocking (stdin, FSETLOCKING_BYCALLER);
229 106 : __fsetlocking (stdout, FSETLOCKING_BYCALLER);
230 106 : __fsetlocking (stderr, FSETLOCKING_BYCALLER);
231 :
232 : /* Set locale. */
233 106 : setlocale (LC_ALL, "");
234 :
235 : /* Make sure the message catalog can be found. */
236 106 : bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
237 :
238 : /* Initialize the message catalog. */
239 106 : textdomain (PACKAGE_TARNAME);
240 :
241 : /* Parse and process arguments. */
242 106 : if (argp_parse (&argp, argc, argv, 0, &remaining, NULL) != 0)
243 : return EXIT_FAILURE;
244 :
245 106 : if (reloc_debug && debug_fname == NULL)
246 : error (EXIT_FAILURE, 0,
247 0 : gettext ("--reloc-debug-sections used without -f"));
248 :
249 120 : if (reloc_debug_only &&
250 28 : (debug_fname != NULL || remove_secs != NULL
251 14 : || remove_comment == true || remove_debug == true))
252 : error (EXIT_FAILURE, 0,
253 0 : gettext ("--reloc-debug-sections-only incompatible with -f, -g, --remove-comment and --remove-section"));
254 :
255 : /* Tell the library which version we are expecting. */
256 106 : elf_version (EV_CURRENT);
257 :
258 106 : if (remaining == argc)
259 : /* The user didn't specify a name so we use a.out. */
260 5 : result = process_file ("a.out");
261 : else
262 : {
263 : /* If we have seen the '-o' or '-f' option there must be exactly one
264 : input file. */
265 101 : if ((output_fname != NULL || debug_fname != NULL)
266 73 : && remaining + 1 < argc)
267 0 : error (EXIT_FAILURE, 0, gettext ("\
268 : Only one input file allowed together with '-o' and '-f'"));
269 :
270 : /* Process all the remaining files. */
271 : do
272 101 : result |= process_file (argv[remaining]);
273 101 : while (++remaining < argc);
274 : }
275 :
276 106 : free_patterns ();
277 106 : return result;
278 : }
279 :
280 :
281 : /* Handle program arguments. */
282 : static error_t
283 732 : parse_opt (int key, char *arg, struct argp_state *state)
284 : {
285 732 : switch (key)
286 : {
287 56 : case 'f':
288 56 : if (debug_fname != NULL)
289 : {
290 0 : error (0, 0, gettext ("-f option specified twice"));
291 0 : return EINVAL;
292 : }
293 56 : debug_fname = arg;
294 56 : break;
295 :
296 6 : case 'F':
297 6 : if (debug_fname_embed != NULL)
298 : {
299 0 : error (0, 0, gettext ("-F option specified twice"));
300 0 : return EINVAL;
301 : }
302 6 : debug_fname_embed = arg;
303 6 : break;
304 :
305 76 : case 'o':
306 76 : if (output_fname != NULL)
307 : {
308 0 : error (0, 0, gettext ("-o option specified twice"));
309 0 : return EINVAL;
310 : }
311 76 : output_fname = arg;
312 76 : break;
313 :
314 0 : case 'p':
315 0 : preserve_dates = true;
316 0 : break;
317 :
318 14 : case OPT_RELOC_DEBUG:
319 14 : reloc_debug = true;
320 14 : break;
321 :
322 14 : case OPT_RELOC_DEBUG_ONLY:
323 14 : reloc_debug_only = true;
324 14 : break;
325 :
326 1 : case OPT_REMOVE_COMMENT:
327 1 : remove_comment = true;
328 1 : break;
329 :
330 4 : case 'R':
331 4 : if (fnmatch (arg, ".comment", FNM_EXTMATCH) == 0)
332 0 : remove_comment = true;
333 4 : add_pattern (&remove_secs, arg);
334 4 : break;
335 :
336 6 : case OPT_KEEP_SECTION:
337 6 : add_pattern (&keep_secs, arg);
338 6 : break;
339 :
340 25 : case 'g':
341 : case 'd':
342 : case 'S':
343 25 : remove_debug = true;
344 25 : break;
345 :
346 0 : case OPT_STRIP_SECTIONS:
347 0 : remove_shdrs = true;
348 0 : break;
349 :
350 0 : case OPT_PERMISSIVE:
351 0 : permissive = true;
352 0 : break;
353 :
354 : case 's': /* Ignored for compatibility. */
355 : break;
356 :
357 106 : case ARGP_KEY_SUCCESS:
358 106 : if (remove_comment == true
359 2 : && section_name_matches (keep_secs, ".comment"))
360 : {
361 0 : argp_error (state,
362 0 : gettext ("cannot both keep and remove .comment section"));
363 0 : return EINVAL;
364 : }
365 : break;
366 :
367 : default:
368 : return ARGP_ERR_UNKNOWN;
369 : }
370 : return 0;
371 : }
372 :
373 : static const char *
374 78534 : secndx_name (Elf *elf, size_t ndx)
375 : {
376 : size_t shstrndx;
377 : GElf_Shdr mem;
378 78534 : Elf_Scn *sec = elf_getscn (elf, ndx);
379 78534 : GElf_Shdr *shdr = gelf_getshdr (sec, &mem);
380 78534 : if (shdr == NULL || elf_getshdrstrndx (elf, &shstrndx) < 0)
381 : return "???";
382 78534 : return elf_strptr (elf, shstrndx, shdr->sh_name) ?: "???";
383 : }
384 :
385 : /* Get the extended section index table data for a symbol table section. */
386 : static Elf_Data *
387 122 : get_xndxdata (Elf *elf, Elf_Scn *symscn)
388 : {
389 122 : Elf_Data *xndxdata = NULL;
390 : GElf_Shdr shdr_mem;
391 122 : GElf_Shdr *shdr = gelf_getshdr (symscn, &shdr_mem);
392 122 : if (shdr != NULL && shdr->sh_type == SHT_SYMTAB)
393 : {
394 122 : size_t scnndx = elf_ndxscn (symscn);
395 122 : Elf_Scn *xndxscn = NULL;
396 4144 : while ((xndxscn = elf_nextscn (elf, xndxscn)) != NULL)
397 : {
398 : GElf_Shdr xndxshdr_mem;
399 3900 : GElf_Shdr *xndxshdr = gelf_getshdr (xndxscn, &xndxshdr_mem);
400 :
401 3900 : if (xndxshdr != NULL
402 3900 : && xndxshdr->sh_type == SHT_SYMTAB_SHNDX
403 0 : && xndxshdr->sh_link == scnndx)
404 : {
405 0 : xndxdata = elf_getdata (xndxscn, NULL);
406 0 : break;
407 : }
408 : }
409 : }
410 :
411 122 : return xndxdata;
412 : }
413 :
414 : /* Updates the shdrstrndx for the given Elf by updating the Ehdr and
415 : possibly the section zero extension field. Returns zero on success. */
416 : static int
417 173 : update_shdrstrndx (Elf *elf, size_t shdrstrndx)
418 : {
419 : GElf_Ehdr ehdr;
420 173 : if (gelf_getehdr (elf, &ehdr) == 0)
421 : return 1;
422 :
423 173 : if (shdrstrndx < SHN_LORESERVE)
424 171 : ehdr.e_shstrndx = shdrstrndx;
425 : else
426 : {
427 2 : ehdr.e_shstrndx = SHN_XINDEX;
428 2 : Elf_Scn *scn0 = elf_getscn (elf, 0);
429 : GElf_Shdr shdr0_mem;
430 2 : GElf_Shdr *shdr0 = gelf_getshdr (scn0, &shdr0_mem);
431 2 : if (shdr0 == NULL)
432 0 : return 1;
433 :
434 2 : shdr0->sh_link = shdrstrndx;
435 2 : if (gelf_update_shdr (scn0, shdr0) == 0)
436 : return 1;
437 : }
438 :
439 173 : if (unlikely (gelf_update_ehdr (elf, &ehdr) == 0))
440 : return 1;
441 :
442 173 : return 0;
443 : }
444 :
445 : /* Remove any relocations between debug sections in ET_REL
446 : for the debug file when requested. These relocations are always
447 : zero based between the unallocated sections. */
448 : static void
449 27 : remove_debug_relocations (Ebl *ebl, Elf *elf, GElf_Ehdr *ehdr,
450 : const char *fname, size_t shstrndx)
451 : {
452 27 : Elf_Scn *scn = NULL;
453 898 : while ((scn = elf_nextscn (elf, scn)) != NULL)
454 : {
455 : /* We need the actual section and header from the elf
456 : not just the cached original in shdr_info because we
457 : might want to change the size. */
458 : GElf_Shdr shdr_mem;
459 844 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
460 844 : if (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
461 : {
462 : /* Make sure that this relocation section points to a
463 : section to relocate with contents, that isn't
464 : allocated and that is a debug section. */
465 122 : Elf_Scn *tscn = elf_getscn (elf, shdr->sh_info);
466 : GElf_Shdr tshdr_mem;
467 122 : GElf_Shdr *tshdr = gelf_getshdr (tscn, &tshdr_mem);
468 122 : if (tshdr->sh_type == SHT_NOBITS
469 122 : || tshdr->sh_size == 0
470 122 : || (tshdr->sh_flags & SHF_ALLOC) != 0)
471 0 : continue;
472 :
473 122 : const char *tname = elf_strptr (elf, shstrndx,
474 122 : tshdr->sh_name);
475 122 : if (! tname || ! ebl_debugscn_p (ebl, tname))
476 0 : continue;
477 :
478 : /* OK, lets relocate all trivial cross debug section
479 : relocations. */
480 122 : Elf_Data *reldata = elf_getdata (scn, NULL);
481 122 : if (reldata == NULL || reldata->d_buf == NULL)
482 0 : INTERNAL_ERROR (fname);
483 :
484 : /* Make sure we adjust the uncompressed debug data
485 : (and recompress if necessary at the end). */
486 : GElf_Chdr tchdr;
487 122 : int tcompress_type = 0;
488 122 : bool is_gnu_compressed = false;
489 122 : if (strncmp (tname, ".zdebug", strlen ("zdebug")) == 0)
490 : {
491 4 : is_gnu_compressed = true;
492 4 : if (elf_compress_gnu (tscn, 0, 0) != 1)
493 0 : INTERNAL_ERROR (fname);
494 : }
495 : else
496 : {
497 118 : if (gelf_getchdr (tscn, &tchdr) != NULL)
498 : {
499 14 : tcompress_type = tchdr.ch_type;
500 14 : if (elf_compress (tscn, 0, 0) != 1)
501 0 : INTERNAL_ERROR (fname);
502 : }
503 : }
504 :
505 122 : Elf_Data *tdata = elf_getdata (tscn, NULL);
506 122 : if (tdata == NULL || tdata->d_buf == NULL
507 122 : || tdata->d_type != ELF_T_BYTE)
508 0 : INTERNAL_ERROR (fname);
509 :
510 : /* Pick up the symbol table and shndx table to
511 : resolve relocation symbol indexes. */
512 122 : Elf64_Word symt = shdr->sh_link;
513 : Elf_Data *symdata, *xndxdata;
514 122 : Elf_Scn * symscn = elf_getscn (elf, symt);
515 122 : symdata = elf_getdata (symscn, NULL);
516 122 : xndxdata = get_xndxdata (elf, symscn);
517 122 : if (symdata == NULL)
518 0 : INTERNAL_ERROR (fname);
519 :
520 : /* Apply one relocation. Returns true when trivial
521 : relocation actually done. */
522 78542 : bool relocate (GElf_Addr offset, const GElf_Sxword addend,
523 : bool is_rela, int rtype, int symndx)
524 : {
525 : /* R_*_NONE relocs can always just be removed. */
526 78542 : if (rtype == 0)
527 : return true;
528 :
529 : /* We only do simple absolute relocations. */
530 78542 : int addsub = 0;
531 78542 : Elf_Type type = ebl_reloc_simple_type (ebl, rtype, &addsub);
532 78542 : if (type == ELF_T_NUM)
533 : return false;
534 :
535 : /* These are the types we can relocate. */
536 : #define TYPES DO_TYPE (BYTE, Byte); DO_TYPE (HALF, Half); \
537 : DO_TYPE (WORD, Word); DO_TYPE (SWORD, Sword); \
538 : DO_TYPE (XWORD, Xword); DO_TYPE (SXWORD, Sxword)
539 :
540 : /* And only for relocations against other debug sections. */
541 : GElf_Sym sym_mem;
542 : Elf32_Word xndx;
543 78534 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
544 : symndx, &sym_mem,
545 : &xndx);
546 157068 : Elf32_Word sec = (sym->st_shndx == SHN_XINDEX
547 78534 : ? xndx : sym->st_shndx);
548 :
549 78534 : if (ebl_debugscn_p (ebl, secndx_name (elf, sec)))
550 : {
551 : size_t size;
552 :
553 : #define DO_TYPE(NAME, Name) GElf_##Name Name;
554 : union { TYPES; } tmpbuf;
555 : #undef DO_TYPE
556 :
557 49236 : switch (type)
558 : {
559 : #define DO_TYPE(NAME, Name) \
560 : case ELF_T_##NAME: \
561 : size = sizeof (GElf_##Name); \
562 : tmpbuf.Name = 0; \
563 : break;
564 0 : TYPES;
565 : #undef DO_TYPE
566 : default:
567 : return false;
568 : }
569 :
570 49236 : if (offset > tdata->d_size
571 49236 : || tdata->d_size - offset < size)
572 : {
573 0 : cleanup_debug ();
574 0 : error (EXIT_FAILURE, 0, gettext ("bad relocation"));
575 : }
576 :
577 : /* When the symbol value is zero then for SHT_REL
578 : sections this is all that needs to be checked.
579 : The addend is contained in the original data at
580 : the offset already. So if the (section) symbol
581 : address is zero and the given addend is zero
582 : just remove the relocation, it isn't needed
583 : anymore. */
584 49236 : if (addend == 0 && sym->st_value == 0)
585 : return true;
586 :
587 42704 : Elf_Data tmpdata =
588 : {
589 : .d_type = type,
590 : .d_buf = &tmpbuf,
591 : .d_size = size,
592 : .d_version = EV_CURRENT,
593 : };
594 85408 : Elf_Data rdata =
595 : {
596 : .d_type = type,
597 42704 : .d_buf = tdata->d_buf + offset,
598 : .d_size = size,
599 : .d_version = EV_CURRENT,
600 : };
601 :
602 42704 : GElf_Addr value = sym->st_value;
603 42704 : if (is_rela)
604 : {
605 : /* For SHT_RELA sections we just take the
606 : given addend and add it to the value. */
607 42704 : value += addend;
608 : /* For ADD/SUB relocations we need to fetch the
609 : current section contents. */
610 42704 : if (addsub != 0)
611 : {
612 0 : Elf_Data *d = gelf_xlatetom (elf, &tmpdata,
613 : &rdata,
614 0 : ehdr->e_ident[EI_DATA]);
615 0 : if (d == NULL)
616 0 : INTERNAL_ERROR (fname);
617 0 : assert (d == &tmpdata);
618 : }
619 : }
620 : else
621 : {
622 : /* For SHT_REL sections we have to peek at
623 : what is already in the section at the given
624 : offset to get the addend. */
625 0 : Elf_Data *d = gelf_xlatetom (elf, &tmpdata,
626 : &rdata,
627 0 : ehdr->e_ident[EI_DATA]);
628 0 : if (d == NULL)
629 0 : INTERNAL_ERROR (fname);
630 0 : assert (d == &tmpdata);
631 : }
632 :
633 42704 : switch (type)
634 : {
635 : #define DO_TYPE(NAME, Name) \
636 : case ELF_T_##NAME: \
637 : if (addsub < 0) \
638 : tmpbuf.Name -= (GElf_##Name) value; \
639 : else \
640 : tmpbuf.Name += (GElf_##Name) value; \
641 : break;
642 0 : TYPES;
643 : #undef DO_TYPE
644 0 : default:
645 0 : abort ();
646 : }
647 :
648 : /* Now finally put in the new value. */
649 42704 : Elf_Data *s = gelf_xlatetof (elf, &rdata,
650 : &tmpdata,
651 42704 : ehdr->e_ident[EI_DATA]);
652 42704 : if (s == NULL)
653 0 : INTERNAL_ERROR (fname);
654 42704 : assert (s == &rdata);
655 :
656 : return true;
657 : }
658 : return false;
659 : }
660 :
661 122 : if (shdr->sh_entsize == 0)
662 0 : INTERNAL_ERROR (fname);
663 :
664 122 : size_t nrels = shdr->sh_size / shdr->sh_entsize;
665 122 : size_t next = 0;
666 122 : if (shdr->sh_type == SHT_REL)
667 6424 : for (size_t relidx = 0; relidx < nrels; ++relidx)
668 : {
669 : GElf_Rel rel_mem;
670 6424 : GElf_Rel *r = gelf_getrel (reldata, relidx, &rel_mem);
671 6424 : if (! relocate (r->r_offset, 0, false,
672 : GELF_R_TYPE (r->r_info),
673 6424 : GELF_R_SYM (r->r_info)))
674 : {
675 36 : if (relidx != next)
676 32 : gelf_update_rel (reldata, next, r);
677 36 : ++next;
678 : }
679 : }
680 : else
681 72118 : for (size_t relidx = 0; relidx < nrels; ++relidx)
682 : {
683 : GElf_Rela rela_mem;
684 72118 : GElf_Rela *r = gelf_getrela (reldata, relidx, &rela_mem);
685 72118 : if (! relocate (r->r_offset, r->r_addend, true,
686 : GELF_R_TYPE (r->r_info),
687 72118 : GELF_R_SYM (r->r_info)))
688 : {
689 29270 : if (relidx != next)
690 13242 : gelf_update_rela (reldata, next, r);
691 29270 : ++next;
692 : }
693 : }
694 :
695 122 : nrels = next;
696 122 : shdr->sh_size = reldata->d_size = nrels * shdr->sh_entsize;
697 122 : gelf_update_shdr (scn, shdr);
698 :
699 122 : if (is_gnu_compressed)
700 : {
701 4 : if (elf_compress_gnu (tscn, 1, ELF_CHF_FORCE) != 1)
702 0 : INTERNAL_ERROR (fname);
703 : }
704 118 : else if (tcompress_type != 0)
705 : {
706 14 : if (elf_compress (tscn, tcompress_type, ELF_CHF_FORCE) != 1)
707 0 : INTERNAL_ERROR (fname);
708 : }
709 : }
710 : }
711 27 : }
712 :
713 : static int
714 106 : process_file (const char *fname)
715 : {
716 : /* If we have to preserve the modify and access timestamps get them
717 : now. We cannot use fstat() after opening the file since the open
718 : would change the access time. */
719 : struct stat pre_st;
720 : struct timespec tv[2];
721 106 : again:
722 106 : if (preserve_dates)
723 : {
724 0 : if (stat (fname, &pre_st) != 0)
725 : {
726 0 : error (0, errno, gettext ("cannot stat input file '%s'"), fname);
727 0 : return 1;
728 : }
729 :
730 : /* If we have to preserve the timestamp, we need it in the
731 : format utimes() understands. */
732 0 : tv[0] = pre_st.st_atim;
733 0 : tv[1] = pre_st.st_mtim;
734 : }
735 :
736 : /* Open the file. */
737 212 : int fd = open (fname, output_fname == NULL ? O_RDWR : O_RDONLY);
738 106 : if (fd == -1)
739 : {
740 0 : error (0, errno, gettext ("while opening '%s'"), fname);
741 0 : return 1;
742 : }
743 :
744 : /* We always use fstat() even if we called stat() before. This is
745 : done to make sure the information returned by stat() is for the
746 : same file. */
747 : struct stat st;
748 106 : if (fstat (fd, &st) != 0)
749 : {
750 0 : error (0, errno, gettext ("cannot stat input file '%s'"), fname);
751 0 : return 1;
752 : }
753 : /* Paranoid mode on. */
754 106 : if (preserve_dates
755 0 : && (st.st_ino != pre_st.st_ino || st.st_dev != pre_st.st_dev))
756 : {
757 : /* We detected a race. Try again. */
758 0 : close (fd);
759 0 : goto again;
760 : }
761 :
762 : /* Now get the ELF descriptor. */
763 106 : Elf *elf = elf_begin (fd, output_fname == NULL ? ELF_C_RDWR : ELF_C_READ,
764 : NULL);
765 : int result;
766 106 : switch (elf_kind (elf))
767 : {
768 106 : case ELF_K_ELF:
769 106 : result = handle_elf (fd, elf, NULL, fname, st.st_mode & ACCESSPERMS,
770 106 : preserve_dates ? tv : NULL);
771 106 : break;
772 :
773 0 : case ELF_K_AR:
774 : /* It is not possible to strip the content of an archive direct
775 : the output to a specific file. */
776 0 : if (unlikely (output_fname != NULL || debug_fname != NULL))
777 : {
778 0 : error (0, 0, gettext ("%s: cannot use -o or -f when stripping archive"),
779 : fname);
780 0 : result = 1;
781 : }
782 : else
783 : {
784 : /* We would like to support ar archives, but currently it just
785 : doesn't work at all since we call elf_clone on the members
786 : which doesn't really support ar members.
787 : result = handle_ar (fd, elf, NULL, fname,
788 : preserve_dates ? tv : NULL);
789 : */
790 0 : error (0, 0, gettext ("%s: no support for stripping archive"),
791 : fname);
792 0 : result = 1;
793 : }
794 : break;
795 :
796 0 : default:
797 0 : error (0, 0, gettext ("%s: File format not recognized"), fname);
798 0 : result = 1;
799 0 : break;
800 : }
801 :
802 106 : if (unlikely (elf_end (elf) != 0))
803 0 : INTERNAL_ERROR (fname);
804 :
805 106 : close (fd);
806 :
807 106 : return result;
808 : }
809 :
810 : /* Processing for --reloc-debug-sections-only. */
811 : static int
812 14 : handle_debug_relocs (Elf *elf, Ebl *ebl, Elf *new_elf,
813 : GElf_Ehdr *ehdr, const char *fname, size_t shstrndx,
814 : GElf_Off *last_offset, GElf_Xword *last_size)
815 : {
816 :
817 : /* Copy over the ELF header. */
818 14 : if (gelf_update_ehdr (new_elf, ehdr) == 0)
819 : {
820 0 : error (0, 0, "couldn't update new ehdr: %s", elf_errmsg (-1));
821 0 : return 1;
822 : }
823 :
824 : /* Copy over sections and record end of allocated sections. */
825 : GElf_Off lastoffset = 0;
826 : Elf_Scn *scn = NULL;
827 455 : while ((scn = elf_nextscn (elf, scn)) != NULL)
828 : {
829 : /* Get the header. */
830 : GElf_Shdr shdr;
831 441 : if (gelf_getshdr (scn, &shdr) == NULL)
832 : {
833 0 : error (0, 0, "couldn't get shdr: %s", elf_errmsg (-1));
834 0 : return 1;
835 : }
836 :
837 : /* Create new section. */
838 441 : Elf_Scn *new_scn = elf_newscn (new_elf);
839 441 : if (new_scn == NULL)
840 : {
841 0 : error (0, 0, "couldn't create new section: %s", elf_errmsg (-1));
842 0 : return 1;
843 : }
844 :
845 441 : if (gelf_update_shdr (new_scn, &shdr) == 0)
846 : {
847 0 : error (0, 0, "couldn't update shdr: %s", elf_errmsg (-1));
848 0 : return 1;
849 : }
850 :
851 : /* Copy over section data. */
852 : Elf_Data *data = NULL;
853 882 : while ((data = elf_getdata (scn, data)) != NULL)
854 : {
855 441 : Elf_Data *new_data = elf_newdata (new_scn);
856 441 : if (new_data == NULL)
857 : {
858 0 : error (0, 0, "couldn't create new section data: %s",
859 : elf_errmsg (-1));
860 0 : return 1;
861 : }
862 441 : *new_data = *data;
863 : }
864 :
865 : /* Record last offset of allocated section. */
866 441 : if ((shdr.sh_flags & SHF_ALLOC) != 0)
867 : {
868 306 : GElf_Off filesz = (shdr.sh_type != SHT_NOBITS
869 153 : ? shdr.sh_size : 0);
870 153 : if (lastoffset < shdr.sh_offset + filesz)
871 31 : lastoffset = shdr.sh_offset + filesz;
872 : }
873 : }
874 :
875 : /* Make sure section header name table is setup correctly, we'll
876 : need it to determine whether to relocate sections. */
877 14 : if (update_shdrstrndx (new_elf, shstrndx) != 0)
878 : {
879 0 : error (0, 0, "error updating shdrstrndx: %s", elf_errmsg (-1));
880 0 : return 1;
881 : }
882 :
883 : /* Adjust the relocation sections. */
884 14 : remove_debug_relocations (ebl, new_elf, ehdr, fname, shstrndx);
885 :
886 : /* Adjust the offsets of the non-allocated sections, so they come after
887 : the allocated sections. */
888 14 : scn = NULL;
889 14 : while ((scn = elf_nextscn (new_elf, scn)) != NULL)
890 : {
891 : /* Get the header. */
892 : GElf_Shdr shdr;
893 441 : if (gelf_getshdr (scn, &shdr) == NULL)
894 : {
895 0 : error (0, 0, "couldn't get shdr: %s", elf_errmsg (-1));
896 0 : return 1;
897 : }
898 :
899 : /* Adjust non-allocated section offsets to be after any allocated. */
900 441 : if ((shdr.sh_flags & SHF_ALLOC) == 0)
901 : {
902 576 : shdr.sh_offset = ((lastoffset + shdr.sh_addralign - 1)
903 288 : & ~((GElf_Off) (shdr.sh_addralign - 1)));
904 288 : if (gelf_update_shdr (scn, &shdr) == 0)
905 : {
906 0 : error (0, 0, "couldn't update shdr: %s", elf_errmsg (-1));
907 0 : return 1;
908 : }
909 :
910 576 : GElf_Off filesz = (shdr.sh_type != SHT_NOBITS
911 288 : ? shdr.sh_size : 0);
912 288 : lastoffset = shdr.sh_offset + filesz;
913 288 : *last_offset = shdr.sh_offset;
914 288 : *last_size = filesz;
915 : }
916 : }
917 :
918 : return 0;
919 : }
920 :
921 : /* Maximum size of array allocated on stack. */
922 : #define MAX_STACK_ALLOC (400 * 1024)
923 :
924 : static int
925 106 : handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
926 : mode_t mode, struct timespec tvp[2])
927 : {
928 106 : size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
929 106 : size_t fname_len = strlen (fname) + 1;
930 106 : char *fullname = alloca (prefix_len + 1 + fname_len);
931 106 : char *cp = fullname;
932 106 : Elf *debugelf = NULL;
933 106 : tmp_debug_fname = NULL;
934 106 : int result = 0;
935 106 : size_t shdridx = 0;
936 106 : GElf_Off lastsec_offset = 0;
937 106 : Elf64_Xword lastsec_size = 0;
938 : size_t shstrndx;
939 : struct shdr_info
940 : {
941 : Elf_Scn *scn;
942 : GElf_Shdr shdr;
943 : Elf_Data *data;
944 : Elf_Data *debug_data;
945 : const char *name;
946 : Elf32_Word idx; /* Index in new file. */
947 : Elf32_Word old_sh_link; /* Original value of shdr.sh_link. */
948 : Elf32_Word symtab_idx;
949 : Elf32_Word version_idx;
950 : Elf32_Word group_idx;
951 : Elf32_Word group_cnt;
952 : Elf_Scn *newscn;
953 : Dwelf_Strent *se;
954 : Elf32_Word *newsymidx;
955 106 : } *shdr_info = NULL;
956 : Elf_Scn *scn;
957 : size_t cnt;
958 : size_t idx;
959 : bool changes;
960 : GElf_Ehdr newehdr_mem;
961 : GElf_Ehdr *newehdr;
962 : GElf_Ehdr debugehdr_mem;
963 : GElf_Ehdr *debugehdr;
964 106 : Dwelf_Strtab *shst = NULL;
965 : Elf_Data debuglink_crc_data;
966 106 : bool any_symtab_changes = false;
967 106 : Elf_Data *shstrtab_data = NULL;
968 106 : void *debuglink_buf = NULL;
969 :
970 : /* Create the full name of the file. */
971 106 : if (prefix != NULL)
972 : {
973 0 : cp = mempcpy (cp, prefix, prefix_len);
974 0 : *cp++ = ':';
975 : }
976 212 : memcpy (cp, fname, fname_len);
977 :
978 : /* If we are not replacing the input file open a new file here. */
979 106 : if (output_fname != NULL)
980 : {
981 152 : fd = open (output_fname, O_RDWR | O_CREAT, mode);
982 76 : if (unlikely (fd == -1))
983 : {
984 0 : error (0, errno, gettext ("cannot open '%s'"), output_fname);
985 0 : return 1;
986 : }
987 : }
988 :
989 106 : debug_fd = -1;
990 :
991 : /* Get the EBL handling. Removing all debugging symbols with the -g
992 : option or resolving all relocations between debug sections with
993 : the --reloc-debug-sections option are currently the only reasons
994 : we need EBL so don't open the backend unless necessary. */
995 106 : Ebl *ebl = NULL;
996 106 : if (remove_debug || reloc_debug || reloc_debug_only)
997 : {
998 53 : ebl = ebl_openbackend (elf);
999 53 : if (ebl == NULL)
1000 : {
1001 0 : error (0, errno, gettext ("cannot open EBL backend"));
1002 0 : result = 1;
1003 0 : goto fail;
1004 : }
1005 : }
1006 :
1007 : /* Open the additional file the debug information will be stored in. */
1008 106 : if (debug_fname != NULL)
1009 : {
1010 : /* Create a temporary file name. We do not want to overwrite
1011 : the debug file if the file would not contain any
1012 : information. */
1013 56 : size_t debug_fname_len = strlen (debug_fname);
1014 56 : tmp_debug_fname = (char *) xmalloc (debug_fname_len + sizeof (".XXXXXX"));
1015 168 : strcpy (mempcpy (tmp_debug_fname, debug_fname, debug_fname_len),
1016 : ".XXXXXX");
1017 :
1018 56 : debug_fd = mkstemp (tmp_debug_fname);
1019 56 : if (unlikely (debug_fd == -1))
1020 : {
1021 0 : error (0, errno, gettext ("cannot open '%s'"), debug_fname);
1022 0 : result = 1;
1023 0 : goto fail;
1024 : }
1025 : }
1026 :
1027 : /* Get the information from the old file. */
1028 : GElf_Ehdr ehdr_mem;
1029 106 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
1030 106 : if (ehdr == NULL)
1031 0 : INTERNAL_ERROR (fname);
1032 :
1033 : /* Get the section header string table index. */
1034 106 : if (unlikely (elf_getshdrstrndx (elf, &shstrndx) < 0))
1035 : {
1036 0 : cleanup_debug ();
1037 : error (EXIT_FAILURE, 0,
1038 0 : gettext ("cannot get section header string table index"));
1039 : }
1040 :
1041 : /* Get the number of phdrs in the old file. */
1042 : size_t phnum;
1043 106 : if (elf_getphdrnum (elf, &phnum) != 0)
1044 : {
1045 0 : cleanup_debug ();
1046 0 : error (EXIT_FAILURE, 0, gettext ("cannot get number of phdrs"));
1047 : }
1048 :
1049 : /* We now create a new ELF descriptor for the same file. We
1050 : construct it almost exactly in the same way with some information
1051 : dropped. */
1052 : Elf *newelf;
1053 106 : if (output_fname != NULL)
1054 76 : newelf = elf_begin (fd, ELF_C_WRITE_MMAP, NULL);
1055 : else
1056 30 : newelf = elf_clone (elf, ELF_C_EMPTY);
1057 :
1058 106 : if (unlikely (gelf_newehdr (newelf, gelf_getclass (elf)) == 0))
1059 : {
1060 0 : error (0, 0, gettext ("cannot create new ehdr for file '%s': %s"),
1061 0 : output_fname ?: fname, elf_errmsg (-1));
1062 : goto fail;
1063 : }
1064 :
1065 : /* Copy over the old program header if needed. */
1066 106 : if (phnum > 0)
1067 : {
1068 53 : if (unlikely (gelf_newphdr (newelf, phnum) == 0))
1069 : {
1070 0 : error (0, 0, gettext ("cannot create new phdr for file '%s': %s"),
1071 0 : output_fname ?: fname, elf_errmsg (-1));
1072 : goto fail;
1073 : }
1074 :
1075 453 : for (cnt = 0; cnt < phnum; ++cnt)
1076 : {
1077 : GElf_Phdr phdr_mem;
1078 400 : GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
1079 400 : if (phdr == NULL
1080 400 : || unlikely (gelf_update_phdr (newelf, cnt, phdr) == 0))
1081 0 : INTERNAL_ERROR (fname);
1082 : }
1083 : }
1084 :
1085 106 : if (reloc_debug_only)
1086 : {
1087 14 : if (handle_debug_relocs (elf, ebl, newelf, ehdr, fname, shstrndx,
1088 : &lastsec_offset, &lastsec_size) != 0)
1089 : {
1090 : result = 1;
1091 : goto fail_close;
1092 : }
1093 14 : idx = shstrndx;
1094 14 : goto done; /* Skip all actual stripping operations. */
1095 : }
1096 :
1097 92 : if (debug_fname != NULL)
1098 : {
1099 : /* Also create an ELF descriptor for the debug file */
1100 56 : debugelf = elf_begin (debug_fd, ELF_C_WRITE_MMAP, NULL);
1101 56 : if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0))
1102 : {
1103 0 : error (0, 0, gettext ("cannot create new ehdr for file '%s': %s"),
1104 : debug_fname, elf_errmsg (-1));
1105 : goto fail_close;
1106 : }
1107 :
1108 : /* Copy over the old program header if needed. */
1109 56 : if (phnum > 0)
1110 : {
1111 23 : if (unlikely (gelf_newphdr (debugelf, phnum) == 0))
1112 : {
1113 0 : error (0, 0, gettext ("cannot create new phdr for file '%s': %s"),
1114 : debug_fname, elf_errmsg (-1));
1115 : goto fail_close;
1116 : }
1117 :
1118 197 : for (cnt = 0; cnt < phnum; ++cnt)
1119 : {
1120 : GElf_Phdr phdr_mem;
1121 174 : GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
1122 174 : if (phdr == NULL
1123 174 : || unlikely (gelf_update_phdr (debugelf, cnt, phdr) == 0))
1124 0 : INTERNAL_ERROR (fname);
1125 : }
1126 : }
1127 : }
1128 :
1129 : /* Number of sections. */
1130 : size_t shnum;
1131 92 : if (unlikely (elf_getshdrnum (elf, &shnum) < 0))
1132 : {
1133 0 : error (0, 0, gettext ("cannot determine number of sections: %s"),
1134 : elf_errmsg (-1));
1135 : goto fail_close;
1136 : }
1137 :
1138 92 : if (shstrndx >= shnum)
1139 : goto illformed;
1140 :
1141 : #define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
1142 :
1143 : /* Storage for section information. We leave room for two more
1144 : entries since we unconditionally create a section header string
1145 : table. Maybe some weird tool created an ELF file without one.
1146 : The other one is used for the debug link section. */
1147 92 : if ((shnum + 2) * sizeof (struct shdr_info) > MAX_STACK_ALLOC)
1148 3 : shdr_info = (struct shdr_info *) xcalloc (shnum + 2,
1149 : sizeof (struct shdr_info));
1150 : else
1151 : {
1152 89 : shdr_info = (struct shdr_info *) alloca ((shnum + 2)
1153 : * sizeof (struct shdr_info));
1154 89 : memset (shdr_info, '\0', (shnum + 2) * sizeof (struct shdr_info));
1155 : }
1156 :
1157 : /* Track whether allocated sections all come before non-allocated ones. */
1158 92 : bool seen_allocated = false;
1159 92 : bool seen_unallocated = false;
1160 92 : bool mixed_allocated_unallocated = false;
1161 :
1162 : /* Prepare section information data structure. */
1163 92 : scn = NULL;
1164 92 : cnt = 1;
1165 199703 : while ((scn = elf_nextscn (elf, scn)) != NULL)
1166 : {
1167 : /* This should always be true (i.e., there should not be any
1168 : holes in the numbering). */
1169 199519 : elf_assert (elf_ndxscn (scn) == cnt);
1170 :
1171 199519 : shdr_info[cnt].scn = scn;
1172 :
1173 : /* Get the header. */
1174 199519 : if (gelf_getshdr (scn, &shdr_info[cnt].shdr) == NULL)
1175 0 : INTERNAL_ERROR (fname);
1176 :
1177 : /* Normally (in non-ET_REL files) we see all allocated sections first,
1178 : then all non-allocated. */
1179 199519 : if ((shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) == 0)
1180 : seen_unallocated = true;
1181 : else
1182 : {
1183 1536 : if (seen_unallocated && seen_allocated)
1184 280 : mixed_allocated_unallocated = true;
1185 : seen_allocated = true;
1186 : }
1187 :
1188 : /* Get the name of the section. */
1189 199519 : shdr_info[cnt].name = elf_strptr (elf, shstrndx,
1190 199519 : shdr_info[cnt].shdr.sh_name);
1191 199519 : if (shdr_info[cnt].name == NULL)
1192 : {
1193 0 : illformed:
1194 1 : error (0, 0, gettext ("illformed file '%s'"), fname);
1195 : goto fail_close;
1196 : }
1197 :
1198 : /* Sanity check the user. */
1199 399038 : if (section_name_matches (remove_secs, shdr_info[cnt].name))
1200 : {
1201 6 : if ((shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) != 0)
1202 : {
1203 0 : error (0, 0,
1204 0 : gettext ("Cannot remove allocated section '%s'"),
1205 : shdr_info[cnt].name);
1206 0 : result = 1;
1207 0 : goto fail_close;
1208 : }
1209 :
1210 12 : if (section_name_matches (keep_secs, shdr_info[cnt].name))
1211 : {
1212 0 : error (0, 0,
1213 0 : gettext ("Cannot both keep and remove section '%s'"),
1214 0 : shdr_info[cnt].name);
1215 0 : result = 1;
1216 0 : goto fail_close;
1217 : }
1218 : }
1219 :
1220 : /* Mark them as present but not yet investigated. */
1221 199519 : shdr_info[cnt].idx = 1;
1222 :
1223 : /* Remember the shdr.sh_link value. */
1224 199519 : shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
1225 199519 : if (shdr_info[cnt].old_sh_link >= shnum)
1226 : goto illformed;
1227 :
1228 : /* Sections in files other than relocatable object files which
1229 : not loaded can be freely moved by us. In theory we can also
1230 : freely move around allocated nobits sections. But we don't
1231 : to keep the layout of all allocated sections as similar as
1232 : possible to the original file. In relocatable object files
1233 : everything can be moved. */
1234 199519 : if (phnum == 0
1235 198328 : || (shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) == 0)
1236 198326 : shdr_info[cnt].shdr.sh_offset = 0;
1237 :
1238 : /* If this is an extended section index table store an
1239 : appropriate reference. */
1240 199519 : if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
1241 : {
1242 0 : elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
1243 0 : shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
1244 : }
1245 199519 : else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
1246 : {
1247 : /* Cross-reference the sections contained in the section
1248 : group. */
1249 10 : shdr_info[cnt].data = elf_getdata (shdr_info[cnt].scn, NULL);
1250 10 : if (shdr_info[cnt].data == NULL
1251 10 : || shdr_info[cnt].data->d_size < sizeof (Elf32_Word))
1252 0 : INTERNAL_ERROR (fname);
1253 :
1254 : /* XXX Fix for unaligned access. */
1255 10 : Elf32_Word *grpref = (Elf32_Word *) shdr_info[cnt].data->d_buf;
1256 : size_t inner;
1257 38 : for (inner = 1;
1258 28 : inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
1259 18 : ++inner)
1260 : {
1261 18 : if (grpref[inner] < shnum)
1262 18 : shdr_info[grpref[inner]].group_idx = cnt;
1263 : else
1264 : goto illformed;
1265 : }
1266 :
1267 10 : if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
1268 : /* If the section group contains only one element and this
1269 : is n COMDAT section we can drop it right away. */
1270 2 : shdr_info[cnt].idx = 0;
1271 : else
1272 8 : shdr_info[cnt].group_cnt = inner - 1;
1273 : }
1274 199509 : else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
1275 : {
1276 52 : elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
1277 52 : shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
1278 : }
1279 :
1280 : /* If this section is part of a group make sure it is not
1281 : discarded right away. */
1282 199519 : if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
1283 : {
1284 18 : elf_assert (shdr_info[cnt].group_idx != 0);
1285 :
1286 18 : if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
1287 : {
1288 : /* The section group section might be removed.
1289 : Don't remove the SHF_GROUP flag. The section is
1290 : either also removed, in which case the flag doesn't matter.
1291 : Or it moves with the group into the debug file, then
1292 : it will be reconnected with the new group and should
1293 : still have the flag set. */
1294 2 : shdr_info[cnt].group_idx = 0;
1295 : }
1296 : }
1297 :
1298 : /* Increment the counter. */
1299 199519 : ++cnt;
1300 : }
1301 :
1302 : /* Now determine which sections can go away. The general rule is that
1303 : all sections which are not used at runtime are stripped out. But
1304 : there are a few exceptions:
1305 :
1306 : - special sections named ".comment" and ".note" are kept
1307 : - OS or architecture specific sections are kept since we might not
1308 : know how to handle them
1309 : - if a section is referred to from a section which is not removed
1310 : in the sh_link or sh_info element it cannot be removed either
1311 : - the user might have explicitly said to remove or keep a section
1312 : */
1313 199611 : for (cnt = 1; cnt < shnum; ++cnt)
1314 : /* Check whether the section can be removed. Since we will create
1315 : a new .shstrtab assume it will be removed too. */
1316 399038 : if (remove_shdrs ? !(shdr_info[cnt].shdr.sh_flags & SHF_ALLOC)
1317 399038 : : (ebl_section_strip_p (ebl, &shdr_info[cnt].shdr,
1318 199519 : shdr_info[cnt].name, remove_comment,
1319 : remove_debug)
1320 67259 : || cnt == shstrndx
1321 333987 : || section_name_matches (remove_secs, shdr_info[cnt].name)))
1322 : {
1323 : /* The user might want to explicitly keep this one. */
1324 264582 : if (section_name_matches (keep_secs, shdr_info[cnt].name))
1325 6 : continue;
1326 :
1327 : /* For now assume this section will be removed. */
1328 132285 : shdr_info[cnt].idx = 0;
1329 :
1330 132285 : idx = shdr_info[cnt].group_idx;
1331 264571 : while (idx != 0)
1332 : {
1333 : /* The section group data is already loaded. */
1334 15 : elf_assert (shdr_info[idx].data != NULL
1335 : && shdr_info[idx].data->d_buf != NULL
1336 : && shdr_info[idx].data->d_size >= sizeof (Elf32_Word));
1337 :
1338 : /* If the references section group is a normal section
1339 : group and has one element remaining, or if it is an
1340 : empty COMDAT section group it is removed. */
1341 10 : bool is_comdat = (((Elf32_Word *) shdr_info[idx].data->d_buf)[0]
1342 5 : & GRP_COMDAT) != 0;
1343 :
1344 5 : --shdr_info[idx].group_cnt;
1345 5 : if ((!is_comdat && shdr_info[idx].group_cnt == 1)
1346 5 : || (is_comdat && shdr_info[idx].group_cnt == 0))
1347 : {
1348 1 : shdr_info[idx].idx = 0;
1349 : /* Continue recursively. */
1350 1 : idx = shdr_info[idx].group_idx;
1351 : }
1352 : else
1353 : break;
1354 : }
1355 : }
1356 :
1357 : /* Mark the SHT_NULL section as handled. */
1358 92 : shdr_info[0].idx = 2;
1359 :
1360 :
1361 : /* Handle exceptions: section groups and cross-references. We might
1362 : have to repeat this a few times since the resetting of the flag
1363 : might propagate. */
1364 : do
1365 : {
1366 92 : changes = false;
1367 :
1368 199611 : for (cnt = 1; cnt < shnum; ++cnt)
1369 : {
1370 199519 : if (shdr_info[cnt].idx == 0)
1371 : {
1372 : /* If a relocation section is marked as being removed make
1373 : sure the section it is relocating is removed, too. */
1374 264418 : if (shdr_info[cnt].shdr.sh_type == SHT_REL
1375 132209 : || shdr_info[cnt].shdr.sh_type == SHT_RELA)
1376 : {
1377 349 : if (shdr_info[cnt].shdr.sh_info >= shnum)
1378 : goto illformed;
1379 349 : else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
1380 159 : shdr_info[cnt].idx = 1;
1381 : }
1382 :
1383 : /* If a group section is marked as being removed make
1384 : sure all the sections it contains are being removed, too. */
1385 132209 : if (shdr_info[cnt].shdr.sh_type == SHT_GROUP)
1386 : {
1387 : Elf32_Word *grpref;
1388 10 : grpref = (Elf32_Word *) shdr_info[cnt].data->d_buf;
1389 21 : for (size_t in = 1;
1390 11 : in < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
1391 1 : ++in)
1392 10 : if (grpref[in] < shnum)
1393 : {
1394 10 : if (shdr_info[grpref[in]].idx != 0)
1395 : {
1396 9 : shdr_info[cnt].idx = 1;
1397 9 : break;
1398 : }
1399 : }
1400 : else
1401 : goto illformed;
1402 : }
1403 : }
1404 :
1405 199519 : if (shdr_info[cnt].idx == 1)
1406 : {
1407 : /* The content of symbol tables we don't remove must not
1408 : reference any section which we do remove. Otherwise
1409 : we cannot remove the section. */
1410 67478 : if (debug_fname != NULL
1411 66679 : && shdr_info[cnt].debug_data == NULL
1412 133218 : && (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM
1413 66609 : || shdr_info[cnt].shdr.sh_type == SHT_SYMTAB))
1414 : {
1415 : /* Make sure the data is loaded. */
1416 29 : if (shdr_info[cnt].data == NULL)
1417 : {
1418 : shdr_info[cnt].data
1419 29 : = elf_getdata (shdr_info[cnt].scn, NULL);
1420 29 : if (shdr_info[cnt].data == NULL)
1421 0 : INTERNAL_ERROR (fname);
1422 : }
1423 29 : Elf_Data *symdata = shdr_info[cnt].data;
1424 :
1425 : /* If there is an extended section index table load it
1426 : as well. */
1427 29 : if (shdr_info[cnt].symtab_idx != 0
1428 0 : && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
1429 : {
1430 0 : elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
1431 :
1432 : shdr_info[shdr_info[cnt].symtab_idx].data
1433 0 : = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
1434 : NULL);
1435 0 : if (shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
1436 0 : INTERNAL_ERROR (fname);
1437 : }
1438 29 : Elf_Data *xndxdata
1439 29 : = shdr_info[shdr_info[cnt].symtab_idx].data;
1440 :
1441 : /* Go through all symbols and make sure the section they
1442 : reference is not removed. */
1443 29 : size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
1444 :
1445 1075 : for (size_t inner = 0;
1446 1046 : inner < shdr_info[cnt].data->d_size / elsize;
1447 1017 : ++inner)
1448 : {
1449 : GElf_Sym sym_mem;
1450 : Elf32_Word xndx;
1451 1017 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
1452 : inner, &sym_mem,
1453 : &xndx);
1454 1017 : if (sym == NULL)
1455 0 : INTERNAL_ERROR (fname);
1456 :
1457 1017 : size_t scnidx = sym->st_shndx;
1458 1017 : if (scnidx == SHN_UNDEF || scnidx >= shnum
1459 555 : || (scnidx >= SHN_LORESERVE
1460 : && scnidx <= SHN_HIRESERVE
1461 555 : && scnidx != SHN_XINDEX)
1462 : /* Don't count in the section symbols. */
1463 544 : || GELF_ST_TYPE (sym->st_info) == STT_SECTION)
1464 : /* This is no section index, leave it alone. */
1465 699 : continue;
1466 318 : else if (scnidx == SHN_XINDEX)
1467 0 : scnidx = xndx;
1468 :
1469 318 : if (scnidx >= shnum)
1470 : goto illformed;
1471 :
1472 318 : if (shdr_info[scnidx].idx == 0)
1473 : /* This symbol table has a real symbol in
1474 : a discarded section. So preserve the
1475 : original table in the debug file. Unless
1476 : it is a redundant data marker to a debug
1477 : (data only) section. */
1478 12 : if (! (ebl_section_strip_p (ebl,
1479 4 : &shdr_info[scnidx].shdr,
1480 : shdr_info[scnidx].name,
1481 : remove_comment,
1482 : remove_debug)
1483 4 : && ebl_data_marker_symbol (ebl, sym,
1484 8 : elf_strptr (elf,
1485 4 : shdr_info[cnt].shdr.sh_link,
1486 4 : sym->st_name))))
1487 0 : shdr_info[cnt].debug_data = symdata;
1488 : }
1489 : }
1490 :
1491 : /* Cross referencing happens:
1492 : - for the cases the ELF specification says. That are
1493 : + SHT_DYNAMIC in sh_link to string table
1494 : + SHT_HASH in sh_link to symbol table
1495 : + SHT_REL and SHT_RELA in sh_link to symbol table
1496 : + SHT_SYMTAB and SHT_DYNSYM in sh_link to string table
1497 : + SHT_GROUP in sh_link to symbol table
1498 : + SHT_SYMTAB_SHNDX in sh_link to symbol table
1499 : Other (OS or architecture-specific) sections might as
1500 : well use this field so we process it unconditionally.
1501 : - references inside section groups
1502 : - specially marked references in sh_info if the SHF_INFO_LINK
1503 : flag is set
1504 : */
1505 :
1506 67478 : if (shdr_info[shdr_info[cnt].shdr.sh_link].idx == 0)
1507 : {
1508 76 : shdr_info[shdr_info[cnt].shdr.sh_link].idx = 1;
1509 76 : changes |= shdr_info[cnt].shdr.sh_link < cnt;
1510 : }
1511 :
1512 : /* Handle references through sh_info. */
1513 67478 : if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
1514 : {
1515 262 : if (shdr_info[cnt].shdr.sh_info >= shnum)
1516 : goto illformed;
1517 262 : else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
1518 : {
1519 0 : shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
1520 0 : changes |= shdr_info[cnt].shdr.sh_info < cnt;
1521 : }
1522 : }
1523 :
1524 : /* Mark the section as investigated. */
1525 67478 : shdr_info[cnt].idx = 2;
1526 : }
1527 :
1528 199519 : if (debug_fname != NULL
1529 132831 : && (shdr_info[cnt].idx == 0 || shdr_info[cnt].debug_data != NULL))
1530 : {
1531 : /* This section is being preserved in the debug file.
1532 : Sections it refers to must be preserved there too.
1533 :
1534 : In this pass we mark sections to be preserved in both
1535 : files by setting the .debug_data pointer to the original
1536 : file's .data pointer. Below, we'll copy the section
1537 : contents. */
1538 :
1539 66377 : inline void check_preserved (size_t i)
1540 : {
1541 66377 : if (i != 0 && i < shnum + 2 && shdr_info[i].idx != 0
1542 193 : && shdr_info[i].debug_data == NULL)
1543 : {
1544 70 : if (shdr_info[i].data == NULL)
1545 70 : shdr_info[i].data = elf_getdata (shdr_info[i].scn, NULL);
1546 70 : if (shdr_info[i].data == NULL)
1547 0 : INTERNAL_ERROR (fname);
1548 :
1549 70 : shdr_info[i].debug_data = shdr_info[i].data;
1550 70 : changes |= i < cnt;
1551 : }
1552 66377 : }
1553 :
1554 66222 : check_preserved (shdr_info[cnt].shdr.sh_link);
1555 66222 : if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
1556 155 : check_preserved (shdr_info[cnt].shdr.sh_info);
1557 : }
1558 : }
1559 : }
1560 92 : while (changes);
1561 :
1562 : /* Copy the removed sections to the debug output file.
1563 : The ones that are not removed in the stripped file are SHT_NOBITS. */
1564 92 : if (debug_fname != NULL)
1565 : {
1566 132887 : for (cnt = 1; cnt < shnum; ++cnt)
1567 : {
1568 132831 : scn = elf_newscn (debugelf);
1569 132831 : if (scn == NULL)
1570 : {
1571 0 : cleanup_debug ();
1572 0 : error (EXIT_FAILURE, 0,
1573 0 : gettext ("while generating output file: %s"),
1574 : elf_errmsg (-1));
1575 : }
1576 :
1577 265662 : bool discard_section = (shdr_info[cnt].idx > 0
1578 66679 : && shdr_info[cnt].debug_data == NULL
1579 66609 : && shdr_info[cnt].shdr.sh_type != SHT_NOTE
1580 66547 : && shdr_info[cnt].shdr.sh_type != SHT_GROUP
1581 199369 : && cnt != shstrndx);
1582 :
1583 : /* Set the section header in the new file. */
1584 132831 : GElf_Shdr debugshdr = shdr_info[cnt].shdr;
1585 132831 : if (discard_section)
1586 66538 : debugshdr.sh_type = SHT_NOBITS;
1587 :
1588 132831 : if (unlikely (gelf_update_shdr (scn, &debugshdr) == 0))
1589 : /* There cannot be any overflows. */
1590 0 : INTERNAL_ERROR (fname);
1591 :
1592 : /* Get the data from the old file if necessary. */
1593 132831 : if (shdr_info[cnt].data == NULL)
1594 : {
1595 132722 : shdr_info[cnt].data = elf_getdata (shdr_info[cnt].scn, NULL);
1596 132722 : if (shdr_info[cnt].data == NULL)
1597 0 : INTERNAL_ERROR (fname);
1598 : }
1599 :
1600 : /* Set the data. This is done by copying from the old file. */
1601 132831 : Elf_Data *debugdata = elf_newdata (scn);
1602 132831 : if (debugdata == NULL)
1603 0 : INTERNAL_ERROR (fname);
1604 :
1605 : /* Copy the structure. This data may be modified in place
1606 : before we write out the file. */
1607 132831 : *debugdata = *shdr_info[cnt].data;
1608 132831 : if (discard_section)
1609 66538 : debugdata->d_buf = NULL;
1610 66293 : else if (shdr_info[cnt].debug_data != NULL
1611 66223 : || shdr_info[cnt].shdr.sh_type == SHT_GROUP)
1612 : {
1613 : /* Copy the original data before it gets modified. */
1614 80 : shdr_info[cnt].debug_data = debugdata;
1615 80 : if (debugdata->d_buf == NULL)
1616 0 : INTERNAL_ERROR (fname);
1617 160 : debugdata->d_buf = memcpy (xmalloc (debugdata->d_size),
1618 : debugdata->d_buf, debugdata->d_size);
1619 : }
1620 : }
1621 :
1622 : /* Finish the ELF header. Fill in the fields not handled by
1623 : libelf from the old file. */
1624 56 : debugehdr = gelf_getehdr (debugelf, &debugehdr_mem);
1625 56 : if (debugehdr == NULL)
1626 0 : INTERNAL_ERROR (fname);
1627 :
1628 112 : memcpy (debugehdr->e_ident, ehdr->e_ident, EI_NIDENT);
1629 56 : debugehdr->e_type = ehdr->e_type;
1630 56 : debugehdr->e_machine = ehdr->e_machine;
1631 56 : debugehdr->e_version = ehdr->e_version;
1632 56 : debugehdr->e_entry = ehdr->e_entry;
1633 56 : debugehdr->e_flags = ehdr->e_flags;
1634 :
1635 56 : if (unlikely (gelf_update_ehdr (debugelf, debugehdr) == 0))
1636 : {
1637 0 : error (0, 0, gettext ("%s: error while updating ELF header: %s"),
1638 : debug_fname, elf_errmsg (-1));
1639 0 : result = 1;
1640 0 : goto fail_close;
1641 : }
1642 :
1643 : size_t shdrstrndx;
1644 56 : if (elf_getshdrstrndx (elf, &shdrstrndx) < 0)
1645 : {
1646 0 : error (0, 0, gettext ("%s: error while getting shdrstrndx: %s"),
1647 : fname, elf_errmsg (-1));
1648 0 : result = 1;
1649 0 : goto fail_close;
1650 : }
1651 :
1652 56 : if (update_shdrstrndx (debugelf, shdrstrndx) != 0)
1653 : {
1654 0 : error (0, 0, gettext ("%s: error updating shdrstrndx: %s"),
1655 : debug_fname, elf_errmsg (-1));
1656 0 : result = 1;
1657 0 : goto fail_close;
1658 : }
1659 : }
1660 :
1661 : /* Although we always create a new section header string table we
1662 : don't explicitly mark the existing one as unused. It can still
1663 : be used through a symbol table section we are keeping. If not it
1664 : will already be marked as unused. */
1665 :
1666 : /* We need a string table for the section headers. */
1667 92 : shst = dwelf_strtab_init (true);
1668 92 : if (shst == NULL)
1669 : {
1670 0 : cleanup_debug ();
1671 0 : error (EXIT_FAILURE, errno, gettext ("while preparing output for '%s'"),
1672 0 : output_fname ?: fname);
1673 : }
1674 :
1675 : /* Assign new section numbers. */
1676 92 : shdr_info[0].idx = 0;
1677 199611 : for (cnt = idx = 1; cnt < shnum; ++cnt)
1678 199519 : if (shdr_info[cnt].idx > 0)
1679 : {
1680 67478 : shdr_info[cnt].idx = idx++;
1681 :
1682 : /* Create a new section. */
1683 67478 : shdr_info[cnt].newscn = elf_newscn (newelf);
1684 67478 : if (shdr_info[cnt].newscn == NULL)
1685 : {
1686 0 : cleanup_debug ();
1687 0 : error (EXIT_FAILURE, 0,
1688 0 : gettext ("while generating output file: %s"),
1689 : elf_errmsg (-1));
1690 : }
1691 :
1692 67478 : elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
1693 :
1694 : /* Add this name to the section header string table. */
1695 67478 : shdr_info[cnt].se = dwelf_strtab_add (shst, shdr_info[cnt].name);
1696 : }
1697 :
1698 : /* Test whether we are doing anything at all. Either all removable
1699 : sections are already gone. Or the only section we would remove is
1700 : the .shstrtab section which we would add again. */
1701 276 : bool removing_sections = !(cnt == idx
1702 92 : || (cnt == idx + 1
1703 4 : && shdr_info[shstrndx].idx == 0));
1704 92 : if (output_fname == NULL && !removing_sections)
1705 : goto fail_close;
1706 :
1707 : /* Create the reference to the file with the debug info (if any). */
1708 90 : if (debug_fname != NULL && !remove_shdrs && removing_sections)
1709 : {
1710 : /* Add the section header string table section name. */
1711 54 : shdr_info[cnt].se = dwelf_strtab_add_len (shst, ".gnu_debuglink", 15);
1712 54 : shdr_info[cnt].idx = idx++;
1713 :
1714 : /* Create the section header. */
1715 54 : shdr_info[cnt].shdr.sh_type = SHT_PROGBITS;
1716 54 : shdr_info[cnt].shdr.sh_flags = 0;
1717 54 : shdr_info[cnt].shdr.sh_addr = 0;
1718 54 : shdr_info[cnt].shdr.sh_link = SHN_UNDEF;
1719 54 : shdr_info[cnt].shdr.sh_info = SHN_UNDEF;
1720 54 : shdr_info[cnt].shdr.sh_entsize = 0;
1721 54 : shdr_info[cnt].shdr.sh_addralign = 4;
1722 : /* We set the offset to zero here. Before we write the ELF file the
1723 : field must have the correct value. This is done in the final
1724 : loop over all section. Then we have all the information needed. */
1725 54 : shdr_info[cnt].shdr.sh_offset = 0;
1726 :
1727 : /* Create the section. */
1728 54 : shdr_info[cnt].newscn = elf_newscn (newelf);
1729 54 : if (shdr_info[cnt].newscn == NULL)
1730 : {
1731 0 : cleanup_debug ();
1732 0 : error (EXIT_FAILURE, 0,
1733 0 : gettext ("while create section header section: %s"),
1734 : elf_errmsg (-1));
1735 : }
1736 54 : elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
1737 :
1738 54 : shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
1739 54 : if (shdr_info[cnt].data == NULL)
1740 : {
1741 0 : cleanup_debug ();
1742 0 : error (EXIT_FAILURE, 0, gettext ("cannot allocate section data: %s"),
1743 : elf_errmsg (-1));
1744 : }
1745 :
1746 54 : char *debug_basename = basename (debug_fname_embed ?: debug_fname);
1747 54 : off_t crc_offset = strlen (debug_basename) + 1;
1748 : /* Align to 4 byte boundary */
1749 54 : crc_offset = ((crc_offset - 1) & ~3) + 4;
1750 :
1751 54 : shdr_info[cnt].data->d_align = 4;
1752 54 : shdr_info[cnt].shdr.sh_size = shdr_info[cnt].data->d_size
1753 54 : = crc_offset + 4;
1754 54 : debuglink_buf = xcalloc (1, shdr_info[cnt].data->d_size);
1755 54 : shdr_info[cnt].data->d_buf = debuglink_buf;
1756 :
1757 108 : strcpy (shdr_info[cnt].data->d_buf, debug_basename);
1758 :
1759 : /* Cache this Elf_Data describing the CRC32 word in the section.
1760 : We'll fill this in when we have written the debug file. */
1761 54 : debuglink_crc_data = *shdr_info[cnt].data;
1762 108 : debuglink_crc_data.d_buf = ((char *) debuglink_crc_data.d_buf
1763 54 : + crc_offset);
1764 54 : debuglink_crc_data.d_size = 4;
1765 :
1766 : /* One more section done. */
1767 54 : ++cnt;
1768 : }
1769 :
1770 : /* Index of the section header table in the shdr_info array. */
1771 90 : shdridx = cnt;
1772 :
1773 : /* Add the section header string table section name. */
1774 90 : shdr_info[cnt].se = dwelf_strtab_add_len (shst, ".shstrtab", 10);
1775 90 : shdr_info[cnt].idx = idx;
1776 :
1777 : /* Create the section header. */
1778 90 : shdr_info[cnt].shdr.sh_type = SHT_STRTAB;
1779 90 : shdr_info[cnt].shdr.sh_flags = 0;
1780 90 : shdr_info[cnt].shdr.sh_addr = 0;
1781 90 : shdr_info[cnt].shdr.sh_link = SHN_UNDEF;
1782 90 : shdr_info[cnt].shdr.sh_info = SHN_UNDEF;
1783 90 : shdr_info[cnt].shdr.sh_entsize = 0;
1784 : /* We set the offset to zero here. Before we write the ELF file the
1785 : field must have the correct value. This is done in the final
1786 : loop over all section. Then we have all the information needed. */
1787 90 : shdr_info[cnt].shdr.sh_offset = 0;
1788 90 : shdr_info[cnt].shdr.sh_addralign = 1;
1789 :
1790 : /* Create the section. */
1791 90 : shdr_info[cnt].newscn = elf_newscn (newelf);
1792 90 : if (shdr_info[cnt].newscn == NULL)
1793 : {
1794 0 : cleanup_debug ();
1795 0 : error (EXIT_FAILURE, 0,
1796 0 : gettext ("while create section header section: %s"),
1797 : elf_errmsg (-1));
1798 : }
1799 90 : elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
1800 :
1801 : /* Finalize the string table and fill in the correct indices in the
1802 : section headers. */
1803 90 : shstrtab_data = elf_newdata (shdr_info[cnt].newscn);
1804 90 : if (shstrtab_data == NULL)
1805 : {
1806 0 : cleanup_debug ();
1807 0 : error (EXIT_FAILURE, 0,
1808 0 : gettext ("while create section header string table: %s"),
1809 : elf_errmsg (-1));
1810 : }
1811 90 : if (dwelf_strtab_finalize (shst, shstrtab_data) == NULL)
1812 : {
1813 0 : cleanup_debug ();
1814 : error (EXIT_FAILURE, 0,
1815 0 : gettext ("no memory to create section header string table"));
1816 : }
1817 :
1818 : /* We have to set the section size. */
1819 90 : shdr_info[cnt].shdr.sh_size = shstrtab_data->d_size;
1820 :
1821 : /* Update the section information. */
1822 90 : GElf_Off lastoffset = 0;
1823 134161 : for (cnt = 1; cnt <= shdridx; ++cnt)
1824 134072 : if (shdr_info[cnt].idx > 0)
1825 : {
1826 : Elf_Data *newdata;
1827 :
1828 2034 : scn = elf_getscn (newelf, shdr_info[cnt].idx);
1829 2034 : elf_assert (scn != NULL);
1830 :
1831 : /* Update the name. */
1832 2034 : shdr_info[cnt].shdr.sh_name = dwelf_strent_off (shdr_info[cnt].se);
1833 :
1834 : /* Update the section header from the input file. Some fields
1835 : might be section indeces which now have to be adjusted. Keep
1836 : the index to the "current" sh_link in case we need it to lookup
1837 : symbol table names. */
1838 2034 : size_t sh_link = shdr_info[cnt].shdr.sh_link;
1839 2034 : if (shdr_info[cnt].shdr.sh_link != 0)
1840 583 : shdr_info[cnt].shdr.sh_link =
1841 583 : shdr_info[shdr_info[cnt].shdr.sh_link].idx;
1842 :
1843 2034 : if (shdr_info[cnt].shdr.sh_type == SHT_GROUP)
1844 : {
1845 18 : elf_assert (shdr_info[cnt].data != NULL
1846 : && shdr_info[cnt].data->d_buf != NULL);
1847 :
1848 : Elf32_Word *grpref = (Elf32_Word *) shdr_info[cnt].data->d_buf;
1849 : /* First word is the section group flag.
1850 : Followed by section indexes, that need to be renumbered. */
1851 26 : for (size_t inner = 1;
1852 26 : inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
1853 17 : ++inner)
1854 17 : if (grpref[inner] < shnum)
1855 17 : grpref[inner] = shdr_info[grpref[inner]].idx;
1856 : else
1857 : goto illformed;
1858 : }
1859 :
1860 : /* Handle the SHT_REL, SHT_RELA, and SHF_INFO_LINK flag. */
1861 2034 : if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
1862 260 : shdr_info[cnt].shdr.sh_info =
1863 260 : shdr_info[shdr_info[cnt].shdr.sh_info].idx;
1864 :
1865 : /* Get the data from the old file if necessary. We already
1866 : created the data for the section header string table. */
1867 2034 : if (cnt < shnum)
1868 : {
1869 1892 : if (shdr_info[cnt].data == NULL)
1870 : {
1871 774 : shdr_info[cnt].data = elf_getdata (shdr_info[cnt].scn, NULL);
1872 774 : if (shdr_info[cnt].data == NULL)
1873 0 : INTERNAL_ERROR (fname);
1874 : }
1875 :
1876 : /* Set the data. This is done by copying from the old file. */
1877 1892 : newdata = elf_newdata (scn);
1878 1892 : if (newdata == NULL)
1879 0 : INTERNAL_ERROR (fname);
1880 :
1881 : /* Copy the structure. */
1882 1892 : *newdata = *shdr_info[cnt].data;
1883 :
1884 : /* We know the size. */
1885 1892 : shdr_info[cnt].shdr.sh_size = shdr_info[cnt].data->d_size;
1886 :
1887 : /* We have to adjust symbol tables. The st_shndx member might
1888 : have to be updated. */
1889 3784 : if (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM
1890 1892 : || shdr_info[cnt].shdr.sh_type == SHT_SYMTAB)
1891 : {
1892 106 : Elf_Data *versiondata = NULL;
1893 106 : Elf_Data *shndxdata = NULL;
1894 :
1895 106 : size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
1896 :
1897 106 : if (shdr_info[cnt].symtab_idx != 0)
1898 : {
1899 0 : elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX);
1900 : /* This section has extended section information.
1901 : We have to modify that information, too. */
1902 0 : shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
1903 : NULL);
1904 :
1905 0 : elf_assert (shndxdata != NULL
1906 : && shndxdata->d_buf != NULL
1907 : && ((shndxdata->d_size / sizeof (Elf32_Word))
1908 : >= shdr_info[cnt].data->d_size / elsize));
1909 : }
1910 :
1911 106 : if (shdr_info[cnt].version_idx != 0)
1912 : {
1913 50 : elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
1914 : /* This section has associated version
1915 : information. We have to modify that
1916 : information, too. */
1917 50 : versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
1918 : NULL);
1919 :
1920 150 : elf_assert (versiondata != NULL
1921 : && versiondata->d_buf != NULL
1922 : && ((versiondata->d_size / sizeof (GElf_Versym))
1923 : >= shdr_info[cnt].data->d_size / elsize));
1924 : }
1925 :
1926 : shdr_info[cnt].newsymidx
1927 106 : = (Elf32_Word *) xcalloc (shdr_info[cnt].data->d_size
1928 : / elsize, sizeof (Elf32_Word));
1929 :
1930 106 : bool last_was_local = true;
1931 : size_t destidx;
1932 : size_t inner;
1933 19194 : for (destidx = inner = 1;
1934 19088 : inner < shdr_info[cnt].data->d_size / elsize;
1935 18982 : ++inner)
1936 : {
1937 : Elf32_Word sec;
1938 : GElf_Sym sym_mem;
1939 : Elf32_Word xshndx;
1940 18983 : GElf_Sym *sym = gelf_getsymshndx (shdr_info[cnt].data,
1941 : shndxdata, inner,
1942 : &sym_mem, &xshndx);
1943 18983 : if (sym == NULL)
1944 0 : INTERNAL_ERROR (fname);
1945 :
1946 18983 : if (sym->st_shndx == SHN_UNDEF
1947 17384 : || (sym->st_shndx >= shnum
1948 263 : && sym->st_shndx != SHN_XINDEX))
1949 : {
1950 : /* This is no section index, leave it alone
1951 : unless it is moved. */
1952 1862 : if (destidx != inner
1953 1368 : && gelf_update_symshndx (shdr_info[cnt].data,
1954 : shndxdata,
1955 : destidx, sym,
1956 : xshndx) == 0)
1957 0 : INTERNAL_ERROR (fname);
1958 :
1959 1862 : shdr_info[cnt].newsymidx[inner] = destidx++;
1960 :
1961 1862 : if (last_was_local
1962 248 : && GELF_ST_BIND (sym->st_info) != STB_LOCAL)
1963 : {
1964 59 : last_was_local = false;
1965 59 : shdr_info[cnt].shdr.sh_info = destidx - 1;
1966 : }
1967 :
1968 1862 : continue;
1969 : }
1970 :
1971 : /* Get the full section index, if necessary from the
1972 : XINDEX table. */
1973 17121 : if (sym->st_shndx == SHN_XINDEX)
1974 0 : elf_assert (shndxdata != NULL
1975 : && shndxdata->d_buf != NULL);
1976 17121 : size_t sidx = (sym->st_shndx != SHN_XINDEX
1977 17121 : ? sym->st_shndx : xshndx);
1978 17121 : sec = shdr_info[sidx].idx;
1979 :
1980 17121 : if (sec != 0)
1981 : {
1982 : GElf_Section nshndx;
1983 : Elf32_Word nxshndx;
1984 :
1985 2791 : if (sec < SHN_LORESERVE)
1986 : {
1987 2790 : nshndx = sec;
1988 2790 : nxshndx = 0;
1989 : }
1990 : else
1991 : {
1992 : nshndx = SHN_XINDEX;
1993 : nxshndx = sec;
1994 : }
1995 :
1996 2791 : elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
1997 :
1998 2790 : if ((inner != destidx || nshndx != sym->st_shndx
1999 1413 : || (shndxdata != NULL && nxshndx != xshndx))
2000 2754 : && (sym->st_shndx = nshndx,
2001 1377 : gelf_update_symshndx (shdr_info[cnt].data,
2002 : shndxdata,
2003 : destidx, sym,
2004 : nxshndx) == 0))
2005 0 : INTERNAL_ERROR (fname);
2006 :
2007 2790 : shdr_info[cnt].newsymidx[inner] = destidx++;
2008 :
2009 2790 : if (last_was_local
2010 2374 : && GELF_ST_BIND (sym->st_info) != STB_LOCAL)
2011 : {
2012 46 : last_was_local = false;
2013 46 : shdr_info[cnt].shdr.sh_info = destidx - 1;
2014 : }
2015 : }
2016 14330 : else if ((shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) != 0
2017 48 : && GELF_ST_TYPE (sym->st_info) != STT_SECTION
2018 0 : && shdr_info[sidx].shdr.sh_type != SHT_GROUP)
2019 : {
2020 : /* Removing a real symbol from an allocated
2021 : symbol table is hard and probably a
2022 : mistake. Really removing it means
2023 : rewriting the dynamic segment and hash
2024 : sections. Just warn and set the symbol
2025 : section to UNDEF. */
2026 0 : error (0, 0,
2027 0 : gettext ("Cannot remove symbol [%zd] from allocated symbol table [%zd]"), inner, cnt);
2028 0 : sym->st_shndx = SHN_UNDEF;
2029 0 : if (gelf_update_sym (shdr_info[cnt].data, destidx,
2030 : sym) == 0)
2031 0 : INTERNAL_ERROR (fname);
2032 0 : shdr_info[cnt].newsymidx[inner] = destidx++;
2033 : }
2034 14330 : else if (debug_fname != NULL
2035 14152 : && shdr_info[cnt].debug_data == NULL)
2036 : /* The symbol points to a section that is discarded
2037 : but isn't preserved in the debug file. Check that
2038 : this is a section or group signature symbol
2039 : for a section which has been removed. Or a special
2040 : data marker symbol to a debug section. */
2041 : {
2042 50 : elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION
2043 : || ((shdr_info[sidx].shdr.sh_type
2044 : == SHT_GROUP)
2045 : && (shdr_info[sidx].shdr.sh_info
2046 : == inner))
2047 : || ebl_data_marker_symbol (ebl, sym,
2048 : elf_strptr (elf, sh_link,
2049 : sym->st_name)));
2050 : }
2051 : }
2052 :
2053 105 : if (destidx != inner)
2054 : {
2055 : /* The size of the symbol table changed. */
2056 61 : shdr_info[cnt].shdr.sh_size = newdata->d_size
2057 61 : = destidx * elsize;
2058 61 : any_symtab_changes = true;
2059 : }
2060 : else
2061 : {
2062 : /* The symbol table didn't really change. */
2063 44 : free (shdr_info[cnt].newsymidx);
2064 44 : shdr_info[cnt].newsymidx = NULL;
2065 : }
2066 : }
2067 : }
2068 :
2069 : /* If we have to, compute the offset of the section.
2070 : If allocate and unallocated sections are mixed, we only update
2071 : the allocated ones now. The unallocated ones come second. */
2072 2033 : if (! mixed_allocated_unallocated
2073 886 : || (shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) != 0)
2074 : {
2075 1652 : if (shdr_info[cnt].shdr.sh_offset == 0)
2076 : shdr_info[cnt].shdr.sh_offset
2077 1014 : = ((lastoffset + shdr_info[cnt].shdr.sh_addralign - 1)
2078 507 : & ~((GElf_Off) (shdr_info[cnt].shdr.sh_addralign - 1)));
2079 :
2080 : /* Set the section header in the new file. */
2081 1652 : if (unlikely (gelf_update_shdr (scn, &shdr_info[cnt].shdr) == 0))
2082 : /* There cannot be any overflows. */
2083 0 : INTERNAL_ERROR (fname);
2084 :
2085 : /* Remember the last section written so far. */
2086 3304 : GElf_Off filesz = (shdr_info[cnt].shdr.sh_type != SHT_NOBITS
2087 1652 : ? shdr_info[cnt].shdr.sh_size : 0);
2088 1652 : if (lastoffset < shdr_info[cnt].shdr.sh_offset + filesz)
2089 1535 : lastoffset = shdr_info[cnt].shdr.sh_offset + filesz;
2090 : }
2091 : }
2092 :
2093 : /* We might have to update the unallocated sections after we done the
2094 : allocated ones. lastoffset is set to right after the last allocated
2095 : section. */
2096 89 : if (mixed_allocated_unallocated)
2097 132520 : for (cnt = 1; cnt <= shdridx; ++cnt)
2098 132477 : if (shdr_info[cnt].idx > 0)
2099 : {
2100 861 : scn = elf_getscn (newelf, shdr_info[cnt].idx);
2101 861 : if ((shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) == 0)
2102 : {
2103 377 : if (shdr_info[cnt].shdr.sh_offset == 0)
2104 : shdr_info[cnt].shdr.sh_offset
2105 754 : = ((lastoffset + shdr_info[cnt].shdr.sh_addralign - 1)
2106 377 : & ~((GElf_Off) (shdr_info[cnt].shdr.sh_addralign - 1)));
2107 :
2108 : /* Set the section header in the new file. */
2109 377 : if (unlikely (gelf_update_shdr (scn, &shdr_info[cnt].shdr) == 0))
2110 : /* There cannot be any overflows. */
2111 0 : INTERNAL_ERROR (fname);
2112 :
2113 : /* Remember the last section written so far. */
2114 754 : GElf_Off filesz = (shdr_info[cnt].shdr.sh_type != SHT_NOBITS
2115 377 : ? shdr_info[cnt].shdr.sh_size : 0);
2116 377 : if (lastoffset < shdr_info[cnt].shdr.sh_offset + filesz)
2117 374 : lastoffset = shdr_info[cnt].shdr.sh_offset + filesz;
2118 : }
2119 : }
2120 :
2121 : /* Adjust symbol references if symbol tables changed. */
2122 89 : if (any_symtab_changes)
2123 : /* Find all relocation sections which use this symbol table. */
2124 1988 : for (cnt = 1; cnt <= shdridx; ++cnt)
2125 : {
2126 : /* Update section headers when the data size has changed.
2127 : We also update the SHT_NOBITS section in the debug
2128 : file so that the section headers match in sh_size. */
2129 12 : inline void update_section_size (const Elf_Data *newdata)
2130 : {
2131 : GElf_Shdr shdr_mem;
2132 12 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2133 12 : shdr->sh_size = newdata->d_size;
2134 12 : (void) gelf_update_shdr (scn, shdr);
2135 12 : if (debugelf != NULL)
2136 : {
2137 : /* libelf will use d_size to set sh_size. */
2138 2 : Elf_Data *debugdata = elf_getdata (elf_getscn (debugelf,
2139 : cnt), NULL);
2140 2 : if (debugdata == NULL)
2141 0 : INTERNAL_ERROR (fname);
2142 2 : debugdata->d_size = newdata->d_size;
2143 : }
2144 12 : }
2145 :
2146 1929 : if (shdr_info[cnt].idx == 0 && debug_fname == NULL)
2147 : /* Ignore sections which are discarded. When we are saving a
2148 : relocation section in a separate debug file, we must fix up
2149 : the symbol table references. */
2150 211 : continue;
2151 :
2152 1718 : const Elf32_Word symtabidx = shdr_info[cnt].old_sh_link;
2153 1718 : elf_assert (symtabidx < shnum + 2);
2154 1718 : const Elf32_Word *const newsymidx = shdr_info[symtabidx].newsymidx;
2155 1718 : switch (shdr_info[cnt].shdr.sh_type)
2156 : {
2157 : inline bool no_symtab_updates (void)
2158 : {
2159 : /* If the symbol table hasn't changed, do not do anything. */
2160 398 : if (shdr_info[symtabidx].newsymidx == NULL)
2161 : return true;
2162 :
2163 : /* If the symbol table is not discarded, but additionally
2164 : duplicated in the separate debug file and this section
2165 : is discarded, don't adjust anything. */
2166 : return (shdr_info[cnt].idx == 0
2167 355 : && shdr_info[symtabidx].debug_data != NULL);
2168 : }
2169 :
2170 : case SHT_REL:
2171 : case SHT_RELA:
2172 333 : if (no_symtab_updates ())
2173 : break;
2174 :
2175 356 : Elf_Data *d = elf_getdata (shdr_info[cnt].idx == 0
2176 : ? elf_getscn (debugelf, cnt)
2177 178 : : elf_getscn (newelf,
2178 : shdr_info[cnt].idx),
2179 : NULL);
2180 178 : elf_assert (d != NULL && d->d_buf != NULL
2181 : && shdr_info[cnt].shdr.sh_entsize != 0);
2182 178 : size_t nrels = (shdr_info[cnt].shdr.sh_size
2183 : / shdr_info[cnt].shdr.sh_entsize);
2184 :
2185 178 : size_t symsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2186 356 : const Elf32_Word symidxn = (shdr_info[symtabidx].data->d_size
2187 178 : / symsize);
2188 178 : if (shdr_info[cnt].shdr.sh_type == SHT_REL)
2189 2411 : for (size_t relidx = 0; relidx < nrels; ++relidx)
2190 : {
2191 : GElf_Rel rel_mem;
2192 2411 : if (gelf_getrel (d, relidx, &rel_mem) == NULL)
2193 0 : INTERNAL_ERROR (fname);
2194 :
2195 2411 : size_t symidx = GELF_R_SYM (rel_mem.r_info);
2196 2411 : elf_assert (symidx < symidxn);
2197 2411 : if (newsymidx[symidx] != symidx)
2198 : {
2199 : rel_mem.r_info
2200 2206 : = GELF_R_INFO (newsymidx[symidx],
2201 : GELF_R_TYPE (rel_mem.r_info));
2202 :
2203 2206 : if (gelf_update_rel (d, relidx, &rel_mem) == 0)
2204 0 : INTERNAL_ERROR (fname);
2205 : }
2206 : }
2207 : else
2208 12566 : for (size_t relidx = 0; relidx < nrels; ++relidx)
2209 : {
2210 : GElf_Rela rel_mem;
2211 12566 : if (gelf_getrela (d, relidx, &rel_mem) == NULL)
2212 0 : INTERNAL_ERROR (fname);
2213 :
2214 12566 : size_t symidx = GELF_R_SYM (rel_mem.r_info);
2215 12566 : elf_assert (symidx < symidxn);
2216 12566 : if (newsymidx[symidx] != symidx)
2217 : {
2218 : rel_mem.r_info
2219 4091 : = GELF_R_INFO (newsymidx[symidx],
2220 : GELF_R_TYPE (rel_mem.r_info));
2221 :
2222 4091 : if (gelf_update_rela (d, relidx, &rel_mem) == 0)
2223 0 : INTERNAL_ERROR (fname);
2224 : }
2225 : }
2226 : break;
2227 :
2228 : case SHT_HASH:
2229 6 : if (no_symtab_updates ())
2230 : break;
2231 :
2232 : /* We have to recompute the hash table. */
2233 :
2234 6 : elf_assert (shdr_info[cnt].idx > 0);
2235 :
2236 : /* The hash section in the new file. */
2237 6 : scn = elf_getscn (newelf, shdr_info[cnt].idx);
2238 :
2239 : /* The symbol table data. */
2240 6 : Elf_Data *symd = elf_getdata (elf_getscn (newelf,
2241 6 : shdr_info[symtabidx].idx),
2242 : NULL);
2243 6 : elf_assert (symd != NULL && symd->d_buf != NULL);
2244 :
2245 : /* The hash table data. */
2246 6 : Elf_Data *hashd = elf_getdata (scn, NULL);
2247 6 : elf_assert (hashd != NULL && hashd->d_buf != NULL);
2248 :
2249 6 : if (shdr_info[cnt].shdr.sh_entsize == sizeof (Elf32_Word))
2250 : {
2251 : /* Sane arches first. */
2252 6 : elf_assert (hashd->d_size >= 2 * sizeof (Elf32_Word));
2253 6 : Elf32_Word *bucket = (Elf32_Word *) hashd->d_buf;
2254 :
2255 6 : size_t strshndx = shdr_info[symtabidx].old_sh_link;
2256 6 : size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2257 :
2258 6 : Elf32_Word nchain = bucket[1];
2259 6 : Elf32_Word nbucket = bucket[0];
2260 12 : uint64_t used_buf = ((2ULL + nchain + nbucket)
2261 6 : * sizeof (Elf32_Word));
2262 6 : elf_assert (used_buf <= hashd->d_size);
2263 :
2264 : /* Adjust the nchain value. The symbol table size
2265 : changed. We keep the same size for the bucket array. */
2266 6 : bucket[1] = symd->d_size / elsize;
2267 6 : bucket += 2;
2268 6 : Elf32_Word *chain = bucket + nbucket;
2269 :
2270 : /* New size of the section. */
2271 12 : size_t n_size = ((2 + symd->d_size / elsize + nbucket)
2272 6 : * sizeof (Elf32_Word));
2273 6 : elf_assert (n_size <= hashd->d_size);
2274 6 : hashd->d_size = n_size;
2275 6 : update_section_size (hashd);
2276 :
2277 : /* Clear the arrays. */
2278 12 : memset (bucket, '\0',
2279 6 : (symd->d_size / elsize + nbucket)
2280 : * sizeof (Elf32_Word));
2281 :
2282 84 : for (size_t inner = shdr_info[symtabidx].shdr.sh_info;
2283 72 : inner < symd->d_size / elsize; ++inner)
2284 : {
2285 : GElf_Sym sym_mem;
2286 72 : GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
2287 72 : elf_assert (sym != NULL);
2288 :
2289 72 : const char *name = elf_strptr (elf, strshndx,
2290 72 : sym->st_name);
2291 72 : elf_assert (name != NULL && nbucket != 0);
2292 72 : size_t hidx = elf_hash (name) % nbucket;
2293 :
2294 72 : if (bucket[hidx] == 0)
2295 60 : bucket[hidx] = inner;
2296 : else
2297 : {
2298 12 : hidx = bucket[hidx];
2299 :
2300 24 : while (chain[hidx] != 0 && chain[hidx] < nchain)
2301 0 : hidx = chain[hidx];
2302 :
2303 12 : chain[hidx] = inner;
2304 : }
2305 : }
2306 : }
2307 : else
2308 0 : {
2309 : /* Alpha and S390 64-bit use 64-bit SHT_HASH entries. */
2310 0 : elf_assert (shdr_info[cnt].shdr.sh_entsize
2311 : == sizeof (Elf64_Xword));
2312 :
2313 0 : Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
2314 :
2315 0 : size_t strshndx = shdr_info[symtabidx].old_sh_link;
2316 0 : size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2317 :
2318 0 : elf_assert (symd->d_size >= 2 * sizeof (Elf64_Xword));
2319 0 : Elf64_Xword nbucket = bucket[0];
2320 0 : Elf64_Xword nchain = bucket[1];
2321 0 : uint64_t maxwords = hashd->d_size / sizeof (Elf64_Xword);
2322 0 : elf_assert (maxwords >= 2
2323 : && maxwords - 2 >= nbucket
2324 : && maxwords - 2 - nbucket >= nchain);
2325 :
2326 : /* Adjust the nchain value. The symbol table size
2327 : changed. We keep the same size for the bucket array. */
2328 0 : bucket[1] = symd->d_size / elsize;
2329 0 : bucket += 2;
2330 0 : Elf64_Xword *chain = bucket + nbucket;
2331 :
2332 : /* New size of the section. */
2333 0 : size_t n_size = ((2 + symd->d_size / elsize + nbucket)
2334 0 : * sizeof (Elf64_Xword));
2335 0 : elf_assert (n_size <= hashd->d_size);
2336 0 : hashd->d_size = n_size;
2337 0 : update_section_size (hashd);
2338 :
2339 : /* Clear the arrays. */
2340 0 : memset (bucket, '\0',
2341 0 : (symd->d_size / elsize + nbucket)
2342 : * sizeof (Elf64_Xword));
2343 :
2344 0 : for (size_t inner = shdr_info[symtabidx].shdr.sh_info;
2345 0 : inner < symd->d_size / elsize; ++inner)
2346 : {
2347 : GElf_Sym sym_mem;
2348 0 : GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
2349 0 : elf_assert (sym != NULL);
2350 :
2351 0 : const char *name = elf_strptr (elf, strshndx,
2352 0 : sym->st_name);
2353 0 : elf_assert (name != NULL && nbucket != 0);
2354 0 : size_t hidx = elf_hash (name) % nbucket;
2355 :
2356 0 : if (bucket[hidx] == 0)
2357 0 : bucket[hidx] = inner;
2358 : else
2359 : {
2360 : hidx = bucket[hidx];
2361 :
2362 0 : while (chain[hidx] != 0 && chain[hidx] < nchain)
2363 : hidx = chain[hidx];
2364 :
2365 0 : chain[hidx] = inner;
2366 : }
2367 : }
2368 : }
2369 : break;
2370 :
2371 : case SHT_GNU_versym:
2372 : /* If the symbol table changed we have to adjust the entries. */
2373 6 : if (no_symtab_updates ())
2374 : break;
2375 :
2376 6 : elf_assert (shdr_info[cnt].idx > 0);
2377 :
2378 : /* The symbol version section in the new file. */
2379 6 : scn = elf_getscn (newelf, shdr_info[cnt].idx);
2380 :
2381 : /* The symbol table data. */
2382 6 : symd = elf_getdata (elf_getscn (newelf, shdr_info[symtabidx].idx),
2383 : NULL);
2384 6 : elf_assert (symd != NULL && symd->d_buf != NULL);
2385 6 : size_t symz = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2386 6 : const Elf32_Word syms = (shdr_info[symtabidx].data->d_size / symz);
2387 :
2388 : /* The version symbol data. */
2389 6 : Elf_Data *verd = elf_getdata (scn, NULL);
2390 6 : elf_assert (verd != NULL && verd->d_buf != NULL);
2391 :
2392 : /* The symbol version array. */
2393 6 : GElf_Half *verstab = (GElf_Half *) verd->d_buf;
2394 :
2395 : /* Walk through the list and */
2396 6 : size_t elsize = gelf_fsize (elf, verd->d_type, 1, EV_CURRENT);
2397 6 : Elf32_Word vers = verd->d_size / elsize;
2398 258 : for (size_t inner = 1; inner < vers && inner < syms; ++inner)
2399 252 : if (newsymidx[inner] != 0 && newsymidx[inner] < vers)
2400 : /* Overwriting the same array works since the
2401 : reordering can only move entries to lower indices
2402 : in the array. */
2403 204 : verstab[newsymidx[inner]] = verstab[inner];
2404 :
2405 : /* New size of the section. */
2406 6 : verd->d_size = gelf_fsize (newelf, verd->d_type,
2407 6 : symd->d_size
2408 6 : / gelf_fsize (elf, symd->d_type, 1,
2409 : EV_CURRENT),
2410 : EV_CURRENT);
2411 6 : update_section_size (verd);
2412 6 : break;
2413 :
2414 : case SHT_GROUP:
2415 10 : if (no_symtab_updates ())
2416 : break;
2417 :
2418 : /* Yes, the symbol table changed.
2419 : Update the section header of the section group. */
2420 9 : scn = elf_getscn (newelf, shdr_info[cnt].idx);
2421 : GElf_Shdr shdr_mem;
2422 9 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2423 9 : elf_assert (shdr != NULL);
2424 :
2425 9 : size_t symsz = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2426 18 : const Elf32_Word symn = (shdr_info[symtabidx].data->d_size
2427 9 : / symsz);
2428 9 : elf_assert (shdr->sh_info < symn);
2429 9 : shdr->sh_info = newsymidx[shdr->sh_info];
2430 :
2431 9 : (void) gelf_update_shdr (scn, shdr);
2432 9 : break;
2433 : }
2434 : }
2435 :
2436 : /* Remove any relocations between debug sections in ET_REL
2437 : for the debug file when requested. These relocations are always
2438 : zero based between the unallocated sections. */
2439 89 : if (debug_fname != NULL && removing_sections
2440 53 : && reloc_debug && ehdr->e_type == ET_REL)
2441 13 : remove_debug_relocations (ebl, debugelf, ehdr, fname, shstrndx);
2442 :
2443 : /* Now that we have done all adjustments to the data,
2444 : we can actually write out the debug file. */
2445 89 : if (debug_fname != NULL && removing_sections)
2446 : {
2447 : /* Finally write the file. */
2448 53 : if (unlikely (elf_update (debugelf, ELF_C_WRITE) == -1))
2449 : {
2450 0 : error (0, 0, gettext ("while writing '%s': %s"),
2451 : tmp_debug_fname, elf_errmsg (-1));
2452 0 : result = 1;
2453 0 : goto fail_close;
2454 : }
2455 :
2456 : /* Create the real output file. First rename, then change the
2457 : mode. */
2458 53 : if (rename (tmp_debug_fname, debug_fname) != 0
2459 53 : || fchmod (debug_fd, mode) != 0)
2460 : {
2461 0 : error (0, errno, gettext ("while creating '%s'"), debug_fname);
2462 0 : result = 1;
2463 0 : goto fail_close;
2464 : }
2465 :
2466 : /* The temporary file does not exist anymore. */
2467 53 : free (tmp_debug_fname);
2468 53 : tmp_debug_fname = NULL;
2469 :
2470 53 : if (!remove_shdrs)
2471 : {
2472 : uint32_t debug_crc;
2473 53 : Elf_Data debug_crc_data =
2474 : {
2475 : .d_type = ELF_T_WORD,
2476 : .d_buf = &debug_crc,
2477 : .d_size = sizeof (debug_crc),
2478 : .d_version = EV_CURRENT
2479 : };
2480 :
2481 : /* Compute the checksum which we will add to the executable. */
2482 53 : if (crc32_file (debug_fd, &debug_crc) != 0)
2483 : {
2484 0 : error (0, errno, gettext ("\
2485 : while computing checksum for debug information"));
2486 0 : unlink (debug_fname);
2487 0 : result = 1;
2488 0 : goto fail_close;
2489 : }
2490 :
2491 : /* Store it in the debuglink section data. */
2492 53 : if (unlikely (gelf_xlatetof (newelf, &debuglink_crc_data,
2493 : &debug_crc_data, ehdr->e_ident[EI_DATA])
2494 : != &debuglink_crc_data))
2495 0 : INTERNAL_ERROR (fname);
2496 : }
2497 : }
2498 :
2499 89 : lastsec_offset = shdr_info[shdridx].shdr.sh_offset;
2500 89 : lastsec_size = shdr_info[shdridx].shdr.sh_size;
2501 :
2502 103 : done:
2503 : /* Finally finish the ELF header. Fill in the fields not handled by
2504 : libelf from the old file. */
2505 103 : newehdr = gelf_getehdr (newelf, &newehdr_mem);
2506 103 : if (newehdr == NULL)
2507 0 : INTERNAL_ERROR (fname);
2508 :
2509 206 : memcpy (newehdr->e_ident, ehdr->e_ident, EI_NIDENT);
2510 103 : newehdr->e_type = ehdr->e_type;
2511 103 : newehdr->e_machine = ehdr->e_machine;
2512 103 : newehdr->e_version = ehdr->e_version;
2513 103 : newehdr->e_entry = ehdr->e_entry;
2514 103 : newehdr->e_flags = ehdr->e_flags;
2515 103 : newehdr->e_phoff = ehdr->e_phoff;
2516 :
2517 : /* We need to position the section header table. */
2518 103 : const size_t offsize = gelf_fsize (elf, ELF_T_OFF, 1, EV_CURRENT);
2519 206 : newehdr->e_shoff = ((lastsec_offset + lastsec_size + offsize - 1)
2520 103 : & ~((GElf_Off) (offsize - 1)));
2521 103 : newehdr->e_shentsize = gelf_fsize (elf, ELF_T_SHDR, 1, EV_CURRENT);
2522 :
2523 103 : if (gelf_update_ehdr (newelf, newehdr) == 0)
2524 : {
2525 0 : error (0, 0, gettext ("%s: error while creating ELF header: %s"),
2526 0 : output_fname ?: fname, elf_errmsg (-1));
2527 0 : cleanup_debug ();
2528 0 : return 1;
2529 : }
2530 :
2531 : /* The new section header string table index. */
2532 103 : if (update_shdrstrndx (newelf, idx) != 0)
2533 : {
2534 0 : error (0, 0, gettext ("%s: error updating shdrstrndx: %s"),
2535 0 : output_fname ?: fname, elf_errmsg (-1));
2536 0 : cleanup_debug ();
2537 0 : return 1;
2538 : }
2539 :
2540 : /* We have everything from the old file. */
2541 103 : if (elf_cntl (elf, ELF_C_FDDONE) != 0)
2542 : {
2543 0 : error (0, 0, gettext ("%s: error while reading the file: %s"),
2544 : fname, elf_errmsg (-1));
2545 0 : cleanup_debug ();
2546 0 : return 1;
2547 : }
2548 :
2549 : /* The ELF library better follows our layout when this is not a
2550 : relocatable object file. */
2551 103 : elf_flagelf (newelf, ELF_C_SET,
2552 103 : (phnum > 0 ? ELF_F_LAYOUT : 0)
2553 103 : | (permissive ? ELF_F_PERMISSIVE : 0));
2554 :
2555 : /* Finally write the file. */
2556 103 : if (elf_update (newelf, ELF_C_WRITE) == -1)
2557 : {
2558 0 : error (0, 0, gettext ("while writing '%s': %s"),
2559 0 : output_fname ?: fname, elf_errmsg (-1));
2560 0 : result = 1;
2561 : }
2562 :
2563 103 : if (remove_shdrs)
2564 : {
2565 : /* libelf can't cope without the section headers being properly intact.
2566 : So we just let it write them normally, and then we nuke them later. */
2567 :
2568 0 : if (newehdr->e_ident[EI_CLASS] == ELFCLASS32)
2569 : {
2570 : assert (offsetof (Elf32_Ehdr, e_shentsize) + sizeof (Elf32_Half)
2571 : == offsetof (Elf32_Ehdr, e_shnum));
2572 : assert (offsetof (Elf32_Ehdr, e_shnum) + sizeof (Elf32_Half)
2573 : == offsetof (Elf32_Ehdr, e_shstrndx));
2574 0 : const Elf32_Off zero_off = 0;
2575 0 : const Elf32_Half zero[3] = { 0, 0, SHN_UNDEF };
2576 0 : if (pwrite_retry (fd, &zero_off, sizeof zero_off,
2577 : offsetof (Elf32_Ehdr, e_shoff)) != sizeof zero_off
2578 0 : || (pwrite_retry (fd, zero, sizeof zero,
2579 : offsetof (Elf32_Ehdr, e_shentsize))
2580 : != sizeof zero)
2581 0 : || ftruncate (fd, lastsec_offset) < 0)
2582 : {
2583 0 : error (0, errno, gettext ("while writing '%s'"),
2584 0 : output_fname ?: fname);
2585 0 : result = 1;
2586 : }
2587 : }
2588 : else
2589 : {
2590 : assert (offsetof (Elf64_Ehdr, e_shentsize) + sizeof (Elf64_Half)
2591 : == offsetof (Elf64_Ehdr, e_shnum));
2592 : assert (offsetof (Elf64_Ehdr, e_shnum) + sizeof (Elf64_Half)
2593 : == offsetof (Elf64_Ehdr, e_shstrndx));
2594 0 : const Elf64_Off zero_off = 0;
2595 0 : const Elf64_Half zero[3] = { 0, 0, SHN_UNDEF };
2596 0 : if (pwrite_retry (fd, &zero_off, sizeof zero_off,
2597 : offsetof (Elf64_Ehdr, e_shoff)) != sizeof zero_off
2598 0 : || (pwrite_retry (fd, zero, sizeof zero,
2599 : offsetof (Elf64_Ehdr, e_shentsize))
2600 : != sizeof zero)
2601 0 : || ftruncate (fd, lastsec_offset) < 0)
2602 : {
2603 0 : error (0, errno, gettext ("while writing '%s'"),
2604 0 : output_fname ?: fname);
2605 0 : result = 1;
2606 : }
2607 : }
2608 : }
2609 :
2610 211 : fail_close:
2611 106 : if (shdr_info != NULL)
2612 : {
2613 : /* For some sections we might have created an table to map symbol
2614 : table indices. Or we might kept (original) data around to put
2615 : into the .debug file. */
2616 199703 : for (cnt = 1; cnt <= shdridx; ++cnt)
2617 : {
2618 199611 : free (shdr_info[cnt].newsymidx);
2619 199611 : if (shdr_info[cnt].debug_data != NULL)
2620 80 : free (shdr_info[cnt].debug_data->d_buf);
2621 : }
2622 :
2623 : /* Free data we allocated for the .gnu_debuglink section. */
2624 92 : free (debuglink_buf);
2625 :
2626 : /* Free the memory. */
2627 92 : if ((shnum + 2) * sizeof (struct shdr_info) > MAX_STACK_ALLOC)
2628 3 : free (shdr_info);
2629 : }
2630 :
2631 : /* Free other resources. */
2632 106 : if (shstrtab_data != NULL)
2633 90 : free (shstrtab_data->d_buf);
2634 106 : if (shst != NULL)
2635 92 : dwelf_strtab_free (shst);
2636 :
2637 : /* That was it. Close the descriptors. */
2638 106 : if (elf_end (newelf) != 0)
2639 : {
2640 0 : error (0, 0, gettext ("error while finishing '%s': %s"),
2641 0 : output_fname ?: fname, elf_errmsg (-1));
2642 0 : result = 1;
2643 : }
2644 :
2645 162 : if (debugelf != NULL && elf_end (debugelf) != 0)
2646 : {
2647 0 : error (0, 0, gettext ("error while finishing '%s': %s"), debug_fname,
2648 : elf_errmsg (-1));
2649 0 : result = 1;
2650 : }
2651 :
2652 212 : fail:
2653 : /* Close the EBL backend. */
2654 106 : if (ebl != NULL)
2655 53 : ebl_closebackend (ebl);
2656 :
2657 106 : cleanup_debug ();
2658 :
2659 : /* If requested, preserve the timestamp. */
2660 106 : if (tvp != NULL)
2661 : {
2662 0 : if (futimens (fd, tvp) != 0)
2663 : {
2664 0 : error (0, errno, gettext ("\
2665 : cannot set access and modification date of '%s'"),
2666 0 : output_fname ?: fname);
2667 0 : result = 1;
2668 : }
2669 : }
2670 :
2671 : /* Close the file descriptor if we created a new file. */
2672 106 : if (output_fname != NULL)
2673 : {
2674 76 : close (fd);
2675 76 : if (result != 0)
2676 0 : unlink (output_fname);
2677 : }
2678 :
2679 : return result;
2680 : }
2681 :
2682 : static void
2683 106 : cleanup_debug (void)
2684 : {
2685 106 : if (debug_fd >= 0)
2686 : {
2687 56 : if (tmp_debug_fname != NULL)
2688 : {
2689 3 : unlink (tmp_debug_fname);
2690 3 : free (tmp_debug_fname);
2691 3 : tmp_debug_fname = NULL;
2692 : }
2693 56 : close (debug_fd);
2694 56 : debug_fd = -1;
2695 : }
2696 106 : }
2697 :
2698 : static int
2699 : handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
2700 : struct timespec tvp[2])
2701 : {
2702 : size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
2703 : size_t fname_len = strlen (fname) + 1;
2704 : char new_prefix[prefix_len + 1 + fname_len];
2705 : char *cp = new_prefix;
2706 :
2707 : /* Create the full name of the file. */
2708 : if (prefix != NULL)
2709 : {
2710 : cp = mempcpy (cp, prefix, prefix_len);
2711 : *cp++ = ':';
2712 : }
2713 : memcpy (cp, fname, fname_len);
2714 :
2715 :
2716 : /* Process all the files contained in the archive. */
2717 : Elf *subelf;
2718 : Elf_Cmd cmd = ELF_C_RDWR;
2719 : int result = 0;
2720 : while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
2721 : {
2722 : /* The the header for this element. */
2723 : Elf_Arhdr *arhdr = elf_getarhdr (subelf);
2724 :
2725 : if (elf_kind (subelf) == ELF_K_ELF)
2726 : result |= handle_elf (fd, subelf, new_prefix, arhdr->ar_name, 0, NULL);
2727 : else if (elf_kind (subelf) == ELF_K_AR)
2728 : result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name, NULL);
2729 :
2730 : /* Get next archive element. */
2731 : cmd = elf_next (subelf);
2732 : if (unlikely (elf_end (subelf) != 0))
2733 : INTERNAL_ERROR (fname);
2734 : }
2735 :
2736 : if (tvp != NULL)
2737 : {
2738 : if (unlikely (futimens (fd, tvp) != 0))
2739 : {
2740 : error (0, errno, gettext ("\
2741 : cannot set access and modification date of '%s'"), fname);
2742 : result = 1;
2743 : }
2744 : }
2745 :
2746 : if (unlikely (close (fd) != 0))
2747 : error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
2748 :
2749 : return result;
2750 : }
2751 :
2752 :
2753 : #include "debugpred.h"
|