21
21
import sys
22
22
23
23
import dictionary_parser .log_database
24
- from dictionary_parser .log_database import LogDatabase
25
- from dictionary_parser .utils import extract_one_string_in_section
26
- from dictionary_parser .utils import find_string_in_mappings
27
-
28
24
import elftools
25
+ from dictionary_parser .log_database import LogDatabase
26
+ from dictionary_parser .utils import extract_one_string_in_section , find_string_in_mappings
27
+ from elftools .dwarf .descriptions import describe_DWARF_expr
28
+ from elftools .dwarf .locationlists import LocationExpr , LocationParser
29
29
from elftools .elf .constants import SH_FLAGS
30
- from elftools .elf .elffile import ELFFile
31
30
from elftools .elf .descriptions import describe_ei_data
31
+ from elftools .elf .elffile import ELFFile
32
32
from elftools .elf .sections import SymbolTableSection
33
- from elftools .dwarf .descriptions import (
34
- describe_DWARF_expr
35
- )
36
- from elftools .dwarf .locationlists import (
37
- LocationExpr , LocationParser
38
- )
39
-
40
33
41
34
LOGGER_FORMAT = "%(name)s: %(levelname)s: %(message)s"
42
35
logger = logging .getLogger (os .path .basename (sys .argv [0 ]))
@@ -309,7 +302,7 @@ def find_die_var_base_type(compile_unit, die, is_const):
309
302
return die .attributes ['DW_AT_name' ].value .decode ('ascii' ), is_const
310
303
311
304
# Not a type, cannot continue
312
- if not 'DW_AT_type' in die .attributes :
305
+ if 'DW_AT_type' not in die .attributes :
313
306
return None , None
314
307
315
308
if die .tag == 'DW_TAG_const_type' :
@@ -334,10 +327,7 @@ def is_die_var_const_char(compile_unit, die):
334
327
"""
335
328
var_type , is_const = find_die_var_base_type (compile_unit , die , False )
336
329
337
- if var_type is not None and var_type .endswith ('char' ) and is_const :
338
- return True
339
-
340
- return False
330
+ return bool (var_type is not None and var_type .endswith ('char' ) and is_const )
341
331
342
332
343
333
def extract_string_variables (elf ):
@@ -357,32 +347,31 @@ def extract_string_variables(elf):
357
347
for die in compile_unit .iter_DIEs ():
358
348
# Only care about variables with location information
359
349
# and of type "char"
360
- if die .tag == 'DW_TAG_variable' :
361
- if ('DW_AT_type' in die .attributes
362
- and 'DW_AT_location' in die .attributes
363
- and is_die_var_const_char (compile_unit , die )
364
- ):
365
- # Extract location information, which is
366
- # its address in memory.
367
- loc_attr = die .attributes ['DW_AT_location' ]
368
- if loc_parser .attribute_has_location (loc_attr , die .cu ['version' ]):
369
- loc = loc_parser .parse_from_attribute (loc_attr , die .cu ['version' ], die )
370
- if isinstance (loc , LocationExpr ):
371
- try :
372
- addr = describe_DWARF_expr (loc .loc_expr ,
373
- dwarf_info .structs )
374
-
375
- matcher = DT_LOCATION_REGEX .match (addr )
376
- if matcher :
377
- addr = int (matcher .group (1 ), 16 )
378
- if addr > 0 :
379
- strings .append ({
380
- 'name' : die .attributes ['DW_AT_name' ].value ,
381
- 'addr' : addr ,
382
- 'die' : die
383
- })
384
- except KeyError :
385
- pass
350
+ if die .tag == 'DW_TAG_variable' and ('DW_AT_type' in die .attributes
351
+ and 'DW_AT_location' in die .attributes
352
+ and is_die_var_const_char (compile_unit , die )
353
+ ):
354
+ # Extract location information, which is
355
+ # its address in memory.
356
+ loc_attr = die .attributes ['DW_AT_location' ]
357
+ if loc_parser .attribute_has_location (loc_attr , die .cu ['version' ]):
358
+ loc = loc_parser .parse_from_attribute (loc_attr , die .cu ['version' ], die )
359
+ if isinstance (loc , LocationExpr ):
360
+ try :
361
+ addr = describe_DWARF_expr (loc .loc_expr ,
362
+ dwarf_info .structs )
363
+
364
+ matcher = DT_LOCATION_REGEX .match (addr )
365
+ if matcher :
366
+ addr = int (matcher .group (1 ), 16 )
367
+ if addr > 0 :
368
+ strings .append ({
369
+ 'name' : die .attributes ['DW_AT_name' ].value ,
370
+ 'addr' : addr ,
371
+ 'die' : die
372
+ })
373
+ except KeyError :
374
+ pass
386
375
387
376
return strings
388
377
@@ -408,9 +397,8 @@ def is_printable(b):
408
397
def extract_strings_in_one_section (section , str_mappings ):
409
398
"""Extract NULL-terminated strings in one ELF section"""
410
399
data = section ['data' ]
411
- idx = 0
412
400
start = None
413
- for x in data :
401
+ for idx , x in enumerate ( data ) :
414
402
if is_printable (chr (x )):
415
403
# Printable character, potential part of string
416
404
if start is None :
@@ -449,7 +437,6 @@ def extract_strings_in_one_section(section, str_mappings):
449
437
else :
450
438
# Non-printable byte, remove start location
451
439
start = None
452
- idx += 1
453
440
454
441
return str_mappings
455
442
@@ -508,7 +495,7 @@ def main():
508
495
elif args .verbose :
509
496
logger .setLevel (logging .INFO )
510
497
511
- elffile = open (args .elffile , "rb" )
498
+ elffile = open (args .elffile , "rb" ) # noqa: SIM115
512
499
if not elffile :
513
500
logger .error ("ERROR: Cannot open ELF file: %s, exiting..." , args .elffile )
514
501
sys .exit (1 )
@@ -529,8 +516,8 @@ def main():
529
516
530
517
if args .build_header :
531
518
with open (args .build_header ) as f :
532
- for l in f :
533
- match = re .match (r'\s*#define\s+BUILD_VERSION\s+(.*)' , l )
519
+ for line in f :
520
+ match = re .match (r'\s*#define\s+BUILD_VERSION\s+(.*)' , line )
534
521
if match :
535
522
database .set_build_id (match .group (1 ))
536
523
break
@@ -570,17 +557,15 @@ def main():
570
557
extract_logging_subsys_information (elf , database , string_mappings )
571
558
572
559
# Write database file
573
- if args .json :
574
- if not LogDatabase .write_json_database (args .json , database ):
575
- logger .error ("ERROR: Cannot open database file for write: %s, exiting..." ,
576
- args .json )
577
- sys .exit (1 )
560
+ if args .json and not LogDatabase .write_json_database (args .json , database ):
561
+ logger .error ("ERROR: Cannot open database file for write: %s, exiting..." ,
562
+ args .json )
563
+ sys .exit (1 )
578
564
579
- if args .syst :
580
- if not LogDatabase .write_syst_database (args .syst , database ):
581
- logger .error ("ERROR: Cannot open database file for write: %s, exiting..." ,
582
- args .syst )
583
- sys .exit (1 )
565
+ if args .syst and not LogDatabase .write_syst_database (args .syst , database ):
566
+ logger .error ("ERROR: Cannot open database file for write: %s, exiting..." ,
567
+ args .syst )
568
+ sys .exit (1 )
584
569
585
570
elffile .close ()
586
571
0 commit comments