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