authorgravatar for xavierb@gmail.comXavier Bouchoux <xavierb@gmail.com> 2023-03-12 12:19:16+01:00
committergravatar for xavierb@gmail.comXavier Bouchoux <xavierb@gmail.com> 2023-03-20 08:39:23+01:00
loge1cf2c8346fd16849b7f71f982ddda7adf0fe97b
treef57c241fe732e20d3e2f9a2de66b4774c958e19c
parent9ea404f03d53c3589043c9a2d6f9b9e06ec86e20

objcopy: cleanups and extract helper functions to reduce bloat

parts of the code are independant of Elf32/Elf64 variant, so avoid generating the code twice by putting them outside of the generic struct.

1 files changed, 263 insertions(+), 241 deletions(-)

src/objcopy.zig+263-241
......@@ -129,10 +129,6 @@ pub fn cmdObjCopy(
129129 fatal("zig objcopy: ELF to RAW or HEX copying does not support --strip", .{});
130130 if (opt_extract != null)
131131 fatal("zig objcopy: ELF to RAW or HEX copying does not support --extract-to", .{});
132 if (opt_extract != null)
133 fatal("zig objcopy: ELF to RAW or HEX copying does not support --extract-to", .{});
134 if (opt_extract != null)
135 fatal("zig objcopy: ELF to RAW or HEX copying does not support --extract-to", .{});
136132
137133 try emitElf(arena, in_file, out_file, elf_hdr, .{
138134 .ofmt = out_fmt,
......@@ -658,7 +654,7 @@ test "containsValidAddressRange" {
658654// -------------
659655// ELF to ELF stripping
660656
661pub const StripElfOptions = struct {
657const StripElfOptions = struct {
662658 extract_to: ?[]const u8 = null,
663659 add_debuglink: ?[]const u8 = null,
664660 strip_all: bool = false,
......@@ -673,72 +669,64 @@ fn stripElf(
673669 elf_hdr: elf.Header,
674670 options: StripElfOptions,
675671) !void {
676 switch (elf_hdr.is_64) {
677 inline else => |is_64| {
678 const Elf = ElfContents(is_64);
679 const Filter = Elf.Filter;
680 const DebugLink = Elf.DebugLink;
681
682 var elf_contents = try Elf.parse(allocator, in_file, elf_hdr);
683 defer elf_contents.deinit();
684
685 const filter: Filter = filter: {
686 if (options.only_keep_debug) break :filter .debug;
687 if (options.strip_all) break :filter .program;
688 if (options.strip_debug) break :filter .program_and_symbols;
689 break :filter .all;
672 const Filter = ElfFileHelper.Filter;
673 const DebugLink = ElfFileHelper.DebugLink;
674
675 const filter: Filter = filter: {
676 if (options.only_keep_debug) break :filter .debug;
677 if (options.strip_all) break :filter .program;
678 if (options.strip_debug) break :filter .program_and_symbols;
679 break :filter .all;
680 };
681
682 const filter_complement: ?Filter = blk: {
683 if (options.extract_to) |_| {
684 break :blk switch (filter) {
685 .program => .debug_and_symbols,
686 .debug => .program_and_symbols,
687 .program_and_symbols => .debug,
688 .debug_and_symbols => .program,
689 .all => fatal("zig objcopy: nothing to extract", .{}),
690690 };
691 } else {
692 break :blk null;
693 }
694 };
695 const debuglink_path = path: {
696 if (options.add_debuglink) |path| break :path path;
697 if (options.extract_to) |path| break :path path;
698 break :path null;
699 };
691700
692 if (options.extract_to) |filename| {
693 const dbg_file = std.fs.cwd().createFile(filename, .{}) catch |err| {
694 fatal("zig objcopy: unable to create '{s}': {s}", .{ filename, @errorName(err) });
701 switch (elf_hdr.is_64) {
702 inline else => |is_64| {
703 var elf_file = try ElfFile(is_64).parse(allocator, in_file, elf_hdr);
704 defer elf_file.deinit();
705
706 if (filter_complement) |flt| {
707 // write the .dbg file and close it, so it can be read back to compute the debuglink checksum.
708 const path = options.extract_to.?;
709 const dbg_file = std.fs.cwd().createFile(path, .{}) catch |err| {
710 fatal("zig objcopy: unable to create '{s}': {s}", .{ path, @errorName(err) });
695711 };
696712 defer dbg_file.close();
697713
698 const filter_complement: Filter = switch (filter) {
699 .program => .debug_and_symbols,
700 .debug => .program_and_symbols,
701 .program_and_symbols => .debug,
702 .debug_and_symbols => .program,
703 .all => fatal("zig objcopy: nothing to extract", .{}),
704 };
705
706 try elf_contents.emit(allocator, dbg_file, in_file, filter_complement, null);
714 try elf_file.emit(allocator, dbg_file, in_file, .{ .section_filter = flt });
707715 }
708716
709 const debuglink: ?DebugLink = blk: {
710 const debuglink_filename = name: {
711 if (options.add_debuglink) |filename| break :name filename;
712 if (options.extract_to) |filename| break :name filename;
713 break :name null;
714 };
715 if (debuglink_filename) |filename| {
716 const dbg_file = std.fs.cwd().openFile(filename, .{}) catch |err| {
717 fatal("zig objcopy: could not read `{s}`: {s}\n", .{ filename, @errorName(err) });
718 };
719 defer dbg_file.close();
720
721 break :blk .{
722 .name = std.fs.path.basename(filename),
723 .crc32 = try computeFileCrc(dbg_file),
724 };
725 } else {
726 break :blk null;
727 }
728 };
729
730 try elf_contents.emit(allocator, out_file, in_file, filter, debuglink);
717 const debuglink: ?DebugLink = if (debuglink_path) |path| ElfFileHelper.createDebugLink(path) else null;
718 try elf_file.emit(allocator, out_file, in_file, .{ .section_filter = filter, .debuglink = debuglink });
731719 },
732720 }
733721}
734722
735723// note: this is "a minimal effort implementation"
736724// It doesn't support all possibile elf files: some sections type may need fixups, the program header may need fix up, ...
737// 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++` )
738// It manupulates and reoders the sections as little as possible to avoid having to do fixups.
725// 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++` )
726// It moves and reoders the sections as little as possible to avoid having to do fixups.
739727// TODO: support non-native endianess
740728
741fn ElfContents(comptime is_64: bool) type {
729fn ElfFile(comptime is_64: bool) type {
742730 const Elf_Ehdr = if (is_64) elf.Elf64_Ehdr else elf.Elf32_Ehdr;
743731 const Elf_Phdr = if (is_64) elf.Elf64_Phdr else elf.Elf32_Phdr;
744732 const Elf_Shdr = if (is_64) elf.Elf64_Shdr else elf.Elf32_Shdr;
......@@ -752,27 +740,26 @@ fn ElfContents(comptime is_64: bool) type {
752740 sections: []const Section,
753741 arena: std.heap.ArenaAllocator,
754742
743 const SectionCategory = ElfFileHelper.SectionCategory;
755744 const section_memory_align = @alignOf(Elf_Sym); // most restrictive of what we may load in memory
756745 const Section = struct {
757746 section: Elf_Shdr,
758747 name: []const u8 = "",
759748 segment: ?*const Elf_Phdr = null, // if the section is used by a program segment (there can be more than one)
760749 payload: ?[]align(section_memory_align) const u8 = null, // if we need the data in memory
761 usage: Usage = .none, // should the section be kept in the exe or stripped to the debug database, or both.
762
763 const Usage = enum { common, exe, debug, symbols, none };
750 category: SectionCategory = .none, // should the section be kept in the exe or stripped to the debug database, or both.
764751 };
765752
766753 const Self = @This();
767754
768 pub fn parse(gpa: Allocator, source: File, header: elf.Header) !Self {
755 pub fn parse(gpa: Allocator, in_file: File, header: elf.Header) !Self {
769756 var arena = std.heap.ArenaAllocator.init(gpa);
770757 errdefer arena.deinit();
771758 const allocator = arena.allocator();
772759
773760 var raw_header: Elf_Ehdr = undefined;
774761 {
775 const bytes_read = try source.preadAll(std.mem.asBytes(&raw_header), 0);
762 const bytes_read = try in_file.preadAll(std.mem.asBytes(&raw_header), 0);
776763 if (bytes_read < @sizeOf(Elf_Ehdr))
777764 return error.TRUNCATED_ELF;
778765 }
......@@ -783,7 +770,7 @@ fn ElfContents(comptime is_64: bool) type {
783770 fatal("zig objcopy: unsuported ELF file, unexpected phentsize ({d})", .{header.phentsize});
784771
785772 const program_header = try allocator.alloc(Elf_Phdr, header.phnum);
786 const bytes_read = try source.preadAll(std.mem.sliceAsBytes(program_header), header.phoff);
773 const bytes_read = try in_file.preadAll(std.mem.sliceAsBytes(program_header), header.phoff);
787774 if (bytes_read < @sizeOf(Elf_Phdr) * header.phnum)
788775 return error.TRUNCATED_ELF;
789776 break :blk program_header;
......@@ -798,7 +785,7 @@ fn ElfContents(comptime is_64: bool) type {
798785
799786 const raw_section_header = try allocator.alloc(Elf_Shdr, header.shnum);
800787 defer allocator.free(raw_section_header);
801 const bytes_read = try source.preadAll(std.mem.sliceAsBytes(raw_section_header), header.shoff);
788 const bytes_read = try in_file.preadAll(std.mem.sliceAsBytes(raw_section_header), header.shoff);
802789 if (bytes_read < @sizeOf(Elf_Phdr) * header.shnum)
803790 return error.TRUNCATED_ELF;
804791
......@@ -821,7 +808,7 @@ fn ElfContents(comptime is_64: bool) type {
821808
822809 if (need_data or need_strings) {
823810 const buffer = try allocator.alignedAlloc(u8, section_memory_align, @intCast(usize, section.section.sh_size));
824 const bytes_read = try source.preadAll(buffer, section.section.sh_offset);
811 const bytes_read = try in_file.preadAll(buffer, section.section.sh_offset);
825812 if (bytes_read != section.section.sh_size) return error.TRUNCATED_ELF;
826813 section.payload = buffer;
827814 }
......@@ -830,7 +817,7 @@ fn ElfContents(comptime is_64: bool) type {
830817 // fill-in sections info:
831818 // resolve the name
832819 // find if a program segment uses the section
833 // classify sections usage (used by program segments, debug datadase, common metadata, symbol table)
820 // categorise sections usage (used by program segments, debug datadase, common metadata, symbol table)
834821 for (sections) |*section| {
835822 section.segment = for (program_segments) |*seg| {
836823 if (sectionWithinSegment(section.section, seg.*)) break seg;
......@@ -839,65 +826,36 @@ fn ElfContents(comptime is_64: bool) type {
839826 if (section.section.sh_name != 0 and header.shstrndx != elf.SHN_UNDEF)
840827 section.name = std.mem.span(@ptrCast([*:0]const u8, &sections[header.shstrndx].payload.?[section.section.sh_name]));
841828
842 const usage_from_program: Section.Usage = if (section.segment != null) .exe else .debug;
843 section.usage = switch (section.section.sh_type) {
829 const category_from_program: SectionCategory = if (section.segment != null) .exe else .debug;
830 section.category = switch (section.section.sh_type) {
844831 elf.SHT_NOTE => .common,
845832 elf.SHT_SYMTAB => .symbols, // "strip all" vs "strip only debug"
846833 elf.SHT_DYNSYM => .exe,
847 elf.SHT_PROGBITS => usage: {
848 if (std.mem.eql(u8, section.name, ".comment")) break :usage .exe;
849 if (std.mem.eql(u8, section.name, ".gnu_debuglink")) break :usage .none;
850 break :usage usage_from_program;
834 elf.SHT_PROGBITS => cat: {
835 if (std.mem.eql(u8, section.name, ".comment")) break :cat .exe;
836 if (std.mem.eql(u8, section.name, ".gnu_debuglink")) break :cat .none;
837 break :cat category_from_program;
851838 },
852839 elf.SHT_LOPROC...elf.SHT_HIPROC => .common, // don't strip unkonwn sections
853840 elf.SHT_LOUSER...elf.SHT_HIUSER => .common, // don't strip unkonwn sections
854 else => usage_from_program,
841 else => category_from_program,
855842 };
856843 }
857844
858 sections[0].usage = .common; // mandatory null section
845 sections[0].category = .common; // mandatory null section
859846 if (header.shstrndx != elf.SHN_UNDEF)
860 sections[header.shstrndx].usage = .common; // string table for the headers
847 sections[header.shstrndx].category = .common; // string table for the headers
861848
862849 // recursive dependencies
863850 var dirty: u1 = 1;
864851 while (dirty != 0) {
865852 dirty = 0;
866853
867 const Local = struct {
868 fn propagateUsage(cur: *Section.Usage, new: Section.Usage) u1 {
869 const use: Section.Usage = switch (cur.*) {
870 .none => new,
871 .common => .common,
872 .debug => switch (new) {
873 .none, .debug => .debug,
874 else => new,
875 },
876 .exe => switch (new) {
877 .common => .common,
878 .none, .debug, .exe => .exe,
879 .symbols => .exe,
880 },
881 .symbols => switch (new) {
882 .none, .common, .debug, .exe => unreachable,
883 .symbols => .symbols,
884 },
885 };
886
887 if (cur.* != use) {
888 cur.* = use;
889 return 1;
890 } else {
891 return 0;
892 }
893 }
894 };
895
896854 for (sections) |*section| {
897855 if (section.section.sh_link != elf.SHN_UNDEF)
898 dirty |= Local.propagateUsage(&sections[section.section.sh_link].usage, section.usage);
856 dirty |= ElfFileHelper.propagateCategory(&sections[section.section.sh_link].category, section.category);
899857 if ((section.section.sh_flags & elf.SHF_INFO_LINK) != 0 and section.section.sh_info != elf.SHN_UNDEF)
900 dirty |= Local.propagateUsage(&sections[section.section.sh_info].usage, section.usage);
858 dirty |= ElfFileHelper.propagateCategory(&sections[section.section.sh_info].category, section.category);
901859
902860 if (section.payload) |data| {
903861 switch (section.section.sh_type) {
......@@ -906,7 +864,7 @@ fn ElfContents(comptime is_64: bool) type {
906864 const defs = @ptrCast([*]const Elf_Verdef, data)[0 .. @intCast(usize, section.section.sh_size) / @sizeOf(Elf_Verdef)];
907865 for (defs) |def| {
908866 if (def.vd_ndx != elf.SHN_UNDEF)
909 dirty |= Local.propagateUsage(&sections[def.vd_ndx].usage, section.usage);
867 dirty |= ElfFileHelper.propagateCategory(&sections[def.vd_ndx].category, section.category);
910868 }
911869 },
912870 elf.SHT_SYMTAB, elf.SHT_DYNSYM => {
......@@ -915,7 +873,7 @@ fn ElfContents(comptime is_64: bool) type {
915873
916874 for (syms) |sym| {
917875 if (sym.st_shndx != elf.SHN_UNDEF and sym.st_shndx < elf.SHN_LORESERVE)
918 dirty |= Local.propagateUsage(&sections[sym.st_shndx].usage, section.usage);
876 dirty |= ElfFileHelper.propagateCategory(&sections[sym.st_shndx].category, section.category);
919877 }
920878 },
921879 else => {},
......@@ -936,9 +894,13 @@ fn ElfContents(comptime is_64: bool) type {
936894 self.arena.deinit();
937895 }
938896
939 const DebugLink = struct { name: []const u8, crc32: u32 };
940 const Filter = enum { all, program, debug, program_and_symbols, debug_and_symbols };
941 fn emit(self: *const Self, gpa: Allocator, output: File, source: File, filter: Filter, debuglink: ?DebugLink) !void {
897 const Filter = ElfFileHelper.Filter;
898 const DebugLink = ElfFileHelper.DebugLink;
899 const EmitElfOptions = struct {
900 section_filter: Filter = .all,
901 debuglink: ?DebugLink = null,
902 };
903 fn emit(self: *const Self, gpa: Allocator, out_file: File, in_file: File, options: EmitElfOptions) !void {
942904 var arena = std.heap.ArenaAllocator.init(gpa);
943905 defer arena.deinit();
944906 const allocator = arena.allocator();
......@@ -950,63 +912,37 @@ fn ElfContents(comptime is_64: bool) type {
950912 // the program header is kept unchanged. (`strip` does update it, but `eu-strip` does not, and it still works)
951913
952914 const Update = struct {
953 action: enum { keep, strip, empty },
915 action: ElfFileHelper.Action,
954916
955917 // remap the indexs after omitting the filtered sections
956918 remap_idx: u16,
957919
958920 // optionally overrides the payload from the source file
959 payload: ?[]align(section_memory_align) const u8,
921 payload: ?[]align(section_memory_align) const u8 = null,
922 section: ?Elf_Shdr = null,
960923 };
961924 const sections_update = try allocator.alloc(Update, self.sections.len);
962925 const new_shnum = blk: {
963926 var next_idx: u16 = 0;
964927 for (self.sections, sections_update) |section, *update| {
965 update.action = action: {
966 if (section.usage == .none) break :action .strip;
967 break :action switch (filter) {
968 .all => switch (section.usage) {
969 .none => .strip,
970 else => .keep,
971 },
972 .program => switch (section.usage) {
973 .common, .exe => .keep,
974 else => .strip,
975 },
976 .program_and_symbols => switch (section.usage) {
977 .common, .exe, .symbols => .keep,
978 else => .strip,
979 },
980 .debug => switch (section.usage) {
981 .exe, .symbols => .empty,
982 .none => .strip,
983 else => .keep,
984 },
985 .debug_and_symbols => switch (section.usage) {
986 .exe => .empty,
987 .none => .strip,
988 else => .keep,
989 },
990 };
991 };
992
993 if (update.action == .strip) {
994 update.remap_idx = elf.SHN_UNDEF;
995 } else {
996 update.remap_idx = next_idx;
928 const action = ElfFileHelper.selectAction(section.category, options.section_filter);
929 const remap_idx = idx: {
930 if (action == .strip) break :idx elf.SHN_UNDEF;
997931 next_idx += 1;
998 }
999
1000 update.payload = null;
932 break :idx next_idx - 1;
933 };
934 update.* = Update{ .action = action, .remap_idx = remap_idx };
1001935 }
1002936
1003 if (debuglink != null)
937 if (options.debuglink != null)
1004938 next_idx += 1;
939
1005940 break :blk next_idx;
1006941 };
1007942
943 // add a ".gnu_debuglink" to the string table if needed
1008944 const debuglink_name: u32 = blk: {
1009 if (debuglink == null) break :blk elf.SHN_UNDEF;
945 if (options.debuglink == null) break :blk elf.SHN_UNDEF;
1010946 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1011947 fatal("zig objcopy: no strtab, cannot add the debuglink section", .{}); // TODO add the section if needed?
1012948
......@@ -1026,11 +962,7 @@ fn ElfContents(comptime is_64: bool) type {
1026962 break :blk new_offset;
1027963 };
1028964
1029 const WriteCmd = union(enum) {
1030 copy_range: struct { in_offset: u64, len: u64, out_offset: u64 },
1031 write_data: struct { data: []const u8, out_offset: u64 },
1032 };
1033 var cmdbuf = std.ArrayList(WriteCmd).init(allocator);
965 var cmdbuf = std.ArrayList(ElfFileHelper.WriteCmd).init(allocator);
1034966 defer cmdbuf.deinit();
1035967 try cmdbuf.ensureUnusedCapacity(3 + new_shnum);
1036968 var eof_offset: Elf_OffSize = 0; // track the end of the data written so far.
......@@ -1076,8 +1008,9 @@ fn ElfContents(comptime is_64: bool) type {
10761008 if (update.action == .strip) continue;
10771009 std.debug.assert(update.remap_idx == dest_section_idx);
10781010
1079 const src = &section.section;
1011 const src = if (update.section) |*s| s else &section.section;
10801012 const dest = &dest_sections[dest_section_idx];
1013 const payload = if (update.payload) |data| data else section.payload;
10811014 dest_section_idx += 1;
10821015
10831016 dest.* = src.*;
......@@ -1087,7 +1020,6 @@ fn ElfContents(comptime is_64: bool) type {
10871020 if ((src.sh_flags & elf.SHF_INFO_LINK) != 0 and src.sh_info != elf.SHN_UNDEF)
10881021 dest.sh_info = sections_update[src.sh_info].remap_idx;
10891022
1090 const payload = if (update.payload) |data| data else section.payload;
10911023 if (payload) |data|
10921024 dest.sh_size = @intCast(Elf_OffSize, data.len);
10931025
......@@ -1108,29 +1040,36 @@ fn ElfContents(comptime is_64: bool) type {
11081040 if (dest.sh_type != elf.SHT_NOBITS) {
11091041 if (payload) |src_data| {
11101042 // update sections payload and write
1111 const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len);
1112 std.mem.copy(u8, data, src_data);
1043 const dest_data = switch (src.sh_type) {
1044 elf.DT_VERSYM => dst_data: {
1045 const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len);
1046 std.mem.copy(u8, data, src_data);
11131047
1114 switch (src.sh_type) {
1115 elf.DT_VERSYM => {
11161048 const defs = @ptrCast([*]Elf_Verdef, data)[0 .. @intCast(usize, src.sh_size) / @sizeOf(Elf_Verdef)];
11171049 for (defs) |*def| {
11181050 if (def.vd_ndx != elf.SHN_UNDEF)
11191051 def.vd_ndx = sections_update[src.sh_info].remap_idx;
11201052 }
1053
1054 break :dst_data data;
11211055 },
1122 elf.SHT_SYMTAB, elf.SHT_DYNSYM => {
1056 elf.SHT_SYMTAB, elf.SHT_DYNSYM => dst_data: {
1057 const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len);
1058 std.mem.copy(u8, data, src_data);
1059
11231060 const syms = @ptrCast([*]Elf_Sym, data)[0 .. @intCast(usize, src.sh_size) / @sizeOf(Elf_Sym)];
11241061 for (syms) |*sym| {
11251062 if (sym.st_shndx != elf.SHN_UNDEF and sym.st_shndx < elf.SHN_LORESERVE)
11261063 sym.st_shndx = sections_update[sym.st_shndx].remap_idx;
11271064 }
1065
1066 break :dst_data data;
11281067 },
1129 else => {},
1130 }
1068 else => src_data,
1069 };
11311070
1132 std.debug.assert(data.len == dest.sh_size);
1133 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = dest.sh_offset } });
1071 std.debug.assert(dest_data.len == dest.sh_size);
1072 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = dest_data, .out_offset = dest.sh_offset } });
11341073 eof_offset = dest.sh_offset + dest.sh_size;
11351074 } else {
11361075 // direct contents copy
......@@ -1144,7 +1083,7 @@ fn ElfContents(comptime is_64: bool) type {
11441083 }
11451084
11461085 // add a ".gnu_debuglink" section
1147 if (debuglink) |link| {
1086 if (options.debuglink) |link| {
11481087 const payload = payload: {
11491088 const crc_offset = std.mem.alignForward(link.name.len + 1, 4);
11501089 const buf = try allocator.alignedAlloc(u8, 4, crc_offset + 4);
......@@ -1188,71 +1127,7 @@ fn ElfContents(comptime is_64: bool) type {
11881127 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_shoff } });
11891128 }
11901129
1191 // consolidate holes between writes:
1192 // by coping original padding data from in_file (by fusing contiguous ranges)
1193 // by writing zeroes otherwise
1194 const zeroes = [1]u8{0} ** 4096;
1195 const consolidated_cmdbuf = blk: {
1196 var newbuf = std.ArrayList(WriteCmd).init(allocator);
1197 try newbuf.ensureUnusedCapacity(cmdbuf.items.len * 2);
1198 var offset: u64 = 0;
1199 var fused_cmd: ?WriteCmd = null;
1200 for (cmdbuf.items) |cmd| {
1201 switch (cmd) {
1202 .write_data => |data| {
1203 std.debug.assert(data.out_offset >= offset);
1204 if (fused_cmd) |prev| {
1205 newbuf.appendAssumeCapacity(prev);
1206 fused_cmd = null;
1207 }
1208 if (data.out_offset > offset) {
1209 newbuf.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(usize, data.out_offset - offset)], .out_offset = offset } });
1210 }
1211 newbuf.appendAssumeCapacity(cmd);
1212 offset = data.out_offset + data.data.len;
1213 },
1214 .copy_range => |range| {
1215 std.debug.assert(range.out_offset >= offset);
1216 if (fused_cmd) |prev| {
1217 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)) {
1218 fused_cmd = .{ .copy_range = .{
1219 .in_offset = prev.copy_range.in_offset,
1220 .out_offset = prev.copy_range.out_offset,
1221 .len = (range.out_offset + range.len) - prev.copy_range.out_offset,
1222 } };
1223 } else {
1224 newbuf.appendAssumeCapacity(prev);
1225 if (range.out_offset > offset) {
1226 newbuf.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(usize, range.out_offset - offset)], .out_offset = offset } });
1227 }
1228 fused_cmd = cmd;
1229 }
1230 } else {
1231 fused_cmd = cmd;
1232 }
1233 offset = range.out_offset + range.len;
1234 },
1235 }
1236 }
1237 if (fused_cmd) |cmd| {
1238 newbuf.appendAssumeCapacity(cmd);
1239 }
1240 break :blk newbuf.items;
1241 };
1242
1243 // write the output file
1244 for (consolidated_cmdbuf) |cmd| {
1245 switch (cmd) {
1246 .write_data => |data| {
1247 var iovec = [_]std.os.iovec_const{.{ .iov_base = data.data.ptr, .iov_len = data.data.len }};
1248 try output.pwritevAll(&iovec, data.out_offset);
1249 },
1250 .copy_range => |range| {
1251 const copied_bytes = try source.copyRangeAll(range.in_offset, output, range.out_offset, range.len);
1252 if (copied_bytes < range.len) return error.TRUNCATED_ELF;
1253 },
1254 }
1255 }
1130 try ElfFileHelper.write(allocator, out_file, in_file, cmdbuf.items);
12561131 }
12571132
12581133 fn sectionWithinSegment(section: Elf_Shdr, segment: Elf_Phdr) bool {
......@@ -1262,15 +1137,162 @@ fn ElfContents(comptime is_64: bool) type {
12621137 };
12631138}
12641139
1265fn computeFileCrc(file: File) !u32 {
1266 var buf: [8000]u8 = undefined;
1140const ElfFileHelper = struct {
1141 const DebugLink = struct { name: []const u8, crc32: u32 };
1142 const Filter = enum { all, program, debug, program_and_symbols, debug_and_symbols };
1143
1144 const SectionCategory = enum { common, exe, debug, symbols, none };
1145 fn propagateCategory(cur: *SectionCategory, new: SectionCategory) u1 {
1146 const cat: SectionCategory = switch (cur.*) {
1147 .none => new,
1148 .common => .common,
1149 .debug => switch (new) {
1150 .none, .debug => .debug,
1151 else => new,
1152 },
1153 .exe => switch (new) {
1154 .common => .common,
1155 .none, .debug, .exe => .exe,
1156 .symbols => .exe,
1157 },
1158 .symbols => switch (new) {
1159 .none, .common, .debug, .exe => unreachable,
1160 .symbols => .symbols,
1161 },
1162 };
12671163
1268 try file.seekTo(0);
1269 var hasher = std.hash.Crc32.init();
1270 while (true) {
1271 const bytes_read = try file.read(&buf);
1272 if (bytes_read == 0) break;
1273 hasher.update(buf[0..bytes_read]);
1164 if (cur.* != cat) {
1165 cur.* = cat;
1166 return 1;
1167 } else {
1168 return 0;
1169 }
12741170 }
1275 return hasher.final();
1276}
1171
1172 const Action = enum { keep, strip, empty };
1173 fn selectAction(category: SectionCategory, filter: Filter) Action {
1174 if (category == .none) return .strip;
1175 return switch (filter) {
1176 .all => switch (category) {
1177 .none => .strip,
1178 else => .keep,
1179 },
1180 .program => switch (category) {
1181 .common, .exe => .keep,
1182 else => .strip,
1183 },
1184 .program_and_symbols => switch (category) {
1185 .common, .exe, .symbols => .keep,
1186 else => .strip,
1187 },
1188 .debug => switch (category) {
1189 .exe, .symbols => .empty,
1190 .none => .strip,
1191 else => .keep,
1192 },
1193 .debug_and_symbols => switch (category) {
1194 .exe => .empty,
1195 .none => .strip,
1196 else => .keep,
1197 },
1198 };
1199 }
1200
1201 const WriteCmd = union(enum) {
1202 copy_range: struct { in_offset: u64, len: u64, out_offset: u64 },
1203 write_data: struct { data: []const u8, out_offset: u64 },
1204 };
1205 fn write(allocator: Allocator, out_file: File, in_file: File, cmds: []const WriteCmd) !void {
1206 // consolidate holes between writes:
1207 // by coping original padding data from in_file (by fusing contiguous ranges)
1208 // by writing zeroes otherwise
1209 const zeroes = [1]u8{0} ** 4096;
1210 var consolidated = std.ArrayList(WriteCmd).init(allocator);
1211 defer consolidated.deinit();
1212 try consolidated.ensureUnusedCapacity(cmds.len * 2);
1213 var offset: u64 = 0;
1214 var fused_cmd: ?WriteCmd = null;
1215 for (cmds) |cmd| {
1216 switch (cmd) {
1217 .write_data => |data| {
1218 std.debug.assert(data.out_offset >= offset);
1219 if (fused_cmd) |prev| {
1220 consolidated.appendAssumeCapacity(prev);
1221 fused_cmd = null;
1222 }
1223 if (data.out_offset > offset) {
1224 consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(usize, data.out_offset - offset)], .out_offset = offset } });
1225 }
1226 consolidated.appendAssumeCapacity(cmd);
1227 offset = data.out_offset + data.data.len;
1228 },
1229 .copy_range => |range| {
1230 std.debug.assert(range.out_offset >= offset);
1231 if (fused_cmd) |prev| {
1232 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)) {
1233 fused_cmd = .{ .copy_range = .{
1234 .in_offset = prev.copy_range.in_offset,
1235 .out_offset = prev.copy_range.out_offset,
1236 .len = (range.out_offset + range.len) - prev.copy_range.out_offset,
1237 } };
1238 } else {
1239 consolidated.appendAssumeCapacity(prev);
1240 if (range.out_offset > offset) {
1241 consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(usize, range.out_offset - offset)], .out_offset = offset } });
1242 }
1243 fused_cmd = cmd;
1244 }
1245 } else {
1246 fused_cmd = cmd;
1247 }
1248 offset = range.out_offset + range.len;
1249 },
1250 }
1251 }
1252 if (fused_cmd) |cmd| {
1253 consolidated.appendAssumeCapacity(cmd);
1254 }
1255
1256 // write the output file
1257 for (consolidated.items) |cmd| {
1258 switch (cmd) {
1259 .write_data => |data| {
1260 var iovec = [_]std.os.iovec_const{.{ .iov_base = data.data.ptr, .iov_len = data.data.len }};
1261 try out_file.pwritevAll(&iovec, data.out_offset);
1262 },
1263 .copy_range => |range| {
1264 const copied_bytes = try in_file.copyRangeAll(range.in_offset, out_file, range.out_offset, range.len);
1265 if (copied_bytes < range.len) return error.TRUNCATED_ELF;
1266 },
1267 }
1268 }
1269 }
1270
1271 fn createDebugLink(path: []const u8) DebugLink {
1272 const file = std.fs.cwd().openFile(path, .{}) catch |err| {
1273 fatal("zig objcopy: could not open `{s}`: {s}\n", .{ path, @errorName(err) });
1274 };
1275 defer file.close();
1276
1277 const crc = ElfFileHelper.computeFileCrc(file) catch |err| {
1278 fatal("zig objcopy: could not read `{s}`: {s}\n", .{ path, @errorName(err) });
1279 };
1280 return .{
1281 .name = std.fs.path.basename(path),
1282 .crc32 = crc,
1283 };
1284 }
1285
1286 fn computeFileCrc(file: File) !u32 {
1287 var buf: [8000]u8 = undefined;
1288
1289 try file.seekTo(0);
1290 var hasher = std.hash.Crc32.init();
1291 while (true) {
1292 const bytes_read = try file.read(&buf);
1293 if (bytes_read == 0) break;
1294 hasher.update(buf[0..bytes_read]);
1295 }
1296 return hasher.final();
1297 }
1298};