zig/std/special/bootstrap.zig
Andrew Kelley a32b5929cc add stack protector safety when linking libc
* introduce zigrt file. it contains only weak symbols so that
   multiple instances can be merged. it contains __zig_panic
   so that multiple .o files can call the same panic function.
 * remove `@setFnVisible` builtin and add @setGlobalLinkage builtin
   which is more powerful
 * add `@panic` builtin function.
 * fix collision of symbols with extern prototypes and internal
   function names
 * add stack protector safety when linking against libc. To add
   the safety mechanism without libc requires implementing
   Thread Local Storage. See #276
2017-03-26 21:07:07 -04:00

60 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: usize = undefined;
var argv: &&u8 = undefined;
export nakedcc fn _start() -> noreturn {
@setGlobalLinkage(_start, if (want_start_symbol) GlobalLinkage.Strong else GlobalLinkage.Internal);
if (!want_start_symbol) {
unreachable;
}
switch (@compileVar("arch")) {
Arch.x86_64 => {
argc = asm("mov %[argc], [rsp]": [argc] "=r" (-> usize));
argv = asm("lea %[argv], [rsp + 8h]": [argv] "=r" (-> &&u8));
},
Arch.i386 => {
argc = asm("mov %[argc], [esp]": [argc] "=r" (-> usize));
argv = asm("lea %[argv], [esp + 4h]": [argv] "=r" (-> &&u8));
},
else => @compileError("unsupported arch"),
}
callMainAndExit()
}
fn callMain() -> %void {
const args = @alloca([]u8, argc);
for (args) |_, i| {
const ptr = argv[i];
args[i] = ptr[0...std.cstr.len(ptr)];
}
return root.main(args);
}
fn callMainAndExit() -> noreturn {
callMain() %% exit(1);
exit(0);
}
export fn main(c_argc: i32, c_argv: &&u8) -> i32 {
@setGlobalLinkage(main, if (want_main_symbol) GlobalLinkage.Strong else GlobalLinkage.Internal);
if (!want_main_symbol) {
unreachable;
}
argc = usize(c_argc);
argv = c_argv;
callMain() %% return 1;
return 0;
}