Skip sections which are not PROGBITS
[elfdbg/.git] / elfdbg.c
CommitLineData
f4b470b9
SB
1/*-
2 * Copyright (c) 2015 Sofian Brabez <sbz@FreeBSD.org>
3 * All rights reserved.
3131de8b 4 *
f4b470b9
SB
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
3131de8b 14 *
f4b470b9
SB
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
d0dd0df1 26#include <err.h>
363fc57e 27#include <locale.h>
f4b470b9
SB
28#include <stdio.h>
29#include <stdlib.h>
1960d10e 30#include <string.h>
f4b470b9
SB
31#include <sysexits.h>
32#include <unistd.h>
33
1960d10e 34#include "elf.h"
f4b470b9 35
1960d10e 36extern char *__progname;
f4b470b9 37
ef31b1d0 38static void
1960d10e
SB
39usage(void)
40{
363fc57e 41 fprintf(stderr, "Usage: %s [-qv] file\n", __progname);
ef31b1d0
SB
42 exit(EX_USAGE);
43}
44
3131de8b
SB
45int
46main(int argc, char *argv[])
47{
1960d10e 48 int rc, has_debug;
363fc57e 49 int ch, qflag=0, vflag=0;
1960d10e
SB
50 Elf_Obj *e = NULL;
51 Elf_Shdr *shstr = NULL;
f4b470b9 52
ef31b1d0
SB
53 if (argc == 1)
54 usage();
55
363fc57e
SB
56 while ((ch = getopt(argc, argv, "qhv")) != -1) {
57 switch (ch) {
58 case 'q':
59 qflag = 1;
60 break;
61 case 'v':
62 vflag = 1;
63 break;
64 case 'h':
65 default:
66 usage();
67 }
68 }
69
70 argc -= optind;
71 argv += optind;
72
1960d10e 73 /* load elf binary in memory */
363fc57e
SB
74 e = elf_init(*argv);
75 if (e == NULL)
76 errx(EX_DATAERR, "elf_init");
ef31b1d0 77
1960d10e
SB
78 /* load string stable */
79 shstr = elf_strtab(e);
f4b470b9 80
1960d10e
SB
81 /* search for sections name with debug prefix */
82 has_debug = elf_debug(e);
f4b470b9 83
363fc57e
SB
84 if (!qflag)
85 printf("%s\n", (has_debug > 0) ? "HAS DEBUG" : "NO DEBUG");
86
87 if (vflag)
88 elf_debug_print(e);
f4b470b9 89
1960d10e 90 rc = elf_destroy(e);
f4b470b9 91
363fc57e
SB
92 if (qflag) {
93 rc = has_debug > 0;
94 }
95
f4b470b9
SB
96 return (rc);
97}