9f3050fdaf30024d3b2f09ea1a5fe3a38d2e4a12
[platform/upstream/elfutils.git] / libdw / dwarf_begin.c
1 /* Create descriptor from file descriptor for processing file.
2    Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
3    This file is part of elfutils.
4    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
6    This file is free software; you can redistribute it and/or modify
7    it under the terms of either
8
9      * the GNU Lesser General Public License as published by the Free
10        Software Foundation; either version 3 of the License, or (at
11        your option) any later version
12
13    or
14
15      * the GNU General Public License as published by the Free
16        Software Foundation; either version 2 of the License, or (at
17        your option) any later version
18
19    or both in parallel, as here.
20
21    elfutils is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24    General Public License for more details.
25
26    You should have received copies of the GNU General Public License and
27    the GNU Lesser General Public License along with this program.  If
28    not, see <http://www.gnu.org/licenses/>.  */
29
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33
34 #include <errno.h>
35 #include <stddef.h>
36 #include <sys/stat.h>
37
38 #include <libdwP.h>
39
40
41 Dwarf *
42 dwarf_begin (fd, cmd)
43      int fd;
44      Dwarf_Cmd cmd;
45 {
46   Elf *elf;
47   Elf_Cmd elfcmd;
48   Dwarf *result = NULL;
49
50   switch (cmd)
51     {
52     case DWARF_C_READ:
53       elfcmd = ELF_C_READ_MMAP;
54       break;
55     case DWARF_C_WRITE:
56       elfcmd = ELF_C_WRITE;
57       break;
58     case DWARF_C_RDWR:
59       elfcmd = ELF_C_RDWR;
60       break;
61     default:
62       /* No valid mode.  */
63       __libdw_seterrno (DWARF_E_INVALID_CMD);
64       return NULL;
65     }
66
67   /* We have to call `elf_version' here since the user might have not
68      done it or initialized libelf with a different version.  This
69      would break libdwarf since we are using the ELF data structures
70      in a certain way.  */
71   elf_version (EV_CURRENT);
72
73   /* Get an ELF descriptor.  */
74   elf = elf_begin (fd, elfcmd, NULL);
75   if (elf == NULL)
76     {
77       /* Test why the `elf_begin" call failed.  */
78       struct stat64 st;
79
80       if (fstat64 (fd, &st) == 0 && ! S_ISREG (st.st_mode))
81         __libdw_seterrno (DWARF_E_NO_REGFILE);
82       else if (errno == EBADF)
83         __libdw_seterrno (DWARF_E_INVALID_FILE);
84       else
85         __libdw_seterrno (DWARF_E_IO_ERROR);
86     }
87   else
88     {
89       /* Do the real work now that we have an ELF descriptor.  */
90       result = INTUSE(dwarf_begin_elf) (elf, cmd, NULL);
91
92       /* If this failed, free the resources.  */
93       if (result == NULL)
94         elf_end (elf);
95       else
96         result->free_elf = true;
97     }
98
99   return result;
100 }
101 INTDEF(dwarf_begin)