def Pack(self, offset): """Figure out how to pack the entry into the section Most of the time the entries are not fully specified. There may be an alignment but no size. In that case we take the size from the contents of the entry. If an entry has no hard-coded offset, it will be placed at @offset. Once this function is complete, both the offset and size of the entry will be know. Args: Current section offset pointer Returns: New section offset pointer (after this entry) """ self.Detail('Packing: offset=%s, size=%s, content_size=%x' % (ToHex(self.offset), ToHex(self.size), self.contents_size)) if self.offset is None: if self.offset_unset: self.Raise('No offset set with offset-unset: should another ' 'entry provide this correct offset?') self.offset = tools.Align(offset, self.align) needed = self.pad_before + self.contents_size + self.pad_after needed = tools.Align(needed, self.align_size) size = self.size if not size: size = needed new_offset = self.offset + size aligned_offset = tools.Align(new_offset, self.align_end) if aligned_offset != new_offset: size = aligned_offset - self.offset new_offset = aligned_offset if not self.size: self.size = size if self.size < needed: self.Raise("Entry contents size is %#x (%d) but entry size is " "%#x (%d)" % (needed, needed, self.size, self.size)) # Check that the alignment is correct. It could be wrong if the # and offset or size values were provided (i.e. not calculated), but # conflict with the provided alignment values if self.size != tools.Align(self.size, self.align_size): self.Raise( "Size %#x (%d) does not match align-size %#x (%d)" % (self.size, self.size, self.align_size, self.align_size)) if self.offset != tools.Align(self.offset, self.align): self.Raise("Offset %#x (%d) does not match align %#x (%d)" % (self.offset, self.offset, self.align, self.align)) self.Detail( ' - packed: offset=%#x, size=%#x, content_size=%#x, next_offset=%x' % (self.offset, self.size, self.contents_size, new_offset)) return new_offset
def Pack(self, pos): """Figure out how to pack the entry into the image Most of the time the entries are not fully specified. There may be an alignment but no size. In that case we take the size from the contents of the entry. If an entry has no hard-coded position, it will be placed at @pos. Once this function is complete, both the position and size of the entry will be know. Args: Current image position pointer Returns: New image position pointer (after this entry) """ if self.pos is None: if self.pos_unset: self.Raise('No position set with pos-unset: should another ' 'entry provide this correct position?') self.pos = tools.Align(pos, self.align) needed = self.pad_before + self.contents_size + self.pad_after needed = tools.Align(needed, self.align_size) size = self.size if not size: size = needed new_pos = self.pos + size aligned_pos = tools.Align(new_pos, self.align_end) if aligned_pos != new_pos: size = aligned_pos - self.pos new_pos = aligned_pos if not self.size: self.size = size if self.size < needed: self.Raise("Entry contents size is %#x (%d) but entry size is " "%#x (%d)" % (needed, needed, self.size, self.size)) # Check that the alignment is correct. It could be wrong if the # and pos or size values were provided (i.e. not calculated), but # conflict with the provided alignment values if self.size != tools.Align(self.size, self.align_size): self.Raise( "Size %#x (%d) does not match align-size %#x (%d)" % (self.size, self.size, self.align_size, self.align_size)) if self.pos != tools.Align(self.pos, self.align): self.Raise("Position %#x (%d) does not match align %#x (%d)" % (self.pos, self.pos, self.align, self.align)) return new_pos
def CheckSize(self): """Check that the image contents does not exceed its size, etc.""" contents_size = 0 for entry in self._entries.values(): contents_size = max(contents_size, entry.pos + entry.size) contents_size -= self._skip_at_start size = self._size if not size: size = self._pad_before + contents_size + self._pad_after size = tools.Align(size, self._align_size) if self._size and contents_size > self._size: self._Raise("contents size %#x (%d) exceeds image size %#x (%d)" % (contents_size, contents_size, self._size, self._size)) if not self._size: self._size = size if self._size != tools.Align(self._size, self._align_size): self._Raise( "Size %#x (%d) does not match align-size %#x (%d)" % (self._size, self._size, self._align_size, self._align_size))