xref: /freebsd/contrib/elftoolchain/libdwarf/dwarf_pro_init.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * Copyright (c) 2009 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "_libdwarf.h"
28 
29 ELFTC_VCSID("$Id: dwarf_pro_init.c 2074 2011-10-27 03:34:33Z jkoshy $");
30 
31 Dwarf_P_Debug
32 dwarf_producer_init(Dwarf_Unsigned flags, Dwarf_Callback_Func func,
33     Dwarf_Handler errhand, Dwarf_Ptr errarg, Dwarf_Error *error)
34 {
35 	Dwarf_P_Debug dbg;
36 	int mode;
37 
38 	if (flags & DW_DLC_READ || flags & DW_DLC_RDWR) {
39 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
40 		return (DW_DLV_BADADDR);
41 	}
42 
43 	if (flags & DW_DLC_WRITE)
44 		mode = DW_DLC_WRITE;
45 	else {
46 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
47 		return (DW_DLV_BADADDR);
48 	}
49 
50 	if (func == NULL) {
51 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
52 		return (DW_DLV_BADADDR);
53 	}
54 
55 	if (_dwarf_alloc(&dbg, DW_DLC_WRITE, error) != DW_DLE_NONE)
56 		return (DW_DLV_BADADDR);
57 
58 	dbg->dbg_mode = mode;
59 
60 	if (_dwarf_init(dbg, flags, errhand, errarg, error) != DW_DLE_NONE) {
61 		free(dbg);
62 		return (DW_DLV_BADADDR);
63 	}
64 
65 	dbg->dbgp_func = func;
66 
67 	return (dbg);
68 }
69 
70 Dwarf_P_Debug
71 dwarf_producer_init_b(Dwarf_Unsigned flags, Dwarf_Callback_Func_b func,
72     Dwarf_Handler errhand, Dwarf_Ptr errarg, Dwarf_Error *error)
73 {
74 	Dwarf_P_Debug dbg;
75 	int mode;
76 
77 	if (flags & DW_DLC_READ || flags & DW_DLC_RDWR) {
78 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
79 		return (DW_DLV_BADADDR);
80 	}
81 
82 	if (flags & DW_DLC_WRITE)
83 		mode = DW_DLC_WRITE;
84 	else {
85 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
86 		return (DW_DLV_BADADDR);
87 	}
88 
89 	if (func == NULL) {
90 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
91 		return (DW_DLV_BADADDR);
92 	}
93 
94 	if (_dwarf_alloc(&dbg, DW_DLC_WRITE, error) != DW_DLE_NONE)
95 		return (DW_DLV_BADADDR);
96 
97 	dbg->dbg_mode = mode;
98 
99 	if (_dwarf_init(dbg, flags, errhand, errarg, error) != DW_DLE_NONE) {
100 		free(dbg);
101 		return (DW_DLV_BADADDR);
102 	}
103 
104 	dbg->dbgp_func_b = func;
105 
106 	return (dbg);
107 }
108 
109 int
110 dwarf_producer_set_isa(Dwarf_P_Debug dbg, enum Dwarf_ISA isa,
111     Dwarf_Error *error)
112 {
113 
114 	if (dbg == NULL || isa >= DW_ISA_MAX) {
115 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
116 		return (DW_DLV_ERROR);
117 	}
118 
119 	dbg->dbgp_isa = isa;
120 
121 	return (DW_DLV_OK);
122 }
123