mirror of
https://github.com/ziglang/zig.git
synced 2025-12-16 03:03:09 +00:00
this reverts 5c04730534ea7933855429c5fc5dc7b22eba7bc2. sadly the quality of the intel dialect in llvm's assembly parser has many frustrating bugs, and generally has unfortunate syntax. the plan is to use AT&T for now since it at least works, and eventually zig will have its own assembly parser for x86 and it will be as close to NASM as possible.
61 lines
1.6 KiB
Zig
61 lines
1.6 KiB
Zig
// This file is in a package which has the root source file exposed as "@root".
|
|
// It is included in the compilation unit when exporting an executable.
|
|
|
|
const root = @import("@root");
|
|
const std = @import("std");
|
|
|
|
const want_main_symbol = std.target.linking_libc;
|
|
const want_start_symbol = !want_main_symbol;
|
|
|
|
const exit = std.os.posix.exit;
|
|
|
|
var argc_ptr: &usize = undefined;
|
|
|
|
export nakedcc fn _start() -> noreturn {
|
|
if (!want_start_symbol) {
|
|
@setGlobalLinkage(_start, GlobalLinkage.Internal);
|
|
unreachable;
|
|
}
|
|
|
|
switch (@compileVar("arch")) {
|
|
Arch.x86_64 => {
|
|
argc_ptr = asm("lea (%%rsp), %[argc]": [argc] "=r" (-> &usize));
|
|
},
|
|
Arch.i386 => {
|
|
argc_ptr = asm("lea (%%esp), %[argc]": [argc] "=r" (-> &usize));
|
|
},
|
|
else => @compileError("unsupported arch"),
|
|
}
|
|
callMainAndExit()
|
|
}
|
|
|
|
fn callMainAndExit() -> noreturn {
|
|
const argc = *argc_ptr;
|
|
const argv = @ptrCast(&&u8, &argc_ptr[1]);
|
|
const envp = @ptrCast(&?&u8, &argv[argc + 1]);
|
|
callMain(argc, argv, envp) %% exit(1);
|
|
exit(0);
|
|
}
|
|
|
|
fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {
|
|
std.os.args.raw = argv[0...argc];
|
|
|
|
var env_count: usize = 0;
|
|
while (envp[env_count] != null; env_count += 1) {}
|
|
std.os.environ_raw = @ptrCast(&&u8, envp)[0...env_count];
|
|
|
|
std.debug.user_main_fn = root.main;
|
|
|
|
return root.main();
|
|
}
|
|
|
|
export fn main(c_argc: i32, c_argv: &&u8, c_envp: &?&u8) -> i32 {
|
|
if (!want_main_symbol) {
|
|
@setGlobalLinkage(main, GlobalLinkage.Internal);
|
|
unreachable;
|
|
}
|
|
|
|
callMain(usize(c_argc), c_argv, c_envp) %% return 1;
|
|
return 0;
|
|
}
|