generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 2
/
os.go
95 lines (78 loc) · 2.23 KB
/
os.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package obj
import (
`errors`
`io`
`os`
)
// OS reprensets the target operating system.
type OS uint8
const (
Unsupported OS = iota
MacOS
Linux
)
var builderTab = [256]func(io.Writer, *Arch, []byte, uintptr, uintptr) error {
MacOS: buildMachO,
Linux: buildELF,
}
func (self OS) String() string {
switch self {
case MacOS : return "macOS"
case Linux : return "Linux"
default : return "???"
}
}
func (self OS) build(fp *os.File, arch *Arch, code []byte, base uintptr, entry uintptr) error {
err := error(nil)
file := fp.Name()
/* compile the code */
if builderTab[self] != nil {
err = buildMachO(fp, arch, code, base, entry)
} else {
err = errors.New("unsupported operating system")
}
/* close the file, check for errors */
if _ = fp.Close(); err == nil {
return nil
}
/* remove the file when error occures */
_ = os.Remove(file)
return err
}
// Compile wraps the machine code into arch-specific object file.
func (self OS) Compile(name string, arch *Arch, code []byte, base uintptr, entry uintptr) error {
if fp, err := os.Create(name); err != nil {
return err
} else if err = self.build(fp, arch, code, base, entry); err != nil {
return err
} else {
return nil
}
}
// CompileAndLink is like Compile, but also links the generated object file into an executable.
func (self OS) CompileAndLink(name string, arch *Arch, code []byte, base uintptr, entry uintptr) error {
var fp *os.File
var err error
/* can only link for target OS */
if self != CurrentOS {
return errors.New("cannot link for other operating systems")
}
/* create a temporary file for object code */
if fp, err = os.CreateTemp("", "iasm-object-*"); err != nil {
return err
}
/* compile the code */
if err = self.build(fp, arch, code, base, entry); err != nil {
return err
}
/* link the target, and remove the intermediate file */
err = link(name, fp.Name())
_ = os.Remove(fp.Name())
/* check for errors */
if err == nil {
return nil
}
/* remove the output file if error occures */
_ = os.Remove(name)
return err
}