xref: /freebsd/contrib/elftoolchain/libdwarf/dwarf_init.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * Copyright (c) 2007 John Birrell (jb@freebsd.org)
3  * Copyright (c) 2009 Kai Wang
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include "_libdwarf.h"
29 
30 ELFTC_VCSID("$Id: dwarf_init.c 2073 2011-10-27 03:30:47Z jkoshy $");
31 
32 int
33 dwarf_elf_init(Elf *elf, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
34     Dwarf_Debug *ret_dbg, Dwarf_Error *error)
35 {
36 	Dwarf_Debug dbg;
37 	int ret;
38 
39 	if (elf == NULL || ret_dbg == NULL) {
40 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
41 		return (DW_DLV_ERROR);
42 	}
43 
44 	if (mode != DW_DLC_READ) {
45 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
46 		return (DW_DLV_ERROR);
47 	}
48 
49 	if (_dwarf_alloc(&dbg, mode, error) != DW_DLE_NONE)
50 		return (DW_DLV_ERROR);
51 
52 	if (_dwarf_elf_init(dbg, elf, error) != DW_DLE_NONE) {
53 		free(dbg);
54 		return (DW_DLV_ERROR);
55 	}
56 
57 	if ((ret = _dwarf_init(dbg, 0, errhand, errarg, error)) !=
58 	    DW_DLE_NONE) {
59 		_dwarf_elf_deinit(dbg);
60 		free(dbg);
61 		if (ret == DW_DLE_DEBUG_INFO_NULL)
62 			return (DW_DLV_NO_ENTRY);
63 		else
64 			return (DW_DLV_ERROR);
65 	}
66 
67 	*ret_dbg = dbg;
68 
69 	return (DW_DLV_OK);
70 }
71 
72 int
73 dwarf_get_elf(Dwarf_Debug dbg, Elf **elf, Dwarf_Error *error)
74 {
75 	Dwarf_Elf_Object *e;
76 
77 	if (dbg == NULL || elf == NULL) {
78 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
79 		return (DW_DLV_ERROR);
80 	}
81 
82 	e = dbg->dbg_iface->object;
83 	*elf = e->eo_elf;
84 
85 	return (DW_DLV_OK);
86 }
87 
88 int
89 dwarf_init(int fd, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
90     Dwarf_Debug *ret_dbg, Dwarf_Error *error)
91 {
92 	Dwarf_Debug dbg;
93 	Elf *elf;
94 	int ret;
95 
96 	if (fd < 0 || ret_dbg == NULL) {
97 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
98 		return (DW_DLV_ERROR);
99 	}
100 
101 	if (mode != DW_DLC_READ) {
102 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
103 		return (DW_DLV_ERROR);
104 	}
105 
106 	if (elf_version(EV_CURRENT) == EV_NONE) {
107 		DWARF_SET_ELF_ERROR(NULL, error);
108 		return (DW_DLV_ERROR);
109 	}
110 
111 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
112 		DWARF_SET_ELF_ERROR(NULL, error);
113 		return (DW_DLV_ERROR);
114 	}
115 
116 	if (_dwarf_alloc(&dbg, mode, error) != DW_DLE_NONE)
117 		return (DW_DLV_ERROR);
118 
119 	if (_dwarf_elf_init(dbg, elf, error) != DW_DLE_NONE) {
120 		free(dbg);
121 		return (DW_DLV_ERROR);
122 	}
123 
124 	if ((ret = _dwarf_init(dbg, 0, errhand, errarg, error)) !=
125 	    DW_DLE_NONE) {
126 		_dwarf_elf_deinit(dbg);
127 		free(dbg);
128 		if (ret == DW_DLE_DEBUG_INFO_NULL)
129 			return (DW_DLV_NO_ENTRY);
130 		else
131 			return (DW_DLV_ERROR);
132 	}
133 
134 	*ret_dbg = dbg;
135 
136 	return (DW_DLV_OK);
137 }
138 
139 int
140 dwarf_object_init(Dwarf_Obj_Access_Interface *iface, Dwarf_Handler errhand,
141     Dwarf_Ptr errarg, Dwarf_Debug *ret_dbg, Dwarf_Error *error)
142 {
143 	Dwarf_Debug dbg;
144 
145 	if (iface == NULL || ret_dbg == NULL) {
146 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
147 		return (DW_DLV_ERROR);
148 	}
149 
150 	if (_dwarf_alloc(&dbg, DW_DLC_READ, error) != DW_DLE_NONE)
151 		return (DW_DLV_ERROR);
152 
153 	dbg->dbg_iface = iface;
154 
155 	if (_dwarf_init(dbg, 0, errhand, errarg, error) != DW_DLE_NONE) {
156 		free(dbg);
157 		return (DW_DLV_ERROR);
158 	}
159 
160 	*ret_dbg = dbg;
161 
162 	return (DW_DLV_OK);
163 }
164