Commits

Chris Perl  committed f4aac5a

Consolidate how elfclass and byte_order are conveyed throughout

  • Participants
  • Parent commits 2f32e67

Comments (0)

Files changed (1)

     # Variables that control certain behavior
     SYMBOLIC = True
 
+    @staticmethod
+    def save(attr, value):
+        setattr(ElfObject, attr, value)
+
     def __repr__(self):
         values = [ getattr(self, x) for x in self.fields ]
         combined = zip(self.fields, values)
             raise InvalidFileFormat("Incorrect Version Number: %d" % version)
 
         # Set our elfclass and byte_order
-        self.elfclass = self.e_ident[ElfIdent.EI_CLASS]
-        self.byte_order = self.e_ident[ElfIdent.EI_DATA]
+        # Note that this will mean that any object that inherits from ElfObject instantiated after
+        # this will have their 'elfclass' and 'byte_order' attributes set correctly already.  Since
+        # we are the first object to be created (we're the ELF Header, we have to be first),
+        # everyone else can just rely on the fact that these attributes will be there when that
+        # class is instantiated.
+        ElfObject.save("elfclass", self.e_ident[ElfIdent.EI_CLASS])
+        ElfObject.save("byte_order", self.e_ident[ElfIdent.EI_DATA])
 
         # H is unsigned 2 bytes
         # I is unsigned 4 bytes
         self._idx = idx
         self._size = size
 
-        # Remember our elfclass and byte order
-        self.elfclass = self.sht.elfclass
-        self.byte_order = self.sht.byte_order
-
         obj32 = { 'fmt': "IIIIIIIIII",
                   'size': 0x28,
                   'fields': SectionHeader.fields,
         self._idx = idx
         self._size = size
 
-        # Remember our elfclass and byte order
-        self.elfclass = self.pht.elfclass
-        self.byte_order = self.pht.byte_order
-
         obj32 = { 'fmt': "IIIIIIII",
                   'size': 0x20,
                   'fields': ProgramHeader.fields,
     def __init__(self, elf_hdr, f):
         self.elf_hdr = elf_hdr
         self._f = f
-        self.elfclass = self.elf_hdr.elfclass
-        self.byte_order = self.elf_hdr.byte_order
-
         LazyLoadingTable.__init__(self, self.elf_hdr.e_shnum)
 
         if self.elf_hdr.e_shnum > 0:
     def __init__(self, elf_hdr, f):
         self.elf_hdr = elf_hdr
         self._f = f
-        self.elfclass = self.elf_hdr.elfclass
-        self.byte_order = self.elf_hdr.byte_order
-
         LazyLoadingTable.__init__(self, self.elf_hdr.e_phnum)
 
     def _populate_entry(self, key):
                 sct_hdr.sh_type == SectionHeader.SHT_SYMTAB), \
                 "Attempted creation of SymbolTable for invalid section type: %d" % sct_hdr.sh_type
         self.sct_hdr = sct_hdr
-        self.elfclass = self.sct_hdr.elfclass
-        self.byte_order = self.sct_hdr.byte_order
-        
         SingleInstanceTablePerSectHdr.__init__(self, self.sct_hdr.sh_size/self.sct_hdr.sh_entsize)
 
         if len(self) > 0:
         self._idx = idx
         self._size = size
 
-        # Remember our elfclass and byte order
-        self.elfclass = self.st.elfclass
-        self.byte_order = self.st.byte_order
-
         # Note that we don't need a base for our offset, because in this case f is not the actual
         # file, but instead a StringIO instance instantiated by the SymbolTable to which we belong,
         # therefore the start of the table is at offset 0
                 "Attempted creation of RelocationTable for invalid section type: %d" % sct_hdr.sh_type
         self.sct_hdr = sct_hdr
         self._type = self.sct_hdr.sh_type
-        self.elfclass = self.sct_hdr.elfclass
-        self.byte_order = self.sct_hdr.byte_order
-
         SingleInstanceTablePerSectHdr.__init__(self, self.sct_hdr.sh_size/self.sct_hdr.sh_entsize)
 
         if len(self) > 0:
         self._idx = idx
         self._size = size
 
-        # Remember our elfclass and byte order
-        self.elfclass = self.rt.elfclass
-        self.byte_order = self.rt.byte_order
-
         # Call our super class constructor
         BaseRelocation.__init__(self)
 
         self._idx = idx
         self._size = size
 
-        # Remember our elfclass and byte order
-        self.elfclass = self.rt.elfclass
-        self.byte_order = self.rt.byte_order
-
         # Call our super class constructor
         BaseRelocation.__init__(self)