| 1 |
/* |
| 2 |
Copyright (C) 2021 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 <stdlib.h> /* exit() */ |
| 31 |
#include <stdio.h> /* FILE decl for dd_esb.h, printf etc */ |
| 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_common.h" |
| 41 |
#include "dd_esb.h" |
| 42 |
#include "dd_tag_common.h" |
| 43 |
#include "dd_getopt.h" |
| 44 |
#include "dd_tsearchbal.h" |
| 45 |
#include "dd_attr_form.h" |
| 46 |
#include "dd_safe_strcpy.h" |
| 47 |
#include "dd_minimal.h" |
| 48 |
/* works around ref to this via dd_getopt */ |
| 49 |
void dd_minimal_count_global_error(void) {} |
| 50 |
|
| 51 |
Dwarf_Bool ellipsis = FALSE; /* So we can use dwarf_names.c */ |
| 52 |
|
| 53 |
/* Expected input format |
| 54 |
|
| 55 |
0xffffffff |
| 56 |
DW_AT_something (a number as seen here) |
| 57 |
name of a form-class enum entry |
| 58 |
... |
| 59 |
0xffffffff |
| 60 |
DW_AT_something_else (a number as seen here) |
| 61 |
name of a form-class enum entry |
| 62 |
... |
| 63 |
0xffffffff |
| 64 |
... |
| 65 |
|
| 66 |
We generate a text list of numbers as a C header file. |
| 67 |
The array there is used |
| 68 |
by dwarfdump at runtime if attr/form checking |
| 69 |
is requested. |
| 70 |
The file is named dwarfump/dwarfdump-af-table.h and is intended |
| 71 |
to be included in exactly one place in dwarfdump source. |
| 72 |
|
| 73 |
The list is N entries (N is not limited) of |
| 74 |
attribute# formclass# std1/extended2 flag |
| 75 |
followed by a space and the # and then names. |
| 76 |
|
| 77 |
For example: |
| 78 |
{0x02,10, 1}, DW_AT_location, DW_FORM_CLASS_REFERENCE, Std |
| 79 |
with the names in a C comment block (which we do not |
| 80 |
show quite right here). See dwarfdump/dwarfdump-af-table.h |
| 81 |
|
| 82 |
The Standard vs Extended table indication is a rough indication. |
| 83 |
dwarfdump will know what are extension ATtributes and |
| 84 |
extension FORMs by the valuing being at or above |
| 85 |
the formal AT DW_AT_lo_user and the FORM being above 0x2c |
| 86 |
(ie the highest defined by any DWARF standard, lacking |
| 87 |
a DW_FORM_lo_user value) |
| 88 |
|
| 89 |
Lines beggining with a # character are ignored |
| 90 |
by the code in dwarfdump reading this output. |
| 91 |
Any lines commented with C comments are stripped |
| 92 |
by the initial C pre-processor invocation. |
| 93 |
|
| 94 |
*/ |
| 95 |
|
| 96 |
#define AF_STANDARD 1 |
| 97 |
#define AF_EXTENDED 2 |
| 98 |
|
| 99 |
static const char *usage[] = { |
| 100 |
"Usage: attr_form_build <options>", |
| 101 |
" -i input-table-path", |
| 102 |
" -o output-table-path", |
| 103 |
" -s (Generate standard attr-formclass table)", |
| 104 |
" -e (Generate extended attr-formclass table ", |
| 105 |
" common extensions))", |
| 106 |
"" |
| 107 |
}; |
| 108 |
|
| 109 |
const char *program_name = 0; |
| 110 |
char *input_name = 0; |
| 111 |
char *output_name = 0; |
| 112 |
int standard_flag = FALSE; |
| 113 |
int extended_flag = FALSE; |
| 114 |
|
| 115 |
/* process arguments */ |
| 116 |
static void |
| 117 |
process_args(int argc, char *argv[]) |
| 118 |
{ |
| 119 |
int c = 0; |
| 120 |
Dwarf_Bool usage_error = FALSE; |
| 121 |
|
| 122 |
program_name = argv[0]; |
| 123 |
|
| 124 |
while ((c = dwgetopt(argc, argv, "i:o:se")) != EOF) { |
| 125 |
switch (c) { |
| 126 |
case 'i': |
| 127 |
input_name = dwoptarg; |
| 128 |
break; |
| 129 |
case 'o': |
| 130 |
output_name = dwoptarg; |
| 131 |
break; |
| 132 |
case 'e': |
| 133 |
extended_flag = TRUE; |
| 134 |
break; |
| 135 |
case 's': |
| 136 |
standard_flag = TRUE; |
| 137 |
break; |
| 138 |
default: |
| 139 |
usage_error = TRUE; |
| 140 |
break; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
if (usage_error || 1 == dwoptind || dwoptind != argc) { |
| 145 |
print_usage_message(usage); |
| 146 |
exit(EXIT_FAILURE); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
int |
| 151 |
main(int argc, char **argv) |
| 152 |
{ |
| 153 |
unsigned int num = 0; |
| 154 |
int input_eof = 0; |
| 155 |
FILE *fileInp = 0; |
| 156 |
FILE *fileOut = 0; |
| 157 |
unsigned table_type = 0; |
| 158 |
const char *structname = 0; |
| 159 |
const char *macroname = 0; |
| 160 |
|
| 161 |
print_version_details(argv[0]); |
| 162 |
print_args(argc,argv); |
| 163 |
process_args(argc,argv); |
| 164 |
|
| 165 |
if (!input_name ) { |
| 166 |
fprintf(stderr,"Input name required, not supplied.\n"); |
| 167 |
print_usage_message(usage); |
| 168 |
exit(EXIT_FAILURE); |
| 169 |
} |
| 170 |
fileInp = fopen(input_name,"r"); |
| 171 |
if (!fileInp) { |
| 172 |
fprintf(stderr,"Invalid input filename," |
| 173 |
" could not open '%s'\n", |
| 174 |
input_name); |
| 175 |
print_usage_message(usage); |
| 176 |
exit(EXIT_FAILURE); |
| 177 |
} |
| 178 |
|
| 179 |
if (!output_name ) { |
| 180 |
fprintf(stderr,"Output name required, not supplied.\n"); |
| 181 |
print_usage_message(usage); |
| 182 |
exit(EXIT_FAILURE); |
| 183 |
} |
| 184 |
fileOut = fopen(output_name,"w"); |
| 185 |
if (!fileOut) { |
| 186 |
fprintf(stderr,"Invalid output filename," |
| 187 |
" could not open: '%s'\n", |
| 188 |
output_name); |
| 189 |
print_usage_message(usage); |
| 190 |
exit(EXIT_FAILURE); |
| 191 |
} |
| 192 |
if ((standard_flag && extended_flag) || |
| 193 |
(!standard_flag && !extended_flag)) { |
| 194 |
fprintf(stderr,"Invalid table type\n"); |
| 195 |
fprintf(stderr,"Choose -e or -s .\n"); |
| 196 |
print_usage_message(usage); |
| 197 |
exit(EXIT_FAILURE); |
| 198 |
} |
| 199 |
|
| 200 |
input_eof = read_value(&num,fileInp); /* 0xffffffff */ |
| 201 |
if (IS_EOF == input_eof) { |
| 202 |
bad_line_input("Empty input file"); |
| 203 |
} |
| 204 |
if (num != MAGIC_TOKEN_VALUE) { |
| 205 |
bad_line_input("Expected 0xffffffff"); |
| 206 |
} |
| 207 |
if (standard_flag) { |
| 208 |
table_type = AF_STD; |
| 209 |
structname = "dd_threekey_af_table_std"; |
| 210 |
macroname = "DWARFDUMP_AF_TABLE_STD_H"; |
| 211 |
} else { |
| 212 |
table_type = AF_EXTEN; |
| 213 |
structname = "dd_threekey_af_table_ext"; |
| 214 |
macroname = "DWARFDUMP_AF_TABLE_EXT_H"; |
| 215 |
} |
| 216 |
|
| 217 |
fprintf(fileOut,"/* Generated table, do not edit. */\n"); |
| 218 |
fprintf(fileOut,"/* Generated for source version %s */\n", |
| 219 |
PACKAGE_VERSION); |
| 220 |
fprintf(fileOut,"\n"); |
| 221 |
fprintf(fileOut,"%s%s\n", |
| 222 |
"#ifndef ",macroname); |
| 223 |
fprintf(fileOut,"%s%s\n", |
| 224 |
"#define ",macroname); |
| 225 |
fprintf(fileOut,"\n"); |
| 226 |
|
| 227 |
fprintf(fileOut,"%s\n", |
| 228 |
"#ifdef __cplusplus"); |
| 229 |
fprintf(fileOut,"%s\n", "extern \"C\" {"); |
| 230 |
fprintf(fileOut,"%s\n", |
| 231 |
"#endif /* __cplusplus */"); |
| 232 |
fprintf(fileOut,"struct Three_Key_Entry_s %s [] = {\n", |
| 233 |
structname); |
| 234 |
|
| 235 |
while (!feof(stdin)) { |
| 236 |
int res = 0; |
| 237 |
unsigned int attr = 0; |
| 238 |
const char * attrname = 0; |
| 239 |
|
| 240 |
input_eof = read_value(&attr,fileInp); |
| 241 |
if (IS_EOF == input_eof) { |
| 242 |
/* Reached normal eof */ |
| 243 |
break; |
| 244 |
} |
| 245 |
res = dwarf_get_AT_name(attr,&attrname); |
| 246 |
if (res != DW_DLV_OK) { |
| 247 |
printf("Unknown attribute number of 0x%x," |
| 248 |
" Giving up\n",attr); |
| 249 |
exit(EXIT_FAILURE); |
| 250 |
} |
| 251 |
fprintf(fileOut,"/* 0x%04x %s */\n",attr,attrname); |
| 252 |
input_eof = read_value(&num,fileInp); |
| 253 |
if (IS_EOF == input_eof) { |
| 254 |
bad_line_input("Not terminated correctly."); |
| 255 |
} |
| 256 |
|
| 257 |
while (num != MAGIC_TOKEN_VALUE) { |
| 258 |
fprintf(fileOut,"{0x%04x,0x%04x,%u,%d,0,0},\n", |
| 259 |
(Dwarf_Half)attr,(Dwarf_Half)num,(Dwarf_Half)0, |
| 260 |
table_type); |
| 261 |
input_eof = read_value(&num,fileInp); |
| 262 |
if (IS_EOF == input_eof) { |
| 263 |
bad_line_input("Not terminated correctly."); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
fprintf(fileOut,"{0,0,0,0,0,0}};"); |
| 268 |
fprintf(fileOut,"\n/* END FILE */\n"); |
| 269 |
fprintf(fileOut,"%s\n", |
| 270 |
"#ifdef __cplusplus"); |
| 271 |
fprintf(fileOut,"%s\n", "}"); |
| 272 |
fprintf(fileOut,"%s\n", |
| 273 |
"#endif /* __cplusplus */"); |
| 274 |
fprintf(fileOut,"%s%s%s\n", |
| 275 |
"#endif /* ", |
| 276 |
macroname," */"); |
| 277 |
fclose(fileInp); |
| 278 |
fclose(fileOut); |
| 279 |
return (0); |
| 280 |
} |
| 281 |
/* A fake so we can use dwarf_names.c */ |
| 282 |
void print_error (Dwarf_Debug dbg, |
| 283 |
const char * msg, |
| 284 |
int res, |
| 285 |
Dwarf_Error localerr) |
| 286 |
{ |
| 287 |
(void)dbg; |
| 288 |
(void)msg; |
| 289 |
(void)res; |
| 290 |
(void)localerr; |
| 291 |
} |