Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame] | 1 | /* Print information from ELF file in human-readable form. |
| 2 | Copyright (C) 2005, 2006, 2007, 2009, 2011, 2012, 2014, 2015 Red Hat, Inc. |
| 3 | This file is part of elfutils. |
| 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2005. |
| 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 <error.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <inttypes.h> |
| 27 | #include <libintl.h> |
| 28 | #include <locale.h> |
| 29 | #include <stdbool.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdio_ext.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | #include <libeu.h> |
| 37 | #include <system.h> |
| 38 | #include <color.h> |
| 39 | #include <printversion.h> |
| 40 | #include "../libebl/libeblP.h" |
| 41 | |
| 42 | |
| 43 | /* Name and version of program. */ |
| 44 | ARGP_PROGRAM_VERSION_HOOK_DEF = print_version; |
| 45 | |
| 46 | /* Bug report address. */ |
| 47 | ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT; |
| 48 | |
| 49 | |
| 50 | /* Definitions of arguments for argp functions. */ |
| 51 | static const struct argp_option options[] = |
| 52 | { |
| 53 | { NULL, 0, NULL, 0, N_("Mode selection:"), 0 }, |
| 54 | { "reloc", 'r', NULL, 0, N_("Display relocation information."), 0 }, |
| 55 | { "full-contents", 's', NULL, 0, |
| 56 | N_("Display the full contents of all sections requested"), 0 }, |
| 57 | { "disassemble", 'd', NULL, 0, |
| 58 | N_("Display assembler code of executable sections"), 0 }, |
| 59 | |
| 60 | { NULL, 0, NULL, 0, N_("Output content selection:"), 0 }, |
| 61 | { "section", 'j', "NAME", 0, |
| 62 | N_("Only display information for section NAME."), 0 }, |
| 63 | |
| 64 | { NULL, 0, NULL, 0, NULL, 0 } |
| 65 | }; |
| 66 | |
| 67 | /* Short description of program. */ |
| 68 | static const char doc[] = N_("\ |
| 69 | Show information from FILEs (a.out by default)."); |
| 70 | |
| 71 | /* Strings for arguments in help texts. */ |
| 72 | static const char args_doc[] = N_("[FILE...]"); |
| 73 | |
| 74 | /* Prototype for option handler. */ |
| 75 | static error_t parse_opt (int key, char *arg, struct argp_state *state); |
| 76 | |
| 77 | /* Parser children. */ |
| 78 | static struct argp_child argp_children[] = |
| 79 | { |
| 80 | { &color_argp, 0, N_("Output formatting"), 2 }, |
| 81 | { NULL, 0, NULL, 0} |
| 82 | }; |
| 83 | |
| 84 | /* Data structure to communicate with argp functions. */ |
| 85 | static const struct argp argp = |
| 86 | { |
| 87 | options, parse_opt, args_doc, doc, argp_children, NULL, NULL |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | /* Print symbols in file named FNAME. */ |
| 92 | static int process_file (const char *fname, bool more_than_one); |
| 93 | |
| 94 | /* Handle content of archive. */ |
| 95 | static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname, |
| 96 | const char *suffix); |
| 97 | |
| 98 | /* Handle ELF file. */ |
| 99 | static int handle_elf (Elf *elf, const char *prefix, const char *fname, |
| 100 | const char *suffix); |
| 101 | |
| 102 | |
| 103 | #define INTERNAL_ERROR(fname) \ |
| 104 | error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s): %s"), \ |
| 105 | fname, __LINE__, PACKAGE_VERSION, elf_errmsg (-1)) |
| 106 | |
| 107 | |
| 108 | /* List of sections which should be used. */ |
| 109 | static struct section_list |
| 110 | { |
| 111 | bool is_name; |
| 112 | union |
| 113 | { |
| 114 | const char *name; |
| 115 | uint32_t scnndx; |
| 116 | }; |
| 117 | struct section_list *next; |
| 118 | } *section_list; |
| 119 | |
| 120 | |
| 121 | /* If true print archive index. */ |
| 122 | static bool print_relocs; |
| 123 | |
| 124 | /* If true print full contents of requested sections. */ |
| 125 | static bool print_full_content; |
| 126 | |
| 127 | /* If true print disassembled output.. */ |
| 128 | static bool print_disasm; |
| 129 | |
| 130 | |
| 131 | int |
| 132 | main (int argc, char *argv[]) |
| 133 | { |
| 134 | /* We use no threads here which can interfere with handling a stream. */ |
| 135 | (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER); |
| 136 | (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER); |
| 137 | (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER); |
| 138 | |
| 139 | /* Set locale. */ |
| 140 | (void) setlocale (LC_ALL, ""); |
| 141 | |
| 142 | /* Make sure the message catalog can be found. */ |
| 143 | (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR); |
| 144 | |
| 145 | /* Initialize the message catalog. */ |
| 146 | (void) textdomain (PACKAGE_TARNAME); |
| 147 | |
| 148 | /* Parse and process arguments. */ |
| 149 | int remaining; |
| 150 | (void) argp_parse (&argp, argc, argv, 0, &remaining, NULL); |
| 151 | |
| 152 | /* Tell the library which version we are expecting. */ |
| 153 | (void) elf_version (EV_CURRENT); |
| 154 | |
| 155 | int result = 0; |
| 156 | if (remaining == argc) |
| 157 | /* The user didn't specify a name so we use a.out. */ |
| 158 | result = process_file ("a.out", false); |
| 159 | else |
| 160 | { |
| 161 | /* Process all the remaining files. */ |
| 162 | const bool more_than_one = remaining + 1 < argc; |
| 163 | |
| 164 | do |
| 165 | result |= process_file (argv[remaining], more_than_one); |
| 166 | while (++remaining < argc); |
| 167 | } |
| 168 | |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /* Handle program arguments. */ |
| 174 | static error_t |
| 175 | parse_opt (int key, char *arg, |
| 176 | struct argp_state *state __attribute__ ((unused))) |
| 177 | { |
| 178 | /* True if any of the control options is set. */ |
| 179 | static bool any_control_option; |
| 180 | |
| 181 | switch (key) |
| 182 | { |
| 183 | case 'j': |
| 184 | { |
| 185 | struct section_list *newp = xmalloc (sizeof (*newp)); |
| 186 | char *endp; |
| 187 | newp->scnndx = strtoul (arg, &endp, 0); |
| 188 | if (*endp == 0) |
| 189 | newp->is_name = false; |
| 190 | else |
| 191 | { |
| 192 | newp->name = arg; |
| 193 | newp->is_name = true; |
| 194 | } |
| 195 | newp->next = section_list; |
| 196 | section_list = newp; |
| 197 | } |
| 198 | any_control_option = true; |
| 199 | break; |
| 200 | |
| 201 | case 'd': |
| 202 | print_disasm = true; |
| 203 | any_control_option = true; |
| 204 | break; |
| 205 | |
| 206 | case 'r': |
| 207 | print_relocs = true; |
| 208 | any_control_option = true; |
| 209 | break; |
| 210 | |
| 211 | case 's': |
| 212 | print_full_content = true; |
| 213 | any_control_option = true; |
| 214 | break; |
| 215 | |
| 216 | case ARGP_KEY_FINI: |
| 217 | if (! any_control_option) |
| 218 | { |
| 219 | fputs (gettext ("No operation specified.\n"), stderr); |
| 220 | argp_help (&argp, stderr, ARGP_HELP_SEE, |
| 221 | program_invocation_short_name); |
| 222 | exit (EXIT_FAILURE); |
| 223 | } |
| 224 | /* We only use this for checking the number of arguments, we don't |
| 225 | actually want to consume them. */ |
| 226 | FALLTHROUGH; |
| 227 | default: |
| 228 | return ARGP_ERR_UNKNOWN; |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /* Open the file and determine the type. */ |
| 235 | static int |
| 236 | process_file (const char *fname, bool more_than_one) |
| 237 | { |
| 238 | /* Open the file. */ |
| 239 | int fd = open (fname, O_RDONLY); |
| 240 | if (fd == -1) |
| 241 | { |
| 242 | error (0, errno, gettext ("cannot open %s"), fname); |
| 243 | return 1; |
| 244 | } |
| 245 | |
| 246 | /* Now get the ELF descriptor. */ |
| 247 | Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL); |
| 248 | if (elf != NULL) |
| 249 | { |
| 250 | if (elf_kind (elf) == ELF_K_ELF) |
| 251 | { |
| 252 | int result = handle_elf (elf, more_than_one ? "" : NULL, |
| 253 | fname, NULL); |
| 254 | |
| 255 | if (elf_end (elf) != 0) |
| 256 | INTERNAL_ERROR (fname); |
| 257 | |
| 258 | if (close (fd) != 0) |
| 259 | error (EXIT_FAILURE, errno, gettext ("while close `%s'"), fname); |
| 260 | |
| 261 | return result; |
| 262 | } |
| 263 | else if (elf_kind (elf) == ELF_K_AR) |
| 264 | { |
| 265 | int result = handle_ar (fd, elf, NULL, fname, NULL); |
| 266 | |
| 267 | if (elf_end (elf) != 0) |
| 268 | INTERNAL_ERROR (fname); |
| 269 | |
| 270 | if (close (fd) != 0) |
| 271 | error (EXIT_FAILURE, errno, gettext ("while close `%s'"), fname); |
| 272 | |
| 273 | return result; |
| 274 | } |
| 275 | |
| 276 | /* We cannot handle this type. Close the descriptor anyway. */ |
| 277 | if (elf_end (elf) != 0) |
| 278 | INTERNAL_ERROR (fname); |
| 279 | } |
| 280 | |
| 281 | error (0, 0, gettext ("%s: File format not recognized"), fname); |
| 282 | |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | static int |
| 288 | handle_ar (int fd, Elf *elf, const char *prefix, const char *fname, |
| 289 | const char *suffix) |
| 290 | { |
| 291 | size_t fname_len = strlen (fname) + 1; |
| 292 | size_t prefix_len = prefix != NULL ? strlen (prefix) : 0; |
| 293 | char new_prefix[prefix_len + fname_len + 2]; |
| 294 | size_t suffix_len = suffix != NULL ? strlen (suffix) : 0; |
| 295 | char new_suffix[suffix_len + 2]; |
| 296 | Elf *subelf; |
| 297 | Elf_Cmd cmd = ELF_C_READ_MMAP; |
| 298 | int result = 0; |
| 299 | |
| 300 | char *cp = new_prefix; |
| 301 | if (prefix != NULL) |
| 302 | cp = stpcpy (cp, prefix); |
| 303 | cp = stpcpy (cp, fname); |
| 304 | stpcpy (cp, "["); |
| 305 | |
| 306 | cp = new_suffix; |
| 307 | if (suffix != NULL) |
| 308 | cp = stpcpy (cp, suffix); |
| 309 | stpcpy (cp, "]"); |
| 310 | |
| 311 | /* Process all the files contained in the archive. */ |
| 312 | while ((subelf = elf_begin (fd, cmd, elf)) != NULL) |
| 313 | { |
| 314 | /* The the header for this element. */ |
| 315 | Elf_Arhdr *arhdr = elf_getarhdr (subelf); |
| 316 | |
| 317 | /* Skip over the index entries. */ |
| 318 | if (strcmp (arhdr->ar_name, "/") != 0 |
| 319 | && strcmp (arhdr->ar_name, "//") != 0) |
| 320 | { |
| 321 | if (elf_kind (subelf) == ELF_K_ELF) |
| 322 | result |= handle_elf (subelf, new_prefix, arhdr->ar_name, |
| 323 | new_suffix); |
| 324 | else if (elf_kind (subelf) == ELF_K_AR) |
| 325 | result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name, |
| 326 | new_suffix); |
| 327 | else |
| 328 | { |
| 329 | error (0, 0, gettext ("%s%s%s: file format not recognized"), |
| 330 | new_prefix, arhdr->ar_name, new_suffix); |
| 331 | result = 1; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /* Get next archive element. */ |
| 336 | cmd = elf_next (subelf); |
| 337 | if (elf_end (subelf) != 0) |
| 338 | INTERNAL_ERROR (fname); |
| 339 | } |
| 340 | |
| 341 | return result; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | static void |
| 346 | show_relocs_x (Ebl *ebl, GElf_Shdr *shdr, Elf_Data *symdata, |
| 347 | Elf_Data *xndxdata, size_t symstrndx, size_t shstrndx, |
| 348 | GElf_Addr r_offset, GElf_Xword r_info, GElf_Sxword r_addend) |
| 349 | { |
| 350 | int elfclass = gelf_getclass (ebl->elf); |
| 351 | char buf[128]; |
| 352 | |
| 353 | printf ("%0*" PRIx64 " %-20s ", |
| 354 | elfclass == ELFCLASS32 ? 8 : 16, r_offset, |
| 355 | ebl_reloc_type_name (ebl, GELF_R_TYPE (r_info), buf, sizeof (buf))); |
| 356 | |
| 357 | Elf32_Word xndx; |
| 358 | GElf_Sym symmem; |
| 359 | GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata, GELF_R_SYM (r_info), |
| 360 | &symmem, &xndx); |
| 361 | |
| 362 | if (sym == NULL) |
| 363 | printf ("<%s %ld>", |
| 364 | gettext ("INVALID SYMBOL"), (long int) GELF_R_SYM (r_info)); |
| 365 | else if (GELF_ST_TYPE (sym->st_info) != STT_SECTION) |
| 366 | printf ("%s", |
| 367 | elf_strptr (ebl->elf, symstrndx, sym->st_name)); |
| 368 | else |
| 369 | { |
| 370 | GElf_Shdr destshdr_mem; |
| 371 | GElf_Shdr *destshdr; |
| 372 | destshdr = gelf_getshdr (elf_getscn (ebl->elf, |
| 373 | sym->st_shndx == SHN_XINDEX |
| 374 | ? xndx : sym->st_shndx), |
| 375 | &destshdr_mem); |
| 376 | |
| 377 | if (shdr == NULL || destshdr == NULL) |
| 378 | printf ("<%s %ld>", |
| 379 | gettext ("INVALID SECTION"), |
| 380 | (long int) (sym->st_shndx == SHN_XINDEX |
| 381 | ? xndx : sym->st_shndx)); |
| 382 | else |
| 383 | printf ("%s", |
| 384 | elf_strptr (ebl->elf, shstrndx, destshdr->sh_name)); |
| 385 | } |
| 386 | |
| 387 | if (r_addend != 0) |
| 388 | { |
| 389 | char sign = '+'; |
| 390 | if (r_addend < 0) |
| 391 | { |
| 392 | sign = '-'; |
| 393 | r_addend = -r_addend; |
| 394 | } |
| 395 | printf ("%c%#" PRIx64, sign, r_addend); |
| 396 | } |
| 397 | putchar ('\n'); |
| 398 | } |
| 399 | |
| 400 | |
| 401 | static void |
| 402 | show_relocs_rel (Ebl *ebl, GElf_Shdr *shdr, Elf_Data *data, |
| 403 | Elf_Data *symdata, Elf_Data *xndxdata, size_t symstrndx, |
| 404 | size_t shstrndx) |
| 405 | { |
| 406 | size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT); |
| 407 | int nentries = shdr->sh_size / sh_entsize; |
| 408 | |
| 409 | for (int cnt = 0; cnt < nentries; ++cnt) |
| 410 | { |
| 411 | GElf_Rel relmem; |
| 412 | GElf_Rel *rel; |
| 413 | |
| 414 | rel = gelf_getrel (data, cnt, &relmem); |
| 415 | if (rel != NULL) |
| 416 | show_relocs_x (ebl, shdr, symdata, xndxdata, symstrndx, shstrndx, |
| 417 | rel->r_offset, rel->r_info, 0); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | |
| 422 | static void |
| 423 | show_relocs_rela (Ebl *ebl, GElf_Shdr *shdr, Elf_Data *data, |
| 424 | Elf_Data *symdata, Elf_Data *xndxdata, size_t symstrndx, |
| 425 | size_t shstrndx) |
| 426 | { |
| 427 | size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT); |
| 428 | int nentries = shdr->sh_size / sh_entsize; |
| 429 | |
| 430 | for (int cnt = 0; cnt < nentries; ++cnt) |
| 431 | { |
| 432 | GElf_Rela relmem; |
| 433 | GElf_Rela *rel; |
| 434 | |
| 435 | rel = gelf_getrela (data, cnt, &relmem); |
| 436 | if (rel != NULL) |
| 437 | show_relocs_x (ebl, shdr, symdata, xndxdata, symstrndx, shstrndx, |
| 438 | rel->r_offset, rel->r_info, rel->r_addend); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | |
| 443 | static bool |
| 444 | section_match (Elf *elf, uint32_t scnndx, GElf_Shdr *shdr, size_t shstrndx) |
| 445 | { |
| 446 | if (section_list == NULL) |
| 447 | return true; |
| 448 | |
| 449 | struct section_list *runp = section_list; |
| 450 | const char *name = elf_strptr (elf, shstrndx, shdr->sh_name); |
| 451 | |
| 452 | do |
| 453 | { |
| 454 | if (runp->is_name) |
| 455 | { |
| 456 | if (name && strcmp (runp->name, name) == 0) |
| 457 | return true; |
| 458 | } |
| 459 | else |
| 460 | { |
| 461 | if (runp->scnndx == scnndx) |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | runp = runp->next; |
| 466 | } |
| 467 | while (runp != NULL); |
| 468 | |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | |
| 473 | static int |
| 474 | show_relocs (Ebl *ebl, const char *fname, uint32_t shstrndx) |
| 475 | { |
| 476 | int elfclass = gelf_getclass (ebl->elf); |
| 477 | |
| 478 | Elf_Scn *scn = NULL; |
| 479 | while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) |
| 480 | { |
| 481 | GElf_Shdr shdr_mem; |
| 482 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); |
| 483 | |
| 484 | if (shdr == NULL) |
| 485 | INTERNAL_ERROR (fname); |
| 486 | |
| 487 | if (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA) |
| 488 | { |
| 489 | if (! section_match (ebl->elf, elf_ndxscn (scn), shdr, shstrndx)) |
| 490 | continue; |
| 491 | |
| 492 | GElf_Shdr destshdr_mem; |
| 493 | GElf_Shdr *destshdr = gelf_getshdr (elf_getscn (ebl->elf, |
| 494 | shdr->sh_info), |
| 495 | &destshdr_mem); |
| 496 | if (unlikely (destshdr == NULL)) |
| 497 | continue; |
| 498 | |
| 499 | printf (gettext ("\nRELOCATION RECORDS FOR [%s]:\n" |
| 500 | "%-*s TYPE VALUE\n"), |
| 501 | elf_strptr (ebl->elf, shstrndx, destshdr->sh_name), |
| 502 | elfclass == ELFCLASS32 ? 8 : 16, gettext ("OFFSET")); |
| 503 | |
| 504 | /* Get the data of the section. */ |
| 505 | Elf_Data *data = elf_getdata (scn, NULL); |
| 506 | if (data == NULL) |
| 507 | continue; |
| 508 | |
| 509 | /* Get the symbol table information. */ |
| 510 | Elf_Scn *symscn = elf_getscn (ebl->elf, shdr->sh_link); |
| 511 | GElf_Shdr symshdr_mem; |
| 512 | GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem); |
| 513 | Elf_Data *symdata = elf_getdata (symscn, NULL); |
| 514 | if (unlikely (symshdr == NULL || symdata == NULL)) |
| 515 | continue; |
| 516 | |
| 517 | /* Search for the optional extended section index table. */ |
| 518 | Elf_Data *xndxdata = NULL; |
| 519 | Elf_Scn *xndxscn = NULL; |
| 520 | while ((xndxscn = elf_nextscn (ebl->elf, xndxscn)) != NULL) |
| 521 | { |
| 522 | GElf_Shdr xndxshdr_mem; |
| 523 | GElf_Shdr *xndxshdr; |
| 524 | |
| 525 | xndxshdr = gelf_getshdr (xndxscn, &xndxshdr_mem); |
| 526 | if (xndxshdr != NULL && xndxshdr->sh_type == SHT_SYMTAB_SHNDX |
| 527 | && xndxshdr->sh_link == elf_ndxscn (symscn)) |
| 528 | { |
| 529 | /* Found it. */ |
| 530 | xndxdata = elf_getdata (xndxscn, NULL); |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | if (shdr->sh_type == SHT_REL) |
| 536 | show_relocs_rel (ebl, shdr, data, symdata, xndxdata, |
| 537 | symshdr->sh_link, shstrndx); |
| 538 | else |
| 539 | show_relocs_rela (ebl, shdr, data, symdata, xndxdata, |
| 540 | symshdr->sh_link, shstrndx); |
| 541 | |
| 542 | putchar ('\n'); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | |
| 550 | static int |
| 551 | show_full_content (Ebl *ebl, const char *fname, uint32_t shstrndx) |
| 552 | { |
| 553 | Elf_Scn *scn = NULL; |
| 554 | while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) |
| 555 | { |
| 556 | GElf_Shdr shdr_mem; |
| 557 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); |
| 558 | |
| 559 | if (shdr == NULL) |
| 560 | INTERNAL_ERROR (fname); |
| 561 | |
| 562 | if (shdr->sh_type == SHT_PROGBITS && shdr->sh_size > 0) |
| 563 | { |
| 564 | if (! section_match (ebl->elf, elf_ndxscn (scn), shdr, shstrndx)) |
| 565 | continue; |
| 566 | |
| 567 | printf (gettext ("Contents of section %s:\n"), |
| 568 | elf_strptr (ebl->elf, shstrndx, shdr->sh_name)); |
| 569 | |
| 570 | /* Get the data of the section. */ |
| 571 | Elf_Data *data = elf_getdata (scn, NULL); |
| 572 | if (data == NULL) |
| 573 | continue; |
| 574 | |
| 575 | unsigned char *cp = data->d_buf; |
| 576 | size_t cnt; |
| 577 | for (cnt = 0; cnt + 16 < data->d_size; cp += 16, cnt += 16) |
| 578 | { |
| 579 | printf (" %04zx ", cnt); |
| 580 | |
| 581 | for (size_t inner = 0; inner < 16; inner += 4) |
| 582 | printf ("%02hhx%02hhx%02hhx%02hhx ", |
| 583 | cp[inner], cp[inner + 1], cp[inner + 2], |
| 584 | cp[inner + 3]); |
| 585 | fputc_unlocked (' ', stdout); |
| 586 | |
| 587 | for (size_t inner = 0; inner < 16; ++inner) |
| 588 | fputc_unlocked (isascii (cp[inner]) && isprint (cp[inner]) |
| 589 | ? cp[inner] : '.', stdout); |
| 590 | fputc_unlocked ('\n', stdout); |
| 591 | } |
| 592 | |
| 593 | printf (" %04zx ", cnt); |
| 594 | |
| 595 | size_t remaining = data->d_size - cnt; |
| 596 | size_t inner; |
| 597 | for (inner = 0; inner + 4 <= remaining; inner += 4) |
| 598 | printf ("%02hhx%02hhx%02hhx%02hhx ", |
| 599 | cp[inner], cp[inner + 1], cp[inner + 2], cp[inner + 3]); |
| 600 | |
| 601 | for (; inner < remaining; ++inner) |
| 602 | printf ("%02hhx", cp[inner]); |
| 603 | |
| 604 | for (inner = 2 * (16 - inner) + (16 - inner + 3) / 4 + 1; inner > 0; |
| 605 | --inner) |
| 606 | fputc_unlocked (' ', stdout); |
| 607 | |
| 608 | for (inner = 0; inner < remaining; ++inner) |
| 609 | fputc_unlocked (isascii (cp[inner]) && isprint (cp[inner]) |
| 610 | ? cp[inner] : '.', stdout); |
| 611 | fputc_unlocked ('\n', stdout); |
| 612 | |
| 613 | fputc_unlocked ('\n', stdout); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | struct disasm_info |
| 622 | { |
| 623 | GElf_Addr addr; |
| 624 | const uint8_t *cur; |
| 625 | const uint8_t *last_end; |
| 626 | const char *address_color; |
| 627 | const char *bytes_color; |
| 628 | }; |
| 629 | |
| 630 | |
| 631 | // XXX This is not the preferred output for all architectures. Needs |
| 632 | // XXX customization, too. |
| 633 | static int |
| 634 | disasm_output (char *buf, size_t buflen, void *arg) |
| 635 | { |
| 636 | struct disasm_info *info = (struct disasm_info *) arg; |
| 637 | |
| 638 | if (info->address_color != NULL) |
| 639 | printf ("%s%8" PRIx64 "%s: ", |
| 640 | info->address_color, (uint64_t) info->addr, color_off); |
| 641 | else |
| 642 | printf ("%8" PRIx64 ": ", (uint64_t) info->addr); |
| 643 | |
| 644 | if (info->bytes_color != NULL) |
| 645 | fputs_unlocked (info->bytes_color, stdout); |
| 646 | size_t cnt; |
| 647 | for (cnt = 0; cnt < (size_t) MIN (info->cur - info->last_end, 8); ++cnt) |
| 648 | printf (" %02" PRIx8, info->last_end[cnt]); |
| 649 | if (info->bytes_color != NULL) |
| 650 | fputs_unlocked (color_off, stdout); |
| 651 | |
| 652 | printf ("%*s %.*s\n", |
| 653 | (int) (8 - cnt) * 3 + 1, "", (int) buflen, buf); |
| 654 | |
| 655 | info->addr += cnt; |
| 656 | |
| 657 | /* We limit the number of bytes printed before the mnemonic to 8. |
| 658 | Print the rest on a separate, following line. */ |
| 659 | if (info->cur - info->last_end > 8) |
| 660 | { |
| 661 | if (info->address_color != NULL) |
| 662 | printf ("%s%8" PRIx64 "%s: ", |
| 663 | info->address_color, (uint64_t) info->addr, color_off); |
| 664 | else |
| 665 | printf ("%8" PRIx64 ": ", (uint64_t) info->addr); |
| 666 | |
| 667 | if (info->bytes_color != NULL) |
| 668 | fputs_unlocked (info->bytes_color, stdout); |
| 669 | for (; cnt < (size_t) (info->cur - info->last_end); ++cnt) |
| 670 | printf (" %02" PRIx8, info->last_end[cnt]); |
| 671 | if (info->bytes_color != NULL) |
| 672 | fputs_unlocked (color_off, stdout); |
| 673 | putchar_unlocked ('\n'); |
| 674 | info->addr += info->cur - info->last_end - 8; |
| 675 | } |
| 676 | |
| 677 | info->last_end = info->cur; |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | |
| 683 | static int |
| 684 | show_disasm (Ebl *ebl, const char *fname, uint32_t shstrndx) |
| 685 | { |
| 686 | DisasmCtx_t *ctx = disasm_begin (ebl, ebl->elf, NULL /* XXX TODO */); |
| 687 | if (ctx == NULL) |
| 688 | error (EXIT_FAILURE, 0, gettext ("cannot disassemble")); |
| 689 | |
| 690 | Elf_Scn *scn = NULL; |
| 691 | while ((scn = elf_nextscn (ebl->elf, scn)) != NULL) |
| 692 | { |
| 693 | GElf_Shdr shdr_mem; |
| 694 | GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); |
| 695 | |
| 696 | if (shdr == NULL) |
| 697 | INTERNAL_ERROR (fname); |
| 698 | |
| 699 | if (shdr->sh_type == SHT_PROGBITS && shdr->sh_size > 0 |
| 700 | && (shdr->sh_flags & SHF_EXECINSTR) != 0) |
| 701 | { |
| 702 | if (! section_match (ebl->elf, elf_ndxscn (scn), shdr, shstrndx)) |
| 703 | continue; |
| 704 | |
| 705 | Elf_Data *data = elf_getdata (scn, NULL); |
| 706 | if (data == NULL) |
| 707 | continue; |
| 708 | |
| 709 | printf ("Disassembly of section %s:\n\n", |
| 710 | elf_strptr (ebl->elf, shstrndx, shdr->sh_name)); |
| 711 | |
| 712 | struct disasm_info info; |
| 713 | info.addr = shdr->sh_addr; |
| 714 | info.last_end = info.cur = data->d_buf; |
| 715 | char *fmt; |
| 716 | if (color_mode) |
| 717 | { |
| 718 | info.address_color = color_address; |
| 719 | info.bytes_color = color_bytes; |
| 720 | |
| 721 | if (asprintf (&fmt, "%s%%7m %s%%.1o,%s%%.2o,%s%%.3o%%34a %s%%l", |
| 722 | color_mnemonic ?: "", |
| 723 | color_operand1 ?: "", |
| 724 | color_operand2 ?: "", |
| 725 | color_operand3 ?: "", |
| 726 | color_label ?: "") < 0) |
| 727 | error (EXIT_FAILURE, errno, _("cannot allocate memory")); |
| 728 | } |
| 729 | else |
| 730 | { |
| 731 | info.address_color = info.bytes_color = NULL; |
| 732 | |
| 733 | fmt = "%7m %.1o,%.2o,%.3o%34a %l"; |
| 734 | } |
| 735 | |
| 736 | disasm_cb (ctx, &info.cur, info.cur + data->d_size, info.addr, |
| 737 | fmt, disasm_output, &info, NULL /* XXX */); |
| 738 | |
| 739 | if (color_mode) |
| 740 | free (fmt); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | (void) disasm_end (ctx); |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | |
| 750 | static int |
| 751 | handle_elf (Elf *elf, const char *prefix, const char *fname, |
| 752 | const char *suffix) |
| 753 | { |
| 754 | |
| 755 | /* Get the backend for this object file type. */ |
| 756 | Ebl *ebl = ebl_openbackend (elf); |
| 757 | |
| 758 | printf ("%s: elf%d-%s\n\n", |
| 759 | fname, gelf_getclass (elf) == ELFCLASS32 ? 32 : 64, |
| 760 | ebl_backend_name (ebl)); |
| 761 | |
| 762 | /* Create the full name of the file. */ |
| 763 | size_t prefix_len = prefix == NULL ? 0 : strlen (prefix); |
| 764 | size_t suffix_len = suffix == NULL ? 0 : strlen (suffix); |
| 765 | size_t fname_len = strlen (fname) + 1; |
| 766 | char fullname[prefix_len + 1 + fname_len + suffix_len]; |
| 767 | char *cp = fullname; |
| 768 | if (prefix != NULL) |
| 769 | cp = mempcpy (cp, prefix, prefix_len); |
| 770 | cp = mempcpy (cp, fname, fname_len); |
| 771 | if (suffix != NULL) |
| 772 | memcpy (cp - 1, suffix, suffix_len + 1); |
| 773 | |
| 774 | /* Get the section header string table index. */ |
| 775 | size_t shstrndx; |
| 776 | if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0) |
| 777 | error (EXIT_FAILURE, 0, |
| 778 | gettext ("cannot get section header string table index")); |
| 779 | |
| 780 | int result = 0; |
| 781 | if (print_disasm) |
| 782 | result = show_disasm (ebl, fullname, shstrndx); |
| 783 | if (print_relocs && !print_disasm) |
| 784 | result = show_relocs (ebl, fullname, shstrndx); |
| 785 | if (print_full_content) |
| 786 | result = show_full_content (ebl, fullname, shstrndx); |
| 787 | |
| 788 | /* Close the ELF backend library descriptor. */ |
| 789 | ebl_closebackend (ebl); |
| 790 | |
| 791 | return result; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | #include "debugpred.h" |