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.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
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
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
19 or both in parallel, as here.
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.
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/>. */
42 dwarf_begin (int fd, Dwarf_Cmd cmd)
51 elfcmd = ELF_C_READ_MMAP;
61 __libdw_seterrno (DWARF_E_INVALID_CMD);
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
69 elf_version (EV_CURRENT);
71 /* Get an ELF descriptor. */
72 elf = elf_begin (fd, elfcmd, NULL);
75 /* Test why the `elf_begin" call failed. */
78 if (fstat (fd, &st) == 0 && ! S_ISREG (st.st_mode))
79 __libdw_seterrno (DWARF_E_NO_REGFILE);
80 else if (errno == EBADF)
81 __libdw_seterrno (DWARF_E_INVALID_FILE);
83 __libdw_seterrno (DWARF_E_IO_ERROR);
87 /* Do the real work now that we have an ELF descriptor. */
88 result = INTUSE(dwarf_begin_elf) (elf, cmd, NULL);
90 /* If this failed, free the resources. */
94 result->free_elf = true;