5151import argparse
5252import os
5353import struct
54- import elftools
54+
5555from distutils .version import LooseVersion
56+
57+ import elftools
5658from elftools .elf .elffile import ELFFile
5759from elftools .elf .sections import SymbolTableSection
5860
6163
6264
6365def bit (pos ):
66+ """Get value by shifting 1 by pos"""
6467 return 1 << pos
6568
6669
@@ -80,23 +83,27 @@ def bit(pos):
8083ENTRY_XD = FLAG_XD | FLAG_IGNORED2
8184
8285def debug (text ):
86+ """Display verbose debug message"""
8387 if not args .verbose :
8488 return
8589 sys .stdout .write (os .path .basename (sys .argv [0 ]) + ": " + text + "\n " )
8690
8791
8892def error (text ):
93+ """Display error message and exit program"""
8994 sys .exit (os .path .basename (sys .argv [0 ]) + ": " + text )
9095
9196
9297def align_check (base , size ):
98+ """Make sure base and size are page-aligned"""
9399 if (base % 4096 ) != 0 :
94100 error ("unaligned base address %x" % base )
95101 if (size % 4096 ) != 0 :
96102 error ("Unaligned region size %d for base %x" % (size , base ))
97103
98104
99105def dump_flags (flags ):
106+ """Translate page table flags into string"""
100107 ret = ""
101108
102109 if flags & FLAG_P :
@@ -131,7 +138,7 @@ def round_down(val, align):
131138# access or set caching properties at leaf levels.
132139INT_FLAGS = FLAG_P | FLAG_RW | FLAG_US
133140
134- class MMUTable (object ):
141+ class MMUTable ():
135142 """Represents a particular table in a set of page tables, at any level"""
136143
137144 def __init__ (self ):
@@ -275,7 +282,7 @@ class PtXd(Pt):
275282 FLAG_IGNORED0 | FLAG_IGNORED1 | FLAG_IGNORED2 )
276283
277284
278- class PtableSet (object ):
285+ class PtableSet ():
279286 """Represents a complete set of page tables for any paging mode"""
280287
281288 def __init__ (self , pages_start , offset = 0 ):
@@ -309,6 +316,7 @@ def levels(self):
309316 raise NotImplementedError ()
310317
311318 def new_child_table (self , table , virt_addr , depth ):
319+ """Create a new child table"""
312320 new_table_addr = self .get_new_mmutable_addr ()
313321 new_table = self .levels [depth ]()
314322 debug ("new %s at physical addr 0x%x"
@@ -436,10 +444,10 @@ def set_region_perms(self, name, flags, use_offset=False):
436444
437445 def write_output (self , filename ):
438446 """Write the page tables to the output file in binary format"""
439- with open (filename , "wb" ) as fp :
447+ with open (filename , "wb" ) as output_fp :
440448 for addr in sorted (self .tables ):
441449 mmu_table = self .tables [addr ]
442- fp .write (mmu_table .get_binary ())
450+ output_fp .write (mmu_table .get_binary ())
443451
444452 # We always have the top-level table be last. This is because
445453 # in PAE, the top-level PDPT has only 4 entries and is not a
@@ -448,21 +456,26 @@ def write_output(self, filename):
448456 debug ("top-level %s at physical addr 0x%x" %
449457 (self .toplevel .__class__ .__name__ ,
450458 self .get_new_mmutable_addr ()))
451- fp .write (self .toplevel .get_binary ())
459+ output_fp .write (self .toplevel .get_binary ())
452460
453461# Paging mode classes, we'll use one depending on configuration
454462class Ptables32bit (PtableSet ):
463+ """32-bit Page Tables"""
455464 levels = [Pd , Pt ]
456465
457466class PtablesPAE (PtableSet ):
467+ """PAE Page Tables"""
458468 levels = [PdptPAE , PdXd , PtXd ]
459469
460470class PtablesIA32e (PtableSet ):
471+ """Page Tables under IA32e mode"""
461472 levels = [Pml4 , Pdpt , PdXd , PtXd ]
462473
463474
464475def parse_args ():
476+ """Parse command line arguments"""
465477 global args
478+
466479 parser = argparse .ArgumentParser (
467480 description = __doc__ ,
468481 formatter_class = argparse .RawDescriptionHelpFormatter )
@@ -478,23 +491,26 @@ def parse_args():
478491 args .verbose = True
479492
480493
481- def get_symbols (obj ):
482- for section in obj .iter_sections ():
494+ def get_symbols (elf_obj ):
495+ """Get all symbols from the ELF file"""
496+ for section in elf_obj .iter_sections ():
483497 if isinstance (section , SymbolTableSection ):
484498 return {sym .name : sym .entry .st_value
485499 for sym in section .iter_symbols ()}
486500
487501 raise LookupError ("Could not find symbol table" )
488502
489503def isdef (sym_name ):
504+ """True if symbol is defined in ELF file"""
490505 return sym_name in syms
491506
492507def main ():
508+ """Main program"""
493509 global syms
494510 parse_args ()
495511
496- with open (args .kernel , "rb" ) as fp :
497- kernel = ELFFile (fp )
512+ with open (args .kernel , "rb" ) as elf_fp :
513+ kernel = ELFFile (elf_fp )
498514 syms = get_symbols (kernel )
499515
500516 if isdef ("CONFIG_X86_64" ):
0 commit comments