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