authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-20 16:43:47-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-21 12:32:37-07:00
logf1576ef14c5956cdab742aaf31ec4c672b54252b
tree9bea286fa8a5f297e040ffc6843a08f19b1185e5
parentf2a3ac7c0534a74ee544fdf6ef9d2176a8d62389

objcopy: delete most of it

this code is not up to zig project standards tracked by #24522 oh, and fix not adjusting buffer seek position in std.fs.File.Reader

5 files changed, 246 insertions(+), 1148 deletions(-)

lib/compiler/objcopy.zig+62-907
......@@ -13,6 +13,9 @@ const Server = std.zig.Server;
1313var stdin_buffer: [1024]u8 = undefined;
1414var stdout_buffer: [1024]u8 = undefined;
1515
16var input_buffer: [1024]u8 = undefined;
17var output_buffer: [1024]u8 = undefined;
18
1619pub fn main() !void {
1720 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
1821 defer arena_instance.deinit();
......@@ -145,13 +148,16 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
145148 const input = opt_input orelse fatal("expected input parameter", .{});
146149 const output = opt_output orelse fatal("expected output parameter", .{});
147150
148 var in_file = fs.cwd().openFile(input, .{}) catch |err|
149 fatal("unable to open '{s}': {s}", .{ input, @errorName(err) });
150 defer in_file.close();
151 const input_file = fs.cwd().openFile(input, .{}) catch |err| fatal("failed to open {s}: {t}", .{ input, err });
152 defer input_file.close();
153
154 const stat = input_file.stat() catch |err| fatal("failed to stat {s}: {t}", .{ input, err });
151155
152 const elf_hdr = std.elf.Header.read(in_file) catch |err| switch (err) {
153 error.InvalidElfMagic => fatal("not an ELF file: '{s}'", .{input}),
154 else => fatal("unable to read '{s}': {s}", .{ input, @errorName(err) }),
156 var in: File.Reader = .initSize(input_file, &input_buffer, stat.size);
157
158 const elf_hdr = std.elf.Header.read(&in.interface) catch |err| switch (err) {
159 error.ReadFailed => fatal("unable to read {s}: {t}", .{ input, in.err.? }),
160 else => |e| fatal("invalid elf file: {t}", .{e}),
155161 };
156162
157163 const in_ofmt = .elf;
......@@ -168,16 +174,12 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
168174 }
169175 };
170176
171 const mode = mode: {
172 if (out_fmt != .elf or only_keep_debug)
173 break :mode fs.File.default_mode;
174 if (in_file.stat()) |stat|
175 break :mode stat.mode
176 else |_|
177 break :mode fs.File.default_mode;
178 };
179 var out_file = try fs.cwd().createFile(output, .{ .mode = mode });
180 defer out_file.close();
177 const mode = if (out_fmt != .elf or only_keep_debug) fs.File.default_mode else stat.mode;
178
179 var output_file = try fs.cwd().createFile(output, .{ .mode = mode });
180 defer output_file.close();
181
182 var out = output_file.writer(&output_buffer);
181183
182184 switch (out_fmt) {
183185 .hex, .raw => {
......@@ -192,7 +194,7 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
192194 if (set_section_flags != null)
193195 fatal("zig objcopy: ELF to RAW or HEX copying does not support --set_section_flags", .{});
194196
195 try emitElf(arena, in_file, out_file, elf_hdr, .{
197 try emitElf(arena, &in, &out, elf_hdr, .{
196198 .ofmt = out_fmt,
197199 .only_section = only_section,
198200 .pad_to = pad_to,
......@@ -208,22 +210,13 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
208210 if (pad_to) |_|
209211 fatal("zig objcopy: ELF to ELF copying does not support --pad-to", .{});
210212
211 try stripElf(arena, in_file, out_file, elf_hdr, .{
212 .strip_debug = strip_debug,
213 .strip_all = strip_all,
214 .only_keep_debug = only_keep_debug,
215 .add_debuglink = opt_add_debuglink,
216 .extract_to = opt_extract,
217 .compress_debug = compress_debug_sections,
218 .add_section = add_section,
219 .set_section_alignment = set_section_alignment,
220 .set_section_flags = set_section_flags,
221 });
222 return std.process.cleanExit();
213 fatal("unimplemented", .{});
223214 },
224215 else => fatal("unsupported output object format: {s}", .{@tagName(out_fmt)}),
225216 }
226217
218 try out.end();
219
227220 if (listen) {
228221 var stdin_reader = fs.File.stdin().reader(&stdin_buffer);
229222 var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
......@@ -304,12 +297,12 @@ const SetSectionFlags = struct {
304297
305298fn emitElf(
306299 arena: Allocator,
307 in_file: File,
308 out_file: File,
300 in: *File.Reader,
301 out: *File.Writer,
309302 elf_hdr: elf.Header,
310303 options: EmitRawElfOptions,
311304) !void {
312 var binary_elf_output = try BinaryElfOutput.parse(arena, in_file, elf_hdr);
305 var binary_elf_output = try BinaryElfOutput.parse(arena, in, elf_hdr);
313306 defer binary_elf_output.deinit();
314307
315308 if (options.ofmt == .elf) {
......@@ -328,8 +321,8 @@ fn emitElf(
328321 continue;
329322 }
330323
331 try writeBinaryElfSection(in_file, out_file, section);
332 try padFile(out_file, options.pad_to);
324 try writeBinaryElfSection(in, out, section);
325 try padFile(out, options.pad_to);
333326 return;
334327 }
335328 },
......@@ -342,10 +335,10 @@ fn emitElf(
342335 switch (options.ofmt) {
343336 .raw => {
344337 for (binary_elf_output.sections.items) |section| {
345 try out_file.seekTo(section.binaryOffset);
346 try writeBinaryElfSection(in_file, out_file, section);
338 try out.seekTo(section.binaryOffset);
339 try writeBinaryElfSection(in, out, section);
347340 }
348 try padFile(out_file, options.pad_to);
341 try padFile(out, options.pad_to);
349342 },
350343 .hex => {
351344 if (binary_elf_output.segments.items.len == 0) return;
......@@ -353,15 +346,15 @@ fn emitElf(
353346 return error.InvalidHexfileAddressRange;
354347 }
355348
356 var hex_writer = HexWriter{ .out_file = out_file };
349 var hex_writer = HexWriter{ .out = out };
357350 for (binary_elf_output.segments.items) |segment| {
358 try hex_writer.writeSegment(segment, in_file);
351 try hex_writer.writeSegment(segment, in);
359352 }
360353 if (options.pad_to) |_| {
361354 // Padding to a size in hex files isn't applicable
362355 return error.InvalidArgument;
363356 }
364 try hex_writer.writeEOF();
357 try hex_writer.writeEof();
365358 },
366359 else => unreachable,
367360 }
......@@ -399,7 +392,7 @@ const BinaryElfOutput = struct {
399392 self.segments.deinit(self.allocator);
400393 }
401394
402 pub fn parse(allocator: Allocator, elf_file: File, elf_hdr: elf.Header) !Self {
395 pub fn parse(allocator: Allocator, in: *File.Reader, elf_hdr: elf.Header) !Self {
403396 var self: Self = .{
404397 .segments = .{},
405398 .sections = .{},
......@@ -412,7 +405,7 @@ const BinaryElfOutput = struct {
412405 self.shstrtab = blk: {
413406 if (elf_hdr.shstrndx >= elf_hdr.shnum) break :blk null;
414407
415 var section_headers = elf_hdr.section_header_iterator(&elf_file);
408 var section_headers = elf_hdr.iterateSectionHeaders(in);
416409
417410 var section_counter: usize = 0;
418411 while (section_counter < elf_hdr.shstrndx) : (section_counter += 1) {
......@@ -421,18 +414,13 @@ const BinaryElfOutput = struct {
421414
422415 const shstrtab_shdr = (try section_headers.next()).?;
423416
424 const buffer = try allocator.alloc(u8, @intCast(shstrtab_shdr.sh_size));
425 errdefer allocator.free(buffer);
426
427 const num_read = try elf_file.preadAll(buffer, shstrtab_shdr.sh_offset);
428 if (num_read != buffer.len) return error.EndOfStream;
429
430 break :blk buffer;
417 try in.seekTo(shstrtab_shdr.sh_offset);
418 break :blk try in.interface.readAlloc(allocator, shstrtab_shdr.sh_size);
431419 };
432420
433421 errdefer if (self.shstrtab) |shstrtab| allocator.free(shstrtab);
434422
435 var section_headers = elf_hdr.section_header_iterator(&elf_file);
423 var section_headers = elf_hdr.iterateSectionHeaders(in);
436424 while (try section_headers.next()) |section| {
437425 if (sectionValidForOutput(section)) {
438426 const newSection = try allocator.create(BinaryElfSection);
......@@ -451,7 +439,7 @@ const BinaryElfOutput = struct {
451439 }
452440 }
453441
454 var program_headers = elf_hdr.program_header_iterator(&elf_file);
442 var program_headers = elf_hdr.iterateProgramHeaders(in);
455443 while (try program_headers.next()) |phdr| {
456444 if (phdr.p_type == elf.PT_LOAD) {
457445 const newSegment = try allocator.create(BinaryElfSegment);
......@@ -539,19 +527,17 @@ const BinaryElfOutput = struct {
539527 }
540528};
541529
542fn writeBinaryElfSection(elf_file: File, out_file: File, section: *BinaryElfSection) !void {
543 try out_file.writeFileAll(elf_file, .{
544 .in_offset = section.elfOffset,
545 .in_len = section.fileSize,
546 });
530fn writeBinaryElfSection(in: *File.Reader, out: *File.Writer, section: *BinaryElfSection) !void {
531 try in.seekTo(section.elfOffset);
532 _ = try out.interface.sendFileAll(in, .limited(section.fileSize));
547533}
548534
549535const HexWriter = struct {
550536 prev_addr: ?u32 = null,
551 out_file: File,
537 out: *File.Writer,
552538
553539 /// Max data bytes per line of output
554 const MAX_PAYLOAD_LEN: u8 = 16;
540 const max_payload_len: u8 = 16;
555541
556542 fn addressParts(address: u16) [2]u8 {
557543 const msb: u8 = @truncate(address >> 8);
......@@ -627,13 +613,13 @@ const HexWriter = struct {
627613 return (sum ^ 0xFF) +% 1;
628614 }
629615
630 fn write(self: Record, file: File) File.WriteError!void {
616 fn write(self: Record, out: *File.Writer) !void {
631617 const linesep = "\r\n";
632618 // colon, (length, address, type, payload, checksum) as hex, CRLF
633 const BUFSIZE = 1 + (1 + 2 + 1 + MAX_PAYLOAD_LEN + 1) * 2 + linesep.len;
619 const BUFSIZE = 1 + (1 + 2 + 1 + max_payload_len + 1) * 2 + linesep.len;
634620 var outbuf: [BUFSIZE]u8 = undefined;
635621 const payload_bytes = self.getPayloadBytes();
636 assert(payload_bytes.len <= MAX_PAYLOAD_LEN);
622 assert(payload_bytes.len <= max_payload_len);
637623
638624 const line = try std.fmt.bufPrint(&outbuf, ":{0X:0>2}{1X:0>4}{2X:0>2}{3X}{4X:0>2}" ++ linesep, .{
639625 @as(u8, @intCast(payload_bytes.len)),
......@@ -642,38 +628,37 @@ const HexWriter = struct {
642628 payload_bytes,
643629 self.checksum(),
644630 });
645 try file.writeAll(line);
631 try out.interface.writeAll(line);
646632 }
647633 };
648634
649 pub fn writeSegment(self: *HexWriter, segment: *const BinaryElfSegment, elf_file: File) !void {
650 var buf: [MAX_PAYLOAD_LEN]u8 = undefined;
635 pub fn writeSegment(self: *HexWriter, segment: *const BinaryElfSegment, in: *File.Reader) !void {
636 var buf: [max_payload_len]u8 = undefined;
651637 var bytes_read: usize = 0;
652638 while (bytes_read < segment.fileSize) {
653639 const row_address: u32 = @intCast(segment.physicalAddress + bytes_read);
654640
655641 const remaining = segment.fileSize - bytes_read;
656 const to_read: usize = @intCast(@min(remaining, MAX_PAYLOAD_LEN));
657 const did_read = try elf_file.preadAll(buf[0..to_read], segment.elfOffset + bytes_read);
658 if (did_read < to_read) return error.UnexpectedEOF;
642 const dest = buf[0..@min(remaining, max_payload_len)];
643 try in.seekTo(segment.elfOffset + bytes_read);
644 try in.interface.readSliceAll(dest);
645 try self.writeDataRow(row_address, dest);
659646
660 try self.writeDataRow(row_address, buf[0..did_read]);
661
662 bytes_read += did_read;
647 bytes_read += dest.len;
663648 }
664649 }
665650
666 fn writeDataRow(self: *HexWriter, address: u32, data: []const u8) File.WriteError!void {
651 fn writeDataRow(self: *HexWriter, address: u32, data: []const u8) !void {
667652 const record = Record.Data(address, data);
668653 if (address > 0xFFFF and (self.prev_addr == null or record.address != self.prev_addr.?)) {
669 try Record.Address(address).write(self.out_file);
654 try Record.Address(address).write(self.out);
670655 }
671 try record.write(self.out_file);
656 try record.write(self.out);
672657 self.prev_addr = @intCast(record.address + data.len);
673658 }
674659
675 fn writeEOF(self: HexWriter) File.WriteError!void {
676 try Record.EOF().write(self.out_file);
660 fn writeEof(self: HexWriter) !void {
661 try Record.EOF().write(self.out);
677662 }
678663};
679664
......@@ -686,9 +671,9 @@ fn containsValidAddressRange(segments: []*BinaryElfSegment) bool {
686671 return true;
687672}
688673
689fn padFile(f: File, opt_size: ?u64) !void {
674fn padFile(out: *File.Writer, opt_size: ?u64) !void {
690675 const size = opt_size orelse return;
691 try f.setEndPos(size);
676 try out.file.setEndPos(size);
692677}
693678
694679test "HexWriter.Record.Address has correct payload and checksum" {
......@@ -732,836 +717,6 @@ test "containsValidAddressRange" {
732717 try std.testing.expect(containsValidAddressRange(&buf));
733718}
734719
735// -------------
736// ELF to ELF stripping
737
738const StripElfOptions = struct {
739 extract_to: ?[]const u8 = null,
740 add_debuglink: ?[]const u8 = null,
741 strip_all: bool = false,
742 strip_debug: bool = false,
743 only_keep_debug: bool = false,
744 compress_debug: bool = false,
745 add_section: ?AddSection,
746 set_section_alignment: ?SetSectionAlignment,
747 set_section_flags: ?SetSectionFlags,
748};
749
750fn stripElf(
751 allocator: Allocator,
752 in_file: File,
753 out_file: File,
754 elf_hdr: elf.Header,
755 options: StripElfOptions,
756) !void {
757 const Filter = ElfFileHelper.Filter;
758 const DebugLink = ElfFileHelper.DebugLink;
759
760 const filter: Filter = filter: {
761 if (options.only_keep_debug) break :filter .debug;
762 if (options.strip_all) break :filter .program;
763 if (options.strip_debug) break :filter .program_and_symbols;
764 break :filter .all;
765 };
766
767 const filter_complement: ?Filter = blk: {
768 if (options.extract_to) |_| {
769 break :blk switch (filter) {
770 .program => .debug_and_symbols,
771 .debug => .program_and_symbols,
772 .program_and_symbols => .debug,
773 .debug_and_symbols => .program,
774 .all => fatal("zig objcopy: nothing to extract", .{}),
775 };
776 } else {
777 break :blk null;
778 }
779 };
780 const debuglink_path = path: {
781 if (options.add_debuglink) |path| break :path path;
782 if (options.extract_to) |path| break :path path;
783 break :path null;
784 };
785
786 switch (elf_hdr.is_64) {
787 inline else => |is_64| {
788 var elf_file = try ElfFile(is_64).parse(allocator, in_file, elf_hdr);
789 defer elf_file.deinit();
790
791 if (options.add_section) |user_section| {
792 for (elf_file.sections) |section| {
793 if (std.mem.eql(u8, section.name, user_section.section_name)) {
794 fatal("zig objcopy: unable to add section '{s}'. Section already exists in input", .{user_section.section_name});
795 }
796 }
797 }
798
799 if (filter_complement) |flt| {
800 // write the .dbg file and close it, so it can be read back to compute the debuglink checksum.
801 const path = options.extract_to.?;
802 const dbg_file = std.fs.cwd().createFile(path, .{}) catch |err| {
803 fatal("zig objcopy: unable to create '{s}': {s}", .{ path, @errorName(err) });
804 };
805 defer dbg_file.close();
806
807 try elf_file.emit(allocator, dbg_file, in_file, .{ .section_filter = flt, .compress_debug = options.compress_debug });
808 }
809
810 const debuglink: ?DebugLink = if (debuglink_path) |path| ElfFileHelper.createDebugLink(path) else null;
811 try elf_file.emit(allocator, out_file, in_file, .{
812 .section_filter = filter,
813 .debuglink = debuglink,
814 .compress_debug = options.compress_debug,
815 .add_section = options.add_section,
816 .set_section_alignment = options.set_section_alignment,
817 .set_section_flags = options.set_section_flags,
818 });
819 },
820 }
821}
822
823// note: this is "a minimal effort implementation"
824// It doesn't support all possibile elf files: some sections type may need fixups, the program header may need fix up, ...
825// It was written for a specific use case (strip debug info to a sperate file, for linux 64-bits executables built with `zig` or `zig c++` )
826// It moves and reoders the sections as little as possible to avoid having to do fixups.
827// TODO: support non-native endianess
828
829fn ElfFile(comptime is_64: bool) type {
830 const Elf_Ehdr = if (is_64) elf.Elf64_Ehdr else elf.Elf32_Ehdr;
831 const Elf_Phdr = if (is_64) elf.Elf64_Phdr else elf.Elf32_Phdr;
832 const Elf_Shdr = if (is_64) elf.Elf64_Shdr else elf.Elf32_Shdr;
833 const Elf_Chdr = if (is_64) elf.Elf64_Chdr else elf.Elf32_Chdr;
834 const Elf_Sym = if (is_64) elf.Elf64_Sym else elf.Elf32_Sym;
835 const Elf_OffSize = if (is_64) elf.Elf64_Off else elf.Elf32_Off;
836
837 return struct {
838 raw_elf_header: Elf_Ehdr,
839 program_segments: []const Elf_Phdr,
840 sections: []const Section,
841 arena: std.heap.ArenaAllocator,
842
843 const SectionCategory = ElfFileHelper.SectionCategory;
844 const section_memory_align: std.mem.Alignment = .of(Elf_Sym); // most restrictive of what we may load in memory
845 const Section = struct {
846 section: Elf_Shdr,
847 name: []const u8 = "",
848 segment: ?*const Elf_Phdr = null, // if the section is used by a program segment (there can be more than one)
849 payload: ?[]align(section_memory_align.toByteUnits()) const u8 = null, // if we need the data in memory
850 category: SectionCategory = .none, // should the section be kept in the exe or stripped to the debug database, or both.
851 };
852
853 const Self = @This();
854
855 pub fn parse(gpa: Allocator, in_file: File, header: elf.Header) !Self {
856 var arena = std.heap.ArenaAllocator.init(gpa);
857 errdefer arena.deinit();
858 const allocator = arena.allocator();
859
860 var raw_header: Elf_Ehdr = undefined;
861 {
862 const bytes_read = try in_file.preadAll(std.mem.asBytes(&raw_header), 0);
863 if (bytes_read < @sizeOf(Elf_Ehdr))
864 return error.TRUNCATED_ELF;
865 }
866
867 // program header: list of segments
868 const program_segments = blk: {
869 if (@sizeOf(Elf_Phdr) != header.phentsize)
870 fatal("zig objcopy: unsupported ELF file, unexpected phentsize ({d})", .{header.phentsize});
871
872 const program_header = try allocator.alloc(Elf_Phdr, header.phnum);
873 const bytes_read = try in_file.preadAll(std.mem.sliceAsBytes(program_header), header.phoff);
874 if (bytes_read < @sizeOf(Elf_Phdr) * header.phnum)
875 return error.TRUNCATED_ELF;
876 break :blk program_header;
877 };
878
879 // section header
880 const sections = blk: {
881 if (@sizeOf(Elf_Shdr) != header.shentsize)
882 fatal("zig objcopy: unsupported ELF file, unexpected shentsize ({d})", .{header.shentsize});
883
884 const section_header = try allocator.alloc(Section, header.shnum);
885
886 const raw_section_header = try allocator.alloc(Elf_Shdr, header.shnum);
887 defer allocator.free(raw_section_header);
888 const bytes_read = try in_file.preadAll(std.mem.sliceAsBytes(raw_section_header), header.shoff);
889 if (bytes_read < @sizeOf(Elf_Phdr) * header.shnum)
890 return error.TRUNCATED_ELF;
891
892 for (section_header, raw_section_header) |*section, hdr| {
893 section.* = .{ .section = hdr };
894 }
895 break :blk section_header;
896 };
897
898 // load data to memory for some sections:
899 // string tables for access
900 // sections than need modifications when other sections move.
901 for (sections, 0..) |*section, idx| {
902 const need_data = switch (section.section.sh_type) {
903 elf.DT_VERSYM => true,
904 elf.SHT_SYMTAB, elf.SHT_DYNSYM => true,
905 else => false,
906 };
907 const need_strings = (idx == header.shstrndx);
908
909 if (need_data or need_strings) {
910 const buffer = try allocator.alignedAlloc(u8, section_memory_align, @intCast(section.section.sh_size));
911 const bytes_read = try in_file.preadAll(buffer, section.section.sh_offset);
912 if (bytes_read != section.section.sh_size) return error.TRUNCATED_ELF;
913 section.payload = buffer;
914 }
915 }
916
917 // fill-in sections info:
918 // resolve the name
919 // find if a program segment uses the section
920 // categorize sections usage (used by program segments, debug datadase, common metadata, symbol table)
921 for (sections) |*section| {
922 section.segment = for (program_segments) |*seg| {
923 if (sectionWithinSegment(section.section, seg.*)) break seg;
924 } else null;
925
926 if (section.section.sh_name != 0 and header.shstrndx != elf.SHN_UNDEF)
927 section.name = std.mem.span(@as([*:0]const u8, @ptrCast(&sections[header.shstrndx].payload.?[section.section.sh_name])));
928
929 const category_from_program: SectionCategory = if (section.segment != null) .exe else .debug;
930 section.category = switch (section.section.sh_type) {
931 elf.SHT_NOTE => .common,
932 elf.SHT_SYMTAB => .symbols, // "strip all" vs "strip only debug"
933 elf.SHT_DYNSYM => .exe,
934 elf.SHT_PROGBITS => cat: {
935 if (std.mem.eql(u8, section.name, ".comment")) break :cat .exe;
936 if (std.mem.eql(u8, section.name, ".gnu_debuglink")) break :cat .none;
937 break :cat category_from_program;
938 },
939 elf.SHT_LOPROC...elf.SHT_HIPROC => .common, // don't strip unknown sections
940 elf.SHT_LOUSER...elf.SHT_HIUSER => .common, // don't strip unknown sections
941 else => category_from_program,
942 };
943 }
944
945 sections[0].category = .common; // mandatory null section
946 if (header.shstrndx != elf.SHN_UNDEF)
947 sections[header.shstrndx].category = .common; // string table for the headers
948
949 // recursively propagate section categories to their linked sections, so that they are kept together
950 var dirty: u1 = 1;
951 while (dirty != 0) {
952 dirty = 0;
953
954 for (sections) |*section| {
955 if (section.section.sh_link != elf.SHN_UNDEF)
956 dirty |= ElfFileHelper.propagateCategory(&sections[section.section.sh_link].category, section.category);
957 if ((section.section.sh_flags & elf.SHF_INFO_LINK) != 0 and section.section.sh_info != elf.SHN_UNDEF)
958 dirty |= ElfFileHelper.propagateCategory(&sections[section.section.sh_info].category, section.category);
959 }
960 }
961
962 return Self{
963 .arena = arena,
964 .raw_elf_header = raw_header,
965 .program_segments = program_segments,
966 .sections = sections,
967 };
968 }
969
970 pub fn deinit(self: *Self) void {
971 self.arena.deinit();
972 }
973
974 const Filter = ElfFileHelper.Filter;
975 const DebugLink = ElfFileHelper.DebugLink;
976 const EmitElfOptions = struct {
977 section_filter: Filter = .all,
978 debuglink: ?DebugLink = null,
979 compress_debug: bool = false,
980 add_section: ?AddSection = null,
981 set_section_alignment: ?SetSectionAlignment = null,
982 set_section_flags: ?SetSectionFlags = null,
983 };
984 fn emit(self: *const Self, gpa: Allocator, out_file: File, in_file: File, options: EmitElfOptions) !void {
985 var arena = std.heap.ArenaAllocator.init(gpa);
986 defer arena.deinit();
987 const allocator = arena.allocator();
988
989 // when emitting the stripped exe:
990 // - unused sections are removed
991 // when emitting the debug file:
992 // - all sections are kept, but some are emptied and their types is changed to SHT_NOBITS
993 // the program header is kept unchanged. (`strip` does update it, but `eu-strip` does not, and it still works)
994
995 const Update = struct {
996 action: ElfFileHelper.Action,
997
998 // remap the indexs after omitting the filtered sections
999 remap_idx: u16,
1000
1001 // optionally overrides the payload from the source file
1002 payload: ?[]align(section_memory_align.toByteUnits()) const u8 = null,
1003 section: ?Elf_Shdr = null,
1004 };
1005 const sections_update = try allocator.alloc(Update, self.sections.len);
1006 const new_shnum = blk: {
1007 var next_idx: u16 = 0;
1008 for (self.sections, sections_update) |section, *update| {
1009 const action = ElfFileHelper.selectAction(section.category, options.section_filter);
1010 const remap_idx = idx: {
1011 if (action == .strip) break :idx elf.SHN_UNDEF;
1012 next_idx += 1;
1013 break :idx next_idx - 1;
1014 };
1015 update.* = Update{ .action = action, .remap_idx = remap_idx };
1016 }
1017
1018 if (options.debuglink != null)
1019 next_idx += 1;
1020
1021 if (options.add_section != null) {
1022 next_idx += 1;
1023 }
1024
1025 break :blk next_idx;
1026 };
1027
1028 // add a ".gnu_debuglink" to the string table if needed
1029 const debuglink_name: u32 = blk: {
1030 if (options.debuglink == null) break :blk elf.SHN_UNDEF;
1031 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1032 fatal("zig objcopy: no strtab, cannot add the debuglink section", .{}); // TODO add the section if needed?
1033
1034 const strtab = &self.sections[self.raw_elf_header.e_shstrndx];
1035 const update = &sections_update[self.raw_elf_header.e_shstrndx];
1036
1037 const name: []const u8 = ".gnu_debuglink";
1038 const new_offset: u32 = @intCast(strtab.payload.?.len);
1039 const buf = try allocator.alignedAlloc(u8, section_memory_align, new_offset + name.len + 1);
1040 @memcpy(buf[0..new_offset], strtab.payload.?);
1041 @memcpy(buf[new_offset..][0..name.len], name);
1042 buf[new_offset + name.len] = 0;
1043
1044 assert(update.action == .keep);
1045 update.payload = buf;
1046
1047 break :blk new_offset;
1048 };
1049
1050 // add user section to the string table if needed
1051 const user_section_name: u32 = blk: {
1052 if (options.add_section == null) break :blk elf.SHN_UNDEF;
1053 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1054 fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed?
1055
1056 const strtab = &self.sections[self.raw_elf_header.e_shstrndx];
1057 const update = &sections_update[self.raw_elf_header.e_shstrndx];
1058
1059 const name = options.add_section.?.section_name;
1060 const new_offset: u32 = @intCast(strtab.payload.?.len);
1061 const buf = try allocator.alignedAlloc(u8, section_memory_align, new_offset + name.len + 1);
1062 @memcpy(buf[0..new_offset], strtab.payload.?);
1063 @memcpy(buf[new_offset..][0..name.len], name);
1064 buf[new_offset + name.len] = 0;
1065
1066 assert(update.action == .keep);
1067 update.payload = buf;
1068
1069 break :blk new_offset;
1070 };
1071
1072 // maybe compress .debug sections
1073 if (options.compress_debug) {
1074 for (self.sections[1..], sections_update[1..]) |section, *update| {
1075 if (update.action != .keep) continue;
1076 if (!std.mem.startsWith(u8, section.name, ".debug_")) continue;
1077 if ((section.section.sh_flags & elf.SHF_COMPRESSED) != 0) continue; // already compressed
1078
1079 const chdr = Elf_Chdr{
1080 .ch_type = elf.COMPRESS.ZLIB,
1081 .ch_size = section.section.sh_size,
1082 .ch_addralign = section.section.sh_addralign,
1083 };
1084
1085 const compressed_payload = try ElfFileHelper.tryCompressSection(allocator, in_file, section.section.sh_offset, section.section.sh_size, std.mem.asBytes(&chdr));
1086 if (compressed_payload) |payload| {
1087 update.payload = payload;
1088 update.section = section.section;
1089 update.section.?.sh_addralign = @alignOf(Elf_Chdr);
1090 update.section.?.sh_size = @intCast(payload.len);
1091 update.section.?.sh_flags |= elf.SHF_COMPRESSED;
1092 }
1093 }
1094 }
1095
1096 var cmdbuf = std.ArrayList(ElfFileHelper.WriteCmd).init(allocator);
1097 defer cmdbuf.deinit();
1098 try cmdbuf.ensureUnusedCapacity(3 + new_shnum);
1099 var eof_offset: Elf_OffSize = 0; // track the end of the data written so far.
1100
1101 // build the updated headers
1102 // nb: updated_elf_header will be updated before the actual write
1103 var updated_elf_header = self.raw_elf_header;
1104 if (updated_elf_header.e_shstrndx != elf.SHN_UNDEF)
1105 updated_elf_header.e_shstrndx = sections_update[updated_elf_header.e_shstrndx].remap_idx;
1106 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = std.mem.asBytes(&updated_elf_header), .out_offset = 0 } });
1107 eof_offset = @sizeOf(Elf_Ehdr);
1108
1109 // program header as-is.
1110 // nb: for only-debug files, removing it appears to work, but is invalid by ELF specifcation.
1111 {
1112 assert(updated_elf_header.e_phoff == @sizeOf(Elf_Ehdr));
1113 const data = std.mem.sliceAsBytes(self.program_segments);
1114 assert(data.len == @as(usize, updated_elf_header.e_phentsize) * updated_elf_header.e_phnum);
1115 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_phoff } });
1116 eof_offset = updated_elf_header.e_phoff + @as(Elf_OffSize, @intCast(data.len));
1117 }
1118
1119 // update sections and queue payload writes
1120 const updated_section_header = blk: {
1121 const dest_sections = try allocator.alloc(Elf_Shdr, new_shnum);
1122
1123 {
1124 // the ELF format doesn't specify the order for all sections.
1125 // this code only supports when they are in increasing file order.
1126 var offset: u64 = eof_offset;
1127 for (self.sections[1..]) |section| {
1128 if (section.section.sh_type == elf.SHT_NOBITS)
1129 continue;
1130 if (section.section.sh_offset < offset) {
1131 fatal("zig objcopy: unsupported ELF file", .{});
1132 }
1133 offset = section.section.sh_offset;
1134 }
1135 }
1136
1137 dest_sections[0] = self.sections[0].section;
1138
1139 var dest_section_idx: u32 = 1;
1140 for (self.sections[1..], sections_update[1..]) |section, update| {
1141 if (update.action == .strip) continue;
1142 assert(update.remap_idx == dest_section_idx);
1143
1144 const src = if (update.section) |*s| s else &section.section;
1145 const dest = &dest_sections[dest_section_idx];
1146 const payload = if (update.payload) |data| data else section.payload;
1147 dest_section_idx += 1;
1148
1149 dest.* = src.*;
1150
1151 if (src.sh_link != elf.SHN_UNDEF)
1152 dest.sh_link = sections_update[src.sh_link].remap_idx;
1153 if ((src.sh_flags & elf.SHF_INFO_LINK) != 0 and src.sh_info != elf.SHN_UNDEF)
1154 dest.sh_info = sections_update[src.sh_info].remap_idx;
1155
1156 if (payload) |data|
1157 dest.sh_size = @intCast(data.len);
1158
1159 const addralign = if (src.sh_addralign == 0 or dest.sh_type == elf.SHT_NOBITS) 1 else src.sh_addralign;
1160 dest.sh_offset = std.mem.alignForward(Elf_OffSize, eof_offset, addralign);
1161 if (src.sh_offset != dest.sh_offset and section.segment != null and update.action != .empty and dest.sh_type != elf.SHT_NOTE and dest.sh_type != elf.SHT_NOBITS) {
1162 if (src.sh_offset > dest.sh_offset) {
1163 dest.sh_offset = src.sh_offset; // add padding to avoid modifing the program segments
1164 } else {
1165 fatal("zig objcopy: cannot adjust program segments", .{});
1166 }
1167 }
1168 assert(dest.sh_addr % addralign == dest.sh_offset % addralign);
1169
1170 if (update.action == .empty)
1171 dest.sh_type = elf.SHT_NOBITS;
1172
1173 if (dest.sh_type != elf.SHT_NOBITS) {
1174 if (payload) |src_data| {
1175 // update sections payload and write
1176 const dest_data = switch (src.sh_type) {
1177 elf.DT_VERSYM => dst_data: {
1178 const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len);
1179 @memcpy(data, src_data);
1180
1181 const defs = @as([*]elf.Verdef, @ptrCast(data))[0 .. @as(usize, @intCast(src.sh_size)) / @sizeOf(elf.Verdef)];
1182 for (defs) |*def| switch (def.ndx) {
1183 .LOCAL, .GLOBAL => {},
1184 else => def.ndx = @enumFromInt(sections_update[src.sh_info].remap_idx),
1185 };
1186
1187 break :dst_data data;
1188 },
1189 elf.SHT_SYMTAB, elf.SHT_DYNSYM => dst_data: {
1190 const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len);
1191 @memcpy(data, src_data);
1192
1193 const syms = @as([*]Elf_Sym, @ptrCast(data))[0 .. @as(usize, @intCast(src.sh_size)) / @sizeOf(Elf_Sym)];
1194 for (syms) |*sym| {
1195 if (sym.st_shndx != elf.SHN_UNDEF and sym.st_shndx < elf.SHN_LORESERVE)
1196 sym.st_shndx = sections_update[sym.st_shndx].remap_idx;
1197 }
1198
1199 break :dst_data data;
1200 },
1201 else => src_data,
1202 };
1203
1204 assert(dest_data.len == dest.sh_size);
1205 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = dest_data, .out_offset = dest.sh_offset } });
1206 eof_offset = dest.sh_offset + dest.sh_size;
1207 } else {
1208 // direct contents copy
1209 cmdbuf.appendAssumeCapacity(.{ .copy_range = .{ .in_offset = src.sh_offset, .len = dest.sh_size, .out_offset = dest.sh_offset } });
1210 eof_offset = dest.sh_offset + dest.sh_size;
1211 }
1212 } else {
1213 // account for alignment padding even in empty sections to keep logical section order
1214 eof_offset = dest.sh_offset;
1215 }
1216 }
1217
1218 // add a ".gnu_debuglink" section
1219 if (options.debuglink) |link| {
1220 const payload = payload: {
1221 const crc_offset = std.mem.alignForward(usize, link.name.len + 1, 4);
1222 const buf = try allocator.alignedAlloc(u8, .@"4", crc_offset + 4);
1223 @memcpy(buf[0..link.name.len], link.name);
1224 @memset(buf[link.name.len..crc_offset], 0);
1225 @memcpy(buf[crc_offset..], std.mem.asBytes(&link.crc32));
1226 break :payload buf;
1227 };
1228
1229 dest_sections[dest_section_idx] = Elf_Shdr{
1230 .sh_name = debuglink_name,
1231 .sh_type = elf.SHT_PROGBITS,
1232 .sh_flags = 0,
1233 .sh_addr = 0,
1234 .sh_offset = eof_offset,
1235 .sh_size = @intCast(payload.len),
1236 .sh_link = elf.SHN_UNDEF,
1237 .sh_info = elf.SHN_UNDEF,
1238 .sh_addralign = 4,
1239 .sh_entsize = 0,
1240 };
1241 dest_section_idx += 1;
1242
1243 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = payload, .out_offset = eof_offset } });
1244 eof_offset += @as(Elf_OffSize, @intCast(payload.len));
1245 }
1246
1247 // --add-section
1248 if (options.add_section) |add_section| {
1249 var section_file = fs.cwd().openFile(add_section.file_path, .{}) catch |err|
1250 fatal("unable to open '{s}': {s}", .{ add_section.file_path, @errorName(err) });
1251 defer section_file.close();
1252
1253 const payload = try section_file.readToEndAlloc(arena.allocator(), std.math.maxInt(usize));
1254
1255 dest_sections[dest_section_idx] = Elf_Shdr{
1256 .sh_name = user_section_name,
1257 .sh_type = elf.SHT_PROGBITS,
1258 .sh_flags = 0,
1259 .sh_addr = 0,
1260 .sh_offset = eof_offset,
1261 .sh_size = @intCast(payload.len),
1262 .sh_link = elf.SHN_UNDEF,
1263 .sh_info = elf.SHN_UNDEF,
1264 .sh_addralign = 4,
1265 .sh_entsize = 0,
1266 };
1267 dest_section_idx += 1;
1268
1269 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = payload, .out_offset = eof_offset } });
1270 eof_offset += @as(Elf_OffSize, @intCast(payload.len));
1271 }
1272
1273 assert(dest_section_idx == new_shnum);
1274 break :blk dest_sections;
1275 };
1276
1277 // --set-section-alignment: overwrite alignment
1278 if (options.set_section_alignment) |set_align| {
1279 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1280 fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed?
1281
1282 const strtab = &sections_update[self.raw_elf_header.e_shstrndx];
1283 for (updated_section_header) |*section| {
1284 const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name])));
1285 if (std.mem.eql(u8, section_name, set_align.section_name)) {
1286 section.sh_addralign = set_align.alignment;
1287 break;
1288 }
1289 } else std.log.warn("Skipping --set-section-alignment. Section '{s}' not found", .{set_align.section_name});
1290 }
1291
1292 // --set-section-flags: overwrite flags
1293 if (options.set_section_flags) |set_flags| {
1294 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1295 fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed?
1296
1297 const strtab = &sections_update[self.raw_elf_header.e_shstrndx];
1298 for (updated_section_header) |*section| {
1299 const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name])));
1300 if (std.mem.eql(u8, section_name, set_flags.section_name)) {
1301 section.sh_flags = std.elf.SHF_WRITE; // default is writable cleared by "readonly"
1302 const f = set_flags.flags;
1303
1304 // Supporting a subset of GNU and LLVM objcopy for ELF only
1305 // GNU:
1306 // alloc: add SHF_ALLOC
1307 // contents: if section is SHT_NOBITS, set SHT_PROGBITS, otherwise do nothing
1308 // load: if section is SHT_NOBITS, set SHT_PROGBITS, otherwise do nothing (same as contents)
1309 // noload: not ELF relevant
1310 // readonly: clear default SHF_WRITE flag
1311 // code: add SHF_EXECINSTR
1312 // data: not ELF relevant
1313 // rom: ignored
1314 // exclude: add SHF_EXCLUDE
1315 // share: not ELF relevant
1316 // debug: not ELF relevant
1317 // large: add SHF_X86_64_LARGE. Fatal error if target is not x86_64
1318 if (f.alloc) section.sh_flags |= std.elf.SHF_ALLOC;
1319 if (f.contents or f.load) {
1320 if (section.sh_type == std.elf.SHT_NOBITS) section.sh_type = std.elf.SHT_PROGBITS;
1321 }
1322 if (f.readonly) section.sh_flags &= ~@as(@TypeOf(section.sh_type), std.elf.SHF_WRITE);
1323 if (f.code) section.sh_flags |= std.elf.SHF_EXECINSTR;
1324 if (f.exclude) section.sh_flags |= std.elf.SHF_EXCLUDE;
1325 if (f.large) {
1326 if (updated_elf_header.e_machine != std.elf.EM.X86_64)
1327 fatal("zig objcopy: 'large' section flag is only supported on x86_64 targets", .{});
1328 section.sh_flags |= std.elf.SHF_X86_64_LARGE;
1329 }
1330
1331 // LLVM:
1332 // merge: add SHF_MERGE
1333 // strings: add SHF_STRINGS
1334 if (f.merge) section.sh_flags |= std.elf.SHF_MERGE;
1335 if (f.strings) section.sh_flags |= std.elf.SHF_STRINGS;
1336 break;
1337 }
1338 } else std.log.warn("Skipping --set-section-flags. Section '{s}' not found", .{set_flags.section_name});
1339 }
1340
1341 // write the section header at the tail
1342 {
1343 const offset = std.mem.alignForward(Elf_OffSize, eof_offset, @alignOf(Elf_Shdr));
1344
1345 const data = std.mem.sliceAsBytes(updated_section_header);
1346 assert(data.len == @as(usize, updated_elf_header.e_shentsize) * new_shnum);
1347 updated_elf_header.e_shoff = offset;
1348 updated_elf_header.e_shnum = new_shnum;
1349
1350 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_shoff } });
1351 }
1352
1353 try ElfFileHelper.write(allocator, out_file, in_file, cmdbuf.items);
1354 }
1355
1356 fn sectionWithinSegment(section: Elf_Shdr, segment: Elf_Phdr) bool {
1357 const file_size = if (section.sh_type == elf.SHT_NOBITS) 0 else section.sh_size;
1358 return segment.p_offset <= section.sh_offset and (segment.p_offset + segment.p_filesz) >= (section.sh_offset + file_size);
1359 }
1360 };
1361}
1362
1363const ElfFileHelper = struct {
1364 const DebugLink = struct { name: []const u8, crc32: u32 };
1365 const Filter = enum { all, program, debug, program_and_symbols, debug_and_symbols };
1366
1367 const SectionCategory = enum { common, exe, debug, symbols, none };
1368 fn propagateCategory(cur: *SectionCategory, new: SectionCategory) u1 {
1369 const cat: SectionCategory = switch (cur.*) {
1370 .none => new,
1371 .common => .common,
1372 .debug => switch (new) {
1373 .none, .debug => .debug,
1374 else => new,
1375 },
1376 .exe => switch (new) {
1377 .common => .common,
1378 .none, .debug, .exe => .exe,
1379 .symbols => .exe,
1380 },
1381 .symbols => switch (new) {
1382 .none, .common, .debug, .exe => unreachable,
1383 .symbols => .symbols,
1384 },
1385 };
1386
1387 if (cur.* != cat) {
1388 cur.* = cat;
1389 return 1;
1390 } else {
1391 return 0;
1392 }
1393 }
1394
1395 const Action = enum { keep, strip, empty };
1396 fn selectAction(category: SectionCategory, filter: Filter) Action {
1397 if (category == .none) return .strip;
1398 return switch (filter) {
1399 .all => switch (category) {
1400 .none => .strip,
1401 else => .keep,
1402 },
1403 .program => switch (category) {
1404 .common, .exe => .keep,
1405 else => .strip,
1406 },
1407 .program_and_symbols => switch (category) {
1408 .common, .exe, .symbols => .keep,
1409 else => .strip,
1410 },
1411 .debug => switch (category) {
1412 .exe, .symbols => .empty,
1413 .none => .strip,
1414 else => .keep,
1415 },
1416 .debug_and_symbols => switch (category) {
1417 .exe => .empty,
1418 .none => .strip,
1419 else => .keep,
1420 },
1421 };
1422 }
1423
1424 const WriteCmd = union(enum) {
1425 copy_range: struct { in_offset: u64, len: u64, out_offset: u64 },
1426 write_data: struct { data: []const u8, out_offset: u64 },
1427 };
1428 fn write(allocator: Allocator, out_file: File, in_file: File, cmds: []const WriteCmd) !void {
1429 // consolidate holes between writes:
1430 // by coping original padding data from in_file (by fusing contiguous ranges)
1431 // by writing zeroes otherwise
1432 const zeroes = [1]u8{0} ** 4096;
1433 var consolidated = std.ArrayList(WriteCmd).init(allocator);
1434 defer consolidated.deinit();
1435 try consolidated.ensureUnusedCapacity(cmds.len * 2);
1436 var offset: u64 = 0;
1437 var fused_cmd: ?WriteCmd = null;
1438 for (cmds) |cmd| {
1439 switch (cmd) {
1440 .write_data => |data| {
1441 assert(data.out_offset >= offset);
1442 if (fused_cmd) |prev| {
1443 consolidated.appendAssumeCapacity(prev);
1444 fused_cmd = null;
1445 }
1446 if (data.out_offset > offset) {
1447 consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(data.out_offset - offset)], .out_offset = offset } });
1448 }
1449 consolidated.appendAssumeCapacity(cmd);
1450 offset = data.out_offset + data.data.len;
1451 },
1452 .copy_range => |range| {
1453 assert(range.out_offset >= offset);
1454 if (fused_cmd) |prev| {
1455 if (range.in_offset >= prev.copy_range.in_offset + prev.copy_range.len and (range.out_offset - prev.copy_range.out_offset == range.in_offset - prev.copy_range.in_offset)) {
1456 fused_cmd = .{ .copy_range = .{
1457 .in_offset = prev.copy_range.in_offset,
1458 .out_offset = prev.copy_range.out_offset,
1459 .len = (range.out_offset + range.len) - prev.copy_range.out_offset,
1460 } };
1461 } else {
1462 consolidated.appendAssumeCapacity(prev);
1463 if (range.out_offset > offset) {
1464 consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(range.out_offset - offset)], .out_offset = offset } });
1465 }
1466 fused_cmd = cmd;
1467 }
1468 } else {
1469 fused_cmd = cmd;
1470 }
1471 offset = range.out_offset + range.len;
1472 },
1473 }
1474 }
1475 if (fused_cmd) |cmd| {
1476 consolidated.appendAssumeCapacity(cmd);
1477 }
1478
1479 // write the output file
1480 for (consolidated.items) |cmd| {
1481 switch (cmd) {
1482 .write_data => |data| {
1483 var iovec = [_]std.posix.iovec_const{.{ .base = data.data.ptr, .len = data.data.len }};
1484 try out_file.pwritevAll(&iovec, data.out_offset);
1485 },
1486 .copy_range => |range| {
1487 const copied_bytes = try in_file.copyRangeAll(range.in_offset, out_file, range.out_offset, range.len);
1488 if (copied_bytes < range.len) return error.TRUNCATED_ELF;
1489 },
1490 }
1491 }
1492 }
1493
1494 fn tryCompressSection(allocator: Allocator, in_file: File, offset: u64, size: u64, prefix: []const u8) !?[]align(8) const u8 {
1495 if (size < prefix.len) return null;
1496
1497 try in_file.seekTo(offset);
1498 var section_reader = std.io.limitedReader(in_file.deprecatedReader(), size);
1499
1500 // allocate as large as decompressed data. if the compression doesn't fit, keep the data uncompressed.
1501 const compressed_data = try allocator.alignedAlloc(u8, .@"8", @intCast(size));
1502 var compressed_stream = std.io.fixedBufferStream(compressed_data);
1503
1504 try compressed_stream.writer().writeAll(prefix);
1505
1506 {
1507 var compressor = try std.compress.zlib.compressor(compressed_stream.writer(), .{});
1508
1509 var buf: [8000]u8 = undefined;
1510 while (true) {
1511 const bytes_read = try section_reader.read(&buf);
1512 if (bytes_read == 0) break;
1513 const bytes_written = compressor.write(buf[0..bytes_read]) catch |err| switch (err) {
1514 error.NoSpaceLeft => {
1515 allocator.free(compressed_data);
1516 return null;
1517 },
1518 else => return err,
1519 };
1520 std.debug.assert(bytes_written == bytes_read);
1521 }
1522 compressor.finish() catch |err| switch (err) {
1523 error.NoSpaceLeft => {
1524 allocator.free(compressed_data);
1525 return null;
1526 },
1527 else => return err,
1528 };
1529 }
1530
1531 const compressed_len: usize = @intCast(compressed_stream.getPos() catch unreachable);
1532 const data = allocator.realloc(compressed_data, compressed_len) catch compressed_data;
1533 return data[0..compressed_len];
1534 }
1535
1536 fn createDebugLink(path: []const u8) DebugLink {
1537 const file = std.fs.cwd().openFile(path, .{}) catch |err| {
1538 fatal("zig objcopy: could not open `{s}`: {s}\n", .{ path, @errorName(err) });
1539 };
1540 defer file.close();
1541
1542 const crc = ElfFileHelper.computeFileCrc(file) catch |err| {
1543 fatal("zig objcopy: could not read `{s}`: {s}\n", .{ path, @errorName(err) });
1544 };
1545 return .{
1546 .name = std.fs.path.basename(path),
1547 .crc32 = crc,
1548 };
1549 }
1550
1551 fn computeFileCrc(file: File) !u32 {
1552 var buf: [8000]u8 = undefined;
1553
1554 try file.seekTo(0);
1555 var hasher = std.hash.Crc32.init();
1556 while (true) {
1557 const bytes_read = try file.read(&buf);
1558 if (bytes_read == 0) break;
1559 hasher.update(buf[0..bytes_read]);
1560 }
1561 return hasher.final();
1562 }
1563};
1564
1565720const SectionFlags = packed struct {
1566721 alloc: bool = false,
1567722 contents: bool = false,
lib/std/elf.zig+102-171
......@@ -482,6 +482,7 @@ pub const Header = struct {
482482 is_64: bool,
483483 endian: std.builtin.Endian,
484484 os_abi: OSABI,
485 /// The meaning of this value depends on `os_abi`.
485486 abi_version: u8,
486487 type: ET,
487488 machine: EM,
......@@ -494,205 +495,135 @@ pub const Header = struct {
494495 shnum: u16,
495496 shstrndx: u16,
496497
497 pub fn program_header_iterator(self: Header, parse_source: anytype) ProgramHeaderIterator(@TypeOf(parse_source)) {
498 return ProgramHeaderIterator(@TypeOf(parse_source)){
499 .elf_header = self,
500 .parse_source = parse_source,
498 pub fn iterateProgramHeaders(h: Header, file_reader: *std.fs.File.Reader) ProgramHeaderIterator {
499 return .{
500 .elf_header = h,
501 .file_reader = file_reader,
501502 };
502503 }
503504
504 pub fn section_header_iterator(self: Header, parse_source: anytype) SectionHeaderIterator(@TypeOf(parse_source)) {
505 return SectionHeaderIterator(@TypeOf(parse_source)){
506 .elf_header = self,
507 .parse_source = parse_source,
505 pub fn iterateSectionHeaders(h: Header, file_reader: *std.fs.File.Reader) SectionHeaderIterator {
506 return .{
507 .elf_header = h,
508 .file_reader = file_reader,
508509 };
509510 }
510511
511 pub fn read(parse_source: anytype) !Header {
512 var hdr_buf: [@sizeOf(Elf64_Ehdr)]u8 align(@alignOf(Elf64_Ehdr)) = undefined;
513 try parse_source.seekableStream().seekTo(0);
514 try parse_source.deprecatedReader().readNoEof(&hdr_buf);
515 return Header.parse(&hdr_buf);
516 }
512 pub const ReadError = std.Io.Reader.Error || error{
513 InvalidElfMagic,
514 InvalidElfVersion,
515 InvalidElfClass,
516 InvalidElfEndian,
517 };
517518
518 pub fn parse(hdr_buf: *align(@alignOf(Elf64_Ehdr)) const [@sizeOf(Elf64_Ehdr)]u8) !Header {
519 const hdr32 = @as(*const Elf32_Ehdr, @ptrCast(hdr_buf));
520 const hdr64 = @as(*const Elf64_Ehdr, @ptrCast(hdr_buf));
521 if (!mem.eql(u8, hdr32.e_ident[0..4], MAGIC)) return error.InvalidElfMagic;
522 if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion;
519 pub fn read(r: *std.Io.Reader) ReadError!Header {
520 const buf = try r.peek(@sizeOf(Elf64_Ehdr));
523521
524 const is_64 = switch (hdr32.e_ident[EI_CLASS]) {
525 ELFCLASS32 => false,
526 ELFCLASS64 => true,
527 else => return error.InvalidElfClass,
528 };
522 if (!mem.eql(u8, buf[0..4], MAGIC)) return error.InvalidElfMagic;
523 if (buf[EI_VERSION] != 1) return error.InvalidElfVersion;
529524
530 const endian: std.builtin.Endian = switch (hdr32.e_ident[EI_DATA]) {
525 const endian: std.builtin.Endian = switch (buf[EI_DATA]) {
531526 ELFDATA2LSB => .little,
532527 ELFDATA2MSB => .big,
533528 else => return error.InvalidElfEndian,
534529 };
535 const need_bswap = endian != native_endian;
536530
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 {
537539 // Converting integers to exhaustive enums using `@enumFromInt` could cause a panic.
538540 comptime assert(!@typeInfo(OSABI).@"enum".is_exhaustive);
539 const os_abi: OSABI = @enumFromInt(hdr32.e_ident[EI_OSABI]);
541 return .{
542 .is_64 = switch (@TypeOf(hdr)) {
543 Elf32_Ehdr => false,
544 Elf64_Ehdr => true,
545 else => @compileError("bad type"),
546 },
547 .endian = endian,
548 .os_abi = @enumFromInt(hdr.e_ident[EI_OSABI]),
549 .abi_version = hdr.e_ident[EI_ABIVERSION],
550 .type = hdr.e_type,
551 .machine = hdr.e_machine,
552 .entry = hdr.e_entry,
553 .phoff = hdr.e_phoff,
554 .shoff = hdr.e_shoff,
555 .phentsize = hdr.e_phentsize,
556 .phnum = hdr.e_phnum,
557 .shentsize = hdr.e_shentsize,
558 .shnum = hdr.e_shnum,
559 .shstrndx = hdr.e_shstrndx,
560 };
561 }
562};
540563
541 // The meaning of this value depends on `os_abi` so just make it available as `u8`.
542 const abi_version = hdr32.e_ident[EI_ABIVERSION];
564pub const ProgramHeaderIterator = struct {
565 elf_header: Header,
566 file_reader: *std.fs.File.Reader,
567 index: usize = 0,
543568
544 const @"type" = if (need_bswap) blk: {
545 comptime assert(!@typeInfo(ET).@"enum".is_exhaustive);
546 const value = @intFromEnum(hdr32.e_type);
547 break :blk @as(ET, @enumFromInt(@byteSwap(value)));
548 } else hdr32.e_type;
569 pub fn next(it: *ProgramHeaderIterator) !?Elf64_Phdr {
570 if (it.index >= it.elf_header.phnum) return null;
571 defer it.index += 1;
549572
550 const machine = if (need_bswap) blk: {
551 comptime assert(!@typeInfo(EM).@"enum".is_exhaustive);
552 const value = @intFromEnum(hdr32.e_machine);
553 break :blk @as(EM, @enumFromInt(@byteSwap(value)));
554 } else hdr32.e_machine;
573 if (it.elf_header.is_64) {
574 const offset = it.elf_header.phoff + @sizeOf(Elf64_Phdr) * it.index;
575 try it.file_reader.seekTo(offset);
576 const phdr = try it.file_reader.interface.takeStruct(Elf64_Phdr, it.elf_header.endian);
577 return phdr;
578 }
555579
556 return @as(Header, .{
557 .is_64 = is_64,
558 .endian = endian,
559 .os_abi = os_abi,
560 .abi_version = abi_version,
561 .type = @"type",
562 .machine = machine,
563 .entry = int(is_64, need_bswap, hdr32.e_entry, hdr64.e_entry),
564 .phoff = int(is_64, need_bswap, hdr32.e_phoff, hdr64.e_phoff),
565 .shoff = int(is_64, need_bswap, hdr32.e_shoff, hdr64.e_shoff),
566 .phentsize = int(is_64, need_bswap, hdr32.e_phentsize, hdr64.e_phentsize),
567 .phnum = int(is_64, need_bswap, hdr32.e_phnum, hdr64.e_phnum),
568 .shentsize = int(is_64, need_bswap, hdr32.e_shentsize, hdr64.e_shentsize),
569 .shnum = int(is_64, need_bswap, hdr32.e_shnum, hdr64.e_shnum),
570 .shstrndx = int(is_64, need_bswap, hdr32.e_shstrndx, hdr64.e_shstrndx),
571 });
580 const offset = it.elf_header.phoff + @sizeOf(Elf32_Phdr) * it.index;
581 try it.file_reader.seekTo(offset);
582 const phdr = try it.file_reader.interface.takeStruct(Elf32_Phdr, it.elf_header.endian);
583 return .{
584 .p_type = phdr.p_type,
585 .p_offset = phdr.p_offset,
586 .p_vaddr = phdr.p_vaddr,
587 .p_paddr = phdr.p_paddr,
588 .p_filesz = phdr.p_filesz,
589 .p_memsz = phdr.p_memsz,
590 .p_flags = phdr.p_flags,
591 .p_align = phdr.p_align,
592 };
572593 }
573594};
574595
575pub fn ProgramHeaderIterator(comptime ParseSource: anytype) type {
576 return struct {
577 elf_header: Header,
578 parse_source: ParseSource,
579 index: usize = 0,
580
581 pub fn next(self: *@This()) !?Elf64_Phdr {
582 if (self.index >= self.elf_header.phnum) return null;
583 defer self.index += 1;
584
585 if (self.elf_header.is_64) {
586 var phdr: Elf64_Phdr = undefined;
587 const offset = self.elf_header.phoff + @sizeOf(@TypeOf(phdr)) * self.index;
588 try self.parse_source.seekableStream().seekTo(offset);
589 try self.parse_source.deprecatedReader().readNoEof(mem.asBytes(&phdr));
590
591 // ELF endianness matches native endianness.
592 if (self.elf_header.endian == native_endian) return phdr;
593
594 // Convert fields to native endianness.
595 mem.byteSwapAllFields(Elf64_Phdr, &phdr);
596 return phdr;
597 }
598
599 var phdr: Elf32_Phdr = undefined;
600 const offset = self.elf_header.phoff + @sizeOf(@TypeOf(phdr)) * self.index;
601 try self.parse_source.seekableStream().seekTo(offset);
602 try self.parse_source.deprecatedReader().readNoEof(mem.asBytes(&phdr));
603
604 // ELF endianness does NOT match native endianness.
605 if (self.elf_header.endian != native_endian) {
606 // Convert fields to native endianness.
607 mem.byteSwapAllFields(Elf32_Phdr, &phdr);
608 }
609
610 // Convert 32-bit header to 64-bit.
611 return Elf64_Phdr{
612 .p_type = phdr.p_type,
613 .p_offset = phdr.p_offset,
614 .p_vaddr = phdr.p_vaddr,
615 .p_paddr = phdr.p_paddr,
616 .p_filesz = phdr.p_filesz,
617 .p_memsz = phdr.p_memsz,
618 .p_flags = phdr.p_flags,
619 .p_align = phdr.p_align,
620 };
621 }
622 };
623}
596pub const SectionHeaderIterator = struct {
597 elf_header: Header,
598 file_reader: *std.fs.File.Reader,
599 index: usize = 0,
624600
625pub fn SectionHeaderIterator(comptime ParseSource: anytype) type {
626 return struct {
627 elf_header: Header,
628 parse_source: ParseSource,
629 index: usize = 0,
630
631 pub fn next(self: *@This()) !?Elf64_Shdr {
632 if (self.index >= self.elf_header.shnum) return null;
633 defer self.index += 1;
634
635 if (self.elf_header.is_64) {
636 var shdr: Elf64_Shdr = undefined;
637 const offset = self.elf_header.shoff + @sizeOf(@TypeOf(shdr)) * self.index;
638 try self.parse_source.seekableStream().seekTo(offset);
639 try self.parse_source.deprecatedReader().readNoEof(mem.asBytes(&shdr));
640
641 // ELF endianness matches native endianness.
642 if (self.elf_header.endian == native_endian) return shdr;
643
644 // Convert fields to native endianness.
645 mem.byteSwapAllFields(Elf64_Shdr, &shdr);
646 return shdr;
647 }
648
649 var shdr: Elf32_Shdr = undefined;
650 const offset = self.elf_header.shoff + @sizeOf(@TypeOf(shdr)) * self.index;
651 try self.parse_source.seekableStream().seekTo(offset);
652 try self.parse_source.deprecatedReader().readNoEof(mem.asBytes(&shdr));
653
654 // ELF endianness does NOT match native endianness.
655 if (self.elf_header.endian != native_endian) {
656 // Convert fields to native endianness.
657 mem.byteSwapAllFields(Elf32_Shdr, &shdr);
658 }
659
660 // Convert 32-bit header to 64-bit.
661 return Elf64_Shdr{
662 .sh_name = shdr.sh_name,
663 .sh_type = shdr.sh_type,
664 .sh_flags = shdr.sh_flags,
665 .sh_addr = shdr.sh_addr,
666 .sh_offset = shdr.sh_offset,
667 .sh_size = shdr.sh_size,
668 .sh_link = shdr.sh_link,
669 .sh_info = shdr.sh_info,
670 .sh_addralign = shdr.sh_addralign,
671 .sh_entsize = shdr.sh_entsize,
672 };
673 }
674 };
675}
601 pub fn next(it: *SectionHeaderIterator) !?Elf64_Shdr {
602 if (it.index >= it.elf_header.shnum) return null;
603 defer it.index += 1;
676604
677fn int(is_64: bool, need_bswap: bool, int_32: anytype, int_64: anytype) @TypeOf(int_64) {
678 if (is_64) {
679 if (need_bswap) {
680 return @byteSwap(int_64);
681 } else {
682 return int_64;
605 if (it.elf_header.is_64) {
606 try it.file_reader.seekTo(it.elf_header.shoff + @sizeOf(Elf64_Shdr) * it.index);
607 const shdr = try it.file_reader.interface.takeStruct(Elf64_Shdr, it.elf_header.endian);
608 return shdr;
683609 }
684 } else {
685 return int32(need_bswap, int_32, @TypeOf(int_64));
686 }
687}
688610
689fn int32(need_bswap: bool, int_32: anytype, comptime Int64: anytype) Int64 {
690 if (need_bswap) {
691 return @byteSwap(int_32);
692 } else {
693 return int_32;
611 try it.file_reader.seekTo(it.elf_header.shoff + @sizeOf(Elf32_Shdr) * it.index);
612 const shdr = try it.file_reader.interface.takeStruct(Elf32_Shdr, it.elf_header.endian);
613 return .{
614 .sh_name = shdr.sh_name,
615 .sh_type = shdr.sh_type,
616 .sh_flags = shdr.sh_flags,
617 .sh_addr = shdr.sh_addr,
618 .sh_offset = shdr.sh_offset,
619 .sh_size = shdr.sh_size,
620 .sh_link = shdr.sh_link,
621 .sh_info = shdr.sh_info,
622 .sh_addralign = shdr.sh_addralign,
623 .sh_entsize = shdr.sh_entsize,
624 };
694625 }
695}
626};
696627
697628pub const ELFCLASSNONE = 0;
698629pub const ELFCLASS32 = 1;
lib/std/fs/File.zig+16-6
......@@ -1228,14 +1228,12 @@ pub const Reader = struct {
12281228 pub fn seekBy(r: *Reader, offset: i64) Reader.SeekError!void {
12291229 switch (r.mode) {
12301230 .positional, .positional_reading => {
1231 // TODO: make += operator allow any integer types
1232 r.pos = @intCast(@as(i64, @intCast(r.pos)) + offset);
1231 setPosAdjustingBuffer(r, @intCast(@as(i64, @intCast(r.pos)) + offset));
12331232 },
12341233 .streaming, .streaming_reading => {
12351234 const seek_err = r.seek_err orelse e: {
12361235 if (posix.lseek_CUR(r.file.handle, offset)) |_| {
1237 // TODO: make += operator allow any integer types
1238 r.pos = @intCast(@as(i64, @intCast(r.pos)) + offset);
1236 setPosAdjustingBuffer(r, @intCast(@as(i64, @intCast(r.pos)) + offset));
12391237 return;
12401238 } else |err| {
12411239 r.seek_err = err;
......@@ -1251,6 +1249,8 @@ pub const Reader = struct {
12511249 r.pos += n;
12521250 remaining -= n;
12531251 }
1252 r.interface.seek = 0;
1253 r.interface.end = 0;
12541254 },
12551255 .failure => return r.seek_err.?,
12561256 }
......@@ -1259,7 +1259,7 @@ pub const Reader = struct {
12591259 pub fn seekTo(r: *Reader, offset: u64) Reader.SeekError!void {
12601260 switch (r.mode) {
12611261 .positional, .positional_reading => {
1262 r.pos = offset;
1262 setPosAdjustingBuffer(r, offset);
12631263 },
12641264 .streaming, .streaming_reading => {
12651265 if (offset >= r.pos) return Reader.seekBy(r, @intCast(offset - r.pos));
......@@ -1268,12 +1268,22 @@ pub const Reader = struct {
12681268 r.seek_err = err;
12691269 return err;
12701270 };
1271 r.pos = offset;
1271 setPosAdjustingBuffer(r, offset);
12721272 },
12731273 .failure => return r.seek_err.?,
12741274 }
12751275 }
12761276
1277 fn setPosAdjustingBuffer(r: *Reader, offset: u64) void {
1278 if (offset < r.pos or offset >= r.pos + r.interface.bufferedLen()) {
1279 r.interface.seek = 0;
1280 r.interface.end = 0;
1281 } else {
1282 r.interface.seek += @intCast(offset - r.pos);
1283 }
1284 r.pos = offset;
1285 }
1286
12771287 /// Number of slices to store on the stack, when trying to send as many byte
12781288 /// vectors through the underlying read calls as possible.
12791289 const max_buffers_len = 16;
test/standalone/stack_iterator/build.zig+64-63
......@@ -65,69 +65,70 @@ pub fn build(b: *std.Build) void {
6565 test_step.dependOn(&run_cmd.step);
6666 }
6767
68 // Unwinding through a C shared library without a frame pointer (libc)
69 //
70 // getcontext version: libc
71 //
72 // Unwind info type:
73 // - ELF: DWARF .eh_frame + .debug_frame
74 // - MachO: __unwind_info encodings:
75 // - x86_64: STACK_IMMD, STACK_IND
76 // - aarch64: FRAMELESS, DWARF
77 {
78 const c_shared_lib = b.addLibrary(.{
79 .linkage = .dynamic,
80 .name = "c_shared_lib",
81 .root_module = b.createModule(.{
82 .root_source_file = null,
83 .target = target,
84 .optimize = optimize,
85 .link_libc = true,
86 .strip = false,
87 }),
88 });
89
90 if (target.result.os.tag == .windows)
91 c_shared_lib.root_module.addCMacro("LIB_API", "__declspec(dllexport)");
92
93 c_shared_lib.root_module.addCSourceFile(.{
94 .file = b.path("shared_lib.c"),
95 .flags = &.{"-fomit-frame-pointer"},
96 });
97
98 const exe = b.addExecutable(.{
99 .name = "shared_lib_unwind",
100 .root_module = b.createModule(.{
101 .root_source_file = b.path("shared_lib_unwind.zig"),
102 .target = target,
103 .optimize = optimize,
104 .unwind_tables = if (target.result.os.tag.isDarwin()) .async else null,
105 .omit_frame_pointer = true,
106 }),
107 // zig objcopy doesn't support incremental binaries
108 .use_llvm = true,
109 });
110
111 exe.linkLibrary(c_shared_lib);
112
113 const run_cmd = b.addRunArtifact(exe);
114 test_step.dependOn(&run_cmd.step);
115
116 // Separate debug info ELF file
117 if (target.result.ofmt == .elf) {
118 const filename = b.fmt("{s}_stripped", .{exe.out_filename});
119 const stripped_exe = b.addObjCopy(exe.getEmittedBin(), .{
120 .basename = filename, // set the name for the debuglink
121 .compress_debug = true,
122 .strip = .debug,
123 .extract_to_separate_file = true,
124 });
125
126 const run_stripped = std.Build.Step.Run.create(b, b.fmt("run {s}", .{filename}));
127 run_stripped.addFileArg(stripped_exe.getOutput());
128 test_step.dependOn(&run_stripped.step);
129 }
130 }
68 // https://github.com/ziglang/zig/issues/24522
69 //// Unwinding through a C shared library without a frame pointer (libc)
70 ////
71 //// getcontext version: libc
72 ////
73 //// Unwind info type:
74 //// - ELF: DWARF .eh_frame + .debug_frame
75 //// - MachO: __unwind_info encodings:
76 //// - x86_64: STACK_IMMD, STACK_IND
77 //// - aarch64: FRAMELESS, DWARF
78 //{
79 // const c_shared_lib = b.addLibrary(.{
80 // .linkage = .dynamic,
81 // .name = "c_shared_lib",
82 // .root_module = b.createModule(.{
83 // .root_source_file = null,
84 // .target = target,
85 // .optimize = optimize,
86 // .link_libc = true,
87 // .strip = false,
88 // }),
89 // });
90
91 // if (target.result.os.tag == .windows)
92 // c_shared_lib.root_module.addCMacro("LIB_API", "__declspec(dllexport)");
93
94 // c_shared_lib.root_module.addCSourceFile(.{
95 // .file = b.path("shared_lib.c"),
96 // .flags = &.{"-fomit-frame-pointer"},
97 // });
98
99 // const exe = b.addExecutable(.{
100 // .name = "shared_lib_unwind",
101 // .root_module = b.createModule(.{
102 // .root_source_file = b.path("shared_lib_unwind.zig"),
103 // .target = target,
104 // .optimize = optimize,
105 // .unwind_tables = if (target.result.os.tag.isDarwin()) .async else null,
106 // .omit_frame_pointer = true,
107 // }),
108 // // zig objcopy doesn't support incremental binaries
109 // .use_llvm = true,
110 // });
111
112 // exe.linkLibrary(c_shared_lib);
113
114 // const run_cmd = b.addRunArtifact(exe);
115 // test_step.dependOn(&run_cmd.step);
116
117 // // Separate debug info ELF file
118 // if (target.result.ofmt == .elf) {
119 // const filename = b.fmt("{s}_stripped", .{exe.out_filename});
120 // const stripped_exe = b.addObjCopy(exe.getEmittedBin(), .{
121 // .basename = filename, // set the name for the debuglink
122 // .compress_debug = true,
123 // .strip = .debug,
124 // .extract_to_separate_file = true,
125 // });
126
127 // const run_stripped = std.Build.Step.Run.create(b, b.fmt("run {s}", .{filename}));
128 // run_stripped.addFileArg(stripped_exe.getOutput());
129 // test_step.dependOn(&run_stripped.step);
130 // }
131 //}
131132
132133 // Unwinding without libc/posix
133134 //
tools/gen_stubs.zig+2-1
......@@ -310,7 +310,8 @@ pub fn main() !void {
310310 build_all_path, libc_so_path, @errorName(err),
311311 });
312312 };
313 const header = try elf.Header.parse(elf_bytes[0..@sizeOf(elf.Elf64_Ehdr)]);
313 var stream: std.Io.Reader = .fixed(elf_bytes);
314 const header = try elf.Header.read(&stream);
314315
315316 const parse: Parse = .{
316317 .arena = arena,