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