Implemented ELF header

This commit is contained in:
2023-10-17 20:29:36 +02:00
parent cae6696c3e
commit 4554fb82cb
7 changed files with 217 additions and 2 deletions

View File

@ -1,6 +1,12 @@
package build
import "path/filepath"
import (
"bufio"
"os"
"path/filepath"
"git.akyoto.dev/cli/q/build/elf"
)
// Build describes a compiler build.
type Build struct {
@ -30,5 +36,37 @@ func New(directory string) (*Build, error) {
// Run parses the input files and generates an executable file.
func (build *Build) Run() error {
if build.WriteExecutable {
sampleCode := []byte{
0xb8, 0x3c, 0x00, 0x00, 0x00, // mov rax, 60
0xbf, 0x00, 0x00, 0x00, 0x00, // mov rdi, 0
0x0f, 0x05, // syscall
}
return writeToDisk(build.ExecutablePath, sampleCode, nil)
}
return nil
}
// writeToDisk writes the executable file to disk.
func writeToDisk(filePath string, code []byte, data []byte) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
buffer := bufio.NewWriter(file)
executable := elf.New()
executable.Write(buffer)
buffer.Flush()
err = file.Close()
if err != nil {
return err
}
return os.Chmod(filePath, 0755)
}