| 1 |
/* |
| 2 |
Copyright (C) 2006 Silicon Graphics, Inc. All Rights Reserved. |
| 3 |
Portions Copyright 2011-2019 David Anderson. All Rights Reserved. |
| 4 |
Portions Copyright 2012 SN Systems Ltd. All rights reserved. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of version 2 of the GNU General |
| 8 |
Public License as published by the Free Software Foundation. |
| 9 |
|
| 10 |
This program is distributed in the hope that it would be |
| 11 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 12 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 13 |
PURPOSE. |
| 14 |
|
| 15 |
Further, this software is distributed without any warranty |
| 16 |
that it is free of the rightful claim of any third person |
| 17 |
regarding infringement or the like. Any license provided |
| 18 |
herein, whether implied or otherwise, applies only to this |
| 19 |
software file. Patent licenses, if any, provided herein |
| 20 |
do not apply to combinations of this program with other |
| 21 |
software, or any other product whatsoever. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public |
| 24 |
License along with this program; if not, write the Free |
| 25 |
Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
| 26 |
Boston MA 02110-1301, USA. |
| 27 |
|
| 28 |
*/ |
| 29 |
|
| 30 |
#include <config.h> |
| 31 |
|
| 32 |
#include <ctype.h> /* isspace() */ |
| 33 |
#include <stdlib.h> /* exit() free() getenv() realloc() strtoul() */ |
| 34 |
#include <string.h> /* memset() strcmp() strlen() |
| 35 |
strncmp() strrchr()*/ |
| 36 |
#include <stdio.h> /* FILE decl for dd_esb.h */ |
| 37 |
|
| 38 |
/* Windows specific header files */ |
| 39 |
#ifdef _WIN32 |
| 40 |
#ifdef HAVE_STDAFX_H |
| 41 |
#include "stdafx.h" |
| 42 |
#endif /* HAVE_STDAFX_H */ |
| 43 |
#ifndef WIN32_LEAN_AND_MEAN |
| 44 |
#define WIN32_LEAN_AND_MEAN |
| 45 |
#endif /* WIN32_LEAN_AND_MEAN */ |
| 46 |
#include <windows.h> |
| 47 |
#endif /* _WIN32 */ |
| 48 |
|
| 49 |
#include "dwarf.h" |
| 50 |
#include "libdwarf.h" |
| 51 |
#include "libdwarf_private.h" |
| 52 |
#include "dd_defined_types.h" |
| 53 |
#include "dd_checkutil.h" |
| 54 |
#include "dd_glflags.h" |
| 55 |
#include "dd_globals.h" |
| 56 |
#include "dd_dwconf.h" |
| 57 |
#include "dd_canonical_append.h" |
| 58 |
#include "dd_makename.h" |
| 59 |
#include "dd_sanitized.h" |
| 60 |
#include "dd_esb.h" |
| 61 |
#include "dd_safe_strcpy.h" |
| 62 |
|
| 63 |
/* The nesting level is arbitrary, 2 should suffice. |
| 64 |
But at least this prevents an infinite loop. |
| 65 |
*/ |
| 66 |
#define MAX_NEST_LEVEL 3 |
| 67 |
|
| 68 |
struct token_s { |
| 69 |
unsigned tk_len; |
| 70 |
char *tk_data; |
| 71 |
}; |
| 72 |
enum linetype_e { |
| 73 |
LT_ERROR, |
| 74 |
LT_COMMENT, |
| 75 |
LT_BLANK, |
| 76 |
LT_BEGINABI, |
| 77 |
LT_REG, |
| 78 |
LT_FRAME_INTERFACE, |
| 79 |
LT_CFA_REG, |
| 80 |
LT_INITIAL_REG_VALUE, |
| 81 |
LT_SAME_VAL_REG, |
| 82 |
LT_UNDEFINED_VAL_REG, |
| 83 |
LT_REG_TABLE_SIZE, |
| 84 |
LT_ADDRESS_SIZE, |
| 85 |
LT_INCLUDEABI, |
| 86 |
LT_ENDABI, |
| 87 |
LT_OPTION |
| 88 |
}; |
| 89 |
|
| 90 |
struct comtable_s { |
| 91 |
enum linetype_e type; |
| 92 |
char *name; |
| 93 |
size_t namelen; |
| 94 |
}; |
| 95 |
|
| 96 |
static int errcount = 0; /* Count errors found in this scan of |
| 97 |
the configuration file. */ |
| 98 |
|
| 99 |
static char name_begin_abi[] = "beginabi:"; |
| 100 |
static char name_reg[] = "reg:"; |
| 101 |
static char name_frame_interface[] = "frame_interface:"; |
| 102 |
static char name_cfa_reg[] = "cfa_reg:"; |
| 103 |
static char name_initial_reg_value[] = "initial_reg_value:"; |
| 104 |
static char name_same_val_reg[] = "same_val_reg:"; |
| 105 |
static char name_undefined_val_reg[] = "undefined_val_reg:"; |
| 106 |
static char name_reg_table_size[] = "reg_table_size:"; |
| 107 |
static char name_address_size[] = "address_size:"; |
| 108 |
static char name_includeabi[] = "includeabi:"; |
| 109 |
static char name_endabi[] = "endabi:"; |
| 110 |
static char name_option[] = "option:"; |
| 111 |
|
| 112 |
/* The namelen field is filled in at runtime with |
| 113 |
the correct value. Filling in a fake '1' avoids |
| 114 |
a compiler warning. */ |
| 115 |
static struct comtable_s comtable[] = { |
| 116 |
{LT_BEGINABI, name_begin_abi,1}, |
| 117 |
{LT_REG, name_reg,1}, |
| 118 |
{LT_FRAME_INTERFACE, name_frame_interface,1}, |
| 119 |
{LT_CFA_REG, name_cfa_reg,1}, |
| 120 |
{LT_INITIAL_REG_VALUE, name_initial_reg_value,1}, |
| 121 |
{LT_SAME_VAL_REG, name_same_val_reg,1}, |
| 122 |
{LT_UNDEFINED_VAL_REG, name_undefined_val_reg,1}, |
| 123 |
{LT_REG_TABLE_SIZE, name_reg_table_size,1}, |
| 124 |
{LT_ADDRESS_SIZE, name_address_size,1}, |
| 125 |
{LT_INCLUDEABI, name_includeabi,1}, |
| 126 |
{LT_ENDABI, name_endabi,1}, |
| 127 |
{LT_OPTION, name_option,1}, |
| 128 |
}; |
| 129 |
|
| 130 |
struct conf_internal_s { |
| 131 |
unsigned long beginabi_lineno; |
| 132 |
unsigned long frame_interface_lineno; |
| 133 |
unsigned long initial_reg_value_lineno; |
| 134 |
unsigned long reg_table_size_lineno; |
| 135 |
unsigned long address_size_lineno; |
| 136 |
unsigned long same_val_reg_lineno; |
| 137 |
unsigned long undefined_val_reg_lineno; |
| 138 |
unsigned long cfa_reg_lineno; |
| 139 |
unsigned long regcount; |
| 140 |
struct dwconf_s * conf_out; |
| 141 |
const char * conf_name_used; |
| 142 |
char ** conf_defaults; |
| 143 |
}; |
| 144 |
static void |
| 145 |
init_conf_internal(struct conf_internal_s *s, |
| 146 |
struct dwconf_s * conf_out) |
| 147 |
{ |
| 148 |
s->beginabi_lineno = 0; |
| 149 |
s->frame_interface_lineno = 0; |
| 150 |
s->initial_reg_value_lineno = 0; |
| 151 |
s->reg_table_size_lineno = 0; |
| 152 |
s->address_size_lineno = 0; |
| 153 |
s->same_val_reg_lineno = 0; |
| 154 |
s->undefined_val_reg_lineno = 0; |
| 155 |
s->cfa_reg_lineno = 0; |
| 156 |
s->cfa_reg_lineno = 0; |
| 157 |
s->conf_name_used = 0; |
| 158 |
s->conf_defaults = 0; |
| 159 |
s->regcount = 0; |
| 160 |
s->conf_out = conf_out; |
| 161 |
} |
| 162 |
|
| 163 |
static unsigned size_of_comtable = sizeof(comtable) / |
| 164 |
sizeof(comtable[0]); |
| 165 |
|
| 166 |
static FILE *find_a_file(const char *named_file, char **defaults, |
| 167 |
const char** name_used); |
| 168 |
static int find_abi_start(FILE * stream, const char *abi_name, |
| 169 |
long *offset, |
| 170 |
unsigned long *lineno_out); |
| 171 |
static int parse_abi(FILE * stream, const char *fname, |
| 172 |
const char *abiname, |
| 173 |
struct conf_internal_s *out, unsigned long lineno, |
| 174 |
unsigned nest_level); |
| 175 |
static char *get_token(char *cp, struct token_s *outtok); |
| 176 |
|
| 177 |
/* We insist that the dwarfdump.conf file |
| 178 |
contain ASCII only and reject if non-ascii |
| 179 |
characters are found. */ |
| 180 |
static int |
| 181 |
is_this_text_file(FILE *stream) |
| 182 |
{ |
| 183 |
unsigned char buf[512]; |
| 184 |
size_t bytesread = 0; |
| 185 |
unsigned long i = 0; |
| 186 |
unsigned long curlinelen = 0; |
| 187 |
unsigned long int maxlinelen = 0; |
| 188 |
|
| 189 |
bytesread = fread(buf,1,sizeof(buf),stream); |
| 190 |
if (bytesread != sizeof(buf)) { |
| 191 |
if (bytesread < 100) { |
| 192 |
printf("ERROR: Found the configure file is too small to " |
| 193 |
"be reasonable. Add a few comment lines " |
| 194 |
"to enlarge it from %lu bytes to 100\n", |
| 195 |
(unsigned long)bytesread); |
| 196 |
glflags.gf_count_major_errors++; |
| 197 |
return FALSE; |
| 198 |
} |
| 199 |
} |
| 200 |
for ( i = 0; i < bytesread; ++i,++curlinelen) { |
| 201 |
unsigned char c = buf[i]; |
| 202 |
|
| 203 |
if (c == '\n') { |
| 204 |
if (curlinelen > maxlinelen) { |
| 205 |
maxlinelen = curlinelen; |
| 206 |
} |
| 207 |
curlinelen = 0; |
| 208 |
continue; |
| 209 |
} |
| 210 |
if (c == '\r' || c == '\t') { |
| 211 |
continue; |
| 212 |
} |
| 213 |
if (c >= ' ' && c <= '}') { |
| 214 |
continue; |
| 215 |
} else { |
| 216 |
printf("Found non-ascii character 0x%02x " |
| 217 |
"and only ascii allowed in dwarfdump.conf\n", |
| 218 |
c); |
| 219 |
return FALSE; |
| 220 |
} |
| 221 |
} |
| 222 |
if (maxlinelen > 100) { |
| 223 |
printf("ERROR: Found maximum configure file" |
| 224 |
" line length of %lu" |
| 225 |
" which seems too long to be reasonable\n", |
| 226 |
maxlinelen); |
| 227 |
glflags.gf_count_major_errors++; |
| 228 |
return FALSE; |
| 229 |
} |
| 230 |
return TRUE; |
| 231 |
} |
| 232 |
|
| 233 |
/* This finds a dwarfdump.conf file and |
| 234 |
then parses it. It updates |
| 235 |
conf_out as appropriate. |
| 236 |
|
| 237 |
This finds the first file (looking in a set of places) |
| 238 |
with that name. It then looks for the right ABI entry. |
| 239 |
If the first file it finds does not have that ABI entry it |
| 240 |
gives up. |
| 241 |
|
| 242 |
It would also be reasonable to search every |
| 243 |
'dwarfdump.conf' |
| 244 |
it finds for the abi. |
| 245 |
But we stop at the first dwarfdump.conf |
| 246 |
we find. |
| 247 |
This is the internal call to get the |
| 248 |
conf data to implement |
| 249 |
a crude 'includeabi' feature. |
| 250 |
|
| 251 |
Each call here starts at offset 0 of |
| 252 |
a new stream. |
| 253 |
|
| 254 |
Returns 0 if no errors found, else returns > 0. |
| 255 |
*/ |
| 256 |
static int |
| 257 |
find_conf_file_and_read_config_inner(const char *named_file, |
| 258 |
const char *named_abi, |
| 259 |
struct conf_internal_s *conf_internal, |
| 260 |
unsigned nest_level) |
| 261 |
{ |
| 262 |
|
| 263 |
FILE *conf_stream = 0; |
| 264 |
const char *name_used = 0; |
| 265 |
long offset = 0; |
| 266 |
int res = FALSE; |
| 267 |
unsigned long lineno = 0; |
| 268 |
|
| 269 |
errcount = 0; |
| 270 |
|
| 271 |
conf_stream = find_a_file(named_file, |
| 272 |
conf_internal->conf_defaults, |
| 273 |
&name_used); |
| 274 |
if (!conf_stream) { |
| 275 |
++errcount; |
| 276 |
if (!named_file || !strlen(named_file)) { |
| 277 |
printf("ERROR: dwarfdump found no dwarfdump.conf file " |
| 278 |
"in any of the standard places\n" |
| 279 |
"(precede all arguments " |
| 280 |
"with option --show-dwarfdump-conf to see " |
| 281 |
"what file paths tried)\n" |
| 282 |
"If no print/check arguments also provided " |
| 283 |
"dwarfdump may silently just stop.\n"); |
| 284 |
glflags.gf_count_major_errors++; |
| 285 |
} else { |
| 286 |
printf("ERROR: dwarfdump found no dwarfdump.conf " |
| 287 |
"file \"%s\"\n" |
| 288 |
"If no print/check arguments also provided " |
| 289 |
"dwarfdump may silently just stop.\n", |
| 290 |
sanitized(named_file)); |
| 291 |
glflags.gf_count_major_errors++; |
| 292 |
} |
| 293 |
return FOUND_ERROR; |
| 294 |
} |
| 295 |
if (glflags.gf_show_dwarfdump_conf) { |
| 296 |
printf("dwarfdump using configuration " |
| 297 |
"file \"%s\"\n", sanitized(name_used)); |
| 298 |
} |
| 299 |
conf_internal->conf_name_used = name_used; |
| 300 |
|
| 301 |
res = is_this_text_file(conf_stream); |
| 302 |
if (res == FALSE) { |
| 303 |
printf("ERROR: dwarfdump configuration " |
| 304 |
"file \"%s\" is not plain text. Error.\n", |
| 305 |
sanitized(name_used)); |
| 306 |
glflags.gf_count_major_errors++; |
| 307 |
fclose(conf_stream); |
| 308 |
return FOUND_ERROR; |
| 309 |
} |
| 310 |
res = fseek(conf_stream,0,SEEK_SET); |
| 311 |
if (res != 0) { |
| 312 |
printf("ERROR: dwarfdump configuration " |
| 313 |
"file \"%s\" fseek to 0 failed. Error\n", |
| 314 |
sanitized(name_used)); |
| 315 |
glflags.gf_count_major_errors++; |
| 316 |
fclose(conf_stream); |
| 317 |
return FOUND_ERROR; |
| 318 |
} |
| 319 |
/* And option: lines must come before any abi data. */ |
| 320 |
res = find_abi_start(conf_stream, named_abi, &offset, &lineno); |
| 321 |
if (res == FOUND_DONE ||res == FOUND_OPTION) { |
| 322 |
/* nothing more to do */ |
| 323 |
fclose(conf_stream); |
| 324 |
return FOUND_DONE; |
| 325 |
} |
| 326 |
if (res == FOUND_ERROR) { |
| 327 |
++errcount; |
| 328 |
fclose(conf_stream); |
| 329 |
printf("ERROR: dwarfdump found no usable abi \"%s\" " |
| 330 |
"in file \"%s\".\n", |
| 331 |
named_abi?named_abi:"<not looking for abi>", |
| 332 |
name_used); |
| 333 |
glflags.gf_count_major_errors++; |
| 334 |
return FOUND_ERROR; |
| 335 |
} |
| 336 |
/* res == FOUND_ABI_START */ |
| 337 |
res = fseek(conf_stream, offset, SEEK_SET); |
| 338 |
if (res != 0) { |
| 339 |
++errcount; |
| 340 |
fclose(conf_stream); |
| 341 |
printf("ERROR: dwarfdump seek to %ld offset in %s failed!\n", |
| 342 |
offset, name_used); |
| 343 |
glflags.gf_count_major_errors++; |
| 344 |
return FOUND_ERROR; |
| 345 |
} |
| 346 |
parse_abi(conf_stream, name_used, named_abi, |
| 347 |
conf_internal, lineno, |
| 348 |
nest_level); |
| 349 |
fclose(conf_stream); |
| 350 |
if (errcount) { |
| 351 |
return FOUND_ERROR; |
| 352 |
} |
| 353 |
return FOUND_ABI_START; |
| 354 |
} |
| 355 |
|
| 356 |
/* This is the external-facing call to get the conf data. */ |
| 357 |
int |
| 358 |
find_conf_file_and_read_config(const char *named_file, |
| 359 |
const char *named_abi, char **defaults, |
| 360 |
struct dwconf_s *conf_out) |
| 361 |
{ |
| 362 |
int res = 0; |
| 363 |
struct conf_internal_s conf_internal; |
| 364 |
init_conf_file_data(conf_out); |
| 365 |
init_conf_internal(&conf_internal,conf_out); |
| 366 |
conf_internal.conf_defaults = defaults; |
| 367 |
res = find_conf_file_and_read_config_inner(named_file, |
| 368 |
named_abi, |
| 369 |
&conf_internal,0); |
| 370 |
/* Nothing in conf_internal need be freed */ |
| 371 |
return res; |
| 372 |
} |
| 373 |
|
| 374 |
/* Try to find a file as named and open for read. |
| 375 |
We treat each name as a full name, we are not |
| 376 |
combining separate name and path components. |
| 377 |
This is an arbitrary choice... |
| 378 |
|
| 379 |
The defaults are listed in command_options.c in the array |
| 380 |
config_file_defaults[]. |
| 381 |
|
| 382 |
Since named_file comes from an esb_get_string, lname |
| 383 |
may be non-null but pointing to empty string to mean |
| 384 |
no-name. |
| 385 |
*/ |
| 386 |
static FILE * |
| 387 |
find_a_file(const char *named_file, |
| 388 |
char **defaults, |
| 389 |
const char ** name_used) |
| 390 |
{ |
| 391 |
FILE *fin = 0; |
| 392 |
const char *lname = named_file; |
| 393 |
const char *type = "r"; |
| 394 |
int i = 0; |
| 395 |
|
| 396 |
if (lname && (strlen(lname) > 0)) { |
| 397 |
/* Name given, just assume it is fully correct, |
| 398 |
try no other. */ |
| 399 |
printf("dwarfdump looking for" |
| 400 |
" configuration as \"%s\"\n", lname); |
| 401 |
fin = fopen(lname, type); |
| 402 |
if (fin) { |
| 403 |
*name_used = makename(lname); |
| 404 |
return fin; |
| 405 |
} |
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
#ifdef _WIN32 |
| 410 |
/* Open the configuration file, located |
| 411 |
in the directory where the tool is loaded from */ |
| 412 |
{ |
| 413 |
static char szPath[MAX_PATH]; |
| 414 |
if (GetModuleFileName(NULL,szPath,MAX_PATH)) { |
| 415 |
char *pDir = strrchr(szPath,'/'); |
| 416 |
size_t len = 0; |
| 417 |
if (!pDir) { |
| 418 |
pDir = strrchr(szPath,'\\'); |
| 419 |
if (!pDir) { |
| 420 |
/* No file was found */ |
| 421 |
goto try_unix_path; |
| 422 |
} |
| 423 |
} |
| 424 |
/* Add the configuration name to the pathname */ |
| 425 |
++pDir; |
| 426 |
len = pDir - szPath; |
| 427 |
dd_safe_strcpy(pDir,sizeof(szPath)-len, |
| 428 |
"dwarfdump.conf",14); |
| 429 |
lname = szPath; |
| 430 |
|
| 431 |
if (!lname || !strlen(lname)) { |
| 432 |
lname="<Impossible file name string>"; |
| 433 |
} |
| 434 |
if (glflags.gf_show_dwarfdump_conf) { |
| 435 |
printf("dwarfdump looking for" |
| 436 |
" configuration as: \"%s\"\n", lname); |
| 437 |
} |
| 438 |
fin = fopen(lname, type); |
| 439 |
if (fin) { |
| 440 |
*name_used = makename(lname); |
| 441 |
return fin; |
| 442 |
} |
| 443 |
/* No file was found */ |
| 444 |
goto try_unix_path; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
try_unix_path: |
| 449 |
#endif |
| 450 |
|
| 451 |
/* No name given, find a default, if we can. */ |
| 452 |
for (i = 0; defaults[i]; ++i) { |
| 453 |
char buf[2000]; |
| 454 |
|
| 455 |
buf[0] = 0; |
| 456 |
lname = defaults[i]; |
| 457 |
if (strncmp(lname, "HOME/", 5) == 0) { |
| 458 |
/* arbitrary size */ |
| 459 |
char *homedir = getenv("HOME"); |
| 460 |
if (homedir) { |
| 461 |
char *cp = _dwarf_canonical_append(buf, |
| 462 |
sizeof(buf), homedir, lname + 5); |
| 463 |
if (!cp) { |
| 464 |
/* OOps, ignore this one. */ |
| 465 |
continue; |
| 466 |
} |
| 467 |
lname = buf; |
| 468 |
} |
| 469 |
} |
| 470 |
if (glflags.gf_show_dwarfdump_conf) { |
| 471 |
if (!lname || !strlen(lname)) { |
| 472 |
lname="<Impossible name string>"; |
| 473 |
} |
| 474 |
printf("dwarfdump looking for" |
| 475 |
" configuration as: \"%s\"\n", lname); |
| 476 |
} |
| 477 |
fin = fopen(lname, type); |
| 478 |
if (fin) { |
| 479 |
*name_used = makename(lname); |
| 480 |
return fin; |
| 481 |
} |
| 482 |
} |
| 483 |
return 0; |
| 484 |
} |
| 485 |
|
| 486 |
/* Start at a token begin, see how long it is, |
| 487 |
return length. */ |
| 488 |
static unsigned |
| 489 |
find_token_len(char *cp) |
| 490 |
{ |
| 491 |
unsigned len = 0; |
| 492 |
|
| 493 |
for (; *cp; ++cp) { |
| 494 |
if (isspace(*cp)) { |
| 495 |
return len; |
| 496 |
} |
| 497 |
if (*cp == '#') { |
| 498 |
return len; /* begins comment */ |
| 499 |
} |
| 500 |
++len; |
| 501 |
} |
| 502 |
return len; |
| 503 |
} |
| 504 |
|
| 505 |
/* |
| 506 |
Skip past all whitespace: the only code that even knows |
| 507 |
what whitespace is. |
| 508 |
*/ |
| 509 |
static char * |
| 510 |
skipwhite(char *cp) |
| 511 |
{ |
| 512 |
for (; *cp; ++cp) { |
| 513 |
if (!isspace(*cp)) { |
| 514 |
return cp; |
| 515 |
} |
| 516 |
} |
| 517 |
return cp; |
| 518 |
} |
| 519 |
|
| 520 |
/* Return TRUE if ok. FALSE if find more tokens. |
| 521 |
Emit error message if error. |
| 522 |
*/ |
| 523 |
static int |
| 524 |
ensure_has_no_more_tokens(char *cp, |
| 525 |
const char *fname, |
| 526 |
unsigned long lineno) |
| 527 |
{ |
| 528 |
struct token_s tok; |
| 529 |
|
| 530 |
get_token(cp, &tok); |
| 531 |
if (tok.tk_len > 0) { |
| 532 |
printf("dwarfdump.conf error: " |
| 533 |
"extra characters after command operands, found " |
| 534 |
"\"%s\" in %s line %lu\n", tok.tk_data, fname, lineno); |
| 535 |
++errcount; |
| 536 |
return FALSE; |
| 537 |
} |
| 538 |
return TRUE; |
| 539 |
} |
| 540 |
|
| 541 |
/* There may be many beginabi: lines in a dwarfdump.conf file, |
| 542 |
find the one we want and return its file offset. |
| 543 |
*/ |
| 544 |
static int |
| 545 |
find_abi_start(FILE * stream, const char *abi_name, |
| 546 |
long *offset, unsigned long *lineno_out) |
| 547 |
{ |
| 548 |
char buf[100]; |
| 549 |
unsigned long lineno = 0; |
| 550 |
|
| 551 |
for (; !feof(stream);) { |
| 552 |
struct token_s tok; |
| 553 |
char *line = 0; |
| 554 |
long loffset = ftell(stream); |
| 555 |
|
| 556 |
line = fgets(buf, sizeof(buf), stream); |
| 557 |
++lineno; |
| 558 |
if (!line) { |
| 559 |
if (!abi_name) { |
| 560 |
return FOUND_DONE; |
| 561 |
} |
| 562 |
++errcount; |
| 563 |
return FOUND_ERROR; |
| 564 |
} |
| 565 |
line = get_token(buf, &tok); |
| 566 |
if (!strcmp(tok.tk_data, "option:")) { |
| 567 |
get_token(line, &tok); |
| 568 |
if (tok.tk_data && !strcmp(tok.tk_data, |
| 569 |
"--format-expr-ops-joined")) { |
| 570 |
/* print expr ops joined onto one line: |
| 571 |
matching historical behavior. */ |
| 572 |
glflags.gf_expr_ops_joined = TRUE; |
| 573 |
} else { |
| 574 |
printf("ERROR: option command %s is not understood" |
| 575 |
" giving up\n",tok.tk_data); |
| 576 |
++errcount; |
| 577 |
return FOUND_ERROR; |
| 578 |
} |
| 579 |
continue; |
| 580 |
} |
| 581 |
if (strcmp(tok.tk_data, "beginabi:")) { |
| 582 |
continue; |
| 583 |
} |
| 584 |
/* Is beginabi: */ |
| 585 |
if (!abi_name) { |
| 586 |
continue; |
| 587 |
} |
| 588 |
get_token(line, &tok); |
| 589 |
if (strcmp(tok.tk_data, abi_name)) { |
| 590 |
continue; |
| 591 |
} |
| 592 |
*offset = loffset; |
| 593 |
*lineno_out = lineno; |
| 594 |
return FOUND_ABI_START; |
| 595 |
} |
| 596 |
if (!abi_name) { |
| 597 |
/* All is ok */ |
| 598 |
return FOUND_DONE; |
| 599 |
} |
| 600 |
++errcount; |
| 601 |
return FALSE; |
| 602 |
} |
| 603 |
|
| 604 |
/* Turn a non-delimited |
| 605 |
input char array into a NUL-terminated C string |
| 606 |
(with the help of makename() to get a permanent |
| 607 |
address for the result ing string). |
| 608 |
*/ |
| 609 |
static char * |
| 610 |
build_string(unsigned tlen, char *cp) |
| 611 |
{ |
| 612 |
struct esb_s x; |
| 613 |
char buffer[32]; |
| 614 |
char *ret = 0; |
| 615 |
|
| 616 |
esb_constructor_fixed(&x,buffer,sizeof(buffer)); |
| 617 |
esb_appendn(&x,cp,tlen); |
| 618 |
ret = makename(esb_get_string(&x)); |
| 619 |
esb_destructor(&x); |
| 620 |
return ret; |
| 621 |
} |
| 622 |
|
| 623 |
/* The tokenizer for our simple parser. |
| 624 |
*/ |
| 625 |
static char * |
| 626 |
get_token(char *cp, struct token_s *outtok) |
| 627 |
{ |
| 628 |
char *lcp = skipwhite(cp); |
| 629 |
unsigned tlen = find_token_len(lcp); |
| 630 |
static int outofmem = FALSE; |
| 631 |
|
| 632 |
outtok->tk_len = tlen; |
| 633 |
if (tlen > 0) { |
| 634 |
char *src = build_string(tlen, lcp); |
| 635 |
if (!src) { |
| 636 |
if (!outofmem) { |
| 637 |
printf("Dwarfdump out of memory reading " |
| 638 |
"dwarfdump.conf and will likely not work.\n"); |
| 639 |
} |
| 640 |
outofmem = TRUE; |
| 641 |
return ""; |
| 642 |
} |
| 643 |
outtok->tk_data = build_string(tlen, lcp); |
| 644 |
} else { |
| 645 |
outtok->tk_data = ""; |
| 646 |
} |
| 647 |
return lcp + tlen; |
| 648 |
|
| 649 |
} |
| 650 |
|
| 651 |
/* |
| 652 |
We can't get all the field set up statically very easily, |
| 653 |
so we get the command string length set here. |
| 654 |
*/ |
| 655 |
static void |
| 656 |
finish_comtable_setup(void) |
| 657 |
{ |
| 658 |
unsigned i; |
| 659 |
|
| 660 |
for (i = 0; i < size_of_comtable; ++i) { |
| 661 |
comtable[i].namelen = strlen(comtable[i].name); |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
/* |
| 666 |
Given a line of the table, determine if it is a command |
| 667 |
or not, and if a command, which one is it. |
| 668 |
Return LT_ERROR if it's not recognized. |
| 669 |
*/ |
| 670 |
static enum linetype_e |
| 671 |
which_command(char *cp, struct comtable_s **tableentry) |
| 672 |
{ |
| 673 |
unsigned i = 0; |
| 674 |
struct token_s tok; |
| 675 |
|
| 676 |
if (*cp == '#') |
| 677 |
return LT_COMMENT; |
| 678 |
if (!*cp) |
| 679 |
return LT_BLANK; |
| 680 |
|
| 681 |
get_token(cp, &tok); |
| 682 |
|
| 683 |
for (i = 0; i < size_of_comtable; ++i) { |
| 684 |
if (tok.tk_len == comtable[i].namelen && |
| 685 |
strcmp(comtable[i].name, tok.tk_data) == 0) { |
| 686 |
|
| 687 |
*tableentry = &comtable[i]; |
| 688 |
return comtable[i].type; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
return LT_ERROR; |
| 693 |
} |
| 694 |
|
| 695 |
static int |
| 696 |
parseoption(char *cp, const char *fname,unsigned long lineno, |
| 697 |
struct comtable_s *comtab) |
| 698 |
{ |
| 699 |
size_t clen = comtab->namelen; |
| 700 |
struct token_s tok; |
| 701 |
|
| 702 |
cp = cp + clen + 1; |
| 703 |
cp = skipwhite(cp); |
| 704 |
get_token(cp, &tok); |
| 705 |
if (!tok.tk_data) { |
| 706 |
printf("ERROR: empty option: command is ignored"); |
| 707 |
return FALSE; |
| 708 |
} |
| 709 |
ensure_has_no_more_tokens(cp + tok.tk_len, fname, lineno); |
| 710 |
if (!strcmp(tok.tk_data,"--format-expr-ops-joined")) { |
| 711 |
glflags.gf_expr_ops_joined = TRUE; |
| 712 |
} else { |
| 713 |
printf("ERROR: option command %s is not understood" |
| 714 |
" and is ignored",tok.tk_data); |
| 715 |
return FALSE; |
| 716 |
} |
| 717 |
return TRUE; |
| 718 |
|
| 719 |
} |
| 720 |
/* We are promised it's an abiname: command |
| 721 |
find the name on the line. |
| 722 |
*/ |
| 723 |
static int |
| 724 |
parsebeginabi(char *cp, const char *fname, const char *abiname, |
| 725 |
unsigned long lineno, struct comtable_s *comtab) |
| 726 |
{ |
| 727 |
size_t clen = comtab->namelen; |
| 728 |
size_t abinamelen = strlen(abiname); |
| 729 |
struct token_s tok; |
| 730 |
|
| 731 |
cp = cp + clen + 1; |
| 732 |
cp = skipwhite(cp); |
| 733 |
get_token(cp, &tok); |
| 734 |
if (tok.tk_len != abinamelen || |
| 735 |
strncmp(cp, abiname, abinamelen)) { |
| 736 |
printf("dwarfdump internal error: " |
| 737 |
"mismatch \"%s\" with \"%s\" \"%s\" line %lu\n", |
| 738 |
cp, tok.tk_data, fname, lineno); |
| 739 |
++errcount; |
| 740 |
return FALSE; |
| 741 |
} |
| 742 |
ensure_has_no_more_tokens(cp + tok.tk_len, fname, lineno); |
| 743 |
return TRUE; |
| 744 |
} |
| 745 |
|
| 746 |
/* This expands register names as required, but does not |
| 747 |
ensure no names duplicated. |
| 748 |
*/ |
| 749 |
#define CONF_TABLE_OVERSIZE 100 |
| 750 |
static void |
| 751 |
add_to_reg_table(struct dwconf_s *conf, |
| 752 |
char *rname, unsigned long rval, const char *fname, |
| 753 |
unsigned long lineno) |
| 754 |
{ |
| 755 |
if (conf->cf_regs_malloced == 0) { |
| 756 |
conf->cf_regs = 0; |
| 757 |
conf->cf_named_regs_table_size = 0; |
| 758 |
} |
| 759 |
if (rval >= conf->cf_named_regs_table_size) { |
| 760 |
char **newregs = 0; |
| 761 |
unsigned long newtablen = rval + CONF_TABLE_OVERSIZE; |
| 762 |
unsigned long newtabsize = newtablen * sizeof(char *); |
| 763 |
unsigned long oldtabsize = |
| 764 |
conf->cf_named_regs_table_size * sizeof(char *); |
| 765 |
newregs = realloc(conf->cf_regs, newtabsize); |
| 766 |
if (!newregs) { |
| 767 |
printf("dwarfdump: unable to malloc table %lu bytes. " |
| 768 |
" %s line %lu\n", newtabsize, fname, lineno); |
| 769 |
exit(EXIT_FAILURE); |
| 770 |
} |
| 771 |
/* Zero out the new entries. */ |
| 772 |
memset((char *) newregs + (oldtabsize), 0, |
| 773 |
(newtabsize - oldtabsize)); |
| 774 |
conf->cf_named_regs_table_size = (unsigned long) newtablen; |
| 775 |
conf->cf_regs = newregs; |
| 776 |
conf->cf_regs_malloced = 1; |
| 777 |
} |
| 778 |
conf->cf_regs[rval] = rname; |
| 779 |
return; |
| 780 |
} |
| 781 |
|
| 782 |
/* Our input is supposed to be a number. |
| 783 |
Determine the value (and return it) or generate an error message. |
| 784 |
*/ |
| 785 |
static int |
| 786 |
make_a_number(char *cmd, const char *filename, unsigned long |
| 787 |
lineno, struct token_s *tok, unsigned long *val_out) |
| 788 |
{ |
| 789 |
char *endnum = 0; |
| 790 |
unsigned long val = 0; |
| 791 |
|
| 792 |
val = strtoul(tok->tk_data, &endnum, 0); |
| 793 |
if (val == 0 && endnum == (tok->tk_data)) { |
| 794 |
printf("dwarfdump.conf error: " |
| 795 |
"%s missing register number (\"%s\" " |
| 796 |
"not valid) %s line %lu\n", |
| 797 |
cmd, tok->tk_data, filename, lineno); |
| 798 |
++errcount; |
| 799 |
return FALSE; |
| 800 |
} |
| 801 |
if (endnum != (tok->tk_data + tok->tk_len)) { |
| 802 |
printf("dwarfdump.conf error: " |
| 803 |
"%s Missing register number (\"%s\" " |
| 804 |
"not valid) %s line %lu\n", |
| 805 |
cmd, tok->tk_data, filename, lineno); |
| 806 |
++errcount; |
| 807 |
return FALSE; |
| 808 |
} |
| 809 |
*val_out = val; |
| 810 |
return TRUE; |
| 811 |
|
| 812 |
} |
| 813 |
|
| 814 |
/* We are guaranteed it's a reg: command, so parse that command |
| 815 |
and record the interesting data. |
| 816 |
*/ |
| 817 |
static int |
| 818 |
parsereg(char *cp, const char *fname, unsigned long lineno, |
| 819 |
struct conf_internal_s *conf, struct comtable_s *comtab) |
| 820 |
{ |
| 821 |
size_t clen = comtab->namelen; |
| 822 |
struct token_s regnum; |
| 823 |
struct token_s tokreg; |
| 824 |
unsigned long val = 0; |
| 825 |
int ok = FALSE; |
| 826 |
int res = FALSE; |
| 827 |
|
| 828 |
cp = cp + clen + 1; |
| 829 |
cp = get_token(cp, &tokreg); |
| 830 |
cp = get_token(cp, ®num); |
| 831 |
if (tokreg.tk_len == 0) { |
| 832 |
printf("dwarfdump.conf error: " |
| 833 |
"reg: missing register name %s line %lu", |
| 834 |
fname, lineno); |
| 835 |
++errcount; |
| 836 |
return FALSE; |
| 837 |
|
| 838 |
} |
| 839 |
if (regnum.tk_len == 0) { |
| 840 |
printf("dwarfdump.conf error: " |
| 841 |
"reg: missing register number %s line %lu", |
| 842 |
fname, lineno); |
| 843 |
++errcount; |
| 844 |
return FALSE; |
| 845 |
} |
| 846 |
|
| 847 |
ok = make_a_number(comtab->name, fname, lineno, ®num, &val); |
| 848 |
|
| 849 |
if (!ok) { |
| 850 |
++errcount; |
| 851 |
return FALSE; |
| 852 |
} |
| 853 |
|
| 854 |
add_to_reg_table(conf->conf_out, tokreg.tk_data, |
| 855 |
val, fname, lineno); |
| 856 |
|
| 857 |
res = ensure_has_no_more_tokens(cp, fname, lineno); |
| 858 |
return res; |
| 859 |
} |
| 860 |
|
| 861 |
/* |
| 862 |
We are guaranteed it's an frame_interface: command. |
| 863 |
Parse it and record the value data. |
| 864 |
*/ |
| 865 |
static int |
| 866 |
parseframe_interface(char *cp, |
| 867 |
const char *fname, |
| 868 |
unsigned long lineno, |
| 869 |
struct conf_internal_s *conf, |
| 870 |
struct comtable_s *comtab) |
| 871 |
{ |
| 872 |
size_t clen = comtab->namelen; |
| 873 |
struct token_s tok; |
| 874 |
unsigned long val = 0; |
| 875 |
int ok = FALSE; |
| 876 |
int res = FALSE; |
| 877 |
|
| 878 |
cp = cp + clen + 1; |
| 879 |
cp = get_token(cp, &tok); |
| 880 |
if (tok.tk_len == 0) { |
| 881 |
printf("dwarfdump.conf error: " |
| 882 |
"%s missing interface number %s line %lu", |
| 883 |
comtab->name, fname, lineno); |
| 884 |
++errcount; |
| 885 |
return FALSE; |
| 886 |
} |
| 887 |
|
| 888 |
ok = make_a_number(comtab->name, fname, lineno, &tok, &val); |
| 889 |
|
| 890 |
if (!ok) { |
| 891 |
++errcount; |
| 892 |
return FALSE; |
| 893 |
} |
| 894 |
if (val != 2 && val != 3) { |
| 895 |
printf("dwarfdump.conf error: " |
| 896 |
"%s only interface numbers 2 or 3 are allowed, " |
| 897 |
" not %lu. %s line %lu", |
| 898 |
comtab->name, val, fname, lineno); |
| 899 |
++errcount; |
| 900 |
return FALSE; |
| 901 |
} |
| 902 |
|
| 903 |
conf->conf_out->cf_interface_number = (int) val; |
| 904 |
res = ensure_has_no_more_tokens(cp, fname, lineno); |
| 905 |
return res; |
| 906 |
} |
| 907 |
|
| 908 |
/* |
| 909 |
We are guaranteed it's a cfa_reg: command. Parse it |
| 910 |
and record the important data. |
| 911 |
*/ |
| 912 |
static int |
| 913 |
parsecfa_reg(char *cp, const char *fname, unsigned long lineno, |
| 914 |
struct conf_internal_s *conf, struct comtable_s *comtab) |
| 915 |
{ |
| 916 |
size_t clen = comtab->namelen; |
| 917 |
struct token_s tok; |
| 918 |
unsigned long val = 0; |
| 919 |
int ok = FALSE; |
| 920 |
int res = FALSE; |
| 921 |
|
| 922 |
cp = cp + clen + 1; |
| 923 |
cp = get_token(cp, &tok); |
| 924 |
if (tok.tk_len == 0) { |
| 925 |
printf("dwarfdump.conf error: " |
| 926 |
"%s missing cfa_reg number %s line %lu", |
| 927 |
comtab->name, fname, lineno); |
| 928 |
++errcount; |
| 929 |
return FALSE; |
| 930 |
} |
| 931 |
|
| 932 |
ok = make_a_number(comtab->name, fname, lineno, &tok, &val); |
| 933 |
|
| 934 |
if (!ok) { |
| 935 |
++errcount; |
| 936 |
return FALSE; |
| 937 |
} |
| 938 |
conf->conf_out->cf_cfa_reg = (int) val; |
| 939 |
res = ensure_has_no_more_tokens(cp, fname, lineno); |
| 940 |
return res; |
| 941 |
} |
| 942 |
|
| 943 |
/* We are guaranteed it's an initial_reg_value: command, |
| 944 |
parse it and put the reg value where it will be remembered. |
| 945 |
*/ |
| 946 |
static int |
| 947 |
parseinitial_reg_value(char *cp, const char *fname, |
| 948 |
unsigned long lineno, |
| 949 |
struct conf_internal_s *conf, struct comtable_s *comtab) |
| 950 |
{ |
| 951 |
size_t clen = comtab->namelen; |
| 952 |
struct token_s tok; |
| 953 |
unsigned long val = 0; |
| 954 |
int ok = FALSE; |
| 955 |
int res = FALSE; |
| 956 |
|
| 957 |
cp = cp + clen + 1; |
| 958 |
cp = get_token(cp, &tok); |
| 959 |
if (tok.tk_len == 0) { |
| 960 |
printf("dwarfdump.conf error: " |
| 961 |
"%s missing initial reg value %s line %lu", |
| 962 |
comtab->name, fname, lineno); |
| 963 |
++errcount; |
| 964 |
return FALSE; |
| 965 |
} |
| 966 |
|
| 967 |
ok = make_a_number(comtab->name, fname, lineno, &tok, &val); |
| 968 |
|
| 969 |
if (!ok) { |
| 970 |
|
| 971 |
++errcount; |
| 972 |
return FALSE; |
| 973 |
} |
| 974 |
conf->conf_out->cf_initial_rule_value = (int) val; |
| 975 |
res = ensure_has_no_more_tokens(cp, fname, lineno); |
| 976 |
return res; |
| 977 |
} |
| 978 |
|
| 979 |
static int |
| 980 |
parsesame_val_reg(char *cp, const char *fname, |
| 981 |
unsigned long lineno, |
| 982 |
struct conf_internal_s *conf, struct comtable_s *comtab) |
| 983 |
{ |
| 984 |
size_t clen = comtab->namelen; |
| 985 |
struct token_s tok; |
| 986 |
unsigned long val = 0; |
| 987 |
int ok = FALSE; |
| 988 |
int res = FALSE; |
| 989 |
|
| 990 |
cp = cp + clen + 1; |
| 991 |
cp = get_token(cp, &tok); |
| 992 |
if (tok.tk_len == 0) { |
| 993 |
printf("dwarfdump.conf error: " |
| 994 |
"%s missing same_reg value %s line %lu", |
| 995 |
comtab->name, fname, lineno); |
| 996 |
++errcount; |
| 997 |
return FALSE; |
| 998 |
} |
| 999 |
|
| 1000 |
ok = make_a_number(comtab->name, fname, lineno, &tok, &val); |
| 1001 |
|
| 1002 |
if (!ok) { |
| 1003 |
|
| 1004 |
++errcount; |
| 1005 |
return FALSE; |
| 1006 |
} |
| 1007 |
conf->conf_out->cf_same_val = val; |
| 1008 |
res = ensure_has_no_more_tokens(cp, fname, lineno); |
| 1009 |
return res; |
| 1010 |
} |
| 1011 |
|
| 1012 |
static int |
| 1013 |
parseundefined_val_reg(char *cp, const char *fname, |
| 1014 |
unsigned long lineno, |
| 1015 |
struct conf_internal_s *conf, struct comtable_s *comtab) |
| 1016 |
{ |
| 1017 |
size_t clen = comtab->namelen; |
| 1018 |
struct token_s tok; |
| 1019 |
unsigned long val = 0; |
| 1020 |
int ok = FALSE; |
| 1021 |
int res = FALSE; |
| 1022 |
|
| 1023 |
cp = cp + clen + 1; |
| 1024 |
cp = get_token(cp, &tok); |
| 1025 |
if (tok.tk_len == 0) { |
| 1026 |
printf("dwarfdump.conf error: " |
| 1027 |
"%s missing undefined_reg value %s line %lu", |
| 1028 |
comtab->name, fname, lineno); |
| 1029 |
++errcount; |
| 1030 |
return FALSE; |
| 1031 |
} |
| 1032 |
|
| 1033 |
ok = make_a_number(comtab->name, fname, lineno, &tok, &val); |
| 1034 |
|
| 1035 |
if (!ok) { |
| 1036 |
|
| 1037 |
++errcount; |
| 1038 |
return FALSE; |
| 1039 |
} |
| 1040 |
conf->conf_out->cf_undefined_val = (int) val; |
| 1041 |
res = ensure_has_no_more_tokens(cp, fname, lineno); |
| 1042 |
return res; |
| 1043 |
} |
| 1044 |
|
| 1045 |
/* We are guaranteed it's a table size command, parse it |
| 1046 |
and record the table size. |
| 1047 |
*/ |
| 1048 |
static int |
| 1049 |
parsereg_table_size(char *cp, const char *fname, unsigned long lineno, |
| 1050 |
struct conf_internal_s *conf, struct comtable_s *comtab) |
| 1051 |
{ |
| 1052 |
size_t clen = comtab->namelen; |
| 1053 |
struct token_s tok; |
| 1054 |
unsigned long val = 0; |
| 1055 |
int ok = FALSE; |
| 1056 |
int res = FALSE; |
| 1057 |
|
| 1058 |
cp = cp + clen + 1; |
| 1059 |
cp = get_token(cp, &tok); |
| 1060 |
if (tok.tk_len == 0) { |
| 1061 |
printf("dwarfdump.conf error: " |
| 1062 |
"%s missing reg table size value %s line %lu", |
| 1063 |
comtab->name, fname, lineno); |
| 1064 |
++errcount; |
| 1065 |
return FALSE; |
| 1066 |
} |
| 1067 |
|
| 1068 |
ok = make_a_number(comtab->name, fname, lineno, &tok, &val); |
| 1069 |
|
| 1070 |
if (!ok) { |
| 1071 |
++errcount; |
| 1072 |
return FALSE; |
| 1073 |
} |
| 1074 |
conf->conf_out->cf_table_entry_count = (unsigned long) val; |
| 1075 |
res = ensure_has_no_more_tokens(cp, fname, lineno); |
| 1076 |
return res; |
| 1077 |
} |
| 1078 |
|
| 1079 |
/* We are guaranteed it's a table size command, parse it |
| 1080 |
and record the table size. |
| 1081 |
*/ |
| 1082 |
static int |
| 1083 |
parseaddress_size(char *cp, const char *fname, unsigned long lineno, |
| 1084 |
struct conf_internal_s *conf, struct comtable_s *comtab) |
| 1085 |
{ |
| 1086 |
size_t clen = comtab->namelen; |
| 1087 |
struct token_s tok; |
| 1088 |
unsigned long val = 0; |
| 1089 |
int ok = FALSE; |
| 1090 |
int res = FALSE; |
| 1091 |
|
| 1092 |
cp = cp + clen + 1; |
| 1093 |
cp = get_token(cp, &tok); |
| 1094 |
if (tok.tk_len == 0) { |
| 1095 |
printf("dwarfdump.conf error: " |
| 1096 |
"%s missing address size value %s line %lu", |
| 1097 |
comtab->name, fname, lineno); |
| 1098 |
++errcount; |
| 1099 |
return FALSE; |
| 1100 |
} |
| 1101 |
|
| 1102 |
ok = make_a_number(comtab->name, fname, lineno, &tok, &val); |
| 1103 |
|
| 1104 |
if (!ok) { |
| 1105 |
++errcount; |
| 1106 |
return FALSE; |
| 1107 |
} |
| 1108 |
conf->conf_out->cf_address_size = (unsigned long) val; |
| 1109 |
res = ensure_has_no_more_tokens(cp, fname, lineno); |
| 1110 |
return res; |
| 1111 |
} |
| 1112 |
|
| 1113 |
/* We are guaranteed it's an endabi: command, parse it and |
| 1114 |
check we have the right abi. |
| 1115 |
*/ |
| 1116 |
static int |
| 1117 |
parseendabi(char *cp, const char *fname, |
| 1118 |
const char *abiname, unsigned long lineno, |
| 1119 |
struct comtable_s *comtab) |
| 1120 |
{ |
| 1121 |
size_t clen = comtab->namelen; |
| 1122 |
struct token_s tok; |
| 1123 |
int res = 0; |
| 1124 |
|
| 1125 |
cp = cp + clen + 1; |
| 1126 |
cp = get_token(cp, &tok); |
| 1127 |
if (strcmp(abiname, tok.tk_data) != 0) { |
| 1128 |
printf("%s error: " |
| 1129 |
"mismatch abi name %s (here) vs. " |
| 1130 |
"%s (beginabi:) %s line %lu\n", |
| 1131 |
comtab->name, tok.tk_data, abiname, fname, lineno); |
| 1132 |
++errcount; |
| 1133 |
return FALSE; |
| 1134 |
} |
| 1135 |
res = ensure_has_no_more_tokens(cp, fname, lineno); |
| 1136 |
return res; |
| 1137 |
} |
| 1138 |
static int |
| 1139 |
parseincludeabi(char *cp, const char *fname, unsigned long lineno, |
| 1140 |
char **abiname_out, |
| 1141 |
struct comtable_s *comtab) |
| 1142 |
{ |
| 1143 |
size_t clen = comtab->namelen; |
| 1144 |
struct token_s tok; |
| 1145 |
char *name = 0; |
| 1146 |
int res = FALSE; |
| 1147 |
|
| 1148 |
cp = cp + clen + 1; |
| 1149 |
cp = get_token(cp, &tok); |
| 1150 |
name = makename(tok.tk_data); |
| 1151 |
*abiname_out = name; |
| 1152 |
res = ensure_has_no_more_tokens(cp, fname, lineno); |
| 1153 |
return res; |
| 1154 |
} |
| 1155 |
|
| 1156 |
/* Return TRUE if we succeeded and filed in *out. |
| 1157 |
Return FALSE if we failed (and fill in nothing). |
| 1158 |
beginabi: <abiname> |
| 1159 |
reg: <regname> <dwarf regnumber> |
| 1160 |
frame_interface: <integer value 2 or 3> |
| 1161 |
cfa_reg: <number> |
| 1162 |
initial_reg_value: <number: normally 1034 or 1035 > |
| 1163 |
reg_table_size: <size of table> |
| 1164 |
endabi: <abiname> |
| 1165 |
|
| 1166 |
We are positioned at the start of a beginabi: line when |
| 1167 |
called. |
| 1168 |
|
| 1169 |
*/ |
| 1170 |
static int |
| 1171 |
parse_abi(FILE * stream, const char *fname, const char *abiname, |
| 1172 |
struct conf_internal_s *conf_internal, |
| 1173 |
unsigned long lineno, |
| 1174 |
unsigned int nest_level) |
| 1175 |
{ |
| 1176 |
struct dwconf_s *localconf = conf_internal->conf_out; |
| 1177 |
char buf[1000]; |
| 1178 |
int comtype = 0; |
| 1179 |
static int first_time_done = 0; |
| 1180 |
struct comtable_s *comtabp = 0; |
| 1181 |
int inourabi = FALSE; |
| 1182 |
int unknowntextcount = 0; |
| 1183 |
|
| 1184 |
if (nest_level > MAX_NEST_LEVEL) { |
| 1185 |
++errcount; |
| 1186 |
printf("dwarfdump.conf: includeabi nest " |
| 1187 |
"too deep in %s at line %lu\n", |
| 1188 |
sanitized(fname), lineno); |
| 1189 |
return FALSE; |
| 1190 |
} |
| 1191 |
|
| 1192 |
if (first_time_done == 0) { |
| 1193 |
finish_comtable_setup(); |
| 1194 |
first_time_done = 1; |
| 1195 |
} |
| 1196 |
|
| 1197 |
for (; !feof(stream);) { |
| 1198 |
char *line = 0; |
| 1199 |
|
| 1200 |
/* long loffset = ftell(stream); */ |
| 1201 |
line = fgets(buf, sizeof(buf), stream); |
| 1202 |
if (!line) { |
| 1203 |
++errcount; |
| 1204 |
printf("dwarfdump: end of file or error" |
| 1205 |
" before endabi: in %s, line %lu\n", |
| 1206 |
sanitized(fname), lineno); |
| 1207 |
return FALSE; |
| 1208 |
} |
| 1209 |
++lineno; |
| 1210 |
line = skipwhite(line); |
| 1211 |
comtype = which_command(line, &comtabp); |
| 1212 |
switch (comtype) { |
| 1213 |
case LT_OPTION: |
| 1214 |
if (!inourabi) break; |
| 1215 |
parseoption(line, sanitized(fname), lineno,comtabp); |
| 1216 |
break; |
| 1217 |
case LT_ERROR: |
| 1218 |
++unknowntextcount; |
| 1219 |
++errcount; |
| 1220 |
printf("dwarfdump: Unknown text in %s ", |
| 1221 |
sanitized(fname)); |
| 1222 |
printf("is \"%s\" " |
| 1223 |
"at line %lu\n", |
| 1224 |
sanitized(line), lineno); |
| 1225 |
if (unknowntextcount > 2) { |
| 1226 |
printf("Too much unknown text. Giving up " |
| 1227 |
"on the dwarfdump.conf file\n"); |
| 1228 |
return FALSE; |
| 1229 |
} |
| 1230 |
break; |
| 1231 |
case LT_COMMENT: |
| 1232 |
break; |
| 1233 |
case LT_BLANK: |
| 1234 |
break; |
| 1235 |
case LT_BEGINABI: |
| 1236 |
if (conf_internal->beginabi_lineno > 0) { |
| 1237 |
++errcount; |
| 1238 |
printf("dwarfdump: Encountered beginabi: " |
| 1239 |
"when not expected. " |
| 1240 |
"%s line %lu previous beginabi line %lu\n", |
| 1241 |
sanitized(fname), |
| 1242 |
lineno, conf_internal->beginabi_lineno); |
| 1243 |
} |
| 1244 |
conf_internal->beginabi_lineno = lineno; |
| 1245 |
inourabi = parsebeginabi(line, fname, |
| 1246 |
abiname, lineno, comtabp); |
| 1247 |
break; |
| 1248 |
|
| 1249 |
case LT_REG: |
| 1250 |
if (!inourabi) break; |
| 1251 |
parsereg(line, fname, lineno, conf_internal, comtabp); |
| 1252 |
conf_internal->regcount++; |
| 1253 |
break; |
| 1254 |
case LT_FRAME_INTERFACE: |
| 1255 |
if (!inourabi) break; |
| 1256 |
if (conf_internal->frame_interface_lineno > 0) { |
| 1257 |
++errcount; |
| 1258 |
printf("dwarfdump: Encountered duplicate " |
| 1259 |
"frame_interface: " |
| 1260 |
"%s line %lu previous frame_interface: " |
| 1261 |
"line %lu\n", |
| 1262 |
sanitized(fname), lineno, |
| 1263 |
conf_internal->frame_interface_lineno); |
| 1264 |
} |
| 1265 |
conf_internal->frame_interface_lineno = lineno; |
| 1266 |
parseframe_interface(line, fname, |
| 1267 |
lineno, conf_internal, comtabp); |
| 1268 |
break; |
| 1269 |
case LT_CFA_REG: |
| 1270 |
if (!inourabi) break; |
| 1271 |
if (conf_internal->cfa_reg_lineno > 0) { |
| 1272 |
printf("dwarfdump: Encountered duplicate cfa_reg: " |
| 1273 |
"%s line %lu previous cfa_reg line %lu\n", |
| 1274 |
sanitized(fname), |
| 1275 |
lineno, conf_internal->cfa_reg_lineno); |
| 1276 |
++errcount; |
| 1277 |
} |
| 1278 |
conf_internal->cfa_reg_lineno = lineno; |
| 1279 |
parsecfa_reg(line, fname, lineno, conf_internal, comtabp); |
| 1280 |
break; |
| 1281 |
case LT_INITIAL_REG_VALUE: |
| 1282 |
if (!inourabi) break; |
| 1283 |
if (conf_internal->initial_reg_value_lineno > 0) { |
| 1284 |
printf |
| 1285 |
("dwarfdump: Encountered duplicate " |
| 1286 |
"initial_reg_value: " |
| 1287 |
"%s line %lu previous initial_reg_value: " |
| 1288 |
"line %lu\n", |
| 1289 |
sanitized(fname), lineno, |
| 1290 |
conf_internal->initial_reg_value_lineno); |
| 1291 |
++errcount; |
| 1292 |
} |
| 1293 |
conf_internal->initial_reg_value_lineno = lineno; |
| 1294 |
parseinitial_reg_value(line, fname, |
| 1295 |
lineno, conf_internal, comtabp); |
| 1296 |
break; |
| 1297 |
case LT_SAME_VAL_REG: |
| 1298 |
if (!inourabi) break; |
| 1299 |
if (conf_internal->same_val_reg_lineno > 0) { |
| 1300 |
++errcount; |
| 1301 |
printf |
| 1302 |
("dwarfdump: Encountered duplicate " |
| 1303 |
"same_val_reg: " |
| 1304 |
"%s line %lu previous initial_reg_value: " |
| 1305 |
"line %lu\n", |
| 1306 |
sanitized(fname), lineno, |
| 1307 |
conf_internal->initial_reg_value_lineno); |
| 1308 |
} |
| 1309 |
conf_internal->same_val_reg_lineno = lineno; |
| 1310 |
parsesame_val_reg(line, fname, |
| 1311 |
lineno, conf_internal, comtabp); |
| 1312 |
break; |
| 1313 |
case LT_UNDEFINED_VAL_REG: |
| 1314 |
if (!inourabi) break; |
| 1315 |
if (conf_internal->undefined_val_reg_lineno > 0) { |
| 1316 |
++errcount; |
| 1317 |
printf |
| 1318 |
("dwarfdump: Encountered duplicate " |
| 1319 |
"undefined_val_reg: " |
| 1320 |
"%s line %lu previous initial_reg_value: " |
| 1321 |
"line %lu\n", |
| 1322 |
sanitized(fname), lineno, |
| 1323 |
conf_internal->initial_reg_value_lineno); |
| 1324 |
} |
| 1325 |
conf_internal->undefined_val_reg_lineno = lineno; |
| 1326 |
parseundefined_val_reg(line, fname, |
| 1327 |
lineno, conf_internal, comtabp); |
| 1328 |
break; |
| 1329 |
case LT_REG_TABLE_SIZE: |
| 1330 |
if (!inourabi) break; |
| 1331 |
if (conf_internal->reg_table_size_lineno > 0) { |
| 1332 |
printf("dwarfdump: duplicate reg_table_size: " |
| 1333 |
"%s line %lu previous reg_table_size: line %lu\n", |
| 1334 |
sanitized(fname), lineno, |
| 1335 |
conf_internal->reg_table_size_lineno); |
| 1336 |
++errcount; |
| 1337 |
} |
| 1338 |
conf_internal->reg_table_size_lineno = lineno; |
| 1339 |
parsereg_table_size(line, fname, |
| 1340 |
lineno, conf_internal, comtabp); |
| 1341 |
break; |
| 1342 |
case LT_ENDABI: |
| 1343 |
if (!inourabi) break; |
| 1344 |
parseendabi(line, fname, abiname, lineno, comtabp); |
| 1345 |
if (conf_internal->regcount > |
| 1346 |
localconf->cf_table_entry_count) { |
| 1347 |
printf("dwarfdump: more registers named than " |
| 1348 |
" in %s ( %lu named vs %s %lu)" |
| 1349 |
" %s line %lu\n", |
| 1350 |
abiname, (unsigned long) conf_internal->regcount, |
| 1351 |
name_reg_table_size, |
| 1352 |
(unsigned long) localconf->cf_table_entry_count, |
| 1353 |
sanitized(fname), (unsigned long) lineno); |
| 1354 |
++errcount; |
| 1355 |
} |
| 1356 |
inourabi = FALSE; |
| 1357 |
return TRUE; |
| 1358 |
case LT_ADDRESS_SIZE: |
| 1359 |
if (!inourabi) break; |
| 1360 |
if (conf_internal->address_size_lineno > 0) { |
| 1361 |
printf("dwarfdump: duplicate address_size: " |
| 1362 |
"%s line %lu previous address_size:" |
| 1363 |
" line %lu\n", |
| 1364 |
fname, lineno, |
| 1365 |
conf_internal->address_size_lineno); |
| 1366 |
++errcount; |
| 1367 |
} |
| 1368 |
conf_internal->address_size_lineno = lineno; |
| 1369 |
parseaddress_size(line, fname, |
| 1370 |
lineno, conf_internal, comtabp); |
| 1371 |
break; |
| 1372 |
case LT_INCLUDEABI: { |
| 1373 |
char *abiname_inner = 0; |
| 1374 |
unsigned long abilno = conf_internal->beginabi_lineno; |
| 1375 |
int ires = 0; |
| 1376 |
|
| 1377 |
if (!inourabi) break; |
| 1378 |
ires = parseincludeabi(line,fname,lineno, |
| 1379 |
&abiname_inner,comtabp); |
| 1380 |
if (ires == FALSE) { |
| 1381 |
return FALSE; |
| 1382 |
} |
| 1383 |
/* For the nested abi read, the abi line |
| 1384 |
number must be set as if not-yet-read |
| 1385 |
and then restored. */ |
| 1386 |
conf_internal->beginabi_lineno = 0; |
| 1387 |
ires = find_conf_file_and_read_config_inner( |
| 1388 |
conf_internal->conf_name_used, |
| 1389 |
abiname_inner, conf_internal,nest_level+1); |
| 1390 |
if (ires == FOUND_ERROR) { |
| 1391 |
return ires; |
| 1392 |
} |
| 1393 |
conf_internal->beginabi_lineno = abilno; |
| 1394 |
} |
| 1395 |
break; |
| 1396 |
default: |
| 1397 |
printf("dwarfdump internal error," |
| 1398 |
" impossible line type %d %s %lu \n", |
| 1399 |
(int) comtype, sanitized(fname), lineno); |
| 1400 |
exit(EXIT_FAILURE); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
++errcount; |
| 1404 |
printf("End of file, no endabi: found. %s, line %lu\n", |
| 1405 |
sanitized(fname), lineno); |
| 1406 |
return FALSE; |
| 1407 |
} |
| 1408 |
|
| 1409 |
/* Naming a few registers makes printing these just |
| 1410 |
a little bit faster. |
| 1411 |
*/ |
| 1412 |
static char *genericregnames[] = { |
| 1413 |
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", |
| 1414 |
"r10", "r11", "r12", "r13", "r14", "r15", "r16", |
| 1415 |
"r17", "r18", "r19", "r20" |
| 1416 |
}; |
| 1417 |
|
| 1418 |
/* This is a simple generic set of registers. The |
| 1419 |
table entry count is pretty arbitrary. |
| 1420 |
*/ |
| 1421 |
void |
| 1422 |
init_conf_file_data(struct dwconf_s *config_file_data) |
| 1423 |
{ |
| 1424 |
unsigned generic_table_count; |
| 1425 |
config_file_data->cf_abi_name = ""; |
| 1426 |
config_file_data->cf_config_file_path = ""; |
| 1427 |
config_file_data->cf_interface_number = 3; |
| 1428 |
config_file_data->cf_table_entry_count = 100; |
| 1429 |
config_file_data->cf_initial_rule_value = DW_FRAME_UNDEFINED_VAL; |
| 1430 |
config_file_data->cf_cfa_reg = DW_FRAME_CFA_COL3; |
| 1431 |
config_file_data->cf_address_size = 0; |
| 1432 |
config_file_data->cf_same_val = DW_FRAME_SAME_VAL; |
| 1433 |
config_file_data->cf_undefined_val = DW_FRAME_UNDEFINED_VAL; |
| 1434 |
config_file_data->cf_regs = genericregnames; |
| 1435 |
generic_table_count = |
| 1436 |
sizeof(genericregnames) / sizeof(genericregnames[0]); |
| 1437 |
config_file_data->cf_named_regs_table_size = generic_table_count; |
| 1438 |
config_file_data->cf_regs_malloced = 0; |
| 1439 |
} |
| 1440 |
|
| 1441 |
/* A 'generic' ABI. For up to 1200 registers. |
| 1442 |
Perhaps cf_initial_rule_value should be d |
| 1443 |
UNDEFINED VALUE (1034) instead, but for the purposes of |
| 1444 |
getting the dwarfdump output correct |
| 1445 |
either will work. |
| 1446 |
*/ |
| 1447 |
void |
| 1448 |
init_generic_config_1200_regs(struct dwconf_s *config_file_data) |
| 1449 |
{ |
| 1450 |
unsigned long generic_table_count = |
| 1451 |
sizeof(genericregnames) / sizeof(genericregnames[0]); |
| 1452 |
config_file_data->cf_interface_number = 3; |
| 1453 |
config_file_data->cf_table_entry_count = 1200; |
| 1454 |
/* There is no defined name for cf_initial_rule_value, |
| 1455 |
cf_same_val, or cf_undefined_val in libdwarf.h, |
| 1456 |
these must just be high enough to be higher than |
| 1457 |
any real register number. |
| 1458 |
DW_FRAME_CFA_COL3 must also be higher than any |
| 1459 |
real register number. */ |
| 1460 |
config_file_data->cf_initial_rule_value = 1235; /* SAME VALUE */ |
| 1461 |
config_file_data->cf_cfa_reg = DW_FRAME_CFA_COL3; |
| 1462 |
config_file_data->cf_address_size = 0; |
| 1463 |
config_file_data->cf_same_val = 1235; |
| 1464 |
config_file_data->cf_undefined_val = 1234; |
| 1465 |
config_file_data->cf_regs = genericregnames; |
| 1466 |
config_file_data->cf_named_regs_table_size = generic_table_count; |
| 1467 |
config_file_data->cf_regs_malloced = 0; |
| 1468 |
} |
| 1469 |
|
| 1470 |
/* Print the 'right' string for the register we are given. |
| 1471 |
Deal sensibly with the special regs as well as numbers |
| 1472 |
we know and those we have not been told about. |
| 1473 |
|
| 1474 |
We are not allowing negative register numbers. |
| 1475 |
*/ |
| 1476 |
void |
| 1477 |
print_reg_from_config_data(Dwarf_Unsigned reg, |
| 1478 |
struct dwconf_s *config_data) |
| 1479 |
{ |
| 1480 |
char *name = 0; |
| 1481 |
if (reg == config_data->cf_cfa_reg) { |
| 1482 |
fputs("cfa",stdout); |
| 1483 |
return; |
| 1484 |
} |
| 1485 |
if (reg == config_data->cf_undefined_val) { |
| 1486 |
fputs("u",stdout); |
| 1487 |
return; |
| 1488 |
} |
| 1489 |
if (reg == config_data->cf_same_val) { |
| 1490 |
fputs("s",stdout); |
| 1491 |
return; |
| 1492 |
} |
| 1493 |
|
| 1494 |
if (config_data->cf_regs == 0 || |
| 1495 |
reg >= config_data->cf_named_regs_table_size) { |
| 1496 |
printf("r%" DW_PR_DUu "",reg); |
| 1497 |
return; |
| 1498 |
} |
| 1499 |
name = config_data->cf_regs[reg]; |
| 1500 |
if (!name) { |
| 1501 |
/* Can happen, the reg names table can be sparse. */ |
| 1502 |
printf("r%" DW_PR_DUu "", reg); |
| 1503 |
return; |
| 1504 |
} |
| 1505 |
fputs(name,stdout); |
| 1506 |
return; |
| 1507 |
} |
| 1508 |
|
| 1509 |
void |
| 1510 |
free_all_dwconf(struct dwconf_s *conf) |
| 1511 |
{ |
| 1512 |
if (conf->cf_regs_malloced) { |
| 1513 |
free(conf->cf_regs); |
| 1514 |
} |
| 1515 |
conf->cf_regs = 0; |
| 1516 |
conf->cf_named_regs_table_size =0; |
| 1517 |
conf->cf_regs_malloced = 0; |
| 1518 |
} |