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(...@@ -129,10 +129,6 @@ pub fn cmdObjCopy(
129 fatal("zig objcopy: ELF to RAW or HEX copying does not support --strip", .{});129 fatal("zig objcopy: ELF to RAW or HEX copying does not support --strip", .{});
130 if (opt_extract != null)130 if (opt_extract != null)
131 fatal("zig objcopy: ELF to RAW or HEX copying does not support --extract-to", .{});131 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
137 try emitElf(arena, in_file, out_file, elf_hdr, .{133 try emitElf(arena, in_file, out_file, elf_hdr, .{
138 .ofmt = out_fmt,134 .ofmt = out_fmt,
...@@ -658,7 +654,7 @@ test "containsValidAddressRange" {...@@ -658,7 +654,7 @@ test "containsValidAddressRange" {
658// -------------654// -------------
659// ELF to ELF stripping655// ELF to ELF stripping
660656
661pub const StripElfOptions = struct {657const StripElfOptions = struct {
662 extract_to: ?[]const u8 = null,658 extract_to: ?[]const u8 = null,
663 add_debuglink: ?[]const u8 = null,659 add_debuglink: ?[]const u8 = null,
664 strip_all: bool = false,660 strip_all: bool = false,
...@@ -673,72 +669,64 @@ fn stripElf(...@@ -673,72 +669,64 @@ fn stripElf(
673 elf_hdr: elf.Header,669 elf_hdr: elf.Header,
674 options: StripElfOptions,670 options: StripElfOptions,
675) !void {671) !void {
676 switch (elf_hdr.is_64) {672 const Filter = ElfFileHelper.Filter;
677 inline else => |is_64| {673 const DebugLink = ElfFileHelper.DebugLink;
678 const Elf = ElfContents(is_64);674
679 const Filter = Elf.Filter;675 const filter: Filter = filter: {
680 const DebugLink = Elf.DebugLink;676 if (options.only_keep_debug) break :filter .debug;
681677 if (options.strip_all) break :filter .program;
682 var elf_contents = try Elf.parse(allocator, in_file, elf_hdr);678 if (options.strip_debug) break :filter .program_and_symbols;
683 defer elf_contents.deinit();679 break :filter .all;
684680 };
685 const filter: Filter = filter: {681
686 if (options.only_keep_debug) break :filter .debug;682 const filter_complement: ?Filter = blk: {
687 if (options.strip_all) break :filter .program;683 if (options.extract_to) |_| {
688 if (options.strip_debug) break :filter .program_and_symbols;684 break :blk switch (filter) {
689 break :filter .all;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", .{}),
690 };690 };
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| {701 switch (elf_hdr.is_64) {
693 const dbg_file = std.fs.cwd().createFile(filename, .{}) catch |err| {702 inline else => |is_64| {
694 fatal("zig objcopy: unable to create '{s}': {s}", .{ filename, @errorName(err) });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) });
695 };711 };
696 defer dbg_file.close();712 defer dbg_file.close();
697713
698 const filter_complement: Filter = switch (filter) {714 try elf_file.emit(allocator, dbg_file, in_file, .{ .section_filter = flt });
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);
707 }715 }
708716
709 const debuglink: ?DebugLink = blk: {717 const debuglink: ?DebugLink = if (debuglink_path) |path| ElfFileHelper.createDebugLink(path) else null;
710 const debuglink_filename = name: {718 try elf_file.emit(allocator, out_file, in_file, .{ .section_filter = filter, .debuglink = debuglink });
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);
731 },719 },
732 }720 }
733}721}
734722
735// note: this is "a minimal effort implementation"723// note: this is "a minimal effort implementation"
736// It doesn't support all possibile elf files: some sections type may need fixups, the program header may need fix up, ...724// 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++` )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++` )
738// It manupulates and reoders the sections as little as possible to avoid having to do fixups.726// It moves and reoders the sections as little as possible to avoid having to do fixups.
739// TODO: support non-native endianess727// TODO: support non-native endianess
740728
741fn ElfContents(comptime is_64: bool) type {729fn ElfFile(comptime is_64: bool) type {
742 const Elf_Ehdr = if (is_64) elf.Elf64_Ehdr else elf.Elf32_Ehdr;730 const Elf_Ehdr = if (is_64) elf.Elf64_Ehdr else elf.Elf32_Ehdr;
743 const Elf_Phdr = if (is_64) elf.Elf64_Phdr else elf.Elf32_Phdr;731 const Elf_Phdr = if (is_64) elf.Elf64_Phdr else elf.Elf32_Phdr;
744 const Elf_Shdr = if (is_64) elf.Elf64_Shdr else elf.Elf32_Shdr;732 const Elf_Shdr = if (is_64) elf.Elf64_Shdr else elf.Elf32_Shdr;
...@@ -752,27 +740,26 @@ fn ElfContents(comptime is_64: bool) type {...@@ -752,27 +740,26 @@ fn ElfContents(comptime is_64: bool) type {
752 sections: []const Section,740 sections: []const Section,
753 arena: std.heap.ArenaAllocator,741 arena: std.heap.ArenaAllocator,
754742
743 const SectionCategory = ElfFileHelper.SectionCategory;
755 const section_memory_align = @alignOf(Elf_Sym); // most restrictive of what we may load in memory744 const section_memory_align = @alignOf(Elf_Sym); // most restrictive of what we may load in memory
756 const Section = struct {745 const Section = struct {
757 section: Elf_Shdr,746 section: Elf_Shdr,
758 name: []const u8 = "",747 name: []const u8 = "",
759 segment: ?*const Elf_Phdr = null, // if the section is used by a program segment (there can be more than one)748 segment: ?*const Elf_Phdr = null, // if the section is used by a program segment (there can be more than one)
760 payload: ?[]align(section_memory_align) const u8 = null, // if we need the data in memory749 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.750 category: SectionCategory = .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 };
764 };751 };
765752
766 const Self = @This();753 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 {
769 var arena = std.heap.ArenaAllocator.init(gpa);756 var arena = std.heap.ArenaAllocator.init(gpa);
770 errdefer arena.deinit();757 errdefer arena.deinit();
771 const allocator = arena.allocator();758 const allocator = arena.allocator();
772759
773 var raw_header: Elf_Ehdr = undefined;760 var raw_header: Elf_Ehdr = undefined;
774 {761 {
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);
776 if (bytes_read < @sizeOf(Elf_Ehdr))763 if (bytes_read < @sizeOf(Elf_Ehdr))
777 return error.TRUNCATED_ELF;764 return error.TRUNCATED_ELF;
778 }765 }
...@@ -783,7 +770,7 @@ fn ElfContents(comptime is_64: bool) type {...@@ -783,7 +770,7 @@ fn ElfContents(comptime is_64: bool) type {
783 fatal("zig objcopy: unsuported ELF file, unexpected phentsize ({d})", .{header.phentsize});770 fatal("zig objcopy: unsuported ELF file, unexpected phentsize ({d})", .{header.phentsize});
784771
785 const program_header = try allocator.alloc(Elf_Phdr, header.phnum);772 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);
787 if (bytes_read < @sizeOf(Elf_Phdr) * header.phnum)774 if (bytes_read < @sizeOf(Elf_Phdr) * header.phnum)
788 return error.TRUNCATED_ELF;775 return error.TRUNCATED_ELF;
789 break :blk program_header;776 break :blk program_header;
...@@ -798,7 +785,7 @@ fn ElfContents(comptime is_64: bool) type {...@@ -798,7 +785,7 @@ fn ElfContents(comptime is_64: bool) type {
798785
799 const raw_section_header = try allocator.alloc(Elf_Shdr, header.shnum);786 const raw_section_header = try allocator.alloc(Elf_Shdr, header.shnum);
800 defer allocator.free(raw_section_header);787 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);
802 if (bytes_read < @sizeOf(Elf_Phdr) * header.shnum)789 if (bytes_read < @sizeOf(Elf_Phdr) * header.shnum)
803 return error.TRUNCATED_ELF;790 return error.TRUNCATED_ELF;
804791
...@@ -821,7 +808,7 @@ fn ElfContents(comptime is_64: bool) type {...@@ -821,7 +808,7 @@ fn ElfContents(comptime is_64: bool) type {
821808
822 if (need_data or need_strings) {809 if (need_data or need_strings) {
823 const buffer = try allocator.alignedAlloc(u8, section_memory_align, @intCast(usize, section.section.sh_size));810 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);
825 if (bytes_read != section.section.sh_size) return error.TRUNCATED_ELF;812 if (bytes_read != section.section.sh_size) return error.TRUNCATED_ELF;
826 section.payload = buffer;813 section.payload = buffer;
827 }814 }
...@@ -830,7 +817,7 @@ fn ElfContents(comptime is_64: bool) type {...@@ -830,7 +817,7 @@ fn ElfContents(comptime is_64: bool) type {
830 // fill-in sections info:817 // fill-in sections info:
831 // resolve the name818 // resolve the name
832 // find if a program segment uses the section819 // 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)
834 for (sections) |*section| {821 for (sections) |*section| {
835 section.segment = for (program_segments) |*seg| {822 section.segment = for (program_segments) |*seg| {
836 if (sectionWithinSegment(section.section, seg.*)) break seg;823 if (sectionWithinSegment(section.section, seg.*)) break seg;
...@@ -839,65 +826,36 @@ fn ElfContents(comptime is_64: bool) type {...@@ -839,65 +826,36 @@ fn ElfContents(comptime is_64: bool) type {
839 if (section.section.sh_name != 0 and header.shstrndx != elf.SHN_UNDEF)826 if (section.section.sh_name != 0 and header.shstrndx != elf.SHN_UNDEF)
840 section.name = std.mem.span(@ptrCast([*:0]const u8, &sections[header.shstrndx].payload.?[section.section.sh_name]));827 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;829 const category_from_program: SectionCategory = if (section.segment != null) .exe else .debug;
843 section.usage = switch (section.section.sh_type) {830 section.category = switch (section.section.sh_type) {
844 elf.SHT_NOTE => .common,831 elf.SHT_NOTE => .common,
845 elf.SHT_SYMTAB => .symbols, // "strip all" vs "strip only debug"832 elf.SHT_SYMTAB => .symbols, // "strip all" vs "strip only debug"
846 elf.SHT_DYNSYM => .exe,833 elf.SHT_DYNSYM => .exe,
847 elf.SHT_PROGBITS => usage: {834 elf.SHT_PROGBITS => cat: {
848 if (std.mem.eql(u8, section.name, ".comment")) break :usage .exe;835 if (std.mem.eql(u8, section.name, ".comment")) break :cat .exe;
849 if (std.mem.eql(u8, section.name, ".gnu_debuglink")) break :usage .none;836 if (std.mem.eql(u8, section.name, ".gnu_debuglink")) break :cat .none;
850 break :usage usage_from_program;837 break :cat category_from_program;
851 },838 },
852 elf.SHT_LOPROC...elf.SHT_HIPROC => .common, // don't strip unkonwn sections839 elf.SHT_LOPROC...elf.SHT_HIPROC => .common, // don't strip unkonwn sections
853 elf.SHT_LOUSER...elf.SHT_HIUSER => .common, // don't strip unkonwn sections840 elf.SHT_LOUSER...elf.SHT_HIUSER => .common, // don't strip unkonwn sections
854 else => usage_from_program,841 else => category_from_program,
855 };842 };
856 }843 }
857844
858 sections[0].usage = .common; // mandatory null section845 sections[0].category = .common; // mandatory null section
859 if (header.shstrndx != elf.SHN_UNDEF)846 if (header.shstrndx != elf.SHN_UNDEF)
860 sections[header.shstrndx].usage = .common; // string table for the headers847 sections[header.shstrndx].category = .common; // string table for the headers
861848
862 // recursive dependencies849 // recursive dependencies
863 var dirty: u1 = 1;850 var dirty: u1 = 1;
864 while (dirty != 0) {851 while (dirty != 0) {
865 dirty = 0;852 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
896 for (sections) |*section| {854 for (sections) |*section| {
897 if (section.section.sh_link != elf.SHN_UNDEF)855 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);
899 if ((section.section.sh_flags & elf.SHF_INFO_LINK) != 0 and section.section.sh_info != elf.SHN_UNDEF)857 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
902 if (section.payload) |data| {860 if (section.payload) |data| {
903 switch (section.section.sh_type) {861 switch (section.section.sh_type) {
...@@ -906,7 +864,7 @@ fn ElfContents(comptime is_64: bool) type {...@@ -906,7 +864,7 @@ fn ElfContents(comptime is_64: bool) type {
906 const defs = @ptrCast([*]const Elf_Verdef, data)[0 .. @intCast(usize, section.section.sh_size) / @sizeOf(Elf_Verdef)];864 const defs = @ptrCast([*]const Elf_Verdef, data)[0 .. @intCast(usize, section.section.sh_size) / @sizeOf(Elf_Verdef)];
907 for (defs) |def| {865 for (defs) |def| {
908 if (def.vd_ndx != elf.SHN_UNDEF)866 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);
910 }868 }
911 },869 },
912 elf.SHT_SYMTAB, elf.SHT_DYNSYM => {870 elf.SHT_SYMTAB, elf.SHT_DYNSYM => {
...@@ -915,7 +873,7 @@ fn ElfContents(comptime is_64: bool) type {...@@ -915,7 +873,7 @@ fn ElfContents(comptime is_64: bool) type {
915873
916 for (syms) |sym| {874 for (syms) |sym| {
917 if (sym.st_shndx != elf.SHN_UNDEF and sym.st_shndx < elf.SHN_LORESERVE)875 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);
919 }877 }
920 },878 },
921 else => {},879 else => {},
...@@ -936,9 +894,13 @@ fn ElfContents(comptime is_64: bool) type {...@@ -936,9 +894,13 @@ fn ElfContents(comptime is_64: bool) type {
936 self.arena.deinit();894 self.arena.deinit();
937 }895 }
938896
939 const DebugLink = struct { name: []const u8, crc32: u32 };897 const Filter = ElfFileHelper.Filter;
940 const Filter = enum { all, program, debug, program_and_symbols, debug_and_symbols };898 const DebugLink = ElfFileHelper.DebugLink;
941 fn emit(self: *const Self, gpa: Allocator, output: File, source: File, filter: Filter, debuglink: ?DebugLink) !void {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 {
942 var arena = std.heap.ArenaAllocator.init(gpa);904 var arena = std.heap.ArenaAllocator.init(gpa);
943 defer arena.deinit();905 defer arena.deinit();
944 const allocator = arena.allocator();906 const allocator = arena.allocator();
...@@ -950,63 +912,37 @@ fn ElfContents(comptime is_64: bool) type {...@@ -950,63 +912,37 @@ fn ElfContents(comptime is_64: bool) type {
950 // the program header is kept unchanged. (`strip` does update it, but `eu-strip` does not, and it still works)912 // the program header is kept unchanged. (`strip` does update it, but `eu-strip` does not, and it still works)
951913
952 const Update = struct {914 const Update = struct {
953 action: enum { keep, strip, empty },915 action: ElfFileHelper.Action,
954916
955 // remap the indexs after omitting the filtered sections917 // remap the indexs after omitting the filtered sections
956 remap_idx: u16,918 remap_idx: u16,
957919
958 // optionally overrides the payload from the source file920 // 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,
960 };923 };
961 const sections_update = try allocator.alloc(Update, self.sections.len);924 const sections_update = try allocator.alloc(Update, self.sections.len);
962 const new_shnum = blk: {925 const new_shnum = blk: {
963 var next_idx: u16 = 0;926 var next_idx: u16 = 0;
964 for (self.sections, sections_update) |section, *update| {927 for (self.sections, sections_update) |section, *update| {
965 update.action = action: {928 const action = ElfFileHelper.selectAction(section.category, options.section_filter);
966 if (section.usage == .none) break :action .strip;929 const remap_idx = idx: {
967 break :action switch (filter) {930 if (action == .strip) break :idx elf.SHN_UNDEF;
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;
997 next_idx += 1;931 next_idx += 1;
998 }932 break :idx next_idx - 1;
999933 };
1000 update.payload = null;934 update.* = Update{ .action = action, .remap_idx = remap_idx };
1001 }935 }
1002936
1003 if (debuglink != null)937 if (options.debuglink != null)
1004 next_idx += 1;938 next_idx += 1;
939
1005 break :blk next_idx;940 break :blk next_idx;
1006 };941 };
1007942
943 // add a ".gnu_debuglink" to the string table if needed
1008 const debuglink_name: u32 = blk: {944 const debuglink_name: u32 = blk: {
1009 if (debuglink == null) break :blk elf.SHN_UNDEF;945 if (options.debuglink == null) break :blk elf.SHN_UNDEF;
1010 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)946 if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF)
1011 fatal("zig objcopy: no strtab, cannot add the debuglink section", .{}); // TODO add the section if needed?947 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 {...@@ -1026,11 +962,7 @@ fn ElfContents(comptime is_64: bool) type {
1026 break :blk new_offset;962 break :blk new_offset;
1027 };963 };
1028964
1029 const WriteCmd = union(enum) {965 var cmdbuf = std.ArrayList(ElfFileHelper.WriteCmd).init(allocator);
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);
1034 defer cmdbuf.deinit();966 defer cmdbuf.deinit();
1035 try cmdbuf.ensureUnusedCapacity(3 + new_shnum);967 try cmdbuf.ensureUnusedCapacity(3 + new_shnum);
1036 var eof_offset: Elf_OffSize = 0; // track the end of the data written so far.968 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 {...@@ -1076,8 +1008,9 @@ fn ElfContents(comptime is_64: bool) type {
1076 if (update.action == .strip) continue;1008 if (update.action == .strip) continue;
1077 std.debug.assert(update.remap_idx == dest_section_idx);1009 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;
1080 const dest = &dest_sections[dest_section_idx];1012 const dest = &dest_sections[dest_section_idx];
1013 const payload = if (update.payload) |data| data else section.payload;
1081 dest_section_idx += 1;1014 dest_section_idx += 1;
10821015
1083 dest.* = src.*;1016 dest.* = src.*;
...@@ -1087,7 +1020,6 @@ fn ElfContents(comptime is_64: bool) type {...@@ -1087,7 +1020,6 @@ fn ElfContents(comptime is_64: bool) type {
1087 if ((src.sh_flags & elf.SHF_INFO_LINK) != 0 and src.sh_info != elf.SHN_UNDEF)1020 if ((src.sh_flags & elf.SHF_INFO_LINK) != 0 and src.sh_info != elf.SHN_UNDEF)
1088 dest.sh_info = sections_update[src.sh_info].remap_idx;1021 dest.sh_info = sections_update[src.sh_info].remap_idx;
10891022
1090 const payload = if (update.payload) |data| data else section.payload;
1091 if (payload) |data|1023 if (payload) |data|
1092 dest.sh_size = @intCast(Elf_OffSize, data.len);1024 dest.sh_size = @intCast(Elf_OffSize, data.len);
10931025
...@@ -1108,29 +1040,36 @@ fn ElfContents(comptime is_64: bool) type {...@@ -1108,29 +1040,36 @@ fn ElfContents(comptime is_64: bool) type {
1108 if (dest.sh_type != elf.SHT_NOBITS) {1040 if (dest.sh_type != elf.SHT_NOBITS) {
1109 if (payload) |src_data| {1041 if (payload) |src_data| {
1110 // update sections payload and write1042 // update sections payload and write
1111 const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len);1043 const dest_data = switch (src.sh_type) {
1112 std.mem.copy(u8, data, src_data);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 => {
1116 const defs = @ptrCast([*]Elf_Verdef, data)[0 .. @intCast(usize, src.sh_size) / @sizeOf(Elf_Verdef)];1048 const defs = @ptrCast([*]Elf_Verdef, data)[0 .. @intCast(usize, src.sh_size) / @sizeOf(Elf_Verdef)];
1117 for (defs) |*def| {1049 for (defs) |*def| {
1118 if (def.vd_ndx != elf.SHN_UNDEF)1050 if (def.vd_ndx != elf.SHN_UNDEF)
1119 def.vd_ndx = sections_update[src.sh_info].remap_idx;1051 def.vd_ndx = sections_update[src.sh_info].remap_idx;
1120 }1052 }
1053
1054 break :dst_data data;
1121 },1055 },
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
1123 const syms = @ptrCast([*]Elf_Sym, data)[0 .. @intCast(usize, src.sh_size) / @sizeOf(Elf_Sym)];1060 const syms = @ptrCast([*]Elf_Sym, data)[0 .. @intCast(usize, src.sh_size) / @sizeOf(Elf_Sym)];
1124 for (syms) |*sym| {1061 for (syms) |*sym| {
1125 if (sym.st_shndx != elf.SHN_UNDEF and sym.st_shndx < elf.SHN_LORESERVE)1062 if (sym.st_shndx != elf.SHN_UNDEF and sym.st_shndx < elf.SHN_LORESERVE)
1126 sym.st_shndx = sections_update[sym.st_shndx].remap_idx;1063 sym.st_shndx = sections_update[sym.st_shndx].remap_idx;
1127 }1064 }
1065
1066 break :dst_data data;
1128 },1067 },
1129 else => {},1068 else => src_data,
1130 }1069 };
11311070
1132 std.debug.assert(data.len == dest.sh_size);1071 std.debug.assert(dest_data.len == dest.sh_size);
1133 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = dest.sh_offset } });1072 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = dest_data, .out_offset = dest.sh_offset } });
1134 eof_offset = dest.sh_offset + dest.sh_size;1073 eof_offset = dest.sh_offset + dest.sh_size;
1135 } else {1074 } else {
1136 // direct contents copy1075 // direct contents copy
...@@ -1144,7 +1083,7 @@ fn ElfContents(comptime is_64: bool) type {...@@ -1144,7 +1083,7 @@ fn ElfContents(comptime is_64: bool) type {
1144 }1083 }
11451084
1146 // add a ".gnu_debuglink" section1085 // add a ".gnu_debuglink" section
1147 if (debuglink) |link| {1086 if (options.debuglink) |link| {
1148 const payload = payload: {1087 const payload = payload: {
1149 const crc_offset = std.mem.alignForward(link.name.len + 1, 4);1088 const crc_offset = std.mem.alignForward(link.name.len + 1, 4);
1150 const buf = try allocator.alignedAlloc(u8, 4, crc_offset + 4);1089 const buf = try allocator.alignedAlloc(u8, 4, crc_offset + 4);
...@@ -1188,71 +1127,7 @@ fn ElfContents(comptime is_64: bool) type {...@@ -1188,71 +1127,7 @@ fn ElfContents(comptime is_64: bool) type {
1188 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_shoff } });1127 cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_shoff } });
1189 }1128 }
11901129
1191 // consolidate holes between writes:1130 try ElfFileHelper.write(allocator, out_file, in_file, cmdbuf.items);
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 }
1256 }1131 }
12571132
1258 fn sectionWithinSegment(section: Elf_Shdr, segment: Elf_Phdr) bool {1133 fn sectionWithinSegment(section: Elf_Shdr, segment: Elf_Phdr) bool {
...@@ -1262,15 +1137,162 @@ fn ElfContents(comptime is_64: bool) type {...@@ -1262,15 +1137,162 @@ fn ElfContents(comptime is_64: bool) type {
1262 };1137 };
1263}1138}
12641139
1265fn computeFileCrc(file: File) !u32 {1140const ElfFileHelper = struct {
1266 var buf: [8000]u8 = undefined;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);1164 if (cur.* != cat) {
1269 var hasher = std.hash.Crc32.init();1165 cur.* = cat;
1270 while (true) {1166 return 1;
1271 const bytes_read = try file.read(&buf);1167 } else {
1272 if (bytes_read == 0) break;1168 return 0;
1273 hasher.update(buf[0..bytes_read]);1169 }
1274 }1170 }
1275 return hasher.final();1171
1276}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};