| author | |
| committer | |
| log | 5e6b8e17eff77107af7fb69cf86009bd45222778 |
| tree | b98cebeabcb01a44fe04cb3073788b93b55af210 |
| parent | 70994b13df94ac4a3392decef498724d0e0a0a28 |
| parent | f34b4780b7bd52d14df253d0762d9c73db8eb226 |
21 files changed, 632 insertions(+), 1654 deletions(-)
lib/compiler/build_runner.zig+1-1| ... | @@ -708,7 +708,7 @@ fn runStepNames( | ... | @@ -708,7 +708,7 @@ fn runStepNames( |
| 708 | 708 | ||
| 709 | const total_count = success_count + failure_count + pending_count + skipped_count; | 709 | const total_count = success_count + failure_count + pending_count + skipped_count; |
| 710 | ttyconf.setColor(w, .cyan) catch {}; | 710 | ttyconf.setColor(w, .cyan) catch {}; |
| 711 | w.writeAll("Build Summary:") catch {}; | 711 | w.writeAll("\nBuild Summary:") catch {}; |
| 712 | ttyconf.setColor(w, .reset) catch {}; | 712 | ttyconf.setColor(w, .reset) catch {}; |
| 713 | w.print(" {d}/{d} steps succeeded", .{ success_count, total_count }) catch {}; | 713 | w.print(" {d}/{d} steps succeeded", .{ success_count, total_count }) catch {}; |
| 714 | if (skipped_count > 0) w.print("; {d} skipped", .{skipped_count}) catch {}; | 714 | 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; | ... | @@ -13,6 +13,9 @@ const Server = std.zig.Server; |
| 13 | var stdin_buffer: [1024]u8 = undefined; | 13 | var stdin_buffer: [1024]u8 = undefined; |
| 14 | var stdout_buffer: [1024]u8 = undefined; | 14 | var stdout_buffer: [1024]u8 = undefined; |
| 15 | 15 | ||
| 16 | var input_buffer: [1024]u8 = undefined; | ||
| 17 | var output_buffer: [1024]u8 = undefined; | ||
| 18 | |||
| 16 | pub fn main() !void { | 19 | pub fn main() !void { |
| 17 | var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); | 20 | var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); |
| 18 | defer arena_instance.deinit(); | 21 | defer arena_instance.deinit(); |
| ... | @@ -145,13 +148,16 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void | ... | @@ -145,13 +148,16 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void |
| 145 | const input = opt_input orelse fatal("expected input parameter", .{}); | 148 | const input = opt_input orelse fatal("expected input parameter", .{}); |
| 146 | const output = opt_output orelse fatal("expected output parameter", .{}); | 149 | const output = opt_output orelse fatal("expected output parameter", .{}); |
| 147 | 150 | ||
| 148 | var in_file = fs.cwd().openFile(input, .{}) catch |err| | 151 | const input_file = fs.cwd().openFile(input, .{}) catch |err| fatal("failed to open {s}: {t}", .{ input, err }); |
| 149 | fatal("unable to open '{s}': {s}", .{ input, @errorName(err) }); | 152 | defer input_file.close(); |
| 150 | defer in_file.close(); | 153 | |
| 154 | const stat = input_file.stat() catch |err| fatal("failed to stat {s}: {t}", .{ input, err }); | ||
| 151 | 155 | ||
| 152 | const elf_hdr = std.elf.Header.read(in_file) catch |err| switch (err) { | 156 | var in: File.Reader = .initSize(input_file, &input_buffer, stat.size); |
| 153 | error.InvalidElfMagic => fatal("not an ELF file: '{s}'", .{input}), | 157 | |
| 154 | else => fatal("unable to read '{s}': {s}", .{ input, @errorName(err) }), | 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}), | ||
| 155 | }; | 161 | }; |
| 156 | 162 | ||
| 157 | const in_ofmt = .elf; | 163 | const in_ofmt = .elf; |
| ... | @@ -168,16 +174,12 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void | ... | @@ -168,16 +174,12 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void |
| 168 | } | 174 | } |
| 169 | }; | 175 | }; |
| 170 | 176 | ||
| 171 | const mode = mode: { | 177 | const mode = if (out_fmt != .elf or only_keep_debug) fs.File.default_mode else stat.mode; |
| 172 | if (out_fmt != .elf or only_keep_debug) | 178 | |
| 173 | break :mode fs.File.default_mode; | 179 | var output_file = try fs.cwd().createFile(output, .{ .mode = mode }); |
| 174 | if (in_file.stat()) |stat| | 180 | defer output_file.close(); |
| 175 | break :mode stat.mode | 181 | |
| 176 | else |_| | 182 | var out = output_file.writer(&output_buffer); |
| 177 | break :mode fs.File.default_mode; | ||
| 178 | }; | ||
| 179 | var out_file = try fs.cwd().createFile(output, .{ .mode = mode }); | ||
| 180 | defer out_file.close(); | ||
| 181 | 183 | ||
| 182 | switch (out_fmt) { | 184 | switch (out_fmt) { |
| 183 | .hex, .raw => { | 185 | .hex, .raw => { |
| ... | @@ -192,7 +194,7 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void | ... | @@ -192,7 +194,7 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void |
| 192 | if (set_section_flags != null) | 194 | if (set_section_flags != null) |
| 193 | fatal("zig objcopy: ELF to RAW or HEX copying does not support --set_section_flags", .{}); | 195 | fatal("zig objcopy: ELF to RAW or HEX copying does not support --set_section_flags", .{}); |
| 194 | 196 | ||
| 195 | try emitElf(arena, in_file, out_file, elf_hdr, .{ | 197 | try emitElf(arena, &in, &out, elf_hdr, .{ |
| 196 | .ofmt = out_fmt, | 198 | .ofmt = out_fmt, |
| 197 | .only_section = only_section, | 199 | .only_section = only_section, |
| 198 | .pad_to = pad_to, | 200 | .pad_to = pad_to, |
| ... | @@ -208,22 +210,13 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void | ... | @@ -208,22 +210,13 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void |
| 208 | if (pad_to) |_| | 210 | if (pad_to) |_| |
| 209 | fatal("zig objcopy: ELF to ELF copying does not support --pad-to", .{}); | 211 | fatal("zig objcopy: ELF to ELF copying does not support --pad-to", .{}); |
| 210 | 212 | ||
| 211 | try stripElf(arena, in_file, out_file, elf_hdr, .{ | 213 | fatal("unimplemented", .{}); |
| 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(); | ||
| 223 | }, | 214 | }, |
| 224 | else => fatal("unsupported output object format: {s}", .{@tagName(out_fmt)}), | 215 | else => fatal("unsupported output object format: {s}", .{@tagName(out_fmt)}), |
| 225 | } | 216 | } |
| 226 | 217 | ||
| 218 | try out.end(); | ||
| 219 | |||
| 227 | if (listen) { | 220 | if (listen) { |
| 228 | var stdin_reader = fs.File.stdin().reader(&stdin_buffer); | 221 | var stdin_reader = fs.File.stdin().reader(&stdin_buffer); |
| 229 | var stdout_writer = fs.File.stdout().writer(&stdout_buffer); | 222 | var stdout_writer = fs.File.stdout().writer(&stdout_buffer); |
| ... | @@ -304,12 +297,12 @@ const SetSectionFlags = struct { | ... | @@ -304,12 +297,12 @@ const SetSectionFlags = struct { |
| 304 | 297 | ||
| 305 | fn emitElf( | 298 | fn emitElf( |
| 306 | arena: Allocator, | 299 | arena: Allocator, |
| 307 | in_file: File, | 300 | in: *File.Reader, |
| 308 | out_file: File, | 301 | out: *File.Writer, |
| 309 | elf_hdr: elf.Header, | 302 | elf_hdr: elf.Header, |
| 310 | options: EmitRawElfOptions, | 303 | options: EmitRawElfOptions, |
| 311 | ) !void { | 304 | ) !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); |
| 313 | defer binary_elf_output.deinit(); | 306 | defer binary_elf_output.deinit(); |
| 314 | 307 | ||
| 315 | if (options.ofmt == .elf) { | 308 | if (options.ofmt == .elf) { |
| ... | @@ -328,8 +321,8 @@ fn emitElf( | ... | @@ -328,8 +321,8 @@ fn emitElf( |
| 328 | continue; | 321 | continue; |
| 329 | } | 322 | } |
| 330 | 323 | ||
| 331 | try writeBinaryElfSection(in_file, out_file, section); | 324 | try writeBinaryElfSection(in, out, section); |
| 332 | try padFile(out_file, options.pad_to); | 325 | try padFile(out, options.pad_to); |
| 333 | return; | 326 | return; |
| 334 | } | 327 | } |
| 335 | }, | 328 | }, |
| ... | @@ -342,10 +335,10 @@ fn emitElf( | ... | @@ -342,10 +335,10 @@ fn emitElf( |
| 342 | switch (options.ofmt) { | 335 | switch (options.ofmt) { |
| 343 | .raw => { | 336 | .raw => { |
| 344 | for (binary_elf_output.sections.items) |section| { | 337 | for (binary_elf_output.sections.items) |section| { |
| 345 | try out_file.seekTo(section.binaryOffset); | 338 | try out.seekTo(section.binaryOffset); |
| 346 | try writeBinaryElfSection(in_file, out_file, section); | 339 | try writeBinaryElfSection(in, out, section); |
| 347 | } | 340 | } |
| 348 | try padFile(out_file, options.pad_to); | 341 | try padFile(out, options.pad_to); |
| 349 | }, | 342 | }, |
| 350 | .hex => { | 343 | .hex => { |
| 351 | if (binary_elf_output.segments.items.len == 0) return; | 344 | if (binary_elf_output.segments.items.len == 0) return; |
| ... | @@ -353,15 +346,15 @@ fn emitElf( | ... | @@ -353,15 +346,15 @@ fn emitElf( |
| 353 | return error.InvalidHexfileAddressRange; | 346 | return error.InvalidHexfileAddressRange; |
| 354 | } | 347 | } |
| 355 | 348 | ||
| 356 | var hex_writer = HexWriter{ .out_file = out_file }; | 349 | var hex_writer = HexWriter{ .out = out }; |
| 357 | for (binary_elf_output.segments.items) |segment| { | 350 | for (binary_elf_output.segments.items) |segment| { |
| 358 | try hex_writer.writeSegment(segment, in_file); | 351 | try hex_writer.writeSegment(segment, in); |
| 359 | } | 352 | } |
| 360 | if (options.pad_to) |_| { | 353 | if (options.pad_to) |_| { |
| 361 | // Padding to a size in hex files isn't applicable | 354 | // Padding to a size in hex files isn't applicable |
| 362 | return error.InvalidArgument; | 355 | return error.InvalidArgument; |
| 363 | } | 356 | } |
| 364 | try hex_writer.writeEOF(); | 357 | try hex_writer.writeEof(); |
| 365 | }, | 358 | }, |
| 366 | else => unreachable, | 359 | else => unreachable, |
| 367 | } | 360 | } |
| ... | @@ -399,7 +392,7 @@ const BinaryElfOutput = struct { | ... | @@ -399,7 +392,7 @@ const BinaryElfOutput = struct { |
| 399 | self.segments.deinit(self.allocator); | 392 | self.segments.deinit(self.allocator); |
| 400 | } | 393 | } |
| 401 | 394 | ||
| 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 { |
| 403 | var self: Self = .{ | 396 | var self: Self = .{ |
| 404 | .segments = .{}, | 397 | .segments = .{}, |
| 405 | .sections = .{}, | 398 | .sections = .{}, |
| ... | @@ -412,7 +405,7 @@ const BinaryElfOutput = struct { | ... | @@ -412,7 +405,7 @@ const BinaryElfOutput = struct { |
| 412 | self.shstrtab = blk: { | 405 | self.shstrtab = blk: { |
| 413 | if (elf_hdr.shstrndx >= elf_hdr.shnum) break :blk null; | 406 | if (elf_hdr.shstrndx >= elf_hdr.shnum) break :blk null; |
| 414 | 407 | ||
| 415 | var section_headers = elf_hdr.section_header_iterator(&elf_file); | 408 | var section_headers = elf_hdr.iterateSectionHeaders(in); |
| 416 | 409 | ||
| 417 | var section_counter: usize = 0; | 410 | var section_counter: usize = 0; |
| 418 | while (section_counter < elf_hdr.shstrndx) : (section_counter += 1) { | 411 | while (section_counter < elf_hdr.shstrndx) : (section_counter += 1) { |
| ... | @@ -421,18 +414,13 @@ const BinaryElfOutput = struct { | ... | @@ -421,18 +414,13 @@ const BinaryElfOutput = struct { |
| 421 | 414 | ||
| 422 | const shstrtab_shdr = (try section_headers.next()).?; | 415 | const shstrtab_shdr = (try section_headers.next()).?; |
| 423 | 416 | ||
| 424 | const buffer = try allocator.alloc(u8, @intCast(shstrtab_shdr.sh_size)); | 417 | try in.seekTo(shstrtab_shdr.sh_offset); |
| 425 | errdefer allocator.free(buffer); | 418 | break :blk try in.interface.readAlloc(allocator, shstrtab_shdr.sh_size); |
| 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; | ||
| 431 | }; | 419 | }; |
| 432 | 420 | ||
| 433 | errdefer if (self.shstrtab) |shstrtab| allocator.free(shstrtab); | 421 | errdefer if (self.shstrtab) |shstrtab| allocator.free(shstrtab); |
| 434 | 422 | ||
| 435 | var section_headers = elf_hdr.section_header_iterator(&elf_file); | 423 | var section_headers = elf_hdr.iterateSectionHeaders(in); |
| 436 | while (try section_headers.next()) |section| { | 424 | while (try section_headers.next()) |section| { |
| 437 | if (sectionValidForOutput(section)) { | 425 | if (sectionValidForOutput(section)) { |
| 438 | const newSection = try allocator.create(BinaryElfSection); | 426 | const newSection = try allocator.create(BinaryElfSection); |
| ... | @@ -451,7 +439,7 @@ const BinaryElfOutput = struct { | ... | @@ -451,7 +439,7 @@ const BinaryElfOutput = struct { |
| 451 | } | 439 | } |
| 452 | } | 440 | } |
| 453 | 441 | ||
| 454 | var program_headers = elf_hdr.program_header_iterator(&elf_file); | 442 | var program_headers = elf_hdr.iterateProgramHeaders(in); |
| 455 | while (try program_headers.next()) |phdr| { | 443 | while (try program_headers.next()) |phdr| { |
| 456 | if (phdr.p_type == elf.PT_LOAD) { | 444 | if (phdr.p_type == elf.PT_LOAD) { |
| 457 | const newSegment = try allocator.create(BinaryElfSegment); | 445 | const newSegment = try allocator.create(BinaryElfSegment); |
| ... | @@ -539,19 +527,17 @@ const BinaryElfOutput = struct { | ... | @@ -539,19 +527,17 @@ const BinaryElfOutput = struct { |
| 539 | } | 527 | } |
| 540 | }; | 528 | }; |
| 541 | 529 | ||
| 542 | fn writeBinaryElfSection(elf_file: File, out_file: File, section: *BinaryElfSection) !void { | 530 | fn writeBinaryElfSection(in: *File.Reader, out: *File.Writer, section: *BinaryElfSection) !void { |
| 543 | try out_file.writeFileAll(elf_file, .{ | 531 | try in.seekTo(section.elfOffset); |
| 544 | .in_offset = section.elfOffset, | 532 | _ = try out.interface.sendFileAll(in, .limited(section.fileSize)); |
| 545 | .in_len = section.fileSize, | ||
| 546 | }); | ||
| 547 | } | 533 | } |
| 548 | 534 | ||
| 549 | const HexWriter = struct { | 535 | const HexWriter = struct { |
| 550 | prev_addr: ?u32 = null, | 536 | prev_addr: ?u32 = null, |
| 551 | out_file: File, | 537 | out: *File.Writer, |
| 552 | 538 | ||
| 553 | /// Max data bytes per line of output | 539 | /// Max data bytes per line of output |
| 554 | const MAX_PAYLOAD_LEN: u8 = 16; | 540 | const max_payload_len: u8 = 16; |
| 555 | 541 | ||
| 556 | fn addressParts(address: u16) [2]u8 { | 542 | fn addressParts(address: u16) [2]u8 { |
| 557 | const msb: u8 = @truncate(address >> 8); | 543 | const msb: u8 = @truncate(address >> 8); |
| ... | @@ -627,13 +613,13 @@ const HexWriter = struct { | ... | @@ -627,13 +613,13 @@ const HexWriter = struct { |
| 627 | return (sum ^ 0xFF) +% 1; | 613 | return (sum ^ 0xFF) +% 1; |
| 628 | } | 614 | } |
| 629 | 615 | ||
| 630 | fn write(self: Record, file: File) File.WriteError!void { | 616 | fn write(self: Record, out: *File.Writer) !void { |
| 631 | const linesep = "\r\n"; | 617 | const linesep = "\r\n"; |
| 632 | // colon, (length, address, type, payload, checksum) as hex, CRLF | 618 | // 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; |
| 634 | var outbuf: [BUFSIZE]u8 = undefined; | 620 | var outbuf: [BUFSIZE]u8 = undefined; |
| 635 | const payload_bytes = self.getPayloadBytes(); | 621 | const payload_bytes = self.getPayloadBytes(); |
| 636 | assert(payload_bytes.len <= MAX_PAYLOAD_LEN); | 622 | assert(payload_bytes.len <= max_payload_len); |
| 637 | 623 | ||
| 638 | const line = try std.fmt.bufPrint(&outbuf, ":{0X:0>2}{1X:0>4}{2X:0>2}{3X}{4X:0>2}" ++ linesep, .{ | 624 | const line = try std.fmt.bufPrint(&outbuf, ":{0X:0>2}{1X:0>4}{2X:0>2}{3X}{4X:0>2}" ++ linesep, .{ |
| 639 | @as(u8, @intCast(payload_bytes.len)), | 625 | @as(u8, @intCast(payload_bytes.len)), |
| ... | @@ -642,38 +628,37 @@ const HexWriter = struct { | ... | @@ -642,38 +628,37 @@ const HexWriter = struct { |
| 642 | payload_bytes, | 628 | payload_bytes, |
| 643 | self.checksum(), | 629 | self.checksum(), |
| 644 | }); | 630 | }); |
| 645 | try file.writeAll(line); | 631 | try out.interface.writeAll(line); |
| 646 | } | 632 | } |
| 647 | }; | 633 | }; |
| 648 | 634 | ||
| 649 | pub fn writeSegment(self: *HexWriter, segment: *const BinaryElfSegment, elf_file: File) !void { | 635 | pub fn writeSegment(self: *HexWriter, segment: *const BinaryElfSegment, in: *File.Reader) !void { |
| 650 | var buf: [MAX_PAYLOAD_LEN]u8 = undefined; | 636 | var buf: [max_payload_len]u8 = undefined; |
| 651 | var bytes_read: usize = 0; | 637 | var bytes_read: usize = 0; |
| 652 | while (bytes_read < segment.fileSize) { | 638 | while (bytes_read < segment.fileSize) { |
| 653 | const row_address: u32 = @intCast(segment.physicalAddress + bytes_read); | 639 | const row_address: u32 = @intCast(segment.physicalAddress + bytes_read); |
| 654 | 640 | ||
| 655 | const remaining = segment.fileSize - bytes_read; | 641 | const remaining = segment.fileSize - bytes_read; |
| 656 | const to_read: usize = @intCast(@min(remaining, MAX_PAYLOAD_LEN)); | 642 | const dest = buf[0..@min(remaining, max_payload_len)]; |
| 657 | const did_read = try elf_file.preadAll(buf[0..to_read], segment.elfOffset + bytes_read); | 643 | try in.seekTo(segment.elfOffset + bytes_read); |
| 658 | if (did_read < to_read) return error.UnexpectedEOF; | 644 | try in.interface.readSliceAll(dest); |
| 645 | try self.writeDataRow(row_address, dest); | ||
| 659 | 646 | ||
| 660 | try self.writeDataRow(row_address, buf[0..did_read]); | 647 | bytes_read += dest.len; |
| 661 | |||
| 662 | bytes_read += did_read; | ||
| 663 | } | 648 | } |
| 664 | } | 649 | } |
| 665 | 650 | ||
| 666 | fn writeDataRow(self: *HexWriter, address: u32, data: []const u8) File.WriteError!void { | 651 | fn writeDataRow(self: *HexWriter, address: u32, data: []const u8) !void { |
| 667 | const record = Record.Data(address, data); | 652 | const record = Record.Data(address, data); |
| 668 | if (address > 0xFFFF and (self.prev_addr == null or record.address != self.prev_addr.?)) { | 653 | 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); |
| 670 | } | 655 | } |
| 671 | try record.write(self.out_file); | 656 | try record.write(self.out); |
| 672 | self.prev_addr = @intCast(record.address + data.len); | 657 | self.prev_addr = @intCast(record.address + data.len); |
| 673 | } | 658 | } |
| 674 | 659 | ||
| 675 | fn writeEOF(self: HexWriter) File.WriteError!void { | 660 | fn writeEof(self: HexWriter) !void { |
| 676 | try Record.EOF().write(self.out_file); | 661 | try Record.EOF().write(self.out); |
| 677 | } | 662 | } |
| 678 | }; | 663 | }; |
| 679 | 664 | ||
| ... | @@ -686,9 +671,9 @@ fn containsValidAddressRange(segments: []*BinaryElfSegment) bool { | ... | @@ -686,9 +671,9 @@ fn containsValidAddressRange(segments: []*BinaryElfSegment) bool { |
| 686 | return true; | 671 | return true; |
| 687 | } | 672 | } |
| 688 | 673 | ||
| 689 | fn padFile(f: File, opt_size: ?u64) !void { | 674 | fn padFile(out: *File.Writer, opt_size: ?u64) !void { |
| 690 | const size = opt_size orelse return; | 675 | const size = opt_size orelse return; |
| 691 | try f.setEndPos(size); | 676 | try out.file.setEndPos(size); |
| 692 | } | 677 | } |
| 693 | 678 | ||
| 694 | test "HexWriter.Record.Address has correct payload and checksum" { | 679 | test "HexWriter.Record.Address has correct payload and checksum" { |
| ... | @@ -732,836 +717,6 @@ test "containsValidAddressRange" { | ... | @@ -732,836 +717,6 @@ test "containsValidAddressRange" { |
| 732 | try std.testing.expect(containsValidAddressRange(&buf)); | 717 | try std.testing.expect(containsValidAddressRange(&buf)); |
| 733 | } | 718 | } |
| 734 | 719 | ||
| 735 | // ------------- | ||
| 736 | // ELF to ELF stripping | ||
| 737 | |||
| 738 | const 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 | |||
| 750 | fn 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 | |||
| 829 | fn 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 | |||
| 1363 | const 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 | |||
| 1565 | const SectionFlags = packed struct { | 720 | const SectionFlags = packed struct { |
| 1566 | alloc: bool = false, | 721 | alloc: bool = false, |
| 1567 | contents: bool = false, | 722 | contents: bool = false, |
lib/std/Build/Step/Run.zig+13-4| ... | @@ -1764,13 +1764,22 @@ fn evalGeneric(run: *Run, child: *std.process.Child) !StdIoResult { | ... | @@ -1764,13 +1764,22 @@ fn evalGeneric(run: *Run, child: *std.process.Child) !StdIoResult { |
| 1764 | child.stdin = null; | 1764 | child.stdin = null; |
| 1765 | }, | 1765 | }, |
| 1766 | .lazy_path => |lazy_path| { | 1766 | .lazy_path => |lazy_path| { |
| 1767 | const path = lazy_path.getPath2(b, &run.step); | 1767 | const path = lazy_path.getPath3(b, &run.step); |
| 1768 | const file = b.build_root.handle.openFile(path, .{}) catch |err| { | 1768 | const file = path.root_dir.handle.openFile(path.subPathOrDot(), .{}) catch |err| { |
| 1769 | return run.step.fail("unable to open stdin file: {s}", .{@errorName(err)}); | 1769 | return run.step.fail("unable to open stdin file: {s}", .{@errorName(err)}); |
| 1770 | }; | 1770 | }; |
| 1771 | defer file.close(); | 1771 | defer file.close(); |
| 1772 | child.stdin.?.writeFileAll(file, .{}) catch |err| { | 1772 | // TODO https://github.com/ziglang/zig/issues/23955 |
| 1773 | return run.step.fail("unable to write file to stdin: {s}", .{@errorName(err)}); | 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 | }), | ||
| 1774 | }; | 1783 | }; |
| 1775 | child.stdin.?.close(); | 1784 | child.stdin.?.close(); |
| 1776 | child.stdin = null; | 1785 | child.stdin = null; |
lib/std/Io/Writer.zig+2-1| ... | @@ -440,7 +440,8 @@ pub fn advance(w: *Writer, n: usize) void { | ... | @@ -440,7 +440,8 @@ pub fn advance(w: *Writer, n: usize) void { |
| 440 | /// After calling `writableVector`, this function tracks how many bytes were | 440 | /// After calling `writableVector`, this function tracks how many bytes were |
| 441 | /// written to it. | 441 | /// written to it. |
| 442 | pub fn advanceVector(w: *Writer, n: usize) usize { | 442 | pub fn advanceVector(w: *Writer, n: usize) usize { |
| 443 | return consume(w, n); | 443 | if (w.vtable != VectorWrapper.vtable) advance(w, n); |
| 444 | return n; | ||
| 444 | } | 445 | } |
| 445 | 446 | ||
| 446 | /// The `data` parameter is mutable because this function needs to mutate the | 447 | /// 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 { | ... | @@ -912,18 +912,9 @@ const WasiThreadImpl = struct { |
| 912 | allocator.free(self.thread.memory); | 912 | allocator.free(self.thread.memory); |
| 913 | } | 913 | } |
| 914 | 914 | ||
| 915 | var spin: u8 = 10; | ||
| 916 | while (true) { | 915 | while (true) { |
| 917 | const tid = self.thread.tid.load(.seq_cst); | 916 | const tid = self.thread.tid.load(.seq_cst); |
| 918 | if (tid == 0) { | 917 | if (tid == 0) break; |
| 919 | break; | ||
| 920 | } | ||
| 921 | |||
| 922 | if (spin > 0) { | ||
| 923 | spin -= 1; | ||
| 924 | std.atomic.spinLoopHint(); | ||
| 925 | continue; | ||
| 926 | } | ||
| 927 | 918 | ||
| 928 | const result = asm ( | 919 | const result = asm ( |
| 929 | \\ local.get %[ptr] | 920 | \\ local.get %[ptr] |
| ... | @@ -1515,18 +1506,9 @@ const LinuxThreadImpl = struct { | ... | @@ -1515,18 +1506,9 @@ const LinuxThreadImpl = struct { |
| 1515 | fn join(self: Impl) void { | 1506 | fn join(self: Impl) void { |
| 1516 | defer posix.munmap(self.thread.mapped); | 1507 | defer posix.munmap(self.thread.mapped); |
| 1517 | 1508 | ||
| 1518 | var spin: u8 = 10; | ||
| 1519 | while (true) { | 1509 | while (true) { |
| 1520 | const tid = self.thread.child_tid.load(.seq_cst); | 1510 | const tid = self.thread.child_tid.load(.seq_cst); |
| 1521 | if (tid == 0) { | 1511 | if (tid == 0) break; |
| 1522 | break; | ||
| 1523 | } | ||
| 1524 | |||
| 1525 | if (spin > 0) { | ||
| 1526 | spin -= 1; | ||
| 1527 | std.atomic.spinLoopHint(); | ||
| 1528 | continue; | ||
| 1529 | } | ||
| 1530 | 1512 | ||
| 1531 | switch (linux.E.init(linux.futex_4arg( | 1513 | switch (linux.E.init(linux.futex_4arg( |
| 1532 | &self.thread.child_tid.raw, | 1514 | &self.thread.child_tid.raw, |
| ... | @@ -1617,7 +1599,6 @@ test "setName, getName" { | ... | @@ -1617,7 +1599,6 @@ test "setName, getName" { |
| 1617 | } | 1599 | } |
| 1618 | 1600 | ||
| 1619 | test { | 1601 | test { |
| 1620 | // Doesn't use testing.refAllDecls() since that would pull in the compileError spinLoopHint. | ||
| 1621 | _ = Futex; | 1602 | _ = Futex; |
| 1622 | _ = ResetEvent; | 1603 | _ = ResetEvent; |
| 1623 | _ = Mutex; | 1604 | _ = Mutex; |
lib/std/c.zig+2-2| ... | @@ -10497,9 +10497,9 @@ pub const sysconf = switch (native_os) { | ... | @@ -10497,9 +10497,9 @@ pub const sysconf = switch (native_os) { |
| 10497 | 10497 | ||
| 10498 | pub const sf_hdtr = switch (native_os) { | 10498 | pub const sf_hdtr = switch (native_os) { |
| 10499 | .freebsd, .macos, .ios, .tvos, .watchos, .visionos => extern struct { | 10499 | .freebsd, .macos, .ios, .tvos, .watchos, .visionos => extern struct { |
| 10500 | headers: [*]const iovec_const, | 10500 | headers: ?[*]const iovec_const, |
| 10501 | hdr_cnt: c_int, | 10501 | hdr_cnt: c_int, |
| 10502 | trailers: [*]const iovec_const, | 10502 | trailers: ?[*]const iovec_const, |
| 10503 | trl_cnt: c_int, | 10503 | trl_cnt: c_int, |
| 10504 | }, | 10504 | }, |
| 10505 | else => void, | 10505 | else => void, |
lib/std/elf.zig+40-94| ... | @@ -482,6 +482,7 @@ pub const Header = struct { | ... | @@ -482,6 +482,7 @@ pub const Header = struct { |
| 482 | is_64: bool, | 482 | is_64: bool, |
| 483 | endian: std.builtin.Endian, | 483 | endian: std.builtin.Endian, |
| 484 | os_abi: OSABI, | 484 | os_abi: OSABI, |
| 485 | /// The meaning of this value depends on `os_abi`. | ||
| 485 | abi_version: u8, | 486 | abi_version: u8, |
| 486 | type: ET, | 487 | type: ET, |
| 487 | machine: EM, | 488 | machine: EM, |
| ... | @@ -508,75 +509,54 @@ pub const Header = struct { | ... | @@ -508,75 +509,54 @@ pub const Header = struct { |
| 508 | }; | 509 | }; |
| 509 | } | 510 | } |
| 510 | 511 | ||
| 511 | pub const ReadError = std.io.Reader.Error || ParseError; | 512 | pub const ReadError = std.Io.Reader.Error || error{ |
| 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{ | ||
| 521 | InvalidElfMagic, | 513 | InvalidElfMagic, |
| 522 | InvalidElfVersion, | 514 | InvalidElfVersion, |
| 523 | InvalidElfClass, | 515 | InvalidElfClass, |
| 524 | InvalidElfEndian, | 516 | InvalidElfEndian, |
| 525 | }; | 517 | }; |
| 526 | 518 | ||
| 527 | pub fn parse(hdr_buf: *align(@alignOf(Elf64_Ehdr)) const [@sizeOf(Elf64_Ehdr)]u8) ParseError!Header { | 519 | pub fn read(r: *std.Io.Reader) ReadError!Header { |
| 528 | const hdr32: *const Elf32_Ehdr = @ptrCast(hdr_buf); | 520 | const buf = try r.peek(@sizeOf(Elf64_Ehdr)); |
| 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; | ||
| 532 | 521 | ||
| 533 | const is_64 = switch (hdr32.e_ident[EI_CLASS]) { | 522 | if (!mem.eql(u8, buf[0..4], MAGIC)) return error.InvalidElfMagic; |
| 534 | ELFCLASS32 => false, | 523 | if (buf[EI_VERSION] != 1) return error.InvalidElfVersion; |
| 535 | ELFCLASS64 => true, | ||
| 536 | else => return error.InvalidElfClass, | ||
| 537 | }; | ||
| 538 | 524 | ||
| 539 | const endian: std.builtin.Endian = switch (hdr32.e_ident[EI_DATA]) { | 525 | const endian: std.builtin.Endian = switch (buf[EI_DATA]) { |
| 540 | ELFDATA2LSB => .little, | 526 | ELFDATA2LSB => .little, |
| 541 | ELFDATA2MSB => .big, | 527 | ELFDATA2MSB => .big, |
| 542 | else => return error.InvalidElfEndian, | 528 | else => return error.InvalidElfEndian, |
| 543 | }; | 529 | }; |
| 544 | const need_bswap = endian != native_endian; | ||
| 545 | 530 | ||
| 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 { | ||
| 546 | // Converting integers to exhaustive enums using `@enumFromInt` could cause a panic. | 539 | // Converting integers to exhaustive enums using `@enumFromInt` could cause a panic. |
| 547 | comptime assert(!@typeInfo(OSABI).@"enum".is_exhaustive); | 540 | 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 | |||
| 565 | return .{ | 541 | 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 | }, | ||
| 567 | .endian = endian, | 547 | .endian = endian, |
| 568 | .os_abi = os_abi, | 548 | .os_abi = @enumFromInt(hdr.e_ident[EI_OSABI]), |
| 569 | .abi_version = abi_version, | 549 | .abi_version = hdr.e_ident[EI_ABIVERSION], |
| 570 | .type = @"type", | 550 | .type = hdr.e_type, |
| 571 | .machine = machine, | 551 | .machine = hdr.e_machine, |
| 572 | .entry = int(is_64, need_bswap, hdr32.e_entry, hdr64.e_entry), | 552 | .entry = hdr.e_entry, |
| 573 | .phoff = int(is_64, need_bswap, hdr32.e_phoff, hdr64.e_phoff), | 553 | .phoff = hdr.e_phoff, |
| 574 | .shoff = int(is_64, need_bswap, hdr32.e_shoff, hdr64.e_shoff), | 554 | .shoff = hdr.e_shoff, |
| 575 | .phentsize = int(is_64, need_bswap, hdr32.e_phentsize, hdr64.e_phentsize), | 555 | .phentsize = hdr.e_phentsize, |
| 576 | .phnum = int(is_64, need_bswap, hdr32.e_phnum, hdr64.e_phnum), | 556 | .phnum = hdr.e_phnum, |
| 577 | .shentsize = int(is_64, need_bswap, hdr32.e_shentsize, hdr64.e_shentsize), | 557 | .shentsize = hdr.e_shentsize, |
| 578 | .shnum = int(is_64, need_bswap, hdr32.e_shnum, hdr64.e_shnum), | 558 | .shnum = hdr.e_shnum, |
| 579 | .shstrndx = int(is_64, need_bswap, hdr32.e_shstrndx, hdr64.e_shstrndx), | 559 | .shstrndx = hdr.e_shstrndx, |
| 580 | }; | 560 | }; |
| 581 | } | 561 | } |
| 582 | }; | 562 | }; |
| ... | @@ -591,21 +571,15 @@ pub const ProgramHeaderIterator = struct { | ... | @@ -591,21 +571,15 @@ pub const ProgramHeaderIterator = struct { |
| 591 | defer it.index += 1; | 571 | defer it.index += 1; |
| 592 | 572 | ||
| 593 | if (it.elf_header.is_64) { | 573 | if (it.elf_header.is_64) { |
| 594 | var phdr: Elf64_Phdr = undefined; | 574 | const offset = it.elf_header.phoff + @sizeOf(Elf64_Phdr) * it.index; |
| 595 | const offset = it.elf_header.phoff + @sizeOf(@TypeOf(phdr)) * it.index; | ||
| 596 | try it.file_reader.seekTo(offset); | 575 | try it.file_reader.seekTo(offset); |
| 597 | try it.file_reader.interface.readSlice(@ptrCast(&phdr)); | 576 | const phdr = try it.file_reader.interface.takeStruct(Elf64_Phdr, it.elf_header.endian); |
| 598 | if (it.elf_header.endian != native_endian) | ||
| 599 | mem.byteSwapAllFields(Elf64_Phdr, &phdr); | ||
| 600 | return phdr; | 577 | return phdr; |
| 601 | } | 578 | } |
| 602 | 579 | ||
| 603 | var phdr: Elf32_Phdr = undefined; | 580 | const offset = it.elf_header.phoff + @sizeOf(Elf32_Phdr) * it.index; |
| 604 | const offset = it.elf_header.phoff + @sizeOf(@TypeOf(phdr)) * it.index; | ||
| 605 | try it.file_reader.seekTo(offset); | 581 | try it.file_reader.seekTo(offset); |
| 606 | try it.file_reader.interface.readSlice(@ptrCast(&phdr)); | 582 | const phdr = try it.file_reader.interface.takeStruct(Elf32_Phdr, it.elf_header.endian); |
| 607 | if (it.elf_header.endian != native_endian) | ||
| 608 | mem.byteSwapAllFields(Elf32_Phdr, &phdr); | ||
| 609 | return .{ | 583 | return .{ |
| 610 | .p_type = phdr.p_type, | 584 | .p_type = phdr.p_type, |
| 611 | .p_offset = phdr.p_offset, | 585 | .p_offset = phdr.p_offset, |
| ... | @@ -629,21 +603,13 @@ pub const SectionHeaderIterator = struct { | ... | @@ -629,21 +603,13 @@ pub const SectionHeaderIterator = struct { |
| 629 | defer it.index += 1; | 603 | defer it.index += 1; |
| 630 | 604 | ||
| 631 | if (it.elf_header.is_64) { | 605 | if (it.elf_header.is_64) { |
| 632 | var shdr: Elf64_Shdr = undefined; | 606 | try it.file_reader.seekTo(it.elf_header.shoff + @sizeOf(Elf64_Shdr) * it.index); |
| 633 | const offset = it.elf_header.shoff + @sizeOf(@TypeOf(shdr)) * it.index; | 607 | const shdr = try it.file_reader.interface.takeStruct(Elf64_Shdr, it.elf_header.endian); |
| 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); | ||
| 638 | return shdr; | 608 | return shdr; |
| 639 | } | 609 | } |
| 640 | 610 | ||
| 641 | var shdr: Elf32_Shdr = undefined; | 611 | try it.file_reader.seekTo(it.elf_header.shoff + @sizeOf(Elf32_Shdr) * it.index); |
| 642 | const offset = it.elf_header.shoff + @sizeOf(@TypeOf(shdr)) * it.index; | 612 | const shdr = try it.file_reader.interface.takeStruct(Elf32_Shdr, it.elf_header.endian); |
| 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); | ||
| 647 | return .{ | 613 | return .{ |
| 648 | .sh_name = shdr.sh_name, | 614 | .sh_name = shdr.sh_name, |
| 649 | .sh_type = shdr.sh_type, | 615 | .sh_type = shdr.sh_type, |
| ... | @@ -659,26 +625,6 @@ pub const SectionHeaderIterator = struct { | ... | @@ -659,26 +625,6 @@ pub const SectionHeaderIterator = struct { |
| 659 | } | 625 | } |
| 660 | }; | 626 | }; |
| 661 | 627 | ||
| 662 | fn 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 | |||
| 674 | fn 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 | |||
| 682 | pub const ELFCLASSNONE = 0; | 628 | pub const ELFCLASSNONE = 0; |
| 683 | pub const ELFCLASS32 = 1; | 629 | pub const ELFCLASS32 = 1; |
| 684 | pub const ELFCLASS64 = 2; | 630 | pub const ELFCLASS64 = 2; |
lib/std/fs/Dir.zig+30-29| ... | @@ -1,3 +1,20 @@ | ... | @@ -1,3 +1,20 @@ |
| 1 | const Dir = @This(); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | const std = @import("../std.zig"); | ||
| 4 | const File = std.fs.File; | ||
| 5 | const AtomicFile = std.fs.AtomicFile; | ||
| 6 | const base64_encoder = fs.base64_encoder; | ||
| 7 | const posix = std.posix; | ||
| 8 | const mem = std.mem; | ||
| 9 | const path = fs.path; | ||
| 10 | const fs = std.fs; | ||
| 11 | const Allocator = std.mem.Allocator; | ||
| 12 | const assert = std.debug.assert; | ||
| 13 | const linux = std.os.linux; | ||
| 14 | const windows = std.os.windows; | ||
| 15 | const native_os = builtin.os.tag; | ||
| 16 | const have_flock = @TypeOf(posix.system.flock) != void; | ||
| 17 | |||
| 1 | fd: Handle, | 18 | fd: Handle, |
| 2 | 19 | ||
| 3 | pub const Handle = posix.fd_t; | 20 | pub const Handle = posix.fd_t; |
| ... | @@ -1862,9 +1879,10 @@ pub fn symLinkW( | ... | @@ -1862,9 +1879,10 @@ pub fn symLinkW( |
| 1862 | 1879 | ||
| 1863 | /// Same as `symLink`, except tries to create the symbolic link until it | 1880 | /// Same as `symLink`, except tries to create the symbolic link until it |
| 1864 | /// succeeds or encounters an error other than `error.PathAlreadyExists`. | 1881 | /// 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/). | 1882 | /// |
| 1866 | /// On WASI, both paths should be encoded as valid UTF-8. | 1883 | /// * On Windows, both paths should be encoded as [WTF-8](https://simonsapin.github.io/wtf-8/). |
| 1867 | /// On other platforms, both paths are an opaque sequence of bytes with no particular encoding. | 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. | ||
| 1868 | pub fn atomicSymLink( | 1886 | pub fn atomicSymLink( |
| 1869 | dir: Dir, | 1887 | dir: Dir, |
| 1870 | target_path: []const u8, | 1888 | target_path: []const u8, |
| ... | @@ -1880,9 +1898,8 @@ pub fn atomicSymLink( | ... | @@ -1880,9 +1898,8 @@ pub fn atomicSymLink( |
| 1880 | 1898 | ||
| 1881 | const dirname = path.dirname(sym_link_path) orelse "."; | 1899 | const dirname = path.dirname(sym_link_path) orelse "."; |
| 1882 | 1900 | ||
| 1883 | var rand_buf: [AtomicFile.random_bytes_len]u8 = undefined; | 1901 | const rand_len = @sizeOf(u64) * 2; |
| 1884 | 1902 | const temp_path_len = dirname.len + 1 + rand_len; | |
| 1885 | const temp_path_len = dirname.len + 1 + base64_encoder.calcSize(rand_buf.len); | ||
| 1886 | var temp_path_buf: [fs.max_path_bytes]u8 = undefined; | 1903 | var temp_path_buf: [fs.max_path_bytes]u8 = undefined; |
| 1887 | 1904 | ||
| 1888 | if (temp_path_len > temp_path_buf.len) return error.NameTooLong; | 1905 | if (temp_path_len > temp_path_buf.len) return error.NameTooLong; |
| ... | @@ -1892,8 +1909,8 @@ pub fn atomicSymLink( | ... | @@ -1892,8 +1909,8 @@ pub fn atomicSymLink( |
| 1892 | const temp_path = temp_path_buf[0..temp_path_len]; | 1909 | const temp_path = temp_path_buf[0..temp_path_len]; |
| 1893 | 1910 | ||
| 1894 | while (true) { | 1911 | while (true) { |
| 1895 | crypto.random.bytes(rand_buf[0..]); | 1912 | const random_integer = std.crypto.random.int(u64); |
| 1896 | _ = base64_encoder.encode(temp_path[dirname.len + 1 ..], rand_buf[0..]); | 1913 | temp_path[dirname.len + 1 ..][0..rand_len].* = std.fmt.hex(random_integer); |
| 1897 | 1914 | ||
| 1898 | if (dir.symLink(target_path, temp_path, flags)) { | 1915 | if (dir.symLink(target_path, temp_path, flags)) { |
| 1899 | return dir.rename(temp_path, sym_link_path); | 1916 | return dir.rename(temp_path, sym_link_path); |
| ... | @@ -2623,8 +2640,9 @@ pub fn updateFile( | ... | @@ -2623,8 +2640,9 @@ pub fn updateFile( |
| 2623 | return .stale; | 2640 | return .stale; |
| 2624 | } | 2641 | } |
| 2625 | 2642 | ||
| 2626 | pub const CopyFileError = File.OpenError || File.StatError || File.ReadError || File.WriteError || | 2643 | pub const CopyFileError = File.OpenError || File.StatError || |
| 2627 | AtomicFile.InitError || AtomicFile.FinishError; | 2644 | AtomicFile.InitError || AtomicFile.FinishError || |
| 2645 | File.ReadError || File.WriteError; | ||
| 2628 | 2646 | ||
| 2629 | /// Atomically creates a new file at `dest_path` within `dest_dir` with the | 2647 | /// Atomically creates a new file at `dest_path` within `dest_dir` with the |
| 2630 | /// same contents as `source_path` within `source_dir`, overwriting any already | 2648 | /// same contents as `source_path` within `source_dir`, overwriting any already |
| ... | @@ -2655,7 +2673,7 @@ pub fn copyFile( | ... | @@ -2655,7 +2673,7 @@ pub fn copyFile( |
| 2655 | break :blk st.mode; | 2673 | break :blk st.mode; |
| 2656 | }; | 2674 | }; |
| 2657 | 2675 | ||
| 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. |
| 2659 | var atomic_file = try dest_dir.atomicFile(dest_path, .{ | 2677 | var atomic_file = try dest_dir.atomicFile(dest_path, .{ |
| 2660 | .mode = mode, | 2678 | .mode = mode, |
| 2661 | .write_buffer = &buffer, | 2679 | .write_buffer = &buffer, |
| ... | @@ -2666,6 +2684,7 @@ pub fn copyFile( | ... | @@ -2666,6 +2684,7 @@ pub fn copyFile( |
| 2666 | error.ReadFailed => return file_reader.err.?, | 2684 | error.ReadFailed => return file_reader.err.?, |
| 2667 | error.WriteFailed => return atomic_file.file_writer.err.?, | 2685 | error.WriteFailed => return atomic_file.file_writer.err.?, |
| 2668 | }; | 2686 | }; |
| 2687 | |||
| 2669 | try atomic_file.finish(); | 2688 | try atomic_file.finish(); |
| 2670 | } | 2689 | } |
| 2671 | 2690 | ||
| ... | @@ -2790,21 +2809,3 @@ pub fn setPermissions(self: Dir, permissions: Permissions) SetPermissionsError!v | ... | @@ -2790,21 +2809,3 @@ pub fn setPermissions(self: Dir, permissions: Permissions) SetPermissionsError!v |
| 2790 | const file: File = .{ .handle = self.fd }; | 2809 | const file: File = .{ .handle = self.fd }; |
| 2791 | try file.setPermissions(permissions); | 2810 | try file.setPermissions(permissions); |
| 2792 | } | 2811 | } |
| 2793 | |||
| 2794 | const Dir = @This(); | ||
| 2795 | const builtin = @import("builtin"); | ||
| 2796 | const std = @import("../std.zig"); | ||
| 2797 | const File = std.fs.File; | ||
| 2798 | const AtomicFile = std.fs.AtomicFile; | ||
| 2799 | const base64_encoder = fs.base64_encoder; | ||
| 2800 | const crypto = std.crypto; | ||
| 2801 | const posix = std.posix; | ||
| 2802 | const mem = std.mem; | ||
| 2803 | const path = fs.path; | ||
| 2804 | const fs = std.fs; | ||
| 2805 | const Allocator = std.mem.Allocator; | ||
| 2806 | const assert = std.debug.assert; | ||
| 2807 | const linux = std.os.linux; | ||
| 2808 | const windows = std.os.windows; | ||
| 2809 | const native_os = builtin.os.tag; | ||
| 2810 | const have_flock = @TypeOf(posix.system.flock) != void; |
lib/std/fs/File.zig+173-22| ... | @@ -918,7 +918,7 @@ pub const Reader = struct { | ... | @@ -918,7 +918,7 @@ pub const Reader = struct { |
| 918 | err: ?ReadError = null, | 918 | err: ?ReadError = null, |
| 919 | mode: Reader.Mode = .positional, | 919 | mode: Reader.Mode = .positional, |
| 920 | /// Tracks the true seek position in the file. To obtain the logical | 920 | /// 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`. |
| 922 | pos: u64 = 0, | 922 | pos: u64 = 0, |
| 923 | size: ?u64 = null, | 923 | size: ?u64 = null, |
| 924 | size_err: ?GetEndPosError = null, | 924 | size_err: ?GetEndPosError = null, |
| ... | @@ -1011,14 +1011,12 @@ pub const Reader = struct { | ... | @@ -1011,14 +1011,12 @@ pub const Reader = struct { |
| 1011 | pub fn seekBy(r: *Reader, offset: i64) Reader.SeekError!void { | 1011 | pub fn seekBy(r: *Reader, offset: i64) Reader.SeekError!void { |
| 1012 | switch (r.mode) { | 1012 | switch (r.mode) { |
| 1013 | .positional, .positional_reading => { | 1013 | .positional, .positional_reading => { |
| 1014 | // TODO: make += operator allow any integer types | 1014 | setPosAdjustingBuffer(r, @intCast(@as(i64, @intCast(r.pos)) + offset)); |
| 1015 | r.pos = @intCast(@as(i64, @intCast(r.pos)) + offset); | ||
| 1016 | }, | 1015 | }, |
| 1017 | .streaming, .streaming_reading => { | 1016 | .streaming, .streaming_reading => { |
| 1018 | const seek_err = r.seek_err orelse e: { | 1017 | const seek_err = r.seek_err orelse e: { |
| 1019 | if (posix.lseek_CUR(r.file.handle, offset)) |_| { | 1018 | if (posix.lseek_CUR(r.file.handle, offset)) |_| { |
| 1020 | // TODO: make += operator allow any integer types | 1019 | setPosAdjustingBuffer(r, @intCast(@as(i64, @intCast(r.pos)) + offset)); |
| 1021 | r.pos = @intCast(@as(i64, @intCast(r.pos)) + offset); | ||
| 1022 | return; | 1020 | return; |
| 1023 | } else |err| { | 1021 | } else |err| { |
| 1024 | r.seek_err = err; | 1022 | r.seek_err = err; |
| ... | @@ -1034,6 +1032,8 @@ pub const Reader = struct { | ... | @@ -1034,6 +1032,8 @@ pub const Reader = struct { |
| 1034 | r.pos += n; | 1032 | r.pos += n; |
| 1035 | remaining -= n; | 1033 | remaining -= n; |
| 1036 | } | 1034 | } |
| 1035 | r.interface.seek = 0; | ||
| 1036 | r.interface.end = 0; | ||
| 1037 | }, | 1037 | }, |
| 1038 | .failure => return r.seek_err.?, | 1038 | .failure => return r.seek_err.?, |
| 1039 | } | 1039 | } |
| ... | @@ -1042,7 +1042,7 @@ pub const Reader = struct { | ... | @@ -1042,7 +1042,7 @@ pub const Reader = struct { |
| 1042 | pub fn seekTo(r: *Reader, offset: u64) Reader.SeekError!void { | 1042 | pub fn seekTo(r: *Reader, offset: u64) Reader.SeekError!void { |
| 1043 | switch (r.mode) { | 1043 | switch (r.mode) { |
| 1044 | .positional, .positional_reading => { | 1044 | .positional, .positional_reading => { |
| 1045 | r.pos = offset; | 1045 | setPosAdjustingBuffer(r, offset); |
| 1046 | }, | 1046 | }, |
| 1047 | .streaming, .streaming_reading => { | 1047 | .streaming, .streaming_reading => { |
| 1048 | if (offset >= r.pos) return Reader.seekBy(r, @intCast(offset - r.pos)); | 1048 | if (offset >= r.pos) return Reader.seekBy(r, @intCast(offset - r.pos)); |
| ... | @@ -1051,12 +1051,28 @@ pub const Reader = struct { | ... | @@ -1051,12 +1051,28 @@ pub const Reader = struct { |
| 1051 | r.seek_err = err; | 1051 | r.seek_err = err; |
| 1052 | return err; | 1052 | return err; |
| 1053 | }; | 1053 | }; |
| 1054 | r.pos = offset; | 1054 | setPosAdjustingBuffer(r, offset); |
| 1055 | }, | 1055 | }, |
| 1056 | .failure => return r.seek_err.?, | 1056 | .failure => return r.seek_err.?, |
| 1057 | } | 1057 | } |
| 1058 | } | 1058 | } |
| 1059 | 1059 | ||
| 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 | |||
| 1060 | /// Number of slices to store on the stack, when trying to send as many byte | 1076 | /// Number of slices to store on the stack, when trying to send as many byte |
| 1061 | /// vectors through the underlying read calls as possible. | 1077 | /// vectors through the underlying read calls as possible. |
| 1062 | const max_buffers_len = 16; | 1078 | const max_buffers_len = 16; |
| ... | @@ -1106,7 +1122,7 @@ pub const Reader = struct { | ... | @@ -1106,7 +1122,7 @@ pub const Reader = struct { |
| 1106 | return error.EndOfStream; | 1122 | return error.EndOfStream; |
| 1107 | } | 1123 | } |
| 1108 | r.pos += n; | 1124 | r.pos += n; |
| 1109 | return n; | 1125 | return w.advanceVector(n); |
| 1110 | }, | 1126 | }, |
| 1111 | .streaming_reading => { | 1127 | .streaming_reading => { |
| 1112 | if (is_windows) { | 1128 | if (is_windows) { |
| ... | @@ -1129,7 +1145,7 @@ pub const Reader = struct { | ... | @@ -1129,7 +1145,7 @@ pub const Reader = struct { |
| 1129 | return error.EndOfStream; | 1145 | return error.EndOfStream; |
| 1130 | } | 1146 | } |
| 1131 | r.pos += n; | 1147 | r.pos += n; |
| 1132 | return n; | 1148 | return w.advanceVector(n); |
| 1133 | }, | 1149 | }, |
| 1134 | .failure => return error.ReadFailed, | 1150 | .failure => return error.ReadFailed, |
| 1135 | } | 1151 | } |
| ... | @@ -1202,7 +1218,7 @@ pub const Reader = struct { | ... | @@ -1202,7 +1218,7 @@ pub const Reader = struct { |
| 1202 | } | 1218 | } |
| 1203 | return 0; | 1219 | return 0; |
| 1204 | }; | 1220 | }; |
| 1205 | const n = @min(size - pos, std.math.maxInt(i64), @intFromEnum(limit)); | 1221 | const n = @min(size - pos, maxInt(i64), @intFromEnum(limit)); |
| 1206 | file.seekBy(n) catch |err| { | 1222 | file.seekBy(n) catch |err| { |
| 1207 | r.seek_err = err; | 1223 | r.seek_err = err; |
| 1208 | return 0; | 1224 | return 0; |
| ... | @@ -1391,7 +1407,6 @@ pub const Writer = struct { | ... | @@ -1391,7 +1407,6 @@ pub const Writer = struct { |
| 1391 | const pattern = data[data.len - 1]; | 1407 | const pattern = data[data.len - 1]; |
| 1392 | if (pattern.len == 0 or splat == 0) return 0; | 1408 | if (pattern.len == 0 or splat == 0) return 0; |
| 1393 | const n = windows.WriteFile(handle, pattern, null) catch |err| { | 1409 | const n = windows.WriteFile(handle, pattern, null) catch |err| { |
| 1394 | std.debug.print("windows write file failed3: {t}\n", .{err}); | ||
| 1395 | w.err = err; | 1410 | w.err = err; |
| 1396 | return error.WriteFailed; | 1411 | return error.WriteFailed; |
| 1397 | }; | 1412 | }; |
| ... | @@ -1493,18 +1508,141 @@ pub const Writer = struct { | ... | @@ -1493,18 +1508,141 @@ pub const Writer = struct { |
| 1493 | file_reader: *Reader, | 1508 | file_reader: *Reader, |
| 1494 | limit: std.io.Limit, | 1509 | limit: std.io.Limit, |
| 1495 | ) std.io.Writer.FileError!usize { | 1510 | ) 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; | ||
| 1496 | const w: *Writer = @alignCast(@fieldParentPtr("interface", io_w)); | 1516 | const w: *Writer = @alignCast(@fieldParentPtr("interface", io_w)); |
| 1497 | const out_fd = w.file.handle; | 1517 | const out_fd = w.file.handle; |
| 1498 | const in_fd = file_reader.file.handle; | 1518 | const in_fd = file_reader.file.handle; |
| 1499 | // TODO try using copy_file_range on FreeBSD | 1519 | |
| 1500 | // TODO try using sendfile on macOS | 1520 | if (file_reader.size) |size| { |
| 1501 | // TODO try using sendfile on FreeBSD | 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 | |||
| 1502 | if (native_os == .linux and w.mode == .streaming) sf: { | 1640 | if (native_os == .linux and w.mode == .streaming) sf: { |
| 1503 | // Try using sendfile on Linux. | 1641 | // Try using sendfile on Linux. |
| 1504 | if (w.sendfile_err != null) break :sf; | 1642 | if (w.sendfile_err != null) break :sf; |
| 1505 | // Linux sendfile does not support headers. | 1643 | // Linux sendfile does not support headers. |
| 1506 | const buffered = limit.slice(file_reader.interface.buffer); | 1644 | if (writer_buffered.len != 0 or reader_buffered.len != 0) |
| 1507 | if (io_w.end != 0 or buffered.len != 0) return drain(io_w, &.{buffered}, 1); | 1645 | return sendFileBuffered(io_w, file_reader, reader_buffered); |
| 1508 | const max_count = 0x7ffff000; // Avoid EINVAL. | 1646 | const max_count = 0x7ffff000; // Avoid EINVAL. |
| 1509 | var off: std.os.linux.off_t = undefined; | 1647 | var off: std.os.linux.off_t = undefined; |
| 1510 | const off_ptr: ?*std.os.linux.off_t, const count: usize = switch (file_reader.mode) { | 1648 | const off_ptr: ?*std.os.linux.off_t, const count: usize = switch (file_reader.mode) { |
| ... | @@ -1551,6 +1689,7 @@ pub const Writer = struct { | ... | @@ -1551,6 +1689,7 @@ pub const Writer = struct { |
| 1551 | w.pos += n; | 1689 | w.pos += n; |
| 1552 | return n; | 1690 | return n; |
| 1553 | } | 1691 | } |
| 1692 | |||
| 1554 | const copy_file_range = switch (native_os) { | 1693 | const copy_file_range = switch (native_os) { |
| 1555 | .freebsd => std.os.freebsd.copy_file_range, | 1694 | .freebsd => std.os.freebsd.copy_file_range, |
| 1556 | .linux => if (std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 })) std.os.linux.wrapped.copy_file_range else {}, | 1695 | .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 { | ... | @@ -1558,8 +1697,8 @@ pub const Writer = struct { |
| 1558 | }; | 1697 | }; |
| 1559 | if (@TypeOf(copy_file_range) != void) cfr: { | 1698 | if (@TypeOf(copy_file_range) != void) cfr: { |
| 1560 | if (w.copy_file_range_err != null) break :cfr; | 1699 | if (w.copy_file_range_err != null) break :cfr; |
| 1561 | const buffered = limit.slice(file_reader.interface.buffer); | 1700 | if (writer_buffered.len != 0 or reader_buffered.len != 0) |
| 1562 | if (io_w.end != 0 or buffered.len != 0) return drain(io_w, &.{buffered}, 1); | 1701 | return sendFileBuffered(io_w, file_reader, reader_buffered); |
| 1563 | var off_in: i64 = undefined; | 1702 | var off_in: i64 = undefined; |
| 1564 | var off_out: i64 = undefined; | 1703 | var off_out: i64 = undefined; |
| 1565 | const off_in_ptr: ?*i64 = switch (file_reader.mode) { | 1704 | const off_in_ptr: ?*i64 = switch (file_reader.mode) { |
| ... | @@ -1598,6 +1737,9 @@ pub const Writer = struct { | ... | @@ -1598,6 +1737,9 @@ pub const Writer = struct { |
| 1598 | if (file_reader.pos != 0) break :fcf; | 1737 | if (file_reader.pos != 0) break :fcf; |
| 1599 | if (w.pos != 0) break :fcf; | 1738 | if (w.pos != 0) break :fcf; |
| 1600 | if (limit != .unlimited) break :fcf; | 1739 | 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); | ||
| 1601 | const rc = std.c.fcopyfile(in_fd, out_fd, null, .{ .DATA = true }); | 1743 | const rc = std.c.fcopyfile(in_fd, out_fd, null, .{ .DATA = true }); |
| 1602 | switch (posix.errno(rc)) { | 1744 | switch (posix.errno(rc)) { |
| 1603 | .SUCCESS => {}, | 1745 | .SUCCESS => {}, |
| ... | @@ -1618,15 +1760,24 @@ pub const Writer = struct { | ... | @@ -1618,15 +1760,24 @@ pub const Writer = struct { |
| 1618 | return 0; | 1760 | return 0; |
| 1619 | }, | 1761 | }, |
| 1620 | } | 1762 | } |
| 1621 | const n = if (file_reader.size) |size| size else @panic("TODO figure out how much copied"); | 1763 | file_reader.pos = size; |
| 1622 | file_reader.pos = n; | 1764 | w.pos = size; |
| 1623 | w.pos = n; | 1765 | return size; |
| 1624 | return n; | ||
| 1625 | } | 1766 | } |
| 1626 | 1767 | ||
| 1627 | return error.Unimplemented; | 1768 | return error.Unimplemented; |
| 1628 | } | 1769 | } |
| 1629 | 1770 | ||
| 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 | |||
| 1630 | pub fn seekTo(w: *Writer, offset: u64) SeekError!void { | 1781 | pub fn seekTo(w: *Writer, offset: u64) SeekError!void { |
| 1631 | switch (w.mode) { | 1782 | switch (w.mode) { |
| 1632 | .positional, .positional_reading => { | 1783 | .positional, .positional_reading => { |
lib/std/fs/test.zig+84-26| ... | @@ -1499,32 +1499,18 @@ test "sendfile" { | ... | @@ -1499,32 +1499,18 @@ test "sendfile" { |
| 1499 | const header2 = "second header\n"; | 1499 | const header2 = "second header\n"; |
| 1500 | const trailer1 = "trailer1\n"; | 1500 | const trailer1 = "trailer1\n"; |
| 1501 | const trailer2 = "second trailer\n"; | 1501 | const trailer2 = "second trailer\n"; |
| 1502 | var hdtr = [_]posix.iovec_const{ | 1502 | var headers: [2][]const u8 = .{ header1, header2 }; |
| 1503 | .{ | 1503 | var trailers: [2][]const u8 = .{ trailer1, trailer2 }; |
| 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 | }; | ||
| 1520 | 1504 | ||
| 1521 | var written_buf: [100]u8 = undefined; | 1505 | var written_buf: [100]u8 = undefined; |
| 1522 | try dest_file.writeFileAll(src_file, .{ | 1506 | var file_reader = src_file.reader(&.{}); |
| 1523 | .in_offset = 1, | 1507 | var fallback_buffer: [50]u8 = undefined; |
| 1524 | .in_len = 10, | 1508 | var file_writer = dest_file.writer(&fallback_buffer); |
| 1525 | .headers_and_trailers = &hdtr, | 1509 | try file_writer.interface.writeVecAll(&headers); |
| 1526 | .header_count = 2, | 1510 | try file_reader.seekTo(1); |
| 1527 | }); | 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(); | ||
| 1528 | const amt = try dest_file.preadAll(&written_buf, 0); | 1514 | const amt = try dest_file.preadAll(&written_buf, 0); |
| 1529 | try testing.expectEqualStrings("header1\nsecond header\nine1\nsecontrailer1\nsecond trailer\n", written_buf[0..amt]); | 1515 | try testing.expectEqualStrings("header1\nsecond header\nine1\nsecontrailer1\nsecond trailer\n", written_buf[0..amt]); |
| 1530 | } | 1516 | } |
| ... | @@ -1595,9 +1581,10 @@ test "AtomicFile" { | ... | @@ -1595,9 +1581,10 @@ test "AtomicFile" { |
| 1595 | ; | 1581 | ; |
| 1596 | 1582 | ||
| 1597 | { | 1583 | { |
| 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 }); | ||
| 1599 | defer af.deinit(); | 1586 | defer af.deinit(); |
| 1600 | try af.file.writeAll(test_content); | 1587 | try af.file_writer.interface.writeAll(test_content); |
| 1601 | try af.finish(); | 1588 | try af.finish(); |
| 1602 | } | 1589 | } |
| 1603 | const content = try ctx.dir.readFileAlloc(allocator, test_out_file, 9999); | 1590 | const content = try ctx.dir.readFileAlloc(allocator, test_out_file, 9999); |
| ... | @@ -2071,3 +2058,74 @@ test "invalid UTF-8/WTF-8 paths" { | ... | @@ -2071,3 +2058,74 @@ test "invalid UTF-8/WTF-8 paths" { |
| 2071 | } | 2058 | } |
| 2072 | }.impl); | 2059 | }.impl); |
| 2073 | } | 2060 | } |
| 2061 | |||
| 2062 | test "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 | |||
| 2092 | test "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; | ... | @@ -69,7 +69,6 @@ pub const ArrayHashMap = @import("json/hashmap.zig").ArrayHashMap; |
| 69 | pub const Scanner = @import("json/Scanner.zig"); | 69 | pub const Scanner = @import("json/Scanner.zig"); |
| 70 | pub const validate = Scanner.validate; | 70 | pub const validate = Scanner.validate; |
| 71 | pub const Error = Scanner.Error; | 71 | pub const Error = Scanner.Error; |
| 72 | pub const reader = Scanner.reader; | ||
| 73 | pub const default_buffer_size = Scanner.default_buffer_size; | 72 | pub const default_buffer_size = Scanner.default_buffer_size; |
| 74 | pub const Token = Scanner.Token; | 73 | pub const Token = Scanner.Token; |
| 75 | pub const TokenType = Scanner.TokenType; | 74 | pub const TokenType = Scanner.TokenType; |
lib/std/posix.zig-282| ... | @@ -6322,288 +6322,6 @@ pub fn send( | ... | @@ -6322,288 +6322,6 @@ pub fn send( |
| 6322 | }; | 6322 | }; |
| 6323 | } | 6323 | } |
| 6324 | 6324 | ||
| 6325 | pub 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)`. | ||
| 6362 | pub 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 | |||
| 6599 | fn 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 | |||
| 6607 | pub const PollError = error{ | 6325 | pub const PollError = error{ |
| 6608 | /// The network subsystem has failed. | 6326 | /// The network subsystem has failed. |
| 6609 | NetworkSubsystemFailed, | 6327 | NetworkSubsystemFailed, |
src/Builtin.zig+2-2| ... | @@ -342,9 +342,9 @@ pub fn updateFileOnDisk(file: *File, comp: *Compilation) !void { | ... | @@ -342,9 +342,9 @@ pub fn updateFileOnDisk(file: *File, comp: *Compilation) !void { |
| 342 | } | 342 | } |
| 343 | 343 | ||
| 344 | // `make_path` matters because the dir hasn't actually been created yet. | 344 | // `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 = &.{} }); |
| 346 | defer af.deinit(); | 346 | defer af.deinit(); |
| 347 | try af.file.writeAll(file.source.?); | 347 | try af.file_writer.interface.writeAll(file.source.?); |
| 348 | af.finish() catch |err| switch (err) { | 348 | af.finish() catch |err| switch (err) { |
| 349 | error.AccessDenied => switch (builtin.os.tag) { | 349 | error.AccessDenied => switch (builtin.os.tag) { |
| 350 | .windows => { | 350 | .windows => { |
src/Compilation.zig+117-117| ... | @@ -3382,7 +3382,7 @@ pub fn saveState(comp: *Compilation) !void { | ... | @@ -3382,7 +3382,7 @@ pub fn saveState(comp: *Compilation) !void { |
| 3382 | 3382 | ||
| 3383 | const gpa = comp.gpa; | 3383 | const gpa = comp.gpa; |
| 3384 | 3384 | ||
| 3385 | var bufs = std.ArrayList(std.posix.iovec_const).init(gpa); | 3385 | var bufs = std.ArrayList([]const u8).init(gpa); |
| 3386 | defer bufs.deinit(); | 3386 | defer bufs.deinit(); |
| 3387 | 3387 | ||
| 3388 | var pt_headers = std.ArrayList(Header.PerThread).init(gpa); | 3388 | var pt_headers = std.ArrayList(Header.PerThread).init(gpa); |
| ... | @@ -3421,50 +3421,50 @@ pub fn saveState(comp: *Compilation) !void { | ... | @@ -3421,50 +3421,50 @@ pub fn saveState(comp: *Compilation) !void { |
| 3421 | 3421 | ||
| 3422 | try bufs.ensureTotalCapacityPrecise(14 + 8 * pt_headers.items.len); | 3422 | try bufs.ensureTotalCapacityPrecise(14 + 8 * pt_headers.items.len); |
| 3423 | addBuf(&bufs, mem.asBytes(&header)); | 3423 | addBuf(&bufs, mem.asBytes(&header)); |
| 3424 | addBuf(&bufs, mem.sliceAsBytes(pt_headers.items)); | 3424 | addBuf(&bufs, @ptrCast(pt_headers.items)); |
| 3425 | 3425 | ||
| 3426 | addBuf(&bufs, mem.sliceAsBytes(ip.src_hash_deps.keys())); | 3426 | addBuf(&bufs, @ptrCast(ip.src_hash_deps.keys())); |
| 3427 | addBuf(&bufs, mem.sliceAsBytes(ip.src_hash_deps.values())); | 3427 | addBuf(&bufs, @ptrCast(ip.src_hash_deps.values())); |
| 3428 | addBuf(&bufs, mem.sliceAsBytes(ip.nav_val_deps.keys())); | 3428 | addBuf(&bufs, @ptrCast(ip.nav_val_deps.keys())); |
| 3429 | addBuf(&bufs, mem.sliceAsBytes(ip.nav_val_deps.values())); | 3429 | addBuf(&bufs, @ptrCast(ip.nav_val_deps.values())); |
| 3430 | addBuf(&bufs, mem.sliceAsBytes(ip.nav_ty_deps.keys())); | 3430 | addBuf(&bufs, @ptrCast(ip.nav_ty_deps.keys())); |
| 3431 | addBuf(&bufs, mem.sliceAsBytes(ip.nav_ty_deps.values())); | 3431 | addBuf(&bufs, @ptrCast(ip.nav_ty_deps.values())); |
| 3432 | addBuf(&bufs, mem.sliceAsBytes(ip.interned_deps.keys())); | 3432 | addBuf(&bufs, @ptrCast(ip.interned_deps.keys())); |
| 3433 | addBuf(&bufs, mem.sliceAsBytes(ip.interned_deps.values())); | 3433 | addBuf(&bufs, @ptrCast(ip.interned_deps.values())); |
| 3434 | addBuf(&bufs, mem.sliceAsBytes(ip.zon_file_deps.keys())); | 3434 | addBuf(&bufs, @ptrCast(ip.zon_file_deps.keys())); |
| 3435 | addBuf(&bufs, mem.sliceAsBytes(ip.zon_file_deps.values())); | 3435 | addBuf(&bufs, @ptrCast(ip.zon_file_deps.values())); |
| 3436 | addBuf(&bufs, mem.sliceAsBytes(ip.embed_file_deps.keys())); | 3436 | addBuf(&bufs, @ptrCast(ip.embed_file_deps.keys())); |
| 3437 | addBuf(&bufs, mem.sliceAsBytes(ip.embed_file_deps.values())); | 3437 | addBuf(&bufs, @ptrCast(ip.embed_file_deps.values())); |
| 3438 | addBuf(&bufs, mem.sliceAsBytes(ip.namespace_deps.keys())); | 3438 | addBuf(&bufs, @ptrCast(ip.namespace_deps.keys())); |
| 3439 | addBuf(&bufs, mem.sliceAsBytes(ip.namespace_deps.values())); | 3439 | addBuf(&bufs, @ptrCast(ip.namespace_deps.values())); |
| 3440 | addBuf(&bufs, mem.sliceAsBytes(ip.namespace_name_deps.keys())); | 3440 | addBuf(&bufs, @ptrCast(ip.namespace_name_deps.keys())); |
| 3441 | addBuf(&bufs, mem.sliceAsBytes(ip.namespace_name_deps.values())); | 3441 | addBuf(&bufs, @ptrCast(ip.namespace_name_deps.values())); |
| 3442 | 3442 | ||
| 3443 | addBuf(&bufs, mem.sliceAsBytes(ip.first_dependency.keys())); | 3443 | addBuf(&bufs, @ptrCast(ip.first_dependency.keys())); |
| 3444 | addBuf(&bufs, mem.sliceAsBytes(ip.first_dependency.values())); | 3444 | addBuf(&bufs, @ptrCast(ip.first_dependency.values())); |
| 3445 | addBuf(&bufs, mem.sliceAsBytes(ip.dep_entries.items)); | 3445 | addBuf(&bufs, @ptrCast(ip.dep_entries.items)); |
| 3446 | addBuf(&bufs, mem.sliceAsBytes(ip.free_dep_entries.items)); | 3446 | addBuf(&bufs, @ptrCast(ip.free_dep_entries.items)); |
| 3447 | 3447 | ||
| 3448 | for (ip.locals, pt_headers.items) |*local, pt_header| { | 3448 | for (ip.locals, pt_headers.items) |*local, pt_header| { |
| 3449 | if (pt_header.intern_pool.limbs_len > 0) { | 3449 | 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])); |
| 3451 | } | 3451 | } |
| 3452 | if (pt_header.intern_pool.extra_len > 0) { | 3452 | 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])); |
| 3454 | } | 3454 | } |
| 3455 | if (pt_header.intern_pool.items_len > 0) { | 3455 | 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])); | 3456 | addBuf(&bufs, @ptrCast(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])); | 3457 | addBuf(&bufs, @ptrCast(local.shared.items.view().items(.tag)[0..pt_header.intern_pool.items_len])); |
| 3458 | } | 3458 | } |
| 3459 | if (pt_header.intern_pool.string_bytes_len > 0) { | 3459 | if (pt_header.intern_pool.string_bytes_len > 0) { |
| 3460 | addBuf(&bufs, local.shared.strings.view().items(.@"0")[0..pt_header.intern_pool.string_bytes_len]); | 3460 | addBuf(&bufs, local.shared.strings.view().items(.@"0")[0..pt_header.intern_pool.string_bytes_len]); |
| 3461 | } | 3461 | } |
| 3462 | if (pt_header.intern_pool.tracked_insts_len > 0) { | 3462 | 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])); |
| 3464 | } | 3464 | } |
| 3465 | if (pt_header.intern_pool.files_len > 0) { | 3465 | 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])); | 3466 | addBuf(&bufs, @ptrCast(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])); | 3467 | addBuf(&bufs, @ptrCast(local.shared.files.view().items(.root_type)[0..pt_header.intern_pool.files_len])); |
| 3468 | } | 3468 | } |
| 3469 | } | 3469 | } |
| 3470 | 3470 | ||
| ... | @@ -3482,95 +3482,95 @@ pub fn saveState(comp: *Compilation) !void { | ... | @@ -3482,95 +3482,95 @@ pub fn saveState(comp: *Compilation) !void { |
| 3482 | try bufs.ensureUnusedCapacity(85); | 3482 | try bufs.ensureUnusedCapacity(85); |
| 3483 | addBuf(&bufs, wasm.string_bytes.items); | 3483 | addBuf(&bufs, wasm.string_bytes.items); |
| 3484 | // TODO make it well-defined memory layout | 3484 | // TODO make it well-defined memory layout |
| 3485 | //addBuf(&bufs, mem.sliceAsBytes(wasm.objects.items)); | 3485 | //addBuf(&bufs, @ptrCast(wasm.objects.items)); |
| 3486 | addBuf(&bufs, mem.sliceAsBytes(wasm.func_types.keys())); | 3486 | addBuf(&bufs, @ptrCast(wasm.func_types.keys())); |
| 3487 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_function_imports.keys())); | 3487 | addBuf(&bufs, @ptrCast(wasm.object_function_imports.keys())); |
| 3488 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_function_imports.values())); | 3488 | addBuf(&bufs, @ptrCast(wasm.object_function_imports.values())); |
| 3489 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_functions.items)); | 3489 | addBuf(&bufs, @ptrCast(wasm.object_functions.items)); |
| 3490 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_global_imports.keys())); | 3490 | addBuf(&bufs, @ptrCast(wasm.object_global_imports.keys())); |
| 3491 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_global_imports.values())); | 3491 | addBuf(&bufs, @ptrCast(wasm.object_global_imports.values())); |
| 3492 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_globals.items)); | 3492 | addBuf(&bufs, @ptrCast(wasm.object_globals.items)); |
| 3493 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_table_imports.keys())); | 3493 | addBuf(&bufs, @ptrCast(wasm.object_table_imports.keys())); |
| 3494 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_table_imports.values())); | 3494 | addBuf(&bufs, @ptrCast(wasm.object_table_imports.values())); |
| 3495 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_tables.items)); | 3495 | addBuf(&bufs, @ptrCast(wasm.object_tables.items)); |
| 3496 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_memory_imports.keys())); | 3496 | addBuf(&bufs, @ptrCast(wasm.object_memory_imports.keys())); |
| 3497 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_memory_imports.values())); | 3497 | addBuf(&bufs, @ptrCast(wasm.object_memory_imports.values())); |
| 3498 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_memories.items)); | 3498 | addBuf(&bufs, @ptrCast(wasm.object_memories.items)); |
| 3499 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations.items(.tag))); | 3499 | addBuf(&bufs, @ptrCast(wasm.object_relocations.items(.tag))); |
| 3500 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations.items(.offset))); | 3500 | addBuf(&bufs, @ptrCast(wasm.object_relocations.items(.offset))); |
| 3501 | // TODO handle the union safety field | 3501 | // TODO handle the union safety field |
| 3502 | //addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations.items(.pointee))); | 3502 | //addBuf(&bufs, @ptrCast(wasm.object_relocations.items(.pointee))); |
| 3503 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations.items(.addend))); | 3503 | addBuf(&bufs, @ptrCast(wasm.object_relocations.items(.addend))); |
| 3504 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_init_funcs.items)); | 3504 | addBuf(&bufs, @ptrCast(wasm.object_init_funcs.items)); |
| 3505 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_data_segments.items)); | 3505 | addBuf(&bufs, @ptrCast(wasm.object_data_segments.items)); |
| 3506 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_datas.items)); | 3506 | addBuf(&bufs, @ptrCast(wasm.object_datas.items)); |
| 3507 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_data_imports.keys())); | 3507 | addBuf(&bufs, @ptrCast(wasm.object_data_imports.keys())); |
| 3508 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_data_imports.values())); | 3508 | addBuf(&bufs, @ptrCast(wasm.object_data_imports.values())); |
| 3509 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_custom_segments.keys())); | 3509 | addBuf(&bufs, @ptrCast(wasm.object_custom_segments.keys())); |
| 3510 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_custom_segments.values())); | 3510 | addBuf(&bufs, @ptrCast(wasm.object_custom_segments.values())); |
| 3511 | // TODO make it well-defined memory layout | 3511 | // TODO make it well-defined memory layout |
| 3512 | // addBuf(&bufs, mem.sliceAsBytes(wasm.object_comdats.items)); | 3512 | // addBuf(&bufs, @ptrCast(wasm.object_comdats.items)); |
| 3513 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations_table.keys())); | 3513 | addBuf(&bufs, @ptrCast(wasm.object_relocations_table.keys())); |
| 3514 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_relocations_table.values())); | 3514 | addBuf(&bufs, @ptrCast(wasm.object_relocations_table.values())); |
| 3515 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_comdat_symbols.items(.kind))); | 3515 | addBuf(&bufs, @ptrCast(wasm.object_comdat_symbols.items(.kind))); |
| 3516 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_comdat_symbols.items(.index))); | 3516 | addBuf(&bufs, @ptrCast(wasm.object_comdat_symbols.items(.index))); |
| 3517 | addBuf(&bufs, mem.sliceAsBytes(wasm.out_relocs.items(.tag))); | 3517 | addBuf(&bufs, @ptrCast(wasm.out_relocs.items(.tag))); |
| 3518 | addBuf(&bufs, mem.sliceAsBytes(wasm.out_relocs.items(.offset))); | 3518 | addBuf(&bufs, @ptrCast(wasm.out_relocs.items(.offset))); |
| 3519 | // TODO handle the union safety field | 3519 | // TODO handle the union safety field |
| 3520 | //addBuf(&bufs, mem.sliceAsBytes(wasm.out_relocs.items(.pointee))); | 3520 | //addBuf(&bufs, @ptrCast(wasm.out_relocs.items(.pointee))); |
| 3521 | addBuf(&bufs, mem.sliceAsBytes(wasm.out_relocs.items(.addend))); | 3521 | addBuf(&bufs, @ptrCast(wasm.out_relocs.items(.addend))); |
| 3522 | addBuf(&bufs, mem.sliceAsBytes(wasm.uav_fixups.items)); | 3522 | addBuf(&bufs, @ptrCast(wasm.uav_fixups.items)); |
| 3523 | addBuf(&bufs, mem.sliceAsBytes(wasm.nav_fixups.items)); | 3523 | addBuf(&bufs, @ptrCast(wasm.nav_fixups.items)); |
| 3524 | addBuf(&bufs, mem.sliceAsBytes(wasm.func_table_fixups.items)); | 3524 | addBuf(&bufs, @ptrCast(wasm.func_table_fixups.items)); |
| 3525 | if (is_obj) { | 3525 | if (is_obj) { |
| 3526 | addBuf(&bufs, mem.sliceAsBytes(wasm.navs_obj.keys())); | 3526 | addBuf(&bufs, @ptrCast(wasm.navs_obj.keys())); |
| 3527 | addBuf(&bufs, mem.sliceAsBytes(wasm.navs_obj.values())); | 3527 | addBuf(&bufs, @ptrCast(wasm.navs_obj.values())); |
| 3528 | addBuf(&bufs, mem.sliceAsBytes(wasm.uavs_obj.keys())); | 3528 | addBuf(&bufs, @ptrCast(wasm.uavs_obj.keys())); |
| 3529 | addBuf(&bufs, mem.sliceAsBytes(wasm.uavs_obj.values())); | 3529 | addBuf(&bufs, @ptrCast(wasm.uavs_obj.values())); |
| 3530 | } else { | 3530 | } else { |
| 3531 | addBuf(&bufs, mem.sliceAsBytes(wasm.navs_exe.keys())); | 3531 | addBuf(&bufs, @ptrCast(wasm.navs_exe.keys())); |
| 3532 | addBuf(&bufs, mem.sliceAsBytes(wasm.navs_exe.values())); | 3532 | addBuf(&bufs, @ptrCast(wasm.navs_exe.values())); |
| 3533 | addBuf(&bufs, mem.sliceAsBytes(wasm.uavs_exe.keys())); | 3533 | addBuf(&bufs, @ptrCast(wasm.uavs_exe.keys())); |
| 3534 | addBuf(&bufs, mem.sliceAsBytes(wasm.uavs_exe.values())); | 3534 | addBuf(&bufs, @ptrCast(wasm.uavs_exe.values())); |
| 3535 | } | 3535 | } |
| 3536 | addBuf(&bufs, mem.sliceAsBytes(wasm.overaligned_uavs.keys())); | 3536 | addBuf(&bufs, @ptrCast(wasm.overaligned_uavs.keys())); |
| 3537 | addBuf(&bufs, mem.sliceAsBytes(wasm.overaligned_uavs.values())); | 3537 | addBuf(&bufs, @ptrCast(wasm.overaligned_uavs.values())); |
| 3538 | addBuf(&bufs, mem.sliceAsBytes(wasm.zcu_funcs.keys())); | 3538 | addBuf(&bufs, @ptrCast(wasm.zcu_funcs.keys())); |
| 3539 | // TODO handle the union safety field | 3539 | // TODO handle the union safety field |
| 3540 | // addBuf(&bufs, mem.sliceAsBytes(wasm.zcu_funcs.values())); | 3540 | // addBuf(&bufs, @ptrCast(wasm.zcu_funcs.values())); |
| 3541 | addBuf(&bufs, mem.sliceAsBytes(wasm.nav_exports.keys())); | 3541 | addBuf(&bufs, @ptrCast(wasm.nav_exports.keys())); |
| 3542 | addBuf(&bufs, mem.sliceAsBytes(wasm.nav_exports.values())); | 3542 | addBuf(&bufs, @ptrCast(wasm.nav_exports.values())); |
| 3543 | addBuf(&bufs, mem.sliceAsBytes(wasm.uav_exports.keys())); | 3543 | addBuf(&bufs, @ptrCast(wasm.uav_exports.keys())); |
| 3544 | addBuf(&bufs, mem.sliceAsBytes(wasm.uav_exports.values())); | 3544 | addBuf(&bufs, @ptrCast(wasm.uav_exports.values())); |
| 3545 | addBuf(&bufs, mem.sliceAsBytes(wasm.imports.keys())); | 3545 | addBuf(&bufs, @ptrCast(wasm.imports.keys())); |
| 3546 | addBuf(&bufs, mem.sliceAsBytes(wasm.missing_exports.keys())); | 3546 | addBuf(&bufs, @ptrCast(wasm.missing_exports.keys())); |
| 3547 | addBuf(&bufs, mem.sliceAsBytes(wasm.function_exports.keys())); | 3547 | addBuf(&bufs, @ptrCast(wasm.function_exports.keys())); |
| 3548 | addBuf(&bufs, mem.sliceAsBytes(wasm.function_exports.values())); | 3548 | addBuf(&bufs, @ptrCast(wasm.function_exports.values())); |
| 3549 | addBuf(&bufs, mem.sliceAsBytes(wasm.hidden_function_exports.keys())); | 3549 | addBuf(&bufs, @ptrCast(wasm.hidden_function_exports.keys())); |
| 3550 | addBuf(&bufs, mem.sliceAsBytes(wasm.hidden_function_exports.values())); | 3550 | addBuf(&bufs, @ptrCast(wasm.hidden_function_exports.values())); |
| 3551 | addBuf(&bufs, mem.sliceAsBytes(wasm.global_exports.items)); | 3551 | addBuf(&bufs, @ptrCast(wasm.global_exports.items)); |
| 3552 | addBuf(&bufs, mem.sliceAsBytes(wasm.functions.keys())); | 3552 | addBuf(&bufs, @ptrCast(wasm.functions.keys())); |
| 3553 | addBuf(&bufs, mem.sliceAsBytes(wasm.function_imports.keys())); | 3553 | addBuf(&bufs, @ptrCast(wasm.function_imports.keys())); |
| 3554 | addBuf(&bufs, mem.sliceAsBytes(wasm.function_imports.values())); | 3554 | addBuf(&bufs, @ptrCast(wasm.function_imports.values())); |
| 3555 | addBuf(&bufs, mem.sliceAsBytes(wasm.data_imports.keys())); | 3555 | addBuf(&bufs, @ptrCast(wasm.data_imports.keys())); |
| 3556 | addBuf(&bufs, mem.sliceAsBytes(wasm.data_imports.values())); | 3556 | addBuf(&bufs, @ptrCast(wasm.data_imports.values())); |
| 3557 | addBuf(&bufs, mem.sliceAsBytes(wasm.data_segments.keys())); | 3557 | addBuf(&bufs, @ptrCast(wasm.data_segments.keys())); |
| 3558 | addBuf(&bufs, mem.sliceAsBytes(wasm.globals.keys())); | 3558 | addBuf(&bufs, @ptrCast(wasm.globals.keys())); |
| 3559 | addBuf(&bufs, mem.sliceAsBytes(wasm.global_imports.keys())); | 3559 | addBuf(&bufs, @ptrCast(wasm.global_imports.keys())); |
| 3560 | addBuf(&bufs, mem.sliceAsBytes(wasm.global_imports.values())); | 3560 | addBuf(&bufs, @ptrCast(wasm.global_imports.values())); |
| 3561 | addBuf(&bufs, mem.sliceAsBytes(wasm.tables.keys())); | 3561 | addBuf(&bufs, @ptrCast(wasm.tables.keys())); |
| 3562 | addBuf(&bufs, mem.sliceAsBytes(wasm.table_imports.keys())); | 3562 | addBuf(&bufs, @ptrCast(wasm.table_imports.keys())); |
| 3563 | addBuf(&bufs, mem.sliceAsBytes(wasm.table_imports.values())); | 3563 | addBuf(&bufs, @ptrCast(wasm.table_imports.values())); |
| 3564 | addBuf(&bufs, mem.sliceAsBytes(wasm.zcu_indirect_function_set.keys())); | 3564 | addBuf(&bufs, @ptrCast(wasm.zcu_indirect_function_set.keys())); |
| 3565 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_indirect_function_import_set.keys())); | 3565 | addBuf(&bufs, @ptrCast(wasm.object_indirect_function_import_set.keys())); |
| 3566 | addBuf(&bufs, mem.sliceAsBytes(wasm.object_indirect_function_set.keys())); | 3566 | addBuf(&bufs, @ptrCast(wasm.object_indirect_function_set.keys())); |
| 3567 | addBuf(&bufs, mem.sliceAsBytes(wasm.mir_instructions.items(.tag))); | 3567 | addBuf(&bufs, @ptrCast(wasm.mir_instructions.items(.tag))); |
| 3568 | // TODO handle the union safety field | 3568 | // TODO handle the union safety field |
| 3569 | //addBuf(&bufs, mem.sliceAsBytes(wasm.mir_instructions.items(.data))); | 3569 | //addBuf(&bufs, @ptrCast(wasm.mir_instructions.items(.data))); |
| 3570 | addBuf(&bufs, mem.sliceAsBytes(wasm.mir_extra.items)); | 3570 | addBuf(&bufs, @ptrCast(wasm.mir_extra.items)); |
| 3571 | addBuf(&bufs, mem.sliceAsBytes(wasm.mir_locals.items)); | 3571 | addBuf(&bufs, @ptrCast(wasm.mir_locals.items)); |
| 3572 | addBuf(&bufs, mem.sliceAsBytes(wasm.tag_name_bytes.items)); | 3572 | addBuf(&bufs, @ptrCast(wasm.tag_name_bytes.items)); |
| 3573 | addBuf(&bufs, mem.sliceAsBytes(wasm.tag_name_offs.items)); | 3573 | addBuf(&bufs, @ptrCast(wasm.tag_name_offs.items)); |
| 3574 | 3574 | ||
| 3575 | // TODO add as header fields | 3575 | // TODO add as header fields |
| 3576 | // entry_resolution: FunctionImport.Resolution | 3576 | // entry_resolution: FunctionImport.Resolution |
| ... | @@ -3596,16 +3596,16 @@ pub fn saveState(comp: *Compilation) !void { | ... | @@ -3596,16 +3596,16 @@ pub fn saveState(comp: *Compilation) !void { |
| 3596 | 3596 | ||
| 3597 | // Using an atomic file prevents a crash or power failure from corrupting | 3597 | // Using an atomic file prevents a crash or power failure from corrupting |
| 3598 | // the previous incremental compilation state. | 3598 | // 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 }); | ||
| 3600 | defer af.deinit(); | 3601 | defer af.deinit(); |
| 3601 | try af.file.pwritevAll(bufs.items, 0); | 3602 | try af.file_writer.interface.writeVecAll(bufs.items); |
| 3602 | try af.finish(); | 3603 | try af.finish(); |
| 3603 | } | 3604 | } |
| 3604 | 3605 | ||
| 3605 | fn addBuf(list: *std.ArrayList(std.posix.iovec_const), buf: []const u8) void { | 3606 | fn addBuf(list: *std.ArrayList([]const u8), buf: []const u8) void { |
| 3606 | // Even when len=0, the undefined pointer might cause EFAULT. | ||
| 3607 | if (buf.len == 0) return; | 3607 | if (buf.len == 0) return; |
| 3608 | list.appendAssumeCapacity(.{ .base = buf.ptr, .len = buf.len }); | 3608 | list.appendAssumeCapacity(buf); |
| 3609 | } | 3609 | } |
| 3610 | 3610 | ||
| 3611 | /// This function is temporally single-threaded. | 3611 | /// This function is temporally single-threaded. |
src/Sema.zig+33-6| ... | @@ -5000,9 +5000,11 @@ fn validateUnionInit( | ... | @@ -5000,9 +5000,11 @@ fn validateUnionInit( |
| 5000 | } | 5000 | } |
| 5001 | if (init_ref) |v| try sema.validateRuntimeValue(block, block.nodeOffset(field_ptr_data.src_node), v); | 5001 | if (init_ref) |v| try sema.validateRuntimeValue(block, block.nodeOffset(field_ptr_data.src_node), v); |
| 5002 | 5002 | ||
| 5003 | const new_tag = Air.internedToRef(tag_val.toIntern()); | 5003 | if ((try sema.typeHasOnePossibleValue(tag_ty)) == null) { |
| 5004 | const set_tag_inst = try block.addBinOp(.set_union_tag, union_ptr, new_tag); | 5004 | const new_tag = Air.internedToRef(tag_val.toIntern()); |
| 5005 | try sema.checkComptimeKnownStore(block, set_tag_inst, LazySrcLoc.unneeded); // `unneeded` since this isn't a "proper" store | 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 | } | ||
| 5006 | } | 5008 | } |
| 5007 | 5009 | ||
| 5008 | fn validateStructInit( | 5010 | fn validateStructInit( |
| ... | @@ -6560,6 +6562,11 @@ fn resolveAnalyzedBlock( | ... | @@ -6560,6 +6562,11 @@ fn resolveAnalyzedBlock( |
| 6560 | } }, | 6562 | } }, |
| 6561 | }); | 6563 | }); |
| 6562 | } | 6564 | } |
| 6565 | |||
| 6566 | if (try sema.typeHasOnePossibleValue(resolved_ty)) |block_only_value| { | ||
| 6567 | return Air.internedToRef(block_only_value.toIntern()); | ||
| 6568 | } | ||
| 6569 | |||
| 6563 | return merges.block_inst.toRef(); | 6570 | return merges.block_inst.toRef(); |
| 6564 | } | 6571 | } |
| 6565 | 6572 | ||
| ... | @@ -9056,6 +9063,10 @@ fn analyzeErrUnionPayload( | ... | @@ -9056,6 +9063,10 @@ fn analyzeErrUnionPayload( |
| 9056 | try sema.addSafetyCheckUnwrapError(block, src, operand, .unwrap_errunion_err, .is_non_err); | 9063 | try sema.addSafetyCheckUnwrapError(block, src, operand, .unwrap_errunion_err, .is_non_err); |
| 9057 | } | 9064 | } |
| 9058 | 9065 | ||
| 9066 | if (try sema.typeHasOnePossibleValue(payload_ty)) |payload_only_value| { | ||
| 9067 | return Air.internedToRef(payload_only_value.toIntern()); | ||
| 9068 | } | ||
| 9069 | |||
| 9059 | return block.addTyOp(.unwrap_errunion_payload, payload_ty, operand); | 9070 | return block.addTyOp(.unwrap_errunion_payload, payload_ty, operand); |
| 9060 | } | 9071 | } |
| 9061 | 9072 | ||
| ... | @@ -19690,8 +19701,10 @@ fn zirStructInit( | ... | @@ -19690,8 +19701,10 @@ fn zirStructInit( |
| 19690 | const base_ptr = try sema.optEuBasePtrInit(block, alloc, src); | 19701 | const base_ptr = try sema.optEuBasePtrInit(block, alloc, src); |
| 19691 | const field_ptr = try sema.unionFieldPtr(block, field_src, base_ptr, field_name, field_src, resolved_ty, true); | 19702 | const field_ptr = try sema.unionFieldPtr(block, field_src, base_ptr, field_name, field_src, resolved_ty, true); |
| 19692 | try sema.storePtr(block, src, field_ptr, init_inst); | 19703 | try sema.storePtr(block, src, field_ptr, init_inst); |
| 19693 | const new_tag = Air.internedToRef(tag_val.toIntern()); | 19704 | if ((try sema.typeHasOnePossibleValue(tag_ty)) == null) { |
| 19694 | _ = try block.addBinOp(.set_union_tag, base_ptr, new_tag); | 19705 | const new_tag = Air.internedToRef(tag_val.toIntern()); |
| 19706 | _ = try block.addBinOp(.set_union_tag, base_ptr, new_tag); | ||
| 19707 | } | ||
| 19695 | return sema.makePtrConst(block, alloc); | 19708 | return sema.makePtrConst(block, alloc); |
| 19696 | } | 19709 | } |
| 19697 | 19710 | ||
| ... | @@ -28079,10 +28092,16 @@ fn unionFieldVal( | ... | @@ -28079,10 +28092,16 @@ fn unionFieldVal( |
| 28079 | const active_tag = try block.addTyOp(.get_union_tag, .fromInterned(union_obj.enum_tag_ty), union_byval); | 28092 | const active_tag = try block.addTyOp(.get_union_tag, .fromInterned(union_obj.enum_tag_ty), union_byval); |
| 28080 | try sema.addSafetyCheckInactiveUnionField(block, src, active_tag, wanted_tag); | 28093 | try sema.addSafetyCheckInactiveUnionField(block, src, active_tag, wanted_tag); |
| 28081 | } | 28094 | } |
| 28095 | |||
| 28082 | if (field_ty.zigTypeTag(zcu) == .noreturn) { | 28096 | if (field_ty.zigTypeTag(zcu) == .noreturn) { |
| 28083 | _ = try block.addNoOp(.unreach); | 28097 | _ = try block.addNoOp(.unreach); |
| 28084 | return .unreachable_value; | 28098 | return .unreachable_value; |
| 28085 | } | 28099 | } |
| 28100 | |||
| 28101 | if (try sema.typeHasOnePossibleValue(field_ty)) |field_only_value| { | ||
| 28102 | return Air.internedToRef(field_only_value.toIntern()); | ||
| 28103 | } | ||
| 28104 | |||
| 28086 | try field_ty.resolveLayout(pt); | 28105 | try field_ty.resolveLayout(pt); |
| 28087 | return block.addStructFieldVal(union_byval, field_index, field_ty); | 28106 | return block.addStructFieldVal(union_byval, field_index, field_ty); |
| 28088 | } | 28107 | } |
| ... | @@ -28214,12 +28233,12 @@ fn elemVal( | ... | @@ -28214,12 +28233,12 @@ fn elemVal( |
| 28214 | .many, .c => { | 28233 | .many, .c => { |
| 28215 | const maybe_indexable_val = try sema.resolveDefinedValue(block, indexable_src, indexable); | 28234 | const maybe_indexable_val = try sema.resolveDefinedValue(block, indexable_src, indexable); |
| 28216 | const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index); | 28235 | const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index); |
| 28236 | const elem_ty = indexable_ty.elemType2(zcu); | ||
| 28217 | 28237 | ||
| 28218 | ct: { | 28238 | ct: { |
| 28219 | const indexable_val = maybe_indexable_val orelse break :ct; | 28239 | const indexable_val = maybe_indexable_val orelse break :ct; |
| 28220 | const index_val = maybe_index_val orelse break :ct; | 28240 | const index_val = maybe_index_val orelse break :ct; |
| 28221 | const index: usize = @intCast(try index_val.toUnsignedIntSema(pt)); | 28241 | const index: usize = @intCast(try index_val.toUnsignedIntSema(pt)); |
| 28222 | const elem_ty = indexable_ty.elemType2(zcu); | ||
| 28223 | const many_ptr_ty = try pt.manyConstPtrType(elem_ty); | 28242 | const many_ptr_ty = try pt.manyConstPtrType(elem_ty); |
| 28224 | const many_ptr_val = try pt.getCoerced(indexable_val, many_ptr_ty); | 28243 | const many_ptr_val = try pt.getCoerced(indexable_val, many_ptr_ty); |
| 28225 | const elem_ptr_ty = try pt.singleConstPtrType(elem_ty); | 28244 | const elem_ptr_ty = try pt.singleConstPtrType(elem_ty); |
| ... | @@ -28228,6 +28247,10 @@ fn elemVal( | ... | @@ -28228,6 +28247,10 @@ fn elemVal( |
| 28228 | return Air.internedToRef((try pt.getCoerced(elem_val, elem_ty)).toIntern()); | 28247 | return Air.internedToRef((try pt.getCoerced(elem_val, elem_ty)).toIntern()); |
| 28229 | } | 28248 | } |
| 28230 | 28249 | ||
| 28250 | if (try sema.typeHasOnePossibleValue(elem_ty)) |elem_only_value| { | ||
| 28251 | return Air.internedToRef(elem_only_value.toIntern()); | ||
| 28252 | } | ||
| 28253 | |||
| 28231 | try sema.checkLogicalPtrOperation(block, src, indexable_ty); | 28254 | try sema.checkLogicalPtrOperation(block, src, indexable_ty); |
| 28232 | return block.addBinOp(.ptr_elem_val, indexable, elem_index); | 28255 | return block.addBinOp(.ptr_elem_val, indexable, elem_index); |
| 28233 | }, | 28256 | }, |
| ... | @@ -28578,6 +28601,10 @@ fn elemValSlice( | ... | @@ -28578,6 +28601,10 @@ fn elemValSlice( |
| 28578 | } | 28601 | } |
| 28579 | } | 28602 | } |
| 28580 | 28603 | ||
| 28604 | if (try sema.typeHasOnePossibleValue(elem_ty)) |elem_only_value| { | ||
| 28605 | return Air.internedToRef(elem_only_value.toIntern()); | ||
| 28606 | } | ||
| 28607 | |||
| 28581 | try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ty, slice_ty, slice_src); | 28608 | try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ty, slice_ty, slice_src); |
| 28582 | try sema.validateRuntimeValue(block, slice_src, slice); | 28609 | try sema.validateRuntimeValue(block, slice_src, slice); |
| 28583 | 28610 |
src/fmt.zig+2-2| ... | @@ -349,10 +349,10 @@ fn fmtPathFile( | ... | @@ -349,10 +349,10 @@ fn fmtPathFile( |
| 349 | try fmt.stdout_writer.interface.print("{s}\n", .{file_path}); | 349 | try fmt.stdout_writer.interface.print("{s}\n", .{file_path}); |
| 350 | fmt.any_error = true; | 350 | fmt.any_error = true; |
| 351 | } else { | 351 | } 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 = &.{} }); |
| 353 | defer af.deinit(); | 353 | defer af.deinit(); |
| 354 | 354 | ||
| 355 | try af.file.writeAll(fmt.out_buffer.getWritten()); | 355 | try af.file_writer.interface.writeAll(fmt.out_buffer.getWritten()); |
| 356 | try af.finish(); | 356 | try af.finish(); |
| 357 | try fmt.stdout_writer.interface.print("{s}\n", .{file_path}); | 357 | try fmt.stdout_writer.interface.print("{s}\n", .{file_path}); |
| 358 | } | 358 | } |
src/link/MachO.zig-1| ... | @@ -613,7 +613,6 @@ pub fn flush( | ... | @@ -613,7 +613,6 @@ pub fn flush( |
| 613 | }; | 613 | }; |
| 614 | const emit = self.base.emit; | 614 | const emit = self.base.emit; |
| 615 | invalidateKernelCache(emit.root_dir.handle, emit.sub_path) catch |err| switch (err) { | 615 | invalidateKernelCache(emit.root_dir.handle, emit.sub_path) catch |err| switch (err) { |
| 616 | error.OutOfMemory => return error.OutOfMemory, | ||
| 617 | else => |e| return diags.fail("failed to invalidate kernel cache: {s}", .{@errorName(e)}), | 616 | else => |e| return diags.fail("failed to invalidate kernel cache: {s}", .{@errorName(e)}), |
| 618 | }; | 617 | }; |
| 619 | } | 618 | } |
src/main.zig+3-1| ... | @@ -4624,7 +4624,9 @@ fn cmdTranslateC( | ... | @@ -4624,7 +4624,9 @@ fn cmdTranslateC( |
| 4624 | fatal("unable to open cached translated zig file '{s}{s}{s}': {s}", .{ path, fs.path.sep_str, out_zig_path, @errorName(err) }); | 4624 | fatal("unable to open cached translated zig file '{s}{s}{s}': {s}", .{ path, fs.path.sep_str, out_zig_path, @errorName(err) }); |
| 4625 | }; | 4625 | }; |
| 4626 | defer zig_file.close(); | 4626 | 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); | ||
| 4628 | return cleanExit(); | 4630 | return cleanExit(); |
| 4629 | } | 4631 | } |
| 4630 | } | 4632 | } |
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 | ||
| 6 | pub fn main() !void {} | ||
| 7 | comptime { @compileError("c0"); } | ||
| 8 | comptime { @compileError("c1"); } | ||
| 9 | comptime { @compileError("c2"); } | ||
| 10 | comptime { @compileError("c3"); } | ||
| 11 | comptime { @compileError("c4"); } | ||
| 12 | comptime { @compileError("c5"); } | ||
| 13 | comptime { @compileError("c6"); } | ||
| 14 | comptime { @compileError("c7"); } | ||
| 15 | comptime { @compileError("c8"); } | ||
| 16 | comptime { @compileError("c9"); } | ||
| 17 | export fn f0() void { @compileError("f0"); } | ||
| 18 | export fn f1() void { @compileError("f1"); } | ||
| 19 | export fn f2() void { @compileError("f2"); } | ||
| 20 | export fn f3() void { @compileError("f3"); } | ||
| 21 | export fn f4() void { @compileError("f4"); } | ||
| 22 | export fn f5() void { @compileError("f5"); } | ||
| 23 | export fn f6() void { @compileError("f6"); } | ||
| 24 | export fn f7() void { @compileError("f7"); } | ||
| 25 | export fn f8() void { @compileError("f8"); } | ||
| 26 | export 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 | ||
| 49 | pub fn main() !void {} | ||
| 50 | comptime {} | ||
| 51 | comptime {} | ||
| 52 | comptime {} | ||
| 53 | comptime {} | ||
| 54 | comptime {} | ||
| 55 | comptime {} | ||
| 56 | comptime {} | ||
| 57 | comptime {} | ||
| 58 | comptime {} | ||
| 59 | comptime {} | ||
| 60 | export fn f0() void {} | ||
| 61 | export fn f1() void {} | ||
| 62 | export fn f2() void {} | ||
| 63 | export fn f3() void {} | ||
| 64 | export fn f4() void {} | ||
| 65 | export fn f5() void {} | ||
| 66 | export fn f6() void {} | ||
| 67 | export fn f7() void {} | ||
| 68 | export fn f8() void {} | ||
| 69 | export fn f9() void {} | ||
| 70 | const 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 { | ... | @@ -65,69 +65,70 @@ pub fn build(b: *std.Build) void { |
| 65 | test_step.dependOn(&run_cmd.step); | 65 | test_step.dependOn(&run_cmd.step); |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | // Unwinding through a C shared library without a frame pointer (libc) | 68 | // https://github.com/ziglang/zig/issues/24522 |
| 69 | // | 69 | //// Unwinding through a C shared library without a frame pointer (libc) |
| 70 | // getcontext version: libc | 70 | //// |
| 71 | // | 71 | //// getcontext version: libc |
| 72 | // Unwind info type: | 72 | //// |
| 73 | // - ELF: DWARF .eh_frame + .debug_frame | 73 | //// Unwind info type: |
| 74 | // - MachO: __unwind_info encodings: | 74 | //// - ELF: DWARF .eh_frame + .debug_frame |
| 75 | // - x86_64: STACK_IMMD, STACK_IND | 75 | //// - MachO: __unwind_info encodings: |
| 76 | // - aarch64: FRAMELESS, DWARF | 76 | //// - x86_64: STACK_IMMD, STACK_IND |
| 77 | { | 77 | //// - aarch64: FRAMELESS, DWARF |
| 78 | const c_shared_lib = b.addLibrary(.{ | 78 | //{ |
| 79 | .linkage = .dynamic, | 79 | // const c_shared_lib = b.addLibrary(.{ |
| 80 | .name = "c_shared_lib", | 80 | // .linkage = .dynamic, |
| 81 | .root_module = b.createModule(.{ | 81 | // .name = "c_shared_lib", |
| 82 | .root_source_file = null, | 82 | // .root_module = b.createModule(.{ |
| 83 | .target = target, | 83 | // .root_source_file = null, |
| 84 | .optimize = optimize, | 84 | // .target = target, |
| 85 | .link_libc = true, | 85 | // .optimize = optimize, |
| 86 | .strip = false, | 86 | // .link_libc = true, |
| 87 | }), | 87 | // .strip = false, |
| 88 | }); | 88 | // }), |
| 89 | 89 | // }); | |
| 90 | if (target.result.os.tag == .windows) | 90 | |
| 91 | c_shared_lib.root_module.addCMacro("LIB_API", "__declspec(dllexport)"); | 91 | // if (target.result.os.tag == .windows) |
| 92 | 92 | // c_shared_lib.root_module.addCMacro("LIB_API", "__declspec(dllexport)"); | |
| 93 | c_shared_lib.root_module.addCSourceFile(.{ | 93 | |
| 94 | .file = b.path("shared_lib.c"), | 94 | // c_shared_lib.root_module.addCSourceFile(.{ |
| 95 | .flags = &.{"-fomit-frame-pointer"}, | 95 | // .file = b.path("shared_lib.c"), |
| 96 | }); | 96 | // .flags = &.{"-fomit-frame-pointer"}, |
| 97 | 97 | // }); | |
| 98 | const exe = b.addExecutable(.{ | 98 | |
| 99 | .name = "shared_lib_unwind", | 99 | // const exe = b.addExecutable(.{ |
| 100 | .root_module = b.createModule(.{ | 100 | // .name = "shared_lib_unwind", |
| 101 | .root_source_file = b.path("shared_lib_unwind.zig"), | 101 | // .root_module = b.createModule(.{ |
| 102 | .target = target, | 102 | // .root_source_file = b.path("shared_lib_unwind.zig"), |
| 103 | .optimize = optimize, | 103 | // .target = target, |
| 104 | .unwind_tables = if (target.result.os.tag.isDarwin()) .async else null, | 104 | // .optimize = optimize, |
| 105 | .omit_frame_pointer = true, | 105 | // .unwind_tables = if (target.result.os.tag.isDarwin()) .async else null, |
| 106 | }), | 106 | // .omit_frame_pointer = true, |
| 107 | // zig objcopy doesn't support incremental binaries | 107 | // }), |
| 108 | .use_llvm = true, | 108 | // // zig objcopy doesn't support incremental binaries |
| 109 | }); | 109 | // .use_llvm = true, |
| 110 | 110 | // }); | |
| 111 | exe.linkLibrary(c_shared_lib); | 111 | |
| 112 | 112 | // exe.linkLibrary(c_shared_lib); | |
| 113 | const run_cmd = b.addRunArtifact(exe); | 113 | |
| 114 | test_step.dependOn(&run_cmd.step); | 114 | // const run_cmd = b.addRunArtifact(exe); |
| 115 | 115 | // test_step.dependOn(&run_cmd.step); | |
| 116 | // Separate debug info ELF file | 116 | |
| 117 | if (target.result.ofmt == .elf) { | 117 | // // Separate debug info ELF file |
| 118 | const filename = b.fmt("{s}_stripped", .{exe.out_filename}); | 118 | // if (target.result.ofmt == .elf) { |
| 119 | const stripped_exe = b.addObjCopy(exe.getEmittedBin(), .{ | 119 | // const filename = b.fmt("{s}_stripped", .{exe.out_filename}); |
| 120 | .basename = filename, // set the name for the debuglink | 120 | // const stripped_exe = b.addObjCopy(exe.getEmittedBin(), .{ |
| 121 | .compress_debug = true, | 121 | // .basename = filename, // set the name for the debuglink |
| 122 | .strip = .debug, | 122 | // .compress_debug = true, |
| 123 | .extract_to_separate_file = true, | 123 | // .strip = .debug, |
| 124 | }); | 124 | // .extract_to_separate_file = true, |
| 125 | 125 | // }); | |
| 126 | const run_stripped = std.Build.Step.Run.create(b, b.fmt("run {s}", .{filename})); | 126 | |
| 127 | run_stripped.addFileArg(stripped_exe.getOutput()); | 127 | // const run_stripped = std.Build.Step.Run.create(b, b.fmt("run {s}", .{filename})); |
| 128 | test_step.dependOn(&run_stripped.step); | 128 | // run_stripped.addFileArg(stripped_exe.getOutput()); |
| 129 | } | 129 | // test_step.dependOn(&run_stripped.step); |
| 130 | } | 130 | // } |
| 131 | //} | ||
| 131 | 132 | ||
| 132 | // Unwinding without libc/posix | 133 | // Unwinding without libc/posix |
| 133 | // | 134 | // |
tools/gen_stubs.zig+2-1| ... | @@ -310,7 +310,8 @@ pub fn main() !void { | ... | @@ -310,7 +310,8 @@ pub fn main() !void { |
| 310 | build_all_path, libc_so_path, @errorName(err), | 310 | build_all_path, libc_so_path, @errorName(err), |
| 311 | }); | 311 | }); |
| 312 | }; | 312 | }; |
| 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); | ||
| 314 | 315 | ||
| 315 | const parse: Parse = .{ | 316 | const parse: Parse = .{ |
| 316 | .arena = arena, | 317 | .arena = arena, |