Improved section offset calculation

This commit is contained in:
2025-01-20 13:51:47 +01:00
parent e2a6a31d8e
commit 4b7c9f387d
7 changed files with 56 additions and 38 deletions

View File

@ -9,6 +9,8 @@ import (
"git.akyoto.dev/cli/q/src/fs"
)
const HeaderEnd = HeaderSize + ProgramHeaderSize*2
// ELF represents an ELF file.
type ELF struct {
Header
@ -18,8 +20,6 @@ type ELF struct {
// Write writes the ELF64 format to the given writer.
func Write(writer io.Writer, code []byte, data []byte) {
const HeaderEnd = HeaderSize + ProgramHeaderSize*2
var (
codeStart, codePadding = fs.Align(HeaderEnd, config.Align)
dataStart, dataPadding = fs.Align(codeStart+len(code), config.Align)
@ -36,7 +36,7 @@ func Write(writer io.Writer, code []byte, data []byte) {
Type: TypeExecutable,
Architecture: ArchitectureAMD64,
FileVersion: 1,
EntryPointInMemory: config.BaseAddress + config.CodeOffset,
EntryPointInMemory: int64(config.BaseAddress + codeStart),
ProgramHeaderOffset: HeaderSize,
SectionHeaderOffset: 0,
Flags: 0,
@ -50,9 +50,9 @@ func Write(writer io.Writer, code []byte, data []byte) {
CodeHeader: ProgramHeader{
Type: ProgramTypeLOAD,
Flags: ProgramFlagsExecutable | ProgramFlagsReadable,
Offset: config.CodeOffset,
VirtualAddress: config.BaseAddress + config.CodeOffset,
PhysicalAddress: config.BaseAddress + config.CodeOffset,
Offset: int64(codeStart),
VirtualAddress: int64(config.BaseAddress + codeStart),
PhysicalAddress: int64(config.BaseAddress + codeStart),
SizeInFile: int64(len(code)),
SizeInMemory: int64(len(code)),
Align: config.Align,