| 1 |
/* |
| 2 |
Copyright (C) 2021 David Anderson. All Rights Reserved. |
| 3 |
|
| 4 |
This program is free software; you can redistribute it |
| 5 |
and/or modify it under the terms of version 2.1 of the |
| 6 |
GNU Lesser General Public License as published by the Free |
| 7 |
Software Foundation. |
| 8 |
|
| 9 |
This program is distributed in the hope that it would be |
| 10 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 11 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 12 |
PURPOSE. |
| 13 |
|
| 14 |
Further, this software is distributed without any warranty |
| 15 |
that it is free of the rightful claim of any third person |
| 16 |
regarding infringement or the like. Any license provided |
| 17 |
herein, whether implied or otherwise, applies only to this |
| 18 |
software file. Patent licenses, if any, provided herein |
| 19 |
do not apply to combinations of this program with other |
| 20 |
software, or any other product whatsoever. |
| 21 |
|
| 22 |
You should have received a copy of the GNU Lesser General |
| 23 |
Public License along with this program; if not, write the |
| 24 |
Free Software Foundation, Inc., 51 Franklin Street - Fifth |
| 25 |
Floor, Boston MA 02110-1301, USA. |
| 26 |
|
| 27 |
*/ |
| 28 |
|
| 29 |
#include <config.h> |
| 30 |
|
| 31 |
#include <string.h> /* memcmp() */ |
| 32 |
#include <stdio.h> /* printf() debugging */ |
| 33 |
|
| 34 |
#if defined(_WIN32) && defined(HAVE_STDAFX_H) |
| 35 |
#include "stdafx.h" |
| 36 |
#endif /* HAVE_STDAFX_H */ |
| 37 |
|
| 38 |
#include "dwarf.h" |
| 39 |
#include "libdwarf.h" |
| 40 |
#include "libdwarf_private.h" |
| 41 |
#include "dwarf_base_types.h" |
| 42 |
#include "dwarf_opaque.h" |
| 43 |
#include "dwarf_alloc.h" |
| 44 |
#include "dwarf_error.h" |
| 45 |
#include "dwarf_util.h" |
| 46 |
#include "dwarf_string.h" |
| 47 |
#if 0 /* dump_bytes */ |
| 48 |
static void |
| 49 |
dump_bytes(const char *msg,int line, |
| 50 |
Dwarf_Small * start, long len) |
| 51 |
{ |
| 52 |
Dwarf_Small *end = start + len; |
| 53 |
Dwarf_Small *cur = start; |
| 54 |
printf("%s (0x%lx) line %d\n ",msg,(unsigned long)start,line); |
| 55 |
for (; cur < end; cur++) { |
| 56 |
printf("%02x", *cur); |
| 57 |
} |
| 58 |
printf("\n"); |
| 59 |
} |
| 60 |
#endif /*0*/ |
| 61 |
|
| 62 |
static int |
| 63 |
_dwarf_find_CU_Context_given_sig(Dwarf_Debug dbg, |
| 64 |
int context_level, |
| 65 |
Dwarf_Sig8 *sig_in, |
| 66 |
Dwarf_CU_Context *cu_context_out, |
| 67 |
Dwarf_Bool *is_info_out, |
| 68 |
Dwarf_Error *error) |
| 69 |
{ |
| 70 |
Dwarf_CU_Context cu_context = 0; |
| 71 |
Dwarf_Bool is_info = FALSE; |
| 72 |
int loopcount = 0; |
| 73 |
int lres = 0; |
| 74 |
Dwarf_Debug_InfoTypes dis = 0; |
| 75 |
struct Dwarf_Section_s *secdp = 0; |
| 76 |
|
| 77 |
/* Loop once with is_info, once with !is_info. |
| 78 |
Then stop. */ |
| 79 |
for ( ; loopcount < 2; ++loopcount) { |
| 80 |
Dwarf_CU_Context prev_cu_context = 0; |
| 81 |
Dwarf_Unsigned section_size = 0; |
| 82 |
Dwarf_Unsigned new_cu_offset = 0; |
| 83 |
|
| 84 |
is_info = !is_info; |
| 85 |
|
| 86 |
if (is_info) { |
| 87 |
dis = &dbg->de_info_reading; |
| 88 |
secdp = &dbg->de_debug_info; |
| 89 |
} else { |
| 90 |
dis = &dbg->de_types_reading; |
| 91 |
secdp = &dbg->de_debug_types; |
| 92 |
} |
| 93 |
lres = _dwarf_load_die_containing_section(dbg,is_info,error); |
| 94 |
if (lres == DW_DLV_ERROR) { |
| 95 |
return lres; |
| 96 |
} |
| 97 |
if (lres == DW_DLV_NO_ENTRY ) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
/* Lets see if we already have the CU we need. */ |
| 101 |
for (cu_context = dis->de_cu_context_list; |
| 102 |
cu_context; cu_context = cu_context->cc_next) { |
| 103 |
prev_cu_context = cu_context; |
| 104 |
|
| 105 |
if (memcmp(sig_in,&cu_context->cc_signature, |
| 106 |
sizeof(Dwarf_Sig8))) { |
| 107 |
continue; |
| 108 |
} |
| 109 |
if (cu_context->cc_unit_type == DW_UT_split_type|| |
| 110 |
cu_context->cc_unit_type == DW_UT_type) { |
| 111 |
*cu_context_out = cu_context; |
| 112 |
*is_info_out = cu_context->cc_is_info; |
| 113 |
return DW_DLV_OK; |
| 114 |
} |
| 115 |
} |
| 116 |
if (context_level > 0) { |
| 117 |
/* Make no attempt to create new context, |
| 118 |
we are finishing cu die base fields |
| 119 |
on one already. |
| 120 |
Just look for the other context, |
| 121 |
DWARF4 debug_types */ |
| 122 |
continue; |
| 123 |
} |
| 124 |
if (prev_cu_context) { |
| 125 |
Dwarf_CU_Context lcu_context = prev_cu_context; |
| 126 |
new_cu_offset = |
| 127 |
_dwarf_calculate_next_cu_context_offset( |
| 128 |
lcu_context); |
| 129 |
} else { |
| 130 |
new_cu_offset = 0; |
| 131 |
} |
| 132 |
section_size = secdp->dss_size; |
| 133 |
for ( ; new_cu_offset < section_size; |
| 134 |
new_cu_offset = |
| 135 |
_dwarf_calculate_next_cu_context_offset( |
| 136 |
cu_context)) { |
| 137 |
#if 0 /* unnecessary load section call, we think. */ |
| 138 |
lres = _dwarf_load_die_containing_section(dbg, |
| 139 |
is_info,error); |
| 140 |
if (lres == DW_DLV_ERROR) { |
| 141 |
return lres; |
| 142 |
} |
| 143 |
if (lres == DW_DLV_NO_ENTRY) { |
| 144 |
continue; |
| 145 |
} |
| 146 |
#endif /*0*/ |
| 147 |
lres = _dwarf_create_a_new_cu_context_record_on_list( |
| 148 |
dbg, dis,is_info,section_size,new_cu_offset, |
| 149 |
&cu_context,NULL,error); |
| 150 |
if (lres == DW_DLV_ERROR) { |
| 151 |
return lres; |
| 152 |
} |
| 153 |
if (lres == DW_DLV_NO_ENTRY) { |
| 154 |
break; |
| 155 |
} |
| 156 |
if (memcmp(sig_in,&cu_context->cc_signature, |
| 157 |
sizeof(Dwarf_Sig8))) { |
| 158 |
continue; |
| 159 |
} |
| 160 |
if (cu_context->cc_unit_type == DW_UT_split_type|| |
| 161 |
cu_context->cc_unit_type == DW_UT_type) { |
| 162 |
*cu_context_out = cu_context; |
| 163 |
*is_info_out = cu_context->cc_is_info; |
| 164 |
return DW_DLV_OK; |
| 165 |
} |
| 166 |
} |
| 167 |
} /* Loop-end. */ |
| 168 |
/* Not found */ |
| 169 |
return DW_DLV_NO_ENTRY; |
| 170 |
} |
| 171 |
|
| 172 |
/* We will search to find a CU with the indicated signature |
| 173 |
The attribute leading us here is often |
| 174 |
We are looking for a DW_UT_split_type or DW_UT_type |
| 175 |
CU. |
| 176 |
DW_AT_type and if DWARF4 that means our first look is |
| 177 |
to !is_info */ |
| 178 |
int |
| 179 |
dwarf_find_die_given_sig8(Dwarf_Debug dbg, |
| 180 |
Dwarf_Sig8 *ref, |
| 181 |
Dwarf_Die *die_out, |
| 182 |
Dwarf_Bool *is_info, |
| 183 |
Dwarf_Error *error) |
| 184 |
{ |
| 185 |
int res = 0; |
| 186 |
CHECK_DBG(dbg,error,"dwarf_find_die_given_sig8()"); |
| 187 |
res = _dwarf_internal_find_die_given_sig8( |
| 188 |
dbg,0,ref,die_out,is_info,error); |
| 189 |
return res; |
| 190 |
} |
| 191 |
|
| 192 |
/* If context level > 0 restrict what we will do |
| 193 |
to avoid recursion creating CU Contexts */ |
| 194 |
int |
| 195 |
_dwarf_internal_find_die_given_sig8(Dwarf_Debug dbg, |
| 196 |
int context_level, |
| 197 |
Dwarf_Sig8 *ref, |
| 198 |
Dwarf_Die *die_out, |
| 199 |
Dwarf_Bool *is_info, |
| 200 |
Dwarf_Error *error) |
| 201 |
{ |
| 202 |
int res = 0; |
| 203 |
Dwarf_Die ndi = 0; |
| 204 |
Dwarf_CU_Context context = 0; |
| 205 |
Dwarf_Bool result_is_info = FALSE; |
| 206 |
Dwarf_Unsigned dieoffset = 0; |
| 207 |
|
| 208 |
res =_dwarf_find_CU_Context_given_sig(dbg, |
| 209 |
context_level, |
| 210 |
ref, &context, &result_is_info,error); |
| 211 |
if (res != DW_DLV_OK) { |
| 212 |
return res; |
| 213 |
} |
| 214 |
dieoffset = context->cc_debug_offset + |
| 215 |
context->cc_signature_offset; |
| 216 |
res = dwarf_offdie_b(dbg,dieoffset,result_is_info, |
| 217 |
&ndi,error); |
| 218 |
if (res == DW_DLV_OK) { |
| 219 |
*die_out = ndi; |
| 220 |
*is_info = result_is_info; |
| 221 |
} |
| 222 |
return res; |
| 223 |
} |