-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathvas.v
More file actions
93 lines (81 loc) · 2.35 KB
/
Copy pathvas.v
File metadata and controls
93 lines (81 loc) · 2.35 KB
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
module main
import os
import flag
import lexer
import encoder
import elf
import macho
import pe
fn file_name_without_ext(file_name string) string {
ext_len := os.file_ext(file_name).len
bytes := file_name.bytes()
return if file_name == '-' {
'main'
} else {
bytes[..bytes.len - ext_len].bytestr()
}
}
fn main() {
mut fp := flag.new_flag_parser(os.args)
fp.application('vas')
fp.version('v0.0.0')
fp.skip_executable()
mut out_file := fp.string('o', `o`, 'out_file_none', 'set output file name')
keep_locals := fp.bool('keep-locals', 0, false, 'keeps local symbols (e.g., those starting with `.L`)')
format_flag := fp.string('format', `f`, 'auto', 'output format: elf or macho (default: auto-detect from OS)')
additional_args := fp.finalize() or {
println(fp.usage())
return
}
if additional_args.len < 1 {
println(fp.usage())
return
}
file_name := additional_args[0]
if out_file == 'out_file_none' {
out_file = file_name_without_ext(file_name) + '.o'
}
program := if file_name == '-' {
os.get_raw_lines_joined()
} else {
os.read_file(file_name) or {
eprintln('error: reading file `${file_name}`')
exit(1)
}
}
effective_format := if format_flag == 'auto' {
if os.user_os() == 'macos' { 'macho' } else if os.user_os() == 'windows' { 'pe' } else { 'elf' }
} else {
format_flag
}
valid_formats := ['elf', 'macho', 'pe']
if effective_format !in valid_formats {
eprintln('error: unknown format `${effective_format}` — valid values: ${valid_formats.join(', ')}')
exit(1)
}
mut l := lexer.new(file_name, program)
mut en := encoder.new(mut l, file_name)
en.encode()
en.assign_addresses()
if effective_format == 'macho' {
mut m := macho.new(out_file, keep_locals, en.rela_text_users, en.user_defined_sections, en.user_defined_symbols)
m.collect_rela_symbols()
m.build_symtab_strtab()
m.build_relocations()
m.write_macho()
} else if effective_format == 'pe' {
mut p := pe.new(out_file, keep_locals, en.rela_text_users, en.user_defined_sections, en.user_defined_symbols)
p.collect_rela_symbols()
p.build_symtab_strtab()
p.build_relocations()
p.write_pe()
} else {
mut e := elf.new(out_file, keep_locals, en.rela_text_users, en.user_defined_sections, en.user_defined_symbols)
e.collect_rela_symbols()
e.build_symtab_strtab()
e.rela_text_users()
e.build_shstrtab()
e.build_headers()
e.write_elf()
}
}