Error handling when argument file is not ELF object and style(9)
[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>
f4b470b9
SB
27#include <stdio.h>
28#include <stdlib.h>
1960d10e 29#include <string.h>
f4b470b9
SB
30#include <sysexits.h>
31#include <unistd.h>
32
1960d10e 33#include "elf.h"
f4b470b9 34
1960d10e 35extern char *__progname;
f4b470b9 36
ef31b1d0 37static void
1960d10e
SB
38usage(void)
39{
ef31b1d0
SB
40 fprintf(stderr, "Usage: %s file\n", __progname);
41 exit(EX_USAGE);
42}
43
3131de8b
SB
44int
45main(int argc, char *argv[])
46{
1960d10e
SB
47 int rc, has_debug;
48 Elf_Obj *e = NULL;
49 Elf_Shdr *shstr = NULL;
f4b470b9 50
ef31b1d0
SB
51 if (argc == 1)
52 usage();
53
1960d10e
SB
54 /* load elf binary in memory */
55 e = elf_init(argv[1]);
d0dd0df1
SB
56 if (e == NULL)
57 errx(EX_DATAERR, "elf_init");
ef31b1d0 58
1960d10e
SB
59 /* load string stable */
60 shstr = elf_strtab(e);
f4b470b9 61
1960d10e
SB
62 /* search for sections name with debug prefix */
63 has_debug = elf_debug(e);
f4b470b9 64
1960d10e 65 printf("%s\n", (has_debug > 0) ? "HAS DEBUG" : "NO DEBUG");
f4b470b9 66
1960d10e 67 rc = elf_destroy(e);
f4b470b9
SB
68
69 return (rc);
70}