1 /*- 2 * Copyright (c) 2010 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_frame.c 2074 2011-10-27 03:34:33Z jkoshy $"); 30 31 Dwarf_P_Fde 32 dwarf_new_fde(Dwarf_P_Debug dbg, Dwarf_Error *error) 33 { 34 Dwarf_P_Fde fde; 35 36 if (dbg == NULL) { 37 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); 38 return (DW_DLV_BADADDR); 39 } 40 41 if ((fde = calloc(1, sizeof(struct _Dwarf_Fde))) == NULL) { 42 DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); 43 return (DW_DLV_BADADDR); 44 } 45 46 fde->fde_dbg = dbg; 47 48 return (fde); 49 } 50 51 Dwarf_Unsigned 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) 55 { 56 Dwarf_P_Cie cie; 57 58 if (dbg == NULL) { 59 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); 60 return (DW_DLV_NOCOUNT); 61 } 62 63 if ((cie = calloc(1, sizeof(struct _Dwarf_Cie))) == NULL) { 64 DWARF_SET_ERROR(dbg, error,DW_DLE_MEMORY); 65 return (DW_DLV_NOCOUNT); 66 } 67 STAILQ_INSERT_TAIL(&dbg->dbgp_cielist, cie, cie_next); 68 69 cie->cie_index = dbg->dbgp_cielen++; 70 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); 76 } 77 } 78 79 cie->cie_caf = caf; 80 cie->cie_daf = (int8_t) daf; /* daf is signed. */ 81 cie->cie_ra = ra; 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); 87 } 88 memcpy(cie->cie_initinst, initinst, inst_len); 89 cie->cie_instlen = inst_len; 90 } 91 92 return (cie->cie_index); 93 } 94 95 Dwarf_Unsigned 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) 99 { 100 101 return (dwarf_add_frame_fde_b(dbg, fde, die, cie, virt_addr, code_len, 102 symbol_index, 0, 0, error)); 103 } 104 105 Dwarf_Unsigned 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) 110 { 111 Dwarf_P_Cie ciep; 112 int i; 113 114 /* 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? 118 */ 119 (void) die; 120 121 if (dbg == NULL || fde == NULL || fde->fde_dbg != dbg) { 122 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); 123 return (DW_DLV_NOCOUNT); 124 } 125 126 ciep = STAILQ_FIRST(&dbg->dbgp_cielist); 127 for (i = 0; (Dwarf_Unsigned) i < cie; i++) { 128 ciep = STAILQ_NEXT(ciep, cie_next); 129 if (ciep == NULL) 130 break; 131 } 132 if (ciep == NULL) { 133 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); 134 return (DW_DLV_NOCOUNT); 135 } 136 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); 141 } 142 143 fde->fde_cie = ciep; 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; 149 150 STAILQ_INSERT_TAIL(&dbg->dbgp_fdelist, fde, fde_next); 151 152 return (dbg->dbgp_fdelen++); 153 } 154 155 Dwarf_P_Fde 156 dwarf_fde_cfa_offset(Dwarf_P_Fde fde, Dwarf_Unsigned reg, Dwarf_Signed offset, 157 Dwarf_Error *error) 158 { 159 int ret; 160 Dwarf_Debug dbg; 161 162 dbg = fde != NULL ? fde->fde_dbg : NULL; 163 164 if (fde == NULL || reg > 0x3f) { 165 DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); 166 return (DW_DLV_BADADDR); 167 } 168 169 ret = _dwarf_frame_fde_add_inst(fde, DW_CFA_offset | (reg & 0x3f), 170 offset, 0, error); 171 172 if (ret != DW_DLE_NONE) 173 return (DW_DLV_BADADDR); 174 175 return (fde); 176 } 177 178 Dwarf_P_Fde 179 dwarf_add_fde_inst(Dwarf_P_Fde fde, Dwarf_Small op, Dwarf_Unsigned val1, 180 Dwarf_Unsigned val2, Dwarf_Error *error) 181 { 182 int ret; 183 184 if (fde == NULL) { 185 DWARF_SET_ERROR(NULL, error, DW_DLE_ARGUMENT); 186 return (DW_DLV_BADADDR); 187 } 188 189 ret = _dwarf_frame_fde_add_inst(fde, op, val1, val2, error); 190 191 if (ret != DW_DLE_NONE) 192 return (DW_DLV_BADADDR); 193 194 return (fde); 195 } 196