# Nobody cares about DOS headers, so put the PE header right after.
PE_OFFSET = 64
+PE_MAGIC = b"PE\0\0"
def align_to(x: int, align: int) -> int:
def apply_elf_relative_relocation(
- reloc: ElfRelocation, image_base: int, sections: typing.List[PeSection], addend_size: int
+ reloc: ElfRelocation,
+ image_base: int,
+ sections: typing.List[PeSection],
+ addend_size: int,
):
# fmt: off
[target] = [
file.seek(0x3C, io.SEEK_SET)
file.write(PE_OFFSET.to_bytes(2, byteorder="little"))
file.seek(PE_OFFSET, io.SEEK_SET)
- file.write(b"PE\0\0")
+ file.write(PE_MAGIC)
file.write(coff)
file.write(opt)
file.write(pe_s)
offset = align_to(offset + len(pe_s.data), FILE_ALIGNMENT)
+ assert file.tell() <= opt.SizeOfHeaders
+
for pe_s in sections:
file.seek(pe_s.PointerToRawData, io.SEEK_SET)
file.write(pe_s.data)
opt.SizeOfHeaders = align_to(
PE_OFFSET
+ + len(PE_MAGIC)
+ + sizeof(PeCoffHeader)
+ coff.SizeOfOptionalHeader
+ sizeof(PeSection) * max(coff.NumberOfSections, args.minimum_sections),
FILE_ALIGNMENT,