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