/src/elfutils/libdw/dwarf_begin.c
Line | Count | Source |
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 (int fd, Dwarf_Cmd cmd) |
43 | 0 | { |
44 | 0 | Elf *elf; |
45 | 0 | Elf_Cmd elfcmd; |
46 | 0 | Dwarf *result = NULL; |
47 | |
|
48 | 0 | switch (cmd) |
49 | 0 | { |
50 | 0 | case DWARF_C_READ: |
51 | 0 | elfcmd = ELF_C_READ_MMAP; |
52 | 0 | break; |
53 | 0 | case DWARF_C_WRITE: |
54 | 0 | elfcmd = ELF_C_WRITE; |
55 | 0 | break; |
56 | 0 | case DWARF_C_RDWR: |
57 | 0 | elfcmd = ELF_C_RDWR; |
58 | 0 | break; |
59 | 0 | default: |
60 | | /* No valid mode. */ |
61 | 0 | __libdw_seterrno (DWARF_E_INVALID_CMD); |
62 | 0 | return NULL; |
63 | 0 | } |
64 | | |
65 | | /* We have to call `elf_version' here since the user might have not |
66 | | done it or initialized libelf with a different version. This |
67 | | would break libdwarf since we are using the ELF data structures |
68 | | in a certain way. */ |
69 | 0 | elf_version (EV_CURRENT); |
70 | | |
71 | | /* Get an ELF descriptor. */ |
72 | 0 | elf = elf_begin (fd, elfcmd, NULL); |
73 | 0 | if (elf == NULL) |
74 | 0 | { |
75 | | /* Test why the `elf_begin" call failed. */ |
76 | 0 | struct stat st; |
77 | |
|
78 | 0 | if (fstat (fd, &st) == 0 && ! S_ISREG (st.st_mode)) |
79 | 0 | __libdw_seterrno (DWARF_E_NO_REGFILE); |
80 | 0 | else if (errno == EBADF) |
81 | 0 | __libdw_seterrno (DWARF_E_INVALID_FILE); |
82 | 0 | else |
83 | 0 | __libdw_seterrno (DWARF_E_IO_ERROR); |
84 | 0 | } |
85 | 0 | else |
86 | 0 | { |
87 | | /* Do the real work now that we have an ELF descriptor. */ |
88 | 0 | result = INTUSE(dwarf_begin_elf) (elf, cmd, NULL); |
89 | | |
90 | | /* If this failed, free the resources. */ |
91 | 0 | if (result == NULL) |
92 | 0 | elf_end (elf); |
93 | 0 | else |
94 | 0 | result->free_elf = true; |
95 | 0 | } |
96 | |
|
97 | 0 | return result; |
98 | 0 | } |
99 | | INTDEF(dwarf_begin) |