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