authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-02 20:59:17+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-02 22:10:59-07:00
logd107ef86973692123892bcd5e47ce1b17982283a
tree23959c8a254ef0eb9144a518f407ae59fd1302f2
parentcee0f082df0bb0f90c7ee38c98ba8071c3be8d0c

Merge pull request #10769 from ziglang/link-lib-fixes

stage2: handle name-qualified imports in sema, add a zerofill sections workaround to incremental macho

4 files changed, 44 insertions(+), 32 deletions(-)

src/Sema.zig+11-5
......@@ -5145,11 +5145,6 @@ fn funcCommon(
51455145 if (opt_lib_name) |lib_name| blk: {
51465146 const lib_name_src: LazySrcLoc = .{ .node_offset_lib_name = src_node_offset };
51475147 log.debug("extern fn symbol expected in lib '{s}'", .{lib_name});
5148 mod.comp.stage1AddLinkLib(lib_name) catch |err| {
5149 return sema.fail(block, lib_name_src, "unable to add link lib '{s}': {s}", .{
5150 lib_name, @errorName(err),
5151 });
5152 };
51535148 const target = mod.getTarget();
51545149 if (target_util.is_libc_lib_name(target, lib_name)) {
51555150 if (!mod.comp.bin_file.options.link_libc) {
......@@ -5160,6 +5155,7 @@ fn funcCommon(
51605155 .{},
51615156 );
51625157 }
5158 mod.comp.bin_file.options.link_libc = true;
51635159 break :blk;
51645160 }
51655161 if (target_util.is_libcpp_lib_name(target, lib_name)) {
......@@ -5171,6 +5167,11 @@ fn funcCommon(
51715167 .{},
51725168 );
51735169 }
5170 mod.comp.bin_file.options.link_libcpp = true;
5171 break :blk;
5172 }
5173 if (mem.eql(u8, lib_name, "unwind")) {
5174 mod.comp.bin_file.options.link_libunwind = true;
51745175 break :blk;
51755176 }
51765177 if (!target.isWasm() and !mod.comp.bin_file.options.pic) {
......@@ -5181,6 +5182,11 @@ fn funcCommon(
51815182 .{ lib_name, lib_name },
51825183 );
51835184 }
5185 mod.comp.stage1AddLinkLib(lib_name) catch |err| {
5186 return sema.fail(block, lib_name_src, "unable to add link lib '{s}': {s}", .{
5187 lib_name, @errorName(err),
5188 });
5189 };
51845190 }
51855191
51865192 if (is_extern) {
src/link/MachO.zig+14-8
......@@ -143,9 +143,6 @@ objc_data_section_index: ?u16 = null,
143143rustc_section_index: ?u16 = null,
144144rustc_section_size: u64 = 0,
145145
146bss_file_offset: u32 = 0,
147tlv_bss_file_offset: u32 = 0,
148
149146locals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
150147globals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
151148undefs: std.ArrayListUnmanaged(macho.nlist_64) = .{},
......@@ -384,7 +381,8 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*MachO {
384381 // Adhoc code signature is required when targeting aarch64-macos either directly or indirectly via the simulator
385382 // ABI such as aarch64-ios-simulator, etc.
386383 const requires_adhoc_codesig = cpu_arch == .aarch64 and (os_tag == .macos or abi == .simulator);
387 const needs_prealloc = !(build_options.is_stage1 and options.use_stage1);
384 const use_stage1 = build_options.is_stage1 and options.use_stage1;
385 const needs_prealloc = !(use_stage1 or options.cache_mode == .whole);
388386
389387 self.* = .{
390388 .base = .{
......@@ -2129,7 +2127,8 @@ fn writeAtoms(self: *MachO) !void {
21292127 const sect = seg.sections.items[match.sect];
21302128 var atom: *Atom = entry.value_ptr.*;
21312129
2132 if (sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL) continue;
2130 // TODO handle zerofill in stage2
2131 // if (sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL) continue;
21332132
21342133 log.debug("writing atoms in {s},{s}", .{ sect.segName(), sect.sectName() });
21352134
......@@ -4566,9 +4565,12 @@ fn allocateSegment(self: *MachO, index: u16, offset: u64) !void {
45664565 var start: u64 = offset;
45674566 for (seg.sections.items) |*sect, sect_id| {
45684567 const is_zerofill = sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL;
4568 const use_stage1 = build_options.is_stage1 and self.base.options.use_stage1;
45694569 const alignment = try math.powi(u32, 2, sect.@"align");
45704570 const start_aligned = mem.alignForwardGeneric(u64, start, alignment);
4571 sect.offset = if (is_zerofill) 0 else @intCast(u32, seg.inner.fileoff + start_aligned);
4571
4572 // TODO handle zerofill sections in stage2
4573 sect.offset = if (is_zerofill and use_stage1) 0 else @intCast(u32, seg.inner.fileoff + start_aligned);
45724574 sect.addr = seg.inner.vmaddr + start_aligned;
45734575
45744576 // Recalculate section size given the allocated start address
......@@ -4596,7 +4598,7 @@ fn allocateSegment(self: *MachO, index: u16, offset: u64) !void {
45964598
45974599 start = start_aligned + sect.size;
45984600
4599 if (!is_zerofill) {
4601 if (!(is_zerofill and use_stage1)) {
46004602 seg.inner.filesize = start;
46014603 }
46024604 seg.inner.vmsize = start;
......@@ -4644,7 +4646,11 @@ fn initSection(
46444646
46454647 sect.addr = seg.inner.vmaddr + off - seg.inner.fileoff;
46464648
4647 if (opts.flags != macho.S_ZEROFILL and opts.flags != macho.S_THREAD_LOCAL_ZEROFILL) {
4649 const is_zerofill = opts.flags == macho.S_ZEROFILL or opts.flags == macho.S_THREAD_LOCAL_ZEROFILL;
4650 const use_stage1 = build_options.is_stage1 and self.base.options.use_stage1;
4651
4652 // TODO handle zerofill in stage2
4653 if (!(is_zerofill and use_stage1)) {
46484654 sect.offset = @intCast(u32, off);
46494655 }
46504656 }
test/stage2/aarch64.zig+6-6
......@@ -121,8 +121,8 @@ pub fn addCases(ctx: *TestContext) !void {
121121
122122 // Regular old hello world
123123 case.addCompareOutput(
124 \\extern fn write(usize, usize, usize) usize;
125 \\extern fn exit(usize) noreturn;
124 \\extern "c" fn write(usize, usize, usize) usize;
125 \\extern "c" fn exit(usize) noreturn;
126126 \\
127127 \\pub export fn main() noreturn {
128128 \\ print();
......@@ -141,7 +141,7 @@ pub fn addCases(ctx: *TestContext) !void {
141141
142142 // Now using start.zig without an explicit extern exit fn
143143 case.addCompareOutput(
144 \\extern fn write(usize, usize, usize) usize;
144 \\extern "c" fn write(usize, usize, usize) usize;
145145 \\
146146 \\pub fn main() void {
147147 \\ print();
......@@ -158,7 +158,7 @@ pub fn addCases(ctx: *TestContext) !void {
158158
159159 // Print it 4 times and force growth and realloc.
160160 case.addCompareOutput(
161 \\extern fn write(usize, usize, usize) usize;
161 \\extern "c" fn write(usize, usize, usize) usize;
162162 \\
163163 \\pub fn main() void {
164164 \\ print();
......@@ -182,7 +182,7 @@ pub fn addCases(ctx: *TestContext) !void {
182182
183183 // Print it once, and change the message.
184184 case.addCompareOutput(
185 \\extern fn write(usize, usize, usize) usize;
185 \\extern "c" fn write(usize, usize, usize) usize;
186186 \\
187187 \\pub fn main() void {
188188 \\ print();
......@@ -199,7 +199,7 @@ pub fn addCases(ctx: *TestContext) !void {
199199
200200 // Now we print it twice.
201201 case.addCompareOutput(
202 \\extern fn write(usize, usize, usize) usize;
202 \\extern "c" fn write(usize, usize, usize) usize;
203203 \\
204204 \\pub fn main() void {
205205 \\ print();
test/stage2/x86_64.zig+13-13
......@@ -328,7 +328,7 @@ pub fn addCases(ctx: *TestContext) !void {
328328 .macos => {
329329 // While loops
330330 case.addCompareOutput(
331 \\extern fn write(usize, usize, usize) usize;
331 \\extern "c" fn write(usize, usize, usize) usize;
332332 \\
333333 \\pub fn main() void {
334334 \\ var i: u32 = 0;
......@@ -349,7 +349,7 @@ pub fn addCases(ctx: *TestContext) !void {
349349
350350 // inline while requires the condition to be comptime known.
351351 case.addError(
352 \\extern fn write(usize, usize, usize) usize;
352 \\extern "c" fn write(usize, usize, usize) usize;
353353 \\
354354 \\pub fn main() void {
355355 \\ var i: u32 = 0;
......@@ -652,7 +652,7 @@ pub fn addCases(ctx: *TestContext) !void {
652652 .macos => {
653653 // Basic for loop
654654 case.addCompareOutput(
655 \\extern fn write(usize, usize, usize) usize;
655 \\extern "c" fn write(usize, usize, usize) usize;
656656 \\
657657 \\pub fn main() void {
658658 \\ for ("hello") |_| print();
......@@ -736,7 +736,7 @@ pub fn addCases(ctx: *TestContext) !void {
736736 }),
737737 .macos => try case.files.append(.{
738738 .src =
739 \\extern fn write(usize, usize, usize) usize;
739 \\extern "c" fn write(usize, usize, usize) usize;
740740 \\
741741 \\pub fn print() void {
742742 \\ _ = write(1, @ptrToInt("Hello, World!\n"), 14);
......@@ -814,7 +814,7 @@ pub fn addCases(ctx: *TestContext) !void {
814814 }),
815815 .macos => try case.files.append(.{
816816 .src =
817 \\extern fn write(usize, usize, usize) usize;
817 \\extern "c" fn write(usize, usize, usize) usize;
818818 \\fn print() void {
819819 \\ _ = write(1, @ptrToInt("Hello, World!\n"), 14);
820820 \\}
......@@ -1478,7 +1478,7 @@ pub fn addCases(ctx: *TestContext) !void {
14781478 \\}
14791479 , "HelloHello, World!\n"),
14801480 .macos => case.addCompareOutput(
1481 \\extern fn write(usize, usize, usize) usize;
1481 \\extern "c" fn write(usize, usize, usize) usize;
14821482 \\
14831483 \\pub fn main() void {
14841484 \\ comptime var len: u32 = 5;
......@@ -1550,7 +1550,7 @@ pub fn addCases(ctx: *TestContext) !void {
15501550 \\}
15511551 , "HeHelHellHello"),
15521552 .macos => case.addCompareOutput(
1553 \\extern fn write(usize, usize, usize) usize;
1553 \\extern "c" fn write(usize, usize, usize) usize;
15541554 \\
15551555 \\pub fn main() void {
15561556 \\ comptime var i: u64 = 2;
......@@ -1877,8 +1877,8 @@ fn addMacOsTestCases(ctx: *TestContext) !void {
18771877
18781878 // Regular old hello world
18791879 case.addCompareOutput(
1880 \\extern fn write(usize, usize, usize) usize;
1881 \\extern fn exit(usize) noreturn;
1880 \\extern "c" fn write(usize, usize, usize) usize;
1881 \\extern "c" fn exit(usize) noreturn;
18821882 \\
18831883 \\pub export fn main() noreturn {
18841884 \\ print();
......@@ -1897,7 +1897,7 @@ fn addMacOsTestCases(ctx: *TestContext) !void {
18971897
18981898 // Now using start.zig without an explicit extern exit fn
18991899 case.addCompareOutput(
1900 \\extern fn write(usize, usize, usize) usize;
1900 \\extern "c" fn write(usize, usize, usize) usize;
19011901 \\
19021902 \\pub fn main() void {
19031903 \\ print();
......@@ -1914,7 +1914,7 @@ fn addMacOsTestCases(ctx: *TestContext) !void {
19141914
19151915 // Print it 4 times and force growth and realloc.
19161916 case.addCompareOutput(
1917 \\extern fn write(usize, usize, usize) usize;
1917 \\extern "c" fn write(usize, usize, usize) usize;
19181918 \\
19191919 \\pub fn main() void {
19201920 \\ print();
......@@ -1938,7 +1938,7 @@ fn addMacOsTestCases(ctx: *TestContext) !void {
19381938
19391939 // Print it once, and change the message.
19401940 case.addCompareOutput(
1941 \\extern fn write(usize, usize, usize) usize;
1941 \\extern "c" fn write(usize, usize, usize) usize;
19421942 \\
19431943 \\pub fn main() void {
19441944 \\ print();
......@@ -1955,7 +1955,7 @@ fn addMacOsTestCases(ctx: *TestContext) !void {
19551955
19561956 // Now we print it twice.
19571957 case.addCompareOutput(
1958 \\extern fn write(usize, usize, usize) usize;
1958 \\extern "c" fn write(usize, usize, usize) usize;
19591959 \\
19601960 \\pub fn main() void {
19611961 \\ print();