Implemented data labels
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package elf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
@ -10,13 +11,19 @@ import (
|
||||
// ELF represents an ELF file.
|
||||
type ELF struct {
|
||||
Header
|
||||
ProgramHeader
|
||||
Code []byte
|
||||
Data []byte
|
||||
CodeHeader ProgramHeader
|
||||
PadCode []byte
|
||||
Code []byte
|
||||
PadData []byte
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// New creates a new ELF binary.
|
||||
func New(code []byte, data []byte) *ELF {
|
||||
dataOffset := config.CodeOffset + int64(len(code))
|
||||
dataPadding := Padding(dataOffset, config.Align)
|
||||
dataOffset += dataPadding
|
||||
|
||||
elf := &ELF{
|
||||
Header: Header{
|
||||
Magic: [4]byte{0x7F, 'E', 'L', 'F'},
|
||||
@ -39,9 +46,9 @@ func New(code []byte, data []byte) *ELF {
|
||||
SectionHeaderEntryCount: 0,
|
||||
SectionNameStringTableIndex: 0,
|
||||
},
|
||||
ProgramHeader: ProgramHeader{
|
||||
CodeHeader: ProgramHeader{
|
||||
Type: ProgramTypeLOAD,
|
||||
Flags: ProgramFlagsExecutable,
|
||||
Flags: ProgramFlagsExecutable | ProgramFlagsReadable,
|
||||
Offset: config.CodeOffset,
|
||||
VirtualAddress: config.BaseAddress + config.CodeOffset,
|
||||
PhysicalAddress: config.BaseAddress + config.CodeOffset,
|
||||
@ -49,18 +56,25 @@ func New(code []byte, data []byte) *ELF {
|
||||
SizeInMemory: int64(len(code)),
|
||||
Align: config.Align,
|
||||
},
|
||||
Code: code,
|
||||
Data: data,
|
||||
PadCode: bytes.Repeat([]byte{0}, 8),
|
||||
Code: code,
|
||||
PadData: 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.ProgramHeader)
|
||||
writer.Write([]byte{0, 0, 0, 0, 0, 0, 0, 0})
|
||||
binary.Write(writer, binary.LittleEndian, &elf.CodeHeader)
|
||||
writer.Write(elf.PadCode)
|
||||
writer.Write(elf.Code)
|
||||
writer.Write(elf.PadData)
|
||||
writer.Write(elf.Data)
|
||||
}
|
||||
|
Reference in New Issue
Block a user