xref: /minix3/external/bsd/elftoolchain/dist/libdwarf/dwarf_init.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: dwarf_init.c,v 1.2 2014/03/09 16:58:04 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5*0a6a1f1dSLionel Sambuc  * Copyright (c) 2009 Kai Wang
6*0a6a1f1dSLionel Sambuc  * All rights reserved.
7*0a6a1f1dSLionel Sambuc  *
8*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
10*0a6a1f1dSLionel Sambuc  * are met:
11*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
12*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
13*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
14*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
15*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
16*0a6a1f1dSLionel Sambuc  *
17*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*0a6a1f1dSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*0a6a1f1dSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*0a6a1f1dSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*0a6a1f1dSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*0a6a1f1dSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*0a6a1f1dSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*0a6a1f1dSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*0a6a1f1dSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*0a6a1f1dSLionel Sambuc  * SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc  */
29*0a6a1f1dSLionel Sambuc 
30*0a6a1f1dSLionel Sambuc #include "_libdwarf.h"
31*0a6a1f1dSLionel Sambuc 
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: dwarf_init.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
33*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: dwarf_init.c 2073 2011-10-27 03:30:47Z jkoshy ");
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc int
dwarf_elf_init(Elf * elf,int mode,Dwarf_Handler errhand,Dwarf_Ptr errarg,Dwarf_Debug * ret_dbg,Dwarf_Error * error)36*0a6a1f1dSLionel Sambuc dwarf_elf_init(Elf *elf, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
37*0a6a1f1dSLionel Sambuc     Dwarf_Debug *ret_dbg, Dwarf_Error *error)
38*0a6a1f1dSLionel Sambuc {
39*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
40*0a6a1f1dSLionel Sambuc 	int ret;
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc 	if (elf == NULL || ret_dbg == NULL) {
43*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
44*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
45*0a6a1f1dSLionel Sambuc 	}
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc 	if (mode != DW_DLC_READ) {
48*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
49*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
50*0a6a1f1dSLionel Sambuc 	}
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc 	if (_dwarf_alloc(&dbg, mode, error) != DW_DLE_NONE)
53*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
54*0a6a1f1dSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc 	if (_dwarf_elf_init(dbg, elf, error) != DW_DLE_NONE) {
56*0a6a1f1dSLionel Sambuc 		free(dbg);
57*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
58*0a6a1f1dSLionel Sambuc 	}
59*0a6a1f1dSLionel Sambuc 
60*0a6a1f1dSLionel Sambuc 	if ((ret = _dwarf_init(dbg, 0, errhand, errarg, error)) !=
61*0a6a1f1dSLionel Sambuc 	    DW_DLE_NONE) {
62*0a6a1f1dSLionel Sambuc 		_dwarf_elf_deinit(dbg);
63*0a6a1f1dSLionel Sambuc 		free(dbg);
64*0a6a1f1dSLionel Sambuc 		if (ret == DW_DLE_DEBUG_INFO_NULL)
65*0a6a1f1dSLionel Sambuc 			return (DW_DLV_NO_ENTRY);
66*0a6a1f1dSLionel Sambuc 		else
67*0a6a1f1dSLionel Sambuc 			return (DW_DLV_ERROR);
68*0a6a1f1dSLionel Sambuc 	}
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc 	*ret_dbg = dbg;
71*0a6a1f1dSLionel Sambuc 
72*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
73*0a6a1f1dSLionel Sambuc }
74*0a6a1f1dSLionel Sambuc 
75*0a6a1f1dSLionel Sambuc int
dwarf_get_elf(Dwarf_Debug dbg,Elf ** elf,Dwarf_Error * error)76*0a6a1f1dSLionel Sambuc dwarf_get_elf(Dwarf_Debug dbg, Elf **elf, Dwarf_Error *error)
77*0a6a1f1dSLionel Sambuc {
78*0a6a1f1dSLionel Sambuc 	Dwarf_Elf_Object *e;
79*0a6a1f1dSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc 	if (dbg == NULL || elf == NULL) {
81*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
82*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
83*0a6a1f1dSLionel Sambuc 	}
84*0a6a1f1dSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc 	e = dbg->dbg_iface->object;
86*0a6a1f1dSLionel Sambuc 	*elf = e->eo_elf;
87*0a6a1f1dSLionel Sambuc 
88*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc 
91*0a6a1f1dSLionel Sambuc int
dwarf_init(int fd,int mode,Dwarf_Handler errhand,Dwarf_Ptr errarg,Dwarf_Debug * ret_dbg,Dwarf_Error * error)92*0a6a1f1dSLionel Sambuc dwarf_init(int fd, int mode, Dwarf_Handler errhand, Dwarf_Ptr errarg,
93*0a6a1f1dSLionel Sambuc     Dwarf_Debug *ret_dbg, Dwarf_Error *error)
94*0a6a1f1dSLionel Sambuc {
95*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
96*0a6a1f1dSLionel Sambuc 	Elf *elf;
97*0a6a1f1dSLionel Sambuc 	int ret;
98*0a6a1f1dSLionel Sambuc 
99*0a6a1f1dSLionel Sambuc 	if (fd < 0 || ret_dbg == NULL) {
100*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
101*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
102*0a6a1f1dSLionel Sambuc 	}
103*0a6a1f1dSLionel Sambuc 
104*0a6a1f1dSLionel Sambuc 	if (mode != DW_DLC_READ) {
105*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
106*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
107*0a6a1f1dSLionel Sambuc 	}
108*0a6a1f1dSLionel Sambuc 
109*0a6a1f1dSLionel Sambuc 	if (elf_version(EV_CURRENT) == EV_NONE) {
110*0a6a1f1dSLionel Sambuc 		DWARF_SET_ELF_ERROR(NULL, error);
111*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
112*0a6a1f1dSLionel Sambuc 	}
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
115*0a6a1f1dSLionel Sambuc 		DWARF_SET_ELF_ERROR(NULL, error);
116*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
117*0a6a1f1dSLionel Sambuc 	}
118*0a6a1f1dSLionel Sambuc 
119*0a6a1f1dSLionel Sambuc 	if (_dwarf_alloc(&dbg, mode, error) != DW_DLE_NONE)
120*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc 	if (_dwarf_elf_init(dbg, elf, error) != DW_DLE_NONE) {
123*0a6a1f1dSLionel Sambuc 		free(dbg);
124*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
125*0a6a1f1dSLionel Sambuc 	}
126*0a6a1f1dSLionel Sambuc 
127*0a6a1f1dSLionel Sambuc 	if ((ret = _dwarf_init(dbg, 0, errhand, errarg, error)) !=
128*0a6a1f1dSLionel Sambuc 	    DW_DLE_NONE) {
129*0a6a1f1dSLionel Sambuc 		_dwarf_elf_deinit(dbg);
130*0a6a1f1dSLionel Sambuc 		free(dbg);
131*0a6a1f1dSLionel Sambuc 		if (ret == DW_DLE_DEBUG_INFO_NULL)
132*0a6a1f1dSLionel Sambuc 			return (DW_DLV_NO_ENTRY);
133*0a6a1f1dSLionel Sambuc 		else
134*0a6a1f1dSLionel Sambuc 			return (DW_DLV_ERROR);
135*0a6a1f1dSLionel Sambuc 	}
136*0a6a1f1dSLionel Sambuc 
137*0a6a1f1dSLionel Sambuc 	*ret_dbg = dbg;
138*0a6a1f1dSLionel Sambuc 
139*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
140*0a6a1f1dSLionel Sambuc }
141*0a6a1f1dSLionel Sambuc 
142*0a6a1f1dSLionel Sambuc int
dwarf_object_init(Dwarf_Obj_Access_Interface * iface,Dwarf_Handler errhand,Dwarf_Ptr errarg,Dwarf_Debug * ret_dbg,Dwarf_Error * error)143*0a6a1f1dSLionel Sambuc dwarf_object_init(Dwarf_Obj_Access_Interface *iface, Dwarf_Handler errhand,
144*0a6a1f1dSLionel Sambuc     Dwarf_Ptr errarg, Dwarf_Debug *ret_dbg, Dwarf_Error *error)
145*0a6a1f1dSLionel Sambuc {
146*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
147*0a6a1f1dSLionel Sambuc 
148*0a6a1f1dSLionel Sambuc 	if (iface == NULL || ret_dbg == NULL) {
149*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
150*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
151*0a6a1f1dSLionel Sambuc 	}
152*0a6a1f1dSLionel Sambuc 
153*0a6a1f1dSLionel Sambuc 	if (_dwarf_alloc(&dbg, DW_DLC_READ, error) != DW_DLE_NONE)
154*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
155*0a6a1f1dSLionel Sambuc 
156*0a6a1f1dSLionel Sambuc 	dbg->dbg_iface = iface;
157*0a6a1f1dSLionel Sambuc 
158*0a6a1f1dSLionel Sambuc 	if (_dwarf_init(dbg, 0, errhand, errarg, error) != DW_DLE_NONE) {
159*0a6a1f1dSLionel Sambuc 		free(dbg);
160*0a6a1f1dSLionel Sambuc 		return (DW_DLV_ERROR);
161*0a6a1f1dSLionel Sambuc 	}
162*0a6a1f1dSLionel Sambuc 
163*0a6a1f1dSLionel Sambuc 	*ret_dbg = dbg;
164*0a6a1f1dSLionel Sambuc 
165*0a6a1f1dSLionel Sambuc 	return (DW_DLV_OK);
166*0a6a1f1dSLionel Sambuc }
167