2 * Copyright (c) 2010 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include "_libdwarf.h"
29 ELFTC_VCSID("$Id: dwarf_pro_frame.c 2074 2011-10-27 03:34:33Z jkoshy $");
32 dwarf_new_fde(Dwarf_P_Debug dbg, Dwarf_Error *error)
37 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
38 return (DW_DLV_BADADDR);
41 if ((fde = calloc(1, sizeof(struct _Dwarf_Fde))) == NULL) {
42 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
43 return (DW_DLV_BADADDR);
52 dwarf_add_frame_cie(Dwarf_P_Debug dbg, char *augmenter, Dwarf_Small caf,
53 Dwarf_Small daf, Dwarf_Small ra, Dwarf_Ptr initinst,
54 Dwarf_Unsigned inst_len, Dwarf_Error *error)
59 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
60 return (DW_DLV_NOCOUNT);
63 if ((cie = calloc(1, sizeof(struct _Dwarf_Cie))) == NULL) {
64 DWARF_SET_ERROR(dbg, error,DW_DLE_MEMORY);
65 return (DW_DLV_NOCOUNT);
67 STAILQ_INSERT_TAIL(&dbg->dbgp_cielist, cie, cie_next);
69 cie->cie_index = dbg->dbgp_cielen++;
71 if (augmenter != NULL) {
72 cie->cie_augment = (uint8_t *) strdup(augmenter);
73 if (cie->cie_augment == NULL) {
74 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
75 return (DW_DLV_NOCOUNT);
80 cie->cie_daf = (int8_t) daf; /* daf is signed. */
82 if (initinst != NULL && inst_len > 0) {
83 cie->cie_initinst = malloc((size_t) inst_len);
84 if (cie->cie_initinst == NULL) {
85 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
86 return (DW_DLV_NOCOUNT);
88 memcpy(cie->cie_initinst, initinst, inst_len);
89 cie->cie_instlen = inst_len;
92 return (cie->cie_index);
96 dwarf_add_frame_fde(Dwarf_P_Debug dbg, Dwarf_P_Fde fde, Dwarf_P_Die die,
97 Dwarf_Unsigned cie, Dwarf_Addr virt_addr, Dwarf_Unsigned code_len,
98 Dwarf_Unsigned symbol_index, Dwarf_Error *error)
101 return (dwarf_add_frame_fde_b(dbg, fde, die, cie, virt_addr, code_len,
102 symbol_index, 0, 0, error));
106 dwarf_add_frame_fde_b(Dwarf_P_Debug dbg, Dwarf_P_Fde fde, Dwarf_P_Die die,
107 Dwarf_Unsigned cie, Dwarf_Addr virt_addr, Dwarf_Unsigned code_len,
108 Dwarf_Unsigned symbol_index, Dwarf_Unsigned end_symbol_index,
109 Dwarf_Addr offset_from_end_sym, Dwarf_Error *error)
115 * XXX SGI libdwarf need the DIE arg because later it will insert a
116 * DW_AT_MIPS_fde attribute, which points to the offset the
117 * correspoding FDE, into this DIE. Do we need this?
121 if (dbg == NULL || fde == NULL || fde->fde_dbg != dbg) {
122 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
123 return (DW_DLV_NOCOUNT);
126 ciep = STAILQ_FIRST(&dbg->dbgp_cielist);
127 for (i = 0; (Dwarf_Unsigned) i < cie; i++) {
128 ciep = STAILQ_NEXT(ciep, cie_next);
133 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
134 return (DW_DLV_NOCOUNT);
137 if (end_symbol_index > 0 &&
138 (dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
139 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
140 return (DW_DLV_NOCOUNT);
144 fde->fde_initloc = virt_addr;
145 fde->fde_adrange = code_len;
146 fde->fde_symndx = symbol_index;
147 fde->fde_esymndx = end_symbol_index;
148 fde->fde_eoff = offset_from_end_sym;
150 STAILQ_INSERT_TAIL(&dbg->dbgp_fdelist, fde, fde_next);
152 return (dbg->dbgp_fdelen++);
156 dwarf_fde_cfa_offset(Dwarf_P_Fde fde, Dwarf_Unsigned reg, Dwarf_Signed offset,
162 dbg = fde != NULL ? fde->fde_dbg : NULL;
164 if (fde == NULL || reg > 0x3f) {
165 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT);
166 return (DW_DLV_BADADDR);
169 ret = _dwarf_frame_fde_add_inst(fde, DW_CFA_offset | (reg & 0x3f),
172 if (ret != DW_DLE_NONE)
173 return (DW_DLV_BADADDR);
179 dwarf_add_fde_inst(Dwarf_P_Fde fde, Dwarf_Small op, Dwarf_Unsigned val1,
180 Dwarf_Unsigned val2, Dwarf_Error *error)
185 DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT);
186 return (DW_DLV_BADADDR);
189 ret = _dwarf_frame_fde_add_inst(fde, op, val1, val2, error);
191 if (ret != DW_DLE_NONE)
192 return (DW_DLV_BADADDR);