Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macho: fix a sad typo in calculating the address of a TLV pointer #126

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/MachO/synthetic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ pub const TlvPtrSection = struct {
pub fn getAddress(tlv: TlvPtrSection, index: Index, macho_file: *MachO) u64 {
assert(index < tlv.symbols.items.len);
const header = macho_file.sections.items(.header)[macho_file.tlv_ptr_sect_index.?];
return header.addr + index * @sizeOf(u64) * 3;
return header.addr + index * @sizeOf(u64);
}

pub fn size(tlv: TlvPtrSection) usize {
Expand Down
69 changes: 68 additions & 1 deletion test/macho.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub fn addMachOTests(b: *Build, options: common.Options) *Step {
macho_step.dependOn(testThunks(b, opts));
macho_step.dependOn(testTls(b, opts));
macho_step.dependOn(testTlsLargeTbss(b, opts));
macho_step.dependOn(testTlsPointers(b, opts));
macho_step.dependOn(testTwoLevelNamespace(b, opts));
macho_step.dependOn(testUndefinedFlag(b, opts));
macho_step.dependOn(testUnwindInfo(b, opts));
Expand Down Expand Up @@ -2115,7 +2116,7 @@ fn testReexportsZig(b: *Build, opts: Options) *Step {
\\ return x;
\\}
\\comptime {
\\ @export(foo, .{ .name = "bar", .linkage = .Strong });
\\ @export(foo, .{ .name = "bar", .linkage = .strong });
\\}
);

Expand Down Expand Up @@ -2922,6 +2923,72 @@ fn testTlsLargeTbss(b: *Build, opts: Options) *Step {
return test_step;
}

// https://github.com/ziglang/zig/issues/19221
fn testTlsPointers(b: *Build, opts: Options) *Step {
const test_step = b.step("test-macho-tls-pointers", "");

const includes = WriteFile.create(b);
_ = includes.add("foo.h",
\\template<typename just4fun>
\\struct Foo {
\\
\\public:
\\ static int getVar() {
\\ static int thread_local var = 0;
\\ ++var;
\\ return var;
\\}
\\};
);

const bar_o = cc(b, "bar.o", opts);
bar_o.addCppSource(
\\#include "foo.h"
\\int bar() {
\\ int v1 = Foo<int>::getVar();
\\ return v1;
\\}
);
bar_o.addArgs(&.{ "-c", "-std=c++17" });
bar_o.addPrefixedDirectorySource("-I", includes.getDirectory());

const baz_o = cc(b, "baz.o", opts);
baz_o.addCppSource(
\\#include "foo.h"
\\int baz() {
\\ int v1 = Foo<unsigned>::getVar();
\\ return v1;
\\}
);
baz_o.addArgs(&.{ "-c", "-std=c++17" });
baz_o.addPrefixedDirectorySource("-I", includes.getDirectory());

const main_o = cc(b, "main.o", opts);
main_o.addCppSource(
\\extern int bar();
\\extern int baz();
\\int main() {
\\ int v1 = bar();
\\ int v2 = baz();
\\ return v1 != v2;
\\}
);
main_o.addArgs(&.{ "-c", "-std=c++17" });
main_o.addPrefixedDirectorySource("-I", includes.getDirectory());

const exe = cc(b, "a.out", opts);
exe.addFileSource(bar_o.getFile());
exe.addFileSource(baz_o.getFile());
exe.addFileSource(main_o.getFile());
exe.addArg("-lc++");

const run = exe.run();
run.expectExitCode(0);
test_step.dependOn(run.step());

return test_step;
}

fn testUndefinedFlag(b: *Build, opts: Options) *Step {
const test_step = b.step("test-macho-undefined-flag", "");

Expand Down
Loading