authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-22 20:10:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-22 20:10:57-07:00
log5e6b8e17eff77107af7fb69cf86009bd45222778
treeb98cebeabcb01a44fe04cb3073788b93b55af210
parent70994b13df94ac4a3392decef498724d0e0a0a28
parentf34b4780b7bd52d14df253d0762d9c73db8eb226

Merge remote-tracking branch 'origin/master' into wrangle-writer-buffering


21 files changed, 632 insertions(+), 1654 deletions(-)

lib/compiler/build_runner.zig+1-1
......@@ -708,7 +708,7 @@ fn runStepNames(
708708
709709 const total_count = success_count + failure_count + pending_count + skipped_count;
710710 ttyconf.setColor(w, .cyan) catch {};
711 w.writeAll("Build Summary:") catch {};
711 w.writeAll("\nBuild Summary:") catch {};
712712 ttyconf.setColor(w, .reset) catch {};
713713 w.print(" {d}/{d} steps succeeded", .{ success_count, total_count }) catch {};
714714 if (skipped_count > 0) w.print("; {d} skipped", .{skipped_count}) catch {};
lib/compiler/objcopy.zig+62-907
......@@ -13,6 +13,9 @@ const Server = std.zig.Server;
1313var stdin_buffer: [1024]u8 = undefined;
1414var stdout_buffer: [1024]u8 = undefined;
1515
16var input_buffer: [1024]u8 = undefined;
17var output_buffer: [1024]u8 = undefined;
18
1619pub fn main() !void {
1720 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
1821 defer arena_instance.deinit();
......@@ -145,13 +148,16 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
145148 const input = opt_input orelse fatal("expected input parameter", .{});
146149 const output = opt_output orelse fatal("expected output parameter", .{});
147150
148 var in_file = fs.cwd().openFile(input, .{}) catch |err|
149 fatal("unable to open '{s}': {s}", .{ input, @errorName(err) });
150 defer in_file.close();
151 const input_file = fs.cwd().openFile(input, .{}) catch |err| fatal("failed to open {s}: {t}", .{ input, err });
152 defer input_file.close();
153
154 const stat = input_file.stat() catch |err| fatal("failed to stat {s}: {t}", .{ input, err });
151155
152 const elf_hdr = std.elf.Header.read(in_file) catch |err| switch (err) {
153 error.InvalidElfMagic => fatal("not an ELF file: '{s}'", .{input}),
154 else => fatal("unable to read '{s}': {s}", .{ input, @errorName(err) }),
156 var in: File.Reader = .initSize(input_file, &input_buffer, stat.size);
157
158 const elf_hdr = std.elf.Header.read(&in.interface) catch |err| switch (err) {
159 error.ReadFailed => fatal("unable to read {s}: {t}", .{ input, in.err.? }),
160 else => |e| fatal("invalid elf file: {t}", .{e}),
155161 };
156162
157163 const in_ofmt = .elf;
......@@ -168,16 +174,12 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
168174 }
169175 };
170176
171 const mode = mode: {
172 if (out_fmt != .elf or only_keep_debug)
173 break :mode fs.File.default_mode;
174 if (in_file.stat()) |stat|
175 break :mode stat.mode
176 else |_|
177 break :mode fs.File.default_mode;
178 };
179 var out_file = try fs.cwd().createFile(output, .{ .mode = mode });
180 defer out_file.close();
177 const mode = if (out_fmt != .elf or only_keep_debug) fs.File.default_mode else stat.mode;
178
179 var output_file = try fs.cwd().createFile(output, .{ .mode = mode });
180 defer output_file.close();
181
182 var out = output_file.writer(&output_buffer);
181183
182184 switch (out_fmt) {
183185 .hex, .raw => {
......@@ -192,7 +194,7 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
192194 if (set_section_flags != null)
193195 fatal("zig objcopy: ELF to RAW or HEX copying does not support --set_section_flags", .{});
194196
195 try emitElf(arena, in_file, out_file, elf_hdr, .{
197 try emitElf(arena, &in, &out, elf_hdr, .{
196198 .ofmt = out_fmt,
197199 .only_section = only_section,
198200 .pad_to = pad_to,
......@@ -208,22 +210,13 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
208210 if (pad_to) |_|
209211 fatal("zig objcopy: ELF to ELF copying does not support --pad-to", .{});
210212
211 try stripElf(arena, in_file, out_file, elf_hdr, .{
212 .strip_debug = strip_debug,
213 .strip_all = strip_all,
214 .only_keep_debug = only_keep_debug,
215 .add_debuglink = opt_add_debuglink,
216 .extract_to = opt_extract,
217 .compress_debug = compress_debug_sections,
218 .add_section = add_section,
219 .set_section_alignment = set_section_alignment,
220 .set_section_flags = set_section_flags,
221 });
222 return std.process.cleanExit();
213 fatal("unimplemented", .{});
223214 },
224215 else => fatal("unsupported output object format: {s}", .{@tagName(out_fmt)}),
225216 }
226217
218 try out.end();
219
227220 if (listen) {
228221 var stdin_reader = fs.File.stdin().reader(&stdin_buffer);
229222 var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
......@@ -304,12 +297,12 @@ const SetSectionFlags = struct {
304297
305298fn emitElf(
306299 arena: Allocator,
307 in_file: File,
308 out_file: File,
300 in: *File.Reader,
301 out: *File.Writer,
309302 elf_hdr: elf.Header,
310303 options: EmitRawElfOptions,
311304) !void {
312 var binary_elf_output = try BinaryElfOutput.parse(arena, in_file, elf_hdr);
305 var binary_elf_output = try BinaryElfOutput.parse(arena, in, elf_hdr);
313306 defer binary_elf_output.deinit();
314307
315308 if (options.ofmt == .elf) {
......@@ -328,8 +321,8 @@ fn emitElf(
328321 continue;
329322 }
330323
331 try writeBinaryElfSection(in_file, out_file, section);
332 try padFile(out_file, options.pad_to);
324 try writeBinaryElfSection(in, out, section);
325 try padFile(out, options.pad_to);
333326 return;
334327 }
335328 },
......@@ -342,10 +335,10 @@ fn emitElf(
342335 switch (options.ofmt) {
343336 .raw => {
344337 for (binary_elf_output.sections.items) |section| {
345 try out_file.seekTo(section.binaryOffset);
346 try writeBinaryElfSection(in_file, out_file, section);
338 try out.seekTo(section.binaryOffset);
339 try writeBinaryElfSection(in, out, section);
347340 }
348 try padFile(out_file, options.pad_to);
341 try padFile(out, options.pad_to);
349342 },
350343 .hex => {
351344 if (binary_elf_output.segments.items.len == 0) return;
......@@ -353,15 +346,15 @@ fn emitElf(
353346 return error.InvalidHexfileAddressRange;
354347 }
355348
356 var hex_writer = HexWriter{ .out_file = out_file };
349 var hex_writer = HexWriter{ .out = out };
357350 for (binary_elf_output.segments.items) |segment| {
358 try hex_writer.writeSegment(segment, in_file);
351 try hex_writer.writeSegment(segment, in);
359352 }
360353 if (options.pad_to) |_| {
361354 // Padding to a size in hex files isn't applicable
362355 return error.InvalidArgument;
363356 }
364 try hex_writer.writeEOF();
357 try hex_writer.writeEof();
365358 },
366359 else => unreachable,
367360 }
......@@ -399,7 +392,7 @@ const BinaryElfOutput = struct {
399392 self.segments.deinit(self.allocator);
400393 }
401394
402 pub fn parse(allocator: Allocator, elf_file: File, elf_hdr: elf.Header) !Self {
395 pub fn parse(allocator: Allocator, in: *File.Reader, elf_hdr: elf.Header) !Self {
403396 var self: Self = .{
404397 .segments = .{},
405398 .sections = .{},
......@@ -412,7 +405,7 @@ const BinaryElfOutput = struct {
412405 self.shstrtab = blk: {
413406 if (elf_hdr.shstrndx >= elf_hdr.shnum) break :blk null;
414407
415 var section_headers = elf_hdr.section_header_iterator(&elf_file);
408 var section_headers = elf_hdr.iterateSectionHeaders(in);
416409
417410 var section_counter: usize = 0;
418411 while (section_counter < elf_hdr.shstrndx) : (section_counter += 1) {
......@@ -421,18 +414,13 @@ const BinaryElfOutput = struct {
421414
422415 const shstrtab_shdr = (try section_headers.next()).?;
423416
424 const buffer = try allocator.alloc(u8, @intCast(shstrtab_shdr.sh_size));
425 errdefer allocator.free(buffer);
426
427 const num_read = try elf_file.preadAll(buffer, shstrtab_shdr.sh_offset);
428 if (num_read != buffer.len) return error.EndOfStream;
429
430 break :blk buffer;
417 try in.seekTo(shstrtab_shdr.sh_offset);
418 break :blk try in.interface.readAlloc(allocator, shstrtab_shdr.sh_size);
431419 };
432420
433421 errdefer if (self.shstrtab) |shstrtab| allocator.free(shstrtab);
434422
435 var section_headers = elf_hdr.section_header_iterator(&elf_file);
423 var section_headers = elf_hdr.iterateSectionHeaders(in);
436424 while (try section_headers.next()) |section| {
437425 if (sectionValidForOutput(section)) {
438426 const newSection = try allocator.create(BinaryElfSection);
......@@ -451,7 +439,7 @@ const BinaryElfOutput = struct {
451439 }
452440 }
453441
454 var program_headers = elf_hdr.program_header_iterator(&elf_file);
442 var program_headers = elf_hdr.iterateProgramHeaders(in);
455443 while (try program_headers.next()) |phdr| {
456444 if (phdr.p_type == elf.PT_LOAD) {
457445 const newSegment = try allocator.create(BinaryElfSegment);
......@@ -539,19 +527,17 @@ const BinaryElfOutput = struct {
539527 }
540528};
541529
542fn writeBinaryElfSection(elf_file: File, out_file: File, section: *BinaryElfSection) !void {
543 try out_file.writeFileAll(elf_file, .{
544 .in_offset = section.elfOffset,
545 .in_len = section.fileSize,
546 });
530fn writeBinaryElfSection(in: *File.Reader, out: *File.Writer, section: *BinaryElfSection) !void {
531 try in.seekTo(section.elfOffset);
532 _ = try out.interface.sendFileAll(in, .limited(section.fileSize));
547533}
548534
549535const HexWriter = struct {
550536 prev_addr: ?u32 = null,
551 out_file: File,
537 out: *File.Writer,
552538
553539 /// Max data bytes per line of output
554 const MAX_PAYLOAD_LEN: u8 = 16;
540 const max_payload_len: u8 = 16;
555541
556542 fn addressParts(address: u16) [2]u8 {
557543 const msb: u8 = @truncate(address >> 8);
......@@ -627,13 +613,13 @@ const HexWriter = struct {
627613 return (sum ^ 0xFF) +% 1;
628614 }
629615
630 fn write(self: Record, file: File) File.WriteError!void {
616 fn write(self: Record, out: *File.Writer) !void {
631617 const linesep = "\r\n";
632618 // colon, (length, address, type, payload, checksum) as hex, CRLF
633 const BUFSIZE = 1 + (1 + 2 + 1 + MAX_PAYLOAD_LEN + 1) * 2 + linesep.len;
619 const BUFSIZE = 1 + (1 + 2 + 1 + max_payload_len + 1) * 2 + linesep.len;
634620 var outbuf: [BUFSIZE]u8 = undefined;
635621 const payload_bytes = self.getPayloadBytes();
636 assert(payload_bytes.len <= MAX_PAYLOAD_LEN);
622 assert(payload_bytes.len <= max_payload_len);
637623
638624 const line = try std.fmt.bufPrint(&outbuf, ":{0X:0>2}{1X:0>4}{2X:0>2}{3X}{4X:0>2}" ++ linesep, .{
639625 @as(u8, @intCast(payload_bytes.len)),
......@@ -642,38 +628,37 @@ const HexWriter = struct {
642628 payload_bytes,
643629 self.checksum(),
644630 });
645 try file.writeAll(line);
631 try out.interface.writeAll(line);
646632 }
647633 };
648634
649 pub fn writeSegment(self: *HexWriter, segment: *const BinaryElfSegment, elf_file: File) !void {
650 var buf: [MAX_PAYLOAD_LEN]u8 = undefined;
635 pub fn writeSegment(self: *HexWriter, segment: *const BinaryElfSegment, in: *File.Reader) !void {
636 var buf: [max_payload_len]u8 = undefined;
651637 var bytes_read: usize = 0;
652638 while (bytes_read < segment.fileSize) {
653639 const row_address: u32 = @intCast(segment.physicalAddress + bytes_read);
654640
655641 const remaining = segment.fileSize - bytes_read;
656 const to_read: usize = @intCast(@min(remaining, MAX_PAYLOAD_LEN));
657 const did_read = try elf_file.preadAll(buf[0..to_read], segment.elfOffset + bytes_read);
658 if (did_read < to_read) return error.UnexpectedEOF;
642 const dest = buf[0..@min(remaining, max_payload_len)];
643 try in.seekTo(segment.elfOffset + bytes_read);
644 try in.interface.readSliceAll(dest);
645 try self.writeDataRow(row_address, dest);
659646
660 try self.writeDataRow(row_address, buf[0..did_read]);
661
662 bytes_read += did_read;
647 bytes_read += dest.len;
663648 }
664649 }
665650
666 fn writeDataRow(self: *HexWriter, address: u32, data: []const u8) File.WriteError!void {
651 fn writeDataRow(self: *HexWriter, address: u32, data: []const u8) !void {
667652 const record = Record.Data(address, data);
668653 if (address > 0xFFFF and (self.prev_addr == null or record.address != self.prev_addr.?)) {
669 try Record.Address(address).write(self.out_file);
654 try Record.Address(address).write(self.out);
670655 }
671 try record.write(self.out_file);
656 try record.write(self.out);
672657 self.prev_addr = @intCast(record.address + data.len);
673658 }
674659
675 fn writeEOF(self: HexWriter) File.WriteError!void {
676 try Record.EOF().write(self.out_file);
660 fn writeEof(self: HexWriter) !void {
661 try Record.EOF().write(self.out);
677662 }
678663};
679664
......@@ -686,9 +671,9 @@ fn containsValidAddressRange(segments: []*BinaryElfSegment) bool {
686671 return true;
687672}
688673
689fn padFile(f: File, opt_size: ?u64) !void {
674fn padFile(out: *File.Writer, opt_size: ?u64) !void {
690675 const size = opt_size orelse return;
691 try f.setEndPos(size);
676 try out.file.setEndPos(size);
692677}
693678
694679test "HexWriter.Record.Address has correct payload and checksum" {
......@@ -732,836 +717,6 @@ test "containsValidAddressRange" {
732717 try std.testing.expect(containsValidAddressRange(&buf));
733718}
734719
735// -------------
736// ELF to ELF stripping
737
738const StripElfOptions = struct {
739 extract_to: ?[]const u8 = null,
740 add_debuglink: ?[]const u8 = null,
741 strip_all: bool = false,
742 strip_debug: bool = false,
743 only_keep_debug: bool = false,
744 compress_debug: bool = false,
745 add_section: ?AddSection,
746 set_section_alignment: ?SetSectionAlignment,
747 set_section_flags: ?SetSectionFlags,
748};
749
750fn stripElf(
751 allocator: Allocator,
752 in_file: File,
753 out_file: File,
754 elf_hdr: elf.Header,
755 options: StripElfOptions,
756) !void {
757 const Filter = ElfFileHelper.Filter;
758 const DebugLink = ElfFileHelper.DebugLink;
759
760 const filter: Filter = filter: {
761 if (options.only_keep_debug) break :filter .debug;
762 if (options.strip_all) break :filter .program;
763 if (options.strip_debug) break :filter .program_and_symbols;
764 break :filter .all;
765 };
766
767 const filter_complement: ?Filter = blk: {
768 if (options.extract_to) |_| {
769 break :blk switch (filter) {
770 .program => .debug_and_symbols,
771 .debug => .program_and_symbols,
772 .program_and_symbols => .debug,
773 .debug_and_symbols => .program,
774 .all => fatal("zig objcopy: nothing to extract", .{}),
775 };
776 } else {
777 break :blk null;
778 }
779 };
780 const debuglink_path = path: {
781 if (options.add_debuglink) |path| break :path path;
782 if (options.extract_to) |path| break :path path;
783 break :path null;
784 };
785
786 switch (elf_hdr.is_64) {
787 inline else => |is_64| {
788 var elf_file = try ElfFile(is_64).parse(allocator, in_file, elf_hdr);
789 defer elf_file.deinit();
790
791 if (options.add_section) |user_section| {
792 for (elf_file.sections) |section| {
793 if (std.mem.eql(u8, section.name, user_section.section_name)) {
794 fatal("zig objcopy: unable to add section '{s}'. Section already exists in input", .{user_section.section_name});
795 }
796 }
797 }
798
799 if (filter_complement) |flt| {
800 // write the .dbg file and close it, so it can be read back to compute the debuglink checksum.
801 const path = options.extract_to.?;
802 const dbg_file = std.fs.cwd().createFile(path, .{}) catch |err| {
803 fatal("zig objcopy: unable to create '{s}': {s}", .{ path, @errorName(err) });
804 };
805 defer dbg_file.close();
806
807 try elf_file.emit(allocator, dbg_file, in_file, .{ .section_filter = flt, .compress_debug = options.compress_debug });
808 }
809
810 const debuglink: ?DebugLink = if (debuglink_path) |path| ElfFileHelper.createDebugLink(path) else null;
811 try elf_file.emit(allocator, out_file, in_file, .{
812 .section_filter = filter,
813 .debuglink = debuglink,
814 .compress_debug = options.compress_debug,
815 .add_section = options.add_section,
816 .set_section_alignment = options.set_section_alignment,
817 .set_section_flags = options.set_section_flags,
818 });
819 },
820 }
821}
822
823// note: this is "a minimal effort implementation"
824// It doesn't support all possibile elf files: some sections type may need fixups, the program header may need fix up, ...
825// It was written for a specific use case (strip debug info to a sperate file, for linux 64-bits executables built with `zig` or `zig c++` )
826// It moves and reoders the sections as little as possible to avoid having to do fixups.
827// TODO: support non-native endianess
828
829fn ElfFile(comptime is_64: bool) type {
830 const Elf_Ehdr = if (is_64) elf.Elf64_Ehdr else elf.Elf32_Ehdr;
831 const Elf_Phdr = if (is_64) elf.Elf64_Phdr else elf.Elf32_Phdr;
832 const Elf_Shdr = if (is_64) elf.Elf64_Shdr else elf.Elf32_Shdr;
833 const Elf_Chdr = if (is_64) elf.Elf64_Chdr else elf.Elf32_Chdr;
834 const Elf_Sym = if (is_64) elf.Elf64_Sym else elf.Elf32_Sym;
835 const Elf_OffSize = if (is_64) elf.Elf64_Off else elf.Elf32_Off;
836
837 return struct {
838 raw_elf_header: Elf_Ehdr,
839 program_segments: []const Elf_Phdr,
840 sections: []const Section,
841 arena: std.heap.ArenaAllocator,
842
843 const SectionCategory = ElfFileHelper.SectionCategory;
844 const section_memory_align: std.mem.Alignment = .of(Elf_Sym); // most restrictive of what we may load in memory
845 const Section = struct {
846 section: Elf_Shdr,
847 name: []const u8 = "",
848 segment: ?*const Elf_Phdr = null, // if the section is used by a program segment (there can be more than one)
849 payload: ?[]align(section_memory_align.toByteUnits()) const u8 = null, // if we need the data in memory
850 category: SectionCategory = .none, // should the section be kept in the exe or stripped to the debug database, or both.
851 };
852
853 const Self = @This();
854
855 pub fn parse(gpa: Allocator, in_file: File, header: elf.Header) !Self {
856 var arena = std.heap.ArenaAllocator.init(gpa);
857 errdefer arena.deinit();
858 const allocator = arena.allocator();
859
860 var raw_header: Elf_Ehdr = undefined;
861 {
862 const bytes_read = try in_file.preadAll(std.mem.asBytes(&raw_header), 0);
863 if (bytes_read < @sizeOf(Elf_Ehdr))
864 return error.TRUNCATED_ELF;
865 }
866
867 // program header: list of segments
868 const program_segments = blk: {
869 if (@sizeOf(Elf_Phdr) != header.phentsize)
870 fatal("zig objcopy: unsupported ELF file, unexpected phentsize ({d})", .{header.phentsize});
871
872 const program_header = try allocator.alloc(Elf_Phdr, header.phnum);
873 const bytes_read = try in_file.preadAll(std.mem.sliceAsBytes(program_header), header.phoff);
874 if (bytes_read < @sizeOf(Elf_Phdr) * header.phnum)
875 return error.TRUNCATED_ELF;
876 break :blk program_header;
877 };
878
879 // section header
880 const sections = blk: {
881 if (@sizeOf(Elf_Shdr) != header.shentsize)
882 fatal("zig objcopy: unsupported ELF file, unexpected shentsize ({d})", .{header.shentsize});
883
884 const section_header = try allocator.alloc(Section, header.shnum);
885
886 const raw_section_header = try allocator.alloc(Elf_Shdr, header.shnum);
887 defer allocator.free(raw_section_header);
888 const bytes_read = try in_file.preadAll(std.mem.sliceAsBytes(raw_section_header), header.shoff);
889 if (bytes_read < @sizeOf(Elf_Phdr) * header.shnum)
890 return error.TRUNCATED_ELF;
891
892 for (section_header, raw_section_header) |*section, hdr| {
893 section.* = .{ .section = hdr };
894 }
895 break :blk section_header;
896 };
897
898 // load data to memory for some sections:
899 // string tables for access
900 // sections than need modifications when other sections move.
901 for (sections, 0..) |*section, idx| {
902 const need_data = switch (section.section.sh_type) {
903 elf.DT_VERSYM => true,
904 elf.SHT_SYMTAB, elf.SHT_DYNSYM => true,
905 else => false,
906 };
907 const need_strings = (idx == header.shstrndx);
908
909 if (need_data or need_strings) {
910 const buffer = try allocator.alignedAlloc(u8, section_memory_align, @intCast(section.section.sh_size));
911 const bytes_read = try in_file.preadAll(buffer, section.section.sh_offset);
912 if (bytes_read != section.section.sh_size) return error.TRUNCATED_ELF;
913 section.payload = buffer;
914 }
915 }
916
917 // fill-in sections info:
918 // resolve the name
919 // find if a program segment uses the section
920 // categorize sections usage (used by program segments, debug datadase, common metadata, symbol table)
921 for (sections) |*section| {
922 section.segment = for (program_segments) |*seg| {
923 if (sectionWithinSegment(section.section, seg.*)) break seg;
924 } else null;
925
926 if (section.section.sh_name != 0 and header.shstrndx != elf.SHN_UNDEF)
927 section.name = std.mem.span(@as([*:0]const u8, @ptrCast(&sections[header.shstrndx].payload.?[section.section.sh_name])));
928
929 const category_from_program: SectionCategory = if (section.segment != null) .exe else .debug;
930 section.category = switch (section.section.sh_type) {
931 elf.SHT_NOTE => .common,
932 elf.SHT_SYMTAB => .symbols, // "strip all" vs "strip only debug"
933 elf.SHT_DYNSYM => .exe,
934 elf.SHT_PROGBITS => cat: {
935 if (std.mem.eql(u8, section.name, ".comment")) break :cat .exe;
936 if (std.mem.eql(u8, section.name, ".gnu_debuglink")) break :cat .none;
937 break :cat category_from_program;
938 },
939 elf.SHT_LOPROC...elf.SHT_HIPROC => .common, // don't strip unknown sections
940 elf.SHT_LOUSER...elf.SHT_HIUSER => .common, // don't strip unknown sections
941 else => category_from_program,
942 };
943 }
944
945 sections[0].category = .common; // mandatory null section
946 if (header.shstrndx != elf.SHN_UNDEF)
947 sections[header.shstrndx].category = .common; // string table for the headers
948
949 // recursively propagate section categories to their linked sections, so that they are kept together
950 var dirty: u1 = 1;
951 while (dirty != 0) {
952 dirty = 0;
953
954 for (sections) |*section| {
955 if (section.section.sh_link != elf.SHN_UNDEF)
956 dirty |= ElfFileHelper.propagateCategory(&sections[section.section.sh_link].category, section.category);
957 if ((section.section.sh_flags & elf.SHF_INFO_LINK) != 0 and section.section.sh_info != elf.SHN_UNDEF)
958 dirty |= ElfFileHelper.propagateCategory(&sections[section.section.sh_info].category, section.category);
959 }
960 }
961
962 return Self{
963 .arena = arena,
964 .raw_elf_header = raw_header,
965 .program_segments = program_segments,
966 .sections = sections,
967 };
968 }
969
970 pub fn deinit(self: *Self) void {
971 self.arena.deinit();
972 }
973
974 const Filter = ElfFileHelper.Filter;
975 const DebugLink = ElfFileHelper.DebugLink;
976 const EmitElfOptions = struct {
977 section_filter: Filter = .all,
978 debuglink: ?DebugLink = null,
979 compress_debug: bool = false,
980 add_section: ?AddSection = null,
981 set_section_alignment: ?SetSectionAlignment = null,
982 set_section_flags: ?SetSectionFlags = null,
983 };
984 fn emit(self: *const Self, gpa: Allocator, out_file: File, in_file: File, options: EmitElfOptions) !void {
985 var arena = std.heap.ArenaAllocator.init(gpa);
986 defer arena.deinit();
987 const allocator = arena.allocator();
988
989 // when emitting the stripped exe:
990 // - unused sections are removed
991 // when emitting the debug file:
992 // - all sections are kept, but some are emptied and their types is changed to SHT_NOBITS
993 // the program header is kept unchanged. (`strip` does update it, but `eu-strip` does not, and it still works)
994
995 const Update = struct {
996 action: ElfFileHelper.Action,
997
998 // remap the indexs after omitting the filtered sections
999 remap_idx: u16,
1000
1001 // optionally overrides the payload from the source file
1002 payload: ?[]align(section_memory_align.toByteUnits()) const u8 = null,
1003 section: ?Elf_Shdr = null,
1004 };
1005 const sections_update = try allocator.alloc(Update, self.sections.len);
1006 const new_shnum = blk: {
1007 var next_idx: u16 = 0;
1008 for (self.sections, sections_update) |section, *update| {
1009 const action = ElfFileHelper.selectAction(section.category, options.section_filter);
1010 const remap_idx = idx: {
1011 if (action == .strip) break :idx elf.SHN_UNDEF;
1012 next_idx += 1;
1013 break :idx next_idx - 1;
1014 };
1015 update.* = Update{ .action = action, .remap_idx = remap_idx };
1016 }
1017
1018 if (options.debuglink != null)
1019 next_idx += 1;
1020
1021 if (options.add_section != null) {
1022 next_idx += 1;
1023 }
1024
1025 break :blk next_idx;
1026 };
1027
1028 // add a ".gnu_debuglink" to the string table if needed
1029 const debuglink_name: u32 = blk: {
1030 if (options.debuglink == null) break :blk elf.SHN_UNDEF;
1031 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1032 fatal("zig objcopy: no strtab, cannot add the debuglink section", .{}); // TODO add the section if needed?
1033
1034 const strtab = &self.sections[self.raw_elf_header.e_shstrndx];
1035 const update = &sections_update[self.raw_elf_header.e_shstrndx];
1036
1037 const name: []const u8 = ".gnu_debuglink";
1038 const new_offset: u32 = @intCast(strtab.payload.?.len);
1039 const buf = try allocator.alignedAlloc(u8, section_memory_align, new_offset + name.len + 1);
1040 @memcpy(buf[0..new_offset], strtab.payload.?);
1041 @memcpy(buf[new_offset..][0..name.len], name);
1042 buf[new_offset + name.len] = 0;
1043
1044 assert(update.action == .keep);
1045 update.payload = buf;
1046
1047 break :blk new_offset;
1048 };
1049
1050 // add user section to the string table if needed
1051 const user_section_name: u32 = blk: {
1052 if (options.add_section == null) break :blk elf.SHN_UNDEF;
1053 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1054 fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed?
1055
1056 const strtab = &self.sections[self.raw_elf_header.e_shstrndx];
1057 const update = &sections_update[self.raw_elf_header.e_shstrndx];
1058
1059 const name = options.add_section.?.section_name;
1060 const new_offset: u32 = @intCast(strtab.payload.?.len);
1061 const buf = try allocator.alignedAlloc(u8, section_memory_align, new_offset + name.len + 1);
1062 @memcpy(buf[0..new_offset], strtab.payload.?);
1063 @memcpy(buf[new_offset..][0..name.len], name);
1064 buf[new_offset + name.len] = 0;
1065
1066 assert(update.action == .keep);
1067 update.payload = buf;
1068
1069 break :blk new_offset;
1070 };
1071
1072 // maybe compress .debug sections
1073 if (options.compress_debug) {
1074 for (self.sections[1..], sections_update[1..]) |section, *update| {
1075 if (update.action != .keep) continue;
1076 if (!std.mem.startsWith(u8, section.name, ".debug_")) continue;
1077 if ((section.section.sh_flags & elf.SHF_COMPRESSED) != 0) continue; // already compressed
1078
1079 const chdr = Elf_Chdr{
1080 .ch_type = elf.COMPRESS.ZLIB,
1081 .ch_size = section.section.sh_size,
1082 .ch_addralign = section.section.sh_addralign,
1083 };
1084
1085 const compressed_payload = try ElfFileHelper.tryCompressSection(allocator, in_file, section.section.sh_offset, section.section.sh_size, std.mem.asBytes(&chdr));
1086 if (compressed_payload) |payload| {
1087 update.payload = payload;
1088 update.section = section.section;
1089 update.section.?.sh_addralign = @alignOf(Elf_Chdr);
1090 update.section.?.sh_size = @intCast(payload.len);
1091 update.section.?.sh_flags |= elf.SHF_COMPRESSED;
1092 }
1093 }
1094 }
1095
1096 var cmdbuf = std.ArrayList(ElfFileHelper.WriteCmd).init(allocator);
1097 defer cmdbuf.deinit();
1098 try cmdbuf.ensureUnusedCapacity(3 + new_shnum);
1099 var eof_offset: Elf_OffSize = 0; // track the end of the data written so far.
1100
1101 // build the updated headers
1102 // nb: updated_elf_header will be updated before the actual write
1103 var updated_elf_header = self.raw_elf_header;
1104 if (updated_elf_header.e_shstrndx != elf.SHN_UNDEF)
1105 updated_elf_header.e_shstrndx = sections_update[updated_elf_header.e_shstrndx].remap_idx;
1106 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = std.mem.asBytes(&updated_elf_header), .out_offset = 0 } });
1107 eof_offset = @sizeOf(Elf_Ehdr);
1108
1109 // program header as-is.
1110 // nb: for only-debug files, removing it appears to work, but is invalid by ELF specifcation.
1111 {
1112 assert(updated_elf_header.e_phoff == @sizeOf(Elf_Ehdr));
1113 const data = std.mem.sliceAsBytes(self.program_segments);
1114 assert(data.len == @as(usize, updated_elf_header.e_phentsize) * updated_elf_header.e_phnum);
1115 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_phoff } });
1116 eof_offset = updated_elf_header.e_phoff + @as(Elf_OffSize, @intCast(data.len));
1117 }
1118
1119 // update sections and queue payload writes
1120 const updated_section_header = blk: {
1121 const dest_sections = try allocator.alloc(Elf_Shdr, new_shnum);
1122
1123 {
1124 // the ELF format doesn't specify the order for all sections.
1125 // this code only supports when they are in increasing file order.
1126 var offset: u64 = eof_offset;
1127 for (self.sections[1..]) |section| {
1128 if (section.section.sh_type == elf.SHT_NOBITS)
1129 continue;
1130 if (section.section.sh_offset < offset) {
1131 fatal("zig objcopy: unsupported ELF file", .{});
1132 }
1133 offset = section.section.sh_offset;
1134 }
1135 }
1136
1137 dest_sections[0] = self.sections[0].section;
1138
1139 var dest_section_idx: u32 = 1;
1140 for (self.sections[1..], sections_update[1..]) |section, update| {
1141 if (update.action == .strip) continue;
1142 assert(update.remap_idx == dest_section_idx);
1143
1144 const src = if (update.section) |*s| s else &section.section;
1145 const dest = &dest_sections[dest_section_idx];
1146 const payload = if (update.payload) |data| data else section.payload;
1147 dest_section_idx += 1;
1148
1149 dest.* = src.*;
1150
1151 if (src.sh_link != elf.SHN_UNDEF)
1152 dest.sh_link = sections_update[src.sh_link].remap_idx;
1153 if ((src.sh_flags & elf.SHF_INFO_LINK) != 0 and src.sh_info != elf.SHN_UNDEF)
1154 dest.sh_info = sections_update[src.sh_info].remap_idx;
1155
1156 if (payload) |data|
1157 dest.sh_size = @intCast(data.len);
1158
1159 const addralign = if (src.sh_addralign == 0 or dest.sh_type == elf.SHT_NOBITS) 1 else src.sh_addralign;
1160 dest.sh_offset = std.mem.alignForward(Elf_OffSize, eof_offset, addralign);
1161 if (src.sh_offset != dest.sh_offset and section.segment != null and update.action != .empty and dest.sh_type != elf.SHT_NOTE and dest.sh_type != elf.SHT_NOBITS) {
1162 if (src.sh_offset > dest.sh_offset) {
1163 dest.sh_offset = src.sh_offset; // add padding to avoid modifing the program segments
1164 } else {
1165 fatal("zig objcopy: cannot adjust program segments", .{});
1166 }
1167 }
1168 assert(dest.sh_addr % addralign == dest.sh_offset % addralign);
1169
1170 if (update.action == .empty)
1171 dest.sh_type = elf.SHT_NOBITS;
1172
1173 if (dest.sh_type != elf.SHT_NOBITS) {
1174 if (payload) |src_data| {
1175 // update sections payload and write
1176 const dest_data = switch (src.sh_type) {
1177 elf.DT_VERSYM => dst_data: {
1178 const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len);
1179 @memcpy(data, src_data);
1180
1181 const defs = @as([*]elf.Verdef, @ptrCast(data))[0 .. @as(usize, @intCast(src.sh_size)) / @sizeOf(elf.Verdef)];
1182 for (defs) |*def| switch (def.ndx) {
1183 .LOCAL, .GLOBAL => {},
1184 else => def.ndx = @enumFromInt(sections_update[src.sh_info].remap_idx),
1185 };
1186
1187 break :dst_data data;
1188 },
1189 elf.SHT_SYMTAB, elf.SHT_DYNSYM => dst_data: {
1190 const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len);
1191 @memcpy(data, src_data);
1192
1193 const syms = @as([*]Elf_Sym, @ptrCast(data))[0 .. @as(usize, @intCast(src.sh_size)) / @sizeOf(Elf_Sym)];
1194 for (syms) |*sym| {
1195 if (sym.st_shndx != elf.SHN_UNDEF and sym.st_shndx < elf.SHN_LORESERVE)
1196 sym.st_shndx = sections_update[sym.st_shndx].remap_idx;
1197 }
1198
1199 break :dst_data data;
1200 },
1201 else => src_data,
1202 };
1203
1204 assert(dest_data.len == dest.sh_size);
1205 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = dest_data, .out_offset = dest.sh_offset } });
1206 eof_offset = dest.sh_offset + dest.sh_size;
1207 } else {
1208 // direct contents copy
1209 cmdbuf.appendAssumeCapacity(.{ .copy_range = .{ .in_offset = src.sh_offset, .len = dest.sh_size, .out_offset = dest.sh_offset } });
1210 eof_offset = dest.sh_offset + dest.sh_size;
1211 }
1212 } else {
1213 // account for alignment padding even in empty sections to keep logical section order
1214 eof_offset = dest.sh_offset;
1215 }
1216 }
1217
1218 // add a ".gnu_debuglink" section
1219 if (options.debuglink) |link| {
1220 const payload = payload: {
1221 const crc_offset = std.mem.alignForward(usize, link.name.len + 1, 4);
1222 const buf = try allocator.alignedAlloc(u8, .@"4", crc_offset + 4);
1223 @memcpy(buf[0..link.name.len], link.name);
1224 @memset(buf[link.name.len..crc_offset], 0);
1225 @memcpy(buf[crc_offset..], std.mem.asBytes(&link.crc32));
1226 break :payload buf;
1227 };
1228
1229 dest_sections[dest_section_idx] = Elf_Shdr{
1230 .sh_name = debuglink_name,
1231 .sh_type = elf.SHT_PROGBITS,
1232 .sh_flags = 0,
1233 .sh_addr = 0,
1234 .sh_offset = eof_offset,
1235 .sh_size = @intCast(payload.len),
1236 .sh_link = elf.SHN_UNDEF,
1237 .sh_info = elf.SHN_UNDEF,
1238 .sh_addralign = 4,
1239 .sh_entsize = 0,
1240 };
1241 dest_section_idx += 1;
1242
1243 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = payload, .out_offset = eof_offset } });
1244 eof_offset += @as(Elf_OffSize, @intCast(payload.len));
1245 }
1246
1247 // --add-section
1248 if (options.add_section) |add_section| {
1249 var section_file = fs.cwd().openFile(add_section.file_path, .{}) catch |err|
1250 fatal("unable to open '{s}': {s}", .{ add_section.file_path, @errorName(err) });
1251 defer section_file.close();
1252
1253 const payload = try section_file.readToEndAlloc(arena.allocator(), std.math.maxInt(usize));
1254
1255 dest_sections[dest_section_idx] = Elf_Shdr{
1256 .sh_name = user_section_name,
1257 .sh_type = elf.SHT_PROGBITS,
1258 .sh_flags = 0,
1259 .sh_addr = 0,
1260 .sh_offset = eof_offset,
1261 .sh_size = @intCast(payload.len),
1262 .sh_link = elf.SHN_UNDEF,
1263 .sh_info = elf.SHN_UNDEF,
1264 .sh_addralign = 4,
1265 .sh_entsize = 0,
1266 };
1267 dest_section_idx += 1;
1268
1269 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = payload, .out_offset = eof_offset } });
1270 eof_offset += @as(Elf_OffSize, @intCast(payload.len));
1271 }
1272
1273 assert(dest_section_idx == new_shnum);
1274 break :blk dest_sections;
1275 };
1276
1277 // --set-section-alignment: overwrite alignment
1278 if (options.set_section_alignment) |set_align| {
1279 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1280 fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed?
1281
1282 const strtab = &sections_update[self.raw_elf_header.e_shstrndx];
1283 for (updated_section_header) |*section| {
1284 const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name])));
1285 if (std.mem.eql(u8, section_name, set_align.section_name)) {
1286 section.sh_addralign = set_align.alignment;
1287 break;
1288 }
1289 } else std.log.warn("Skipping --set-section-alignment. Section '{s}' not found", .{set_align.section_name});
1290 }
1291
1292 // --set-section-flags: overwrite flags
1293 if (options.set_section_flags) |set_flags| {
1294 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1295 fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed?
1296
1297 const strtab = &sections_update[self.raw_elf_header.e_shstrndx];
1298 for (updated_section_header) |*section| {
1299 const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name])));
1300 if (std.mem.eql(u8, section_name, set_flags.section_name)) {
1301 section.sh_flags = std.elf.SHF_WRITE; // default is writable cleared by "readonly"
1302 const f = set_flags.flags;
1303
1304 // Supporting a subset of GNU and LLVM objcopy for ELF only
1305 // GNU:
1306 // alloc: add SHF_ALLOC
1307 // contents: if section is SHT_NOBITS, set SHT_PROGBITS, otherwise do nothing
1308 // load: if section is SHT_NOBITS, set SHT_PROGBITS, otherwise do nothing (same as contents)
1309 // noload: not ELF relevant
1310 // readonly: clear default SHF_WRITE flag
1311 // code: add SHF_EXECINSTR
1312 // data: not ELF relevant
1313 // rom: ignored
1314 // exclude: add SHF_EXCLUDE
1315 // share: not ELF relevant
1316 // debug: not ELF relevant
1317 // large: add SHF_X86_64_LARGE. Fatal error if target is not x86_64
1318 if (f.alloc) section.sh_flags |= std.elf.SHF_ALLOC;
1319 if (f.contents or f.load) {
1320 if (section.sh_type == std.elf.SHT_NOBITS) section.sh_type = std.elf.SHT_PROGBITS;
1321 }
1322 if (f.readonly) section.sh_flags &= ~@as(@TypeOf(section.sh_type), std.elf.SHF_WRITE);
1323 if (f.code) section.sh_flags |= std.elf.SHF_EXECINSTR;
1324 if (f.exclude) section.sh_flags |= std.elf.SHF_EXCLUDE;
1325 if (f.large) {
1326 if (updated_elf_header.e_machine != std.elf.EM.X86_64)
1327 fatal("zig objcopy: 'large' section flag is only supported on x86_64 targets", .{});
1328 section.sh_flags |= std.elf.SHF_X86_64_LARGE;
1329 }
1330
1331 // LLVM:
1332 // merge: add SHF_MERGE
1333 // strings: add SHF_STRINGS
1334 if (f.merge) section.sh_flags |= std.elf.SHF_MERGE;
1335 if (f.strings) section.sh_flags |= std.elf.SHF_STRINGS;
1336 break;
1337 }
1338 } else std.log.warn("Skipping --set-section-flags. Section '{s}' not found", .{set_flags.section_name});
1339 }
1340
1341 // write the section header at the tail
1342 {
1343 const offset = std.mem.alignForward(Elf_OffSize, eof_offset, @alignOf(Elf_Shdr));
1344
1345 const data = std.mem.sliceAsBytes(updated_section_header);
1346 assert(data.len == @as(usize, updated_elf_header.e_shentsize) * new_shnum);
1347 updated_elf_header.e_shoff = offset;
1348 updated_elf_header.e_shnum = new_shnum;
1349
1350 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_shoff } });
1351 }
1352
1353 try ElfFileHelper.write(allocator, out_file, in_file, cmdbuf.items);
1354 }
1355
1356 fn sectionWithinSegment(section: Elf_Shdr, segment: Elf_Phdr) bool {
1357 const file_size = if (section.sh_type == elf.SHT_NOBITS) 0 else section.sh_size;
1358 return segment.p_offset <= section.sh_offset and (segment.p_offset + segment.p_filesz) >= (section.sh_offset + file_size);
1359 }
1360 };
1361}
1362
1363const ElfFileHelper = struct {
1364 const DebugLink = struct { name: []const u8, crc32: u32 };
1365 const Filter = enum { all, program, debug, program_and_symbols, debug_and_symbols };
1366
1367 const SectionCategory = enum { common, exe, debug, symbols, none };
1368 fn propagateCategory(cur: *SectionCategory, new: SectionCategory) u1 {
1369 const cat: SectionCategory = switch (cur.*) {
1370 .none => new,
1371 .common => .common,
1372 .debug => switch (new) {
1373 .none, .debug => .debug,
1374 else => new,
1375 },
1376 .exe => switch (new) {
1377 .common => .common,
1378 .none, .debug, .exe => .exe,
1379 .symbols => .exe,
1380 },
1381 .symbols => switch (new) {
1382 .none, .common, .debug, .exe => unreachable,
1383 .symbols => .symbols,
1384 },
1385 };
1386
1387 if (cur.* != cat) {
1388 cur.* = cat;
1389 return 1;
1390 } else {
1391 return 0;
1392 }
1393 }
1394
1395 const Action = enum { keep, strip, empty };
1396 fn selectAction(category: SectionCategory, filter: Filter) Action {
1397 if (category == .none) return .strip;
1398 return switch (filter) {
1399 .all => switch (category) {
1400 .none => .strip,
1401 else => .keep,
1402 },
1403 .program => switch (category) {
1404 .common, .exe => .keep,
1405 else => .strip,
1406 },
1407 .program_and_symbols => switch (category) {
1408 .common, .exe, .symbols => .keep,
1409 else => .strip,
1410 },
1411 .debug => switch (category) {
1412 .exe, .symbols => .empty,
1413 .none => .strip,
1414 else => .keep,
1415 },
1416 .debug_and_symbols => switch (category) {
1417 .exe => .empty,
1418 .none => .strip,
1419 else => .keep,
1420 },
1421 };
1422 }
1423
1424 const WriteCmd = union(enum) {
1425 copy_range: struct { in_offset: u64, len: u64, out_offset: u64 },
1426 write_data: struct { data: []const u8, out_offset: u64 },
1427 };
1428 fn write(allocator: Allocator, out_file: File, in_file: File, cmds: []const WriteCmd) !void {
1429 // consolidate holes between writes:
1430 // by coping original padding data from in_file (by fusing contiguous ranges)
1431 // by writing zeroes otherwise
1432 const zeroes = [1]u8{0} ** 4096;
1433 var consolidated = std.ArrayList(WriteCmd).init(allocator);
1434 defer consolidated.deinit();
1435 try consolidated.ensureUnusedCapacity(cmds.len * 2);
1436 var offset: u64 = 0;
1437 var fused_cmd: ?WriteCmd = null;
1438 for (cmds) |cmd| {
1439 switch (cmd) {
1440 .write_data => |data| {
1441 assert(data.out_offset >= offset);
1442 if (fused_cmd) |prev| {
1443 consolidated.appendAssumeCapacity(prev);
1444 fused_cmd = null;
1445 }
1446 if (data.out_offset > offset) {
1447 consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(data.out_offset - offset)], .out_offset = offset } });
1448 }
1449 consolidated.appendAssumeCapacity(cmd);
1450 offset = data.out_offset + data.data.len;
1451 },
1452 .copy_range => |range| {
1453 assert(range.out_offset >= offset);
1454 if (fused_cmd) |prev| {
1455 if (range.in_offset >= prev.copy_range.in_offset + prev.copy_range.len and (range.out_offset - prev.copy_range.out_offset == range.in_offset - prev.copy_range.in_offset)) {
1456 fused_cmd = .{ .copy_range = .{
1457 .in_offset = prev.copy_range.in_offset,
1458 .out_offset = prev.copy_range.out_offset,
1459 .len = (range.out_offset + range.len) - prev.copy_range.out_offset,
1460 } };
1461 } else {
1462 consolidated.appendAssumeCapacity(prev);
1463 if (range.out_offset > offset) {
1464 consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(range.out_offset - offset)], .out_offset = offset } });
1465 }
1466 fused_cmd = cmd;
1467 }
1468 } else {
1469 fused_cmd = cmd;
1470 }
1471 offset = range.out_offset + range.len;
1472 },
1473 }
1474 }
1475 if (fused_cmd) |cmd| {
1476 consolidated.appendAssumeCapacity(cmd);
1477 }
1478
1479 // write the output file
1480 for (consolidated.items) |cmd| {
1481 switch (cmd) {
1482 .write_data => |data| {
1483 var iovec = [_]std.posix.iovec_const{.{ .base = data.data.ptr, .len = data.data.len }};
1484 try out_file.pwritevAll(&iovec, data.out_offset);
1485 },
1486 .copy_range => |range| {
1487 const copied_bytes = try in_file.copyRangeAll(range.in_offset, out_file, range.out_offset, range.len);
1488 if (copied_bytes < range.len) return error.TRUNCATED_ELF;
1489 },
1490 }
1491 }
1492 }
1493
1494 fn tryCompressSection(allocator: Allocator, in_file: File, offset: u64, size: u64, prefix: []const u8) !?[]align(8) const u8 {
1495 if (size < prefix.len) return null;
1496
1497 try in_file.seekTo(offset);
1498 var section_reader = std.io.limitedReader(in_file.deprecatedReader(), size);
1499
1500 // allocate as large as decompressed data. if the compression doesn't fit, keep the data uncompressed.
1501 const compressed_data = try allocator.alignedAlloc(u8, .@"8", @intCast(size));
1502 var compressed_stream = std.io.fixedBufferStream(compressed_data);
1503
1504 try compressed_stream.writer().writeAll(prefix);
1505
1506 {
1507 var compressor = try std.compress.zlib.compressor(compressed_stream.writer(), .{});
1508
1509 var buf: [8000]u8 = undefined;
1510 while (true) {
1511 const bytes_read = try section_reader.read(&buf);
1512 if (bytes_read == 0) break;
1513 const bytes_written = compressor.write(buf[0..bytes_read]) catch |err| switch (err) {
1514 error.NoSpaceLeft => {
1515 allocator.free(compressed_data);
1516 return null;
1517 },
1518 else => return err,
1519 };
1520 std.debug.assert(bytes_written == bytes_read);
1521 }
1522 compressor.finish() catch |err| switch (err) {
1523 error.NoSpaceLeft => {
1524 allocator.free(compressed_data);
1525 return null;
1526 },
1527 else => return err,
1528 };
1529 }
1530
1531 const compressed_len: usize = @intCast(compressed_stream.getPos() catch unreachable);
1532 const data = allocator.realloc(compressed_data, compressed_len) catch compressed_data;
1533 return data[0..compressed_len];
1534 }
1535
1536 fn createDebugLink(path: []const u8) DebugLink {
1537 const file = std.fs.cwd().openFile(path, .{}) catch |err| {
1538 fatal("zig objcopy: could not open `{s}`: {s}\n", .{ path, @errorName(err) });
1539 };
1540 defer file.close();
1541
1542 const crc = ElfFileHelper.computeFileCrc(file) catch |err| {
1543 fatal("zig objcopy: could not read `{s}`: {s}\n", .{ path, @errorName(err) });
1544 };
1545 return .{
1546 .name = std.fs.path.basename(path),
1547 .crc32 = crc,
1548 };
1549 }
1550
1551 fn computeFileCrc(file: File) !u32 {
1552 var buf: [8000]u8 = undefined;
1553
1554 try file.seekTo(0);
1555 var hasher = std.hash.Crc32.init();
1556 while (true) {
1557 const bytes_read = try file.read(&buf);
1558 if (bytes_read == 0) break;
1559 hasher.update(buf[0..bytes_read]);
1560 }
1561 return hasher.final();
1562 }
1563};
1564
1565720const SectionFlags = packed struct {
1566721 alloc: bool = false,
1567722 contents: bool = false,
lib/std/Build/Step/Run.zig+13-4
......@@ -1764,13 +1764,22 @@ fn evalGeneric(run: *Run, child: *std.process.Child) !StdIoResult {
17641764 child.stdin = null;
17651765 },
17661766 .lazy_path => |lazy_path| {
1767 const path = lazy_path.getPath2(b, &run.step);
1768 const file = b.build_root.handle.openFile(path, .{}) catch |err| {
1767 const path = lazy_path.getPath3(b, &run.step);
1768 const file = path.root_dir.handle.openFile(path.subPathOrDot(), .{}) catch |err| {
17691769 return run.step.fail("unable to open stdin file: {s}", .{@errorName(err)});
17701770 };
17711771 defer file.close();
1772 child.stdin.?.writeFileAll(file, .{}) catch |err| {
1773 return run.step.fail("unable to write file to stdin: {s}", .{@errorName(err)});
1772 // TODO https://github.com/ziglang/zig/issues/23955
1773 var buffer: [1024]u8 = undefined;
1774 var file_reader = file.reader(&buffer);
1775 var stdin_writer = child.stdin.?.writer(&.{});
1776 _ = stdin_writer.interface.sendFileAll(&file_reader, .unlimited) catch |err| switch (err) {
1777 error.ReadFailed => return run.step.fail("failed to read from {f}: {t}", .{
1778 path, file_reader.err.?,
1779 }),
1780 error.WriteFailed => return run.step.fail("failed to write to stdin: {t}", .{
1781 stdin_writer.err.?,
1782 }),
17741783 };
17751784 child.stdin.?.close();
17761785 child.stdin = null;
lib/std/Io/Writer.zig+2-1
......@@ -440,7 +440,8 @@ pub fn advance(w: *Writer, n: usize) void {
440440/// After calling `writableVector`, this function tracks how many bytes were
441441/// written to it.
442442pub fn advanceVector(w: *Writer, n: usize) usize {
443 return consume(w, n);
443 if (w.vtable != VectorWrapper.vtable) advance(w, n);
444 return n;
444445}
445446
446447/// The `data` parameter is mutable because this function needs to mutate the
lib/std/Thread.zig+2-21
......@@ -912,18 +912,9 @@ const WasiThreadImpl = struct {
912912 allocator.free(self.thread.memory);
913913 }
914914
915 var spin: u8 = 10;
916915 while (true) {
917916 const tid = self.thread.tid.load(.seq_cst);
918 if (tid == 0) {
919 break;
920 }
921
922 if (spin > 0) {
923 spin -= 1;
924 std.atomic.spinLoopHint();
925 continue;
926 }
917 if (tid == 0) break;
927918
928919 const result = asm (
929920 \\ local.get %[ptr]
......@@ -1515,18 +1506,9 @@ const LinuxThreadImpl = struct {
15151506 fn join(self: Impl) void {
15161507 defer posix.munmap(self.thread.mapped);
15171508
1518 var spin: u8 = 10;
15191509 while (true) {
15201510 const tid = self.thread.child_tid.load(.seq_cst);
1521 if (tid == 0) {
1522 break;
1523 }
1524
1525 if (spin > 0) {
1526 spin -= 1;
1527 std.atomic.spinLoopHint();
1528 continue;
1529 }
1511 if (tid == 0) break;
15301512
15311513 switch (linux.E.init(linux.futex_4arg(
15321514 &self.thread.child_tid.raw,
......@@ -1617,7 +1599,6 @@ test "setName, getName" {
16171599}
16181600
16191601test {
1620 // Doesn't use testing.refAllDecls() since that would pull in the compileError spinLoopHint.
16211602 _ = Futex;
16221603 _ = ResetEvent;
16231604 _ = Mutex;
lib/std/c.zig+2-2
......@@ -10497,9 +10497,9 @@ pub const sysconf = switch (native_os) {
1049710497
1049810498pub const sf_hdtr = switch (native_os) {
1049910499 .freebsd, .macos, .ios, .tvos, .watchos, .visionos => extern struct {
10500 headers: [*]const iovec_const,
10500 headers: ?[*]const iovec_const,
1050110501 hdr_cnt: c_int,
10502 trailers: [*]const iovec_const,
10502 trailers: ?[*]const iovec_const,
1050310503 trl_cnt: c_int,
1050410504 },
1050510505 else => void,
lib/std/elf.zig+40-94
......@@ -482,6 +482,7 @@ pub const Header = struct {
482482 is_64: bool,
483483 endian: std.builtin.Endian,
484484 os_abi: OSABI,
485 /// The meaning of this value depends on `os_abi`.
485486 abi_version: u8,
486487 type: ET,
487488 machine: EM,
......@@ -508,75 +509,54 @@ pub const Header = struct {
508509 };
509510 }
510511
511 pub const ReadError = std.io.Reader.Error || ParseError;
512
513 pub fn read(r: *std.io.Reader) ReadError!Header {
514 const buf = try r.peek(@sizeOf(Elf64_Ehdr));
515 const result = try parse(@ptrCast(buf));
516 r.toss(if (result.is_64) @sizeOf(Elf64_Ehdr) else @sizeOf(Elf32_Ehdr));
517 return result;
518 }
519
520 pub const ParseError = error{
512 pub const ReadError = std.Io.Reader.Error || error{
521513 InvalidElfMagic,
522514 InvalidElfVersion,
523515 InvalidElfClass,
524516 InvalidElfEndian,
525517 };
526518
527 pub fn parse(hdr_buf: *align(@alignOf(Elf64_Ehdr)) const [@sizeOf(Elf64_Ehdr)]u8) ParseError!Header {
528 const hdr32: *const Elf32_Ehdr = @ptrCast(hdr_buf);
529 const hdr64: *const Elf64_Ehdr = @ptrCast(hdr_buf);
530 if (!mem.eql(u8, hdr32.e_ident[0..4], MAGIC)) return error.InvalidElfMagic;
531 if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion;
519 pub fn read(r: *std.Io.Reader) ReadError!Header {
520 const buf = try r.peek(@sizeOf(Elf64_Ehdr));
532521
533 const is_64 = switch (hdr32.e_ident[EI_CLASS]) {
534 ELFCLASS32 => false,
535 ELFCLASS64 => true,
536 else => return error.InvalidElfClass,
537 };
522 if (!mem.eql(u8, buf[0..4], MAGIC)) return error.InvalidElfMagic;
523 if (buf[EI_VERSION] != 1) return error.InvalidElfVersion;
538524
539 const endian: std.builtin.Endian = switch (hdr32.e_ident[EI_DATA]) {
525 const endian: std.builtin.Endian = switch (buf[EI_DATA]) {
540526 ELFDATA2LSB => .little,
541527 ELFDATA2MSB => .big,
542528 else => return error.InvalidElfEndian,
543529 };
544 const need_bswap = endian != native_endian;
545530
531 return switch (buf[EI_CLASS]) {
532 ELFCLASS32 => .init(try r.takeStruct(Elf32_Ehdr, endian), endian),
533 ELFCLASS64 => .init(try r.takeStruct(Elf64_Ehdr, endian), endian),
534 else => return error.InvalidElfClass,
535 };
536 }
537
538 pub fn init(hdr: anytype, endian: std.builtin.Endian) Header {
546539 // Converting integers to exhaustive enums using `@enumFromInt` could cause a panic.
547540 comptime assert(!@typeInfo(OSABI).@"enum".is_exhaustive);
548 const os_abi: OSABI = @enumFromInt(hdr32.e_ident[EI_OSABI]);
549
550 // The meaning of this value depends on `os_abi` so just make it available as `u8`.
551 const abi_version = hdr32.e_ident[EI_ABIVERSION];
552
553 const @"type": ET = if (need_bswap) blk: {
554 comptime assert(!@typeInfo(ET).@"enum".is_exhaustive);
555 const value = @intFromEnum(hdr32.e_type);
556 break :blk @enumFromInt(@byteSwap(value));
557 } else hdr32.e_type;
558
559 const machine: EM = if (need_bswap) blk: {
560 comptime assert(!@typeInfo(EM).@"enum".is_exhaustive);
561 const value = @intFromEnum(hdr32.e_machine);
562 break :blk @enumFromInt(@byteSwap(value));
563 } else hdr32.e_machine;
564
565541 return .{
566 .is_64 = is_64,
542 .is_64 = switch (@TypeOf(hdr)) {
543 Elf32_Ehdr => false,
544 Elf64_Ehdr => true,
545 else => @compileError("bad type"),
546 },
567547 .endian = endian,
568 .os_abi = os_abi,
569 .abi_version = abi_version,
570 .type = @"type",
571 .machine = machine,
572 .entry = int(is_64, need_bswap, hdr32.e_entry, hdr64.e_entry),
573 .phoff = int(is_64, need_bswap, hdr32.e_phoff, hdr64.e_phoff),
574 .shoff = int(is_64, need_bswap, hdr32.e_shoff, hdr64.e_shoff),
575 .phentsize = int(is_64, need_bswap, hdr32.e_phentsize, hdr64.e_phentsize),
576 .phnum = int(is_64, need_bswap, hdr32.e_phnum, hdr64.e_phnum),
577 .shentsize = int(is_64, need_bswap, hdr32.e_shentsize, hdr64.e_shentsize),
578 .shnum = int(is_64, need_bswap, hdr32.e_shnum, hdr64.e_shnum),
579 .shstrndx = int(is_64, need_bswap, hdr32.e_shstrndx, hdr64.e_shstrndx),
548 .os_abi = @enumFromInt(hdr.e_ident[EI_OSABI]),
549 .abi_version = hdr.e_ident[EI_ABIVERSION],
550 .type = hdr.e_type,
551 .machine = hdr.e_machine,
552 .entry = hdr.e_entry,
553 .phoff = hdr.e_phoff,
554 .shoff = hdr.e_shoff,
555 .phentsize = hdr.e_phentsize,
556 .phnum = hdr.e_phnum,
557 .shentsize = hdr.e_shentsize,
558 .shnum = hdr.e_shnum,
559 .shstrndx = hdr.e_shstrndx,
580560 };
581561 }
582562};
......@@ -591,21 +571,15 @@ pub const ProgramHeaderIterator = struct {
591571 defer it.index += 1;
592572
593573 if (it.elf_header.is_64) {
594 var phdr: Elf64_Phdr = undefined;
595 const offset = it.elf_header.phoff + @sizeOf(@TypeOf(phdr)) * it.index;
574 const offset = it.elf_header.phoff + @sizeOf(Elf64_Phdr) * it.index;
596575 try it.file_reader.seekTo(offset);
597 try it.file_reader.interface.readSlice(@ptrCast(&phdr));
598 if (it.elf_header.endian != native_endian)
599 mem.byteSwapAllFields(Elf64_Phdr, &phdr);
576 const phdr = try it.file_reader.interface.takeStruct(Elf64_Phdr, it.elf_header.endian);
600577 return phdr;
601578 }
602579
603 var phdr: Elf32_Phdr = undefined;
604 const offset = it.elf_header.phoff + @sizeOf(@TypeOf(phdr)) * it.index;
580 const offset = it.elf_header.phoff + @sizeOf(Elf32_Phdr) * it.index;
605581 try it.file_reader.seekTo(offset);
606 try it.file_reader.interface.readSlice(@ptrCast(&phdr));
607 if (it.elf_header.endian != native_endian)
608 mem.byteSwapAllFields(Elf32_Phdr, &phdr);
582 const phdr = try it.file_reader.interface.takeStruct(Elf32_Phdr, it.elf_header.endian);
609583 return .{
610584 .p_type = phdr.p_type,
611585 .p_offset = phdr.p_offset,
......@@ -629,21 +603,13 @@ pub const SectionHeaderIterator = struct {
629603 defer it.index += 1;
630604
631605 if (it.elf_header.is_64) {
632 var shdr: Elf64_Shdr = undefined;
633 const offset = it.elf_header.shoff + @sizeOf(@TypeOf(shdr)) * it.index;
634 try it.file_reader.seekTo(offset);
635 try it.file_reader.interface.readSlice(@ptrCast(&shdr));
636 if (it.elf_header.endian != native_endian)
637 mem.byteSwapAllFields(Elf64_Shdr, &shdr);
606 try it.file_reader.seekTo(it.elf_header.shoff + @sizeOf(Elf64_Shdr) * it.index);
607 const shdr = try it.file_reader.interface.takeStruct(Elf64_Shdr, it.elf_header.endian);
638608 return shdr;
639609 }
640610
641 var shdr: Elf32_Shdr = undefined;
642 const offset = it.elf_header.shoff + @sizeOf(@TypeOf(shdr)) * it.index;
643 try it.file_reader.seekTo(offset);
644 try it.file_reader.interface.readSlice(@ptrCast(&shdr));
645 if (it.elf_header.endian != native_endian)
646 mem.byteSwapAllFields(Elf32_Shdr, &shdr);
611 try it.file_reader.seekTo(it.elf_header.shoff + @sizeOf(Elf32_Shdr) * it.index);
612 const shdr = try it.file_reader.interface.takeStruct(Elf32_Shdr, it.elf_header.endian);
647613 return .{
648614 .sh_name = shdr.sh_name,
649615 .sh_type = shdr.sh_type,
......@@ -659,26 +625,6 @@ pub const SectionHeaderIterator = struct {
659625 }
660626};
661627
662fn int(is_64: bool, need_bswap: bool, int_32: anytype, int_64: anytype) @TypeOf(int_64) {
663 if (is_64) {
664 if (need_bswap) {
665 return @byteSwap(int_64);
666 } else {
667 return int_64;
668 }
669 } else {
670 return int32(need_bswap, int_32, @TypeOf(int_64));
671 }
672}
673
674fn int32(need_bswap: bool, int_32: anytype, comptime Int64: anytype) Int64 {
675 if (need_bswap) {
676 return @byteSwap(int_32);
677 } else {
678 return int_32;
679 }
680}
681
682628pub const ELFCLASSNONE = 0;
683629pub const ELFCLASS32 = 1;
684630pub const ELFCLASS64 = 2;
lib/std/fs/Dir.zig+30-29
......@@ -1,3 +1,20 @@
1const Dir = @This();
2const builtin = @import("builtin");
3const std = @import("../std.zig");
4const File = std.fs.File;
5const AtomicFile = std.fs.AtomicFile;
6const base64_encoder = fs.base64_encoder;
7const posix = std.posix;
8const mem = std.mem;
9const path = fs.path;
10const fs = std.fs;
11const Allocator = std.mem.Allocator;
12const assert = std.debug.assert;
13const linux = std.os.linux;
14const windows = std.os.windows;
15const native_os = builtin.os.tag;
16const have_flock = @TypeOf(posix.system.flock) != void;
17
118fd: Handle,
219
320pub const Handle = posix.fd_t;
......@@ -1862,9 +1879,10 @@ pub fn symLinkW(
18621879
18631880/// Same as `symLink`, except tries to create the symbolic link until it
18641881/// succeeds or encounters an error other than `error.PathAlreadyExists`.
1865/// On Windows, both paths should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
1866/// On WASI, both paths should be encoded as valid UTF-8.
1867/// On other platforms, both paths are an opaque sequence of bytes with no particular encoding.
1882///
1883/// * On Windows, both paths should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
1884/// * On WASI, both paths should be encoded as valid UTF-8.
1885/// * On other platforms, both paths are an opaque sequence of bytes with no particular encoding.
18681886pub fn atomicSymLink(
18691887 dir: Dir,
18701888 target_path: []const u8,
......@@ -1880,9 +1898,8 @@ pub fn atomicSymLink(
18801898
18811899 const dirname = path.dirname(sym_link_path) orelse ".";
18821900
1883 var rand_buf: [AtomicFile.random_bytes_len]u8 = undefined;
1884
1885 const temp_path_len = dirname.len + 1 + base64_encoder.calcSize(rand_buf.len);
1901 const rand_len = @sizeOf(u64) * 2;
1902 const temp_path_len = dirname.len + 1 + rand_len;
18861903 var temp_path_buf: [fs.max_path_bytes]u8 = undefined;
18871904
18881905 if (temp_path_len > temp_path_buf.len) return error.NameTooLong;
......@@ -1892,8 +1909,8 @@ pub fn atomicSymLink(
18921909 const temp_path = temp_path_buf[0..temp_path_len];
18931910
18941911 while (true) {
1895 crypto.random.bytes(rand_buf[0..]);
1896 _ = base64_encoder.encode(temp_path[dirname.len + 1 ..], rand_buf[0..]);
1912 const random_integer = std.crypto.random.int(u64);
1913 temp_path[dirname.len + 1 ..][0..rand_len].* = std.fmt.hex(random_integer);
18971914
18981915 if (dir.symLink(target_path, temp_path, flags)) {
18991916 return dir.rename(temp_path, sym_link_path);
......@@ -2623,8 +2640,9 @@ pub fn updateFile(
26232640 return .stale;
26242641}
26252642
2626pub const CopyFileError = File.OpenError || File.StatError || File.ReadError || File.WriteError ||
2627 AtomicFile.InitError || AtomicFile.FinishError;
2643pub const CopyFileError = File.OpenError || File.StatError ||
2644 AtomicFile.InitError || AtomicFile.FinishError ||
2645 File.ReadError || File.WriteError;
26282646
26292647/// Atomically creates a new file at `dest_path` within `dest_dir` with the
26302648/// same contents as `source_path` within `source_dir`, overwriting any already
......@@ -2655,7 +2673,7 @@ pub fn copyFile(
26552673 break :blk st.mode;
26562674 };
26572675
2658 var buffer: [1000]u8 = undefined; // Used only when direct fd-to-fd is not available.
2676 var buffer: [1024]u8 = undefined; // Used only when direct fd-to-fd is not available.
26592677 var atomic_file = try dest_dir.atomicFile(dest_path, .{
26602678 .mode = mode,
26612679 .write_buffer = &buffer,
......@@ -2666,6 +2684,7 @@ pub fn copyFile(
26662684 error.ReadFailed => return file_reader.err.?,
26672685 error.WriteFailed => return atomic_file.file_writer.err.?,
26682686 };
2687
26692688 try atomic_file.finish();
26702689}
26712690
......@@ -2790,21 +2809,3 @@ pub fn setPermissions(self: Dir, permissions: Permissions) SetPermissionsError!v
27902809 const file: File = .{ .handle = self.fd };
27912810 try file.setPermissions(permissions);
27922811}
2793
2794const Dir = @This();
2795const builtin = @import("builtin");
2796const std = @import("../std.zig");
2797const File = std.fs.File;
2798const AtomicFile = std.fs.AtomicFile;
2799const base64_encoder = fs.base64_encoder;
2800const crypto = std.crypto;
2801const posix = std.posix;
2802const mem = std.mem;
2803const path = fs.path;
2804const fs = std.fs;
2805const Allocator = std.mem.Allocator;
2806const assert = std.debug.assert;
2807const linux = std.os.linux;
2808const windows = std.os.windows;
2809const native_os = builtin.os.tag;
2810const have_flock = @TypeOf(posix.system.flock) != void;
lib/std/fs/File.zig+173-22
......@@ -918,7 +918,7 @@ pub const Reader = struct {
918918 err: ?ReadError = null,
919919 mode: Reader.Mode = .positional,
920920 /// Tracks the true seek position in the file. To obtain the logical
921 /// position, subtract the buffer size from this value.
921 /// position, use `logicalPos`.
922922 pos: u64 = 0,
923923 size: ?u64 = null,
924924 size_err: ?GetEndPosError = null,
......@@ -1011,14 +1011,12 @@ pub const Reader = struct {
10111011 pub fn seekBy(r: *Reader, offset: i64) Reader.SeekError!void {
10121012 switch (r.mode) {
10131013 .positional, .positional_reading => {
1014 // TODO: make += operator allow any integer types
1015 r.pos = @intCast(@as(i64, @intCast(r.pos)) + offset);
1014 setPosAdjustingBuffer(r, @intCast(@as(i64, @intCast(r.pos)) + offset));
10161015 },
10171016 .streaming, .streaming_reading => {
10181017 const seek_err = r.seek_err orelse e: {
10191018 if (posix.lseek_CUR(r.file.handle, offset)) |_| {
1020 // TODO: make += operator allow any integer types
1021 r.pos = @intCast(@as(i64, @intCast(r.pos)) + offset);
1019 setPosAdjustingBuffer(r, @intCast(@as(i64, @intCast(r.pos)) + offset));
10221020 return;
10231021 } else |err| {
10241022 r.seek_err = err;
......@@ -1034,6 +1032,8 @@ pub const Reader = struct {
10341032 r.pos += n;
10351033 remaining -= n;
10361034 }
1035 r.interface.seek = 0;
1036 r.interface.end = 0;
10371037 },
10381038 .failure => return r.seek_err.?,
10391039 }
......@@ -1042,7 +1042,7 @@ pub const Reader = struct {
10421042 pub fn seekTo(r: *Reader, offset: u64) Reader.SeekError!void {
10431043 switch (r.mode) {
10441044 .positional, .positional_reading => {
1045 r.pos = offset;
1045 setPosAdjustingBuffer(r, offset);
10461046 },
10471047 .streaming, .streaming_reading => {
10481048 if (offset >= r.pos) return Reader.seekBy(r, @intCast(offset - r.pos));
......@@ -1051,12 +1051,28 @@ pub const Reader = struct {
10511051 r.seek_err = err;
10521052 return err;
10531053 };
1054 r.pos = offset;
1054 setPosAdjustingBuffer(r, offset);
10551055 },
10561056 .failure => return r.seek_err.?,
10571057 }
10581058 }
10591059
1060 pub fn logicalPos(r: *const Reader) u64 {
1061 return r.pos - r.interface.bufferedLen();
1062 }
1063
1064 fn setPosAdjustingBuffer(r: *Reader, offset: u64) void {
1065 const logical_pos = logicalPos(r);
1066 if (offset < logical_pos or offset >= r.pos) {
1067 r.interface.seek = 0;
1068 r.interface.end = 0;
1069 r.pos = offset;
1070 } else {
1071 const logical_delta: usize = @intCast(offset - logical_pos);
1072 r.interface.seek += logical_delta;
1073 }
1074 }
1075
10601076 /// Number of slices to store on the stack, when trying to send as many byte
10611077 /// vectors through the underlying read calls as possible.
10621078 const max_buffers_len = 16;
......@@ -1106,7 +1122,7 @@ pub const Reader = struct {
11061122 return error.EndOfStream;
11071123 }
11081124 r.pos += n;
1109 return n;
1125 return w.advanceVector(n);
11101126 },
11111127 .streaming_reading => {
11121128 if (is_windows) {
......@@ -1129,7 +1145,7 @@ pub const Reader = struct {
11291145 return error.EndOfStream;
11301146 }
11311147 r.pos += n;
1132 return n;
1148 return w.advanceVector(n);
11331149 },
11341150 .failure => return error.ReadFailed,
11351151 }
......@@ -1202,7 +1218,7 @@ pub const Reader = struct {
12021218 }
12031219 return 0;
12041220 };
1205 const n = @min(size - pos, std.math.maxInt(i64), @intFromEnum(limit));
1221 const n = @min(size - pos, maxInt(i64), @intFromEnum(limit));
12061222 file.seekBy(n) catch |err| {
12071223 r.seek_err = err;
12081224 return 0;
......@@ -1391,7 +1407,6 @@ pub const Writer = struct {
13911407 const pattern = data[data.len - 1];
13921408 if (pattern.len == 0 or splat == 0) return 0;
13931409 const n = windows.WriteFile(handle, pattern, null) catch |err| {
1394 std.debug.print("windows write file failed3: {t}\n", .{err});
13951410 w.err = err;
13961411 return error.WriteFailed;
13971412 };
......@@ -1493,18 +1508,141 @@ pub const Writer = struct {
14931508 file_reader: *Reader,
14941509 limit: std.io.Limit,
14951510 ) std.io.Writer.FileError!usize {
1511 const reader_buffered = file_reader.interface.buffered();
1512 if (reader_buffered.len >= @intFromEnum(limit))
1513 return sendFileBuffered(io_w, file_reader, reader_buffered);
1514 const writer_buffered = io_w.buffered();
1515 const file_limit = @intFromEnum(limit) - reader_buffered.len;
14961516 const w: *Writer = @alignCast(@fieldParentPtr("interface", io_w));
14971517 const out_fd = w.file.handle;
14981518 const in_fd = file_reader.file.handle;
1499 // TODO try using copy_file_range on FreeBSD
1500 // TODO try using sendfile on macOS
1501 // TODO try using sendfile on FreeBSD
1519
1520 if (file_reader.size) |size| {
1521 if (size - file_reader.pos == 0) {
1522 if (reader_buffered.len != 0) {
1523 return sendFileBuffered(io_w, file_reader, reader_buffered);
1524 } else {
1525 return error.EndOfStream;
1526 }
1527 }
1528 }
1529
1530 if (native_os == .freebsd and w.mode == .streaming) sf: {
1531 // Try using sendfile on FreeBSD.
1532 if (w.sendfile_err != null) break :sf;
1533 const offset = std.math.cast(std.c.off_t, file_reader.pos) orelse break :sf;
1534 var hdtr_data: std.c.sf_hdtr = undefined;
1535 var headers: [2]posix.iovec_const = undefined;
1536 var headers_i: u8 = 0;
1537 if (writer_buffered.len != 0) {
1538 headers[headers_i] = .{ .base = writer_buffered.ptr, .len = writer_buffered.len };
1539 headers_i += 1;
1540 }
1541 if (reader_buffered.len != 0) {
1542 headers[headers_i] = .{ .base = reader_buffered.ptr, .len = reader_buffered.len };
1543 headers_i += 1;
1544 }
1545 const hdtr: ?*std.c.sf_hdtr = if (headers_i == 0) null else b: {
1546 hdtr_data = .{
1547 .headers = &headers,
1548 .hdr_cnt = headers_i,
1549 .trailers = null,
1550 .trl_cnt = 0,
1551 };
1552 break :b &hdtr_data;
1553 };
1554 var sbytes: std.c.off_t = undefined;
1555 const nbytes: usize = @min(file_limit, maxInt(usize));
1556 const flags = 0;
1557 switch (posix.errno(std.c.sendfile(in_fd, out_fd, offset, nbytes, hdtr, &sbytes, flags))) {
1558 .SUCCESS, .INTR => {},
1559 .INVAL, .OPNOTSUPP, .NOTSOCK, .NOSYS => w.sendfile_err = error.UnsupportedOperation,
1560 .BADF => if (builtin.mode == .Debug) @panic("race condition") else {
1561 w.sendfile_err = error.Unexpected;
1562 },
1563 .FAULT => if (builtin.mode == .Debug) @panic("segmentation fault") else {
1564 w.sendfile_err = error.Unexpected;
1565 },
1566 .NOTCONN => w.sendfile_err = error.BrokenPipe,
1567 .AGAIN, .BUSY => if (sbytes == 0) {
1568 w.sendfile_err = error.WouldBlock;
1569 },
1570 .IO => w.sendfile_err = error.InputOutput,
1571 .PIPE => w.sendfile_err = error.BrokenPipe,
1572 .NOBUFS => w.sendfile_err = error.SystemResources,
1573 else => |err| w.sendfile_err = posix.unexpectedErrno(err),
1574 }
1575 if (sbytes == 0) {
1576 file_reader.size = file_reader.pos;
1577 return error.EndOfStream;
1578 }
1579 const consumed = io_w.consume(@intCast(sbytes));
1580 file_reader.seekTo(file_reader.pos + consumed) catch return error.ReadFailed;
1581 return consumed;
1582 }
1583
1584 if (native_os.isDarwin() and w.mode == .streaming) sf: {
1585 // Try using sendfile on macOS.
1586 if (w.sendfile_err != null) break :sf;
1587 const offset = std.math.cast(std.c.off_t, file_reader.pos) orelse break :sf;
1588 var hdtr_data: std.c.sf_hdtr = undefined;
1589 var headers: [2]posix.iovec_const = undefined;
1590 var headers_i: u8 = 0;
1591 if (writer_buffered.len != 0) {
1592 headers[headers_i] = .{ .base = writer_buffered.ptr, .len = writer_buffered.len };
1593 headers_i += 1;
1594 }
1595 if (reader_buffered.len != 0) {
1596 headers[headers_i] = .{ .base = reader_buffered.ptr, .len = reader_buffered.len };
1597 headers_i += 1;
1598 }
1599 const hdtr: ?*std.c.sf_hdtr = if (headers_i == 0) null else b: {
1600 hdtr_data = .{
1601 .headers = &headers,
1602 .hdr_cnt = headers_i,
1603 .trailers = null,
1604 .trl_cnt = 0,
1605 };
1606 break :b &hdtr_data;
1607 };
1608 const max_count = maxInt(i32); // Avoid EINVAL.
1609 var len: std.c.off_t = @min(file_limit, max_count);
1610 const flags = 0;
1611 switch (posix.errno(std.c.sendfile(in_fd, out_fd, offset, &len, hdtr, flags))) {
1612 .SUCCESS, .INTR => {},
1613 .OPNOTSUPP, .NOTSOCK, .NOSYS => w.sendfile_err = error.UnsupportedOperation,
1614 .BADF => if (builtin.mode == .Debug) @panic("race condition") else {
1615 w.sendfile_err = error.Unexpected;
1616 },
1617 .FAULT => if (builtin.mode == .Debug) @panic("segmentation fault") else {
1618 w.sendfile_err = error.Unexpected;
1619 },
1620 .INVAL => if (builtin.mode == .Debug) @panic("invalid API usage") else {
1621 w.sendfile_err = error.Unexpected;
1622 },
1623 .NOTCONN => w.sendfile_err = error.BrokenPipe,
1624 .AGAIN => if (len == 0) {
1625 w.sendfile_err = error.WouldBlock;
1626 },
1627 .IO => w.sendfile_err = error.InputOutput,
1628 .PIPE => w.sendfile_err = error.BrokenPipe,
1629 else => |err| w.sendfile_err = posix.unexpectedErrno(err),
1630 }
1631 if (len == 0) {
1632 file_reader.size = file_reader.pos;
1633 return error.EndOfStream;
1634 }
1635 const consumed = io_w.consume(@bitCast(len));
1636 file_reader.seekTo(file_reader.pos + consumed) catch return error.ReadFailed;
1637 return consumed;
1638 }
1639
15021640 if (native_os == .linux and w.mode == .streaming) sf: {
15031641 // Try using sendfile on Linux.
15041642 if (w.sendfile_err != null) break :sf;
15051643 // Linux sendfile does not support headers.
1506 const buffered = limit.slice(file_reader.interface.buffer);
1507 if (io_w.end != 0 or buffered.len != 0) return drain(io_w, &.{buffered}, 1);
1644 if (writer_buffered.len != 0 or reader_buffered.len != 0)
1645 return sendFileBuffered(io_w, file_reader, reader_buffered);
15081646 const max_count = 0x7ffff000; // Avoid EINVAL.
15091647 var off: std.os.linux.off_t = undefined;
15101648 const off_ptr: ?*std.os.linux.off_t, const count: usize = switch (file_reader.mode) {
......@@ -1551,6 +1689,7 @@ pub const Writer = struct {
15511689 w.pos += n;
15521690 return n;
15531691 }
1692
15541693 const copy_file_range = switch (native_os) {
15551694 .freebsd => std.os.freebsd.copy_file_range,
15561695 .linux => if (std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 })) std.os.linux.wrapped.copy_file_range else {},
......@@ -1558,8 +1697,8 @@ pub const Writer = struct {
15581697 };
15591698 if (@TypeOf(copy_file_range) != void) cfr: {
15601699 if (w.copy_file_range_err != null) break :cfr;
1561 const buffered = limit.slice(file_reader.interface.buffer);
1562 if (io_w.end != 0 or buffered.len != 0) return drain(io_w, &.{buffered}, 1);
1700 if (writer_buffered.len != 0 or reader_buffered.len != 0)
1701 return sendFileBuffered(io_w, file_reader, reader_buffered);
15631702 var off_in: i64 = undefined;
15641703 var off_out: i64 = undefined;
15651704 const off_in_ptr: ?*i64 = switch (file_reader.mode) {
......@@ -1598,6 +1737,9 @@ pub const Writer = struct {
15981737 if (file_reader.pos != 0) break :fcf;
15991738 if (w.pos != 0) break :fcf;
16001739 if (limit != .unlimited) break :fcf;
1740 const size = file_reader.getSize() catch break :fcf;
1741 if (writer_buffered.len != 0 or reader_buffered.len != 0)
1742 return sendFileBuffered(io_w, file_reader, reader_buffered);
16011743 const rc = std.c.fcopyfile(in_fd, out_fd, null, .{ .DATA = true });
16021744 switch (posix.errno(rc)) {
16031745 .SUCCESS => {},
......@@ -1618,15 +1760,24 @@ pub const Writer = struct {
16181760 return 0;
16191761 },
16201762 }
1621 const n = if (file_reader.size) |size| size else @panic("TODO figure out how much copied");
1622 file_reader.pos = n;
1623 w.pos = n;
1624 return n;
1763 file_reader.pos = size;
1764 w.pos = size;
1765 return size;
16251766 }
16261767
16271768 return error.Unimplemented;
16281769 }
16291770
1771 fn sendFileBuffered(
1772 io_w: *std.io.Writer,
1773 file_reader: *Reader,
1774 reader_buffered: []const u8,
1775 ) std.io.Writer.FileError!usize {
1776 const n = try drain(io_w, &.{reader_buffered}, 1);
1777 file_reader.seekTo(file_reader.pos + n) catch return error.ReadFailed;
1778 return n;
1779 }
1780
16301781 pub fn seekTo(w: *Writer, offset: u64) SeekError!void {
16311782 switch (w.mode) {
16321783 .positional, .positional_reading => {
lib/std/fs/test.zig+84-26
......@@ -1499,32 +1499,18 @@ test "sendfile" {
14991499 const header2 = "second header\n";
15001500 const trailer1 = "trailer1\n";
15011501 const trailer2 = "second trailer\n";
1502 var hdtr = [_]posix.iovec_const{
1503 .{
1504 .base = header1,
1505 .len = header1.len,
1506 },
1507 .{
1508 .base = header2,
1509 .len = header2.len,
1510 },
1511 .{
1512 .base = trailer1,
1513 .len = trailer1.len,
1514 },
1515 .{
1516 .base = trailer2,
1517 .len = trailer2.len,
1518 },
1519 };
1502 var headers: [2][]const u8 = .{ header1, header2 };
1503 var trailers: [2][]const u8 = .{ trailer1, trailer2 };
15201504
15211505 var written_buf: [100]u8 = undefined;
1522 try dest_file.writeFileAll(src_file, .{
1523 .in_offset = 1,
1524 .in_len = 10,
1525 .headers_and_trailers = &hdtr,
1526 .header_count = 2,
1527 });
1506 var file_reader = src_file.reader(&.{});
1507 var fallback_buffer: [50]u8 = undefined;
1508 var file_writer = dest_file.writer(&fallback_buffer);
1509 try file_writer.interface.writeVecAll(&headers);
1510 try file_reader.seekTo(1);
1511 try testing.expectEqual(10, try file_writer.interface.sendFileAll(&file_reader, .limited(10)));
1512 try file_writer.interface.writeVecAll(&trailers);
1513 try file_writer.interface.flush();
15281514 const amt = try dest_file.preadAll(&written_buf, 0);
15291515 try testing.expectEqualStrings("header1\nsecond header\nine1\nsecontrailer1\nsecond trailer\n", written_buf[0..amt]);
15301516}
......@@ -1595,9 +1581,10 @@ test "AtomicFile" {
15951581 ;
15961582
15971583 {
1598 var af = try ctx.dir.atomicFile(test_out_file, .{});
1584 var buffer: [100]u8 = undefined;
1585 var af = try ctx.dir.atomicFile(test_out_file, .{ .write_buffer = &buffer });
15991586 defer af.deinit();
1600 try af.file.writeAll(test_content);
1587 try af.file_writer.interface.writeAll(test_content);
16011588 try af.finish();
16021589 }
16031590 const content = try ctx.dir.readFileAlloc(allocator, test_out_file, 9999);
......@@ -2071,3 +2058,74 @@ test "invalid UTF-8/WTF-8 paths" {
20712058 }
20722059 }.impl);
20732060}
2061
2062test "read file non vectored" {
2063 var tmp_dir = testing.tmpDir(.{});
2064 defer tmp_dir.cleanup();
2065
2066 const contents = "hello, world!\n";
2067
2068 const file = try tmp_dir.dir.createFile("input.txt", .{ .read = true });
2069 defer file.close();
2070 {
2071 var file_writer: std.fs.File.Writer = .init(file, &.{});
2072 try file_writer.interface.writeAll(contents);
2073 try file_writer.interface.flush();
2074 }
2075
2076 var file_reader: std.fs.File.Reader = .init(file, &.{});
2077
2078 var write_buffer: [100]u8 = undefined;
2079 var w: std.Io.Writer = .fixed(&write_buffer);
2080
2081 var i: usize = 0;
2082 while (true) {
2083 i += file_reader.interface.stream(&w, .limited(3)) catch |err| switch (err) {
2084 error.EndOfStream => break,
2085 else => |e| return e,
2086 };
2087 }
2088 try testing.expectEqualStrings(contents, w.buffered());
2089 try testing.expectEqual(contents.len, i);
2090}
2091
2092test "seek keeping partial buffer" {
2093 var tmp_dir = testing.tmpDir(.{});
2094 defer tmp_dir.cleanup();
2095
2096 const contents = "0123456789";
2097
2098 const file = try tmp_dir.dir.createFile("input.txt", .{ .read = true });
2099 defer file.close();
2100 {
2101 var file_writer: std.fs.File.Writer = .init(file, &.{});
2102 try file_writer.interface.writeAll(contents);
2103 try file_writer.interface.flush();
2104 }
2105
2106 var read_buffer: [3]u8 = undefined;
2107 var file_reader: std.fs.File.Reader = .init(file, &read_buffer);
2108
2109 try testing.expectEqual(0, file_reader.logicalPos());
2110
2111 var buf: [4]u8 = undefined;
2112 try file_reader.interface.readSliceAll(&buf);
2113
2114 if (file_reader.interface.bufferedLen() != 3) {
2115 // Pass the test if the OS doesn't give us vectored reads.
2116 return;
2117 }
2118
2119 try testing.expectEqual(4, file_reader.logicalPos());
2120 try testing.expectEqual(7, file_reader.pos);
2121 try file_reader.seekTo(6);
2122 try testing.expectEqual(6, file_reader.logicalPos());
2123 try testing.expectEqual(7, file_reader.pos);
2124
2125 try testing.expectEqualStrings("0123", &buf);
2126
2127 const n = try file_reader.interface.readSliceShort(&buf);
2128 try testing.expectEqual(4, n);
2129
2130 try testing.expectEqualStrings("6789", &buf);
2131}
lib/std/json.zig-1
......@@ -69,7 +69,6 @@ pub const ArrayHashMap = @import("json/hashmap.zig").ArrayHashMap;
6969pub const Scanner = @import("json/Scanner.zig");
7070pub const validate = Scanner.validate;
7171pub const Error = Scanner.Error;
72pub const reader = Scanner.reader;
7372pub const default_buffer_size = Scanner.default_buffer_size;
7473pub const Token = Scanner.Token;
7574pub const TokenType = Scanner.TokenType;
lib/std/posix.zig-282
......@@ -6322,288 +6322,6 @@ pub fn send(
63226322 };
63236323}
63246324
6325pub const SendFileError = PReadError || WriteError || SendError;
6326
6327/// Transfer data between file descriptors, with optional headers and trailers.
6328///
6329/// Returns the number of bytes written, which can be zero.
6330///
6331/// The `sendfile` call copies `in_len` bytes from one file descriptor to another. When possible,
6332/// this is done within the operating system kernel, which can provide better performance
6333/// characteristics than transferring data from kernel to user space and back, such as with
6334/// `read` and `write` calls. When `in_len` is `0`, it means to copy until the end of the input file has been
6335/// reached. Note, however, that partial writes are still possible in this case.
6336///
6337/// `in_fd` must be a file descriptor opened for reading, and `out_fd` must be a file descriptor
6338/// opened for writing. They may be any kind of file descriptor; however, if `in_fd` is not a regular
6339/// file system file, it may cause this function to fall back to calling `read` and `write`, in which case
6340/// atomicity guarantees no longer apply.
6341///
6342/// Copying begins reading at `in_offset`. The input file descriptor seek position is ignored and not updated.
6343/// If the output file descriptor has a seek position, it is updated as bytes are written. When
6344/// `in_offset` is past the end of the input file, it successfully reads 0 bytes.
6345///
6346/// `flags` has different meanings per operating system; refer to the respective man pages.
6347///
6348/// These systems support atomically sending everything, including headers and trailers:
6349/// * macOS
6350/// * FreeBSD
6351///
6352/// These systems support in-kernel data copying, but headers and trailers are not sent atomically:
6353/// * Linux
6354///
6355/// Other systems fall back to calling `read` / `write`.
6356///
6357/// Linux has a limit on how many bytes may be transferred in one `sendfile` call, which is `0x7ffff000`
6358/// on both 64-bit and 32-bit systems. This is due to using a signed C int as the return value, as
6359/// well as stuffing the errno codes into the last `4096` values. This is noted on the `sendfile` man page.
6360/// The limit on Darwin is `0x7fffffff`, trying to write more than that returns EINVAL.
6361/// The corresponding POSIX limit on this is `maxInt(isize)`.
6362pub fn sendfile(
6363 out_fd: fd_t,
6364 in_fd: fd_t,
6365 in_offset: u64,
6366 in_len: u64,
6367 headers: []const iovec_const,
6368 trailers: []const iovec_const,
6369 flags: u32,
6370) SendFileError!usize {
6371 var header_done = false;
6372 var total_written: usize = 0;
6373
6374 // Prevents EOVERFLOW.
6375 const size_t = std.meta.Int(.unsigned, @typeInfo(usize).int.bits - 1);
6376 const max_count = switch (native_os) {
6377 .linux => 0x7ffff000,
6378 .macos, .ios, .watchos, .tvos, .visionos => maxInt(i32),
6379 else => maxInt(size_t),
6380 };
6381
6382 switch (native_os) {
6383 .linux => sf: {
6384 if (headers.len != 0) {
6385 const amt = try writev(out_fd, headers);
6386 total_written += amt;
6387 if (amt < count_iovec_bytes(headers)) return total_written;
6388 header_done = true;
6389 }
6390
6391 // Here we match BSD behavior, making a zero count value send as many bytes as possible.
6392 const adjusted_count = if (in_len == 0) max_count else @min(in_len, max_count);
6393
6394 const sendfile_sym = if (lfs64_abi) system.sendfile64 else system.sendfile;
6395 while (true) {
6396 var offset: off_t = @bitCast(in_offset);
6397 const rc = sendfile_sym(out_fd, in_fd, &offset, adjusted_count);
6398 switch (errno(rc)) {
6399 .SUCCESS => {
6400 const amt: usize = @bitCast(rc);
6401 total_written += amt;
6402 return total_written;
6403 },
6404
6405 .BADF => unreachable, // Always a race condition.
6406 .FAULT => unreachable, // Segmentation fault.
6407 .OVERFLOW => unreachable, // We avoid passing too large of a `count`.
6408 .NOTCONN => return error.BrokenPipe, // `out_fd` is an unconnected socket
6409
6410 .INVAL => {
6411 // EINVAL could be any of the following situations:
6412 // * Descriptor is not valid or locked
6413 // * an mmap(2)-like operation is not available for in_fd
6414 // * count is negative
6415 // * out_fd has the APPEND flag set
6416 // Because of the "mmap(2)-like operation" possibility, we fall back to doing read/write
6417 // manually.
6418 break :sf;
6419 },
6420 .AGAIN => return error.WouldBlock,
6421 .IO => return error.InputOutput,
6422 .PIPE => return error.BrokenPipe,
6423 .NOMEM => return error.SystemResources,
6424 .NXIO => return error.Unseekable,
6425 .SPIPE => return error.Unseekable,
6426 else => |err| {
6427 unexpectedErrno(err) catch {};
6428 break :sf;
6429 },
6430 }
6431 }
6432
6433 if (trailers.len != 0) {
6434 total_written += try writev(out_fd, trailers);
6435 }
6436
6437 return total_written;
6438 },
6439 .freebsd => sf: {
6440 var hdtr_data: std.c.sf_hdtr = undefined;
6441 var hdtr: ?*std.c.sf_hdtr = null;
6442 if (headers.len != 0 or trailers.len != 0) {
6443 // Here we carefully avoid `@intCast` by returning partial writes when
6444 // too many io vectors are provided.
6445 const hdr_cnt = cast(u31, headers.len) orelse maxInt(u31);
6446 if (headers.len > hdr_cnt) return writev(out_fd, headers);
6447
6448 const trl_cnt = cast(u31, trailers.len) orelse maxInt(u31);
6449
6450 hdtr_data = std.c.sf_hdtr{
6451 .headers = headers.ptr,
6452 .hdr_cnt = hdr_cnt,
6453 .trailers = trailers.ptr,
6454 .trl_cnt = trl_cnt,
6455 };
6456 hdtr = &hdtr_data;
6457 }
6458
6459 while (true) {
6460 var sbytes: off_t = undefined;
6461 const err = errno(system.sendfile(in_fd, out_fd, @bitCast(in_offset), @min(in_len, max_count), hdtr, &sbytes, flags));
6462 const amt: usize = @bitCast(sbytes);
6463 switch (err) {
6464 .SUCCESS => return amt,
6465
6466 .BADF => unreachable, // Always a race condition.
6467 .FAULT => unreachable, // Segmentation fault.
6468 .NOTCONN => return error.BrokenPipe, // `out_fd` is an unconnected socket
6469
6470 .INVAL, .OPNOTSUPP, .NOTSOCK, .NOSYS => {
6471 // EINVAL could be any of the following situations:
6472 // * The fd argument is not a regular file.
6473 // * The s argument is not a SOCK.STREAM type socket.
6474 // * The offset argument is negative.
6475 // Because of some of these possibilities, we fall back to doing read/write
6476 // manually, the same as ENOSYS.
6477 break :sf;
6478 },
6479
6480 .INTR => if (amt != 0) return amt else continue,
6481
6482 .AGAIN => if (amt != 0) {
6483 return amt;
6484 } else {
6485 return error.WouldBlock;
6486 },
6487
6488 .BUSY => if (amt != 0) {
6489 return amt;
6490 } else {
6491 return error.WouldBlock;
6492 },
6493
6494 .IO => return error.InputOutput,
6495 .NOBUFS => return error.SystemResources,
6496 .PIPE => return error.BrokenPipe,
6497
6498 else => {
6499 unexpectedErrno(err) catch {};
6500 if (amt != 0) {
6501 return amt;
6502 } else {
6503 break :sf;
6504 }
6505 },
6506 }
6507 }
6508 },
6509 .macos, .ios, .tvos, .watchos, .visionos => sf: {
6510 var hdtr_data: std.c.sf_hdtr = undefined;
6511 var hdtr: ?*std.c.sf_hdtr = null;
6512 if (headers.len != 0 or trailers.len != 0) {
6513 // Here we carefully avoid `@intCast` by returning partial writes when
6514 // too many io vectors are provided.
6515 const hdr_cnt = cast(u31, headers.len) orelse maxInt(u31);
6516 if (headers.len > hdr_cnt) return writev(out_fd, headers);
6517
6518 const trl_cnt = cast(u31, trailers.len) orelse maxInt(u31);
6519
6520 hdtr_data = std.c.sf_hdtr{
6521 .headers = headers.ptr,
6522 .hdr_cnt = hdr_cnt,
6523 .trailers = trailers.ptr,
6524 .trl_cnt = trl_cnt,
6525 };
6526 hdtr = &hdtr_data;
6527 }
6528
6529 while (true) {
6530 var sbytes: off_t = @min(in_len, max_count);
6531 const err = errno(system.sendfile(in_fd, out_fd, @bitCast(in_offset), &sbytes, hdtr, flags));
6532 const amt: usize = @bitCast(sbytes);
6533 switch (err) {
6534 .SUCCESS => return amt,
6535
6536 .BADF => unreachable, // Always a race condition.
6537 .FAULT => unreachable, // Segmentation fault.
6538 .INVAL => unreachable,
6539 .NOTCONN => return error.BrokenPipe, // `out_fd` is an unconnected socket
6540
6541 .OPNOTSUPP, .NOTSOCK, .NOSYS => break :sf,
6542
6543 .INTR => if (amt != 0) return amt else continue,
6544
6545 .AGAIN => if (amt != 0) {
6546 return amt;
6547 } else {
6548 return error.WouldBlock;
6549 },
6550
6551 .IO => return error.InputOutput,
6552 .PIPE => return error.BrokenPipe,
6553
6554 else => {
6555 unexpectedErrno(err) catch {};
6556 if (amt != 0) {
6557 return amt;
6558 } else {
6559 break :sf;
6560 }
6561 },
6562 }
6563 }
6564 },
6565 else => {}, // fall back to read/write
6566 }
6567
6568 if (headers.len != 0 and !header_done) {
6569 const amt = try writev(out_fd, headers);
6570 total_written += amt;
6571 if (amt < count_iovec_bytes(headers)) return total_written;
6572 }
6573
6574 rw: {
6575 var buf: [8 * 4096]u8 = undefined;
6576 // Here we match BSD behavior, making a zero count value send as many bytes as possible.
6577 const adjusted_count = if (in_len == 0) buf.len else @min(buf.len, in_len);
6578 const amt_read = try pread(in_fd, buf[0..adjusted_count], in_offset);
6579 if (amt_read == 0) {
6580 if (in_len == 0) {
6581 // We have detected EOF from `in_fd`.
6582 break :rw;
6583 } else {
6584 return total_written;
6585 }
6586 }
6587 const amt_written = try write(out_fd, buf[0..amt_read]);
6588 total_written += amt_written;
6589 if (amt_written < in_len or in_len == 0) return total_written;
6590 }
6591
6592 if (trailers.len != 0) {
6593 total_written += try writev(out_fd, trailers);
6594 }
6595
6596 return total_written;
6597}
6598
6599fn count_iovec_bytes(iovs: []const iovec_const) usize {
6600 var count: usize = 0;
6601 for (iovs) |iov| {
6602 count += iov.len;
6603 }
6604 return count;
6605}
6606
66076325pub const PollError = error{
66086326 /// The network subsystem has failed.
66096327 NetworkSubsystemFailed,
src/Builtin.zig+2-2
......@@ -342,9 +342,9 @@ pub fn updateFileOnDisk(file: *File, comp: *Compilation) !void {
342342 }
343343
344344 // `make_path` matters because the dir hasn't actually been created yet.
345 var af = try root_dir.atomicFile(sub_path, .{ .make_path = true });
345 var af = try root_dir.atomicFile(sub_path, .{ .make_path = true, .write_buffer = &.{} });
346346 defer af.deinit();
347 try af.file.writeAll(file.source.?);
347 try af.file_writer.interface.writeAll(file.source.?);
348348 af.finish() catch |err| switch (err) {
349349 error.AccessDenied => switch (builtin.os.tag) {
350350 .windows => {
src/Compilation.zig+117-117
......@@ -3382,7 +3382,7 @@ pub fn saveState(comp: *Compilation) !void {
33823382
33833383 const gpa = comp.gpa;
33843384
3385 var bufs = std.ArrayList(std.posix.iovec_const).init(gpa);
3385 var bufs = std.ArrayList([]const u8).init(gpa);
33863386 defer bufs.deinit();
33873387
33883388 var pt_headers = std.ArrayList(Header.PerThread).init(gpa);
......@@ -3421,50 +3421,50 @@ pub fn saveState(comp: *Compilation) !void {
34213421
34223422 try bufs.ensureTotalCapacityPrecise(14 + 8 * pt_headers.items.len);
34233423 addBuf(&bufs, mem.asBytes(&header));
3424 addBuf(&bufs, mem.sliceAsBytes(pt_headers.items));
3425
3426 addBuf(&bufs, mem.sliceAsBytes(ip.src_hash_deps.keys()));
3427 addBuf(&bufs, mem.sliceAsBytes(ip.src_hash_deps.values()));
3428 addBuf(&bufs, mem.sliceAsBytes(ip.nav_val_deps.keys()));
3429 addBuf(&bufs, mem.sliceAsBytes(ip.nav_val_deps.values()));
3430 addBuf(&bufs, mem.sliceAsBytes(ip.nav_ty_deps.keys()));
3431 addBuf(&bufs, mem.sliceAsBytes(ip.nav_ty_deps.values()));
3432 addBuf(&bufs, mem.sliceAsBytes(ip.interned_deps.keys()));
3433 addBuf(&bufs, mem.sliceAsBytes(ip.interned_deps.values()));
3434 addBuf(&bufs, mem.sliceAsBytes(ip.zon_file_deps.keys()));
3435 addBuf(&bufs, mem.sliceAsBytes(ip.zon_file_deps.values()));
3436 addBuf(&bufs, mem.sliceAsBytes(ip.embed_file_deps.keys()));
3437 addBuf(&bufs, mem.sliceAsBytes(ip.embed_file_deps.values()));
3438 addBuf(&bufs, mem.sliceAsBytes(ip.namespace_deps.keys()));
3439 addBuf(&bufs, mem.sliceAsBytes(ip.namespace_deps.values()));
3440 addBuf(&bufs, mem.sliceAsBytes(ip.namespace_name_deps.keys()));
3441 addBuf(&bufs, mem.sliceAsBytes(ip.namespace_name_deps.values()));
3442
3443 addBuf(&bufs, mem.sliceAsBytes(ip.first_dependency.keys()));
3444 addBuf(&bufs, mem.sliceAsBytes(ip.first_dependency.values()));
3445 addBuf(&bufs, mem.sliceAsBytes(ip.dep_entries.items));
3446 addBuf(&bufs, mem.sliceAsBytes(ip.free_dep_entries.items));
3424 addBuf(&bufs, @ptrCast(pt_headers.items));
3425
3426 addBuf(&bufs, @ptrCast(ip.src_hash_deps.keys()));
3427 addBuf(&bufs, @ptrCast(ip.src_hash_deps.values()));
3428 addBuf(&bufs, @ptrCast(ip.nav_val_deps.keys()));
3429 addBuf(&bufs, @ptrCast(ip.nav_val_deps.values()));
3430 addBuf(&bufs, @ptrCast(ip.nav_ty_deps.keys()));
3431 addBuf(&bufs, @ptrCast(ip.nav_ty_deps.values()));
3432 addBuf(&bufs, @ptrCast(ip.interned_deps.keys()));
3433 addBuf(&bufs, @ptrCast(ip.interned_deps.values()));
3434 addBuf(&bufs, @ptrCast(ip.zon_file_deps.keys()));
3435 addBuf(&bufs, @ptrCast(ip.zon_file_deps.values()));
3436 addBuf(&bufs, @ptrCast(ip.embed_file_deps.keys()));
3437 addBuf(&bufs, @ptrCast(ip.embed_file_deps.values()));
3438 addBuf(&bufs, @ptrCast(ip.namespace_deps.keys()));
3439 addBuf(&bufs, @ptrCast(ip.namespace_deps.values()));
3440 addBuf(&bufs, @ptrCast(ip.namespace_name_deps.keys()));
3441 addBuf(&bufs, @ptrCast(ip.namespace_name_deps.values()));
3442
3443 addBuf(&bufs, @ptrCast(ip.first_dependency.keys()));
3444 addBuf(&bufs, @ptrCast(ip.first_dependency.values()));
3445 addBuf(&bufs, @ptrCast(ip.dep_entries.items));
3446 addBuf(&bufs, @ptrCast(ip.free_dep_entries.items));
34473447
34483448 for (ip.locals, pt_headers.items) |*local, pt_header| {
34493449 if (pt_header.intern_pool.limbs_len > 0) {
3450 addBuf(&bufs, mem.sliceAsBytes(local.shared.limbs.view().items(.@"0")[0..pt_header.intern_pool.limbs_len]));
3450 addBuf(&bufs, @ptrCast(local.shared.limbs.view().items(.@"0")[0..pt_header.intern_pool.limbs_len]));
34513451 }
34523452 if (pt_header.intern_pool.extra_len > 0) {
3453 addBuf(&bufs, mem.sliceAsBytes(local.shared.extra.view().items(.@"0")[0..pt_header.intern_pool.extra_len]));
3453 addBuf(&bufs, @ptrCast(local.shared.extra.view().items(.@"0")[0..pt_header.intern_pool.extra_len]));
34543454 }
34553455 if (pt_header.intern_pool.items_len > 0) {
3456 addBuf(&bufs, mem.sliceAsBytes(local.shared.items.view().items(.data)[0..pt_header.intern_pool.items_len]));
3457 addBuf(&bufs, mem.sliceAsBytes(local.shared.items.view().items(.tag)[0..pt_header.intern_pool.items_len]));
3456 addBuf(&bufs, @ptrCast(local.shared.items.view().items(.data)[0..pt_header.intern_pool.items_len]));
3457 addBuf(&bufs, @ptrCast(local.shared.items.view().items(.tag)[0..pt_header.intern_pool.items_len]));
34583458 }
34593459 if (pt_header.intern_pool.string_bytes_len > 0) {
34603460 addBuf(&bufs, local.shared.strings.view().items(.@"0")[0..pt_header.intern_pool.string_bytes_len]);
34613461 }
34623462 if (pt_header.intern_pool.tracked_insts_len > 0) {
3463 addBuf(&bufs, mem.sliceAsBytes(local.shared.tracked_insts.view().items(.@"0")[0..pt_header.intern_pool.tracked_insts_len]));
3463 addBuf(&bufs, @ptrCast(local.shared.tracked_insts.view().items(.@"0")[0..pt_header.intern_pool.tracked_insts_len]));
34643464 }
34653465 if (pt_header.intern_pool.files_len > 0) {
3466 addBuf(&bufs, mem.sliceAsBytes(local.shared.files.view().items(.bin_digest)[0..pt_header.intern_pool.files_len]));
3467 addBuf(&bufs, mem.sliceAsBytes(local.shared.files.view().items(.root_type)[0..pt_header.intern_pool.files_len]));
3466 addBuf(&bufs, @ptrCast(local.shared.files.view().items(.bin_digest)[0..pt_header.intern_pool.files_len]));
3467 addBuf(&bufs, @ptrCast(local.shared.files.view().items(.root_type)[0..pt_header.intern_pool.files_len]));
34683468 }
34693469 }
34703470
......@@ -3482,95 +3482,95 @@ pub fn saveState(comp: *Compilation) !void {
34823482 try bufs.ensureUnusedCapacity(85);
34833483 addBuf(&bufs, wasm.string_bytes.items);
34843484 // TODO make it well-defined memory layout
3485 //addBuf(&bufs, mem.sliceAsBytes(wasm.objects.items));
3486 addBuf(&bufs, mem.sliceAsBytes(wasm.func_types.keys()));
3487 addBuf(&bufs, mem.sliceAsBytes(wasm.object_function_imports.keys()));
3488 addBuf(&bufs, mem.sliceAsBytes(wasm.object_function_imports.values()));
3489 addBuf(&bufs, mem.sliceAsBytes(wasm.object_functions.items));
3490 addBuf(&bufs, mem.sliceAsBytes(wasm.object_global_imports.keys()));
3491 addBuf(&bufs, mem.sliceAsBytes(wasm.object_global_imports.values()));
3492 addBuf(&bufs, mem.sliceAsBytes(wasm.object_globals.items));
3493 addBuf(&bufs, mem.sliceAsBytes(wasm.object_table_imports.keys()));
3494 addBuf(&bufs, mem.sliceAsBytes(wasm.object_table_imports.values()));
3495 addBuf(&bufs, mem.sliceAsBytes(wasm.object_tables.items));
3496 addBuf(&bufs, mem.sliceAsBytes(wasm.object_memory_imports.keys()));
3497 addBuf(&bufs, mem.sliceAsBytes(wasm.object_memory_imports.values()));
3498 addBuf(&bufs, mem.sliceAsBytes(wasm.object_memories.items));
3499 addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations.items(.tag)));
3500 addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations.items(.offset)));
3485 //addBuf(&bufs, @ptrCast(wasm.objects.items));
3486 addBuf(&bufs, @ptrCast(wasm.func_types.keys()));
3487 addBuf(&bufs, @ptrCast(wasm.object_function_imports.keys()));
3488 addBuf(&bufs, @ptrCast(wasm.object_function_imports.values()));
3489 addBuf(&bufs, @ptrCast(wasm.object_functions.items));
3490 addBuf(&bufs, @ptrCast(wasm.object_global_imports.keys()));
3491 addBuf(&bufs, @ptrCast(wasm.object_global_imports.values()));
3492 addBuf(&bufs, @ptrCast(wasm.object_globals.items));
3493 addBuf(&bufs, @ptrCast(wasm.object_table_imports.keys()));
3494 addBuf(&bufs, @ptrCast(wasm.object_table_imports.values()));
3495 addBuf(&bufs, @ptrCast(wasm.object_tables.items));
3496 addBuf(&bufs, @ptrCast(wasm.object_memory_imports.keys()));
3497 addBuf(&bufs, @ptrCast(wasm.object_memory_imports.values()));
3498 addBuf(&bufs, @ptrCast(wasm.object_memories.items));
3499 addBuf(&bufs, @ptrCast(wasm.object_relocations.items(.tag)));
3500 addBuf(&bufs, @ptrCast(wasm.object_relocations.items(.offset)));
35013501 // TODO handle the union safety field
3502 //addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations.items(.pointee)));
3503 addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations.items(.addend)));
3504 addBuf(&bufs, mem.sliceAsBytes(wasm.object_init_funcs.items));
3505 addBuf(&bufs, mem.sliceAsBytes(wasm.object_data_segments.items));
3506 addBuf(&bufs, mem.sliceAsBytes(wasm.object_datas.items));
3507 addBuf(&bufs, mem.sliceAsBytes(wasm.object_data_imports.keys()));
3508 addBuf(&bufs, mem.sliceAsBytes(wasm.object_data_imports.values()));
3509 addBuf(&bufs, mem.sliceAsBytes(wasm.object_custom_segments.keys()));
3510 addBuf(&bufs, mem.sliceAsBytes(wasm.object_custom_segments.values()));
3502 //addBuf(&bufs, @ptrCast(wasm.object_relocations.items(.pointee)));
3503 addBuf(&bufs, @ptrCast(wasm.object_relocations.items(.addend)));
3504 addBuf(&bufs, @ptrCast(wasm.object_init_funcs.items));
3505 addBuf(&bufs, @ptrCast(wasm.object_data_segments.items));
3506 addBuf(&bufs, @ptrCast(wasm.object_datas.items));
3507 addBuf(&bufs, @ptrCast(wasm.object_data_imports.keys()));
3508 addBuf(&bufs, @ptrCast(wasm.object_data_imports.values()));
3509 addBuf(&bufs, @ptrCast(wasm.object_custom_segments.keys()));
3510 addBuf(&bufs, @ptrCast(wasm.object_custom_segments.values()));
35113511 // TODO make it well-defined memory layout
3512 // addBuf(&bufs, mem.sliceAsBytes(wasm.object_comdats.items));
3513 addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations_table.keys()));
3514 addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations_table.values()));
3515 addBuf(&bufs, mem.sliceAsBytes(wasm.object_comdat_symbols.items(.kind)));
3516 addBuf(&bufs, mem.sliceAsBytes(wasm.object_comdat_symbols.items(.index)));
3517 addBuf(&bufs, mem.sliceAsBytes(wasm.out_relocs.items(.tag)));
3518 addBuf(&bufs, mem.sliceAsBytes(wasm.out_relocs.items(.offset)));
3512 // addBuf(&bufs, @ptrCast(wasm.object_comdats.items));
3513 addBuf(&bufs, @ptrCast(wasm.object_relocations_table.keys()));
3514 addBuf(&bufs, @ptrCast(wasm.object_relocations_table.values()));
3515 addBuf(&bufs, @ptrCast(wasm.object_comdat_symbols.items(.kind)));
3516 addBuf(&bufs, @ptrCast(wasm.object_comdat_symbols.items(.index)));
3517 addBuf(&bufs, @ptrCast(wasm.out_relocs.items(.tag)));
3518 addBuf(&bufs, @ptrCast(wasm.out_relocs.items(.offset)));
35193519 // TODO handle the union safety field
3520 //addBuf(&bufs, mem.sliceAsBytes(wasm.out_relocs.items(.pointee)));
3521 addBuf(&bufs, mem.sliceAsBytes(wasm.out_relocs.items(.addend)));
3522 addBuf(&bufs, mem.sliceAsBytes(wasm.uav_fixups.items));
3523 addBuf(&bufs, mem.sliceAsBytes(wasm.nav_fixups.items));
3524 addBuf(&bufs, mem.sliceAsBytes(wasm.func_table_fixups.items));
3520 //addBuf(&bufs, @ptrCast(wasm.out_relocs.items(.pointee)));
3521 addBuf(&bufs, @ptrCast(wasm.out_relocs.items(.addend)));
3522 addBuf(&bufs, @ptrCast(wasm.uav_fixups.items));
3523 addBuf(&bufs, @ptrCast(wasm.nav_fixups.items));
3524 addBuf(&bufs, @ptrCast(wasm.func_table_fixups.items));
35253525 if (is_obj) {
3526 addBuf(&bufs, mem.sliceAsBytes(wasm.navs_obj.keys()));
3527 addBuf(&bufs, mem.sliceAsBytes(wasm.navs_obj.values()));
3528 addBuf(&bufs, mem.sliceAsBytes(wasm.uavs_obj.keys()));
3529 addBuf(&bufs, mem.sliceAsBytes(wasm.uavs_obj.values()));
3526 addBuf(&bufs, @ptrCast(wasm.navs_obj.keys()));
3527 addBuf(&bufs, @ptrCast(wasm.navs_obj.values()));
3528 addBuf(&bufs, @ptrCast(wasm.uavs_obj.keys()));
3529 addBuf(&bufs, @ptrCast(wasm.uavs_obj.values()));
35303530 } else {
3531 addBuf(&bufs, mem.sliceAsBytes(wasm.navs_exe.keys()));
3532 addBuf(&bufs, mem.sliceAsBytes(wasm.navs_exe.values()));
3533 addBuf(&bufs, mem.sliceAsBytes(wasm.uavs_exe.keys()));
3534 addBuf(&bufs, mem.sliceAsBytes(wasm.uavs_exe.values()));
3531 addBuf(&bufs, @ptrCast(wasm.navs_exe.keys()));
3532 addBuf(&bufs, @ptrCast(wasm.navs_exe.values()));
3533 addBuf(&bufs, @ptrCast(wasm.uavs_exe.keys()));
3534 addBuf(&bufs, @ptrCast(wasm.uavs_exe.values()));
35353535 }
3536 addBuf(&bufs, mem.sliceAsBytes(wasm.overaligned_uavs.keys()));
3537 addBuf(&bufs, mem.sliceAsBytes(wasm.overaligned_uavs.values()));
3538 addBuf(&bufs, mem.sliceAsBytes(wasm.zcu_funcs.keys()));
3536 addBuf(&bufs, @ptrCast(wasm.overaligned_uavs.keys()));
3537 addBuf(&bufs, @ptrCast(wasm.overaligned_uavs.values()));
3538 addBuf(&bufs, @ptrCast(wasm.zcu_funcs.keys()));
35393539 // TODO handle the union safety field
3540 // addBuf(&bufs, mem.sliceAsBytes(wasm.zcu_funcs.values()));
3541 addBuf(&bufs, mem.sliceAsBytes(wasm.nav_exports.keys()));
3542 addBuf(&bufs, mem.sliceAsBytes(wasm.nav_exports.values()));
3543 addBuf(&bufs, mem.sliceAsBytes(wasm.uav_exports.keys()));
3544 addBuf(&bufs, mem.sliceAsBytes(wasm.uav_exports.values()));
3545 addBuf(&bufs, mem.sliceAsBytes(wasm.imports.keys()));
3546 addBuf(&bufs, mem.sliceAsBytes(wasm.missing_exports.keys()));
3547 addBuf(&bufs, mem.sliceAsBytes(wasm.function_exports.keys()));
3548 addBuf(&bufs, mem.sliceAsBytes(wasm.function_exports.values()));
3549 addBuf(&bufs, mem.sliceAsBytes(wasm.hidden_function_exports.keys()));
3550 addBuf(&bufs, mem.sliceAsBytes(wasm.hidden_function_exports.values()));
3551 addBuf(&bufs, mem.sliceAsBytes(wasm.global_exports.items));
3552 addBuf(&bufs, mem.sliceAsBytes(wasm.functions.keys()));
3553 addBuf(&bufs, mem.sliceAsBytes(wasm.function_imports.keys()));
3554 addBuf(&bufs, mem.sliceAsBytes(wasm.function_imports.values()));
3555 addBuf(&bufs, mem.sliceAsBytes(wasm.data_imports.keys()));
3556 addBuf(&bufs, mem.sliceAsBytes(wasm.data_imports.values()));
3557 addBuf(&bufs, mem.sliceAsBytes(wasm.data_segments.keys()));
3558 addBuf(&bufs, mem.sliceAsBytes(wasm.globals.keys()));
3559 addBuf(&bufs, mem.sliceAsBytes(wasm.global_imports.keys()));
3560 addBuf(&bufs, mem.sliceAsBytes(wasm.global_imports.values()));
3561 addBuf(&bufs, mem.sliceAsBytes(wasm.tables.keys()));
3562 addBuf(&bufs, mem.sliceAsBytes(wasm.table_imports.keys()));
3563 addBuf(&bufs, mem.sliceAsBytes(wasm.table_imports.values()));
3564 addBuf(&bufs, mem.sliceAsBytes(wasm.zcu_indirect_function_set.keys()));
3565 addBuf(&bufs, mem.sliceAsBytes(wasm.object_indirect_function_import_set.keys()));
3566 addBuf(&bufs, mem.sliceAsBytes(wasm.object_indirect_function_set.keys()));
3567 addBuf(&bufs, mem.sliceAsBytes(wasm.mir_instructions.items(.tag)));
3540 // addBuf(&bufs, @ptrCast(wasm.zcu_funcs.values()));
3541 addBuf(&bufs, @ptrCast(wasm.nav_exports.keys()));
3542 addBuf(&bufs, @ptrCast(wasm.nav_exports.values()));
3543 addBuf(&bufs, @ptrCast(wasm.uav_exports.keys()));
3544 addBuf(&bufs, @ptrCast(wasm.uav_exports.values()));
3545 addBuf(&bufs, @ptrCast(wasm.imports.keys()));
3546 addBuf(&bufs, @ptrCast(wasm.missing_exports.keys()));
3547 addBuf(&bufs, @ptrCast(wasm.function_exports.keys()));
3548 addBuf(&bufs, @ptrCast(wasm.function_exports.values()));
3549 addBuf(&bufs, @ptrCast(wasm.hidden_function_exports.keys()));
3550 addBuf(&bufs, @ptrCast(wasm.hidden_function_exports.values()));
3551 addBuf(&bufs, @ptrCast(wasm.global_exports.items));
3552 addBuf(&bufs, @ptrCast(wasm.functions.keys()));
3553 addBuf(&bufs, @ptrCast(wasm.function_imports.keys()));
3554 addBuf(&bufs, @ptrCast(wasm.function_imports.values()));
3555 addBuf(&bufs, @ptrCast(wasm.data_imports.keys()));
3556 addBuf(&bufs, @ptrCast(wasm.data_imports.values()));
3557 addBuf(&bufs, @ptrCast(wasm.data_segments.keys()));
3558 addBuf(&bufs, @ptrCast(wasm.globals.keys()));
3559 addBuf(&bufs, @ptrCast(wasm.global_imports.keys()));
3560 addBuf(&bufs, @ptrCast(wasm.global_imports.values()));
3561 addBuf(&bufs, @ptrCast(wasm.tables.keys()));
3562 addBuf(&bufs, @ptrCast(wasm.table_imports.keys()));
3563 addBuf(&bufs, @ptrCast(wasm.table_imports.values()));
3564 addBuf(&bufs, @ptrCast(wasm.zcu_indirect_function_set.keys()));
3565 addBuf(&bufs, @ptrCast(wasm.object_indirect_function_import_set.keys()));
3566 addBuf(&bufs, @ptrCast(wasm.object_indirect_function_set.keys()));
3567 addBuf(&bufs, @ptrCast(wasm.mir_instructions.items(.tag)));
35683568 // TODO handle the union safety field
3569 //addBuf(&bufs, mem.sliceAsBytes(wasm.mir_instructions.items(.data)));
3570 addBuf(&bufs, mem.sliceAsBytes(wasm.mir_extra.items));
3571 addBuf(&bufs, mem.sliceAsBytes(wasm.mir_locals.items));
3572 addBuf(&bufs, mem.sliceAsBytes(wasm.tag_name_bytes.items));
3573 addBuf(&bufs, mem.sliceAsBytes(wasm.tag_name_offs.items));
3569 //addBuf(&bufs, @ptrCast(wasm.mir_instructions.items(.data)));
3570 addBuf(&bufs, @ptrCast(wasm.mir_extra.items));
3571 addBuf(&bufs, @ptrCast(wasm.mir_locals.items));
3572 addBuf(&bufs, @ptrCast(wasm.tag_name_bytes.items));
3573 addBuf(&bufs, @ptrCast(wasm.tag_name_offs.items));
35743574
35753575 // TODO add as header fields
35763576 // entry_resolution: FunctionImport.Resolution
......@@ -3596,16 +3596,16 @@ pub fn saveState(comp: *Compilation) !void {
35963596
35973597 // Using an atomic file prevents a crash or power failure from corrupting
35983598 // the previous incremental compilation state.
3599 var af = try lf.emit.root_dir.handle.atomicFile(basename, .{});
3599 var write_buffer: [1024]u8 = undefined;
3600 var af = try lf.emit.root_dir.handle.atomicFile(basename, .{ .write_buffer = &write_buffer });
36003601 defer af.deinit();
3601 try af.file.pwritevAll(bufs.items, 0);
3602 try af.file_writer.interface.writeVecAll(bufs.items);
36023603 try af.finish();
36033604}
36043605
3605fn addBuf(list: *std.ArrayList(std.posix.iovec_const), buf: []const u8) void {
3606 // Even when len=0, the undefined pointer might cause EFAULT.
3606fn addBuf(list: *std.ArrayList([]const u8), buf: []const u8) void {
36073607 if (buf.len == 0) return;
3608 list.appendAssumeCapacity(.{ .base = buf.ptr, .len = buf.len });
3608 list.appendAssumeCapacity(buf);
36093609}
36103610
36113611/// This function is temporally single-threaded.
src/Sema.zig+33-6
......@@ -5000,9 +5000,11 @@ fn validateUnionInit(
50005000 }
50015001 if (init_ref) |v| try sema.validateRuntimeValue(block, block.nodeOffset(field_ptr_data.src_node), v);
50025002
5003 const new_tag = Air.internedToRef(tag_val.toIntern());
5004 const set_tag_inst = try block.addBinOp(.set_union_tag, union_ptr, new_tag);
5005 try sema.checkComptimeKnownStore(block, set_tag_inst, LazySrcLoc.unneeded); // `unneeded` since this isn't a "proper" store
5003 if ((try sema.typeHasOnePossibleValue(tag_ty)) == null) {
5004 const new_tag = Air.internedToRef(tag_val.toIntern());
5005 const set_tag_inst = try block.addBinOp(.set_union_tag, union_ptr, new_tag);
5006 try sema.checkComptimeKnownStore(block, set_tag_inst, LazySrcLoc.unneeded); // `unneeded` since this isn't a "proper" store
5007 }
50065008}
50075009
50085010fn validateStructInit(
......@@ -6560,6 +6562,11 @@ fn resolveAnalyzedBlock(
65606562 } },
65616563 });
65626564 }
6565
6566 if (try sema.typeHasOnePossibleValue(resolved_ty)) |block_only_value| {
6567 return Air.internedToRef(block_only_value.toIntern());
6568 }
6569
65636570 return merges.block_inst.toRef();
65646571}
65656572
......@@ -9056,6 +9063,10 @@ fn analyzeErrUnionPayload(
90569063 try sema.addSafetyCheckUnwrapError(block, src, operand, .unwrap_errunion_err, .is_non_err);
90579064 }
90589065
9066 if (try sema.typeHasOnePossibleValue(payload_ty)) |payload_only_value| {
9067 return Air.internedToRef(payload_only_value.toIntern());
9068 }
9069
90599070 return block.addTyOp(.unwrap_errunion_payload, payload_ty, operand);
90609071}
90619072
......@@ -19690,8 +19701,10 @@ fn zirStructInit(
1969019701 const base_ptr = try sema.optEuBasePtrInit(block, alloc, src);
1969119702 const field_ptr = try sema.unionFieldPtr(block, field_src, base_ptr, field_name, field_src, resolved_ty, true);
1969219703 try sema.storePtr(block, src, field_ptr, init_inst);
19693 const new_tag = Air.internedToRef(tag_val.toIntern());
19694 _ = try block.addBinOp(.set_union_tag, base_ptr, new_tag);
19704 if ((try sema.typeHasOnePossibleValue(tag_ty)) == null) {
19705 const new_tag = Air.internedToRef(tag_val.toIntern());
19706 _ = try block.addBinOp(.set_union_tag, base_ptr, new_tag);
19707 }
1969519708 return sema.makePtrConst(block, alloc);
1969619709 }
1969719710
......@@ -28079,10 +28092,16 @@ fn unionFieldVal(
2807928092 const active_tag = try block.addTyOp(.get_union_tag, .fromInterned(union_obj.enum_tag_ty), union_byval);
2808028093 try sema.addSafetyCheckInactiveUnionField(block, src, active_tag, wanted_tag);
2808128094 }
28095
2808228096 if (field_ty.zigTypeTag(zcu) == .noreturn) {
2808328097 _ = try block.addNoOp(.unreach);
2808428098 return .unreachable_value;
2808528099 }
28100
28101 if (try sema.typeHasOnePossibleValue(field_ty)) |field_only_value| {
28102 return Air.internedToRef(field_only_value.toIntern());
28103 }
28104
2808628105 try field_ty.resolveLayout(pt);
2808728106 return block.addStructFieldVal(union_byval, field_index, field_ty);
2808828107}
......@@ -28214,12 +28233,12 @@ fn elemVal(
2821428233 .many, .c => {
2821528234 const maybe_indexable_val = try sema.resolveDefinedValue(block, indexable_src, indexable);
2821628235 const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index);
28236 const elem_ty = indexable_ty.elemType2(zcu);
2821728237
2821828238 ct: {
2821928239 const indexable_val = maybe_indexable_val orelse break :ct;
2822028240 const index_val = maybe_index_val orelse break :ct;
2822128241 const index: usize = @intCast(try index_val.toUnsignedIntSema(pt));
28222 const elem_ty = indexable_ty.elemType2(zcu);
2822328242 const many_ptr_ty = try pt.manyConstPtrType(elem_ty);
2822428243 const many_ptr_val = try pt.getCoerced(indexable_val, many_ptr_ty);
2822528244 const elem_ptr_ty = try pt.singleConstPtrType(elem_ty);
......@@ -28228,6 +28247,10 @@ fn elemVal(
2822828247 return Air.internedToRef((try pt.getCoerced(elem_val, elem_ty)).toIntern());
2822928248 }
2823028249
28250 if (try sema.typeHasOnePossibleValue(elem_ty)) |elem_only_value| {
28251 return Air.internedToRef(elem_only_value.toIntern());
28252 }
28253
2823128254 try sema.checkLogicalPtrOperation(block, src, indexable_ty);
2823228255 return block.addBinOp(.ptr_elem_val, indexable, elem_index);
2823328256 },
......@@ -28578,6 +28601,10 @@ fn elemValSlice(
2857828601 }
2857928602 }
2858028603
28604 if (try sema.typeHasOnePossibleValue(elem_ty)) |elem_only_value| {
28605 return Air.internedToRef(elem_only_value.toIntern());
28606 }
28607
2858128608 try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ty, slice_ty, slice_src);
2858228609 try sema.validateRuntimeValue(block, slice_src, slice);
2858328610
src/fmt.zig+2-2
......@@ -349,10 +349,10 @@ fn fmtPathFile(
349349 try fmt.stdout_writer.interface.print("{s}\n", .{file_path});
350350 fmt.any_error = true;
351351 } else {
352 var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode });
352 var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode, .write_buffer = &.{} });
353353 defer af.deinit();
354354
355 try af.file.writeAll(fmt.out_buffer.getWritten());
355 try af.file_writer.interface.writeAll(fmt.out_buffer.getWritten());
356356 try af.finish();
357357 try fmt.stdout_writer.interface.print("{s}\n", .{file_path});
358358 }
src/link/MachO.zig-1
......@@ -613,7 +613,6 @@ pub fn flush(
613613 };
614614 const emit = self.base.emit;
615615 invalidateKernelCache(emit.root_dir.handle, emit.sub_path) catch |err| switch (err) {
616 error.OutOfMemory => return error.OutOfMemory,
617616 else => |e| return diags.fail("failed to invalidate kernel cache: {s}", .{@errorName(e)}),
618617 };
619618 }
src/main.zig+3-1
......@@ -4624,7 +4624,9 @@ fn cmdTranslateC(
46244624 fatal("unable to open cached translated zig file '{s}{s}{s}': {s}", .{ path, fs.path.sep_str, out_zig_path, @errorName(err) });
46254625 };
46264626 defer zig_file.close();
4627 try fs.File.stdout().writeFileAll(zig_file, .{});
4627 var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
4628 var file_reader = zig_file.reader(&.{});
4629 _ = try stdout_writer.interface.sendFileAll(&file_reader, .unlimited);
46284630 return cleanExit();
46294631 }
46304632}
test/incremental/fix_many_errors deleted-71
......@@ -1,71 +0,0 @@
1#target=x86_64-linux-selfhosted
2#target=x86_64-linux-cbe
3#target=x86_64-windows-cbe
4#update=initial version
5#file=main.zig
6pub fn main() !void {}
7comptime { @compileError("c0"); }
8comptime { @compileError("c1"); }
9comptime { @compileError("c2"); }
10comptime { @compileError("c3"); }
11comptime { @compileError("c4"); }
12comptime { @compileError("c5"); }
13comptime { @compileError("c6"); }
14comptime { @compileError("c7"); }
15comptime { @compileError("c8"); }
16comptime { @compileError("c9"); }
17export fn f0() void { @compileError("f0"); }
18export fn f1() void { @compileError("f1"); }
19export fn f2() void { @compileError("f2"); }
20export fn f3() void { @compileError("f3"); }
21export fn f4() void { @compileError("f4"); }
22export fn f5() void { @compileError("f5"); }
23export fn f6() void { @compileError("f6"); }
24export fn f7() void { @compileError("f7"); }
25export fn f8() void { @compileError("f8"); }
26export fn f9() void { @compileError("f9"); }
27#expect_error=main.zig:2:12: error: c0
28#expect_error=main.zig:3:12: error: c1
29#expect_error=main.zig:4:12: error: c2
30#expect_error=main.zig:5:12: error: c3
31#expect_error=main.zig:6:12: error: c4
32#expect_error=main.zig:7:12: error: c5
33#expect_error=main.zig:8:12: error: c6
34#expect_error=main.zig:9:12: error: c7
35#expect_error=main.zig:10:12: error: c8
36#expect_error=main.zig:11:12: error: c9
37#expect_error=main.zig:12:23: error: f0
38#expect_error=main.zig:13:23: error: f1
39#expect_error=main.zig:14:23: error: f2
40#expect_error=main.zig:15:23: error: f3
41#expect_error=main.zig:16:23: error: f4
42#expect_error=main.zig:17:23: error: f5
43#expect_error=main.zig:18:23: error: f6
44#expect_error=main.zig:19:23: error: f7
45#expect_error=main.zig:20:23: error: f8
46#expect_error=main.zig:21:23: error: f9
47#update=fix all the errors
48#file=main.zig
49pub fn main() !void {}
50comptime {}
51comptime {}
52comptime {}
53comptime {}
54comptime {}
55comptime {}
56comptime {}
57comptime {}
58comptime {}
59comptime {}
60export fn f0() void {}
61export fn f1() void {}
62export fn f2() void {}
63export fn f3() void {}
64export fn f4() void {}
65export fn f5() void {}
66export fn f6() void {}
67export fn f7() void {}
68export fn f8() void {}
69export fn f9() void {}
70const std = @import("std");
71#expect_stdout=""
test/standalone/stack_iterator/build.zig+64-63
......@@ -65,69 +65,70 @@ pub fn build(b: *std.Build) void {
6565 test_step.dependOn(&run_cmd.step);
6666 }
6767
68 // Unwinding through a C shared library without a frame pointer (libc)
69 //
70 // getcontext version: libc
71 //
72 // Unwind info type:
73 // - ELF: DWARF .eh_frame + .debug_frame
74 // - MachO: __unwind_info encodings:
75 // - x86_64: STACK_IMMD, STACK_IND
76 // - aarch64: FRAMELESS, DWARF
77 {
78 const c_shared_lib = b.addLibrary(.{
79 .linkage = .dynamic,
80 .name = "c_shared_lib",
81 .root_module = b.createModule(.{
82 .root_source_file = null,
83 .target = target,
84 .optimize = optimize,
85 .link_libc = true,
86 .strip = false,
87 }),
88 });
89
90 if (target.result.os.tag == .windows)
91 c_shared_lib.root_module.addCMacro("LIB_API", "__declspec(dllexport)");
92
93 c_shared_lib.root_module.addCSourceFile(.{
94 .file = b.path("shared_lib.c"),
95 .flags = &.{"-fomit-frame-pointer"},
96 });
97
98 const exe = b.addExecutable(.{
99 .name = "shared_lib_unwind",
100 .root_module = b.createModule(.{
101 .root_source_file = b.path("shared_lib_unwind.zig"),
102 .target = target,
103 .optimize = optimize,
104 .unwind_tables = if (target.result.os.tag.isDarwin()) .async else null,
105 .omit_frame_pointer = true,
106 }),
107 // zig objcopy doesn't support incremental binaries
108 .use_llvm = true,
109 });
110
111 exe.linkLibrary(c_shared_lib);
112
113 const run_cmd = b.addRunArtifact(exe);
114 test_step.dependOn(&run_cmd.step);
115
116 // Separate debug info ELF file
117 if (target.result.ofmt == .elf) {
118 const filename = b.fmt("{s}_stripped", .{exe.out_filename});
119 const stripped_exe = b.addObjCopy(exe.getEmittedBin(), .{
120 .basename = filename, // set the name for the debuglink
121 .compress_debug = true,
122 .strip = .debug,
123 .extract_to_separate_file = true,
124 });
125
126 const run_stripped = std.Build.Step.Run.create(b, b.fmt("run {s}", .{filename}));
127 run_stripped.addFileArg(stripped_exe.getOutput());
128 test_step.dependOn(&run_stripped.step);
129 }
130 }
68 // https://github.com/ziglang/zig/issues/24522
69 //// Unwinding through a C shared library without a frame pointer (libc)
70 ////
71 //// getcontext version: libc
72 ////
73 //// Unwind info type:
74 //// - ELF: DWARF .eh_frame + .debug_frame
75 //// - MachO: __unwind_info encodings:
76 //// - x86_64: STACK_IMMD, STACK_IND
77 //// - aarch64: FRAMELESS, DWARF
78 //{
79 // const c_shared_lib = b.addLibrary(.{
80 // .linkage = .dynamic,
81 // .name = "c_shared_lib",
82 // .root_module = b.createModule(.{
83 // .root_source_file = null,
84 // .target = target,
85 // .optimize = optimize,
86 // .link_libc = true,
87 // .strip = false,
88 // }),
89 // });
90
91 // if (target.result.os.tag == .windows)
92 // c_shared_lib.root_module.addCMacro("LIB_API", "__declspec(dllexport)");
93
94 // c_shared_lib.root_module.addCSourceFile(.{
95 // .file = b.path("shared_lib.c"),
96 // .flags = &.{"-fomit-frame-pointer"},
97 // });
98
99 // const exe = b.addExecutable(.{
100 // .name = "shared_lib_unwind",
101 // .root_module = b.createModule(.{
102 // .root_source_file = b.path("shared_lib_unwind.zig"),
103 // .target = target,
104 // .optimize = optimize,
105 // .unwind_tables = if (target.result.os.tag.isDarwin()) .async else null,
106 // .omit_frame_pointer = true,
107 // }),
108 // // zig objcopy doesn't support incremental binaries
109 // .use_llvm = true,
110 // });
111
112 // exe.linkLibrary(c_shared_lib);
113
114 // const run_cmd = b.addRunArtifact(exe);
115 // test_step.dependOn(&run_cmd.step);
116
117 // // Separate debug info ELF file
118 // if (target.result.ofmt == .elf) {
119 // const filename = b.fmt("{s}_stripped", .{exe.out_filename});
120 // const stripped_exe = b.addObjCopy(exe.getEmittedBin(), .{
121 // .basename = filename, // set the name for the debuglink
122 // .compress_debug = true,
123 // .strip = .debug,
124 // .extract_to_separate_file = true,
125 // });
126
127 // const run_stripped = std.Build.Step.Run.create(b, b.fmt("run {s}", .{filename}));
128 // run_stripped.addFileArg(stripped_exe.getOutput());
129 // test_step.dependOn(&run_stripped.step);
130 // }
131 //}
131132
132133 // Unwinding without libc/posix
133134 //
tools/gen_stubs.zig+2-1
......@@ -310,7 +310,8 @@ pub fn main() !void {
310310 build_all_path, libc_so_path, @errorName(err),
311311 });
312312 };
313 const header = try elf.Header.parse(elf_bytes[0..@sizeOf(elf.Elf64_Ehdr)]);
313 var stream: std.Io.Reader = .fixed(elf_bytes);
314 const header = try elf.Header.read(&stream);
314315
315316 const parse: Parse = .{
316317 .arena = arena,