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