xref: /minix3/external/bsd/elftoolchain/dist/libdwarf/dwarf_pro_frame.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: dwarf_pro_frame.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_frame.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
32*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: dwarf_pro_frame.c 2074 2011-10-27 03:34:33Z jkoshy ");
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc Dwarf_P_Fde
dwarf_new_fde(Dwarf_P_Debug dbg,Dwarf_Error * error)35*0a6a1f1dSLionel Sambuc dwarf_new_fde(Dwarf_P_Debug dbg, Dwarf_Error *error)
36*0a6a1f1dSLionel Sambuc {
37*0a6a1f1dSLionel Sambuc 	Dwarf_P_Fde fde;
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc 	if (dbg == NULL) {
40*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
41*0a6a1f1dSLionel Sambuc 		return (DW_DLV_BADADDR);
42*0a6a1f1dSLionel Sambuc 	}
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc 	if ((fde = calloc(1, sizeof(struct _Dwarf_Fde))) == NULL) {
45*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
46*0a6a1f1dSLionel Sambuc 		return (DW_DLV_BADADDR);
47*0a6a1f1dSLionel Sambuc 	}
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc 	fde->fde_dbg = dbg;
50*0a6a1f1dSLionel Sambuc 
51*0a6a1f1dSLionel Sambuc 	return (fde);
52*0a6a1f1dSLionel Sambuc }
53*0a6a1f1dSLionel Sambuc 
54*0a6a1f1dSLionel Sambuc Dwarf_Unsigned
dwarf_add_frame_cie(Dwarf_P_Debug dbg,char * augmenter,Dwarf_Small caf,Dwarf_Small daf,Dwarf_Small ra,Dwarf_Ptr initinst,Dwarf_Unsigned inst_len,Dwarf_Error * error)55*0a6a1f1dSLionel Sambuc dwarf_add_frame_cie(Dwarf_P_Debug dbg, char *augmenter, Dwarf_Small caf,
56*0a6a1f1dSLionel Sambuc     Dwarf_Small daf, Dwarf_Small ra, Dwarf_Ptr initinst,
57*0a6a1f1dSLionel Sambuc     Dwarf_Unsigned inst_len, Dwarf_Error *error)
58*0a6a1f1dSLionel Sambuc {
59*0a6a1f1dSLionel Sambuc 	Dwarf_P_Cie cie;
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc 	if (dbg == NULL) {
62*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
63*0a6a1f1dSLionel Sambuc 		return (DW_DLV_NOCOUNT);
64*0a6a1f1dSLionel Sambuc 	}
65*0a6a1f1dSLionel Sambuc 
66*0a6a1f1dSLionel Sambuc 	if ((cie = calloc(1, sizeof(struct _Dwarf_Cie))) == NULL) {
67*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error,DW_DLE_MEMORY);
68*0a6a1f1dSLionel Sambuc 		return (DW_DLV_NOCOUNT);
69*0a6a1f1dSLionel Sambuc 	}
70*0a6a1f1dSLionel Sambuc 	STAILQ_INSERT_TAIL(&dbg->dbgp_cielist, cie, cie_next);
71*0a6a1f1dSLionel Sambuc 
72*0a6a1f1dSLionel Sambuc 	cie->cie_index = dbg->dbgp_cielen++;
73*0a6a1f1dSLionel Sambuc 
74*0a6a1f1dSLionel Sambuc 	if (augmenter != NULL) {
75*0a6a1f1dSLionel Sambuc 		cie->cie_augment = (uint8_t *) strdup(augmenter);
76*0a6a1f1dSLionel Sambuc 		if (cie->cie_augment == NULL) {
77*0a6a1f1dSLionel Sambuc 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
78*0a6a1f1dSLionel Sambuc 			return (DW_DLV_NOCOUNT);
79*0a6a1f1dSLionel Sambuc 		}
80*0a6a1f1dSLionel Sambuc 	}
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc 	cie->cie_caf = caf;
83*0a6a1f1dSLionel Sambuc 	cie->cie_daf = (int8_t) daf; /* daf is signed. */
84*0a6a1f1dSLionel Sambuc 	cie->cie_ra = ra;
85*0a6a1f1dSLionel Sambuc 	if (initinst != NULL && inst_len > 0) {
86*0a6a1f1dSLionel Sambuc 		cie->cie_initinst = malloc((size_t) inst_len);
87*0a6a1f1dSLionel Sambuc 		if (cie->cie_initinst == NULL) {
88*0a6a1f1dSLionel Sambuc 			DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
89*0a6a1f1dSLionel Sambuc 			return (DW_DLV_NOCOUNT);
90*0a6a1f1dSLionel Sambuc 		}
91*0a6a1f1dSLionel Sambuc 		memcpy(cie->cie_initinst, initinst, inst_len);
92*0a6a1f1dSLionel Sambuc 		cie->cie_instlen = inst_len;
93*0a6a1f1dSLionel Sambuc 	}
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc 	return (cie->cie_index);
96*0a6a1f1dSLionel Sambuc }
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc Dwarf_Unsigned
dwarf_add_frame_fde(Dwarf_P_Debug dbg,Dwarf_P_Fde fde,Dwarf_P_Die die,Dwarf_Unsigned cie,Dwarf_Addr virt_addr,Dwarf_Unsigned code_len,Dwarf_Unsigned symbol_index,Dwarf_Error * error)99*0a6a1f1dSLionel Sambuc dwarf_add_frame_fde(Dwarf_P_Debug dbg, Dwarf_P_Fde fde, Dwarf_P_Die die,
100*0a6a1f1dSLionel Sambuc     Dwarf_Unsigned cie, Dwarf_Addr virt_addr, Dwarf_Unsigned code_len,
101*0a6a1f1dSLionel Sambuc     Dwarf_Unsigned symbol_index, Dwarf_Error *error)
102*0a6a1f1dSLionel Sambuc {
103*0a6a1f1dSLionel Sambuc 
104*0a6a1f1dSLionel Sambuc 	return (dwarf_add_frame_fde_b(dbg, fde, die, cie, virt_addr, code_len,
105*0a6a1f1dSLionel Sambuc 	    symbol_index, 0, 0, error));
106*0a6a1f1dSLionel Sambuc }
107*0a6a1f1dSLionel Sambuc 
108*0a6a1f1dSLionel Sambuc Dwarf_Unsigned
dwarf_add_frame_fde_b(Dwarf_P_Debug dbg,Dwarf_P_Fde fde,Dwarf_P_Die die,Dwarf_Unsigned cie,Dwarf_Addr virt_addr,Dwarf_Unsigned code_len,Dwarf_Unsigned symbol_index,Dwarf_Unsigned end_symbol_index,Dwarf_Addr offset_from_end_sym,Dwarf_Error * error)109*0a6a1f1dSLionel Sambuc dwarf_add_frame_fde_b(Dwarf_P_Debug dbg, Dwarf_P_Fde fde, Dwarf_P_Die die,
110*0a6a1f1dSLionel Sambuc     Dwarf_Unsigned cie, Dwarf_Addr virt_addr, Dwarf_Unsigned code_len,
111*0a6a1f1dSLionel Sambuc     Dwarf_Unsigned symbol_index, Dwarf_Unsigned end_symbol_index,
112*0a6a1f1dSLionel Sambuc     Dwarf_Addr offset_from_end_sym, Dwarf_Error *error)
113*0a6a1f1dSLionel Sambuc {
114*0a6a1f1dSLionel Sambuc 	Dwarf_P_Cie ciep;
115*0a6a1f1dSLionel Sambuc 	int i;
116*0a6a1f1dSLionel Sambuc 
117*0a6a1f1dSLionel Sambuc 	/*
118*0a6a1f1dSLionel Sambuc 	 * XXX SGI libdwarf need the DIE arg because later it will insert a
119*0a6a1f1dSLionel Sambuc 	 * DW_AT_MIPS_fde attribute, which points to the offset the
120*0a6a1f1dSLionel Sambuc 	 * correspoding FDE, into this DIE. Do we need this?
121*0a6a1f1dSLionel Sambuc 	 */
122*0a6a1f1dSLionel Sambuc 	(void) die;
123*0a6a1f1dSLionel Sambuc 
124*0a6a1f1dSLionel Sambuc 	if (dbg == NULL || fde == NULL || fde->fde_dbg != dbg) {
125*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
126*0a6a1f1dSLionel Sambuc 		return (DW_DLV_NOCOUNT);
127*0a6a1f1dSLionel Sambuc 	}
128*0a6a1f1dSLionel Sambuc 
129*0a6a1f1dSLionel Sambuc 	ciep = STAILQ_FIRST(&dbg->dbgp_cielist);
130*0a6a1f1dSLionel Sambuc 	for (i = 0; (Dwarf_Unsigned) i < cie; i++) {
131*0a6a1f1dSLionel Sambuc 		ciep = STAILQ_NEXT(ciep, cie_next);
132*0a6a1f1dSLionel Sambuc 		if (ciep == NULL)
133*0a6a1f1dSLionel Sambuc 			break;
134*0a6a1f1dSLionel Sambuc 	}
135*0a6a1f1dSLionel Sambuc 	if (ciep == NULL) {
136*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
137*0a6a1f1dSLionel Sambuc 		return (DW_DLV_NOCOUNT);
138*0a6a1f1dSLionel Sambuc 	}
139*0a6a1f1dSLionel Sambuc 
140*0a6a1f1dSLionel Sambuc 	if (end_symbol_index > 0 &&
141*0a6a1f1dSLionel Sambuc 	    (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
142*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
143*0a6a1f1dSLionel Sambuc 		return (DW_DLV_NOCOUNT);
144*0a6a1f1dSLionel Sambuc 	}
145*0a6a1f1dSLionel Sambuc 
146*0a6a1f1dSLionel Sambuc 	fde->fde_cie = ciep;
147*0a6a1f1dSLionel Sambuc 	fde->fde_initloc = virt_addr;
148*0a6a1f1dSLionel Sambuc 	fde->fde_adrange = code_len;
149*0a6a1f1dSLionel Sambuc 	fde->fde_symndx = symbol_index;
150*0a6a1f1dSLionel Sambuc 	fde->fde_esymndx = end_symbol_index;
151*0a6a1f1dSLionel Sambuc 	fde->fde_eoff = offset_from_end_sym;
152*0a6a1f1dSLionel Sambuc 
153*0a6a1f1dSLionel Sambuc 	STAILQ_INSERT_TAIL(&dbg->dbgp_fdelist, fde, fde_next);
154*0a6a1f1dSLionel Sambuc 
155*0a6a1f1dSLionel Sambuc 	return (dbg->dbgp_fdelen++);
156*0a6a1f1dSLionel Sambuc }
157*0a6a1f1dSLionel Sambuc 
158*0a6a1f1dSLionel Sambuc Dwarf_P_Fde
dwarf_fde_cfa_offset(Dwarf_P_Fde fde,Dwarf_Unsigned reg,Dwarf_Signed offset,Dwarf_Error * error)159*0a6a1f1dSLionel Sambuc dwarf_fde_cfa_offset(Dwarf_P_Fde fde, Dwarf_Unsigned reg, Dwarf_Signed offset,
160*0a6a1f1dSLionel Sambuc     Dwarf_Error *error)
161*0a6a1f1dSLionel Sambuc {
162*0a6a1f1dSLionel Sambuc 	int ret;
163*0a6a1f1dSLionel Sambuc 	Dwarf_Debug dbg;
164*0a6a1f1dSLionel Sambuc 
165*0a6a1f1dSLionel Sambuc 	dbg = fde != NULL ? fde->fde_dbg : NULL;
166*0a6a1f1dSLionel Sambuc 
167*0a6a1f1dSLionel Sambuc 	if (fde == NULL || reg > 0x3f) {
168*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
169*0a6a1f1dSLionel Sambuc 		return (DW_DLV_BADADDR);
170*0a6a1f1dSLionel Sambuc 	}
171*0a6a1f1dSLionel Sambuc 
172*0a6a1f1dSLionel Sambuc 	ret = _dwarf_frame_fde_add_inst(fde, DW_CFA_offset | (reg & 0x3f),
173*0a6a1f1dSLionel Sambuc 	    offset, 0, error);
174*0a6a1f1dSLionel Sambuc 
175*0a6a1f1dSLionel Sambuc 	if (ret != DW_DLE_NONE)
176*0a6a1f1dSLionel Sambuc 		return (DW_DLV_BADADDR);
177*0a6a1f1dSLionel Sambuc 
178*0a6a1f1dSLionel Sambuc 	return (fde);
179*0a6a1f1dSLionel Sambuc }
180*0a6a1f1dSLionel Sambuc 
181*0a6a1f1dSLionel Sambuc Dwarf_P_Fde
dwarf_add_fde_inst(Dwarf_P_Fde fde,Dwarf_Small op,Dwarf_Unsigned val1,Dwarf_Unsigned val2,Dwarf_Error * error)182*0a6a1f1dSLionel Sambuc dwarf_add_fde_inst(Dwarf_P_Fde fde, Dwarf_Small op, Dwarf_Unsigned val1,
183*0a6a1f1dSLionel Sambuc     Dwarf_Unsigned val2, Dwarf_Error *error)
184*0a6a1f1dSLionel Sambuc {
185*0a6a1f1dSLionel Sambuc 	int ret;
186*0a6a1f1dSLionel Sambuc 
187*0a6a1f1dSLionel Sambuc 	if (fde == NULL) {
188*0a6a1f1dSLionel Sambuc 		DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
189*0a6a1f1dSLionel Sambuc 		return (DW_DLV_BADADDR);
190*0a6a1f1dSLionel Sambuc 	}
191*0a6a1f1dSLionel Sambuc 
192*0a6a1f1dSLionel Sambuc 	ret = _dwarf_frame_fde_add_inst(fde, op, val1, val2, error);
193*0a6a1f1dSLionel Sambuc 
194*0a6a1f1dSLionel Sambuc 	if (ret != DW_DLE_NONE)
195*0a6a1f1dSLionel Sambuc 		return (DW_DLV_BADADDR);
196*0a6a1f1dSLionel Sambuc 
197*0a6a1f1dSLionel Sambuc 	return (fde);
198*0a6a1f1dSLionel Sambuc }
199