Improved documentation

This commit is contained in:
2024-07-13 14:18:55 +02:00
parent 9df899cb52
commit 948d499231
6 changed files with 49 additions and 26 deletions

View File

@ -11,11 +11,12 @@ import (
// ELF represents an ELF file.
type ELF struct {
Header
CodeHeader ProgramHeader
PadCode []byte
Code []byte
PadData []byte
Data []byte
CodeHeader ProgramHeader
DataHeader ProgramHeader
CodePadding []byte
Code []byte
DataPadding []byte
Data []byte
}
// New creates a new ELF binary.
@ -24,7 +25,7 @@ func New(code []byte, data []byte) *ELF {
dataPadding := Padding(dataOffset, config.Align)
dataOffset += dataPadding
elf := &ELF{
return &ELF{
Header: Header{
Magic: [4]byte{0x7F, 'E', 'L', 'F'},
Class: 2,
@ -41,7 +42,7 @@ func New(code []byte, data []byte) *ELF {
Flags: 0,
Size: HeaderSize,
ProgramHeaderEntrySize: ProgramHeaderSize,
ProgramHeaderEntryCount: 1,
ProgramHeaderEntryCount: 2,
SectionHeaderEntrySize: SectionHeaderSize,
SectionHeaderEntryCount: 0,
SectionNameStringTableIndex: 0,
@ -56,25 +57,30 @@ func New(code []byte, data []byte) *ELF {
SizeInMemory: int64(len(code)),
Align: config.Align,
},
PadCode: bytes.Repeat([]byte{0}, 8),
Code: code,
PadData: bytes.Repeat([]byte{0}, int(dataPadding)),
Data: data,
DataHeader: ProgramHeader{
Type: ProgramTypeLOAD,
Flags: ProgramFlagsReadable,
Offset: dataOffset,
VirtualAddress: config.BaseAddress + dataOffset,
PhysicalAddress: config.BaseAddress + dataOffset,
SizeInFile: int64(len(data)),
SizeInMemory: int64(len(data)),
Align: config.Align,
},
CodePadding: nil,
Code: code,
DataPadding: bytes.Repeat([]byte{0}, int(dataPadding)),
Data: data,
}
return elf
}
func Padding(n int64, align int64) int64 {
return align - (n % align)
}
// Write writes the ELF64 format to the given writer.
func (elf *ELF) Write(writer io.Writer) {
binary.Write(writer, binary.LittleEndian, &elf.Header)
binary.Write(writer, binary.LittleEndian, &elf.CodeHeader)
writer.Write(elf.PadCode)
binary.Write(writer, binary.LittleEndian, &elf.DataHeader)
writer.Write(elf.CodePadding)
writer.Write(elf.Code)
writer.Write(elf.PadData)
writer.Write(elf.DataPadding)
writer.Write(elf.Data)
}