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 : 140104 : secndx_name (Elf *elf, size_t ndx)
373 : : {
374 : 140104 : size_t shstrndx;
375 : 140104 : GElf_Shdr mem;
376 : 140104 : Elf_Scn *sec = elf_getscn (elf, ndx);
377 : 140104 : GElf_Shdr *shdr = gelf_getshdr (sec, &mem);
378 [ + - - + ]: 140104 : if (shdr == NULL || elf_getshdrstrndx (elf, &shstrndx) < 0)
379 : 0 : return "???";
380 [ - + ]: 140104 : 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 : 118464 : 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 : 118464 : size_t size;
457 : :
458 : : #define DO_TYPE(NAME, Name) GElf_##Name Name;
459 : 118464 : union { TYPES; } tmpbuf;
460 : : #undef DO_TYPE
461 : :
462 [ - - + + : 118464 : 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 : 118464 : TYPES;
470 : : #undef DO_TYPE
471 : : default:
472 : : return false;
473 : : }
474 : :
475 [ + - ]: 118464 : if (offset > tdata->d_size
476 [ - + ]: 118464 : || 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 [ + + + + ]: 118464 : if (addend == 0 && sym->st_value == 0)
490 : : return true;
491 : :
492 : 105320 : Elf_Data tmpdata =
493 : : {
494 : : .d_type = type,
495 : : .d_buf = &tmpbuf,
496 : : .d_size = size,
497 : : .d_version = EV_CURRENT,
498 : : };
499 : 105320 : Elf_Data rdata =
500 : : {
501 : : .d_type = type,
502 : 105320 : .d_buf = tdata->d_buf + offset,
503 : : .d_size = size,
504 : : .d_version = EV_CURRENT,
505 : : };
506 : :
507 : 105320 : GElf_Addr value = sym->st_value;
508 [ + - ]: 105320 : if (is_rela)
509 : : {
510 : : /* For SHT_RELA sections we just take the
511 : : given addend and add it to the value. */
512 : 105320 : value += addend;
513 : : /* For ADD/SUB relocations we need to fetch the
514 : : current section contents. */
515 [ - + ]: 105320 : 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 [ - - + + : 105320 : 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 [ - - - - : 105320 : TYPES;
- + - + -
- - - ]
548 : : #undef DO_TYPE
549 : 0 : default:
550 : 0 : abort ();
551 : : }
552 : :
553 : : /* Now finally put in the new value. */
554 : 105320 : Elf_Data *s = gelf_xlatetof (elf, &rdata,
555 : : &tmpdata,
556 : : ei_data);
557 [ - + ]: 105320 : if (s == NULL)
558 : 0 : INTERNAL_ERROR (fname);
559 [ - + ]: 105320 : 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 [ + + ]: 140420 : for (size_t relidx = 0; relidx < nrels; ++relidx)
657 : 118464 : {
658 : 140120 : int rtype, symndx, offset, addend;
659 : 140120 : union { GElf_Rela rela; GElf_Rel rel; } mem;
660 : 140120 : void *rel_p; /* Pointer to either rela or rel above */
661 : :
662 [ + + ]: 140120 : if (is_rela)
663 : : {
664 : 127272 : GElf_Rela *r = gelf_getrela (reldata, relidx, &mem.rela);
665 [ - + ]: 127272 : if (r == NULL)
666 : 0 : INTERNAL_ERROR (fname);
667 : 127272 : offset = r->r_offset;
668 : 127272 : addend = r->r_addend;
669 : 127272 : rtype = GELF_R_TYPE (r->r_info);
670 : 127272 : symndx = GELF_R_SYM (r->r_info);
671 : 127272 : 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 [ - + ]: 140120 : if (rtype == 0)
687 : 118464 : continue;
688 : :
689 : : /* We only do simple absolute relocations. */
690 : 140120 : int addsub = 0;
691 : 140120 : Elf_Type type = ebl_reloc_simple_type (ebl, rtype, &addsub);
692 [ + + ]: 140120 : if (type == ELF_T_NUM)
693 : 16 : goto relocate_failed;
694 : :
695 : : /* And only for relocations against other debug sections. */
696 : 140104 : GElf_Sym sym_mem;
697 : 140104 : Elf32_Word xndx;
698 : 140104 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
699 : : symndx, &sym_mem,
700 : : &xndx);
701 [ - + ]: 140104 : if (sym == NULL)
702 : 0 : INTERNAL_ERROR (fname);
703 : 280208 : Elf32_Word sec = (sym->st_shndx == SHN_XINDEX
704 [ + - ]: 140104 : ? xndx : sym->st_shndx);
705 : :
706 : 140104 : bool dbg_scn = ebl_debugscn_p (ebl, secndx_name (elf, sec));
707 : :
708 [ + + ]: 140104 : if (!dbg_scn)
709 : 21640 : goto relocate_failed;
710 : :
711 [ - + ]: 118464 : if (! relocate (elf, offset, addend,
712 : : tdata, ei_data, fname, is_rela,
713 : : sym, addsub, type))
714 : 0 : goto relocate_failed;
715 : :
716 : 118464 : continue; /* Next */
717 : :
718 : 21656 : relocate_failed:
719 [ + + ]: 21656 : if (relidx != next)
720 : : {
721 : 15664 : int updated;
722 [ + + ]: 15664 : if (is_rela)
723 : 15600 : updated = gelf_update_rela (reldata, next, rel_p);
724 : : else
725 : 64 : updated = gelf_update_rel (reldata, next, rel_p);
726 [ - + ]: 15664 : if (updated == 0)
727 : 0 : INTERNAL_ERROR (fname);
728 : : }
729 : 21656 : ++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 [ + + ]: 1296 : for (cnt = 0; cnt < phnum; ++cnt)
1139 : : {
1140 : 1166 : GElf_Phdr phdr_mem;
1141 : 1166 : GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
1142 [ + - ]: 1166 : if (phdr == NULL
1143 [ - + ]: 1166 : || unlikely (gelf_update_phdr (newelf, cnt, phdr) == 0))
1144 : 1166 : 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 [ + + ]: 766 : for (cnt = 0; cnt < phnum; ++cnt)
1189 : : {
1190 : 696 : GElf_Phdr phdr_mem;
1191 : 696 : GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
1192 [ + - ]: 696 : if (phdr == NULL
1193 [ - + ]: 696 : || unlikely (gelf_update_phdr (debugelf, cnt, phdr) == 0))
1194 : 696 : 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 : : & 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 : 312 : do
1434 : : {
1435 : 312 : changes = false;
1436 : :
1437 [ + + ]: 403246 : for (cnt = 1; cnt < shnum; ++cnt)
1438 : : {
1439 [ + + ]: 402934 : 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 : 266014 : if (shdr_info[cnt].shdr.sh_type == SHT_REL
1444 [ + + ]: 266014 : || shdr_info[cnt].shdr.sh_type == SHT_RELA)
1445 : : {
1446 [ - + ]: 1186 : if (shdr_info[cnt].shdr.sh_info >= shnum)
1447 : 0 : goto illformed;
1448 [ + + ]: 1186 : 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 [ + + ]: 266014 : if (shdr_info[cnt].shdr.sh_type == SHT_GROUP)
1455 : : {
1456 : 22 : Elf32_Word *grpref;
1457 : 22 : grpref = (Elf32_Word *) shdr_info[cnt].data->d_buf;
1458 : 22 : for (size_t in = 1;
1459 [ + + ]: 26 : in < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
1460 : 4 : ++in)
1461 [ + - ]: 22 : if (grpref[in] < shnum)
1462 : : {
1463 [ + + ]: 22 : 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 [ + + ]: 402934 : 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 [ + + ]: 3118 : inner < shdr_info[cnt].data->d_size / elsize;
1516 : 3016 : ++inner)
1517 : : {
1518 : 3016 : GElf_Sym sym_mem;
1519 : 3016 : Elf32_Word xndx;
1520 : 3016 : GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
1521 : : inner, &sym_mem,
1522 : : &xndx);
1523 [ - + ]: 3016 : if (sym == NULL)
1524 : 0 : INTERNAL_ERROR (fname);
1525 : :
1526 : 3016 : size_t scnidx = sym->st_shndx;
1527 [ + + + + ]: 3016 : 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 : 1866 : 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 : : /* Force another iteration in case we need to propagate this
1579 : : change to a previous section (either the sh_link section
1580 : : itself or another section that references it). */
1581 : 176 : changes = true;
1582 : : }
1583 : :
1584 : : /* Handle references through sh_info. */
1585 [ + + - + ]: 135778 : if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
1586 : : {
1587 [ - + ]: 572 : if (shdr_info[cnt].shdr.sh_info >= shnum)
1588 : 0 : goto illformed;
1589 [ - + ]: 572 : else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
1590 : : {
1591 : 0 : shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
1592 : : /* Force another iteration in case we need to propagate
1593 : : this change to a previous section (either the sh_info
1594 : : section itself or another section that references
1595 : : it). */
1596 : 0 : changes = true;
1597 : : }
1598 : : }
1599 : :
1600 : : /* Mark the section as investigated. */
1601 : 135778 : shdr_info[cnt].idx = 2;
1602 : : }
1603 : :
1604 [ + + ]: 402934 : if (debug_fname != NULL
1605 [ + + + + ]: 269346 : && (shdr_info[cnt].idx == 0 || shdr_info[cnt].debug_data != NULL))
1606 : : {
1607 : : /* This section is being preserved in the debug file.
1608 : : Sections it refers to must be preserved there too.
1609 : :
1610 : : In this pass we mark sections to be preserved in both
1611 : : files by setting the .debug_data pointer to the original
1612 : : file's .data pointer. Below, we'll copy the section
1613 : : contents. */
1614 : 134076 : size_t shdr_indices[2] = { shdr_info[cnt].shdr.sh_link, 0 };
1615 : 134076 : int n = 1;
1616 : :
1617 [ + + - + ]: 134076 : if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
1618 : : {
1619 : 732 : shdr_indices[1] = shdr_info[cnt].shdr.sh_info;
1620 : 732 : n++;
1621 : : }
1622 : :
1623 [ + + ]: 268884 : for (int j = 0; j < n; j++)
1624 : : {
1625 : 134808 : size_t i = shdr_indices[j];
1626 [ + + + - : 134808 : if (i != 0 && i < shnum + 2 && shdr_info[i].idx != 0
+ + ]
1627 [ + + ]: 900 : && shdr_info[i].debug_data == NULL)
1628 : : {
1629 [ + - ]: 164 : if (shdr_info[i].data == NULL)
1630 : 164 : shdr_info[i].data = elf_getdata (shdr_info[i].scn, NULL);
1631 [ - + ]: 164 : if (shdr_info[i].data == NULL)
1632 : 0 : INTERNAL_ERROR (fname);
1633 : :
1634 : 164 : shdr_info[i].debug_data = shdr_info[i].data;
1635 : 164 : changes |= i < cnt;
1636 : : }
1637 : : }
1638 : : }
1639 : : }
1640 : : }
1641 [ + + ]: 312 : while (changes);
1642 : :
1643 : : /* Copy the removed sections to the debug output file.
1644 : : The ones that are not removed in the stripped file are SHT_NOBITS. */
1645 [ + + ]: 222 : if (debug_fname != NULL)
1646 : : {
1647 [ + + ]: 266978 : for (cnt = 1; cnt < shnum; ++cnt)
1648 : : {
1649 : 266830 : scn = elf_newscn (debugelf);
1650 [ - + ]: 266830 : if (scn == NULL)
1651 : : {
1652 : 0 : cleanup_debug ();
1653 : 0 : error_exit (0, _("while generating output file: %s"),
1654 : : elf_errmsg (-1));
1655 : : }
1656 : :
1657 : 533660 : bool discard_section = (shdr_info[cnt].idx > 0
1658 [ + + ]: 134174 : && shdr_info[cnt].debug_data == NULL
1659 [ + + ]: 134010 : && shdr_info[cnt].shdr.sh_type != SHT_NOTE
1660 [ + + ]: 133784 : && shdr_info[cnt].shdr.sh_type != SHT_GROUP
1661 [ + + - + ]: 400596 : && cnt != shstrndx);
1662 : :
1663 : : /* Set the section header in the new file. */
1664 : 266830 : GElf_Shdr debugshdr = shdr_info[cnt].shdr;
1665 [ + + ]: 266830 : if (discard_section)
1666 : 133766 : debugshdr.sh_type = SHT_NOBITS;
1667 : :
1668 [ - + ]: 266830 : if (unlikely (gelf_update_shdr (scn, &debugshdr) == 0))
1669 : : /* There cannot be any overflows. */
1670 : 0 : INTERNAL_ERROR (fname);
1671 : :
1672 : : /* Get the data from the old file if necessary. */
1673 [ + + ]: 266830 : if (shdr_info[cnt].data == NULL)
1674 : : {
1675 : 266544 : shdr_info[cnt].data = elf_getdata (shdr_info[cnt].scn, NULL);
1676 [ - + ]: 266544 : if (shdr_info[cnt].data == NULL)
1677 : 0 : INTERNAL_ERROR (fname);
1678 : : }
1679 : :
1680 : : /* Set the data. This is done by copying from the old file. */
1681 : 266830 : Elf_Data *debugdata = elf_newdata (scn);
1682 [ - + ]: 266830 : if (debugdata == NULL)
1683 : 0 : INTERNAL_ERROR (fname);
1684 : :
1685 : : /* Copy the structure. This data may be modified in place
1686 : : before we write out the file. */
1687 : 266830 : *debugdata = *shdr_info[cnt].data;
1688 [ + + ]: 266830 : if (discard_section)
1689 : 133766 : debugdata->d_buf = NULL;
1690 [ + + ]: 133064 : else if (shdr_info[cnt].debug_data != NULL
1691 [ + + ]: 132900 : || shdr_info[cnt].shdr.sh_type == SHT_GROUP)
1692 : : {
1693 : : /* Copy the original data before it gets modified. */
1694 : 184 : shdr_info[cnt].debug_data = debugdata;
1695 [ - + ]: 184 : if (debugdata->d_buf == NULL)
1696 : 0 : INTERNAL_ERROR (fname);
1697 : 184 : debugdata->d_buf = memcpy (xmalloc (debugdata->d_size),
1698 : : debugdata->d_buf, debugdata->d_size);
1699 : : }
1700 : : }
1701 : :
1702 : : /* Finish the ELF header. Fill in the fields not handled by
1703 : : libelf from the old file. */
1704 : 148 : debugehdr = gelf_getehdr (debugelf, &debugehdr_mem);
1705 [ - + ]: 148 : if (debugehdr == NULL)
1706 : 0 : INTERNAL_ERROR (fname);
1707 : :
1708 : 148 : memcpy (debugehdr->e_ident, ehdr->e_ident, EI_NIDENT);
1709 : 148 : debugehdr->e_type = ehdr->e_type;
1710 : 148 : debugehdr->e_machine = ehdr->e_machine;
1711 : 148 : debugehdr->e_version = ehdr->e_version;
1712 : 148 : debugehdr->e_entry = ehdr->e_entry;
1713 : 148 : debugehdr->e_flags = ehdr->e_flags;
1714 : :
1715 [ - + ]: 148 : if (unlikely (gelf_update_ehdr (debugelf, debugehdr) == 0))
1716 : : {
1717 : 0 : error (0, 0, _("%s: error while updating ELF header: %s"),
1718 : : debug_fname, elf_errmsg (-1));
1719 : 0 : result = 1;
1720 : 0 : goto fail_close;
1721 : : }
1722 : :
1723 : 148 : size_t shdrstrndx;
1724 [ - + ]: 148 : if (elf_getshdrstrndx (elf, &shdrstrndx) < 0)
1725 : : {
1726 : 0 : error (0, 0, _("%s: error while getting shdrstrndx: %s"),
1727 : : fname, elf_errmsg (-1));
1728 : 0 : result = 1;
1729 : 0 : goto fail_close;
1730 : : }
1731 : :
1732 [ - + ]: 148 : if (update_shdrstrndx (debugelf, shdrstrndx) != 0)
1733 : : {
1734 : 0 : error (0, 0, _("%s: error updating shdrstrndx: %s"),
1735 : : debug_fname, elf_errmsg (-1));
1736 : 0 : result = 1;
1737 : 0 : goto fail_close;
1738 : : }
1739 : : }
1740 : :
1741 : : /* Although we always create a new section header string table we
1742 : : don't explicitly mark the existing one as unused. It can still
1743 : : be used through a symbol table section we are keeping. If not it
1744 : : will already be marked as unused. */
1745 : :
1746 : : /* We need a string table for the section headers. */
1747 : 222 : shst = dwelf_strtab_init (true);
1748 [ - + ]: 222 : if (shst == NULL)
1749 : : {
1750 : 0 : cleanup_debug ();
1751 [ # # ]: 0 : error_exit (errno, _("while preparing output for '%s'"),
1752 : : output_fname ?: fname);
1753 : : }
1754 : :
1755 : : /* Assign new section numbers. */
1756 : 222 : shdr_info[0].idx = 0;
1757 [ + + ]: 400440 : for (cnt = idx = 1; cnt < shnum; ++cnt)
1758 [ + + ]: 400218 : if (shdr_info[cnt].idx > 0)
1759 : : {
1760 : 135778 : shdr_info[cnt].idx = idx++;
1761 : :
1762 : : /* Create a new section. */
1763 : 135778 : shdr_info[cnt].newscn = elf_newscn (newelf);
1764 [ - + ]: 135778 : if (shdr_info[cnt].newscn == NULL)
1765 : : {
1766 : 0 : cleanup_debug ();
1767 : 0 : error_exit (0,
1768 : : _("while generating output file: %s"),
1769 : : elf_errmsg (-1));
1770 : : }
1771 : :
1772 [ - + ]: 135778 : elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
1773 : :
1774 : : /* Add this name to the section header string table. */
1775 : 135778 : shdr_info[cnt].se = dwelf_strtab_add (shst, shdr_info[cnt].name);
1776 : : }
1777 : :
1778 : : /* Test whether we are doing anything at all. Either all removable
1779 : : sections are already gone. Or the only section we would remove is
1780 : : the .shstrtab section which we would add again. */
1781 [ + - ]: 222 : bool removing_sections = !(cnt == idx
1782 [ + + ]: 222 : || (cnt == idx + 1
1783 [ - + ]: 8 : && shdr_info[shstrndx].idx == 0));
1784 [ + + + + ]: 222 : if (output_fname == NULL && !removing_sections)
1785 : 4 : goto fail_close;
1786 : :
1787 : : /* Create the reference to the file with the debug info (if any). */
1788 [ + + + - : 218 : if (debug_fname != NULL && !remove_shdrs && removing_sections)
+ + ]
1789 : : {
1790 : : /* Add the section header string table section name. */
1791 : 144 : shdr_info[cnt].se = dwelf_strtab_add_len (shst, ".gnu_debuglink", 15);
1792 : 144 : shdr_info[cnt].idx = idx++;
1793 : :
1794 : : /* Create the section header. */
1795 : 144 : shdr_info[cnt].shdr.sh_type = SHT_PROGBITS;
1796 : 144 : shdr_info[cnt].shdr.sh_flags = 0;
1797 : 144 : shdr_info[cnt].shdr.sh_addr = 0;
1798 : 144 : shdr_info[cnt].shdr.sh_link = SHN_UNDEF;
1799 : 144 : shdr_info[cnt].shdr.sh_info = SHN_UNDEF;
1800 : 144 : shdr_info[cnt].shdr.sh_entsize = 0;
1801 : 144 : shdr_info[cnt].shdr.sh_addralign = 4;
1802 : : /* We set the offset to zero here. Before we write the ELF file the
1803 : : field must have the correct value. This is done in the final
1804 : : loop over all section. Then we have all the information needed. */
1805 : 144 : shdr_info[cnt].shdr.sh_offset = 0;
1806 : :
1807 : : /* Create the section. */
1808 : 144 : shdr_info[cnt].newscn = elf_newscn (newelf);
1809 [ - + ]: 144 : if (shdr_info[cnt].newscn == NULL)
1810 : : {
1811 : 0 : cleanup_debug ();
1812 : 0 : error_exit (0, _("while create section header section: %s"),
1813 : : elf_errmsg (-1));
1814 : : }
1815 [ - + ]: 144 : elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
1816 : :
1817 : 144 : shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
1818 [ - + ]: 144 : if (shdr_info[cnt].data == NULL)
1819 : : {
1820 : 0 : cleanup_debug ();
1821 : 0 : error_exit (0, _("cannot allocate section data: %s"),
1822 : : elf_errmsg (-1));
1823 : : }
1824 : :
1825 [ + + ]: 144 : const char *debug_basename = xbasename (debug_fname_embed ?: debug_fname);
1826 : 144 : off_t crc_offset = strlen (debug_basename) + 1;
1827 : : /* Align to 4 byte boundary */
1828 : 144 : crc_offset = ((crc_offset - 1) & ~3) + 4;
1829 : :
1830 : 144 : shdr_info[cnt].data->d_align = 4;
1831 : 144 : shdr_info[cnt].shdr.sh_size = shdr_info[cnt].data->d_size
1832 : 144 : = crc_offset + 4;
1833 : 144 : debuglink_buf = xcalloc (1, shdr_info[cnt].data->d_size);
1834 : 144 : shdr_info[cnt].data->d_buf = debuglink_buf;
1835 : :
1836 : 144 : strcpy (shdr_info[cnt].data->d_buf, debug_basename);
1837 : :
1838 : : /* Cache this Elf_Data describing the CRC32 word in the section.
1839 : : We'll fill this in when we have written the debug file. */
1840 : 144 : debuglink_crc_data = *shdr_info[cnt].data;
1841 : 144 : debuglink_crc_data.d_buf = ((char *) debuglink_crc_data.d_buf
1842 : 144 : + crc_offset);
1843 : 144 : debuglink_crc_data.d_size = 4;
1844 : :
1845 : : /* One more section done. */
1846 : 144 : ++cnt;
1847 : : }
1848 : :
1849 : : /* Index of the section header table in the shdr_info array. */
1850 : 218 : shdridx = cnt;
1851 : :
1852 : : /* Add the section header string table section name. */
1853 : 218 : shdr_info[cnt].se = dwelf_strtab_add_len (shst, ".shstrtab", 10);
1854 : 218 : shdr_info[cnt].idx = idx;
1855 : :
1856 : : /* Create the section header. */
1857 : 218 : shdr_info[cnt].shdr.sh_type = SHT_STRTAB;
1858 : 218 : shdr_info[cnt].shdr.sh_flags = 0;
1859 : 218 : shdr_info[cnt].shdr.sh_addr = 0;
1860 : 218 : shdr_info[cnt].shdr.sh_link = SHN_UNDEF;
1861 : 218 : shdr_info[cnt].shdr.sh_info = SHN_UNDEF;
1862 : 218 : shdr_info[cnt].shdr.sh_entsize = 0;
1863 : : /* We set the offset to zero here. Before we write the ELF file the
1864 : : field must have the correct value. This is done in the final
1865 : : loop over all section. Then we have all the information needed. */
1866 : 218 : shdr_info[cnt].shdr.sh_offset = 0;
1867 : 218 : shdr_info[cnt].shdr.sh_addralign = 1;
1868 : :
1869 : : /* Create the section. */
1870 : 218 : shdr_info[cnt].newscn = elf_newscn (newelf);
1871 [ - + ]: 218 : if (shdr_info[cnt].newscn == NULL)
1872 : : {
1873 : 0 : cleanup_debug ();
1874 : 0 : error_exit (0, _("while create section header section: %s"),
1875 : : elf_errmsg (-1));
1876 : : }
1877 [ - + ]: 218 : elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
1878 : :
1879 : : /* Finalize the string table and fill in the correct indices in the
1880 : : section headers. */
1881 : 218 : shstrtab_data = elf_newdata (shdr_info[cnt].newscn);
1882 [ - + ]: 218 : if (shstrtab_data == NULL)
1883 : : {
1884 : 0 : cleanup_debug ();
1885 : 0 : error_exit (0, _("while create section header string table: %s"),
1886 : : elf_errmsg (-1));
1887 : : }
1888 [ - + ]: 218 : if (dwelf_strtab_finalize (shst, shstrtab_data) == NULL)
1889 : : {
1890 : 0 : cleanup_debug ();
1891 : 0 : error_exit (0, _("no memory to create section header string table"));
1892 : : }
1893 : :
1894 : : /* We have to set the section size. */
1895 : 218 : shdr_info[cnt].shdr.sh_size = shstrtab_data->d_size;
1896 : :
1897 : : /* Update the section information. */
1898 : 218 : GElf_Off lastoffset = 0;
1899 [ + + ]: 400690 : for (cnt = 1; cnt <= shdridx; ++cnt)
1900 [ + + ]: 400472 : if (shdr_info[cnt].idx > 0)
1901 : : {
1902 : 136036 : Elf_Data *newdata;
1903 : :
1904 : 136036 : scn = elf_getscn (newelf, shdr_info[cnt].idx);
1905 [ - + ]: 136036 : elf_assert (scn != NULL);
1906 : :
1907 : : /* Update the name. */
1908 : 136036 : shdr_info[cnt].shdr.sh_name = dwelf_strent_off (shdr_info[cnt].se);
1909 : :
1910 : : /* Update the section header from the input file. Some fields
1911 : : might be section indices which now have to be adjusted. Keep
1912 : : the index to the "current" sh_link in case we need it to lookup
1913 : : symbol table names. */
1914 : 136036 : size_t sh_link = shdr_info[cnt].shdr.sh_link;
1915 [ + + ]: 136036 : if (shdr_info[cnt].shdr.sh_link != 0)
1916 : 1358 : shdr_info[cnt].shdr.sh_link =
1917 : 1358 : shdr_info[shdr_info[cnt].shdr.sh_link].idx;
1918 : :
1919 [ + + ]: 136036 : if (shdr_info[cnt].shdr.sh_type == SHT_GROUP)
1920 : : {
1921 [ + - - + ]: 18 : elf_assert (shdr_info[cnt].data != NULL
1922 : : && shdr_info[cnt].data->d_buf != NULL);
1923 : :
1924 : : Elf32_Word *grpref = (Elf32_Word *) shdr_info[cnt].data->d_buf;
1925 : : /* First word is the section group flag.
1926 : : Followed by section indexes, that need to be renumbered. */
1927 : : for (size_t inner = 1;
1928 [ + + ]: 52 : inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
1929 : 34 : ++inner)
1930 [ + - ]: 34 : if (grpref[inner] < shnum)
1931 : 34 : grpref[inner] = shdr_info[grpref[inner]].idx;
1932 : : else
1933 : 0 : goto illformed;
1934 : : }
1935 : :
1936 : : /* Handle the SHT_REL, SHT_RELA, and SHF_INFO_LINK flag. */
1937 [ + + - + ]: 136036 : if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
1938 : 568 : shdr_info[cnt].shdr.sh_info =
1939 : 568 : shdr_info[shdr_info[cnt].shdr.sh_info].idx;
1940 : :
1941 : : /* Get the data from the old file if necessary. We already
1942 : : created the data for the section header string table. */
1943 [ + + ]: 136036 : if (cnt < shnum)
1944 : : {
1945 [ + + ]: 135674 : if (shdr_info[cnt].data == NULL)
1946 : : {
1947 : 1552 : shdr_info[cnt].data = elf_getdata (shdr_info[cnt].scn, NULL);
1948 [ - + ]: 1552 : if (shdr_info[cnt].data == NULL)
1949 : 0 : INTERNAL_ERROR (fname);
1950 : : }
1951 : :
1952 : : /* Set the data. This is done by copying from the old file. */
1953 : 135674 : newdata = elf_newdata (scn);
1954 [ - + ]: 135674 : if (newdata == NULL)
1955 : 0 : INTERNAL_ERROR (fname);
1956 : :
1957 : : /* Copy the structure. */
1958 : 135674 : *newdata = *shdr_info[cnt].data;
1959 : :
1960 : : /* We know the size. */
1961 : 135674 : shdr_info[cnt].shdr.sh_size = shdr_info[cnt].data->d_size;
1962 : :
1963 : : /* We have to adjust symbol tables. The st_shndx member might
1964 : : have to be updated. */
1965 : 135674 : if (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM
1966 [ + + ]: 135674 : || shdr_info[cnt].shdr.sh_type == SHT_SYMTAB)
1967 : : {
1968 : 268 : Elf_Data *versiondata = NULL;
1969 : 268 : Elf_Data *shndxdata = NULL;
1970 : :
1971 : 268 : size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
1972 : :
1973 [ - + ]: 268 : if (shdr_info[cnt].symtab_idx != 0)
1974 : : {
1975 [ # # ]: 0 : elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX);
1976 : : /* This section has extended section information.
1977 : : We have to modify that information, too. */
1978 : 0 : shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
1979 : : NULL);
1980 : :
1981 [ # # # # : 0 : elf_assert (shndxdata != NULL
# # ]
1982 : : && shndxdata->d_buf != NULL
1983 : : && ((shndxdata->d_size / sizeof (Elf32_Word))
1984 : : >= shdr_info[cnt].data->d_size / elsize));
1985 : : }
1986 : :
1987 [ + + ]: 268 : if (shdr_info[cnt].version_idx != 0)
1988 : : {
1989 [ - + ]: 122 : elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
1990 : : /* This section has associated version
1991 : : information. We have to modify that
1992 : : information, too. */
1993 : 122 : versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
1994 : : NULL);
1995 : :
1996 [ + - + - : 122 : elf_assert (versiondata != NULL
- + ]
1997 : : && versiondata->d_buf != NULL
1998 : : && ((versiondata->d_size / sizeof (GElf_Versym))
1999 : : >= shdr_info[cnt].data->d_size / elsize));
2000 : : }
2001 : :
2002 : 268 : shdr_info[cnt].newsymidx
2003 : 268 : = xcalloc (shdr_info[cnt].data->d_size / elsize,
2004 : : sizeof (Elf32_Word));
2005 : :
2006 : 268 : bool last_was_local = true;
2007 : 268 : size_t destidx;
2008 : 268 : size_t inner;
2009 : 268 : for (destidx = inner = 1;
2010 [ + + ]: 39704 : inner < shdr_info[cnt].data->d_size / elsize;
2011 : 39436 : ++inner)
2012 : : {
2013 : 39436 : Elf32_Word sec;
2014 : 39436 : GElf_Sym sym_mem;
2015 : 39436 : Elf32_Word xshndx;
2016 : 39436 : GElf_Sym *sym = gelf_getsymshndx (shdr_info[cnt].data,
2017 : : shndxdata, inner,
2018 : : &sym_mem, &xshndx);
2019 [ - + ]: 39436 : if (sym == NULL)
2020 : 0 : INTERNAL_ERROR (fname);
2021 : :
2022 : 43750 : if (sym->st_shndx == SHN_UNDEF
2023 [ + + ]: 39436 : || (sym->st_shndx >= SHN_LORESERVE
2024 : : && sym->st_shndx != SHN_XINDEX))
2025 : : {
2026 : : /* This is no section index, leave it alone
2027 : : unless it is moved. */
2028 [ + + ]: 4314 : if (destidx != inner
2029 [ - + ]: 2816 : && gelf_update_symshndx (shdr_info[cnt].data,
2030 : : shndxdata,
2031 : : destidx, sym,
2032 : : xshndx) == 0)
2033 : 0 : INTERNAL_ERROR (fname);
2034 : :
2035 : 4314 : shdr_info[cnt].newsymidx[inner] = destidx++;
2036 : :
2037 [ + + ]: 4314 : if (last_was_local
2038 [ + + ]: 706 : && GELF_ST_BIND (sym->st_info) != STB_LOCAL)
2039 : : {
2040 : 164 : last_was_local = false;
2041 : 164 : shdr_info[cnt].shdr.sh_info = destidx - 1;
2042 : : }
2043 : :
2044 : 4314 : continue;
2045 : : }
2046 : :
2047 : : /* Get the full section index, if necessary from the
2048 : : XINDEX table. */
2049 [ - + ]: 35122 : if (sym->st_shndx == SHN_XINDEX)
2050 [ # # # # ]: 0 : elf_assert (shndxdata != NULL
2051 : : && shndxdata->d_buf != NULL);
2052 : 70244 : size_t sidx = (sym->st_shndx != SHN_XINDEX
2053 [ + - ]: 35122 : ? sym->st_shndx : xshndx);
2054 [ - + ]: 35122 : elf_assert (sidx < shnum);
2055 : 35122 : sec = shdr_info[sidx].idx;
2056 : :
2057 [ + + ]: 35122 : if (sec != 0)
2058 : : {
2059 : 6390 : GElf_Section nshndx;
2060 : 6390 : Elf32_Word nxshndx;
2061 : :
2062 [ + - ]: 6390 : if (sec < SHN_LORESERVE)
2063 : : {
2064 : 6390 : nshndx = sec;
2065 : 6390 : nxshndx = 0;
2066 : : }
2067 : : else
2068 : : {
2069 : : nshndx = SHN_XINDEX;
2070 : : nxshndx = sec;
2071 : : }
2072 : :
2073 [ - + ]: 6390 : elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
2074 : :
2075 [ + + + + ]: 6390 : if ((inner != destidx || nshndx != sym->st_shndx
2076 [ - + - - ]: 3454 : || (shndxdata != NULL && nxshndx != xshndx))
2077 [ - + ]: 2936 : && (sym->st_shndx = nshndx,
2078 : 2936 : gelf_update_symshndx (shdr_info[cnt].data,
2079 : : shndxdata,
2080 : : destidx, sym,
2081 : : nxshndx) == 0))
2082 : 0 : INTERNAL_ERROR (fname);
2083 : :
2084 : 6390 : shdr_info[cnt].newsymidx[inner] = destidx++;
2085 : :
2086 [ + + ]: 6390 : if (last_was_local
2087 [ + + ]: 5256 : && GELF_ST_BIND (sym->st_info) != STB_LOCAL)
2088 : : {
2089 : 104 : last_was_local = false;
2090 : 104 : shdr_info[cnt].shdr.sh_info = destidx - 1;
2091 : : }
2092 : : }
2093 [ + + ]: 28732 : else if ((shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) != 0
2094 [ - + ]: 96 : && GELF_ST_TYPE (sym->st_info) != STT_SECTION
2095 [ # # ]: 0 : && shdr_info[sidx].shdr.sh_type != SHT_GROUP)
2096 : : {
2097 : : /* Removing a real symbol from an allocated
2098 : : symbol table is hard and probably a
2099 : : mistake. Really removing it means
2100 : : rewriting the dynamic segment and hash
2101 : : sections. Just warn and set the symbol
2102 : : section to UNDEF. */
2103 : 0 : error (0, 0,
2104 : 0 : _("Cannot remove symbol [%zd] from allocated symbol table [%zd]"), inner, cnt);
2105 : 0 : sym->st_shndx = SHN_UNDEF;
2106 [ # # ]: 0 : if (gelf_update_sym (shdr_info[cnt].data, destidx,
2107 : : sym) == 0)
2108 : 0 : INTERNAL_ERROR (fname);
2109 : 0 : shdr_info[cnt].newsymidx[inner] = destidx++;
2110 : : }
2111 [ + + ]: 28732 : else if (debug_fname != NULL
2112 [ + + ]: 28376 : && shdr_info[cnt].debug_data == NULL)
2113 : : /* The symbol points to a section that is discarded
2114 : : but isn't preserved in the debug file. Check that
2115 : : this is a section or group signature symbol
2116 : : for a section which has been removed. Or a special
2117 : : data marker symbol to a debug section. */
2118 : : {
2119 [ + + - + : 35212 : elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION
- - + - ]
2120 : : || ((shdr_info[sidx].shdr.sh_type
2121 : : == SHT_GROUP)
2122 : : && (shdr_info[sidx].shdr.sh_info
2123 : : == inner))
2124 : : || ebl_data_marker_symbol (ebl, sym,
2125 : : elf_strptr (elf, sh_link,
2126 : : sym->st_name)));
2127 : : }
2128 : : }
2129 : :
2130 [ + + ]: 268 : if (destidx != inner)
2131 : : {
2132 : : /* The size of the symbol table changed. */
2133 : 134 : shdr_info[cnt].shdr.sh_size = newdata->d_size
2134 : 134 : = destidx * elsize;
2135 : 134 : any_symtab_changes = true;
2136 : : }
2137 : : else
2138 : : {
2139 : : /* The symbol table didn't really change. */
2140 : 134 : free (shdr_info[cnt].newsymidx);
2141 : 134 : shdr_info[cnt].newsymidx = NULL;
2142 : : }
2143 : : }
2144 : : }
2145 : :
2146 : : /* If we have to, compute the offset of the section.
2147 : : If allocate and unallocated sections are mixed, we only update
2148 : : the allocated ones now. The unallocated ones come second. */
2149 [ + + ]: 136036 : if (! mixed_allocated_unallocated
2150 [ + + ]: 133054 : || (shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) != 0)
2151 : : {
2152 [ + + ]: 4110 : if (shdr_info[cnt].shdr.sh_offset == 0)
2153 : 1252 : shdr_info[cnt].shdr.sh_offset
2154 : 1252 : = ((lastoffset + shdr_info[cnt].shdr.sh_addralign - 1)
2155 : 1252 : & ~((GElf_Off) (shdr_info[cnt].shdr.sh_addralign - 1)));
2156 : :
2157 : : /* Set the section header in the new file. */
2158 [ - + ]: 4110 : if (unlikely (gelf_update_shdr (scn, &shdr_info[cnt].shdr) == 0))
2159 : : /* There cannot be any overflows. */
2160 : 0 : INTERNAL_ERROR (fname);
2161 : :
2162 : : /* Remember the last section written so far. */
2163 : 8220 : GElf_Off filesz = (shdr_info[cnt].shdr.sh_type != SHT_NOBITS
2164 [ + + ]: 4110 : ? shdr_info[cnt].shdr.sh_size : 0);
2165 [ + + ]: 4110 : if (lastoffset < shdr_info[cnt].shdr.sh_offset + filesz)
2166 : 400472 : lastoffset = shdr_info[cnt].shdr.sh_offset + filesz;
2167 : : }
2168 : : }
2169 : :
2170 : : /* We might have to update the unallocated sections after we done the
2171 : : allocated ones. lastoffset is set to right after the last allocated
2172 : : section. */
2173 [ + + ]: 218 : if (mixed_allocated_unallocated)
2174 [ + + ]: 396584 : for (cnt = 1; cnt <= shdridx; ++cnt)
2175 [ + + ]: 396484 : if (shdr_info[cnt].idx > 0)
2176 : : {
2177 : 133054 : scn = elf_getscn (newelf, shdr_info[cnt].idx);
2178 [ + + ]: 133054 : if ((shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) == 0)
2179 : : {
2180 [ + - ]: 131926 : if (shdr_info[cnt].shdr.sh_offset == 0)
2181 : 131926 : shdr_info[cnt].shdr.sh_offset
2182 : 131926 : = ((lastoffset + shdr_info[cnt].shdr.sh_addralign - 1)
2183 : 131926 : & ~((GElf_Off) (shdr_info[cnt].shdr.sh_addralign - 1)));
2184 : :
2185 : : /* Set the section header in the new file. */
2186 [ - + ]: 131926 : if (unlikely (gelf_update_shdr (scn, &shdr_info[cnt].shdr) == 0))
2187 : : /* There cannot be any overflows. */
2188 : 0 : INTERNAL_ERROR (fname);
2189 : :
2190 : : /* Remember the last section written so far. */
2191 : 263852 : GElf_Off filesz = (shdr_info[cnt].shdr.sh_type != SHT_NOBITS
2192 [ + - ]: 131926 : ? shdr_info[cnt].shdr.sh_size : 0);
2193 [ + + ]: 131926 : if (lastoffset < shdr_info[cnt].shdr.sh_offset + filesz)
2194 : 396484 : lastoffset = shdr_info[cnt].shdr.sh_offset + filesz;
2195 : : }
2196 : : }
2197 : :
2198 : : /* Adjust symbol references if symbol tables changed. */
2199 [ + + ]: 218 : if (any_symtab_changes)
2200 : : /* Find all relocation sections which use this symbol table. */
2201 [ + + ]: 135448 : for (cnt = 1; cnt <= shdridx; ++cnt)
2202 : : {
2203 : 135318 : struct shdr_info *info = &shdr_info[cnt];
2204 [ + + + + ]: 135318 : if (info->idx == 0 && debug_fname == NULL)
2205 : : /* Ignore sections which are discarded. When we are saving a
2206 : : relocation section in a separate debug file, we must fix up
2207 : : the symbol table references. */
2208 : 422 : continue;
2209 : :
2210 : 134896 : const Elf32_Word symtabidx = info->old_sh_link;
2211 [ - + ]: 134896 : elf_assert (symtabidx < shnum + 2);
2212 : 134896 : const Elf32_Word *const newsymidx = shdr_info[symtabidx].newsymidx;
2213 : :
2214 : : /* If the symbol table hasn't changed, do not do anything. */
2215 [ + + ]: 134896 : if (newsymidx == NULL)
2216 : 134104 : continue;
2217 : :
2218 : : /* If the symbol table is not discarded, but additionally
2219 : : duplicated in the separate debug file and this section
2220 : : is discarded, don't adjust anything. */
2221 [ + + + - ]: 792 : if (info->idx == 0 && shdr_info[symtabidx].debug_data != NULL)
2222 : 368 : continue;
2223 : :
2224 [ + + + + : 424 : switch (info->shdr.sh_type)
- ]
2225 : : {
2226 : 382 : case SHT_REL:
2227 : : case SHT_RELA:
2228 : 764 : scn = (info->idx == 0
2229 : 0 : ? elf_getscn (debugelf, cnt)
2230 [ - + ]: 382 : : elf_getscn (newelf, info->idx));
2231 : 382 : Elf_Data *d = elf_getdata (scn, NULL);
2232 [ + - + - : 382 : elf_assert (d != NULL && d->d_buf != NULL
- + ]
2233 : : && info->shdr.sh_entsize != 0);
2234 : 382 : size_t nrels = (info->shdr.sh_size / info->shdr.sh_entsize);
2235 : :
2236 : 382 : size_t symsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2237 : 382 : const Elf32_Word symidxn = (shdr_info[symtabidx].data->d_size
2238 : 382 : / symsize);
2239 [ + + ]: 382 : if (info->shdr.sh_type == SHT_REL)
2240 [ + + ]: 4878 : for (size_t relidx = 0; relidx < nrels; ++relidx)
2241 : : {
2242 : 4822 : GElf_Rel rel_mem;
2243 [ - + ]: 4822 : if (gelf_getrel (d, relidx, &rel_mem) == NULL)
2244 : 0 : INTERNAL_ERROR (fname);
2245 : :
2246 : 4822 : size_t symidx = GELF_R_SYM (rel_mem.r_info);
2247 [ - + ]: 4822 : elf_assert (symidx < symidxn);
2248 [ + + ]: 4822 : if (newsymidx[symidx] != symidx)
2249 : : {
2250 : 4412 : rel_mem.r_info
2251 : 4412 : = GELF_R_INFO (newsymidx[symidx],
2252 : : GELF_R_TYPE (rel_mem.r_info));
2253 : :
2254 [ - + ]: 4412 : if (gelf_update_rel (d, relidx, &rel_mem) == 0)
2255 : 4822 : INTERNAL_ERROR (fname);
2256 : : }
2257 : : }
2258 : : else
2259 [ + + ]: 27514 : for (size_t relidx = 0; relidx < nrels; ++relidx)
2260 : : {
2261 : 27188 : GElf_Rela rel_mem;
2262 [ - + ]: 27188 : if (gelf_getrela (d, relidx, &rel_mem) == NULL)
2263 : 0 : INTERNAL_ERROR (fname);
2264 : :
2265 : 27188 : size_t symidx = GELF_R_SYM (rel_mem.r_info);
2266 [ - + ]: 27188 : elf_assert (symidx < symidxn);
2267 [ + + ]: 27188 : if (newsymidx[symidx] != symidx)
2268 : : {
2269 : 9820 : rel_mem.r_info
2270 : 9820 : = GELF_R_INFO (newsymidx[symidx],
2271 : : GELF_R_TYPE (rel_mem.r_info));
2272 : :
2273 [ - + ]: 9820 : if (gelf_update_rela (d, relidx, &rel_mem) == 0)
2274 : 27188 : INTERNAL_ERROR (fname);
2275 : : }
2276 : : }
2277 : 424 : break;
2278 : :
2279 : 12 : case SHT_HASH:
2280 : : /* We have to recompute the hash table. */
2281 : :
2282 [ - + ]: 12 : elf_assert (info->idx > 0);
2283 : :
2284 : : /* The hash section in the new file. */
2285 : 12 : scn = elf_getscn (newelf, info->idx);
2286 : :
2287 : : /* The symbol table data. */
2288 : 12 : Elf_Data *symd = elf_getdata (elf_getscn (newelf,
2289 : 12 : shdr_info[symtabidx].idx),
2290 : : NULL);
2291 [ + - - + ]: 12 : elf_assert (symd != NULL && symd->d_buf != NULL);
2292 : :
2293 : : /* The hash table data. */
2294 : 12 : Elf_Data *hashd = elf_getdata (scn, NULL);
2295 [ + - - + ]: 12 : elf_assert (hashd != NULL && hashd->d_buf != NULL);
2296 : :
2297 [ + - ]: 12 : if (info->shdr.sh_entsize == sizeof (Elf32_Word))
2298 : : {
2299 : : /* Sane arches first. */
2300 [ - + ]: 12 : elf_assert (hashd->d_size >= 2 * sizeof (Elf32_Word));
2301 : 12 : Elf32_Word *bucket = (Elf32_Word *) hashd->d_buf;
2302 : :
2303 : 12 : size_t strshndx = shdr_info[symtabidx].old_sh_link;
2304 : 12 : size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2305 : :
2306 : 12 : Elf32_Word nchain = bucket[1];
2307 : 12 : Elf32_Word nbucket = bucket[0];
2308 : 12 : uint64_t used_buf = ((2ULL + nchain + nbucket)
2309 : : * sizeof (Elf32_Word));
2310 [ - + ]: 12 : elf_assert (used_buf <= hashd->d_size);
2311 : :
2312 : : /* Adjust the nchain value. The symbol table size
2313 : : changed. We keep the same size for the bucket array. */
2314 : 12 : bucket[1] = symd->d_size / elsize;
2315 : 12 : bucket += 2;
2316 : 12 : Elf32_Word *chain = bucket + nbucket;
2317 : :
2318 : : /* New size of the section. */
2319 : 12 : size_t n_size = ((2 + symd->d_size / elsize + nbucket)
2320 : : * sizeof (Elf32_Word));
2321 [ - + ]: 12 : elf_assert (n_size <= hashd->d_size);
2322 : 12 : hashd->d_size = n_size;
2323 : 12 : update_section_size (scn, hashd, debugelf, cnt, fname);
2324 : :
2325 : : /* Clear the arrays. */
2326 : 12 : memset (bucket, '\0',
2327 : 12 : (symd->d_size / elsize + nbucket)
2328 : : * sizeof (Elf32_Word));
2329 : :
2330 : 12 : for (size_t inner = shdr_info[symtabidx].shdr.sh_info;
2331 [ + + ]: 156 : inner < symd->d_size / elsize; ++inner)
2332 : : {
2333 : 144 : GElf_Sym sym_mem;
2334 : 144 : GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
2335 [ - + ]: 144 : elf_assert (sym != NULL);
2336 : :
2337 : 288 : const char *name = elf_strptr (elf, strshndx,
2338 : 144 : sym->st_name);
2339 [ - + ]: 144 : elf_assert (name != NULL && nbucket != 0);
2340 : 144 : size_t hidx = elf_hash (name) % nbucket;
2341 : :
2342 [ + + ]: 144 : if (bucket[hidx] == 0)
2343 : 120 : bucket[hidx] = inner;
2344 : : else
2345 : : {
2346 : 24 : hidx = bucket[hidx];
2347 : :
2348 [ - + - - ]: 24 : while (chain[hidx] != 0 && chain[hidx] < nchain)
2349 : 0 : hidx = chain[hidx];
2350 : :
2351 : 24 : chain[hidx] = inner;
2352 : : }
2353 : : }
2354 : : }
2355 : : else
2356 : : {
2357 : : /* Alpha and S390 64-bit use 64-bit SHT_HASH entries. */
2358 [ # # ]: 0 : elf_assert (info->shdr.sh_entsize == sizeof (Elf64_Xword));
2359 : :
2360 : 0 : Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
2361 : :
2362 : 0 : size_t strshndx = shdr_info[symtabidx].old_sh_link;
2363 : 0 : size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2364 : :
2365 [ # # ]: 0 : elf_assert (symd->d_size >= 2 * sizeof (Elf64_Xword));
2366 : 0 : Elf64_Xword nbucket = bucket[0];
2367 : 0 : Elf64_Xword nchain = bucket[1];
2368 : 0 : uint64_t maxwords = hashd->d_size / sizeof (Elf64_Xword);
2369 [ # # # # : 0 : elf_assert (maxwords >= 2
# # ]
2370 : : && maxwords - 2 >= nbucket
2371 : : && maxwords - 2 - nbucket >= nchain);
2372 : :
2373 : : /* Adjust the nchain value. The symbol table size
2374 : : changed. We keep the same size for the bucket array. */
2375 : 0 : bucket[1] = symd->d_size / elsize;
2376 : 0 : bucket += 2;
2377 : 0 : Elf64_Xword *chain = bucket + nbucket;
2378 : :
2379 : : /* New size of the section. */
2380 : 0 : size_t n_size = ((2 + symd->d_size / elsize + nbucket)
2381 : : * sizeof (Elf64_Xword));
2382 [ # # ]: 0 : elf_assert (n_size <= hashd->d_size);
2383 : 0 : hashd->d_size = n_size;
2384 : 0 : update_section_size (scn, hashd, debugelf, cnt, fname);
2385 : :
2386 : : /* Clear the arrays. */
2387 : 0 : memset (bucket, '\0',
2388 : 0 : (symd->d_size / elsize + nbucket)
2389 : : * sizeof (Elf64_Xword));
2390 : :
2391 : 0 : for (size_t inner = shdr_info[symtabidx].shdr.sh_info;
2392 [ # # ]: 0 : inner < symd->d_size / elsize; ++inner)
2393 : : {
2394 : 0 : GElf_Sym sym_mem;
2395 : 0 : GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
2396 [ # # ]: 0 : elf_assert (sym != NULL);
2397 : :
2398 : 0 : const char *name = elf_strptr (elf, strshndx,
2399 : 0 : sym->st_name);
2400 [ # # ]: 0 : elf_assert (name != NULL && nbucket != 0);
2401 : 0 : size_t hidx = elf_hash (name) % nbucket;
2402 : :
2403 [ # # ]: 0 : if (bucket[hidx] == 0)
2404 : 0 : bucket[hidx] = inner;
2405 : : else
2406 : : {
2407 : : hidx = bucket[hidx];
2408 : :
2409 [ # # # # ]: 0 : while (chain[hidx] != 0 && chain[hidx] < nchain)
2410 : : hidx = chain[hidx];
2411 : :
2412 : 0 : chain[hidx] = inner;
2413 : : }
2414 : : }
2415 : : }
2416 : : break;
2417 : :
2418 : 12 : case SHT_GNU_versym:
2419 : : /* If the symbol table changed we have to adjust the entries. */
2420 [ - + ]: 12 : elf_assert (info->idx > 0);
2421 : :
2422 : : /* The symbol version section in the new file. */
2423 : 12 : scn = elf_getscn (newelf, info->idx);
2424 : :
2425 : : /* The symbol table data. */
2426 : 12 : symd = elf_getdata (elf_getscn (newelf, shdr_info[symtabidx].idx),
2427 : : NULL);
2428 [ + - - + ]: 12 : elf_assert (symd != NULL && symd->d_buf != NULL);
2429 : 12 : size_t symz = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2430 : 12 : const Elf32_Word syms = (shdr_info[symtabidx].data->d_size / symz);
2431 : :
2432 : : /* The version symbol data. */
2433 : 12 : Elf_Data *verd = elf_getdata (scn, NULL);
2434 [ + - - + ]: 12 : elf_assert (verd != NULL && verd->d_buf != NULL);
2435 : :
2436 : : /* The symbol version array. */
2437 : 12 : GElf_Half *verstab = (GElf_Half *) verd->d_buf;
2438 : :
2439 : : /* Walk through the list and */
2440 : 12 : size_t elsize = gelf_fsize (elf, verd->d_type, 1, EV_CURRENT);
2441 : 12 : Elf32_Word vers = verd->d_size / elsize;
2442 [ + + + - ]: 516 : for (size_t inner = 1; inner < vers && inner < syms; ++inner)
2443 [ + + + - ]: 504 : if (newsymidx[inner] != 0 && newsymidx[inner] < vers)
2444 : : /* Overwriting the same array works since the
2445 : : reordering can only move entries to lower indices
2446 : : in the array. */
2447 : 408 : verstab[newsymidx[inner]] = verstab[inner];
2448 : :
2449 : : /* New size of the section. */
2450 : 12 : verd->d_size = gelf_fsize (newelf, verd->d_type,
2451 : 12 : symd->d_size
2452 : 12 : / gelf_fsize (elf, symd->d_type, 1,
2453 : : EV_CURRENT),
2454 : : EV_CURRENT);
2455 : 12 : update_section_size (scn, verd, debugelf, cnt, fname);
2456 : 12 : break;
2457 : :
2458 : 18 : case SHT_GROUP:
2459 : : /* Yes, the symbol table changed.
2460 : : Update the section header of the section group. */
2461 : 18 : scn = elf_getscn (newelf, info->idx);
2462 : 18 : GElf_Shdr shdr_mem;
2463 : 18 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2464 [ - + ]: 18 : elf_assert (shdr != NULL);
2465 : :
2466 : 18 : size_t symsz = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2467 : 18 : const Elf32_Word symn = (shdr_info[symtabidx].data->d_size
2468 : 18 : / symsz);
2469 [ - + ]: 18 : elf_assert (shdr->sh_info < symn);
2470 : 18 : shdr->sh_info = newsymidx[shdr->sh_info];
2471 : :
2472 : 18 : (void) gelf_update_shdr (scn, shdr);
2473 : 18 : break;
2474 : : }
2475 : : }
2476 : :
2477 : : /* Remove any relocations between debug sections in ET_REL
2478 : : for the debug file when requested. These relocations are always
2479 : : zero based between the unallocated sections. */
2480 [ + + + + ]: 218 : if (debug_fname != NULL && removing_sections
2481 [ + + + + ]: 144 : && reloc_debug && ehdr->e_type == ET_REL)
2482 : 32 : remove_debug_relocations (ebl, debugelf, ehdr, fname, shstrndx);
2483 : :
2484 : : /* Now that we have done all adjustments to the data,
2485 : : we can actually write out the debug file. */
2486 [ + + + + ]: 218 : if (debug_fname != NULL && removing_sections)
2487 : : {
2488 : : /* Finally write the file. */
2489 [ - + ]: 144 : if (permissive)
2490 : 0 : elf_flagelf (debugelf, ELF_C_SET, ELF_F_PERMISSIVE);
2491 [ - + ]: 144 : if (unlikely (elf_update (debugelf, ELF_C_WRITE) == -1))
2492 : : {
2493 : 0 : error (0, 0, _("while writing '%s': %s"),
2494 : : tmp_debug_fname, elf_errmsg (-1));
2495 : 0 : result = 1;
2496 : 0 : goto fail_close;
2497 : : }
2498 : :
2499 : : /* Create the real output file. First rename, then change the
2500 : : mode. */
2501 [ + - ]: 144 : if (rename (tmp_debug_fname, debug_fname) != 0
2502 [ - + ]: 144 : || fchmod (debug_fd, mode) != 0)
2503 : : {
2504 : 0 : error (0, errno, _("while creating '%s'"), debug_fname);
2505 : 0 : result = 1;
2506 : 0 : goto fail_close;
2507 : : }
2508 : :
2509 : : /* The temporary file does not exist anymore. */
2510 : 144 : free (tmp_debug_fname);
2511 : 144 : tmp_debug_fname = NULL;
2512 : :
2513 [ + - ]: 144 : if (!remove_shdrs)
2514 : : {
2515 : 144 : uint32_t debug_crc;
2516 : 144 : Elf_Data debug_crc_data =
2517 : : {
2518 : : .d_type = ELF_T_WORD,
2519 : : .d_buf = &debug_crc,
2520 : : .d_size = sizeof (debug_crc),
2521 : : .d_version = EV_CURRENT
2522 : : };
2523 : :
2524 : : /* Compute the checksum which we will add to the executable. */
2525 [ - + ]: 144 : if (crc32_file (debug_fd, &debug_crc) != 0)
2526 : : {
2527 : 0 : error (0, errno, _("\
2528 : : while computing checksum for debug information"));
2529 : 0 : unlink (debug_fname);
2530 : 0 : result = 1;
2531 : 0 : goto fail_close;
2532 : : }
2533 : :
2534 : : /* Store it in the debuglink section data. */
2535 [ - + ]: 144 : if (unlikely (gelf_xlatetof (newelf, &debuglink_crc_data,
2536 : : &debug_crc_data, ehdr->e_ident[EI_DATA])
2537 : : != &debuglink_crc_data))
2538 : 144 : INTERNAL_ERROR (fname);
2539 : : }
2540 : : }
2541 : :
2542 : 218 : lastsec_offset = shdr_info[shdridx].shdr.sh_offset;
2543 : 218 : lastsec_size = shdr_info[shdridx].shdr.sh_size;
2544 : :
2545 : 250 : done:
2546 : : /* Finally finish the ELF header. Fill in the fields not handled by
2547 : : libelf from the old file. */
2548 : 250 : newehdr = gelf_getehdr (newelf, &newehdr_mem);
2549 [ - + ]: 250 : if (newehdr == NULL)
2550 : 0 : INTERNAL_ERROR (fname);
2551 : :
2552 : 250 : memcpy (newehdr->e_ident, ehdr->e_ident, EI_NIDENT);
2553 : 250 : newehdr->e_type = ehdr->e_type;
2554 : 250 : newehdr->e_machine = ehdr->e_machine;
2555 : 250 : newehdr->e_version = ehdr->e_version;
2556 : 250 : newehdr->e_entry = ehdr->e_entry;
2557 : 250 : newehdr->e_flags = ehdr->e_flags;
2558 : 250 : newehdr->e_phoff = ehdr->e_phoff;
2559 : :
2560 : : /* We need to position the section header table. */
2561 : 250 : const size_t offsize = gelf_fsize (elf, ELF_T_OFF, 1, EV_CURRENT);
2562 : 250 : newehdr->e_shoff = ((lastsec_offset + lastsec_size + offsize - 1)
2563 : 250 : & ~((GElf_Off) (offsize - 1)));
2564 : 250 : newehdr->e_shentsize = gelf_fsize (elf, ELF_T_SHDR, 1, EV_CURRENT);
2565 : :
2566 [ - + ]: 250 : if (gelf_update_ehdr (newelf, newehdr) == 0)
2567 : : {
2568 : 0 : error (0, 0, _("%s: error while creating ELF header: %s"),
2569 [ # # ]: 0 : output_fname ?: fname, elf_errmsg (-1));
2570 : 0 : result = 1;
2571 : : }
2572 : :
2573 : : /* The new section header string table index. */
2574 [ - + ]: 250 : if (update_shdrstrndx (newelf, idx) != 0)
2575 : : {
2576 : 0 : error (0, 0, _("%s: error updating shdrstrndx: %s"),
2577 [ # # ]: 0 : output_fname ?: fname, elf_errmsg (-1));
2578 : 0 : result = 1;
2579 : : }
2580 : :
2581 : : /* We have everything from the old file. */
2582 [ - + ]: 250 : if (elf_cntl (elf, ELF_C_FDDONE) != 0)
2583 : : {
2584 : 0 : error (0, 0, _("%s: error while reading the file: %s"),
2585 : : fname, elf_errmsg (-1));
2586 : 0 : result = 1;
2587 : : }
2588 : :
2589 : : /* The ELF library better follows our layout when this is not a
2590 : : relocatable object file. */
2591 : 500 : elf_flagelf (newelf, ELF_C_SET,
2592 [ + + ]: 250 : (phnum > 0 ? ELF_F_LAYOUT : 0)
2593 [ + - ]: 500 : | (permissive ? ELF_F_PERMISSIVE : 0));
2594 : :
2595 : : /* Finally write the file. */
2596 [ - + ]: 250 : if (elf_update (newelf, ELF_C_WRITE) == -1)
2597 : : {
2598 : 0 : error (0, 0, _("while writing '%s': %s"),
2599 [ # # ]: 0 : output_fname ?: fname, elf_errmsg (-1));
2600 : 0 : result = 1;
2601 : : }
2602 : :
2603 [ + - ]: 250 : if (remove_shdrs)
2604 : : {
2605 : : /* libelf can't cope without the section headers being properly intact.
2606 : : So we just let it write them normally, and then we nuke them later. */
2607 : :
2608 [ # # ]: 0 : if (newehdr->e_ident[EI_CLASS] == ELFCLASS32)
2609 : : {
2610 : 0 : eu_static_assert (offsetof (Elf32_Ehdr, e_shentsize)
2611 : : + sizeof (Elf32_Half)
2612 : : == offsetof (Elf32_Ehdr, e_shnum));
2613 : 0 : eu_static_assert (offsetof (Elf32_Ehdr, e_shnum)
2614 : : + sizeof (Elf32_Half)
2615 : : == offsetof (Elf32_Ehdr, e_shstrndx));
2616 : 0 : const Elf32_Off zero_off = 0;
2617 : 0 : const Elf32_Half zero[3] = { 0, 0, SHN_UNDEF };
2618 [ # # ]: 0 : if (pwrite_retry (fd, &zero_off, sizeof zero_off,
2619 : : offsetof (Elf32_Ehdr, e_shoff)) != sizeof zero_off
2620 [ # # ]: 0 : || (pwrite_retry (fd, zero, sizeof zero,
2621 : : offsetof (Elf32_Ehdr, e_shentsize))
2622 : : != sizeof zero)
2623 [ # # ]: 0 : || ftruncate (fd, lastsec_offset) < 0)
2624 : : {
2625 : 0 : error (0, errno, _("while writing '%s'"),
2626 [ # # ]: 0 : output_fname ?: fname);
2627 : 0 : result = 1;
2628 : : }
2629 : : }
2630 : : else
2631 : : {
2632 : 0 : eu_static_assert (offsetof (Elf64_Ehdr, e_shentsize)
2633 : : + sizeof (Elf64_Half)
2634 : : == offsetof (Elf64_Ehdr, e_shnum));
2635 : 0 : eu_static_assert (offsetof (Elf64_Ehdr, e_shnum)
2636 : : +sizeof (Elf64_Half)
2637 : : == offsetof (Elf64_Ehdr, e_shstrndx));
2638 : 0 : const Elf64_Off zero_off = 0;
2639 : 0 : const Elf64_Half zero[3] = { 0, 0, SHN_UNDEF };
2640 [ # # ]: 0 : if (pwrite_retry (fd, &zero_off, sizeof zero_off,
2641 : : offsetof (Elf64_Ehdr, e_shoff)) != sizeof zero_off
2642 [ # # ]: 0 : || (pwrite_retry (fd, zero, sizeof zero,
2643 : : offsetof (Elf64_Ehdr, e_shentsize))
2644 : : != sizeof zero)
2645 [ # # ]: 0 : || ftruncate (fd, lastsec_offset) < 0)
2646 : : {
2647 : 0 : error (0, errno, _("while writing '%s'"),
2648 [ # # ]: 0 : output_fname ?: fname);
2649 : 0 : result = 1;
2650 : : }
2651 : : }
2652 : : }
2653 : :
2654 : 250 : fail_close:
2655 [ + + ]: 256 : if (shdr_info != NULL)
2656 : : {
2657 : : /* For some sections we might have created an table to map symbol
2658 : : table indices. Or we might kept (original) data around to put
2659 : : into the .debug file. */
2660 [ + + ]: 400694 : for (cnt = 1; cnt <= shdridx; ++cnt)
2661 : : {
2662 : 400472 : free (shdr_info[cnt].newsymidx);
2663 [ + + ]: 400472 : if (shdr_info[cnt].debug_data != NULL)
2664 : 184 : free (shdr_info[cnt].debug_data->d_buf);
2665 : : }
2666 : :
2667 : : /* Free data we allocated for the .gnu_debuglink section. */
2668 : 222 : free (debuglink_buf);
2669 : :
2670 : : /* Free the memory. */
2671 [ + + ]: 222 : if ((shnum + 2) * sizeof (struct shdr_info) > MAX_STACK_ALLOC)
2672 : 6 : free (shdr_info);
2673 : : }
2674 : :
2675 : : /* Free other resources. */
2676 [ + + ]: 256 : if (shstrtab_data != NULL)
2677 : 218 : free (shstrtab_data->d_buf);
2678 [ + + ]: 256 : if (shst != NULL)
2679 : 222 : dwelf_strtab_free (shst);
2680 : :
2681 : : /* That was it. Close the descriptors. */
2682 [ - + ]: 256 : if (elf_end (newelf) != 0)
2683 : : {
2684 : 0 : error (0, 0, _("error while finishing '%s': %s"),
2685 [ # # ]: 0 : output_fname ?: fname, elf_errmsg (-1));
2686 : 0 : result = 1;
2687 : : }
2688 : :
2689 [ + + + - ]: 256 : if (debugelf != NULL && elf_end (debugelf) != 0)
2690 : : {
2691 : 0 : error (0, 0, _("error while finishing '%s': %s"), debug_fname,
2692 : : elf_errmsg (-1));
2693 : 0 : result = 1;
2694 : : }
2695 : :
2696 : 256 : fail:
2697 : : /* Close the EBL backend. */
2698 [ + + ]: 256 : if (ebl != NULL)
2699 : 140 : ebl_closebackend (ebl);
2700 : :
2701 : 256 : cleanup_debug ();
2702 : :
2703 : : /* If requested, preserve the timestamp. */
2704 [ + - ]: 256 : if (tvp != NULL)
2705 : : {
2706 [ # # ]: 0 : if (futimens (fd, tvp) != 0)
2707 : : {
2708 : 0 : error (0, errno, _("\
2709 : : cannot set access and modification date of '%s'"),
2710 [ # # ]: 0 : output_fname ?: fname);
2711 : 0 : result = 1;
2712 : : }
2713 : : }
2714 : :
2715 : : /* Close the file descriptor if we created a new file. */
2716 [ + + ]: 256 : if (output_fname != NULL)
2717 : : {
2718 : 168 : close (fd);
2719 [ + - ]: 168 : if (result != 0)
2720 : 0 : unlink (output_fname);
2721 : : }
2722 : :
2723 : : return result;
2724 : : }
2725 : :
2726 : : static void
2727 : 256 : cleanup_debug (void)
2728 : : {
2729 [ + + ]: 256 : if (debug_fd >= 0)
2730 : : {
2731 [ + + ]: 148 : if (tmp_debug_fname != NULL)
2732 : : {
2733 : 4 : unlink (tmp_debug_fname);
2734 : 4 : free (tmp_debug_fname);
2735 : 4 : tmp_debug_fname = NULL;
2736 : : }
2737 : 148 : close (debug_fd);
2738 : 148 : debug_fd = -1;
2739 : : }
2740 : 256 : }
2741 : :
2742 : : static int
2743 : : handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
2744 : : struct timespec tvp[2])
2745 : : {
2746 : : size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
2747 : : size_t fname_len = strlen (fname) + 1;
2748 : : char new_prefix[prefix_len + 1 + fname_len];
2749 : : char *cp = new_prefix;
2750 : :
2751 : : /* Create the full name of the file. */
2752 : : if (prefix != NULL)
2753 : : {
2754 : : cp = mempcpy (cp, prefix, prefix_len);
2755 : : *cp++ = ':';
2756 : : }
2757 : : memcpy (cp, fname, fname_len);
2758 : :
2759 : :
2760 : : /* Process all the files contained in the archive. */
2761 : : Elf *subelf;
2762 : : Elf_Cmd cmd = ELF_C_RDWR;
2763 : : int result = 0;
2764 : : while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
2765 : : {
2766 : : /* The the header for this element. */
2767 : : Elf_Arhdr *arhdr = elf_getarhdr (subelf);
2768 : :
2769 : : if (elf_kind (subelf) == ELF_K_ELF)
2770 : : result |= handle_elf (fd, subelf, new_prefix, arhdr->ar_name, 0, NULL);
2771 : : else if (elf_kind (subelf) == ELF_K_AR)
2772 : : result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name, NULL);
2773 : :
2774 : : /* Get next archive element. */
2775 : : cmd = elf_next (subelf);
2776 : : if (unlikely (elf_end (subelf) != 0))
2777 : : INTERNAL_ERROR (fname);
2778 : : }
2779 : :
2780 : : if (tvp != NULL)
2781 : : {
2782 : : if (unlikely (futimens (fd, tvp) != 0))
2783 : : {
2784 : : error (0, errno, _("\
2785 : : cannot set access and modification date of '%s'"), fname);
2786 : : result = 1;
2787 : : }
2788 : : }
2789 : :
2790 : : if (unlikely (close (fd) != 0))
2791 : : error_exit (errno, _("while closing '%s'"), fname);
2792 : :
2793 : : return result;
2794 : : }
2795 : :
2796 : :
2797 : : #include "debugpred.h"
|