2 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3 * Copyright (c) 2009 Kai Wang
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include "_libdwarf.h"
30 ELFTC_VCSID("$Id: dwarf_init.c 2073 2011-10-27 03:30:47Z jkoshy $");
33 dwarf_elf_init(Elf *elf, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
34 Dwarf_Debug *ret_dbg, Dwarf_Error *error)
39 if (elf == NULL || ret_dbg == NULL) {
40 DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
41 return (DW_DLV_ERROR);
44 if (mode != DW_DLC_READ) {
45 DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
46 return (DW_DLV_ERROR);
49 if (_dwarf_alloc(&dbg, mode, error) != DW_DLE_NONE)
50 return (DW_DLV_ERROR);
52 if (_dwarf_elf_init(dbg, elf, error) != DW_DLE_NONE) {
54 return (DW_DLV_ERROR);
57 if ((ret = _dwarf_init(dbg, 0, errhand, errarg, error)) !=
59 _dwarf_elf_deinit(dbg);
61 if (ret == DW_DLE_DEBUG_INFO_NULL)
62 return (DW_DLV_NO_ENTRY);
64 return (DW_DLV_ERROR);
73 dwarf_get_elf(Dwarf_Debug dbg, Elf **elf, Dwarf_Error *error)
77 if (dbg == NULL || elf == NULL) {
78 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
79 return (DW_DLV_ERROR);
82 e = dbg->dbg_iface->object;
89 dwarf_init(int fd, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
90 Dwarf_Debug *ret_dbg, Dwarf_Error *error)
96 if (fd < 0 || ret_dbg == NULL) {
97 DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
98 return (DW_DLV_ERROR);
101 if (mode != DW_DLC_READ) {
102 DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
103 return (DW_DLV_ERROR);
106 if (elf_version(EV_CURRENT) == EV_NONE) {
107 DWARF_SET_ELF_ERROR(NULL, error);
108 return (DW_DLV_ERROR);
111 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
112 DWARF_SET_ELF_ERROR(NULL, error);
113 return (DW_DLV_ERROR);
116 if (_dwarf_alloc(&dbg, mode, error) != DW_DLE_NONE)
117 return (DW_DLV_ERROR);
119 if (_dwarf_elf_init(dbg, elf, error) != DW_DLE_NONE) {
121 return (DW_DLV_ERROR);
124 if ((ret = _dwarf_init(dbg, 0, errhand, errarg, error)) !=
126 _dwarf_elf_deinit(dbg);
128 if (ret == DW_DLE_DEBUG_INFO_NULL)
129 return (DW_DLV_NO_ENTRY);
131 return (DW_DLV_ERROR);
140 dwarf_object_init(Dwarf_Obj_Access_Interface *iface, Dwarf_Handler errhand,
141 Dwarf_Ptr errarg, Dwarf_Debug *ret_dbg, Dwarf_Error *error)
145 if (iface == NULL || ret_dbg == NULL) {
146 DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
147 return (DW_DLV_ERROR);
150 if (_dwarf_alloc(&dbg, DW_DLC_READ, error) != DW_DLE_NONE)
151 return (DW_DLV_ERROR);
153 dbg->dbg_iface = iface;
155 if (_dwarf_init(dbg, 0, errhand, errarg, error) != DW_DLE_NONE) {
157 return (DW_DLV_ERROR);