8778d50643e567a36b840ed1c2588016ada13273
[platform/upstream/elfutils.git] / tests / early-offscn.c
1 /* Copyright (C) 2008 Red Hat, Inc.
2    This file is part of Red Hat elfutils.
3
4    Red Hat elfutils is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by the
6    Free Software Foundation; version 2 of the License.
7
8    Red Hat elfutils is distributed in the hope that it will be useful, but
9    WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11    General Public License for more details.
12
13    You should have received a copy of the GNU General Public License along
14    with Red Hat elfutils; if not, write to the Free Software Foundation,
15    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
16
17    Red Hat elfutils is an included package of the Open Invention Network.
18    An included package of the Open Invention Network is a package for which
19    Open Invention Network licensees cross-license their patents.  No patent
20    license is granted, either expressly or impliedly, by designation as an
21    included package.  Should you wish to participate in the Open Invention
22    Network licensing program, please visit www.openinventionnetwork.com
23    <http://www.openinventionnetwork.com>.  */
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include <errno.h>
30 #include <error.h>
31 #include <fcntl.h>
32 #include <gelf.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 int
37 main (int argc, char *argv[])
38 {
39   if (argc < 2)
40     error (1, 0, "Usage: %s FILE OFFSET", argv[0]);
41
42   /* Set the ELF version.  */
43   elf_version (EV_CURRENT);
44
45   /* Open the archive.  */
46   int fd = open (argv[1], O_RDONLY);
47   if (fd < 0)
48     error (1, errno, "cannot open '%s'", argv[1]);
49
50   Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
51   if (elf == NULL)
52     error (2, 0, "elf_begin: %s", elf_errmsg (-1));
53
54   Elf_Scn *scn = gelf_offscn (elf, strtoull (argv[2], NULL, 0));
55   if (scn == NULL)
56     error (3, 0, "gelf_offscn: %s", elf_errmsg (-1));
57
58   elf_end (elf);
59   return 0;
60 }