Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame] | 1 | /* Test program for dwelf_elf_gnu_build_id, print build ID. |
| 2 | Copyright (C) 2014 Red Hat, Inc. |
| 3 | This file is part of elfutils. |
| 4 | |
| 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | elfutils is distributed in the hope that it will be useful, but |
| 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <config.h> |
| 19 | #include <assert.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <err.h> |
| 22 | #include <errno.h> |
| 23 | #include ELFUTILS_HEADER(elf) |
| 24 | #include ELFUTILS_HEADER(dwelf) |
| 25 | #include <stdio.h> |
| 26 | #include <error.h> |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | int |
| 35 | main (int argc, char *argv[]) |
| 36 | { |
| 37 | if (argc < 2) |
| 38 | error (EXIT_FAILURE, 0, "No input file given"); |
| 39 | |
| 40 | elf_version (EV_CURRENT); |
| 41 | |
| 42 | for (int i = 1; i < argc; i++) |
| 43 | { |
| 44 | const char *file = argv[i]; |
| 45 | int fd = open (file, O_RDONLY); |
| 46 | if (fd < 0) |
| 47 | error (EXIT_FAILURE, errno, "couldn't open file '%s'", file); |
| 48 | |
| 49 | Elf *elf = elf_begin (fd, ELF_C_READ, NULL); |
| 50 | if (elf == NULL) |
| 51 | { |
| 52 | printf("%s: elf_begin failed: %s\n", file, elf_errmsg (-1)); |
| 53 | close (fd); |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | const void *build_id; |
| 58 | ssize_t len = dwelf_elf_gnu_build_id (elf, &build_id); |
| 59 | switch (len) |
| 60 | { |
| 61 | case 0: |
| 62 | printf ("%s: <no NT_GNU_BUILD_ID note>\n", file); |
| 63 | break; |
| 64 | case -1: |
| 65 | errx (1, "dwelf_elf_gnu_build_id (%s): %s", |
| 66 | file, elf_errmsg (-1)); |
| 67 | default: |
| 68 | printf ("%s: build ID: ", file); |
| 69 | const unsigned char *p = build_id; |
| 70 | const unsigned char *end = p + len; |
| 71 | while (p < end) |
| 72 | printf("%02x", (unsigned)*p++); |
| 73 | putchar('\n'); |
| 74 | } |
| 75 | |
| 76 | elf_end (elf); |
| 77 | close (fd); |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |