Added ELF program header
This commit is contained in:
parent
4554fb82cb
commit
8b66c9588c
@ -58,7 +58,7 @@ func writeToDisk(filePath string, code []byte, data []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buffer := bufio.NewWriter(file)
|
buffer := bufio.NewWriter(file)
|
||||||
executable := elf.New()
|
executable := elf.New(code)
|
||||||
executable.Write(buffer)
|
executable.Write(buffer)
|
||||||
buffer.Flush()
|
buffer.Flush()
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ const (
|
|||||||
LittleEndian = 1
|
LittleEndian = 1
|
||||||
TypeExecutable = 2
|
TypeExecutable = 2
|
||||||
ArchitectureAMD64 = 0x3E
|
ArchitectureAMD64 = 0x3E
|
||||||
|
Align = 16
|
||||||
HeaderSize = 64
|
HeaderSize = 64
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -6,18 +6,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
baseAddress = 0x400000
|
baseAddress = 0x10000
|
||||||
programAlign = 16
|
|
||||||
sectionAlign = 16
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ELF64 represents an ELF 64-bit file.
|
// ELF64 represents an ELF 64-bit file.
|
||||||
type ELF64 struct {
|
type ELF64 struct {
|
||||||
Header
|
Header
|
||||||
|
ProgramHeader
|
||||||
|
Padding []byte
|
||||||
|
Code []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new 64-bit ELF binary.
|
// New creates a new 64-bit ELF binary.
|
||||||
func New() *ELF64 {
|
func New(code []byte) *ELF64 {
|
||||||
elf := &ELF64{
|
elf := &ELF64{
|
||||||
Header: Header{
|
Header: Header{
|
||||||
Magic: [4]byte{0x7F, 'E', 'L', 'F'},
|
Magic: [4]byte{0x7F, 'E', 'L', 'F'},
|
||||||
@ -29,17 +30,29 @@ func New() *ELF64 {
|
|||||||
Type: TypeExecutable,
|
Type: TypeExecutable,
|
||||||
Architecture: ArchitectureAMD64,
|
Architecture: ArchitectureAMD64,
|
||||||
FileVersion: 1,
|
FileVersion: 1,
|
||||||
EntryPointInMemory: 0,
|
EntryPointInMemory: baseAddress + 0x80,
|
||||||
ProgramHeaderOffset: HeaderSize,
|
ProgramHeaderOffset: HeaderSize,
|
||||||
SectionHeaderOffset: 0, // TODO
|
SectionHeaderOffset: 0,
|
||||||
Flags: 0,
|
Flags: 0,
|
||||||
Size: HeaderSize,
|
Size: HeaderSize,
|
||||||
ProgramHeaderEntrySize: ProgramHeaderSize,
|
ProgramHeaderEntrySize: ProgramHeaderSize,
|
||||||
ProgramHeaderEntryCount: 0,
|
ProgramHeaderEntryCount: 1,
|
||||||
SectionHeaderEntrySize: SectionHeaderSize,
|
SectionHeaderEntrySize: SectionHeaderSize,
|
||||||
SectionHeaderEntryCount: 0,
|
SectionHeaderEntryCount: 0,
|
||||||
SectionNameStringTableIndex: 0,
|
SectionNameStringTableIndex: 0,
|
||||||
},
|
},
|
||||||
|
ProgramHeader: ProgramHeader{
|
||||||
|
Type: ProgramTypeLOAD,
|
||||||
|
Flags: ProgramFlagsExecutable,
|
||||||
|
Offset: 0x80,
|
||||||
|
VirtualAddress: baseAddress + 0x80,
|
||||||
|
PhysicalAddress: baseAddress + 0x80,
|
||||||
|
SizeInFile: int64(len(code)),
|
||||||
|
SizeInMemory: int64(len(code)),
|
||||||
|
Align: Align,
|
||||||
|
},
|
||||||
|
Padding: []byte{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
Code: code,
|
||||||
}
|
}
|
||||||
|
|
||||||
return elf
|
return elf
|
||||||
@ -48,4 +61,7 @@ func New() *ELF64 {
|
|||||||
// Write writes the ELF64 format to the given writer.
|
// Write writes the ELF64 format to the given writer.
|
||||||
func (elf *ELF64) Write(writer io.Writer) {
|
func (elf *ELF64) Write(writer io.Writer) {
|
||||||
binary.Write(writer, binary.LittleEndian, &elf.Header)
|
binary.Write(writer, binary.LittleEndian, &elf.Header)
|
||||||
|
binary.Write(writer, binary.LittleEndian, &elf.ProgramHeader)
|
||||||
|
writer.Write(elf.Padding)
|
||||||
|
writer.Write(elf.Code)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user