Branch data Line data Source code
1 : : /* Combine stripped files with separate symbols and debug information.
2 : : Copyright (C) 2007-2012, 2014, 2015 Red Hat, Inc.
3 : : This file is part of elfutils.
4 : : Written by Roland McGrath <roland@redhat.com>, 2007.
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 : : /* TODO:
20 : :
21 : : * SHX_XINDEX
22 : :
23 : : * prelink vs .debug_* linked addresses
24 : :
25 : : */
26 : :
27 : : #ifdef HAVE_CONFIG_H
28 : : # include <config.h>
29 : : #endif
30 : :
31 : : #include <argp.h>
32 : : #include <assert.h>
33 : : #include <errno.h>
34 : : #include <fcntl.h>
35 : : #include <fnmatch.h>
36 : : #include <locale.h>
37 : : #include <stdbool.h>
38 : : #include <stdio.h>
39 : : #include <stdio_ext.h>
40 : : #include <inttypes.h>
41 : : #include <stdlib.h>
42 : : #include <string.h>
43 : : #include <unistd.h>
44 : : #include <sys/stat.h>
45 : :
46 : : #include <gelf.h>
47 : : #include <libebl.h>
48 : : #include <libdwfl.h>
49 : : #include "system.h"
50 : : #include "libdwelf.h"
51 : : #include "libeu.h"
52 : : #include "printversion.h"
53 : :
54 : : /* Name and version of program. */
55 : : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
56 : :
57 : : /* Bug report address. */
58 : : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
59 : :
60 : : /* Definitions of arguments for argp functions. */
61 : : static const struct argp_option options[] =
62 : : {
63 : : /* Group 2 will follow group 1 from dwfl_standard_argp. */
64 : : { "match-file-names", 'f', NULL, 0,
65 : : N_("Match MODULE against file names, not module names"), 2 },
66 : : { "ignore-missing", 'i', NULL, 0, N_("Silently skip unfindable files"), 0 },
67 : :
68 : : { NULL, 0, NULL, 0, N_("Output options:"), 0 },
69 : : { "output", 'o', "FILE", 0, N_("Place output into FILE"), 0 },
70 : : { "output-directory", 'd', "DIRECTORY",
71 : : 0, N_("Create multiple output files under DIRECTORY"), 0 },
72 : : { "module-names", 'm', NULL, 0, N_("Use module rather than file names"), 0 },
73 : : { "all", 'a', NULL, 0,
74 : : N_("Create output for modules that have no separate debug information"),
75 : : 0 },
76 : : { "relocate", 'R', NULL, 0,
77 : : N_("Apply relocations to section contents in ET_REL files"), 0 },
78 : : { "list-only", 'n', NULL, 0,
79 : : N_("Only list module and file names, build IDs"), 0 },
80 : : { "force", 'F', NULL, 0,
81 : : N_("Force combining files even if some ELF headers don't seem to match"),
82 : : 0 },
83 : : { NULL, 0, NULL, 0, NULL, 0 }
84 : : };
85 : :
86 : : struct arg_info
87 : : {
88 : : const char *output_file;
89 : : const char *output_dir;
90 : : Dwfl *dwfl;
91 : : char **args;
92 : : bool list;
93 : : bool all;
94 : : bool ignore;
95 : : bool modnames;
96 : : bool match_files;
97 : : bool relocate;
98 : : bool force;
99 : : };
100 : :
101 : : /* Handle program arguments. */
102 : : static error_t
103 : 348 : parse_opt (int key, char *arg, struct argp_state *state)
104 : : {
105 : 348 : struct arg_info *info = state->input;
106 : :
107 [ + + - - : 348 : switch (key)
- - - + -
- + + ]
108 : : {
109 : 60 : case ARGP_KEY_INIT:
110 : 60 : state->child_inputs[0] = &info->dwfl;
111 : 60 : break;
112 : :
113 : 38 : case 'o':
114 [ - + ]: 38 : if (info->output_file != NULL)
115 : : {
116 : 0 : argp_error (state, _("-o option specified twice"));
117 : 0 : return EINVAL;
118 : : }
119 : 38 : info->output_file = arg;
120 : 38 : break;
121 : :
122 : 0 : case 'd':
123 [ # # ]: 0 : if (info->output_dir != NULL)
124 : : {
125 : 0 : argp_error (state, _("-d option specified twice"));
126 : 0 : return EINVAL;
127 : : }
128 : 0 : info->output_dir = arg;
129 : 0 : break;
130 : :
131 : 0 : case 'm':
132 : 0 : info->modnames = true;
133 : 0 : break;
134 : 0 : case 'f':
135 : 0 : info->match_files = true;
136 : 0 : break;
137 : 0 : case 'a':
138 : 0 : info->all = true;
139 : 0 : break;
140 : 0 : case 'i':
141 : 0 : info->ignore = true;
142 : 0 : break;
143 : 10 : case 'n':
144 : 10 : info->list = true;
145 : 10 : break;
146 : 0 : case 'R':
147 : 0 : info->relocate = true;
148 : 0 : break;
149 : 0 : case 'F':
150 : 0 : info->force = true;
151 : 0 : break;
152 : :
153 : 60 : case ARGP_KEY_ARGS:
154 : : case ARGP_KEY_NO_ARGS:
155 : : /* We "consume" all the arguments here. */
156 : 60 : info->args = &state->argv[state->next];
157 : :
158 [ + + - + ]: 60 : if (info->output_file != NULL && info->output_dir != NULL)
159 : : {
160 : 0 : argp_error (state, _("only one of -o or -d allowed"));
161 : 0 : return EINVAL;
162 : : }
163 : :
164 [ + + + - ]: 60 : if (info->list && (info->dwfl == NULL
165 [ + - ]: 10 : || info->output_dir != NULL
166 [ - + ]: 10 : || info->output_file != NULL))
167 : : {
168 : 0 : argp_error (state,
169 : 0 : _("-n cannot be used with explicit files or -o or -d"));
170 : 0 : return EINVAL;
171 : : }
172 : :
173 [ - + ]: 60 : if (info->output_dir != NULL)
174 : : {
175 : 0 : struct stat st;
176 : 0 : error_t fail = 0;
177 [ # # ]: 0 : if (stat (info->output_dir, &st) < 0)
178 : 0 : fail = errno;
179 [ # # ]: 0 : else if (!S_ISDIR (st.st_mode))
180 : : fail = ENOTDIR;
181 [ # # ]: 0 : if (fail)
182 : : {
183 : 0 : argp_failure (state, EXIT_FAILURE, fail,
184 : 0 : _("output directory '%s'"), info->output_dir);
185 : 0 : return fail;
186 : : }
187 : : }
188 : :
189 [ + + ]: 60 : if (info->dwfl == NULL)
190 : : {
191 [ - + ]: 50 : if (state->next + 2 != state->argc)
192 : : {
193 : 0 : argp_error (state, _("exactly two file arguments are required"));
194 : 0 : return EINVAL;
195 : : }
196 : :
197 [ + - + - : 50 : if (info->ignore || info->all || info->modnames || info->relocate)
+ - - + ]
198 : : {
199 : 0 : argp_error (state, _("\
200 : : -m, -a, -R, and -i options not allowed with explicit files"));
201 : 0 : return EINVAL;
202 : : }
203 : :
204 : : /* Bail out immediately to prevent dwfl_standard_argp's parser
205 : : from defaulting to "-e a.out". */
206 : : return ENOSYS;
207 : : }
208 [ + - + - ]: 10 : else if (info->output_file == NULL && info->output_dir == NULL
209 [ - + ]: 10 : && !info->list)
210 : : {
211 : 0 : argp_error (state,
212 : 0 : _("-o or -d is required when using implicit files"));
213 : 0 : return EINVAL;
214 : : }
215 : : break;
216 : :
217 : : default:
218 : : return ARGP_ERR_UNKNOWN;
219 : : }
220 : : return 0;
221 : : }
222 : :
223 : : #define ELF_CHECK(call, msg) \
224 : : do \
225 : : { \
226 : : if (unlikely (!(call))) \
227 : : error_exit (0, msg, elf_errmsg (-1)); \
228 : : } while (0)
229 : :
230 : : /* Copy INELF to newly-created OUTELF, exit via error for any problems. */
231 : : static void
232 : 38 : copy_elf (Elf *outelf, Elf *inelf)
233 : : {
234 [ - + ]: 38 : ELF_CHECK (gelf_newehdr (outelf, gelf_getclass (inelf)),
235 : : _("cannot create ELF header: %s"));
236 : :
237 : 38 : size_t shstrndx;
238 [ - + ]: 38 : ELF_CHECK (elf_getshdrstrndx (inelf, &shstrndx) == 0,
239 : : _("cannot get shdrstrndx:%s"));
240 : :
241 : 38 : GElf_Ehdr ehdr_mem;
242 : 38 : GElf_Ehdr *ehdr = gelf_getehdr (inelf, &ehdr_mem);
243 [ - + ]: 38 : ELF_CHECK (ehdr != NULL, _("cannot get ELF header: %s"));
244 [ + + ]: 38 : if (shstrndx < SHN_LORESERVE)
245 : 36 : ehdr->e_shstrndx = shstrndx;
246 : : else
247 : : {
248 : 2 : ehdr->e_shstrndx = SHN_XINDEX;
249 : 2 : Elf_Scn *scn0 = elf_getscn (outelf, 0);
250 : 2 : GElf_Shdr shdr0_mem;
251 : 2 : GElf_Shdr *shdr0 = gelf_getshdr (scn0, &shdr0_mem);
252 [ - + ]: 2 : ELF_CHECK (shdr0 != NULL,
253 : : _("cannot get new zero section: %s"));
254 : 2 : shdr0->sh_link = shstrndx;
255 [ - + ]: 2 : ELF_CHECK (gelf_update_shdr (scn0, shdr0),
256 : : _("cannot update new zero section: %s"));
257 : : }
258 : :
259 [ - + ]: 38 : ELF_CHECK (gelf_update_ehdr (outelf, ehdr),
260 : : _("cannot copy ELF header: %s"));
261 : :
262 : 38 : size_t phnum;
263 [ - + ]: 38 : ELF_CHECK (elf_getphdrnum (inelf, &phnum) == 0,
264 : : _("cannot get number of program headers: %s"));
265 : :
266 [ + + ]: 38 : if (phnum > 0)
267 : : {
268 [ - + ]: 18 : ELF_CHECK (gelf_newphdr (outelf, phnum),
269 : : _("cannot create program headers: %s"));
270 : :
271 : : GElf_Phdr phdr_mem;
272 [ + + ]: 148 : for (size_t i = 0; i < phnum; ++i)
273 [ - + ]: 130 : ELF_CHECK (gelf_update_phdr (outelf, i,
274 : : gelf_getphdr (inelf, i, &phdr_mem)),
275 : : _("cannot copy program header: %s"));
276 : : }
277 : :
278 : 38 : Elf_Scn *scn = NULL;
279 [ + + ]: 133084 : while ((scn = elf_nextscn (inelf, scn)) != NULL)
280 : : {
281 : 133046 : Elf_Scn *newscn = elf_newscn (outelf);
282 : :
283 : 133046 : GElf_Shdr shdr_mem;
284 [ - + ]: 133046 : ELF_CHECK (gelf_update_shdr (newscn, gelf_getshdr (scn, &shdr_mem)),
285 : : _("cannot copy section header: %s"));
286 : :
287 : 133046 : Elf_Data *data = elf_getdata (scn, NULL);
288 [ - + ]: 133046 : ELF_CHECK (data != NULL, _("cannot get section data: %s"));
289 : 133046 : Elf_Data *newdata = elf_newdata (newscn);
290 [ - + ]: 133046 : ELF_CHECK (newdata != NULL, _("cannot copy section data: %s"));
291 : 133046 : *newdata = *data;
292 : 133046 : elf_flagdata (newdata, ELF_C_SET, ELF_F_DIRTY);
293 : : }
294 : 38 : }
295 : :
296 : : /* Create directories containing PATH. */
297 : : static void
298 : 0 : make_directories (const char *path)
299 : : {
300 : 0 : const char *lastslash = strrchr (path, '/');
301 [ # # ]: 0 : if (lastslash == NULL)
302 : : return;
303 : :
304 [ # # # # ]: 0 : while (lastslash > path && lastslash[-1] == '/')
305 : 0 : --lastslash;
306 [ # # ]: 0 : if (lastslash == path)
307 : : return;
308 : :
309 : 0 : char *dir = strndup (path, lastslash - path);
310 [ # # ]: 0 : if (dir == NULL)
311 : 0 : error(EXIT_FAILURE, errno, _("memory exhausted"));
312 : :
313 [ # # # # ]: 0 : while (mkdir (dir, ACCESSPERMS) < 0 && errno != EEXIST)
314 : : {
315 [ # # ]: 0 : if (errno == ENOENT)
316 : 0 : make_directories (dir);
317 : : else
318 : 0 : error_exit (errno, _("cannot create directory '%s'"), dir);
319 : : }
320 : 0 : free (dir);
321 : : }
322 : :
323 : : /* Keep track of new section data we are creating, so we can free it
324 : : when done. */
325 : : struct data_list
326 : : {
327 : : void *data;
328 : : struct data_list *next;
329 : : };
330 : :
331 : : struct data_list *new_data_list;
332 : :
333 : : static void
334 : 38 : record_new_data (void *data)
335 : : {
336 : 38 : struct data_list *next = new_data_list;
337 : 38 : new_data_list = xmalloc (sizeof (struct data_list));
338 : 38 : new_data_list->data = data;
339 : 38 : new_data_list->next = next;
340 : 38 : }
341 : :
342 : : static void
343 : 44 : free_new_data (void)
344 : : {
345 : 44 : struct data_list *list = new_data_list;
346 [ + + ]: 82 : while (list != NULL)
347 : : {
348 : 38 : struct data_list *next = list->next;
349 : 38 : free (list->data);
350 : 38 : free (list);
351 : 38 : list = next;
352 : : }
353 : 44 : new_data_list = NULL;
354 : 44 : }
355 : :
356 : : /* The binutils linker leaves gratuitous section symbols in .symtab
357 : : that strip has to remove. Older linkers likewise include a
358 : : symbol for every section, even unallocated ones, in .dynsym.
359 : : Because of this, the related sections can shrink in the stripped
360 : : file from their original size. Older versions of strip do not
361 : : adjust the sh_size field in the debuginfo file's SHT_NOBITS
362 : : version of the section header, so it can appear larger. */
363 : : static bool
364 : 14 : section_can_shrink (const GElf_Shdr *shdr)
365 : : {
366 [ - + ]: 14 : switch (shdr->sh_type)
367 : : {
368 : : case SHT_SYMTAB:
369 : : case SHT_DYNSYM:
370 : : case SHT_HASH:
371 : : case SHT_GNU_versym:
372 : : return true;
373 : : }
374 : 0 : return false;
375 : : }
376 : :
377 : : /* See if this symbol table has a leading section symbol for every single
378 : : section, in order. The binutils linker produces this. While we're here,
379 : : update each section symbol's st_value. */
380 : : static size_t
381 : 28 : symtab_count_leading_section_symbols (Elf *elf, Elf_Scn *scn, size_t shnum,
382 : : Elf_Data *newsymdata)
383 : : {
384 : 28 : Elf_Data *data = elf_getdata (scn, NULL);
385 : 28 : Elf_Data *shndxdata = NULL; /* XXX */
386 : :
387 [ + - ]: 192 : for (size_t i = 1; i < shnum; ++i)
388 : : {
389 : 164 : GElf_Sym sym_mem;
390 : 164 : GElf_Word shndx = SHN_UNDEF;
391 : 164 : GElf_Sym *sym = gelf_getsymshndx (data, shndxdata, i, &sym_mem, &shndx);
392 [ - + ]: 164 : ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s"));
393 : :
394 : 164 : GElf_Shdr shdr_mem;
395 : 164 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (elf, i), &shdr_mem);
396 [ - + ]: 164 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
397 : :
398 [ + - ]: 164 : if (sym->st_shndx != SHN_XINDEX)
399 : 164 : shndx = sym->st_shndx;
400 : :
401 [ + + - + ]: 164 : if (shndx != i || GELF_ST_TYPE (sym->st_info) != STT_SECTION)
402 : 28 : return i;
403 : :
404 : 136 : sym->st_value = shdr->sh_addr;
405 [ + - ]: 136 : if (sym->st_shndx != SHN_XINDEX)
406 : 136 : shndx = SHN_UNDEF;
407 [ - + ]: 136 : ELF_CHECK (gelf_update_symshndx (newsymdata, shndxdata, i, sym, shndx),
408 : : _("cannot update symbol table: %s"));
409 : : }
410 : :
411 : : return shnum;
412 : : }
413 : :
414 : : static void
415 : 135048 : update_shdr (Elf_Scn *outscn, GElf_Shdr *newshdr)
416 : : {
417 [ - + ]: 135048 : ELF_CHECK (gelf_update_shdr (outscn, newshdr),
418 : : _("cannot update section header: %s"));
419 : 135048 : }
420 : :
421 : : /* We expanded the output section, so update its header. */
422 : : static void
423 : 12 : update_sh_size (Elf_Scn *outscn, const Elf_Data *data)
424 : : {
425 : 12 : GElf_Shdr shdr_mem;
426 : 12 : GElf_Shdr *newshdr = gelf_getshdr (outscn, &shdr_mem);
427 [ - + ]: 12 : ELF_CHECK (newshdr != NULL, _("cannot get section header: %s"));
428 : :
429 : 12 : newshdr->sh_size = data->d_size;
430 : :
431 : 12 : update_shdr (outscn, newshdr);
432 : 12 : }
433 : :
434 : : static inline void
435 : 62520 : adjust_reloc (GElf_Xword *info,
436 : : size_t map[], size_t map_size)
437 : : {
438 : 62520 : size_t ndx = GELF_R_SYM (*info);
439 [ + + ]: 62520 : if (ndx != STN_UNDEF)
440 : : {
441 [ - + ]: 62508 : if (ndx > map_size)
442 : 0 : error_exit (0, "bad symbol ndx section");
443 : 62508 : *info = GELF_R_INFO (map[ndx - 1], GELF_R_TYPE (*info));
444 : : }
445 : 62520 : }
446 : :
447 : : /* Update relocation sections using the symbol table. */
448 : : static void
449 : 782 : adjust_relocs (Elf_Scn *outscn, Elf_Scn *inscn, const GElf_Shdr *shdr,
450 : : size_t map[], size_t map_size, const GElf_Shdr *symshdr)
451 : : {
452 : 782 : Elf_Data *data = elf_getdata (outscn, NULL);
453 : :
454 [ + + + + : 782 : switch (shdr->sh_type)
+ - ]
455 : : {
456 : 32 : case SHT_REL:
457 [ - + ]: 32 : if (shdr->sh_entsize == 0)
458 : 0 : error_exit (0, "REL section cannot have zero sh_entsize");
459 : :
460 [ + + ]: 3736 : for (size_t i = 0; i < shdr->sh_size / shdr->sh_entsize; ++i)
461 : : {
462 : 3704 : GElf_Rel rel_mem;
463 : 3704 : GElf_Rel *rel = gelf_getrel (data, i, &rel_mem);
464 [ - + ]: 3704 : ELF_CHECK (rel != NULL, _("gelf_getrel failed: %s"));
465 : 3704 : adjust_reloc (&rel->r_info, map, map_size);
466 [ - + ]: 3704 : ELF_CHECK (gelf_update_rel (data, i, rel),
467 : : _("cannot update relocation: %s"));
468 : : }
469 : : break;
470 : :
471 : 424 : case SHT_RELA:
472 [ - + ]: 424 : if (shdr->sh_entsize == 0)
473 : 0 : error_exit (0, "RELA section cannot have zero sh_entsize");
474 : :
475 [ + + ]: 59240 : for (size_t i = 0; i < shdr->sh_size / shdr->sh_entsize; ++i)
476 : : {
477 : 58816 : GElf_Rela rela_mem;
478 : 58816 : GElf_Rela *rela = gelf_getrela (data, i, &rela_mem);
479 [ - + ]: 58816 : ELF_CHECK (rela != NULL, _("gelf_getrela failed: %s"));
480 : 58816 : adjust_reloc (&rela->r_info, map, map_size);
481 [ - + ]: 58816 : ELF_CHECK (gelf_update_rela (data, i, rela),
482 : : _("cannot update relocation: %s"));
483 : : }
484 : : break;
485 : :
486 : 314 : case SHT_GROUP:
487 : : {
488 : 314 : GElf_Shdr shdr_mem;
489 : 314 : GElf_Shdr *newshdr = gelf_getshdr (outscn, &shdr_mem);
490 [ - + ]: 314 : ELF_CHECK (newshdr != NULL, _("cannot get section header: %s"));
491 [ + - ]: 314 : if (newshdr->sh_info != STN_UNDEF)
492 : : {
493 : 314 : newshdr->sh_info = map[newshdr->sh_info - 1];
494 : 314 : update_shdr (outscn, newshdr);
495 : : }
496 : 314 : break;
497 : : }
498 : :
499 : 6 : case SHT_HASH:
500 : : /* We must expand the table and rejigger its contents. */
501 : : {
502 [ - + ]: 6 : if (shdr->sh_entsize == 0)
503 : 0 : error_exit (0, "HASH section cannot have zero sh_entsize");
504 [ - + ]: 6 : if (symshdr->sh_entsize == 0)
505 : 0 : error_exit (0, "Symbol table cannot have zero sh_entsize");
506 : 6 : const size_t nsym = symshdr->sh_size / symshdr->sh_entsize;
507 : 6 : const size_t onent = shdr->sh_size / shdr->sh_entsize;
508 [ - + ]: 6 : if (data->d_size != shdr->sh_size)
509 : 0 : error_exit (0, "HASH section has inconsistent size");
510 : :
511 : : #define CONVERT_HASH(Hash_Word) \
512 : : { \
513 : : const Hash_Word *const old_hash = data->d_buf; \
514 : : const size_t nbucket = old_hash[0]; \
515 : : const size_t nchain = old_hash[1]; \
516 : : const Hash_Word *const old_bucket = &old_hash[2]; \
517 : : const Hash_Word *const old_chain = &old_bucket[nbucket]; \
518 : : if (onent != 2 + nbucket + nchain) \
519 : : error_exit (0, "HASH section has inconsistent entsize"); \
520 : : \
521 : : const size_t nent = 2 + nbucket + nsym; \
522 : : Hash_Word *const new_hash = xcalloc (nent, sizeof new_hash[0]); \
523 : : Hash_Word *const new_bucket = &new_hash[2]; \
524 : : Hash_Word *const new_chain = &new_bucket[nbucket]; \
525 : : \
526 : : new_hash[0] = nbucket; \
527 : : new_hash[1] = nsym; \
528 : : for (size_t i = 0; i < nbucket; ++i) \
529 : : if (old_bucket[i] != STN_UNDEF) \
530 : : new_bucket[i] = map[old_bucket[i] - 1]; \
531 : : \
532 : : for (size_t i = 1; i < nchain; ++i) \
533 : : if (old_chain[i] != STN_UNDEF) \
534 : : new_chain[map[i - 1]] = map[old_chain[i] - 1]; \
535 : : \
536 : : record_new_data (new_hash); \
537 : : data->d_buf = new_hash; \
538 : : data->d_size = nent * sizeof new_hash[0]; \
539 : : }
540 : :
541 [ + - - ]: 6 : switch (shdr->sh_entsize)
542 : : {
543 : 6 : case 4:
544 [ - + + + : 432 : CONVERT_HASH (Elf32_Word);
+ + + + +
+ ]
545 : 6 : break;
546 : 0 : case 8:
547 [ # # # # : 0 : CONVERT_HASH (Elf64_Xword);
# # # # #
# ]
548 : 0 : break;
549 : 0 : default:
550 : 0 : abort ();
551 : : }
552 : :
553 : 6 : elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY);
554 : 6 : update_sh_size (outscn, data);
555 : :
556 : : #undef CONVERT_HASH
557 : : }
558 : 6 : break;
559 : :
560 : 6 : case SHT_GNU_versym:
561 : : /* We must expand the table and move its elements around. */
562 : : {
563 [ - + ]: 6 : if (shdr->sh_entsize == 0)
564 : 0 : error_exit (0, "GNU_versym section cannot have zero sh_entsize");
565 [ - + ]: 6 : if (symshdr->sh_entsize == 0)
566 : 0 : error_exit (0, "Symbol table cannot have zero sh_entsize");
567 : 6 : const size_t nent = symshdr->sh_size / symshdr->sh_entsize;
568 : 6 : const size_t onent = shdr->sh_size / shdr->sh_entsize;
569 [ - + ]: 6 : assert (nent >= onent);
570 : :
571 : : /* We don't bother using gelf_update_versym because there is
572 : : really no conversion to be done. */
573 : 6 : eu_static_assert (sizeof (Elf32_Versym) == sizeof (GElf_Versym));
574 : 6 : eu_static_assert (sizeof (Elf64_Versym) == sizeof (GElf_Versym));
575 : 6 : GElf_Versym *versym = xcalloc (nent, sizeof versym[0]);
576 : :
577 [ + + ]: 216 : for (size_t i = 1; i < onent; ++i)
578 : : {
579 : 204 : GElf_Versym *v = gelf_getversym (data, i, &versym[map[i - 1]]);
580 [ - + ]: 204 : ELF_CHECK (v != NULL, _("cannot get symbol version: %s"));
581 : : }
582 : :
583 : 6 : record_new_data (versym);
584 : 6 : data->d_buf = versym;
585 : 6 : data->d_size = nent * sizeof versym[0];
586 : 6 : elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY);
587 : 6 : update_sh_size (outscn, data);
588 : : }
589 : 6 : break;
590 : :
591 : 0 : default:
592 : 0 : error_exit (0,
593 : : _("unexpected section type in [%zu] with sh_link to symtab"),
594 : : elf_ndxscn (inscn));
595 : : }
596 : 782 : }
597 : :
598 : : /* Adjust all the relocation sections in the file. */
599 : : static void
600 : 26 : adjust_all_relocs (Elf *elf, Elf_Scn *symtab, const GElf_Shdr *symshdr,
601 : : size_t map[], size_t map_size, bool scn_filter[])
602 : : {
603 : 26 : size_t new_sh_link = elf_ndxscn (symtab);
604 : 26 : Elf_Scn *scn = NULL;
605 [ + + ]: 1772 : while ((scn = elf_nextscn (elf, scn)) != NULL)
606 [ + + ]: 1746 : if (scn != symtab)
607 : : {
608 [ + + ]: 1720 : if (scn_filter != NULL)
609 : : {
610 : 1528 : size_t ndx = elf_ndxscn (scn);
611 : :
612 : : /* Skip relocations that were already mapped during adjust_relocs
613 : : for the stripped symtab. This is to avoid mapping a relocation's
614 : : symbol index from X to Y during the first adjust_relocs and then
615 : : wrongly remapping it from Y to Z during the second call. */
616 [ + + ]: 1528 : if (scn_filter[ndx])
617 : 90 : continue;
618 : : }
619 : :
620 : 1630 : GElf_Shdr shdr_mem;
621 : 1630 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
622 [ - + ]: 1630 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
623 : :
624 [ + + + + ]: 1630 : if (shdr->sh_type != SHT_NOBITS && shdr->sh_link == new_sh_link)
625 : 692 : adjust_relocs (scn, scn, shdr, map, map_size, symshdr);
626 : : }
627 : 26 : }
628 : :
629 : : /* The original file probably had section symbols for all of its
630 : : sections, even the unallocated ones. To match it as closely as
631 : : possible, add in section symbols for the added sections. */
632 : : static Elf_Data *
633 : 6 : add_new_section_symbols (Elf_Scn *old_symscn, size_t old_shnum,
634 : : Elf *elf, bool rel, Elf_Scn *symscn, size_t shnum)
635 : 6 : {
636 : 6 : const size_t added = shnum - old_shnum;
637 : :
638 : 6 : GElf_Shdr shdr_mem;
639 : 6 : GElf_Shdr *shdr = gelf_getshdr (symscn, &shdr_mem);
640 [ - + ]: 6 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
641 [ + - - + ]: 6 : if (shdr->sh_entsize == 0 || shdr->sh_size < shdr->sh_entsize)
642 : 0 : error_exit (0, _("Symbol table section cannot have zero sh_entsize"
643 : : " or a sh_size smaller than sh_entsize"));
644 : :
645 : 6 : const size_t nsym = shdr->sh_size / shdr->sh_entsize;
646 : 6 : size_t symndx_map[nsym - 1];
647 : :
648 : 6 : shdr->sh_info += added;
649 : 6 : shdr->sh_size += added * shdr->sh_entsize;
650 : 6 : update_shdr (symscn, shdr);
651 : :
652 : 6 : Elf_Data *symdata = elf_getdata (symscn, NULL);
653 : 6 : Elf_Data *shndxdata = NULL; /* XXX */
654 : :
655 : 6 : symdata->d_size = shdr->sh_size;
656 : 6 : symdata->d_buf = xmalloc (symdata->d_size);
657 : 6 : record_new_data (symdata->d_buf);
658 : :
659 : : /* Copy the existing section symbols. */
660 : 6 : Elf_Data *old_symdata = elf_getdata (old_symscn, NULL);
661 [ + + ]: 150 : for (size_t i = 0; i < old_shnum; ++i)
662 : : {
663 : 138 : GElf_Sym sym_mem;
664 : 138 : GElf_Word shndx = SHN_UNDEF;
665 : 138 : GElf_Sym *sym = gelf_getsymshndx (old_symdata, shndxdata,
666 : : i, &sym_mem, &shndx);
667 [ - + ]: 138 : ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s"));
668 [ - + ]: 138 : ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, i,
669 : : sym, shndx),
670 : : _("cannot update symbol table: %s"));
671 : :
672 [ + + ]: 138 : if (i > 0)
673 : 132 : symndx_map[i - 1] = i;
674 : : }
675 : :
676 : : /* Add in the new section symbols. */
677 [ + + ]: 54 : for (size_t i = old_shnum; i < shnum; ++i)
678 : : {
679 : 48 : GElf_Shdr i_shdr_mem;
680 : 48 : GElf_Shdr *i_shdr = gelf_getshdr (elf_getscn (elf, i), &i_shdr_mem);
681 [ - + ]: 48 : ELF_CHECK (i_shdr != NULL, _("cannot get section header: %s"));
682 [ + - ]: 48 : GElf_Sym sym =
683 : : {
684 [ + - ]: 48 : .st_value = rel ? 0 : i_shdr->sh_addr,
685 : : .st_info = GELF_ST_INFO (STB_LOCAL, STT_SECTION),
686 : : .st_shndx = i < SHN_LORESERVE ? i : SHN_XINDEX
687 : : };
688 [ - + ]: 48 : GElf_Word shndx = i < SHN_LORESERVE ? SHN_UNDEF : i;
689 [ - + ]: 48 : ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, i,
690 : : &sym, shndx),
691 : : _("cannot update symbol table: %s"));
692 : : }
693 : :
694 : : /* Now copy the rest of the existing symbols. */
695 [ + + ]: 78 : for (size_t i = old_shnum; i < nsym; ++i)
696 : : {
697 : 72 : GElf_Sym sym_mem;
698 : 72 : GElf_Word shndx = SHN_UNDEF;
699 : 72 : GElf_Sym *sym = gelf_getsymshndx (old_symdata, shndxdata,
700 : : i, &sym_mem, &shndx);
701 [ - + ]: 72 : ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s"));
702 [ - + ]: 72 : ELF_CHECK (gelf_update_symshndx (symdata, shndxdata,
703 : : i + added, sym, shndx),
704 : : _("cannot update symbol table: %s"));
705 : :
706 : 72 : symndx_map[i - 1] = i + added;
707 : : }
708 : :
709 : : /* Adjust any relocations referring to the old symbol table. */
710 : 6 : adjust_all_relocs (elf, symscn, shdr, symndx_map, nsym - 1, NULL);
711 : :
712 : 6 : return symdata;
713 : : }
714 : :
715 : : /* This has the side effect of updating STT_SECTION symbols' values,
716 : : in case of prelink adjustments. */
717 : : static Elf_Data *
718 : 28 : check_symtab_section_symbols (Elf *elf, bool rel, Elf_Scn *scn,
719 : : size_t shnum, size_t shstrndx,
720 : : Elf_Scn *oscn, size_t oshnum, size_t oshstrndx,
721 : : size_t debuglink)
722 : : {
723 : 28 : size_t n = symtab_count_leading_section_symbols (elf, oscn, oshnum,
724 : : elf_getdata (scn, NULL));
725 : :
726 [ - + ]: 28 : if (n == oshnum)
727 : 0 : return add_new_section_symbols (oscn, n, elf, rel, scn, shnum);
728 : :
729 [ + - + + : 28 : if (n == oshstrndx || (n == debuglink && n == oshstrndx - 1))
+ - ]
730 : 6 : return add_new_section_symbols (oscn, n, elf, rel, scn, shstrndx);
731 : :
732 : : return NULL;
733 : : }
734 : :
735 : : struct section
736 : : {
737 : : Elf_Scn *scn;
738 : : const char *name;
739 : : const char *sig;
740 : : Elf_Scn *outscn;
741 : : Dwelf_Strent *strent;
742 : : GElf_Shdr shdr;
743 : : };
744 : :
745 : : static int
746 : 1650 : compare_alloc_sections (const struct section *s1, const struct section *s2,
747 : : bool rel)
748 : : {
749 [ + + ]: 1650 : if (!rel)
750 : : {
751 : : /* Sort by address. */
752 [ + + ]: 1370 : if (s1->shdr.sh_addr < s2->shdr.sh_addr)
753 : : return -1;
754 [ + - ]: 4 : if (s1->shdr.sh_addr > s2->shdr.sh_addr)
755 : : return 1;
756 : : }
757 : :
758 : : /* At the same address, preserve original section order. */
759 : 284 : return (ssize_t) elf_ndxscn (s1->scn) - (ssize_t) elf_ndxscn (s2->scn);
760 : : }
761 : :
762 : : static int
763 : 398442 : compare_unalloc_sections (const GElf_Shdr *shdr1, const GElf_Shdr *shdr2,
764 : : const char *name1, const char *name2,
765 : : const char *sig1, const char *sig2)
766 : : {
767 : : /* Sort by sh_flags as an arbitrary ordering. */
768 [ + + ]: 398442 : if (shdr1->sh_flags < shdr2->sh_flags)
769 : : return -1;
770 [ + + ]: 397934 : if (shdr1->sh_flags > shdr2->sh_flags)
771 : : return 1;
772 : :
773 : : /* Sizes should be the same. */
774 [ + + ]: 395780 : if (shdr1->sh_size < shdr2->sh_size)
775 : : return -1;
776 [ + + ]: 1036 : if (shdr1->sh_size > shdr2->sh_size)
777 : : return 1;
778 : :
779 : : /* Are they both SHT_GROUP sections? Then compare signatures. */
780 [ + + ]: 224 : if (sig1 != NULL && sig2 != NULL)
781 : 22 : return strcmp (sig1, sig2);
782 : :
783 : : /* Sort by name as last resort. */
784 : 202 : return strcmp (name1, name2);
785 : : }
786 : :
787 : : static int
788 : 2688 : compare_sections (const void *a, const void *b, bool rel)
789 : : {
790 : 2688 : const struct section *s1 = a;
791 : 2688 : const struct section *s2 = b;
792 : :
793 : : /* Sort all non-allocated sections last. */
794 [ + + ]: 2688 : if ((s1->shdr.sh_flags ^ s2->shdr.sh_flags) & SHF_ALLOC)
795 [ + + ]: 416 : return (s1->shdr.sh_flags & SHF_ALLOC) ? -1 : 1;
796 : :
797 : 2272 : return ((s1->shdr.sh_flags & SHF_ALLOC)
798 : 1650 : ? compare_alloc_sections (s1, s2, rel)
799 [ + + ]: 2272 : : compare_unalloc_sections (&s1->shdr, &s2->shdr,
800 : 622 : s1->name, s2->name,
801 : 622 : s1->sig, s2->sig));
802 : : }
803 : :
804 : : static int
805 : 1144 : compare_sections_rel (const void *a, const void *b)
806 : : {
807 : 1144 : return compare_sections (a, b, true);
808 : : }
809 : :
810 : : static int
811 : 1544 : compare_sections_nonrel (const void *a, const void *b)
812 : : {
813 : 1544 : return compare_sections (a, b, false);
814 : : }
815 : :
816 : :
817 : : struct symbol
818 : : {
819 : : size_t *map;
820 : :
821 : : union
822 : : {
823 : : const char *name;
824 : : Dwelf_Strent *strent;
825 : : };
826 : : union
827 : : {
828 : : struct
829 : : {
830 : : GElf_Addr value;
831 : : GElf_Xword size;
832 : : GElf_Word shndx;
833 : : union
834 : : {
835 : : struct
836 : : {
837 : : uint8_t info;
838 : : uint8_t other;
839 : : } info;
840 : : int16_t compare;
841 : : };
842 : : };
843 : :
844 : : /* For a symbol discarded after first sort, this matches its better's
845 : : map pointer. */
846 : : size_t *duplicate;
847 : : };
848 : : };
849 : :
850 : : /* Collect input symbols into our internal form. */
851 : : static void
852 : 40 : collect_symbols (Elf *outelf, bool rel, Elf_Scn *symscn, Elf_Scn *strscn,
853 : : const size_t nent, const GElf_Addr bias,
854 : : const size_t scnmap[], struct symbol *table, size_t *map,
855 : : struct section *split_bss)
856 : : {
857 : 40 : Elf_Data *symdata = elf_getdata (symscn, NULL);
858 [ - + ]: 40 : ELF_CHECK (symdata != NULL, _("cannot get symbol section data: %s"));
859 : 40 : Elf_Data *strdata = elf_getdata (strscn, NULL);
860 [ - + ]: 40 : ELF_CHECK (strdata != NULL, _("cannot get string section data: %s"));
861 : : Elf_Data *shndxdata = NULL; /* XXX */
862 : :
863 [ + + ]: 4072 : for (size_t i = 1; i < nent; ++i)
864 : : {
865 : 4032 : GElf_Sym sym_mem;
866 : 4032 : GElf_Word shndx = SHN_UNDEF;
867 : 4032 : GElf_Sym *sym = gelf_getsymshndx (symdata, shndxdata, i,
868 : : &sym_mem, &shndx);
869 [ - + ]: 4032 : ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s"));
870 [ + - ]: 4032 : if (sym->st_shndx != SHN_XINDEX)
871 : 4032 : shndx = sym->st_shndx;
872 : :
873 [ + - ]: 4032 : if (sym->st_name >= strdata->d_size
874 [ - + ]: 4032 : || memrchr (strdata->d_buf + sym->st_name, '\0',
875 : : strdata->d_size - sym->st_name) == NULL)
876 : 0 : error_exit (0,
877 : : _("invalid string offset in symbol [%zu]"), i);
878 : :
879 : 4032 : struct symbol *s = &table[i - 1];
880 : 4032 : s->map = &map[i - 1];
881 : 4032 : s->name = strdata->d_buf + sym->st_name;
882 : 4032 : s->value = sym->st_value + bias;
883 : 4032 : s->size = sym->st_size;
884 : 4032 : s->shndx = shndx;
885 : 4032 : s->info.info = sym->st_info;
886 : 4032 : s->info.other = sym->st_other;
887 : 4032 : s->duplicate = NULL;
888 : :
889 [ + + + + : 4032 : if (scnmap != NULL && shndx != SHN_UNDEF && shndx < SHN_LORESERVE)
+ + ]
890 : 762 : s->shndx = scnmap[shndx - 1];
891 : :
892 [ + + + + ]: 4032 : if (GELF_ST_TYPE (s->info.info) == STT_SECTION && !rel)
893 : 228 : {
894 : : /* Update the value to match the output section. */
895 : 228 : GElf_Shdr shdr_mem;
896 : 228 : GElf_Shdr *shdr = gelf_getshdr (elf_getscn (outelf, s->shndx),
897 : : &shdr_mem);
898 [ - + ]: 228 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
899 : 228 : s->value = shdr->sh_addr;
900 : : }
901 [ - + ]: 3804 : else if (split_bss != NULL
902 [ # # ]: 0 : && s->value < split_bss->shdr.sh_addr
903 [ # # ]: 0 : && s->value >= split_bss[-1].shdr.sh_addr
904 [ # # ]: 0 : && shndx == elf_ndxscn (split_bss->outscn))
905 : : /* This symbol was in .bss and was split into .dynbss. */
906 : 0 : s->shndx = elf_ndxscn (split_bss[-1].outscn);
907 : : }
908 : 40 : }
909 : :
910 : :
911 : : #define CMP(value) \
912 : : if (s1->value < s2->value) \
913 : : return -1; \
914 : : if (s1->value > s2->value) \
915 : : return 1
916 : :
917 : : /* Symbol comparison used to sort symbols in preparation for deduplication. */
918 : : static int
919 : 25230 : compare_symbols (const void *a, const void *b)
920 : : {
921 : 25230 : const struct symbol *s1 = a;
922 : 25230 : const struct symbol *s2 = b;
923 : :
924 [ + + + + ]: 25230 : CMP (value);
925 [ + + + + ]: 24382 : CMP (size);
926 [ + + + + ]: 19564 : CMP (shndx);
927 : :
928 : 10626 : int res = s1->compare - s2->compare;
929 [ + + ]: 10626 : if (res != 0)
930 : : return res;
931 : :
932 : 9874 : res = strcmp (s1->name, s2->name);
933 [ + + ]: 9874 : if (res != 0)
934 : : return res;
935 : :
936 : : /* Duplicates still have distinct positions in the symbol index map.
937 : : Compare map positions to ensure that duplicate symbols are ordered
938 : : consistently even if the sort function is unstable. */
939 [ - + - - ]: 1462 : CMP (map);
940 : 0 : error_exit (0, _("found two identical index map positions."));
941 : : }
942 : :
943 : : /* Symbol comparison used to deduplicate symbols found in both the stripped
944 : : and unstripped input files.
945 : :
946 : : Similar to compare_symbols, but does not differentiate symbols based
947 : : on their position in the symbol index map. Duplicates can't be found
948 : : by comparing index map postions because they always have distinct
949 : : positions in the map. */
950 : : static int
951 : 4008 : compare_symbols_duplicate (const void *a, const void *b)
952 : : {
953 : 4008 : const struct symbol *s1 = a;
954 : 4008 : const struct symbol *s2 = b;
955 : :
956 [ + + + - ]: 4008 : CMP (value);
957 [ + + + - ]: 3908 : CMP (size);
958 [ + + + - ]: 3568 : CMP (shndx);
959 : :
960 [ + + ]: 2554 : return (s1->compare - s2->compare) ?: strcmp (s1->name, s2->name);
961 : : }
962 : :
963 : : /* Compare symbols for output order after slots have been assigned. */
964 : : static int
965 : 26070 : compare_symbols_output (const void *a, const void *b)
966 : : {
967 : 26070 : const struct symbol *s1 = a;
968 : 26070 : const struct symbol *s2 = b;
969 : 26070 : int cmp;
970 : :
971 : : /* Sort discarded symbols last. */
972 : 26070 : cmp = (s1->name == NULL) - (s2->name == NULL);
973 : :
974 [ + + ]: 26070 : if (cmp == 0)
975 : : /* Local symbols must come first. */
976 : 21754 : cmp = ((GELF_ST_BIND (s2->info.info) == STB_LOCAL)
977 : 21754 : - (GELF_ST_BIND (s1->info.info) == STB_LOCAL));
978 : :
979 [ + + ]: 21754 : if (cmp == 0)
980 : : /* binutils always puts section symbols first. */
981 : 19152 : cmp = ((GELF_ST_TYPE (s2->info.info) == STT_SECTION)
982 : 19152 : - (GELF_ST_TYPE (s1->info.info) == STT_SECTION));
983 : :
984 [ + + ]: 23468 : if (cmp == 0)
985 : : {
986 [ + + ]: 16708 : if (GELF_ST_TYPE (s1->info.info) == STT_SECTION)
987 : : {
988 : : /* binutils always puts section symbols in section index order. */
989 [ + + - + ]: 2748 : CMP (shndx);
990 [ # # ]: 0 : else if (s1 != s2)
991 : 0 : error_exit (0, _("section symbols in unexpected order"));
992 : : }
993 : :
994 : : /* Nothing really matters, so preserve the original order. */
995 [ + + - + ]: 13960 : CMP (map);
996 [ # # ]: 0 : else if (s1 != s2)
997 : 0 : error_exit (0, _("found two identical symbols"));
998 : : }
999 : :
1000 : : return cmp;
1001 : : }
1002 : :
1003 : : #undef CMP
1004 : :
1005 : : /* Return true if the flags of the sections match, ignoring the SHF_INFO_LINK
1006 : : flag if the section contains relocation information. */
1007 : : static bool
1008 : 752 : sections_flags_match (Elf64_Xword sh_flags1, Elf64_Xword sh_flags2,
1009 : : Elf64_Word sh_type)
1010 : : {
1011 [ + + ]: 752 : if (sh_type == SHT_REL || sh_type == SHT_RELA)
1012 : : {
1013 : 50 : sh_flags1 &= ~SHF_INFO_LINK;
1014 : 50 : sh_flags2 &= ~SHF_INFO_LINK;
1015 : : }
1016 : :
1017 : 752 : return sh_flags1 == sh_flags2;
1018 : : }
1019 : :
1020 : : /* Return true iff the flags, size, and name match. */
1021 : : static bool
1022 : 752 : sections_match (const struct section *sections, size_t i,
1023 : : const GElf_Shdr *shdr, const char *name)
1024 : : {
1025 : 752 : return (sections_flags_match (sections[i].shdr.sh_flags, shdr->sh_flags,
1026 : 752 : sections[i].shdr.sh_type)
1027 [ + + ]: 748 : && (sections[i].shdr.sh_size == shdr->sh_size
1028 [ + - ]: 14 : || (sections[i].shdr.sh_size < shdr->sh_size
1029 [ + - ]: 14 : && section_can_shrink (§ions[i].shdr)))
1030 [ + + - + ]: 1500 : && !strcmp (sections[i].name, name));
1031 : : }
1032 : :
1033 : : /* Locate a matching allocated section in SECTIONS. */
1034 : : static struct section *
1035 : 590 : find_alloc_section (const GElf_Shdr *shdr, GElf_Addr bias, const char *name,
1036 : : struct section sections[], size_t nalloc)
1037 : : {
1038 : 590 : const GElf_Addr addr = shdr->sh_addr + bias;
1039 : 590 : size_t l = 0, u = nalloc;
1040 [ + - ]: 2864 : while (l < u)
1041 : : {
1042 : 2274 : size_t i = (l + u) / 2;
1043 [ + + ]: 2274 : if (addr < sections[i].shdr.sh_addr)
1044 : : u = i;
1045 [ + + ]: 1314 : else if (addr > sections[i].shdr.sh_addr)
1046 : 724 : l = i + 1;
1047 : : else
1048 : : {
1049 : : /* We've found allocated sections with this address.
1050 : : Find one with matching size, flags, and name. */
1051 [ + + + + ]: 598 : while (i > 0 && sections[i - 1].shdr.sh_addr == addr)
1052 : : --i;
1053 [ + - + - ]: 594 : for (; i < nalloc && sections[i].shdr.sh_addr == addr;
1054 : 4 : ++i)
1055 [ + + ]: 594 : if (sections_match (sections, i, shdr, name))
1056 : : return §ions[i];
1057 : : break;
1058 : : }
1059 : : }
1060 : : return NULL;
1061 : : }
1062 : :
1063 : : static inline const char *
1064 : 134254 : get_section_name (size_t ndx, const GElf_Shdr *shdr, const Elf_Data *shstrtab)
1065 : : {
1066 [ - + ]: 134254 : if (shdr->sh_name >= shstrtab->d_size)
1067 : 0 : error_exit (0, _("cannot read section [%zu] name: %s"),
1068 : : ndx, elf_errmsg (-1));
1069 : 134254 : return shstrtab->d_buf + shdr->sh_name;
1070 : : }
1071 : :
1072 : : /* Returns the signature of a group section, or NULL if the given
1073 : : section isn't a group. */
1074 : : static const char *
1075 : 133588 : get_group_sig (Elf *elf, GElf_Shdr *shdr)
1076 : : {
1077 [ + + ]: 133588 : if (shdr->sh_type != SHT_GROUP)
1078 : : return NULL;
1079 : :
1080 : 328 : Elf_Scn *symscn = elf_getscn (elf, shdr->sh_link);
1081 [ - + ]: 328 : if (symscn == NULL)
1082 : 0 : error_exit (0, _("bad sh_link for group section: %s"),
1083 : : elf_errmsg (-1));
1084 : :
1085 : 328 : GElf_Shdr symshdr_mem;
1086 : 328 : GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem);
1087 [ - + ]: 328 : if (symshdr == NULL)
1088 : 0 : error_exit (0, _("couldn't get shdr for group section: %s"),
1089 : : elf_errmsg (-1));
1090 : :
1091 : 328 : Elf_Data *symdata = elf_getdata (symscn, NULL);
1092 [ - + ]: 328 : if (symdata == NULL)
1093 : 0 : error_exit (0, _("bad data for group symbol section: %s"),
1094 : : elf_errmsg (-1));
1095 : :
1096 : 328 : GElf_Sym sym_mem;
1097 : 328 : GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
1098 [ - + ]: 328 : if (sym == NULL)
1099 : 0 : error_exit (0, _("couldn't get symbol for group section: %s"),
1100 : : elf_errmsg (-1));
1101 : :
1102 : 328 : const char *sig = elf_strptr (elf, symshdr->sh_link, sym->st_name);
1103 [ - + ]: 328 : if (sig == NULL)
1104 : 0 : error_exit (0, _("bad symbol name for group section: %s"),
1105 : : elf_errmsg (-1));
1106 : :
1107 : : return sig;
1108 : : }
1109 : :
1110 : : static inline bool
1111 : 0 : check_match (bool match, Elf_Scn *scn, const char *name)
1112 : : {
1113 [ # # ]: 0 : if (!match)
1114 : : {
1115 : 0 : error (0, 0, _("cannot find matching section for [%zu] '%s'"),
1116 : : elf_ndxscn (scn), name);
1117 : 0 : return true;
1118 : : }
1119 : :
1120 : : return false;
1121 : : }
1122 : :
1123 : :
1124 : : /* Fix things up when prelink has moved some allocated sections around
1125 : : and the debuginfo file's section headers no longer match up.
1126 : : This fills in SECTIONS[0..NALLOC-1].outscn or exits.
1127 : : If there was a .bss section that was split into two sections
1128 : : with the new one preceding it in sh_addr, we return that pointer. */
1129 : : static struct section *
1130 : 0 : find_alloc_sections_prelink (Elf *debug, Elf_Data *debug_shstrtab,
1131 : : Elf *main, const GElf_Ehdr *main_ehdr,
1132 : : Elf_Data *main_shstrtab, GElf_Addr bias,
1133 : : struct section *sections,
1134 : : size_t nalloc, size_t nsections)
1135 : : {
1136 : 0 : Elf_Scn *undo = NULL;
1137 [ # # ]: 0 : for (size_t i = nalloc; i < nsections; ++i)
1138 : : {
1139 : 0 : const struct section *sec = §ions[i];
1140 [ # # ]: 0 : if (sec->shdr.sh_type == SHT_PROGBITS
1141 [ # # ]: 0 : && !(sec->shdr.sh_flags & SHF_ALLOC)
1142 [ # # ]: 0 : && !strcmp (sec->name, ".gnu.prelink_undo"))
1143 : : {
1144 : 0 : undo = sec->scn;
1145 : 0 : break;
1146 : : }
1147 : : }
1148 : :
1149 : : /* Find the original allocated sections before prelinking. */
1150 : 0 : struct section *undo_sections = NULL;
1151 : 0 : size_t undo_nalloc = 0;
1152 [ # # ]: 0 : if (undo != NULL)
1153 : : {
1154 : : /* Clear assignments that might have been bogus. */
1155 [ # # ]: 0 : for (size_t i = 0; i < nalloc; ++i)
1156 : 0 : sections[i].outscn = NULL;
1157 : :
1158 : 0 : Elf_Data *undodata = elf_rawdata (undo, NULL);
1159 [ # # ]: 0 : ELF_CHECK (undodata != NULL,
1160 : : _("cannot read '.gnu.prelink_undo' section: %s"));
1161 : :
1162 : 0 : union
1163 : : {
1164 : : Elf32_Ehdr e32;
1165 : : Elf64_Ehdr e64;
1166 : : } ehdr;
1167 : 0 : Elf_Data dst =
1168 : : {
1169 : : .d_buf = &ehdr,
1170 : : .d_size = sizeof ehdr,
1171 : : .d_type = ELF_T_EHDR,
1172 : : .d_version = EV_CURRENT
1173 : : };
1174 : 0 : Elf_Data src = *undodata;
1175 : 0 : src.d_size = gelf_fsize (main, ELF_T_EHDR, 1, EV_CURRENT);
1176 : 0 : src.d_type = ELF_T_EHDR;
1177 [ # # ]: 0 : ELF_CHECK (gelf_xlatetom (main, &dst, &src,
1178 : : main_ehdr->e_ident[EI_DATA]) != NULL,
1179 : : _("cannot read '.gnu.prelink_undo' section: %s"));
1180 : :
1181 : 0 : uint_fast16_t phnum;
1182 : 0 : uint_fast16_t shnum; /* prelink doesn't handle > SHN_LORESERVE. */
1183 [ # # ]: 0 : if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
1184 : : {
1185 : 0 : phnum = ehdr.e32.e_phnum;
1186 : 0 : shnum = ehdr.e32.e_shnum;
1187 : : }
1188 : : else
1189 : : {
1190 : 0 : phnum = ehdr.e64.e_phnum;
1191 : 0 : shnum = ehdr.e64.e_shnum;
1192 : : }
1193 : :
1194 : 0 : bool class32 = ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32;
1195 [ # # ]: 0 : size_t shsize = class32 ? sizeof (Elf32_Shdr) : sizeof (Elf64_Shdr);
1196 [ # # # # ]: 0 : if (unlikely (shnum == 0 || shnum > SIZE_MAX / shsize + 1))
1197 : 0 : error_exit (0, _("overflow with shnum = %zu in '%s' section"),
1198 : : (size_t) shnum, ".gnu.prelink_undo");
1199 : :
1200 : 0 : --shnum;
1201 : :
1202 : 0 : size_t phsize = gelf_fsize (main, ELF_T_PHDR, phnum, EV_CURRENT);
1203 : 0 : src.d_buf += src.d_size + phsize;
1204 : 0 : src.d_size = gelf_fsize (main, ELF_T_SHDR, shnum, EV_CURRENT);
1205 : 0 : src.d_type = ELF_T_SHDR;
1206 [ # # ]: 0 : if ((size_t) (src.d_buf - undodata->d_buf) > undodata->d_size
1207 [ # # ]: 0 : || undodata->d_size - (src.d_buf - undodata->d_buf) != src.d_size)
1208 : 0 : error_exit (0, _("invalid contents in '%s' section"),
1209 : : ".gnu.prelink_undo");
1210 : :
1211 : 0 : const size_t shdr_bytes = shnum * shsize;
1212 : 0 : void *shdr = xmalloc (shdr_bytes);
1213 : 0 : dst.d_buf = shdr;
1214 : 0 : dst.d_size = shdr_bytes;
1215 [ # # ]: 0 : ELF_CHECK (gelf_xlatetom (main, &dst, &src,
1216 : : main_ehdr->e_ident[EI_DATA]) != NULL,
1217 : : _("cannot read '.gnu.prelink_undo' section: %s"));
1218 : :
1219 : 0 : undo_sections = xmalloc (shnum * sizeof undo_sections[0]);
1220 [ # # ]: 0 : for (size_t i = 0; i < shnum; ++i)
1221 : : {
1222 : 0 : struct section *sec = &undo_sections[undo_nalloc];
1223 : 0 : Elf32_Shdr (*s32)[shnum] = shdr;
1224 : 0 : Elf64_Shdr (*s64)[shnum] = shdr;
1225 [ # # ]: 0 : if (class32)
1226 : : {
1227 : : #define COPY(field) sec->shdr.field = (*s32)[i].field
1228 : 0 : COPY (sh_name);
1229 : 0 : COPY (sh_type);
1230 : 0 : COPY (sh_flags);
1231 : 0 : COPY (sh_addr);
1232 : 0 : COPY (sh_offset);
1233 : 0 : COPY (sh_size);
1234 : 0 : COPY (sh_link);
1235 : 0 : COPY (sh_info);
1236 : 0 : COPY (sh_addralign);
1237 : 0 : COPY (sh_entsize);
1238 : : #undef COPY
1239 : : }
1240 : : else
1241 : 0 : sec->shdr = (*s64)[i];
1242 [ # # ]: 0 : if (sec->shdr.sh_flags & SHF_ALLOC)
1243 : : {
1244 : 0 : sec->shdr.sh_addr += bias;
1245 : 0 : sec->name = get_section_name (i + 1, &sec->shdr, main_shstrtab);
1246 : 0 : sec->scn = elf_getscn (main, i + 1); /* Really just for ndx. */
1247 : 0 : sec->outscn = NULL;
1248 : 0 : sec->strent = NULL;
1249 : 0 : sec->sig = get_group_sig (main, &sec->shdr);
1250 : 0 : ++undo_nalloc;
1251 : : }
1252 : : }
1253 : 0 : qsort (undo_sections, undo_nalloc,
1254 : : sizeof undo_sections[0], compare_sections_nonrel);
1255 : 0 : free (shdr);
1256 : : }
1257 : :
1258 : 0 : bool fail = false;
1259 : 0 : Elf_Scn *scn = NULL;
1260 [ # # ]: 0 : while ((scn = elf_nextscn (debug, scn)) != NULL)
1261 : : {
1262 : 0 : GElf_Shdr shdr_mem;
1263 : 0 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1264 [ # # ]: 0 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1265 : :
1266 [ # # ]: 0 : if (!(shdr->sh_flags & SHF_ALLOC))
1267 : 0 : continue;
1268 : :
1269 : 0 : const char *name = get_section_name (elf_ndxscn (scn), shdr,
1270 : : debug_shstrtab);
1271 : :
1272 [ # # ]: 0 : if (undo_sections != NULL)
1273 : : {
1274 : 0 : struct section *sec = find_alloc_section (shdr, 0, name,
1275 : : undo_sections,
1276 : : undo_nalloc);
1277 [ # # ]: 0 : if (sec != NULL)
1278 : : {
1279 : 0 : sec->outscn = scn;
1280 : 0 : continue;
1281 : : }
1282 : : }
1283 : :
1284 : : /* If there is no prelink info, we are just here to find
1285 : : the sections to give error messages about. */
1286 [ # # ]: 0 : for (size_t i = 0; shdr != NULL && i < nalloc; ++i)
1287 [ # # ]: 0 : if (sections[i].outscn == scn)
1288 : 0 : shdr = NULL;
1289 : 0 : fail |= check_match (shdr == NULL, scn, name);
1290 : : }
1291 : :
1292 [ # # ]: 0 : if (fail)
1293 : 0 : exit (EXIT_FAILURE);
1294 : :
1295 : : /* Now we have lined up output sections for each of the original sections
1296 : : before prelinking. Translate those to the prelinked sections.
1297 : : This matches what prelink's undo_sections does. */
1298 : : struct section *split_bss = NULL;
1299 [ # # ]: 0 : for (size_t i = 0; i < undo_nalloc; ++i)
1300 : : {
1301 : 0 : const struct section *undo_sec = &undo_sections[i];
1302 : :
1303 : 0 : const char *name = undo_sec->name;
1304 : 0 : scn = undo_sec->scn; /* This is just for elf_ndxscn. */
1305 : :
1306 [ # # ]: 0 : for (size_t j = 0; j < nalloc; ++j)
1307 : : {
1308 : 0 : struct section *sec = §ions[j];
1309 : : #define RELA_SCALED(field) \
1310 : : (2 * sec->shdr.field == 3 * undo_sec->shdr.field)
1311 [ # # ]: 0 : if (sec->outscn == NULL
1312 [ # # ]: 0 : && sec->shdr.sh_name == undo_sec->shdr.sh_name
1313 [ # # ]: 0 : && sec->shdr.sh_flags == undo_sec->shdr.sh_flags
1314 [ # # ]: 0 : && sec->shdr.sh_addralign == undo_sec->shdr.sh_addralign
1315 [ # # ]: 0 : && (((sec->shdr.sh_type == undo_sec->shdr.sh_type
1316 [ # # ]: 0 : && sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize
1317 [ # # ]: 0 : && (sec->shdr.sh_size == undo_sec->shdr.sh_size
1318 [ # # ]: 0 : || (sec->shdr.sh_size > undo_sec->shdr.sh_size
1319 [ # # ]: 0 : && main_ehdr->e_type == ET_EXEC
1320 [ # # ]: 0 : && !strcmp (sec->name, ".dynstr"))))
1321 [ # # ]: 0 : || (sec->shdr.sh_size == undo_sec->shdr.sh_size
1322 [ # # ]: 0 : && ((sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize
1323 [ # # ]: 0 : && undo_sec->shdr.sh_type == SHT_NOBITS)
1324 [ # # ]: 0 : || undo_sec->shdr.sh_type == SHT_PROGBITS)
1325 [ # # ]: 0 : && !strcmp (sec->name, ".plt")))
1326 [ # # ]: 0 : || (sec->shdr.sh_type == SHT_RELA
1327 [ # # ]: 0 : && undo_sec->shdr.sh_type == SHT_REL
1328 [ # # # # ]: 0 : && RELA_SCALED (sh_entsize) && RELA_SCALED (sh_size))
1329 [ # # ]: 0 : || (sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize
1330 [ # # ]: 0 : && (sec->shdr.sh_type == undo_sec->shdr.sh_type
1331 [ # # ]: 0 : || (sec->shdr.sh_type == SHT_PROGBITS
1332 [ # # ]: 0 : && undo_sec->shdr.sh_type == SHT_NOBITS))
1333 [ # # ]: 0 : && sec->shdr.sh_size <= undo_sec->shdr.sh_size
1334 [ # # ]: 0 : && (!strcmp (sec->name, ".bss")
1335 [ # # ]: 0 : || !strcmp (sec->name, ".sbss"))
1336 [ # # ]: 0 : && (sec->shdr.sh_size == undo_sec->shdr.sh_size
1337 [ # # ]: 0 : || (split_bss = sec) > sections))))
1338 : : {
1339 : 0 : sec->outscn = undo_sec->outscn;
1340 : 0 : undo_sec = NULL;
1341 : 0 : break;
1342 : : }
1343 : : }
1344 : :
1345 : 0 : fail |= check_match (undo_sec == NULL, scn, name);
1346 : : }
1347 : :
1348 : 0 : free (undo_sections);
1349 : :
1350 [ # # ]: 0 : if (fail)
1351 : 0 : exit (EXIT_FAILURE);
1352 : :
1353 : 0 : return split_bss;
1354 : : }
1355 : :
1356 : : /* Create new .shstrtab contents, subroutine of copy_elided_sections.
1357 : : This can't be open coded there and still use variable-length auto arrays,
1358 : : since the end of our block would free other VLAs too. */
1359 : : static Elf_Data *
1360 : 50 : new_shstrtab (Elf *unstripped, size_t unstripped_shnum,
1361 : : Elf_Data *shstrtab, size_t unstripped_shstrndx,
1362 : : struct section *sections, size_t stripped_shnum,
1363 : : Dwelf_Strtab *strtab)
1364 : 50 : {
1365 [ + + ]: 50 : if (strtab == NULL)
1366 : : return NULL;
1367 : :
1368 : 2 : Dwelf_Strent *unstripped_strent[unstripped_shnum];
1369 : 2 : memset (unstripped_strent, 0, sizeof unstripped_strent);
1370 : 2 : for (struct section *sec = sections;
1371 [ + + ]: 10 : sec < §ions[stripped_shnum - 1];
1372 : 8 : ++sec)
1373 [ + + ]: 8 : if (sec->outscn != NULL)
1374 : : {
1375 [ + + ]: 6 : if (sec->strent == NULL)
1376 : : {
1377 : 4 : sec->strent = dwelf_strtab_add (strtab, sec->name);
1378 [ - + ]: 4 : ELF_CHECK (sec->strent != NULL,
1379 : : _("cannot add section name to string table: %s"));
1380 : : }
1381 : 6 : unstripped_strent[elf_ndxscn (sec->outscn) - 1] = sec->strent;
1382 : : }
1383 : :
1384 : : /* Add names of sections we aren't touching. */
1385 [ + + ]: 14 : for (size_t i = 0; i < unstripped_shnum - 1; ++i)
1386 [ + + ]: 12 : if (unstripped_strent[i] == NULL)
1387 : : {
1388 : 6 : Elf_Scn *scn = elf_getscn (unstripped, i + 1);
1389 : 6 : GElf_Shdr shdr_mem;
1390 : 6 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1391 [ - + ]: 6 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1392 : 6 : const char *name = get_section_name (i + 1, shdr, shstrtab);
1393 : 6 : unstripped_strent[i] = dwelf_strtab_add (strtab, name);
1394 [ - + ]: 6 : ELF_CHECK (unstripped_strent[i] != NULL,
1395 : : _("cannot add section name to string table: %s"));
1396 : : }
1397 : : else
1398 : 6 : unstripped_strent[i] = NULL;
1399 : :
1400 : : /* Now finalize the string table so we can get offsets. */
1401 : 2 : Elf_Data *strtab_data = elf_getdata (elf_getscn (unstripped,
1402 : : unstripped_shstrndx), NULL);
1403 [ - + ]: 2 : ELF_CHECK (elf_flagdata (strtab_data, ELF_C_SET, ELF_F_DIRTY),
1404 : : _("cannot update section header string table data: %s"));
1405 [ - + ]: 2 : if (dwelf_strtab_finalize (strtab, strtab_data) == NULL)
1406 : 0 : error_exit (0, "Not enough memory to create string table");
1407 : :
1408 : : /* Update the sh_name fields of sections we aren't modifying later. */
1409 [ + + ]: 14 : for (size_t i = 0; i < unstripped_shnum - 1; ++i)
1410 [ + + ]: 12 : if (unstripped_strent[i] != NULL)
1411 : : {
1412 : 6 : Elf_Scn *scn = elf_getscn (unstripped, i + 1);
1413 : 6 : GElf_Shdr shdr_mem;
1414 : 6 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1415 [ - + ]: 6 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1416 : 6 : shdr->sh_name = dwelf_strent_off (unstripped_strent[i]);
1417 [ + + ]: 6 : if (i + 1 == unstripped_shstrndx)
1418 : 2 : shdr->sh_size = strtab_data->d_size;
1419 : 6 : update_shdr (scn, shdr);
1420 : : }
1421 : :
1422 : : return strtab_data;
1423 : : }
1424 : :
1425 : : /* Fill in any SHT_NOBITS sections in UNSTRIPPED by
1426 : : copying their contents and sh_type from STRIPPED. */
1427 : : static void
1428 : 50 : copy_elided_sections (Elf *unstripped, Elf *stripped,
1429 : : const GElf_Ehdr *stripped_ehdr, GElf_Addr bias)
1430 : 50 : {
1431 : 50 : size_t unstripped_shstrndx;
1432 [ - + ]: 50 : ELF_CHECK (elf_getshdrstrndx (unstripped, &unstripped_shstrndx) == 0,
1433 : : _("cannot get section header string table section index: %s"));
1434 : :
1435 : 50 : size_t stripped_shstrndx;
1436 [ - + ]: 50 : ELF_CHECK (elf_getshdrstrndx (stripped, &stripped_shstrndx) == 0,
1437 : : _("cannot get section header string table section index: %s"));
1438 : :
1439 : 50 : size_t unstripped_shnum;
1440 [ - + ]: 50 : ELF_CHECK (elf_getshdrnum (unstripped, &unstripped_shnum) == 0,
1441 : : _("cannot get section count: %s"));
1442 : :
1443 : 50 : size_t stripped_shnum;
1444 [ - + ]: 50 : ELF_CHECK (elf_getshdrnum (stripped, &stripped_shnum) == 0,
1445 : : _("cannot get section count: %s"));
1446 : :
1447 [ - + ]: 50 : if (unlikely (stripped_shnum > unstripped_shnum))
1448 : 0 : error_exit (0, _("\
1449 : : more sections in stripped file than debug file -- arguments reversed?"));
1450 : :
1451 [ - + ]: 50 : if (unlikely (stripped_shnum <= 1))
1452 : 0 : error_exit (0, _("no sections in stripped file"));
1453 : :
1454 : : /* Used as sanity check for allocated section offset, if the section
1455 : : offset needs to be preserved. We want to know the max size of the
1456 : : ELF file, to check if any existing section offsets are OK. */
1457 : 50 : int64_t max_off = -1;
1458 [ + + ]: 50 : if (stripped_ehdr->e_type != ET_REL)
1459 : : {
1460 : 26 : elf_flagelf (stripped, ELF_C_SET, ELF_F_LAYOUT);
1461 : 26 : max_off = elf_update (stripped, ELF_C_NULL);
1462 : : }
1463 : :
1464 : : /* Cache the stripped file's section details. */
1465 : 50 : struct section sections[stripped_shnum - 1];
1466 : 50 : Elf_Scn *scn = NULL;
1467 [ + + ]: 1108 : while ((scn = elf_nextscn (stripped, scn)) != NULL)
1468 : : {
1469 : 1058 : size_t i = elf_ndxscn (scn) - 1;
1470 : 1058 : GElf_Shdr *shdr = gelf_getshdr (scn, §ions[i].shdr);
1471 [ - + ]: 1058 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1472 : 2116 : sections[i].name = elf_strptr (stripped, stripped_shstrndx,
1473 : 1058 : shdr->sh_name);
1474 [ - + ]: 1058 : if (sections[i].name == NULL)
1475 : 0 : error_exit (0, _("cannot read section [%zu] name: %s"),
1476 : : elf_ndxscn (scn), elf_errmsg (-1));
1477 : 1058 : sections[i].scn = scn;
1478 : 1058 : sections[i].outscn = NULL;
1479 : 1058 : sections[i].strent = NULL;
1480 : 1058 : sections[i].sig = get_group_sig (stripped, shdr);
1481 : : }
1482 : :
1483 : 50 : const struct section *stripped_symtab = NULL;
1484 : :
1485 : : /* Sort the sections, allocated by address and others after. */
1486 : 50 : qsort (sections, stripped_shnum - 1, sizeof sections[0],
1487 [ + + ]: 50 : stripped_ehdr->e_type == ET_REL
1488 : : ? compare_sections_rel : compare_sections_nonrel);
1489 : 50 : size_t nalloc = stripped_shnum - 1;
1490 [ + - + + ]: 360 : while (nalloc > 0 && !(sections[nalloc - 1].shdr.sh_flags & SHF_ALLOC))
1491 : : {
1492 : 310 : --nalloc;
1493 [ + + ]: 310 : if (sections[nalloc].shdr.sh_type == SHT_SYMTAB)
1494 : 28 : stripped_symtab = §ions[nalloc];
1495 : : }
1496 : :
1497 : 50 : Elf_Data *shstrtab = elf_getdata (elf_getscn (unstripped,
1498 : : unstripped_shstrndx), NULL);
1499 [ - + ]: 50 : ELF_CHECK (shstrtab != NULL,
1500 : : _("cannot read section header string table: %s"));
1501 : :
1502 : : /* Match each debuginfo section with its corresponding stripped section. */
1503 : : bool check_prelink = false;
1504 : : Elf_Scn *unstripped_symtab = NULL;
1505 : : size_t unstripped_strndx = 0;
1506 : : size_t alloc_avail = 0;
1507 : : scn = NULL;
1508 [ + + ]: 133466 : while ((scn = elf_nextscn (unstripped, scn)) != NULL)
1509 : : {
1510 : 133416 : GElf_Shdr shdr_mem;
1511 : 133416 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1512 [ - + ]: 133416 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1513 : :
1514 [ + + ]: 133416 : if (shdr->sh_type == SHT_SYMTAB)
1515 : : {
1516 : 46 : unstripped_symtab = scn;
1517 : 46 : unstripped_strndx = shdr->sh_link;
1518 : 132498 : continue;
1519 : : }
1520 : :
1521 : 133370 : const size_t ndx = elf_ndxscn (scn);
1522 [ + + + + ]: 133370 : if (ndx == unstripped_shstrndx || ndx == unstripped_strndx)
1523 : 92 : continue;
1524 : :
1525 : 133278 : const char *name = get_section_name (ndx, shdr, shstrtab);
1526 : :
1527 : 133278 : struct section *sec = NULL;
1528 [ + + ]: 133278 : if (shdr->sh_flags & SHF_ALLOC)
1529 : : {
1530 [ + + ]: 748 : if (stripped_ehdr->e_type != ET_REL)
1531 : : {
1532 : : /* Look for the section that matches. */
1533 : 590 : sec = find_alloc_section (shdr, bias, name, sections, nalloc);
1534 [ - + ]: 590 : if (sec == NULL)
1535 : : {
1536 : : /* We couldn't figure it out. It may be a prelink issue. */
1537 : 0 : check_prelink = true;
1538 : 0 : continue;
1539 : : }
1540 : : }
1541 : : else
1542 : : {
1543 : : /* The sh_addr of allocated sections does not help us,
1544 : : but the order usually matches. */
1545 [ + - ]: 158 : if (likely (sections_match (sections, alloc_avail, shdr, name)))
1546 : 158 : sec = §ions[alloc_avail++];
1547 : : else
1548 [ # # ]: 0 : for (size_t i = alloc_avail + 1; i < nalloc; ++i)
1549 [ # # ]: 0 : if (sections_match (sections, i, shdr, name))
1550 : : {
1551 : 0 : sec = §ions[i];
1552 : 0 : break;
1553 : : }
1554 : : }
1555 : : }
1556 : : else
1557 : : {
1558 : : /* Locate a matching unallocated section in SECTIONS. */
1559 : 132530 : const char *sig = get_group_sig (unstripped, shdr);
1560 : 132530 : size_t l = nalloc, u = stripped_shnum - 1;
1561 [ + + ]: 662710 : while (l < u)
1562 : : {
1563 : 397820 : size_t i = (l + u) / 2;
1564 : 397820 : struct section *section = §ions[i];
1565 : 397820 : int cmp = compare_unalloc_sections (shdr, §ion->shdr,
1566 : : name, section->name,
1567 : : sig, section->sig);
1568 [ + + ]: 397820 : if (cmp < 0)
1569 : : u = i;
1570 [ + + ]: 2740 : else if (cmp > 0)
1571 : 2570 : l = i + 1;
1572 : : else
1573 : : {
1574 : : sec = section;
1575 : : break;
1576 : : }
1577 : : }
1578 : :
1579 [ + + ]: 132530 : if (sec == NULL)
1580 : : {
1581 : : /* An additional unallocated section is fine if not SHT_NOBITS.
1582 : : We looked it up anyway in case it's an unallocated section
1583 : : copied in both files (e.g. SHT_NOTE), and don't keep both. */
1584 [ + - ]: 132360 : if (shdr->sh_type != SHT_NOBITS)
1585 : 132360 : continue;
1586 : :
1587 : : /* Somehow some old .debug files wound up with SHT_NOBITS
1588 : : .comment sections, so let those pass. */
1589 [ # # ]: 0 : if (!strcmp (name, ".comment"))
1590 : 0 : continue;
1591 : : }
1592 : : }
1593 : :
1594 [ - + ]: 158 : if (sec == NULL)
1595 : 0 : error_exit (0, _("cannot find matching section for [%zu] '%s'"),
1596 : : elf_ndxscn (scn), name);
1597 : :
1598 : 918 : sec->outscn = scn;
1599 : : }
1600 : :
1601 : : /* If that failed due to changes made by prelink, we take another tack.
1602 : : We keep track of a .bss section that was partly split into .dynbss
1603 : : so that collect_symbols can update symbols' st_shndx fields. */
1604 : 50 : struct section *split_bss = NULL;
1605 [ - + ]: 50 : if (check_prelink)
1606 : : {
1607 : 0 : Elf_Data *data = elf_getdata (elf_getscn (stripped, stripped_shstrndx),
1608 : : NULL);
1609 [ # # ]: 0 : ELF_CHECK (data != NULL,
1610 : : _("cannot read section header string table: %s"));
1611 : 0 : split_bss = find_alloc_sections_prelink (unstripped, shstrtab,
1612 : : stripped, stripped_ehdr,
1613 : : data, bias, sections,
1614 : : nalloc, stripped_shnum - 1);
1615 : : }
1616 : :
1617 : : /* Make sure each main file section has a place to go. */
1618 : 50 : const struct section *stripped_dynsym = NULL;
1619 : 50 : size_t debuglink = SHN_UNDEF;
1620 : 50 : size_t ndx_sec_num = stripped_shnum - 1;
1621 : 50 : size_t ndx_section[ndx_sec_num];
1622 : 50 : Dwelf_Strtab *strtab = NULL;
1623 : 50 : for (struct section *sec = sections;
1624 [ + + ]: 1108 : sec < §ions[ndx_sec_num];
1625 : 1058 : ++sec)
1626 : : {
1627 : 1058 : size_t secndx = elf_ndxscn (sec->scn);
1628 : :
1629 [ + + ]: 1058 : if (sec->outscn == NULL)
1630 : : {
1631 : : /* We didn't find any corresponding section for this. */
1632 : :
1633 [ + + ]: 140 : if (secndx == stripped_shstrndx)
1634 : : {
1635 : : /* We only need one .shstrtab. */
1636 : 50 : ndx_section[secndx - 1] = unstripped_shstrndx;
1637 : 50 : continue;
1638 : : }
1639 : :
1640 [ + + ]: 90 : if (unstripped_symtab != NULL && sec == stripped_symtab)
1641 : : {
1642 : : /* We don't need a second symbol table. */
1643 : 24 : ndx_section[secndx - 1] = elf_ndxscn (unstripped_symtab);
1644 : 24 : continue;
1645 : : }
1646 : :
1647 [ + + ]: 66 : if (unstripped_symtab != NULL && stripped_symtab != NULL
1648 [ + + ]: 40 : && secndx == stripped_symtab->shdr.sh_link
1649 [ + - ]: 24 : && unstripped_strndx != 0)
1650 : : {
1651 : : /* ... nor its string table. */
1652 : 24 : ndx_section[secndx - 1] = unstripped_strndx;
1653 : 24 : continue;
1654 : : }
1655 : :
1656 [ + - ]: 42 : if (!(sec->shdr.sh_flags & SHF_ALLOC)
1657 [ + + ]: 42 : && !strcmp (sec->name, ".gnu_debuglink"))
1658 : : {
1659 : : /* This was created by stripping. We don't want it. */
1660 : 40 : debuglink = secndx;
1661 : 40 : ndx_section[secndx - 1] = SHN_UNDEF;
1662 : 40 : continue;
1663 : : }
1664 : :
1665 : 2 : sec->outscn = elf_newscn (unstripped);
1666 : 2 : Elf_Data *newdata = elf_newdata (sec->outscn);
1667 [ + - - + ]: 2 : ELF_CHECK (newdata != NULL && gelf_update_shdr (sec->outscn,
1668 : : &sec->shdr),
1669 : : _("cannot add new section: %s"));
1670 : 2 : unstripped_shnum++;
1671 : :
1672 [ + - ]: 2 : if (strtab == NULL)
1673 : 2 : strtab = dwelf_strtab_init (true);
1674 : 2 : sec->strent = dwelf_strtab_add (strtab, sec->name);
1675 [ - + ]: 2 : ELF_CHECK (sec->strent != NULL,
1676 : : _("cannot add section name to string table: %s"));
1677 : : }
1678 : :
1679 : : /* Cache the mapping of original section indices to output sections. */
1680 : 920 : ndx_section[secndx - 1] = elf_ndxscn (sec->outscn);
1681 : : }
1682 : :
1683 : : /* We added some sections, so we need a new shstrtab. */
1684 : 50 : Elf_Data *strtab_data = new_shstrtab (unstripped, unstripped_shnum,
1685 : : shstrtab, unstripped_shstrndx,
1686 : : sections, stripped_shnum,
1687 : : strtab);
1688 : :
1689 : : /* Get the updated section count. */
1690 [ - + ]: 50 : ELF_CHECK (elf_getshdrnum (unstripped, &unstripped_shnum) == 0,
1691 : : _("cannot get section count: %s"));
1692 : :
1693 : 50 : bool placed[unstripped_shnum - 1];
1694 : 50 : memset (placed, 0, sizeof placed);
1695 : :
1696 : : /* Now update the output sections and copy in their data. */
1697 : 50 : GElf_Off offset = 0;
1698 : 50 : for (const struct section *sec = sections;
1699 [ + + ]: 1106 : sec < §ions[stripped_shnum - 1];
1700 : 1056 : ++sec)
1701 [ + + ]: 1058 : if (sec->outscn != NULL)
1702 : : {
1703 : 920 : GElf_Shdr shdr_mem;
1704 : 920 : GElf_Shdr *shdr = gelf_getshdr (sec->outscn, &shdr_mem);
1705 [ - + ]: 920 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1706 : :
1707 : : /* In an ET_REL file under --relocate, the sh_addr of SHF_ALLOC
1708 : : sections will have been set nonzero by relocation. This
1709 : : touched the shdrs of whichever file had the symtab. sh_addr
1710 : : is still zero in the corresponding shdr. The relocated
1711 : : address is what we want to use. */
1712 [ + + ]: 920 : if (stripped_ehdr->e_type != ET_REL
1713 [ + + ]: 296 : || !(shdr_mem.sh_flags & SHF_ALLOC)
1714 [ + - ]: 158 : || shdr_mem.sh_addr == 0)
1715 : 920 : shdr_mem.sh_addr = sec->shdr.sh_addr;
1716 : :
1717 : 920 : shdr_mem.sh_type = sec->shdr.sh_type;
1718 : 920 : shdr_mem.sh_size = sec->shdr.sh_size;
1719 : 920 : shdr_mem.sh_info = sec->shdr.sh_info;
1720 : 920 : shdr_mem.sh_link = sec->shdr.sh_link;
1721 : :
1722 : : /* Buggy binutils objdump might have stripped the SHF_INFO_LINK
1723 : : put it back if necessary. */
1724 [ + + ]: 920 : if ((sec->shdr.sh_type == SHT_REL || sec->shdr.sh_type == SHT_RELA)
1725 [ + + ]: 128 : && sec->shdr.sh_flags != shdr_mem.sh_flags
1726 [ + - ]: 4 : && (sec->shdr.sh_flags & SHF_INFO_LINK) != 0)
1727 : 4 : shdr_mem.sh_flags |= SHF_INFO_LINK;
1728 : :
1729 [ + + ]: 920 : if (sec->shdr.sh_link != SHN_UNDEF)
1730 : : {
1731 [ - + ]: 276 : if (sec->shdr.sh_link > ndx_sec_num)
1732 : 0 : error_exit (0,
1733 : : "section [%zd] has invalid sh_link %" PRId32,
1734 : : elf_ndxscn (sec->scn), sec->shdr.sh_link);
1735 : 276 : shdr_mem.sh_link = ndx_section[sec->shdr.sh_link - 1];
1736 : : }
1737 [ + + - + : 920 : if (SH_INFO_LINK_P (&sec->shdr) && sec->shdr.sh_info != 0)
+ + ]
1738 : : {
1739 [ - + ]: 104 : if (sec->shdr.sh_info > ndx_sec_num)
1740 : 0 : error_exit (0,
1741 : : "section [%zd] has invalid sh_info %" PRId32,
1742 : : elf_ndxscn (sec->scn), sec->shdr.sh_info);
1743 : 104 : shdr_mem.sh_info = ndx_section[sec->shdr.sh_info - 1];
1744 : : }
1745 : :
1746 [ + + ]: 920 : if (strtab != NULL)
1747 : 6 : shdr_mem.sh_name = dwelf_strent_off (sec->strent);
1748 : :
1749 : 920 : Elf_Data *indata = elf_getdata (sec->scn, NULL);
1750 [ - + ]: 920 : ELF_CHECK (indata != NULL, _("cannot get section data: %s"));
1751 : 920 : Elf_Data *outdata = elf_getdata (sec->outscn, NULL);
1752 [ - + ]: 920 : ELF_CHECK (outdata != NULL, _("cannot copy section data: %s"));
1753 : 920 : *outdata = *indata;
1754 : 920 : elf_flagdata (outdata, ELF_C_SET, ELF_F_DIRTY);
1755 : :
1756 : : /* Preserve the file layout of the allocated sections. */
1757 [ + + + + ]: 920 : if (stripped_ehdr->e_type != ET_REL && (shdr_mem.sh_flags & SHF_ALLOC))
1758 : : {
1759 [ + - - + ]: 590 : if (max_off > 0 && sec->shdr.sh_offset > (Elf64_Off) max_off)
1760 : 0 : error_exit (0,
1761 : : "allocated section offset too large [%zd] %" PRIx64,
1762 : : elf_ndxscn (sec->scn), sec->shdr.sh_offset);
1763 : :
1764 : 590 : shdr_mem.sh_offset = sec->shdr.sh_offset;
1765 : 590 : placed[elf_ndxscn (sec->outscn) - 1] = true;
1766 : :
1767 : 1180 : const GElf_Off end_offset = (shdr_mem.sh_offset
1768 : 590 : + (shdr_mem.sh_type == SHT_NOBITS
1769 [ + + ]: 590 : ? 0 : shdr_mem.sh_size));
1770 [ + + ]: 590 : if (end_offset > offset)
1771 : 570 : offset = end_offset;
1772 : : }
1773 : :
1774 : 920 : update_shdr (sec->outscn, &shdr_mem);
1775 : :
1776 [ + + ]: 920 : if (shdr_mem.sh_type == SHT_SYMTAB || shdr_mem.sh_type == SHT_DYNSYM)
1777 : : {
1778 : : /* We must adjust all the section indices in the symbol table. */
1779 : :
1780 : 30 : Elf_Data *shndxdata = NULL; /* XXX */
1781 : :
1782 [ + - ]: 30 : if (shdr_mem.sh_entsize == 0
1783 [ + + ]: 30 : || shdr_mem.sh_size < shdr_mem.sh_entsize)
1784 : 2 : error_exit (0,
1785 : : _("SYMTAB section cannot have zero sh_entsize"
1786 : : " or a sh_size smaller than sh_entsize"));
1787 [ + + ]: 870 : for (size_t i = 1; i < shdr_mem.sh_size / shdr_mem.sh_entsize; ++i)
1788 : : {
1789 : 842 : GElf_Sym sym_mem;
1790 : 842 : GElf_Word shndx = SHN_UNDEF;
1791 : 842 : GElf_Sym *sym = gelf_getsymshndx (outdata, shndxdata,
1792 : : i, &sym_mem, &shndx);
1793 [ - + ]: 842 : ELF_CHECK (sym != NULL,
1794 : : _("cannot get symbol table entry: %s"));
1795 [ + - ]: 842 : if (sym->st_shndx != SHN_XINDEX)
1796 : 842 : shndx = sym->st_shndx;
1797 : :
1798 [ + + ]: 842 : if (shndx != SHN_UNDEF && shndx < SHN_LORESERVE)
1799 : : {
1800 [ - + ]: 484 : if (shndx >= stripped_shnum)
1801 : 0 : error_exit (0,
1802 : : _("symbol [%zu] has invalid section index"), i);
1803 : :
1804 : 484 : shndx = ndx_section[shndx - 1];
1805 [ + - ]: 484 : if (shndx < SHN_LORESERVE)
1806 : : {
1807 : 484 : sym->st_shndx = shndx;
1808 : 484 : shndx = SHN_UNDEF;
1809 : : }
1810 : : else
1811 : 0 : sym->st_shndx = SHN_XINDEX;
1812 : :
1813 [ - + ]: 842 : ELF_CHECK (gelf_update_symshndx (outdata, shndxdata,
1814 : : i, sym, shndx),
1815 : : _("cannot update symbol table: %s"));
1816 : : }
1817 : : }
1818 : :
1819 [ + + ]: 28 : if (shdr_mem.sh_type == SHT_SYMTAB)
1820 : 2 : stripped_symtab = sec;
1821 [ + + ]: 28 : if (shdr_mem.sh_type == SHT_DYNSYM)
1822 : 26 : stripped_dynsym = sec;
1823 : : }
1824 : :
1825 [ + + ]: 918 : if (shdr_mem.sh_type == SHT_GROUP)
1826 : : {
1827 : : /* We must adjust all the section indices in the group.
1828 : : Skip the first word, which is the section group flag.
1829 : : Everything else is a section index. */
1830 : 14 : Elf32_Word *shndx = (Elf32_Word *) outdata->d_buf;
1831 [ + + ]: 44 : for (size_t i = 1; i < shdr_mem.sh_size / sizeof (Elf32_Word); ++i)
1832 [ + - - + ]: 30 : if (shndx[i] == SHN_UNDEF || shndx[i] >= stripped_shnum)
1833 : 0 : error_exit (0,
1834 : : _("group has invalid section index [%zd]"), i);
1835 : : else
1836 : 30 : shndx[i] = ndx_section[shndx[i] - 1];
1837 : : }
1838 : : }
1839 : :
1840 : : /* We may need to update the symbol table. */
1841 : 48 : Elf_Data *symdata = NULL;
1842 : 48 : Dwelf_Strtab *symstrtab = NULL;
1843 : 48 : Elf_Data *symstrdata = NULL;
1844 [ + + ]: 48 : if (unstripped_symtab != NULL && (stripped_symtab != NULL
1845 [ + + ]: 46 : || check_prelink /* Section adjustments. */
1846 [ + - ]: 22 : || (stripped_ehdr->e_type != ET_REL
1847 [ - + ]: 22 : && bias != 0)))
1848 : 24 : {
1849 : : /* Merge the stripped file's symbol table into the unstripped one. */
1850 [ + - ]: 24 : if (stripped_symtab != NULL
1851 [ + - ]: 24 : && (stripped_symtab->shdr.sh_entsize == 0
1852 [ + + ]: 24 : || (stripped_symtab->shdr.sh_size
1853 : : < stripped_symtab->shdr.sh_entsize)))
1854 : 2 : error_exit (0,
1855 : : _("stripped SYMTAB section cannot have zero sh_entsize"
1856 : : " or a sh_size smaller than sh_entsize"));
1857 : 22 : const size_t stripped_nsym = (stripped_symtab == NULL ? 1
1858 : : : (stripped_symtab->shdr.sh_size
1859 : : / stripped_symtab->shdr.sh_entsize));
1860 : :
1861 : 22 : GElf_Shdr shdr_mem;
1862 : 22 : GElf_Shdr *shdr = gelf_getshdr (unstripped_symtab, &shdr_mem);
1863 [ - + ]: 22 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1864 [ + - + + ]: 22 : if (shdr->sh_entsize == 0 || shdr->sh_size < shdr->sh_entsize)
1865 : 2 : error_exit (0,
1866 : : _("unstripped SYMTAB section cannot have zero sh_entsize"
1867 : : " or a sh_size smaller than sh_entsize"));
1868 : 20 : const size_t unstripped_nsym = shdr->sh_size / shdr->sh_entsize;
1869 : :
1870 : : /* First collect all the symbols from both tables. */
1871 : :
1872 : 20 : const size_t total_syms = stripped_nsym - 1 + unstripped_nsym - 1;
1873 : 20 : struct symbol *symbols = xmalloc (total_syms * sizeof (struct symbol));
1874 : 20 : size_t *symndx_map = xmalloc (total_syms * sizeof (size_t));
1875 : :
1876 [ + - ]: 20 : if (stripped_symtab != NULL)
1877 : 40 : collect_symbols (unstripped, stripped_ehdr->e_type == ET_REL,
1878 : 20 : stripped_symtab->scn,
1879 : 20 : elf_getscn (stripped, stripped_symtab->shdr.sh_link),
1880 : : stripped_nsym, 0, ndx_section,
1881 : : symbols, symndx_map, NULL);
1882 : :
1883 : 20 : Elf_Scn *unstripped_strtab = elf_getscn (unstripped, shdr->sh_link);
1884 : 20 : collect_symbols (unstripped, stripped_ehdr->e_type == ET_REL,
1885 : : unstripped_symtab, unstripped_strtab, unstripped_nsym,
1886 : 20 : stripped_ehdr->e_type == ET_REL ? 0 : bias, NULL,
1887 : 20 : &symbols[stripped_nsym - 1],
1888 [ + + ]: 20 : &symndx_map[stripped_nsym - 1], split_bss);
1889 : :
1890 : : /* Next, sort our array of all symbols. */
1891 : 20 : qsort (symbols, total_syms, sizeof symbols[0], compare_symbols);
1892 : :
1893 : : /* Now we can weed out the duplicates. Assign remaining symbols
1894 : : new slots, collecting a map from old indices to new. */
1895 : 20 : size_t nsym = 0;
1896 [ + + ]: 2614 : for (struct symbol *s = symbols; s < &symbols[total_syms]; ++s)
1897 : : {
1898 : : /* Skip a section symbol for a removed section. */
1899 [ + + ]: 2574 : if (s->shndx == SHN_UNDEF
1900 [ + + ]: 540 : && GELF_ST_TYPE (s->info.info) == STT_SECTION)
1901 : : {
1902 : 4 : s->name = NULL; /* Mark as discarded. */
1903 : 4 : *s->map = STN_UNDEF;
1904 : 4 : s->duplicate = NULL;
1905 : 4 : continue;
1906 : : }
1907 : :
1908 : : struct symbol *n = s;
1909 : 4028 : while (n + 1 < &symbols[total_syms]
1910 [ + + + + ]: 4028 : && !compare_symbols_duplicate (s, n + 1))
1911 : : ++n;
1912 : :
1913 [ + + ]: 4028 : while (s < n)
1914 : : {
1915 : : /* This is a duplicate. Its twin will get the next slot. */
1916 : 1458 : s->name = NULL; /* Mark as discarded. */
1917 : 1458 : s->duplicate = n->map;
1918 : 1458 : ++s;
1919 : : }
1920 : :
1921 : : /* Allocate the next slot. */
1922 : 2570 : *s->map = ++nsym;
1923 : : }
1924 : :
1925 : : /* Now we sort again, to determine the order in the output. */
1926 : 20 : qsort (symbols, total_syms, sizeof symbols[0], compare_symbols_output);
1927 : :
1928 [ + - ]: 20 : if (nsym < total_syms)
1929 : : /* The discarded symbols are now at the end of the table. */
1930 [ - + ]: 20 : assert (symbols[nsym].name == NULL);
1931 : :
1932 : : /* Now a final pass updates the map with the final order,
1933 : : and builds up the new string table. */
1934 : 20 : symstrtab = dwelf_strtab_init (true);
1935 [ + + ]: 2610 : for (size_t i = 0; i < nsym; ++i)
1936 : : {
1937 [ - + ]: 2570 : assert (symbols[i].name != NULL);
1938 [ - + ]: 2570 : assert (*symbols[i].map != 0);
1939 : 2570 : *symbols[i].map = 1 + i;
1940 : 2570 : symbols[i].strent = dwelf_strtab_add (symstrtab, symbols[i].name);
1941 : : }
1942 : :
1943 : : /* Scan the discarded symbols too, just to update their slots
1944 : : in SYMNDX_MAP to refer to their live duplicates. */
1945 [ + + ]: 1482 : for (size_t i = nsym; i < total_syms; ++i)
1946 : : {
1947 [ - + ]: 1462 : assert (symbols[i].name == NULL);
1948 [ + + ]: 1462 : if (symbols[i].duplicate == NULL)
1949 [ - + ]: 4 : assert (*symbols[i].map == STN_UNDEF);
1950 : : else
1951 : : {
1952 [ - + ]: 1458 : assert (*symbols[i].duplicate != STN_UNDEF);
1953 : 1458 : *symbols[i].map = *symbols[i].duplicate;
1954 : : }
1955 : : }
1956 : :
1957 : : /* Now we are ready to write the new symbol table. */
1958 : 20 : symdata = elf_getdata (unstripped_symtab, NULL);
1959 [ - + ]: 20 : if (symdata == NULL)
1960 : 0 : error_exit (0, "Failed to get data from symbol table: %s",
1961 : : elf_errmsg (-1));
1962 : 20 : symstrdata = elf_getdata (unstripped_strtab, NULL);
1963 [ - + ]: 20 : if (symstrdata == NULL)
1964 : 0 : error_exit (0, "Failed to get data from symbol string table: %s",
1965 : : elf_errmsg (-1));
1966 : 20 : Elf_Data *shndxdata = NULL; /* XXX */
1967 : :
1968 : : /* If symtab and the section header table share the string table
1969 : : add the section names to the strtab and then (after finalizing)
1970 : : fixup the section header sh_names. Also dispose of the old data. */
1971 : 20 : Dwelf_Strent *unstripped_strent[unstripped_shnum - 1];
1972 [ + + ]: 20 : if (unstripped_shstrndx == elf_ndxscn (unstripped_strtab))
1973 : : {
1974 [ + + ]: 972 : for (size_t i = 0; i < unstripped_shnum - 1; ++i)
1975 : : {
1976 : 970 : Elf_Scn *sec = elf_getscn (unstripped, i + 1);
1977 : 970 : GElf_Shdr mem;
1978 : 970 : GElf_Shdr *hdr = gelf_getshdr (sec, &mem);
1979 : 970 : const char *name = get_section_name (i + 1, hdr, shstrtab);
1980 : 970 : unstripped_strent[i] = dwelf_strtab_add (symstrtab, name);
1981 [ - + ]: 970 : ELF_CHECK (unstripped_strent[i] != NULL,
1982 : : _("cannot add section name to string table: %s"));
1983 : : }
1984 : :
1985 [ - + ]: 2 : if (strtab != NULL)
1986 : : {
1987 : 0 : dwelf_strtab_free (strtab);
1988 : 0 : free (strtab_data->d_buf);
1989 : 0 : strtab = NULL;
1990 : : }
1991 : : }
1992 : :
1993 [ - + ]: 20 : if (dwelf_strtab_finalize (symstrtab, symstrdata) == NULL)
1994 : 0 : error_exit (0, "Not enough memory to create symbol table");
1995 : :
1996 : 20 : elf_flagdata (symstrdata, ELF_C_SET, ELF_F_DIRTY);
1997 : :
1998 : : /* And update the section header names if necessary. */
1999 [ + + ]: 20 : if (unstripped_shstrndx == elf_ndxscn (unstripped_strtab))
2000 : : {
2001 [ + + ]: 972 : for (size_t i = 0; i < unstripped_shnum - 1; ++i)
2002 : : {
2003 : 970 : Elf_Scn *sec = elf_getscn (unstripped, i + 1);
2004 : 970 : GElf_Shdr mem;
2005 : 970 : GElf_Shdr *hdr = gelf_getshdr (sec, &mem);
2006 : 970 : shdr->sh_name = dwelf_strent_off (unstripped_strent[i]);
2007 : 970 : update_shdr (sec, hdr);
2008 : : }
2009 : : }
2010 : :
2011 : : /* Now update the symtab shdr. Reload symtab shdr because sh_name
2012 : : might have changed above. */
2013 : 20 : shdr = gelf_getshdr (unstripped_symtab, &shdr_mem);
2014 [ - + ]: 20 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
2015 : :
2016 : 20 : shdr->sh_size = symdata->d_size = (1 + nsym) * shdr->sh_entsize;
2017 : 20 : symdata->d_buf = xmalloc (symdata->d_size);
2018 : 20 : record_new_data (symdata->d_buf);
2019 : :
2020 : 20 : GElf_Sym sym;
2021 : 20 : memset (&sym, 0, sizeof sym);
2022 [ - + ]: 20 : ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, 0, &sym, SHN_UNDEF),
2023 : : _("cannot update symbol table: %s"));
2024 : :
2025 : 20 : shdr->sh_info = 1;
2026 [ + + ]: 2590 : for (size_t i = 0; i < nsym; ++i)
2027 : : {
2028 : 2570 : struct symbol *s = &symbols[i];
2029 : :
2030 : : /* Fill in the symbol details. */
2031 : 2570 : sym.st_name = dwelf_strent_off (s->strent);
2032 : 2570 : sym.st_value = s->value; /* Already biased to output address. */
2033 : 2570 : sym.st_size = s->size;
2034 : 2570 : sym.st_shndx = s->shndx; /* Already mapped to output index. */
2035 : 2570 : sym.st_info = s->info.info;
2036 : 2570 : sym.st_other = s->info.other;
2037 : :
2038 : : /* Keep track of the number of leading local symbols. */
2039 [ + + ]: 2570 : if (GELF_ST_BIND (sym.st_info) == STB_LOCAL)
2040 : : {
2041 [ - + ]: 1946 : assert (shdr->sh_info == 1 + i);
2042 : 1946 : shdr->sh_info = 1 + i + 1;
2043 : : }
2044 : :
2045 [ - + ]: 2570 : ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, 1 + i,
2046 : : &sym, SHN_UNDEF),
2047 : : _("cannot update symbol table: %s"));
2048 : :
2049 : : }
2050 : 20 : elf_flagdata (symdata, ELF_C_SET, ELF_F_DIRTY);
2051 : 20 : update_shdr (unstripped_symtab, shdr);
2052 : :
2053 : : /* Track which sections are adjusted during the first round
2054 : : of calls to adjust_relocs. */
2055 : 20 : bool scn_adjusted[unstripped_shnum];
2056 [ + - ]: 20 : memset (scn_adjusted, 0, sizeof scn_adjusted);
2057 : :
2058 [ + - ]: 20 : if (stripped_symtab != NULL)
2059 : : {
2060 : : /* Adjust any relocations referring to the old symbol table. */
2061 : 20 : const size_t old_sh_link = elf_ndxscn (stripped_symtab->scn);
2062 : 20 : for (const struct section *sec = sections;
2063 [ + + ]: 474 : sec < §ions[stripped_shnum - 1];
2064 : 454 : ++sec)
2065 [ + + + + ]: 454 : if (sec->outscn != NULL && sec->shdr.sh_link == old_sh_link)
2066 : : {
2067 : 90 : adjust_relocs (sec->outscn, sec->scn, &sec->shdr,
2068 : : symndx_map, total_syms, shdr);
2069 : 90 : scn_adjusted[elf_ndxscn (sec->outscn)] = true;
2070 : : }
2071 : : }
2072 : :
2073 : : /* Also adjust references to the other old symbol table. */
2074 : 20 : adjust_all_relocs (unstripped, unstripped_symtab, shdr,
2075 : : &symndx_map[stripped_nsym - 1],
2076 : 20 : total_syms - (stripped_nsym - 1),
2077 : : scn_adjusted);
2078 : :
2079 : 20 : free (symbols);
2080 : 20 : free (symndx_map);
2081 : : }
2082 [ + + + - ]: 24 : else if (stripped_symtab != NULL && stripped_shnum != unstripped_shnum)
2083 : 2 : check_symtab_section_symbols (unstripped,
2084 : 2 : stripped_ehdr->e_type == ET_REL,
2085 : 2 : stripped_symtab->scn,
2086 : : unstripped_shnum, unstripped_shstrndx,
2087 : 2 : stripped_symtab->outscn,
2088 : : stripped_shnum, stripped_shstrndx,
2089 : : debuglink);
2090 : :
2091 [ + + ]: 44 : if (stripped_dynsym != NULL)
2092 : 26 : (void) check_symtab_section_symbols (unstripped,
2093 : 26 : stripped_ehdr->e_type == ET_REL,
2094 : 26 : stripped_dynsym->outscn,
2095 : : unstripped_shnum,
2096 : : unstripped_shstrndx,
2097 : 26 : stripped_dynsym->scn, stripped_shnum,
2098 : : stripped_shstrndx, debuglink);
2099 : :
2100 : : /* We need to preserve the layout of the stripped file so the
2101 : : phdrs will match up. This requires us to do our own layout of
2102 : : the added sections. We do manual layout even for ET_REL just
2103 : : so we can try to match what the original probably had. */
2104 : :
2105 : 44 : elf_flagelf (unstripped, ELF_C_SET, ELF_F_LAYOUT);
2106 : :
2107 [ + + ]: 44 : if (offset == 0)
2108 : : /* For ET_REL we are starting the layout from scratch. */
2109 : 18 : offset = gelf_fsize (unstripped, ELF_T_EHDR, 1, EV_CURRENT);
2110 : :
2111 : 44 : bool skip_reloc = false;
2112 : 88 : do
2113 : : {
2114 : 88 : skip_reloc = !skip_reloc;
2115 [ + + ]: 266868 : for (size_t i = 0; i < unstripped_shnum - 1; ++i)
2116 [ + + ]: 266780 : if (!placed[i])
2117 : : {
2118 : 133246 : scn = elf_getscn (unstripped, 1 + i);
2119 : :
2120 : 133246 : GElf_Shdr shdr_mem;
2121 : 133246 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2122 [ - + ]: 133246 : ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
2123 : :
2124 : : /* We must make sure we have read in the data of all sections
2125 : : beforehand and marked them to be written out. When we're
2126 : : modifying the existing file in place, we might overwrite
2127 : : this part of the file before we get to handling the section. */
2128 : :
2129 [ - + ]: 133246 : ELF_CHECK (elf_flagdata (elf_getdata (scn, NULL),
2130 : : ELF_C_SET, ELF_F_DIRTY),
2131 : : _("cannot read section data: %s"));
2132 : :
2133 [ + + ]: 133246 : if (skip_reloc
2134 [ + + ]: 132800 : && (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA))
2135 : 446 : continue;
2136 : :
2137 [ - + ]: 132800 : GElf_Off align = shdr->sh_addralign ?: 1;
2138 : 132800 : offset = (offset + align - 1) & -align;
2139 : 132800 : shdr->sh_offset = offset;
2140 [ + + ]: 132800 : if (shdr->sh_type != SHT_NOBITS)
2141 : 132782 : offset += shdr->sh_size;
2142 : :
2143 : 132800 : update_shdr (scn, shdr);
2144 : :
2145 [ + + ]: 132800 : if (unstripped_shstrndx == 1 + i)
2146 : : {
2147 : : /* Place the section headers immediately after
2148 : : .shstrtab, and update the ELF header. */
2149 : :
2150 : 44 : GElf_Ehdr ehdr_mem;
2151 : 44 : GElf_Ehdr *ehdr = gelf_getehdr (unstripped, &ehdr_mem);
2152 [ - + ]: 44 : ELF_CHECK (ehdr != NULL, _("cannot get ELF header: %s"));
2153 : :
2154 : 44 : GElf_Off sh_align = gelf_getclass (unstripped) * 4;
2155 : 44 : offset = (offset + sh_align - 1) & -sh_align;
2156 : 44 : ehdr->e_shnum = unstripped_shnum;
2157 : 44 : ehdr->e_shoff = offset;
2158 : 44 : offset += unstripped_shnum * ehdr->e_shentsize;
2159 [ - + ]: 44 : ELF_CHECK (gelf_update_ehdr (unstripped, ehdr),
2160 : : _("cannot update ELF header: %s"));
2161 : : }
2162 : :
2163 : 132800 : placed[i] = true;
2164 : : }
2165 : : }
2166 [ + + ]: 88 : while (skip_reloc);
2167 : :
2168 : 44 : size_t phnum;
2169 [ - + ]: 44 : ELF_CHECK (elf_getphdrnum (stripped, &phnum) == 0,
2170 : : _("cannot get number of program headers: %s"));
2171 : :
2172 [ + + ]: 44 : if (phnum > 0)
2173 [ - + ]: 26 : ELF_CHECK (gelf_newphdr (unstripped, phnum),
2174 : : _("cannot create program headers: %s"));
2175 : :
2176 : : /* Copy each program header from the stripped file. */
2177 [ + + ]: 232 : for (size_t i = 0; i < phnum; ++i)
2178 : : {
2179 : 188 : GElf_Phdr phdr_mem;
2180 : 188 : GElf_Phdr *phdr = gelf_getphdr (stripped, i, &phdr_mem);
2181 [ - + ]: 188 : ELF_CHECK (phdr != NULL, _("cannot get program header: %s"));
2182 : :
2183 [ - + ]: 188 : ELF_CHECK (gelf_update_phdr (unstripped, i, phdr),
2184 : : _("cannot update program header: %s"));
2185 : : }
2186 : :
2187 : : /* Finally, write out the file. */
2188 [ - + ]: 44 : ELF_CHECK (elf_update (unstripped, ELF_C_WRITE) > 0,
2189 : : _("cannot write output file: %s"));
2190 : :
2191 [ - + ]: 44 : if (strtab != NULL)
2192 : : {
2193 : 0 : dwelf_strtab_free (strtab);
2194 : 0 : free (strtab_data->d_buf);
2195 : : }
2196 : :
2197 [ + + ]: 44 : if (symstrtab != NULL)
2198 : : {
2199 : 20 : dwelf_strtab_free (symstrtab);
2200 : 20 : free (symstrdata->d_buf);
2201 : : }
2202 : 44 : free_new_data ();
2203 : 44 : }
2204 : :
2205 : : /* Process one pair of files, already opened. */
2206 : : static void
2207 : 50 : handle_file (const char *output_file, bool create_dirs,
2208 : : Elf *stripped, const GElf_Ehdr *stripped_ehdr,
2209 : : Elf *unstripped)
2210 : : {
2211 : 50 : size_t phnum;
2212 [ - + ]: 50 : ELF_CHECK (elf_getphdrnum (stripped, &phnum) == 0,
2213 : : _("cannot get number of program headers: %s"));
2214 : :
2215 : : /* Determine the address bias between the debuginfo file and the main
2216 : : file, which may have been modified by prelinking. */
2217 : 50 : GElf_Addr bias = 0;
2218 [ + - ]: 50 : if (unstripped != NULL)
2219 [ + + ]: 86 : for (size_t i = 0; i < phnum; ++i)
2220 : : {
2221 : 62 : GElf_Phdr phdr_mem;
2222 : 62 : GElf_Phdr *phdr = gelf_getphdr (stripped, i, &phdr_mem);
2223 [ - + ]: 62 : ELF_CHECK (phdr != NULL, _("cannot get program header: %s"));
2224 [ + + ]: 62 : if (phdr->p_type == PT_LOAD)
2225 : : {
2226 : 26 : GElf_Phdr unstripped_phdr_mem;
2227 : 26 : GElf_Phdr *unstripped_phdr = gelf_getphdr (unstripped, i,
2228 : : &unstripped_phdr_mem);
2229 [ - + ]: 26 : ELF_CHECK (unstripped_phdr != NULL,
2230 : : _("cannot get program header: %s"));
2231 : 26 : bias = phdr->p_vaddr - unstripped_phdr->p_vaddr;
2232 : 26 : break;
2233 : : }
2234 : : }
2235 : :
2236 : : /* One day we could adjust all the DWARF data (like prelink itself does). */
2237 [ - + ]: 50 : if (bias != 0)
2238 : : {
2239 [ # # ]: 0 : if (output_file == NULL)
2240 : 0 : error (0, 0, _("\
2241 : : DWARF data not adjusted for prelinking bias; consider prelink -u"));
2242 : : else
2243 : 0 : error (0, 0, _("\
2244 : : DWARF data in '%s' not adjusted for prelinking bias; consider prelink -u"),
2245 : : output_file);
2246 : : }
2247 : :
2248 [ + + ]: 50 : if (output_file == NULL)
2249 : : /* Modify the unstripped file in place. */
2250 : 12 : copy_elided_sections (unstripped, stripped, stripped_ehdr, bias);
2251 : : else
2252 : : {
2253 [ - + ]: 38 : if (create_dirs)
2254 : 0 : make_directories (output_file);
2255 : :
2256 : : /* Copy the unstripped file and then modify it. */
2257 : 76 : int outfd = open (output_file, O_RDWR | O_CREAT,
2258 [ + + ]: 38 : (stripped_ehdr->e_type == ET_REL
2259 : : ? DEFFILEMODE : ACCESSPERMS));
2260 [ - + ]: 38 : if (outfd < 0)
2261 : 0 : error_exit (errno, _("cannot open '%s'"), output_file);
2262 : 38 : Elf *outelf = elf_begin (outfd, ELF_C_WRITE, NULL);
2263 [ - + ]: 38 : ELF_CHECK (outelf != NULL, _("cannot create ELF descriptor: %s"));
2264 : :
2265 [ - + ]: 38 : if (unstripped == NULL)
2266 : : {
2267 : : /* Actually, we are just copying out the main file as it is. */
2268 : 0 : copy_elf (outelf, stripped);
2269 [ # # ]: 0 : if (stripped_ehdr->e_type != ET_REL)
2270 : 0 : elf_flagelf (outelf, ELF_C_SET, ELF_F_LAYOUT);
2271 [ # # ]: 0 : ELF_CHECK (elf_update (outelf, ELF_C_WRITE) > 0,
2272 : : _("cannot write output file: %s"));
2273 : : }
2274 : : else
2275 : : {
2276 : 38 : copy_elf (outelf, unstripped);
2277 : 38 : copy_elided_sections (outelf, stripped, stripped_ehdr, bias);
2278 : : }
2279 : :
2280 : 32 : elf_end (outelf);
2281 : 32 : close (outfd);
2282 : : }
2283 : 44 : }
2284 : :
2285 : : static int
2286 : 100 : open_file (const char *file, bool writable)
2287 : : {
2288 [ + + ]: 100 : int fd = open (file, writable ? O_RDWR : O_RDONLY);
2289 [ - + ]: 100 : if (fd < 0)
2290 : 0 : error_exit (errno, _("cannot open '%s'"), file);
2291 : 100 : return fd;
2292 : : }
2293 : :
2294 : : /* Warn, and exit if not forced to continue, if some ELF header
2295 : : sanity check for the stripped and unstripped files failed. */
2296 : : static void
2297 : 0 : warn (const char *msg, bool force,
2298 : : const char *stripped_file, const char *unstripped_file)
2299 : : {
2300 [ # # # # ]: 0 : error (force ? 0 : EXIT_FAILURE, 0, "%s'%s' and '%s' %s%s.",
2301 : 0 : force ? _("WARNING: ") : "",
2302 : : stripped_file, unstripped_file, msg,
2303 : 0 : force ? "" : _(", use --force"));
2304 : 0 : }
2305 : :
2306 : : /* Handle a pair of files we need to open by name. */
2307 : : static void
2308 : 50 : handle_explicit_files (const char *output_file, bool create_dirs, bool force,
2309 : : const char *stripped_file, const char *unstripped_file)
2310 : : {
2311 : 50 : int stripped_fd = open_file (stripped_file, false);
2312 : 50 : Elf *stripped = elf_begin (stripped_fd, ELF_C_READ, NULL);
2313 : 50 : GElf_Ehdr stripped_ehdr;
2314 [ - + ]: 50 : ELF_CHECK (gelf_getehdr (stripped, &stripped_ehdr),
2315 : : _("cannot create ELF descriptor: %s"));
2316 : :
2317 : 50 : int unstripped_fd = -1;
2318 : 50 : Elf *unstripped = NULL;
2319 [ + - ]: 50 : if (unstripped_file != NULL)
2320 : : {
2321 : 50 : unstripped_fd = open_file (unstripped_file, output_file == NULL);
2322 [ + + ]: 88 : unstripped = elf_begin (unstripped_fd,
2323 : : (output_file == NULL ? ELF_C_RDWR : ELF_C_READ),
2324 : : NULL);
2325 : 50 : GElf_Ehdr unstripped_ehdr;
2326 [ - + ]: 50 : ELF_CHECK (gelf_getehdr (unstripped, &unstripped_ehdr),
2327 : : _("cannot create ELF descriptor: %s"));
2328 : :
2329 [ - + ]: 50 : if (memcmp (stripped_ehdr.e_ident,
2330 : : unstripped_ehdr.e_ident, EI_NIDENT) != 0)
2331 : 0 : warn (_("ELF header identification (e_ident) different"), force,
2332 : : stripped_file, unstripped_file);
2333 : :
2334 [ - + ]: 50 : if (stripped_ehdr.e_type != unstripped_ehdr.e_type)
2335 : 0 : warn (_("ELF header type (e_type) different"), force,
2336 : : stripped_file, unstripped_file);
2337 : :
2338 [ - + ]: 50 : if (stripped_ehdr.e_machine != unstripped_ehdr.e_machine)
2339 : 0 : warn (_("ELF header machine type (e_machine) different"), force,
2340 : : stripped_file, unstripped_file);
2341 : :
2342 [ - + ]: 50 : if (stripped_ehdr.e_phnum < unstripped_ehdr.e_phnum)
2343 : 0 : warn (_("stripped program header (e_phnum) smaller than unstripped"),
2344 : : force, stripped_file, unstripped_file);
2345 : : }
2346 : :
2347 : 50 : handle_file (output_file, create_dirs, stripped, &stripped_ehdr, unstripped);
2348 : :
2349 : 44 : elf_end (stripped);
2350 : 44 : close (stripped_fd);
2351 : :
2352 : 44 : elf_end (unstripped);
2353 : 44 : close (unstripped_fd);
2354 : 44 : }
2355 : :
2356 : :
2357 : : /* Handle a pair of files opened implicitly by libdwfl for one module. */
2358 : : static void
2359 : 0 : handle_dwfl_module (const char *output_file, bool create_dirs, bool force,
2360 : : Dwfl_Module *mod, bool all, bool ignore, bool relocate)
2361 : : {
2362 : 0 : GElf_Addr bias;
2363 : 0 : Elf *stripped = dwfl_module_getelf (mod, &bias);
2364 [ # # ]: 0 : if (stripped == NULL)
2365 : : {
2366 [ # # ]: 0 : if (ignore)
2367 : 0 : return;
2368 : :
2369 : 0 : const char *file;
2370 : 0 : const char *modname = dwfl_module_info (mod, NULL, NULL, NULL,
2371 : : NULL, NULL, &file, NULL);
2372 [ # # ]: 0 : if (file == NULL)
2373 : 0 : error_exit (0,
2374 : : _("cannot find stripped file for module '%s': %s"),
2375 : : modname, dwfl_errmsg (-1));
2376 : : else
2377 : 0 : error_exit (0,
2378 : : _("cannot open stripped file '%s' for module '%s': %s"),
2379 : : modname, file, dwfl_errmsg (-1));
2380 : : }
2381 : :
2382 : 0 : Elf *debug = dwarf_getelf (dwfl_module_getdwarf (mod, &bias));
2383 [ # # ]: 0 : if (debug == NULL && !all)
2384 : : {
2385 [ # # ]: 0 : if (ignore)
2386 : 0 : return;
2387 : :
2388 : 0 : const char *file;
2389 : 0 : const char *modname = dwfl_module_info (mod, NULL, NULL, NULL,
2390 : : NULL, NULL, NULL, &file);
2391 [ # # ]: 0 : if (file == NULL)
2392 : 0 : error_exit (0,
2393 : : _("cannot find debug file for module '%s': %s"),
2394 : : modname, dwfl_errmsg (-1));
2395 : : else
2396 : 0 : error_exit (0,
2397 : : _("cannot open debug file '%s' for module '%s': %s"),
2398 : : modname, file, dwfl_errmsg (-1));
2399 : : }
2400 : :
2401 [ # # ]: 0 : if (debug == stripped)
2402 : : {
2403 [ # # ]: 0 : if (all)
2404 : : debug = NULL;
2405 : : else
2406 : : {
2407 : 0 : const char *file;
2408 : 0 : const char *modname = dwfl_module_info (mod, NULL, NULL, NULL,
2409 : : NULL, NULL, &file, NULL);
2410 : 0 : error_exit (0, _("module '%s' file '%s' is not stripped"),
2411 : : modname, file);
2412 : : }
2413 : : }
2414 : :
2415 : 0 : GElf_Ehdr stripped_ehdr;
2416 [ # # ]: 0 : ELF_CHECK (gelf_getehdr (stripped, &stripped_ehdr),
2417 : : _("cannot create ELF descriptor: %s"));
2418 : :
2419 [ # # ]: 0 : if (stripped_ehdr.e_type == ET_REL)
2420 : : {
2421 [ # # ]: 0 : if (!relocate)
2422 : : {
2423 : : /* We can't use the Elf handles already open,
2424 : : because the DWARF sections have been relocated. */
2425 : :
2426 : 0 : const char *stripped_file = NULL;
2427 : 0 : const char *unstripped_file = NULL;
2428 : 0 : (void) dwfl_module_info (mod, NULL, NULL, NULL, NULL, NULL,
2429 : : &stripped_file, &unstripped_file);
2430 : :
2431 : 0 : handle_explicit_files (output_file, create_dirs, force,
2432 : : stripped_file, unstripped_file);
2433 : 0 : return;
2434 : : }
2435 : :
2436 : : /* Relocation is what we want! This ensures that all sections that can
2437 : : get sh_addr values assigned have them, even ones not used in DWARF.
2438 : : They might still be used in the symbol table. */
2439 [ # # ]: 0 : if (dwfl_module_relocations (mod) < 0)
2440 : 0 : error_exit (0,
2441 : : _("cannot cache section addresses for module '%s': %s"),
2442 : : dwfl_module_info (mod, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
2443 : : dwfl_errmsg (-1));
2444 : : }
2445 : :
2446 : 0 : handle_file (output_file, create_dirs, stripped, &stripped_ehdr, debug);
2447 : : }
2448 : :
2449 : : /* Handle one module being written to the output directory. */
2450 : : static void
2451 : 0 : handle_output_dir_module (const char *output_dir, Dwfl_Module *mod, bool force,
2452 : : bool all, bool ignore, bool modnames, bool relocate)
2453 : : {
2454 [ # # ]: 0 : if (! modnames)
2455 : : {
2456 : : /* Make sure we've searched for the ELF file. */
2457 : 0 : GElf_Addr bias;
2458 : 0 : (void) dwfl_module_getelf (mod, &bias);
2459 : : }
2460 : :
2461 : 0 : const char *file;
2462 : 0 : const char *name = dwfl_module_info (mod, NULL, NULL, NULL,
2463 : : NULL, NULL, &file, NULL);
2464 : :
2465 [ # # # # ]: 0 : if (file == NULL && ignore)
2466 : 0 : return;
2467 : :
2468 [ # # ]: 0 : char *output_file = xasprintf ("%s/%s", output_dir,
2469 : 0 : modnames ? xbasename (name) : file);
2470 : :
2471 : 0 : handle_dwfl_module (output_file, true, force, mod, all, ignore, relocate);
2472 : :
2473 : 0 : free (output_file);
2474 : : }
2475 : :
2476 : :
2477 : : static void
2478 : 56 : list_module (Dwfl_Module *mod)
2479 : : {
2480 : : /* Make sure we have searched for the files. */
2481 : 56 : GElf_Addr bias;
2482 : 56 : bool have_elf = dwfl_module_getelf (mod, &bias) != NULL;
2483 : 56 : bool have_dwarf = dwfl_module_getdwarf (mod, &bias) != NULL;
2484 : :
2485 : 56 : const char *file;
2486 : 56 : const char *debug;
2487 : 56 : Dwarf_Addr start;
2488 : 56 : Dwarf_Addr end;
2489 : 56 : const char *name = dwfl_module_info (mod, NULL, &start, &end,
2490 : : NULL, NULL, &file, &debug);
2491 [ + + - + : 56 : if (file != NULL && debug != NULL && (debug == file || !strcmp (debug, file)))
- - - - ]
2492 : 0 : debug = ".";
2493 : :
2494 : 56 : const unsigned char *id;
2495 : 56 : GElf_Addr id_vaddr;
2496 : 56 : int id_len = dwfl_module_build_id (mod, &id, &id_vaddr);
2497 : :
2498 : 56 : printf ("%#" PRIx64 "+%#" PRIx64 " ", start, end - start);
2499 : :
2500 [ + + ]: 56 : if (id_len > 0)
2501 : : {
2502 : 960 : do
2503 : 960 : printf ("%02" PRIx8, *id++);
2504 [ + + ]: 960 : while (--id_len > 0);
2505 [ + - ]: 48 : if (id_vaddr != 0)
2506 : 48 : printf ("@%#" PRIx64, id_vaddr);
2507 : : }
2508 : : else
2509 : 8 : putchar ('-');
2510 : :
2511 [ + - + + ]: 108 : printf (" %s %s %s\n",
2512 [ + + ]: 56 : file ?: have_elf ? "." : "-",
2513 [ + - ]: 56 : debug ?: have_dwarf ? "." : "-",
2514 : : name);
2515 : 56 : }
2516 : :
2517 : :
2518 : : struct match_module_info
2519 : : {
2520 : : char **patterns;
2521 : : Dwfl_Module *found;
2522 : : bool match_files;
2523 : : };
2524 : :
2525 : : static int
2526 : 56 : match_module (Dwfl_Module *mod,
2527 : : void **userdata __attribute__ ((unused)),
2528 : : const char *name,
2529 : : Dwarf_Addr start __attribute__ ((unused)),
2530 : : void *arg)
2531 : : {
2532 : 56 : struct match_module_info *info = arg;
2533 : :
2534 [ + - ]: 56 : if (info->patterns[0] == NULL) /* Match all. */
2535 : : {
2536 : 56 : match:
2537 : 56 : info->found = mod;
2538 : 56 : return DWARF_CB_ABORT;
2539 : : }
2540 : :
2541 [ # # ]: 0 : if (info->match_files)
2542 : : {
2543 : : /* Make sure we've searched for the ELF file. */
2544 : 0 : GElf_Addr bias;
2545 : 0 : (void) dwfl_module_getelf (mod, &bias);
2546 : :
2547 : 0 : const char *file;
2548 : 0 : const char *check = dwfl_module_info (mod, NULL, NULL, NULL,
2549 : : NULL, NULL, &file, NULL);
2550 [ # # # # : 0 : if (check == NULL || strcmp (check, name) != 0 || file == NULL)
# # ]
2551 : 0 : return DWARF_CB_OK;
2552 : :
2553 : 0 : name = file;
2554 : : }
2555 : :
2556 [ # # ]: 0 : for (char **p = info->patterns; *p != NULL; ++p)
2557 [ # # ]: 0 : if (fnmatch (*p, name, 0) == 0)
2558 : 0 : goto match;
2559 : :
2560 : : return DWARF_CB_OK;
2561 : : }
2562 : :
2563 : : /* Handle files opened implicitly via libdwfl. */
2564 : : static void
2565 : 10 : handle_implicit_modules (const struct arg_info *info)
2566 : : {
2567 : 10 : struct match_module_info mmi = { info->args, NULL, info->match_files };
2568 : 10 : ptrdiff_t offset = dwfl_getmodules (info->dwfl, &match_module, &mmi, 0);
2569 [ - + ]: 10 : if (offset == 0)
2570 : 0 : error_exit (0, _("no matching modules found"));
2571 : :
2572 [ + - ]: 10 : if (info->list)
2573 : 56 : do
2574 : 56 : list_module (mmi.found);
2575 : 112 : while ((offset = dwfl_getmodules (info->dwfl, &match_module, &mmi,
2576 [ + + ]: 56 : offset)) > 0);
2577 [ # # ]: 0 : else if (info->output_dir == NULL)
2578 : : {
2579 [ # # ]: 0 : if (dwfl_getmodules (info->dwfl, &match_module, &mmi, offset) != 0)
2580 : 0 : error_exit (0, _("matched more than one module"));
2581 : 0 : handle_dwfl_module (info->output_file, false, info->force, mmi.found,
2582 : 0 : info->all, info->ignore, info->relocate);
2583 : : }
2584 : : else
2585 : 0 : do
2586 : 0 : handle_output_dir_module (info->output_dir, mmi.found, info->force,
2587 : 0 : info->all, info->ignore,
2588 : 0 : info->modnames, info->relocate);
2589 : 0 : while ((offset = dwfl_getmodules (info->dwfl, &match_module, &mmi,
2590 [ # # ]: 0 : offset)) > 0);
2591 : 10 : }
2592 : :
2593 : : int
2594 : 60 : main (int argc, char **argv)
2595 : : {
2596 : : /* We use no threads here which can interfere with handling a stream. */
2597 : 60 : __fsetlocking (stdin, FSETLOCKING_BYCALLER);
2598 : 60 : __fsetlocking (stdout, FSETLOCKING_BYCALLER);
2599 : 60 : __fsetlocking (stderr, FSETLOCKING_BYCALLER);
2600 : :
2601 : : /* Set locale. */
2602 : 60 : setlocale (LC_ALL, "");
2603 : :
2604 : : /* Make sure the message catalog can be found. */
2605 : 60 : bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
2606 : :
2607 : : /* Initialize the message catalog. */
2608 : 60 : textdomain (PACKAGE_TARNAME);
2609 : :
2610 : : /* Parse and process arguments. */
2611 : 60 : const struct argp_child argp_children[] =
2612 : : {
2613 : : {
2614 : 60 : .argp = dwfl_standard_argp (),
2615 : : .header = N_("Input selection options:"),
2616 : : .group = 1,
2617 : : },
2618 : : { .argp = NULL },
2619 : : };
2620 : 60 : const struct argp argp =
2621 : : {
2622 : : .options = options,
2623 : : .parser = parse_opt,
2624 : : .children = argp_children,
2625 : : .args_doc = N_("STRIPPED-FILE DEBUG-FILE\n[MODULE...]"),
2626 : : .doc = N_("\
2627 : : Combine stripped files with separate symbols and debug information.\n\
2628 : : \n\
2629 : : The first form puts the result in DEBUG-FILE if -o was not given.\n\
2630 : : \n\
2631 : : MODULE arguments give file name patterns matching modules to process.\n\
2632 : : With -f these match the file name of the main (stripped) file \
2633 : : (slashes are never special), otherwise they match the simple module names. \
2634 : : With no arguments, process all modules found.\n\
2635 : : \n\
2636 : : Multiple modules are written to files under OUTPUT-DIRECTORY, \
2637 : : creating subdirectories as needed. \
2638 : : With -m these files have simple module names, otherwise they have the \
2639 : : name of the main file complete with directory underneath OUTPUT-DIRECTORY.\n\
2640 : : \n\
2641 : : With -n no files are written, but one line to standard output for each module:\
2642 : : \n\tSTART+SIZE BUILDID FILE DEBUGFILE MODULENAME\n\
2643 : : START and SIZE are hexadecimal giving the address bounds of the module. \
2644 : : BUILDID is hexadecimal for the build ID bits, or - if no ID is known; \
2645 : : the hexadecimal may be followed by @0xADDR giving the address where the \
2646 : : ID resides if that is known. \
2647 : : FILE is the file name found for the module, or - if none was found, \
2648 : : or . if an ELF image is available but not from any named file. \
2649 : : DEBUGFILE is the separate debuginfo file name, \
2650 : : or - if no debuginfo was found, or . if FILE contains the debug information.\
2651 : : ")
2652 : : };
2653 : :
2654 : 60 : int remaining;
2655 : 60 : struct arg_info info = { .args = NULL };
2656 : 60 : error_t result = argp_parse (&argp, argc, argv, 0, &remaining, &info);
2657 [ + + ]: 60 : if (result == ENOSYS)
2658 [ - + ]: 50 : assert (info.dwfl == NULL);
2659 [ + - ]: 10 : else if (result)
2660 : : return EXIT_FAILURE;
2661 [ - + ]: 60 : assert (info.args != NULL);
2662 : :
2663 : : /* Tell the library which version we are expecting. */
2664 : 60 : elf_version (EV_CURRENT);
2665 : :
2666 [ + + ]: 60 : if (info.dwfl == NULL)
2667 : : {
2668 [ - + ]: 50 : assert (result == ENOSYS);
2669 : :
2670 [ - + ]: 50 : if (info.output_dir != NULL)
2671 : : {
2672 : 0 : char *file = xasprintf ("%s/%s", info.output_dir, info.args[0]);
2673 : 0 : handle_explicit_files (file, true, info.force,
2674 : 0 : info.args[0], info.args[1]);
2675 : 0 : free (file);
2676 : : }
2677 : : else
2678 : 50 : handle_explicit_files (info.output_file, false, info.force,
2679 : 50 : info.args[0], info.args[1]);
2680 : : }
2681 : : else
2682 : : {
2683 : : /* parse_opt checked this. */
2684 [ + - + - : 10 : assert (info.output_file != NULL || info.output_dir != NULL || info.list);
- + ]
2685 : :
2686 : 10 : handle_implicit_modules (&info);
2687 : :
2688 : 10 : dwfl_end (info.dwfl);
2689 : : }
2690 : :
2691 : : return 0;
2692 : : }
2693 : :
2694 : :
2695 : : #include "debugpred.h"
|