68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package elf
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"io"
|
|
)
|
|
|
|
const (
|
|
baseAddress = 0x10000
|
|
)
|
|
|
|
// ELF64 represents an ELF 64-bit file.
|
|
type ELF64 struct {
|
|
Header
|
|
ProgramHeader
|
|
Padding []byte
|
|
Code []byte
|
|
}
|
|
|
|
// New creates a new 64-bit ELF binary.
|
|
func New(code []byte) *ELF64 {
|
|
elf := &ELF64{
|
|
Header: Header{
|
|
Magic: [4]byte{0x7F, 'E', 'L', 'F'},
|
|
Class: 2,
|
|
Endianness: LittleEndian,
|
|
Version: 1,
|
|
OSABI: 0,
|
|
ABIVersion: 0,
|
|
Type: TypeExecutable,
|
|
Architecture: ArchitectureAMD64,
|
|
FileVersion: 1,
|
|
EntryPointInMemory: baseAddress + 0x80,
|
|
ProgramHeaderOffset: HeaderSize,
|
|
SectionHeaderOffset: 0,
|
|
Flags: 0,
|
|
Size: HeaderSize,
|
|
ProgramHeaderEntrySize: ProgramHeaderSize,
|
|
ProgramHeaderEntryCount: 1,
|
|
SectionHeaderEntrySize: SectionHeaderSize,
|
|
SectionHeaderEntryCount: 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
|
|
}
|
|
|
|
// Write writes the ELF64 format to the given writer.
|
|
func (elf *ELF64) Write(writer io.Writer) {
|
|
binary.Write(writer, binary.LittleEndian, &elf.Header)
|
|
binary.Write(writer, binary.LittleEndian, &elf.ProgramHeader)
|
|
writer.Write(elf.Padding)
|
|
writer.Write(elf.Code)
|
|
}
|