authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-02 20:59:17+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-02-02 20:59:17+01:00
log7694361832090198ed6d4e4fa930990a72813642
treea824ef65e7fc6110e6142acb4e43d3a277888686
parent3eb8d01f522cf23d484411794ac10777b3de1cfa
parent8f3be0e04f47b250eb84029ff524130b8411daf8
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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(+), 33 deletions(-)

src/Sema.zig+11-5
......@@ -5567,11 +5567,6 @@ fn funcCommon(
55675567 if (opt_lib_name) |lib_name| blk: {
55685568 const lib_name_src: LazySrcLoc = .{ .node_offset_lib_name = src_node_offset };
55695569 log.debug("extern fn symbol expected in lib '{s}'", .{lib_name});
5570 mod.comp.stage1AddLinkLib(lib_name) catch |err| {
5571 return sema.fail(block, lib_name_src, "unable to add link lib '{s}': {s}", .{
5572 lib_name, @errorName(err),
5573 });
5574 };
55755570 if (target_util.is_libc_lib_name(target, lib_name)) {
55765571 if (!mod.comp.bin_file.options.link_libc) {
55775572 return sema.fail(
......@@ -5581,6 +5576,7 @@ fn funcCommon(
55815576 .{},
55825577 );
55835578 }
5579 mod.comp.bin_file.options.link_libc = true;
55845580 break :blk;
55855581 }
55865582 if (target_util.is_libcpp_lib_name(target, lib_name)) {
......@@ -5592,6 +5588,11 @@ fn funcCommon(
55925588 .{},
55935589 );
55945590 }
5591 mod.comp.bin_file.options.link_libcpp = true;
5592 break :blk;
5593 }
5594 if (mem.eql(u8, lib_name, "unwind")) {
5595 mod.comp.bin_file.options.link_libunwind = true;
55955596 break :blk;
55965597 }
55975598 if (!target.isWasm() and !mod.comp.bin_file.options.pic) {
......@@ -5602,6 +5603,11 @@ fn funcCommon(
56025603 .{ lib_name, lib_name },
56035604 );
56045605 }
5606 mod.comp.stage1AddLinkLib(lib_name) catch |err| {
5607 return sema.fail(block, lib_name_src, "unable to add link lib '{s}': {s}", .{
5608 lib_name, @errorName(err),
5609 });
5610 };
56055611 }
56065612
56075613 if (is_extern) {
src/link/MachO.zig+14-9
......@@ -144,9 +144,6 @@ objc_data_section_index: ?u16 = null,
144144rustc_section_index: ?u16 = null,
145145rustc_section_size: u64 = 0,
146146
147bss_file_offset: u32 = 0,
148tlv_bss_file_offset: u32 = 0,
149
150147locals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
151148globals: std.ArrayListUnmanaged(macho.nlist_64) = .{},
152149undefs: std.ArrayListUnmanaged(macho.nlist_64) = .{},
......@@ -383,7 +380,8 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*MachO {
383380 // Adhoc code signature is required when targeting aarch64-macos either directly or indirectly via the simulator
384381 // ABI such as aarch64-ios-simulator, etc.
385382 const requires_adhoc_codesig = cpu_arch == .aarch64 and (os_tag == .macos or abi == .simulator);
386 const needs_prealloc = !(build_options.is_stage1 and options.use_stage1);
383 const use_stage1 = build_options.is_stage1 and options.use_stage1;
384 const needs_prealloc = !(use_stage1 or options.cache_mode == .whole);
387385
388386 const self = try gpa.create(MachO);
389387 errdefer gpa.destroy(self);
......@@ -401,7 +399,6 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*MachO {
401399 };
402400
403401 const use_llvm = build_options.have_llvm and options.use_llvm;
404 const use_stage1 = build_options.is_stage1 and options.use_stage1;
405402 if (use_llvm and !use_stage1) {
406403 self.llvm_object = try LlvmObject.create(gpa, options);
407404 }
......@@ -2158,7 +2155,8 @@ fn writeAtoms(self: *MachO) !void {
21582155 const sect = seg.sections.items[match.sect];
21592156 var atom: *Atom = entry.value_ptr.*;
21602157
2161 if (sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL) continue;
2158 // TODO handle zerofill in stage2
2159 // if (sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL) continue;
21622160
21632161 log.debug("writing atoms in {s},{s}", .{ sect.segName(), sect.sectName() });
21642162
......@@ -4756,9 +4754,12 @@ fn allocateSegment(self: *MachO, index: u16, offset: u64) !void {
47564754 var start: u64 = offset;
47574755 for (seg.sections.items) |*sect, sect_id| {
47584756 const is_zerofill = sect.flags == macho.S_ZEROFILL or sect.flags == macho.S_THREAD_LOCAL_ZEROFILL;
4757 const use_stage1 = build_options.is_stage1 and self.base.options.use_stage1;
47594758 const alignment = try math.powi(u32, 2, sect.@"align");
47604759 const start_aligned = mem.alignForwardGeneric(u64, start, alignment);
4761 sect.offset = if (is_zerofill) 0 else @intCast(u32, seg.inner.fileoff + start_aligned);
4760
4761 // TODO handle zerofill sections in stage2
4762 sect.offset = if (is_zerofill and use_stage1) 0 else @intCast(u32, seg.inner.fileoff + start_aligned);
47624763 sect.addr = seg.inner.vmaddr + start_aligned;
47634764
47644765 // Recalculate section size given the allocated start address
......@@ -4786,7 +4787,7 @@ fn allocateSegment(self: *MachO, index: u16, offset: u64) !void {
47864787
47874788 start = start_aligned + sect.size;
47884789
4789 if (!is_zerofill) {
4790 if (!(is_zerofill and use_stage1)) {
47904791 seg.inner.filesize = start;
47914792 }
47924793 seg.inner.vmsize = start;
......@@ -4834,7 +4835,11 @@ fn initSection(
48344835
48354836 sect.addr = seg.inner.vmaddr + off - seg.inner.fileoff;
48364837
4837 if (opts.flags != macho.S_ZEROFILL and opts.flags != macho.S_THREAD_LOCAL_ZEROFILL) {
4838 const is_zerofill = opts.flags == macho.S_ZEROFILL or opts.flags == macho.S_THREAD_LOCAL_ZEROFILL;
4839 const use_stage1 = build_options.is_stage1 and self.base.options.use_stage1;
4840
4841 // TODO handle zerofill in stage2
4842 if (!(is_zerofill and use_stage1)) {
48384843 sect.offset = @intCast(u32, off);
48394844 }
48404845 }
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;
......@@ -2117,8 +2117,8 @@ fn addMacOsTestCases(ctx: *TestContext) !void {
21172117
21182118 // Regular old hello world
21192119 case.addCompareOutput(
2120 \\extern fn write(usize, usize, usize) usize;
2121 \\extern fn exit(usize) noreturn;
2120 \\extern "c" fn write(usize, usize, usize) usize;
2121 \\extern "c" fn exit(usize) noreturn;
21222122 \\
21232123 \\pub export fn main() noreturn {
21242124 \\ print();
......@@ -2137,7 +2137,7 @@ fn addMacOsTestCases(ctx: *TestContext) !void {
21372137
21382138 // Now using start.zig without an explicit extern exit fn
21392139 case.addCompareOutput(
2140 \\extern fn write(usize, usize, usize) usize;
2140 \\extern "c" fn write(usize, usize, usize) usize;
21412141 \\
21422142 \\pub fn main() void {
21432143 \\ print();
......@@ -2154,7 +2154,7 @@ fn addMacOsTestCases(ctx: *TestContext) !void {
21542154
21552155 // Print it 4 times and force growth and realloc.
21562156 case.addCompareOutput(
2157 \\extern fn write(usize, usize, usize) usize;
2157 \\extern "c" fn write(usize, usize, usize) usize;
21582158 \\
21592159 \\pub fn main() void {
21602160 \\ print();
......@@ -2178,7 +2178,7 @@ fn addMacOsTestCases(ctx: *TestContext) !void {
21782178
21792179 // Print it once, and change the message.
21802180 case.addCompareOutput(
2181 \\extern fn write(usize, usize, usize) usize;
2181 \\extern "c" fn write(usize, usize, usize) usize;
21822182 \\
21832183 \\pub fn main() void {
21842184 \\ print();
......@@ -2195,7 +2195,7 @@ fn addMacOsTestCases(ctx: *TestContext) !void {
21952195
21962196 // Now we print it twice.
21972197 case.addCompareOutput(
2198 \\extern fn write(usize, usize, usize) usize;
2198 \\extern "c" fn write(usize, usize, usize) usize;
21992199 \\
22002200 \\pub fn main() void {
22012201 \\ print();