Separated code and data

This commit is contained in:
Eduard Urbach 2023-10-20 13:22:06 +02:00
parent aab33fe86d
commit 0fe419da86
No known key found for this signature in database
GPG key ID: C874F672B1AF20C0
8 changed files with 76 additions and 67 deletions

View file

@ -2,6 +2,7 @@ package build
import (
"bufio"
"bytes"
"os"
"path/filepath"
@ -11,61 +12,62 @@ import (
// Build describes a compiler build.
type Build struct {
ExecutableName string
ExecutablePath string
Name string
Directory string
Code bytes.Buffer
Data bytes.Buffer
WriteExecutable bool
}
// New creates a new build.
func New(directory string) (*Build, error) {
directory, err := filepath.Abs(directory)
func New(directory string) *Build {
return &Build{
Name: filepath.Base(directory),
Directory: directory,
WriteExecutable: true,
}
}
// Run parses the input files and generates an executable file.
func (build *Build) Run() error {
err := build.Compile()
if err != nil {
return nil, err
return err
}
file, err := os.Open(directory)
if build.WriteExecutable {
return writeToDisk(build.Executable(), build.Code.Bytes(), build.Data.Bytes())
}
return nil
}
// Executable returns the path to the executable.
func (build *Build) Executable() string {
return filepath.Join(build.Directory, build.Name)
}
// Compile compiles all the functions.
func (build *Build) Compile() error {
file, err := os.Open(build.Directory)
if err != nil {
return nil, err
return err
}
defer file.Close()
files, err := file.Readdirnames(0)
if err != nil {
return nil, err
return err
}
for _, name := range files {
log.Info.Println(name)
}
executableName := filepath.Base(directory)
build := &Build{
ExecutableName: executableName,
ExecutablePath: filepath.Join(directory, executableName),
WriteExecutable: true,
}
return build, nil
}
// Run parses the input files and generates an executable file.
func (build *Build) Run() error {
code := build.Compile()
if build.WriteExecutable {
return writeToDisk(build.ExecutablePath, code)
}
return nil
}
// Compile compiles all the functions.
func (build *Build) Compile() []byte {
return []byte{
build.Code.Write([]byte{
0xb8, 0x01, 0x00, 0x00, 0x00, // mov eax, 1
0xbf, 0x01, 0x00, 0x00, 0x00, // mov edi, 1
0xbe, 0xa2, 0x00, 0x40, 0x00, // mov esi, 0x4000a2
@ -75,13 +77,14 @@ func (build *Build) Compile() []byte {
0xb8, 0x3c, 0x00, 0x00, 0x00, // mov eax, 60
0xbf, 0x00, 0x00, 0x00, 0x00, // mov edi, 0
0x0f, 0x05, // syscall
})
'H', 'e', 'l', 'l', 'o', '\n',
}
build.Data.Write([]byte{'H', 'e', 'l', 'l', 'o', '\n'})
return nil
}
// writeToDisk writes the executable file to disk.
func writeToDisk(filePath string, code []byte) error {
func writeToDisk(filePath string, code []byte, data []byte) error {
file, err := os.Create(filePath)
if err != nil {
@ -89,7 +92,7 @@ func writeToDisk(filePath string, code []byte) error {
}
buffer := bufio.NewWriter(file)
executable := elf.New(code)
executable := elf.New(code, data)
executable.Write(buffer)
buffer.Flush()