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 : : 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 : 223 : dwarf_begin (int fd, Dwarf_Cmd cmd) 43 : : { 44 : 223 : Elf *elf; 45 : 223 : Elf_Cmd elfcmd; 46 : 223 : Dwarf *result = NULL; 47 : : 48 [ + - ]: 223 : switch (cmd) 49 : : { 50 : : case DWARF_C_READ: 51 : : elfcmd = ELF_C_READ_MMAP; 52 : : break; 53 : : case DWARF_C_WRITE: 54 : : elfcmd = ELF_C_WRITE; 55 : : break; 56 : : case DWARF_C_RDWR: 57 : : elfcmd = ELF_C_RDWR; 58 : : break; 59 : 0 : default: 60 : : /* No valid mode. */ 61 : 0 : __libdw_seterrno (DWARF_E_INVALID_CMD); 62 : 0 : return NULL; 63 : : } 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 : 223 : elf_version (EV_CURRENT); 70 : : 71 : : /* Get an ELF descriptor. */ 72 : 223 : elf = elf_begin (fd, elfcmd, NULL); 73 [ - + ]: 223 : if (elf == NULL) 74 : : { 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 : : else 83 : 0 : __libdw_seterrno (DWARF_E_IO_ERROR); 84 : : } 85 : : else 86 : : { 87 : : /* Do the real work now that we have an ELF descriptor. */ 88 : 223 : result = INTUSE(dwarf_begin_elf) (elf, cmd, NULL); 89 : : 90 : : /* If this failed, free the resources. */ 91 [ - + ]: 223 : if (result == NULL) 92 : 0 : elf_end (elf); 93 : : else 94 : 223 : result->free_elf = true; 95 : : } 96 : : 97 : : return result; 98 : : } 99 : : INTDEF(dwarf_begin)