| 1 |
/* |
| 2 |
Copyright (C) 2017-2017 David Anderson. All rights reserved. |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of version 2 of the GNU General |
| 6 |
Public License as published by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it would be |
| 9 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 10 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. |
| 12 |
|
| 13 |
Further, this software is distributed without any warranty |
| 14 |
that it is free of the rightful claim of any third person |
| 15 |
regarding infringement or the like. Any license provided |
| 16 |
herein, whether implied or otherwise, applies only to this |
| 17 |
software file. Patent licenses, if any, provided herein |
| 18 |
do not apply to combinations of this program with other |
| 19 |
software, or any other product whatsoever. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public |
| 22 |
License along with this program; if not, write the Free |
| 23 |
Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
| 24 |
Boston MA 02110-1301, USA. |
| 25 |
|
| 26 |
*/ |
| 27 |
|
| 28 |
#include <config.h> |
| 29 |
|
| 30 |
#include <stdio.h> /* printf() */ |
| 31 |
#include <stdlib.h> /* calloc() free() */ |
| 32 |
|
| 33 |
#include "dwarf.h" |
| 34 |
#include "libdwarf.h" |
| 35 |
#include "libdwarf_private.h" |
| 36 |
#include "dd_defined_types.h" |
| 37 |
#include "dd_checkutil.h" |
| 38 |
#include "dd_glflags.h" |
| 39 |
#include "dd_globals.h" |
| 40 |
#include "dd_sanitized.h" |
| 41 |
#include "dd_naming.h" |
| 42 |
|
| 43 |
/* Two purposes here related to COMDAT: |
| 44 |
A) get and print the data on sections and groups. |
| 45 |
B) reset certain global 'print' flags so printing |
| 46 |
a COMDAT section does not print the major |
| 47 |
sections that are from group 1 (DW_GROUPDATA_BASE) |
| 48 |
|
| 49 |
The functions are called from dwarfdump.c and only once. |
| 50 |
So static vars are safe. |
| 51 |
*/ |
| 52 |
|
| 53 |
static Dwarf_Unsigned group_map_entry_count = 0; |
| 54 |
static Dwarf_Unsigned selected_group = 0; |
| 55 |
static Dwarf_Unsigned *sec_nums = 0; |
| 56 |
static Dwarf_Unsigned *group_nums = 0; |
| 57 |
static const char ** sec_names = 0; |
| 58 |
static Dwarf_Unsigned group_count; |
| 59 |
static Dwarf_Unsigned section_count; |
| 60 |
|
| 61 |
static void |
| 62 |
freeall_groups_tables(void) |
| 63 |
{ |
| 64 |
free(sec_nums); |
| 65 |
sec_nums = 0; |
| 66 |
free(group_nums); |
| 67 |
group_nums = 0; |
| 68 |
/* Cast prevents an ugly warning about the const |
| 69 |
being stripped off. */ |
| 70 |
free((void*)sec_names); |
| 71 |
sec_names = 0; |
| 72 |
group_map_entry_count = 0; |
| 73 |
selected_group = 0; |
| 74 |
group_count = 0; |
| 75 |
section_count = 0; |
| 76 |
} |
| 77 |
|
| 78 |
#define TRUE 1 |
| 79 |
#define FALSE 0 |
| 80 |
#if 0 |
| 81 |
static struct glfsetting_s { |
| 82 |
const char *secname; |
| 83 |
Dwarf_Bool *flag; |
| 84 |
Dwarf_Bool origset; |
| 85 |
Dwarf_Bool origflag; |
| 86 |
} glftab[] = { |
| 87 |
{".debug_abbrev", &glflags.gf_abbrev_flag,FALSE,FALSE}, |
| 88 |
{".debug_aranges", &glflags.gf_aranges_flag,FALSE,FALSE}, |
| 89 |
{".debug_debug_macinfo",&glflags.gf_macinfo_flag,FALSE,FALSE}, |
| 90 |
{".debug_debug_macro", &glflags.gf_macro_flag,FALSE,FALSE}, |
| 91 |
{".debug_debug_names", &glflags.gf_debug_names_flag,FALSE,FALSE}, |
| 92 |
{".debug_eh_frame", &glflags.gf_eh_frame_flag,FALSE,FALSE}, |
| 93 |
{".debug_frame", &glflags.gf_frame_flag,FALSE,FALSE}, |
| 94 |
{".gdb_index", &glflags.gf_gdbindex_flag,FALSE,FALSE}, |
| 95 |
{".debug_info", &glflags.gf_info_flag,FALSE,FALSE}, |
| 96 |
{".debug_line", &glflags.gf_line_flag,FALSE,FALSE}, |
| 97 |
{".debug_loc", &glflags.gf_loc_flag,FALSE,FALSE}, |
| 98 |
/*{".debug_loclists", &glflags.gf_loclists_flag,FALSE,FALSE}, */ |
| 99 |
{".debug_pubnames", &glflags.gf_pubnames_flag,FALSE,FALSE}, |
| 100 |
|
| 101 |
/* SGI only */ |
| 102 |
{".debug_pubtypes", &glflags.gf_pubtypes_flag,FALSE,FALSE}, |
| 103 |
|
| 104 |
{".debug_ranges", &glflags.gf_ranges_flag,FALSE,FALSE}, |
| 105 |
/*{".debug_rnglists", &glflags.gf_rnglists_flag,FALSE,FALSE}, */ |
| 106 |
|
| 107 |
/* SGI only */ |
| 108 |
{".debug_static_func", &glflags.gf_static_func_flag,FALSE,FALSE}, |
| 109 |
|
| 110 |
/* SGI only */ |
| 111 |
{".debug_static_var", &glflags.gf_static_var_flag,FALSE,FALSE}, |
| 112 |
|
| 113 |
{".debug_str", &glflags.gf_string_flag,FALSE,FALSE}, |
| 114 |
{".debug_types", &glflags.gf_types_flag,FALSE,FALSE}, |
| 115 |
|
| 116 |
/* SGI only */ |
| 117 |
{".debug_weaknames", &glflags.gf_weakname_flag,FALSE,FALSE}, |
| 118 |
{0,0,0,0} |
| 119 |
}; |
| 120 |
#endif /* 0 */ |
| 121 |
|
| 122 |
/* If a section is not in group N but is in group 1 |
| 123 |
then turn off its flag. Since sections are never |
| 124 |
in both (various DW_DLE*DUPLICATE errors |
| 125 |
if libdwarf tries to set in both), just look in |
| 126 |
group one. |
| 127 |
|
| 128 |
See groups_restore_subsidiary_flags() just below. |
| 129 |
|
| 130 |
FIXME: It would be good if, for a wholly missing |
| 131 |
section related to a flag, that the flag got turned |
| 132 |
off. */ |
| 133 |
#if 0 |
| 134 |
static void |
| 135 |
turn_off_subsidiary_flags(void) |
| 136 |
{ |
| 137 |
Dwarf_Unsigned i = 0; |
| 138 |
|
| 139 |
for ( ; i < group_map_entry_count; ++i) { |
| 140 |
if (group_nums[i] == 1) { |
| 141 |
unsigned k = 0; |
| 142 |
const char* oursec = sec_names[i]; |
| 143 |
|
| 144 |
for ( ; glftab[k].secname; ++k ) { |
| 145 |
if (!strcmp(oursec,glftab[k].secname)) { |
| 146 |
if (!glftab[k].origset) { |
| 147 |
glftab[k].origset = TRUE; |
| 148 |
glftab[k].origflag = *(glftab[k].flag); |
| 149 |
} |
| 150 |
*(glftab[k].flag) = FALSE; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
#endif |
| 158 |
/* Restoring original condition in the glftab array |
| 159 |
and in the global flags it points to. |
| 160 |
So that when processing an archive one can restore |
| 161 |
the user-chosen flags and print subsequent object |
| 162 |
groups correctly. |
| 163 |
New October 16, 2017. */ |
| 164 |
|
| 165 |
void |
| 166 |
groups_restore_subsidiary_flags(void) |
| 167 |
{ |
| 168 |
#if 0 |
| 169 |
unsigned k = 0; |
| 170 |
#endif |
| 171 |
|
| 172 |
/* Duplicative but harmless free. */ |
| 173 |
freeall_groups_tables(); |
| 174 |
#if 0 |
| 175 |
for ( ; glftab[k].secname; ++k ) { |
| 176 |
if (glftab[k].origset) { |
| 177 |
*(glftab[k].flag) = glftab[k].origflag; |
| 178 |
glftab[k].origset = FALSE; |
| 179 |
glftab[k].origflag = FALSE; |
| 180 |
} |
| 181 |
} |
| 182 |
#endif |
| 183 |
} |
| 184 |
|
| 185 |
/* NEW May 2017. |
| 186 |
Has a side effect of using the local table set up by |
| 187 |
print_section_groups_data() and then frees the |
| 188 |
table data. |
| 189 |
|
| 190 |
For multi-object archive reading: main() calls |
| 191 |
groups_restore_subsidiary_flags() at the end |
| 192 |
of each object file to restore |
| 193 |
the original flags that turn_off_subsidiary_flags() |
| 194 |
changed. |
| 195 |
*/ |
| 196 |
void |
| 197 |
update_section_flags_per_groups(void) |
| 198 |
{ |
| 199 |
if (!sec_names) { |
| 200 |
/* The tables are absent. Internal logic |
| 201 |
error here somewhere. */ |
| 202 |
freeall_groups_tables(); |
| 203 |
return; |
| 204 |
} |
| 205 |
if (selected_group == DW_GROUPNUMBER_BASE) { |
| 206 |
freeall_groups_tables(); |
| 207 |
return; |
| 208 |
} |
| 209 |
if (selected_group == DW_GROUPNUMBER_DWO) { |
| 210 |
freeall_groups_tables(); |
| 211 |
return; |
| 212 |
} |
| 213 |
#if 0 |
| 214 |
turn_off_subsidiary_flags(); |
| 215 |
#endif |
| 216 |
freeall_groups_tables(); |
| 217 |
} |
| 218 |
|
| 219 |
/* NEW May 2017. |
| 220 |
Reports on section groupings like DWO(split dwarf) |
| 221 |
and COMDAT groups. As a side effect creates local |
| 222 |
table of section and group data */ |
| 223 |
int |
| 224 |
print_section_groups_data(Dwarf_Debug dbg,Dwarf_Error *error) |
| 225 |
{ |
| 226 |
int res = 0; |
| 227 |
Dwarf_Unsigned i = 0; |
| 228 |
|
| 229 |
res = dwarf_sec_group_sizes(dbg,§ion_count, |
| 230 |
&group_count,&selected_group, &group_map_entry_count, |
| 231 |
error); |
| 232 |
if (res != DW_DLV_OK) { |
| 233 |
simple_err_return_msg_either_action(res, |
| 234 |
"ERROR: dwarf_sec_group_sizes failed"); |
| 235 |
return res; |
| 236 |
} |
| 237 |
if (group_count == 1 && selected_group == DW_GROUPNUMBER_BASE) { |
| 238 |
/* This is the traditional DWARF without |
| 239 |
dwo sections in a single object. And no COMDAT data. |
| 240 |
We don't want to print anything as we do not want |
| 241 |
to see differences from existing output in this case. */ |
| 242 |
return DW_DLV_OK; |
| 243 |
} |
| 244 |
printf("Section Groups data\n"); |
| 245 |
printf(" Number of Elf-like sections: %4" DW_PR_DUu "\n", |
| 246 |
section_count); |
| 247 |
printf(" Number of groups : %4" DW_PR_DUu "\n", |
| 248 |
group_count); |
| 249 |
printf(" Group to print : %4" DW_PR_DUu "\n", |
| 250 |
selected_group); |
| 251 |
printf(" Count of map entries : %4" DW_PR_DUu "\n", |
| 252 |
group_map_entry_count); |
| 253 |
|
| 254 |
sec_nums = calloc(group_map_entry_count,sizeof(Dwarf_Unsigned)); |
| 255 |
if (!sec_nums) { |
| 256 |
glflags.gf_count_major_errors++; |
| 257 |
printf("ERROR: Unable to allocate %4" DW_PR_DUu |
| 258 |
" map section values, cannot print group map\n", |
| 259 |
group_map_entry_count); |
| 260 |
return DW_DLV_OK; |
| 261 |
} |
| 262 |
group_nums = calloc(group_map_entry_count,sizeof(Dwarf_Unsigned)); |
| 263 |
if (!group_nums) { |
| 264 |
free(group_nums); |
| 265 |
glflags.gf_count_major_errors++; |
| 266 |
printf("ERROR: Unable to allocate %4" DW_PR_DUu |
| 267 |
" map group values, cannot print group map\n", |
| 268 |
group_map_entry_count); |
| 269 |
return DW_DLV_OK; |
| 270 |
} |
| 271 |
sec_names = calloc(group_map_entry_count,sizeof(char*)); |
| 272 |
if (!sec_names) { |
| 273 |
free(group_nums); |
| 274 |
free(sec_nums); |
| 275 |
glflags.gf_count_major_errors++; |
| 276 |
printf("ERROR: Unable to allocate %4" DW_PR_DUu |
| 277 |
" section name pointers, cannot print group map\n", |
| 278 |
group_map_entry_count); |
| 279 |
return DW_DLV_OK; |
| 280 |
} |
| 281 |
|
| 282 |
res = dwarf_sec_group_map(dbg,group_map_entry_count, |
| 283 |
group_nums,sec_nums,sec_names,error); |
| 284 |
if (res != DW_DLV_OK) { |
| 285 |
simple_err_return_msg_either_action(res, |
| 286 |
"ERROR: dwarf_sec_group_map failed"); |
| 287 |
return res; |
| 288 |
} |
| 289 |
|
| 290 |
for ( i = 0; i < group_map_entry_count; ++i) { |
| 291 |
if (i == 0) { |
| 292 |
printf(" [index] group section\n"); |
| 293 |
} |
| 294 |
printf(" [%5" DW_PR_DUu "] " |
| 295 |
"%4" DW_PR_DUu |
| 296 |
" %4" DW_PR_DUu |
| 297 |
" %s\n",i, |
| 298 |
group_nums[i],sec_nums[i],sanitized(sec_names[i])); |
| 299 |
} |
| 300 |
/* Do not free our allocations. Will do later |
| 301 |
using freeall_groups_tables() */ |
| 302 |
return DW_DLV_OK; |
| 303 |
} |