1*0a6a1f1dSLionel Sambuc /* $NetBSD: dwarf_pro_expr.c,v 1.2 2014/03/09 16:58:04 christos Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2010 Kai Wang
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc * are met:
10*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc *
16*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc */
28*0a6a1f1dSLionel Sambuc
29*0a6a1f1dSLionel Sambuc #include "_libdwarf.h"
30*0a6a1f1dSLionel Sambuc
31*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: dwarf_pro_expr.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: dwarf_pro_expr.c 2074 2011-10-27 03:34:33Z jkoshy ");
33*0a6a1f1dSLionel Sambuc
34*0a6a1f1dSLionel Sambuc static struct _Dwarf_P_Expr_Entry *
_dwarf_add_expr(Dwarf_P_Expr expr,Dwarf_Small opcode,Dwarf_Unsigned val1,Dwarf_Unsigned val2,Dwarf_Error * error)35*0a6a1f1dSLionel Sambuc _dwarf_add_expr(Dwarf_P_Expr expr, Dwarf_Small opcode, Dwarf_Unsigned val1,
36*0a6a1f1dSLionel Sambuc Dwarf_Unsigned val2, Dwarf_Error *error)
37*0a6a1f1dSLionel Sambuc {
38*0a6a1f1dSLionel Sambuc struct _Dwarf_P_Expr_Entry *ee;
39*0a6a1f1dSLionel Sambuc Dwarf_Debug dbg;
40*0a6a1f1dSLionel Sambuc int len;
41*0a6a1f1dSLionel Sambuc
42*0a6a1f1dSLionel Sambuc dbg = expr != NULL ? expr->pe_dbg : NULL;
43*0a6a1f1dSLionel Sambuc
44*0a6a1f1dSLionel Sambuc if (_dwarf_loc_expr_add_atom(expr->pe_dbg, NULL, NULL, opcode, val1,
45*0a6a1f1dSLionel Sambuc val2, &len, error) != DW_DLE_NONE)
46*0a6a1f1dSLionel Sambuc return (NULL);
47*0a6a1f1dSLionel Sambuc assert(len > 0);
48*0a6a1f1dSLionel Sambuc
49*0a6a1f1dSLionel Sambuc if ((ee = calloc(1, sizeof(*ee))) == NULL) {
50*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
51*0a6a1f1dSLionel Sambuc return (NULL);
52*0a6a1f1dSLionel Sambuc }
53*0a6a1f1dSLionel Sambuc
54*0a6a1f1dSLionel Sambuc STAILQ_INSERT_TAIL(&expr->pe_eelist, ee, ee_next);
55*0a6a1f1dSLionel Sambuc
56*0a6a1f1dSLionel Sambuc ee->ee_loc.lr_atom = opcode;
57*0a6a1f1dSLionel Sambuc ee->ee_loc.lr_number = val1;
58*0a6a1f1dSLionel Sambuc ee->ee_loc.lr_number2 = val2;
59*0a6a1f1dSLionel Sambuc ee->ee_loc.lr_offset = expr->pe_length;
60*0a6a1f1dSLionel Sambuc expr->pe_length += len;
61*0a6a1f1dSLionel Sambuc expr->pe_invalid = 1;
62*0a6a1f1dSLionel Sambuc
63*0a6a1f1dSLionel Sambuc return (ee);
64*0a6a1f1dSLionel Sambuc }
65*0a6a1f1dSLionel Sambuc
66*0a6a1f1dSLionel Sambuc int
_dwarf_expr_into_block(Dwarf_P_Expr expr,Dwarf_Error * error)67*0a6a1f1dSLionel Sambuc _dwarf_expr_into_block(Dwarf_P_Expr expr, Dwarf_Error *error)
68*0a6a1f1dSLionel Sambuc {
69*0a6a1f1dSLionel Sambuc struct _Dwarf_P_Expr_Entry *ee;
70*0a6a1f1dSLionel Sambuc Dwarf_Debug dbg;
71*0a6a1f1dSLionel Sambuc int len, pos, ret;
72*0a6a1f1dSLionel Sambuc
73*0a6a1f1dSLionel Sambuc dbg = expr != NULL ? expr->pe_dbg : NULL;
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc if (expr->pe_block != NULL) {
76*0a6a1f1dSLionel Sambuc free(expr->pe_block);
77*0a6a1f1dSLionel Sambuc expr->pe_block = NULL;
78*0a6a1f1dSLionel Sambuc }
79*0a6a1f1dSLionel Sambuc
80*0a6a1f1dSLionel Sambuc if (expr->pe_length <= 0) {
81*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_EXPR_LENGTH_BAD);
82*0a6a1f1dSLionel Sambuc return (DW_DLE_EXPR_LENGTH_BAD);
83*0a6a1f1dSLionel Sambuc }
84*0a6a1f1dSLionel Sambuc
85*0a6a1f1dSLionel Sambuc
86*0a6a1f1dSLionel Sambuc if ((expr->pe_block = calloc((size_t) expr->pe_length, 1)) == NULL) {
87*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
88*0a6a1f1dSLionel Sambuc return (DW_DLE_MEMORY);
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc pos = 0;
92*0a6a1f1dSLionel Sambuc STAILQ_FOREACH(ee, &expr->pe_eelist, ee_next) {
93*0a6a1f1dSLionel Sambuc assert((Dwarf_Unsigned) pos < expr->pe_length);
94*0a6a1f1dSLionel Sambuc ret = _dwarf_loc_expr_add_atom(expr->pe_dbg,
95*0a6a1f1dSLionel Sambuc &expr->pe_block[pos], &expr->pe_block[expr->pe_length],
96*0a6a1f1dSLionel Sambuc ee->ee_loc.lr_atom, ee->ee_loc.lr_number,
97*0a6a1f1dSLionel Sambuc ee->ee_loc.lr_number2, &len, error);
98*0a6a1f1dSLionel Sambuc assert(ret == DW_DLE_NONE);
99*0a6a1f1dSLionel Sambuc assert(len > 0);
100*0a6a1f1dSLionel Sambuc pos += len;
101*0a6a1f1dSLionel Sambuc }
102*0a6a1f1dSLionel Sambuc
103*0a6a1f1dSLionel Sambuc expr->pe_invalid = 0;
104*0a6a1f1dSLionel Sambuc
105*0a6a1f1dSLionel Sambuc return (DW_DLE_NONE);
106*0a6a1f1dSLionel Sambuc }
107*0a6a1f1dSLionel Sambuc
108*0a6a1f1dSLionel Sambuc void
_dwarf_expr_cleanup(Dwarf_P_Debug dbg)109*0a6a1f1dSLionel Sambuc _dwarf_expr_cleanup(Dwarf_P_Debug dbg)
110*0a6a1f1dSLionel Sambuc {
111*0a6a1f1dSLionel Sambuc Dwarf_P_Expr pe, tpe;
112*0a6a1f1dSLionel Sambuc struct _Dwarf_P_Expr_Entry *ee, *tee;
113*0a6a1f1dSLionel Sambuc
114*0a6a1f1dSLionel Sambuc assert(dbg != NULL && dbg->dbg_mode == DW_DLC_WRITE);
115*0a6a1f1dSLionel Sambuc
116*0a6a1f1dSLionel Sambuc STAILQ_FOREACH_SAFE(pe, &dbg->dbgp_pelist, pe_next, tpe) {
117*0a6a1f1dSLionel Sambuc STAILQ_REMOVE(&dbg->dbgp_pelist, pe, _Dwarf_P_Expr, pe_next);
118*0a6a1f1dSLionel Sambuc STAILQ_FOREACH_SAFE(ee, &pe->pe_eelist, ee_next, tee) {
119*0a6a1f1dSLionel Sambuc STAILQ_REMOVE(&pe->pe_eelist, ee, _Dwarf_P_Expr_Entry,
120*0a6a1f1dSLionel Sambuc ee_next);
121*0a6a1f1dSLionel Sambuc free(ee);
122*0a6a1f1dSLionel Sambuc }
123*0a6a1f1dSLionel Sambuc if (pe->pe_block)
124*0a6a1f1dSLionel Sambuc free(pe->pe_block);
125*0a6a1f1dSLionel Sambuc free(pe);
126*0a6a1f1dSLionel Sambuc }
127*0a6a1f1dSLionel Sambuc }
128*0a6a1f1dSLionel Sambuc
129*0a6a1f1dSLionel Sambuc Dwarf_P_Expr
dwarf_new_expr(Dwarf_P_Debug dbg,Dwarf_Error * error)130*0a6a1f1dSLionel Sambuc dwarf_new_expr(Dwarf_P_Debug dbg, Dwarf_Error *error)
131*0a6a1f1dSLionel Sambuc {
132*0a6a1f1dSLionel Sambuc Dwarf_P_Expr pe;
133*0a6a1f1dSLionel Sambuc
134*0a6a1f1dSLionel Sambuc if (dbg == NULL) {
135*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
136*0a6a1f1dSLionel Sambuc return (DW_DLV_BADADDR);
137*0a6a1f1dSLionel Sambuc }
138*0a6a1f1dSLionel Sambuc
139*0a6a1f1dSLionel Sambuc if ((pe = calloc(1, sizeof(struct _Dwarf_P_Expr))) == NULL) {
140*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
141*0a6a1f1dSLionel Sambuc return (DW_DLV_BADADDR);
142*0a6a1f1dSLionel Sambuc }
143*0a6a1f1dSLionel Sambuc STAILQ_INIT(&pe->pe_eelist);
144*0a6a1f1dSLionel Sambuc
145*0a6a1f1dSLionel Sambuc STAILQ_INSERT_TAIL(&dbg->dbgp_pelist, pe, pe_next);
146*0a6a1f1dSLionel Sambuc pe->pe_dbg = dbg;
147*0a6a1f1dSLionel Sambuc
148*0a6a1f1dSLionel Sambuc return (pe);
149*0a6a1f1dSLionel Sambuc }
150*0a6a1f1dSLionel Sambuc
151*0a6a1f1dSLionel Sambuc Dwarf_Unsigned
dwarf_add_expr_gen(Dwarf_P_Expr expr,Dwarf_Small opcode,Dwarf_Unsigned val1,Dwarf_Unsigned val2,Dwarf_Error * error)152*0a6a1f1dSLionel Sambuc dwarf_add_expr_gen(Dwarf_P_Expr expr, Dwarf_Small opcode, Dwarf_Unsigned val1,
153*0a6a1f1dSLionel Sambuc Dwarf_Unsigned val2, Dwarf_Error *error)
154*0a6a1f1dSLionel Sambuc {
155*0a6a1f1dSLionel Sambuc
156*0a6a1f1dSLionel Sambuc if (expr == NULL) {
157*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
158*0a6a1f1dSLionel Sambuc return (DW_DLV_NOCOUNT);
159*0a6a1f1dSLionel Sambuc }
160*0a6a1f1dSLionel Sambuc
161*0a6a1f1dSLionel Sambuc if (_dwarf_add_expr(expr, opcode, val1, val2, error) == NULL)
162*0a6a1f1dSLionel Sambuc return (DW_DLV_NOCOUNT);
163*0a6a1f1dSLionel Sambuc
164*0a6a1f1dSLionel Sambuc return (expr->pe_length);
165*0a6a1f1dSLionel Sambuc }
166*0a6a1f1dSLionel Sambuc
167*0a6a1f1dSLionel Sambuc Dwarf_Unsigned
dwarf_add_expr_addr(Dwarf_P_Expr expr,Dwarf_Unsigned address,Dwarf_Signed sym_index,Dwarf_Error * error)168*0a6a1f1dSLionel Sambuc dwarf_add_expr_addr(Dwarf_P_Expr expr, Dwarf_Unsigned address,
169*0a6a1f1dSLionel Sambuc Dwarf_Signed sym_index, Dwarf_Error *error)
170*0a6a1f1dSLionel Sambuc {
171*0a6a1f1dSLionel Sambuc
172*0a6a1f1dSLionel Sambuc return (dwarf_add_expr_addr_b(expr, address, sym_index, error));
173*0a6a1f1dSLionel Sambuc }
174*0a6a1f1dSLionel Sambuc
175*0a6a1f1dSLionel Sambuc Dwarf_Unsigned
dwarf_add_expr_addr_b(Dwarf_P_Expr expr,Dwarf_Unsigned address,Dwarf_Unsigned sym_index,Dwarf_Error * error)176*0a6a1f1dSLionel Sambuc dwarf_add_expr_addr_b(Dwarf_P_Expr expr, Dwarf_Unsigned address,
177*0a6a1f1dSLionel Sambuc Dwarf_Unsigned sym_index, Dwarf_Error *error)
178*0a6a1f1dSLionel Sambuc {
179*0a6a1f1dSLionel Sambuc struct _Dwarf_P_Expr_Entry *ee;
180*0a6a1f1dSLionel Sambuc
181*0a6a1f1dSLionel Sambuc if (expr == NULL) {
182*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
183*0a6a1f1dSLionel Sambuc return (DW_DLV_NOCOUNT);
184*0a6a1f1dSLionel Sambuc }
185*0a6a1f1dSLionel Sambuc
186*0a6a1f1dSLionel Sambuc if ((ee = _dwarf_add_expr(expr, DW_OP_addr, address, 0, error)) == NULL)
187*0a6a1f1dSLionel Sambuc return (DW_DLV_NOCOUNT);
188*0a6a1f1dSLionel Sambuc
189*0a6a1f1dSLionel Sambuc ee->ee_sym = sym_index;
190*0a6a1f1dSLionel Sambuc
191*0a6a1f1dSLionel Sambuc return (expr->pe_length);
192*0a6a1f1dSLionel Sambuc }
193*0a6a1f1dSLionel Sambuc
194*0a6a1f1dSLionel Sambuc Dwarf_Unsigned
dwarf_expr_current_offset(Dwarf_P_Expr expr,Dwarf_Error * error)195*0a6a1f1dSLionel Sambuc dwarf_expr_current_offset(Dwarf_P_Expr expr, Dwarf_Error *error)
196*0a6a1f1dSLionel Sambuc {
197*0a6a1f1dSLionel Sambuc
198*0a6a1f1dSLionel Sambuc if (expr == NULL) {
199*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
200*0a6a1f1dSLionel Sambuc return (DW_DLV_NOCOUNT);
201*0a6a1f1dSLionel Sambuc }
202*0a6a1f1dSLionel Sambuc
203*0a6a1f1dSLionel Sambuc return (expr->pe_length);
204*0a6a1f1dSLionel Sambuc }
205*0a6a1f1dSLionel Sambuc
206*0a6a1f1dSLionel Sambuc Dwarf_Addr
dwarf_expr_into_block(Dwarf_P_Expr expr,Dwarf_Unsigned * length,Dwarf_Error * error)207*0a6a1f1dSLionel Sambuc dwarf_expr_into_block(Dwarf_P_Expr expr, Dwarf_Unsigned *length,
208*0a6a1f1dSLionel Sambuc Dwarf_Error *error)
209*0a6a1f1dSLionel Sambuc {
210*0a6a1f1dSLionel Sambuc Dwarf_Debug dbg;
211*0a6a1f1dSLionel Sambuc
212*0a6a1f1dSLionel Sambuc dbg = expr != NULL ? expr->pe_dbg : NULL;
213*0a6a1f1dSLionel Sambuc
214*0a6a1f1dSLionel Sambuc if (expr == NULL || length == NULL) {
215*0a6a1f1dSLionel Sambuc DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
216*0a6a1f1dSLionel Sambuc return ((Dwarf_Addr) (uintptr_t) DW_DLV_BADADDR);
217*0a6a1f1dSLionel Sambuc }
218*0a6a1f1dSLionel Sambuc
219*0a6a1f1dSLionel Sambuc if (expr->pe_block == NULL || expr->pe_invalid)
220*0a6a1f1dSLionel Sambuc if (_dwarf_expr_into_block(expr, error) != DW_DLE_NONE)
221*0a6a1f1dSLionel Sambuc return ((Dwarf_Addr) (uintptr_t) DW_DLV_BADADDR);
222*0a6a1f1dSLionel Sambuc
223*0a6a1f1dSLionel Sambuc *length = expr->pe_length;
224*0a6a1f1dSLionel Sambuc
225*0a6a1f1dSLionel Sambuc return ((Dwarf_Addr) (uintptr_t) expr->pe_block);
226*0a6a1f1dSLionel Sambuc }
227