Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Create descriptor from file descriptor for processing file. |
| 2 | Copyright (C) 2000, 2002 Red Hat, Inc. |
| 3 | Written by Ulrich Drepper <drepper@redhat.com>, 2000. |
| 4 | |
| 5 | This program is Open Source software; you can redistribute it and/or |
| 6 | modify it under the terms of the Open Software License version 1.0 as |
| 7 | published by the Open Source Initiative. |
| 8 | |
| 9 | You should have received a copy of the Open Software License along |
| 10 | with this program; if not, you may obtain a copy of the Open Software |
| 11 | License version 1.0 from http://www.opensource.org/licenses/osl.php or |
| 12 | by writing the Open Source Initiative c/o Lawrence Rosen, Esq., |
| 13 | 3001 King Ranch Road, Ukiah, CA 95482. */ |
| 14 | |
| 15 | #ifdef HAVE_CONFIG_H |
| 16 | # include <config.h> |
| 17 | #endif |
| 18 | |
| 19 | #include <stddef.h> |
| 20 | #include <sys/stat.h> |
| 21 | |
| 22 | #include <libdwarfP.h> |
| 23 | |
| 24 | |
| 25 | int |
| 26 | dwarf_init (fd, access, errhand, errarg, dbg, error) |
| 27 | int fd; |
| 28 | Dwarf_Unsigned access; |
| 29 | Dwarf_Handler errhand; |
| 30 | Dwarf_Ptr errarg; |
| 31 | Dwarf_Debug *dbg; |
| 32 | Dwarf_Error *error; |
| 33 | { |
| 34 | Elf *elf; |
| 35 | Elf_Cmd cmd; |
| 36 | struct Dwarf_Debug_s newdbg; |
| 37 | int result = DW_DLV_ERROR; |
| 38 | |
| 39 | switch (access) |
| 40 | { |
| 41 | case DW_DLC_READ: |
| 42 | cmd = ELF_C_READ_MMAP; |
| 43 | break; |
| 44 | case DW_DLC_WRITE: |
| 45 | cmd = ELF_C_WRITE; |
| 46 | break; |
| 47 | case DW_DLC_RDWR: |
| 48 | cmd = ELF_C_RDWR; |
| 49 | break; |
| 50 | default: |
| 51 | /* These are the error values we want to use. */ |
| 52 | newdbg.dbg_errhand = errhand; |
| 53 | newdbg.dbg_errarg = errarg; |
| 54 | |
| 55 | __libdwarf_error (&newdbg, error, DW_E_INVALID_ACCESS); |
| 56 | return DW_DLV_ERROR; |
| 57 | } |
| 58 | |
| 59 | /* We have to call `elf_version' here since the user might have not |
| 60 | done it or initialized libelf with a different version. This |
| 61 | would break libdwarf since we are using the ELF data structures |
| 62 | in a certain way. */ |
| 63 | elf_version (EV_CURRENT); |
| 64 | |
| 65 | /* Get an ELF descriptor. */ |
| 66 | elf = elf_begin (fd, cmd, NULL); |
| 67 | if (elf == NULL) |
| 68 | { |
| 69 | /* Test why the `elf_begin" call failed. */ |
| 70 | struct stat64 st; |
| 71 | |
| 72 | /* These are the error values we want to use. */ |
| 73 | newdbg.dbg_errhand = errhand; |
| 74 | newdbg.dbg_errarg = errarg; |
| 75 | |
| 76 | if (fstat64 (fd, &st) == 0 && ! S_ISREG (st.st_mode)) |
| 77 | __libdwarf_error (&newdbg, error, DW_E_NO_REGFILE); |
| 78 | else |
| 79 | __libdwarf_error (&newdbg, error, DW_E_IO_ERROR); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | /* Do the real work now that we have an ELF descriptor. */ |
| 84 | result = dwarf_elf_init (elf, access, errhand, errarg, dbg, error); |
| 85 | |
| 86 | /* If this failed, free the resources. */ |
| 87 | if (result != DW_DLV_OK) |
| 88 | elf_end (elf); |
| 89 | } |
| 90 | |
| 91 | return result; |
| 92 | } |