Simplified file structure
This commit is contained in:
26
src/config/config.go
Normal file
26
src/config/config.go
Normal file
@ -0,0 +1,26 @@
|
||||
package config
|
||||
|
||||
const (
|
||||
// This is the absolute virtual minimum address we can load a program at, see `sysctl vm.mmap_min_addr`.
|
||||
MinAddress = 0x10000
|
||||
|
||||
// The base address is the virtual address for our ELF file.
|
||||
BaseAddress = 0x40 * MinAddress
|
||||
|
||||
// The code offset is the offset of the executable machine code within the file.
|
||||
CodeOffset = 64 + 56 + 56
|
||||
|
||||
// Align decides the alignment of the sections and it must be a multiple of the page size.
|
||||
Align = 0x1000
|
||||
)
|
||||
|
||||
var (
|
||||
// Shows the assembly instructions at the end of the compilation.
|
||||
Assembler = false
|
||||
|
||||
// Calculates the result of operations on constants at compile time.
|
||||
ConstantFold = true
|
||||
|
||||
// Skips writing the executable to disk.
|
||||
Dry = false
|
||||
)
|
59
src/config/init.go
Normal file
59
src/config/init.go
Normal file
@ -0,0 +1,59 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
var (
|
||||
Executable string
|
||||
Root string
|
||||
Library string
|
||||
WorkingDirectory string
|
||||
)
|
||||
|
||||
func init() {
|
||||
debug.SetGCPercent(-1)
|
||||
|
||||
var err error
|
||||
Executable, err = os.Executable()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
WorkingDirectory, err = os.Getwd()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Root = filepath.Dir(Executable)
|
||||
Library = filepath.Join(Root, "lib")
|
||||
stat, err := os.Stat(Library)
|
||||
|
||||
if os.IsNotExist(err) || stat == nil || !stat.IsDir() {
|
||||
findLibrary()
|
||||
}
|
||||
}
|
||||
|
||||
func findLibrary() {
|
||||
dir := WorkingDirectory
|
||||
|
||||
for {
|
||||
Library = path.Join(dir, "lib")
|
||||
stat, err := os.Stat(Library)
|
||||
|
||||
if !os.IsNotExist(err) && stat != nil && stat.IsDir() {
|
||||
return
|
||||
}
|
||||
|
||||
if dir == "/" {
|
||||
panic("standard library not found")
|
||||
}
|
||||
|
||||
dir = filepath.Dir(dir)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user