| 1 |
/* |
| 2 |
Copyright (C) 2017-2018 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 <stdlib.h> /* calloc() free() */ |
| 32 |
#include <string.h> /* strcmp() */ |
| 33 |
|
| 34 |
#if defined(_WIN32) && defined(HAVE_STDAFX_H) |
| 35 |
#include "stdafx.h" |
| 36 |
#endif /* HAVE_STDAFX_H */ |
| 37 |
|
| 38 |
#ifdef HAVE_STDINT_H |
| 39 |
#include <stdint.h> /* uintptr_t */ |
| 40 |
#endif /* HAVE_STDINT_H */ |
| 41 |
|
| 42 |
#include "dwarf.h" |
| 43 |
#include "libdwarf.h" |
| 44 |
#include "libdwarf_private.h" |
| 45 |
#include "dwarf_base_types.h" |
| 46 |
#include "dwarf_opaque.h" |
| 47 |
#include "dwarf_util.h" |
| 48 |
#include "dwarf_error.h" |
| 49 |
#include "dwarf_tsearch.h" |
| 50 |
|
| 51 |
#define HASHSEARCH |
| 52 |
|
| 53 |
/* It has not escaped our attention that the section-group |
| 54 |
tsearch hash table could |
| 55 |
be replaced by a simple array with space for each possible |
| 56 |
section number, each element being the group number. |
| 57 |
This would be much simpler than what follows here. */ |
| 58 |
|
| 59 |
/* Each section number can appear in at most one record in the hash |
| 60 |
because each section belongs in only one group. |
| 61 |
Each group number appears as often as appropriate. */ |
| 62 |
|
| 63 |
struct Dwarf_Group_Map_Entry_s { |
| 64 |
unsigned gm_key; /* section number */ |
| 65 |
unsigned gm_group_number; /* What group number is. */ |
| 66 |
|
| 67 |
/* The name is from static storage or from elf, |
| 68 |
so there is nothing to free on record delete. */ |
| 69 |
const char * gm_section_name; |
| 70 |
}; |
| 71 |
|
| 72 |
static void * |
| 73 |
grp_make_entry(unsigned section, unsigned group,const char *name) |
| 74 |
{ |
| 75 |
struct Dwarf_Group_Map_Entry_s *e = 0; |
| 76 |
e = calloc(1,sizeof(struct Dwarf_Group_Map_Entry_s)); |
| 77 |
if (e) { |
| 78 |
e->gm_key = section; |
| 79 |
e->gm_group_number = group; |
| 80 |
e->gm_section_name = name; |
| 81 |
} |
| 82 |
return e; |
| 83 |
} |
| 84 |
|
| 85 |
static DW_TSHASHTYPE |
| 86 |
grp_data_hashfunc(const void *keyp) |
| 87 |
{ |
| 88 |
const struct Dwarf_Group_Map_Entry_s * enp = keyp; |
| 89 |
DW_TSHASHTYPE hashv = 0; |
| 90 |
|
| 91 |
hashv = enp->gm_key; |
| 92 |
return hashv; |
| 93 |
} |
| 94 |
|
| 95 |
static int |
| 96 |
grp_compare_function(const void *l, const void *r) |
| 97 |
{ |
| 98 |
const struct Dwarf_Group_Map_Entry_s * lp = l; |
| 99 |
const struct Dwarf_Group_Map_Entry_s * rp = r; |
| 100 |
|
| 101 |
if (lp->gm_key < rp->gm_key) { |
| 102 |
return -1; |
| 103 |
} |
| 104 |
if (lp->gm_key > rp->gm_key) { |
| 105 |
return 1; |
| 106 |
} |
| 107 |
|
| 108 |
/* match. */ |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
int |
| 113 |
_dwarf_insert_in_group_map(Dwarf_Debug dbg, |
| 114 |
unsigned groupnum, |
| 115 |
unsigned section_index, |
| 116 |
const char *name, |
| 117 |
Dwarf_Error * error) |
| 118 |
{ |
| 119 |
struct Dwarf_Group_Data_s *grp = &dbg->de_groupnumbers; |
| 120 |
|
| 121 |
void *entry2 = 0; |
| 122 |
struct Dwarf_Group_Map_Entry_s * entry3 = 0; |
| 123 |
|
| 124 |
if (!grp->gd_map) { |
| 125 |
/* Number of sections is a kind of decent guess |
| 126 |
as to how much space would be useful. */ |
| 127 |
dwarf_initialize_search_hash(&grp->gd_map, |
| 128 |
grp_data_hashfunc,grp->gd_number_of_sections); |
| 129 |
if (!grp->gd_map) { |
| 130 |
/* It's really an error I suppose. */ |
| 131 |
return DW_DLV_NO_ENTRY; |
| 132 |
} |
| 133 |
} |
| 134 |
entry3 = grp_make_entry(section_index,groupnum,name); |
| 135 |
if (!entry3) { |
| 136 |
_dwarf_error(dbg, error, DW_DLE_GROUP_MAP_ALLOC); |
| 137 |
return DW_DLV_ERROR; |
| 138 |
} |
| 139 |
entry2 = dwarf_tsearch(entry3,&grp->gd_map,grp_compare_function); |
| 140 |
if (!entry2) { |
| 141 |
free(entry3); |
| 142 |
_dwarf_error(dbg, error, DW_DLE_GROUP_MAP_ALLOC); |
| 143 |
return DW_DLV_ERROR; |
| 144 |
} else { |
| 145 |
struct Dwarf_Group_Map_Entry_s *re = 0; |
| 146 |
re = *(struct Dwarf_Group_Map_Entry_s **)entry2; |
| 147 |
if (re != entry3) { |
| 148 |
free(entry3); |
| 149 |
_dwarf_error(dbg, error, DW_DLE_GROUP_MAP_DUPLICATE); |
| 150 |
return DW_DLV_ERROR; |
| 151 |
} else { |
| 152 |
++grp->gd_map_entry_count; |
| 153 |
/* OK. Added. Fall thru */ |
| 154 |
} |
| 155 |
} |
| 156 |
return DW_DLV_OK; |
| 157 |
} |
| 158 |
|
| 159 |
int |
| 160 |
_dwarf_section_get_target_group_from_map(Dwarf_Debug dbg, |
| 161 |
unsigned obj_section_index, |
| 162 |
unsigned * groupnumber_out, |
| 163 |
Dwarf_Error * error) |
| 164 |
{ |
| 165 |
struct Dwarf_Group_Map_Entry_s entry; |
| 166 |
struct Dwarf_Group_Map_Entry_s *entry2; |
| 167 |
struct Dwarf_Group_Data_s *grp = &dbg->de_groupnumbers; |
| 168 |
|
| 169 |
(void)error; |
| 170 |
if (!grp->gd_map) { |
| 171 |
return DW_DLV_NO_ENTRY; |
| 172 |
} |
| 173 |
entry.gm_key = obj_section_index; |
| 174 |
entry.gm_group_number = 0; /* FAKE */ |
| 175 |
entry.gm_section_name = ""; /* FAKE */ |
| 176 |
|
| 177 |
entry2 = dwarf_tfind(&entry, &grp->gd_map,grp_compare_function); |
| 178 |
if (entry2) { |
| 179 |
struct Dwarf_Group_Map_Entry_s *e2 = |
| 180 |
*(struct Dwarf_Group_Map_Entry_s **)entry2;; |
| 181 |
*groupnumber_out = e2->gm_group_number; |
| 182 |
return DW_DLV_OK; |
| 183 |
} |
| 184 |
return DW_DLV_NO_ENTRY; |
| 185 |
} |
| 186 |
|
| 187 |
/* New May 2017. So users can find out what groups (dwo or COMDAT) |
| 188 |
are in the object and how much to allocate so one can get the |
| 189 |
group-section map data. */ |
| 190 |
int |
| 191 |
dwarf_sec_group_sizes(Dwarf_Debug dbg, |
| 192 |
Dwarf_Unsigned * section_count_out, |
| 193 |
Dwarf_Unsigned * group_count_out, |
| 194 |
Dwarf_Unsigned * selected_group_out, |
| 195 |
Dwarf_Unsigned * map_entry_count_out, |
| 196 |
Dwarf_Error * error) |
| 197 |
{ |
| 198 |
struct Dwarf_Group_Data_s *grp = 0; |
| 199 |
|
| 200 |
CHECK_DBG(dbg,error,"dwarf_sec_group_sizes()"); |
| 201 |
grp = &dbg->de_groupnumbers; |
| 202 |
*section_count_out = grp->gd_number_of_sections; |
| 203 |
*group_count_out = grp->gd_number_of_groups; |
| 204 |
*selected_group_out = dbg->de_groupnumber; |
| 205 |
*map_entry_count_out = grp->gd_map_entry_count; |
| 206 |
return DW_DLV_OK; |
| 207 |
} |
| 208 |
|
| 209 |
static Dwarf_Unsigned map_reccount = 0; |
| 210 |
static struct temp_map_struc_s { |
| 211 |
Dwarf_Unsigned section; |
| 212 |
Dwarf_Unsigned group; |
| 213 |
const char *name; |
| 214 |
} *temp_map_data; |
| 215 |
|
| 216 |
static void |
| 217 |
grp_walk_map(const void *nodep, |
| 218 |
const DW_VISIT which, |
| 219 |
const int depth) |
| 220 |
{ |
| 221 |
struct Dwarf_Group_Map_Entry_s *re = 0; |
| 222 |
|
| 223 |
(void)depth; |
| 224 |
re = *(struct Dwarf_Group_Map_Entry_s **)nodep; |
| 225 |
if (which == dwarf_postorder || which == dwarf_endorder) { |
| 226 |
return; |
| 227 |
} |
| 228 |
temp_map_data[map_reccount].group = re->gm_group_number; |
| 229 |
temp_map_data[map_reccount].section = re->gm_key; |
| 230 |
temp_map_data[map_reccount].name = re->gm_section_name; |
| 231 |
map_reccount += 1; |
| 232 |
} |
| 233 |
|
| 234 |
/* Looks better sorted by group then sec num. */ |
| 235 |
static int |
| 236 |
map_sort_compar(const void*l, const void*r) |
| 237 |
{ |
| 238 |
struct temp_map_struc_s *lv = (struct temp_map_struc_s *)l; |
| 239 |
struct temp_map_struc_s *rv = (struct temp_map_struc_s *)r; |
| 240 |
|
| 241 |
if (lv->group < rv->group) { |
| 242 |
return -1; |
| 243 |
} |
| 244 |
if (lv->group > rv->group) { |
| 245 |
return 1; |
| 246 |
} |
| 247 |
if (lv->section < rv->section) { |
| 248 |
return -1; |
| 249 |
} |
| 250 |
if (lv->section > rv->section) { |
| 251 |
return 1; |
| 252 |
} |
| 253 |
/* Should never get here! */ |
| 254 |
return 0; |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
/* New May 2017. Reveals the map between group numbers |
| 259 |
and section numbers. |
| 260 |
Caller must allocate the arrays with space for 'map_entry_count' |
| 261 |
values and this function fills in the array entries. |
| 262 |
Output ordered by group number and section number. |
| 263 |
*/ |
| 264 |
int |
| 265 |
dwarf_sec_group_map(Dwarf_Debug dbg, |
| 266 |
Dwarf_Unsigned map_entry_count, |
| 267 |
Dwarf_Unsigned * group_numbers_array, |
| 268 |
Dwarf_Unsigned * sec_numbers_array, |
| 269 |
const char ** sec_names_array, |
| 270 |
Dwarf_Error * error) |
| 271 |
{ |
| 272 |
Dwarf_Unsigned i = 0; |
| 273 |
struct Dwarf_Group_Data_s *grp = 0; |
| 274 |
|
| 275 |
CHECK_DBG(dbg,error,"dwarf_sec_group_map()"); |
| 276 |
if (temp_map_data) { |
| 277 |
_dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR); |
| 278 |
return DW_DLV_ERROR; |
| 279 |
} |
| 280 |
map_reccount = 0; |
| 281 |
grp = &dbg->de_groupnumbers; |
| 282 |
if (map_entry_count < grp->gd_map_entry_count) { |
| 283 |
_dwarf_error(dbg,error,DW_DLE_GROUP_COUNT_ERROR); |
| 284 |
return DW_DLV_ERROR; |
| 285 |
} |
| 286 |
temp_map_data = calloc(map_entry_count, |
| 287 |
sizeof(struct temp_map_struc_s)); |
| 288 |
if (!temp_map_data) { |
| 289 |
_dwarf_error(dbg,error,DW_DLE_GROUP_MAP_ALLOC); |
| 290 |
return DW_DLV_ERROR; |
| 291 |
} |
| 292 |
dwarf_twalk(grp->gd_map,grp_walk_map); |
| 293 |
if (map_reccount != grp->gd_map_entry_count) { |
| 294 |
/* Impossible. */ |
| 295 |
_dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR); |
| 296 |
return DW_DLV_ERROR; |
| 297 |
} |
| 298 |
|
| 299 |
qsort(temp_map_data,map_reccount,sizeof(struct temp_map_struc_s), |
| 300 |
map_sort_compar); |
| 301 |
for (i =0 ; i < map_reccount; ++i) { |
| 302 |
sec_numbers_array[i] = temp_map_data[i].section; |
| 303 |
group_numbers_array[i] = temp_map_data[i].group; |
| 304 |
sec_names_array[i] = temp_map_data[i].name; |
| 305 |
} |
| 306 |
free(temp_map_data); |
| 307 |
map_reccount = 0; |
| 308 |
temp_map_data = 0; |
| 309 |
return DW_DLV_OK; |
| 310 |
} |
| 311 |
|
| 312 |
static const char *dwo_secnames[] = { |
| 313 |
".debug_info.dwo", |
| 314 |
".debug_types.dwo", |
| 315 |
".debug_abbrev.dwo", |
| 316 |
".debug_line.dwo", |
| 317 |
".debug_loc.dwo", |
| 318 |
".debug_str.dwo", |
| 319 |
".debug_loclists.dwo", |
| 320 |
".debug_rnglists.dwo", |
| 321 |
".debug_str_offsets.dwo", |
| 322 |
".debug_macro.dwo", |
| 323 |
".debug_cu_index", |
| 324 |
".debug_tu_index", |
| 325 |
0 }; |
| 326 |
|
| 327 |
/* Assumption: dwo sections are never in a COMDAT group |
| 328 |
(groupnumber >2) |
| 329 |
and by definition here are never group 1. |
| 330 |
Assumption: the map of COMDAT groups (not necessarily all |
| 331 |
sections, but at least all COMDAT) is complete. */ |
| 332 |
int |
| 333 |
_dwarf_dwo_groupnumber_given_name( |
| 334 |
const char *name, |
| 335 |
unsigned *grpnum_out) |
| 336 |
{ |
| 337 |
const char **s = 0; |
| 338 |
|
| 339 |
for (s = dwo_secnames; *s; s++) { |
| 340 |
if (!strcmp(name,*s)) { |
| 341 |
*grpnum_out = DW_GROUPNUMBER_DWO; |
| 342 |
return DW_DLV_OK; |
| 343 |
} |
| 344 |
} |
| 345 |
return DW_DLV_NO_ENTRY; |
| 346 |
} |
| 347 |
|
| 348 |
static unsigned target_group = 0; |
| 349 |
static int found_name_in_group = 0; |
| 350 |
const char *lookfor_name = 0; |
| 351 |
|
| 352 |
static void |
| 353 |
grp_walk_for_name(const void *nodep, |
| 354 |
const DW_VISIT which, |
| 355 |
const int depth) |
| 356 |
{ |
| 357 |
struct Dwarf_Group_Map_Entry_s *re = 0; |
| 358 |
|
| 359 |
(void)depth; |
| 360 |
re = *(struct Dwarf_Group_Map_Entry_s **)nodep; |
| 361 |
if (which == dwarf_postorder || which == dwarf_endorder) { |
| 362 |
return; |
| 363 |
} |
| 364 |
if (re->gm_group_number == target_group) { |
| 365 |
if (!strcmp(lookfor_name,re->gm_section_name)) { |
| 366 |
found_name_in_group = TRUE; |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/* returns TRUE or FALSE */ |
| 372 |
int |
| 373 |
_dwarf_section_in_group_by_name(Dwarf_Debug dbg, |
| 374 |
const char * scn_name, |
| 375 |
unsigned groupnum) |
| 376 |
{ |
| 377 |
struct Dwarf_Group_Data_s *grp = 0; |
| 378 |
|
| 379 |
grp = &dbg->de_groupnumbers; |
| 380 |
found_name_in_group = FALSE; |
| 381 |
target_group = groupnum; |
| 382 |
lookfor_name = scn_name; |
| 383 |
dwarf_twalk(grp->gd_map,grp_walk_for_name); |
| 384 |
return found_name_in_group; |
| 385 |
} |
| 386 |
|
| 387 |
static void |
| 388 |
_dwarf_grp_destroy_free_node(void*nodep) |
| 389 |
{ |
| 390 |
struct Dwarf_Group_Map_Entry_s * enp = nodep; |
| 391 |
free(enp); |
| 392 |
return; |
| 393 |
} |
| 394 |
|
| 395 |
void |
| 396 |
_dwarf_destroy_group_map(Dwarf_Debug dbg) |
| 397 |
{ |
| 398 |
dwarf_tdestroy(dbg->de_groupnumbers.gd_map, |
| 399 |
_dwarf_grp_destroy_free_node); |
| 400 |
dbg->de_groupnumbers.gd_map = 0; |
| 401 |
} |