authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-24 19:51:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-24 19:51:24-07:00
log479f259ea4799583f4062b2dffb7d9a289f7e18b
treee96bd2a15781dc517740b695b132974685d0f3a5
parenta1fb10b766bcf05bb8e40bf77fd538c5dbdede97

make start code more versatile

* always align the stack to 16. I saw an instance on x86_64 linux where it was needed. * detect at runtime if being interpreted by a dynamic loader and if so avoid clobbering the fs register.

2 files changed, 16 insertions(+), 6 deletions(-)

lib/std/dynamic_library.zig+7-1
...@@ -59,8 +59,14 @@ const RDebug = extern struct {...@@ -59,8 +59,14 @@ const RDebug = extern struct {
59 r_ldbase: usize,59 r_ldbase: usize,
60};60};
6161
62/// TODO make it possible to reference this same external symbol 2x so we don't need this
63/// helper function.
64pub fn get_DYNAMIC() ?[*]elf.Dyn {
65 return @extern([*]elf.Dyn, .{ .name = "_DYNAMIC", .linkage = .Weak });
66}
67
62pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {68pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
63 const _DYNAMIC = @extern([*]elf.Dyn, .{ .name = "_DYNAMIC", .linkage = .Weak }) orelse {69 const _DYNAMIC = get_DYNAMIC() orelse {
64 // No PT_DYNAMIC means this is either a statically-linked program or a70 // No PT_DYNAMIC means this is either a statically-linked program or a
65 // badly corrupted dynamically-linked one.71 // badly corrupted dynamically-linked one.
66 return LinkMap.Iterator{ .current = null };72 return LinkMap.Iterator{ .current = null };
lib/std/start.zig+9-5
...@@ -187,9 +187,8 @@ fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {...@@ -187,9 +187,8 @@ fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {
187187
188// TODO https://github.com/ziglang/zig/issues/265188// TODO https://github.com/ziglang/zig/issues/265
189fn posixCallMainAndExit() noreturn {189fn posixCallMainAndExit() noreturn {
190 if (builtin.os.tag == .freebsd) {190 @setAlignStack(16);
191 @setAlignStack(16);191
192 }
193 const argc = argc_argv_ptr[0];192 const argc = argc_argv_ptr[0];
194 const argv = @ptrCast([*][*:0]u8, argc_argv_ptr + 1);193 const argv = @ptrCast([*][*:0]u8, argc_argv_ptr + 1);
195194
...@@ -208,8 +207,13 @@ fn posixCallMainAndExit() noreturn {...@@ -208,8 +207,13 @@ fn posixCallMainAndExit() noreturn {
208 @import("os/linux/start_pie.zig").apply_relocations();207 @import("os/linux/start_pie.zig").apply_relocations();
209 }208 }
210209
211 // Initialize the TLS area210 // Initialize the TLS area. We do a runtime check here to make sure
212 std.os.linux.tls.initStaticTLS();211 // this code is truly being statically executed and not inside a dynamic
212 // loader, otherwise this would clobber the thread ID register.
213 const is_dynamic = @import("dynamic_library.zig").get_DYNAMIC() != null;
214 if (!is_dynamic) {
215 std.os.linux.tls.initStaticTLS();
216 }
213217
214 // TODO This is disabled because what should we do when linking libc and this code218 // TODO This is disabled because what should we do when linking libc and this code
215 // does not execute? And also it's causing a test failure in stack traces in release modes.219 // does not execute? And also it's causing a test failure in stack traces in release modes.