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