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