|
23 | 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
24 | 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | 25 | */ |
26 | | -#include <err.h> |
27 | | -#include <fcntl.h> |
28 | | -#include <gelf.h> |
29 | | -#include <stdint.h> |
30 | | -#include <string.h> |
| 26 | +#include <errno.h> |
31 | 27 | #include <stdio.h> |
32 | 28 | #include <stdlib.h> |
| 29 | +#include <string.h> |
33 | 30 | #include <sysexits.h> |
34 | 31 | #include <unistd.h> |
35 | 32 |
|
36 | | -extern char *__progname; |
37 | | - |
38 | | -static int |
39 | | -elf_debug_sections(Elf *e) |
40 | | -{ |
41 | | - Elf_Scn *scn = NULL; |
42 | | - GElf_Shdr shdr; |
43 | | - size_t n, shstrndx, sz; |
44 | | - char *name; |
45 | | - int has_debug = 0; |
46 | | - |
47 | | - if (elf_getshdrstrndx(e, &shstrndx) != 0) |
48 | | - errx(EX_SOFTWARE, "elf_getshdrstrndx() failed : %s . ", |
49 | | - elf_errmsg(-1)); |
| 33 | +#include "elf.h" |
50 | 34 |
|
51 | | - while ((scn = elf_nextscn(e, scn)) != NULL) { |
52 | | - gelf_getshdr(scn, &shdr); |
53 | | - |
54 | | - name = elf_strptr(e, shstrndx, shdr.sh_name); |
55 | | - if (!strstr(name, "debug_")) |
56 | | - continue; |
57 | | - |
58 | | - has_debug++; |
59 | | - } |
60 | | - |
61 | | - return (has_debug > 0); |
62 | | -} |
| 35 | +extern char *__progname; |
63 | 36 |
|
64 | 37 | static void |
65 | | -usage(void) { |
| 38 | +usage(void) |
| 39 | +{ |
66 | 40 | fprintf(stderr, "Usage: %s file\n", __progname); |
67 | 41 | exit(EX_USAGE); |
68 | 42 | } |
69 | 43 |
|
70 | 44 | int |
71 | 45 | main(int argc, char *argv[]) |
72 | 46 | { |
73 | | - const char *filename; |
74 | | - int fd, rc; |
75 | | - Elf *e; |
76 | | - int has_debug; |
| 47 | + int rc, has_debug; |
| 48 | + Elf_Obj *e = NULL; |
| 49 | + Elf_Shdr *shstr = NULL; |
77 | 50 |
|
78 | 51 | if (argc == 1) |
79 | 52 | usage(); |
80 | 53 |
|
81 | | - filename = argv[1]; |
| 54 | + /* load elf binary in memory */ |
| 55 | + e = elf_init(argv[1]); |
82 | 56 |
|
83 | | - if (elf_version(EV_CURRENT) == EV_NONE) |
84 | | - errx(EX_SOFTWARE, "ELF library initialization failed : %s ", |
85 | | - elf_errmsg(-1)); |
| 57 | + /* load string stable */ |
| 58 | + shstr = elf_strtab(e); |
86 | 59 |
|
87 | | - if ((fd = open(filename, O_RDONLY, 0)) < 0) |
88 | | - err(EX_NOINPUT, "open %s failed ", filename); |
89 | | - if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) |
90 | | - errx(EX_SOFTWARE, "elf_begin() failed : %s", elf_errmsg(-1)); |
91 | | - if (elf_kind(e) != ELF_K_ELF) |
92 | | - errx(EX_DATAERR, "%s is not an ELF object", filename); |
| 60 | + /* search for sections name with debug prefix */ |
| 61 | + has_debug = elf_debug(e); |
93 | 62 |
|
94 | | - has_debug = elf_debug_sections(e); |
95 | | - printf(has_debug ? "HAS DEBUG\n" : "NO DEBUG\n"); |
| 63 | + printf("%s\n", (has_debug > 0) ? "HAS DEBUG" : "NO DEBUG"); |
96 | 64 |
|
97 | | - rc = close(fd); |
98 | | - rc = elf_end(e); |
| 65 | + rc = elf_destroy(e); |
99 | 66 |
|
100 | 67 | return (rc); |
101 | 68 | } |
0 commit comments