| ... | @@ -20,8 +20,13 @@ pub fn cmdObjCopy( | ... | @@ -20,8 +20,13 @@ pub fn cmdObjCopy( |
| 20 | var opt_out_fmt: ?std.Target.ObjectFormat = null; | 20 | var opt_out_fmt: ?std.Target.ObjectFormat = null; |
| 21 | var opt_input: ?[]const u8 = null; | 21 | var opt_input: ?[]const u8 = null; |
| 22 | var opt_output: ?[]const u8 = null; | 22 | var opt_output: ?[]const u8 = null; |
| | 23 | var opt_extract: ?[]const u8 = null; |
| | 24 | var opt_add_debuglink: ?[]const u8 = null; |
| 23 | var only_section: ?[]const u8 = null; | 25 | var only_section: ?[]const u8 = null; |
| 24 | var pad_to: ?u64 = null; | 26 | var pad_to: ?u64 = null; |
| | 27 | var strip_all: bool = false; |
| | 28 | var strip_debug: bool = false; |
| | 29 | var only_keep_debug: bool = false; |
| 25 | var listen = false; | 30 | var listen = false; |
| 26 | while (i < args.len) : (i += 1) { | 31 | while (i < args.len) : (i += 1) { |
| 27 | const arg = args[i]; | 32 | const arg = args[i]; |
| ... | @@ -67,6 +72,24 @@ pub fn cmdObjCopy( | ... | @@ -67,6 +72,24 @@ pub fn cmdObjCopy( |
| 67 | pad_to = std.fmt.parseInt(u64, args[i], 0) catch |err| { | 72 | pad_to = std.fmt.parseInt(u64, args[i], 0) catch |err| { |
| 68 | fatal("unable to parse: '{s}': {s}", .{ args[i], @errorName(err) }); | 73 | fatal("unable to parse: '{s}': {s}", .{ args[i], @errorName(err) }); |
| 69 | }; | 74 | }; |
| | 75 | } else if (mem.eql(u8, arg, "-g") or mem.eql(u8, arg, "--strip-debug")) { |
| | 76 | strip_debug = true; |
| | 77 | } else if (mem.eql(u8, arg, "-S") or mem.eql(u8, arg, "--strip-all")) { |
| | 78 | strip_all = true; |
| | 79 | } else if (mem.eql(u8, arg, "--only-keep-debug")) { |
| | 80 | only_keep_debug = true; |
| | 81 | } else if (mem.startsWith(u8, arg, "--add-gnu-debuglink=")) { |
| | 82 | opt_add_debuglink = arg["--add-gnu-debuglink=".len..]; |
| | 83 | } else if (mem.eql(u8, arg, "--add-gnu-debuglink")) { |
| | 84 | i += 1; |
| | 85 | if (i >= args.len) fatal("expected another argument after '{s}'", .{arg}); |
| | 86 | opt_add_debuglink = args[i]; |
| | 87 | } else if (mem.startsWith(u8, arg, "--extract-to=")) { |
| | 88 | opt_extract = arg["--extract-to=".len..]; |
| | 89 | } else if (mem.eql(u8, arg, "--extract-to")) { |
| | 90 | i += 1; |
| | 91 | if (i >= args.len) fatal("expected another argument after '{s}'", .{arg}); |
| | 92 | opt_extract = args[i]; |
| 70 | } else { | 93 | } else { |
| 71 | fatal("unrecognized argument: '{s}'", .{arg}); | 94 | fatal("unrecognized argument: '{s}'", .{arg}); |
| 72 | } | 95 | } |
| ... | @@ -101,13 +124,37 @@ pub fn cmdObjCopy( | ... | @@ -101,13 +124,37 @@ pub fn cmdObjCopy( |
| 101 | }; | 124 | }; |
| 102 | | 125 | |
| 103 | switch (out_fmt) { | 126 | switch (out_fmt) { |
| 104 | .hex, .raw, .elf => { | 127 | .hex, .raw => { |
| | 128 | if (strip_debug or strip_all or only_keep_debug) |
| | 129 | fatal("zig objcopy: ELF to RAW or HEX copying does not support --strip", .{}); |
| | 130 | if (opt_extract != null) |
| | 131 | fatal("zig objcopy: ELF to RAW or HEX copying does not support --extract-to", .{}); |
| | 132 | |
| 105 | try emitElf(arena, in_file, out_file, elf_hdr, .{ | 133 | try emitElf(arena, in_file, out_file, elf_hdr, .{ |
| 106 | .ofmt = out_fmt, | 134 | .ofmt = out_fmt, |
| 107 | .only_section = only_section, | 135 | .only_section = only_section, |
| 108 | .pad_to = pad_to, | 136 | .pad_to = pad_to, |
| 109 | }); | 137 | }); |
| 110 | }, | 138 | }, |
| | 139 | .elf => { |
| | 140 | if (elf_hdr.endian != @import("builtin").target.cpu.arch.endian()) |
| | 141 | fatal("zig objcopy: ELF to ELF copying only supports native endian", .{}); |
| | 142 | if (elf_hdr.phoff == 0) // no program header |
| | 143 | fatal("zig objcopy: ELF to ELF copying only supports programs", .{}); |
| | 144 | if (only_section) |_| |
| | 145 | fatal("zig objcopy: ELF to ELF copying does not support --only-section", .{}); |
| | 146 | if (pad_to) |_| |
| | 147 | fatal("zig objcopy: ELF to ELF copying does not support --pad-to", .{}); |
| | 148 | |
| | 149 | try stripElf(arena, in_file, out_file, elf_hdr, .{ |
| | 150 | .strip_debug = strip_debug, |
| | 151 | .strip_all = strip_all, |
| | 152 | .only_keep_debug = only_keep_debug, |
| | 153 | .add_debuglink = opt_add_debuglink, |
| | 154 | .extract_to = opt_extract, |
| | 155 | }); |
| | 156 | return std.process.cleanExit(); |
| | 157 | }, |
| 111 | else => fatal("unsupported output object format: {s}", .{@tagName(out_fmt)}), | 158 | else => fatal("unsupported output object format: {s}", .{@tagName(out_fmt)}), |
| 112 | } | 159 | } |
| 113 | | 160 | |
| ... | @@ -152,12 +199,17 @@ const usage = | ... | @@ -152,12 +199,17 @@ const usage = |
| 152 | \\Usage: zig objcopy [options] input output | 199 | \\Usage: zig objcopy [options] input output |
| 153 | \\ | 200 | \\ |
| 154 | \\Options: | 201 | \\Options: |
| 155 | \\ -h, --help Print this help and exit | 202 | \\ -h, --help Print this help and exit |
| 156 | \\ --output-target=<value> Format of the output file | 203 | \\ --output-target=<value> Format of the output file |
| 157 | \\ -O <value> Alias for --output-target | 204 | \\ -O <value> Alias for --output-target |
| 158 | \\ --only-section=<section> Remove all but <section> | 205 | \\ --only-section=<section> Remove all but <section> |
| 159 | \\ -j <value> Alias for --only-section | 206 | \\ -j <value> Alias for --only-section |
| 160 | \\ --pad-to <addr> Pad the last section up to address <addr> | 207 | \\ --pad-to <addr> Pad the last section up to address <addr> |
| | 208 | \\ --strip-debug, -g Remove all debug sections from the output. |
| | 209 | \\ --strip-all, -S Remove all debug sections and symbol table from the output. |
| | 210 | \\ --only-keep-debug Strip a file, removing contents of any sections that would not be stripped by --strip-debug and leaving the debugging sections intact. |
| | 211 | \\ --add-gnu-debuglink=<file> Creates a .gnu_debuglink section which contains a reference to <file> and adds it to the output file. |
| | 212 | \\ --extract-to <file> Extract the removed sections into <file>, and add a .gnu-debuglink section. |
| 161 | \\ | 213 | \\ |
| 162 | ; | 214 | ; |
| 163 | | 215 | |
| ... | @@ -598,3 +650,649 @@ test "containsValidAddressRange" { | ... | @@ -598,3 +650,649 @@ test "containsValidAddressRange" { |
| 598 | segment.fileSize = 1; | 650 | segment.fileSize = 1; |
| 599 | try std.testing.expect(containsValidAddressRange(&buf)); | 651 | try std.testing.expect(containsValidAddressRange(&buf)); |
| 600 | } | 652 | } |
| | 653 | |
| | 654 | // ------------- |
| | 655 | // ELF to ELF stripping |
| | 656 | |
| | 657 | const StripElfOptions = struct { |
| | 658 | extract_to: ?[]const u8 = null, |
| | 659 | add_debuglink: ?[]const u8 = null, |
| | 660 | strip_all: bool = false, |
| | 661 | strip_debug: bool = false, |
| | 662 | only_keep_debug: bool = false, |
| | 663 | }; |
| | 664 | |
| | 665 | fn stripElf( |
| | 666 | allocator: Allocator, |
| | 667 | in_file: File, |
| | 668 | out_file: File, |
| | 669 | elf_hdr: elf.Header, |
| | 670 | options: StripElfOptions, |
| | 671 | ) !void { |
| | 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", .{}), |
| | 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 | }; |
| | 700 | |
| | 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) }); |
| | 711 | }; |
| | 712 | defer dbg_file.close(); |
| | 713 | |
| | 714 | try elf_file.emit(allocator, dbg_file, in_file, .{ .section_filter = flt }); |
| | 715 | } |
| | 716 | |
| | 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 }); |
| | 719 | }, |
| | 720 | } |
| | 721 | } |
| | 722 | |
| | 723 | // note: this is "a minimal effort implementation" |
| | 724 | // It doesn't support all possibile elf files: some sections type may need fixups, the program header may need fix up, ... |
| | 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. |
| | 727 | // TODO: support non-native endianess |
| | 728 | |
| | 729 | fn ElfFile(comptime is_64: bool) type { |
| | 730 | const Elf_Ehdr = if (is_64) elf.Elf64_Ehdr else elf.Elf32_Ehdr; |
| | 731 | const Elf_Phdr = if (is_64) elf.Elf64_Phdr else elf.Elf32_Phdr; |
| | 732 | const Elf_Shdr = if (is_64) elf.Elf64_Shdr else elf.Elf32_Shdr; |
| | 733 | const Elf_Sym = if (is_64) elf.Elf64_Sym else elf.Elf32_Sym; |
| | 734 | const Elf_Verdef = if (is_64) elf.Elf64_Verdef else elf.Elf32_Verdef; |
| | 735 | const Elf_OffSize = if (is_64) elf.Elf64_Off else elf.Elf32_Off; |
| | 736 | |
| | 737 | return struct { |
| | 738 | raw_elf_header: Elf_Ehdr, |
| | 739 | program_segments: []const Elf_Phdr, |
| | 740 | sections: []const Section, |
| | 741 | arena: std.heap.ArenaAllocator, |
| | 742 | |
| | 743 | const SectionCategory = ElfFileHelper.SectionCategory; |
| | 744 | const section_memory_align = @alignOf(Elf_Sym); // most restrictive of what we may load in memory |
| | 745 | const Section = struct { |
| | 746 | section: Elf_Shdr, |
| | 747 | name: []const u8 = "", |
| | 748 | segment: ?*const Elf_Phdr = null, // if the section is used by a program segment (there can be more than one) |
| | 749 | payload: ?[]align(section_memory_align) const u8 = null, // if we need the data in memory |
| | 750 | category: SectionCategory = .none, // should the section be kept in the exe or stripped to the debug database, or both. |
| | 751 | }; |
| | 752 | |
| | 753 | const Self = @This(); |
| | 754 | |
| | 755 | pub fn parse(gpa: Allocator, in_file: File, header: elf.Header) !Self { |
| | 756 | var arena = std.heap.ArenaAllocator.init(gpa); |
| | 757 | errdefer arena.deinit(); |
| | 758 | const allocator = arena.allocator(); |
| | 759 | |
| | 760 | var raw_header: Elf_Ehdr = undefined; |
| | 761 | { |
| | 762 | const bytes_read = try in_file.preadAll(std.mem.asBytes(&raw_header), 0); |
| | 763 | if (bytes_read < @sizeOf(Elf_Ehdr)) |
| | 764 | return error.TRUNCATED_ELF; |
| | 765 | } |
| | 766 | |
| | 767 | // program header: list of segments |
| | 768 | const program_segments = blk: { |
| | 769 | if (@sizeOf(Elf_Phdr) != header.phentsize) |
| | 770 | fatal("zig objcopy: unsuported ELF file, unexpected phentsize ({d})", .{header.phentsize}); |
| | 771 | |
| | 772 | const program_header = try allocator.alloc(Elf_Phdr, header.phnum); |
| | 773 | const bytes_read = try in_file.preadAll(std.mem.sliceAsBytes(program_header), header.phoff); |
| | 774 | if (bytes_read < @sizeOf(Elf_Phdr) * header.phnum) |
| | 775 | return error.TRUNCATED_ELF; |
| | 776 | break :blk program_header; |
| | 777 | }; |
| | 778 | |
| | 779 | // section header |
| | 780 | const sections = blk: { |
| | 781 | if (@sizeOf(Elf_Shdr) != header.shentsize) |
| | 782 | fatal("zig objcopy: unsuported ELF file, unexpected shentsize ({d})", .{header.shentsize}); |
| | 783 | |
| | 784 | const section_header = try allocator.alloc(Section, header.shnum); |
| | 785 | |
| | 786 | const raw_section_header = try allocator.alloc(Elf_Shdr, header.shnum); |
| | 787 | defer allocator.free(raw_section_header); |
| | 788 | const bytes_read = try in_file.preadAll(std.mem.sliceAsBytes(raw_section_header), header.shoff); |
| | 789 | if (bytes_read < @sizeOf(Elf_Phdr) * header.shnum) |
| | 790 | return error.TRUNCATED_ELF; |
| | 791 | |
| | 792 | for (section_header, raw_section_header) |*section, hdr| { |
| | 793 | section.* = .{ .section = hdr }; |
| | 794 | } |
| | 795 | break :blk section_header; |
| | 796 | }; |
| | 797 | |
| | 798 | // load data to memory for some sections: |
| | 799 | // string tables for access |
| | 800 | // sections than need modifications when other sections move. |
| | 801 | for (sections, 0..) |*section, idx| { |
| | 802 | const need_data = switch (section.section.sh_type) { |
| | 803 | elf.DT_VERSYM => true, |
| | 804 | elf.SHT_SYMTAB, elf.SHT_DYNSYM => true, |
| | 805 | else => false, |
| | 806 | }; |
| | 807 | const need_strings = (idx == header.shstrndx); |
| | 808 | |
| | 809 | if (need_data or need_strings) { |
| | 810 | const buffer = try allocator.alignedAlloc(u8, section_memory_align, @intCast(usize, section.section.sh_size)); |
| | 811 | const bytes_read = try in_file.preadAll(buffer, section.section.sh_offset); |
| | 812 | if (bytes_read != section.section.sh_size) return error.TRUNCATED_ELF; |
| | 813 | section.payload = buffer; |
| | 814 | } |
| | 815 | } |
| | 816 | |
| | 817 | // fill-in sections info: |
| | 818 | // resolve the name |
| | 819 | // find if a program segment uses the section |
| | 820 | // categorise sections usage (used by program segments, debug datadase, common metadata, symbol table) |
| | 821 | for (sections) |*section| { |
| | 822 | section.segment = for (program_segments) |*seg| { |
| | 823 | if (sectionWithinSegment(section.section, seg.*)) break seg; |
| | 824 | } else null; |
| | 825 | |
| | 826 | if (section.section.sh_name != 0 and header.shstrndx != elf.SHN_UNDEF) |
| | 827 | section.name = std.mem.span(@ptrCast([*:0]const u8, &sections[header.shstrndx].payload.?[section.section.sh_name])); |
| | 828 | |
| | 829 | const category_from_program: SectionCategory = if (section.segment != null) .exe else .debug; |
| | 830 | section.category = switch (section.section.sh_type) { |
| | 831 | elf.SHT_NOTE => .common, |
| | 832 | elf.SHT_SYMTAB => .symbols, // "strip all" vs "strip only debug" |
| | 833 | elf.SHT_DYNSYM => .exe, |
| | 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; |
| | 838 | }, |
| | 839 | elf.SHT_LOPROC...elf.SHT_HIPROC => .common, // don't strip unkonwn sections |
| | 840 | elf.SHT_LOUSER...elf.SHT_HIUSER => .common, // don't strip unkonwn sections |
| | 841 | else => category_from_program, |
| | 842 | }; |
| | 843 | } |
| | 844 | |
| | 845 | sections[0].category = .common; // mandatory null section |
| | 846 | if (header.shstrndx != elf.SHN_UNDEF) |
| | 847 | sections[header.shstrndx].category = .common; // string table for the headers |
| | 848 | |
| | 849 | // recursive dependencies |
| | 850 | var dirty: u1 = 1; |
| | 851 | while (dirty != 0) { |
| | 852 | dirty = 0; |
| | 853 | |
| | 854 | for (sections) |*section| { |
| | 855 | if (section.section.sh_link != elf.SHN_UNDEF) |
| | 856 | dirty |= ElfFileHelper.propagateCategory(&sections[section.section.sh_link].category, section.category); |
| | 857 | if ((section.section.sh_flags & elf.SHF_INFO_LINK) != 0 and section.section.sh_info != elf.SHN_UNDEF) |
| | 858 | dirty |= ElfFileHelper.propagateCategory(&sections[section.section.sh_info].category, section.category); |
| | 859 | |
| | 860 | if (section.payload) |data| { |
| | 861 | switch (section.section.sh_type) { |
| | 862 | elf.DT_VERSYM => { |
| | 863 | std.debug.assert(section.section.sh_entsize == @sizeOf(Elf_Verdef)); |
| | 864 | const defs = @ptrCast([*]const Elf_Verdef, data)[0 .. @intCast(usize, section.section.sh_size) / @sizeOf(Elf_Verdef)]; |
| | 865 | for (defs) |def| { |
| | 866 | if (def.vd_ndx != elf.SHN_UNDEF) |
| | 867 | dirty |= ElfFileHelper.propagateCategory(&sections[def.vd_ndx].category, section.category); |
| | 868 | } |
| | 869 | }, |
| | 870 | elf.SHT_SYMTAB, elf.SHT_DYNSYM => { |
| | 871 | std.debug.assert(section.section.sh_entsize == @sizeOf(Elf_Sym)); |
| | 872 | const syms = @ptrCast([*]const Elf_Sym, data)[0 .. @intCast(usize, section.section.sh_size) / @sizeOf(Elf_Sym)]; |
| | 873 | |
| | 874 | for (syms) |sym| { |
| | 875 | if (sym.st_shndx != elf.SHN_UNDEF and sym.st_shndx < elf.SHN_LORESERVE) |
| | 876 | dirty |= ElfFileHelper.propagateCategory(&sections[sym.st_shndx].category, section.category); |
| | 877 | } |
| | 878 | }, |
| | 879 | else => {}, |
| | 880 | } |
| | 881 | } |
| | 882 | } |
| | 883 | } |
| | 884 | |
| | 885 | return Self{ |
| | 886 | .arena = arena, |
| | 887 | .raw_elf_header = raw_header, |
| | 888 | .program_segments = program_segments, |
| | 889 | .sections = sections, |
| | 890 | }; |
| | 891 | } |
| | 892 | |
| | 893 | pub fn deinit(self: *Self) void { |
| | 894 | self.arena.deinit(); |
| | 895 | } |
| | 896 | |
| | 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 { |
| | 904 | var arena = std.heap.ArenaAllocator.init(gpa); |
| | 905 | defer arena.deinit(); |
| | 906 | const allocator = arena.allocator(); |
| | 907 | |
| | 908 | // when emitting the stripped exe: |
| | 909 | // - unused sections are removed |
| | 910 | // when emitting the debug file: |
| | 911 | // - all sections are kept, but some are emptied and their types is changed to SHT_NOBITS |
| | 912 | // the program header is kept unchanged. (`strip` does update it, but `eu-strip` does not, and it still works) |
| | 913 | |
| | 914 | const Update = struct { |
| | 915 | action: ElfFileHelper.Action, |
| | 916 | |
| | 917 | // remap the indexs after omitting the filtered sections |
| | 918 | remap_idx: u16, |
| | 919 | |
| | 920 | // optionally overrides the payload from the source file |
| | 921 | payload: ?[]align(section_memory_align) const u8 = null, |
| | 922 | section: ?Elf_Shdr = null, |
| | 923 | }; |
| | 924 | const sections_update = try allocator.alloc(Update, self.sections.len); |
| | 925 | const new_shnum = blk: { |
| | 926 | var next_idx: u16 = 0; |
| | 927 | for (self.sections, sections_update) |section, *update| { |
| | 928 | const action = ElfFileHelper.selectAction(section.category, options.section_filter); |
| | 929 | const remap_idx = idx: { |
| | 930 | if (action == .strip) break :idx elf.SHN_UNDEF; |
| | 931 | next_idx += 1; |
| | 932 | break :idx next_idx - 1; |
| | 933 | }; |
| | 934 | update.* = Update{ .action = action, .remap_idx = remap_idx }; |
| | 935 | } |
| | 936 | |
| | 937 | if (options.debuglink != null) |
| | 938 | next_idx += 1; |
| | 939 | |
| | 940 | break :blk next_idx; |
| | 941 | }; |
| | 942 | |
| | 943 | // add a ".gnu_debuglink" to the string table if needed |
| | 944 | const debuglink_name: u32 = blk: { |
| | 945 | if (options.debuglink == null) break :blk elf.SHN_UNDEF; |
| | 946 | if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF) |
| | 947 | fatal("zig objcopy: no strtab, cannot add the debuglink section", .{}); // TODO add the section if needed? |
| | 948 | |
| | 949 | const strtab = &self.sections[self.raw_elf_header.e_shstrndx]; |
| | 950 | const update = &sections_update[self.raw_elf_header.e_shstrndx]; |
| | 951 | |
| | 952 | const name: []const u8 = ".gnu_debuglink"; |
| | 953 | const new_offset = @intCast(u32, strtab.payload.?.len); |
| | 954 | const buf = try allocator.alignedAlloc(u8, section_memory_align, new_offset + name.len + 1); |
| | 955 | std.mem.copy(u8, buf[0..new_offset], strtab.payload.?); |
| | 956 | std.mem.copy(u8, buf[new_offset .. new_offset + name.len], name); |
| | 957 | buf[new_offset + name.len] = 0; |
| | 958 | |
| | 959 | std.debug.assert(update.action == .keep); |
| | 960 | update.payload = buf; |
| | 961 | |
| | 962 | break :blk new_offset; |
| | 963 | }; |
| | 964 | |
| | 965 | var cmdbuf = std.ArrayList(ElfFileHelper.WriteCmd).init(allocator); |
| | 966 | defer cmdbuf.deinit(); |
| | 967 | try cmdbuf.ensureUnusedCapacity(3 + new_shnum); |
| | 968 | var eof_offset: Elf_OffSize = 0; // track the end of the data written so far. |
| | 969 | |
| | 970 | // build the updated headers |
| | 971 | // nb: updated_elf_header will be updated before the actual write |
| | 972 | var updated_elf_header = self.raw_elf_header; |
| | 973 | if (updated_elf_header.e_shstrndx != elf.SHN_UNDEF) |
| | 974 | updated_elf_header.e_shstrndx = sections_update[updated_elf_header.e_shstrndx].remap_idx; |
| | 975 | cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = std.mem.asBytes(&updated_elf_header), .out_offset = 0 } }); |
| | 976 | eof_offset = @sizeOf(Elf_Ehdr); |
| | 977 | |
| | 978 | // program header as-is. |
| | 979 | // nb: for only-debug files, removing it appears to work, but is invalid by ELF specifcation. |
| | 980 | { |
| | 981 | std.debug.assert(updated_elf_header.e_phoff == @sizeOf(Elf_Ehdr)); |
| | 982 | const data = std.mem.sliceAsBytes(self.program_segments); |
| | 983 | std.debug.assert(data.len == @as(usize, updated_elf_header.e_phentsize) * updated_elf_header.e_phnum); |
| | 984 | cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_phoff } }); |
| | 985 | eof_offset = updated_elf_header.e_phoff + @intCast(Elf_OffSize, data.len); |
| | 986 | } |
| | 987 | |
| | 988 | // update sections and queue payload writes |
| | 989 | const updated_section_header = blk: { |
| | 990 | const dest_sections = try allocator.alloc(Elf_Shdr, new_shnum); |
| | 991 | |
| | 992 | { |
| | 993 | // the ELF format doesn't specify the order for all sections. |
| | 994 | // this code only supports when they are in increasing file order. |
| | 995 | var offset: u64 = eof_offset; |
| | 996 | for (self.sections[1..]) |section| { |
| | 997 | if (section.section.sh_offset < offset) { |
| | 998 | fatal("zig objcopy: unsuported ELF file", .{}); |
| | 999 | } |
| | 1000 | offset = section.section.sh_offset; |
| | 1001 | } |
| | 1002 | } |
| | 1003 | |
| | 1004 | dest_sections[0] = self.sections[0].section; |
| | 1005 | |
| | 1006 | var dest_section_idx: u32 = 1; |
| | 1007 | for (self.sections[1..], sections_update[1..]) |section, update| { |
| | 1008 | if (update.action == .strip) continue; |
| | 1009 | std.debug.assert(update.remap_idx == dest_section_idx); |
| | 1010 | |
| | 1011 | const src = if (update.section) |*s| s else &section.section; |
| | 1012 | const dest = &dest_sections[dest_section_idx]; |
| | 1013 | const payload = if (update.payload) |data| data else section.payload; |
| | 1014 | dest_section_idx += 1; |
| | 1015 | |
| | 1016 | dest.* = src.*; |
| | 1017 | |
| | 1018 | if (src.sh_link != elf.SHN_UNDEF) |
| | 1019 | dest.sh_link = sections_update[src.sh_link].remap_idx; |
| | 1020 | if ((src.sh_flags & elf.SHF_INFO_LINK) != 0 and src.sh_info != elf.SHN_UNDEF) |
| | 1021 | dest.sh_info = sections_update[src.sh_info].remap_idx; |
| | 1022 | |
| | 1023 | if (payload) |data| |
| | 1024 | dest.sh_size = @intCast(Elf_OffSize, data.len); |
| | 1025 | |
| | 1026 | const addralign = if (src.sh_addralign == 0 or dest.sh_type == elf.SHT_NOBITS) 1 else src.sh_addralign; |
| | 1027 | dest.sh_offset = std.mem.alignForwardGeneric(Elf_OffSize, eof_offset, addralign); |
| | 1028 | if (src.sh_offset != dest.sh_offset and section.segment != null and update.action != .empty and dest.sh_type != elf.SHT_NOTE) { |
| | 1029 | if (src.sh_offset > dest.sh_offset) { |
| | 1030 | dest.sh_offset = src.sh_offset; // add padding to avoid modifing the program segments |
| | 1031 | } else { |
| | 1032 | fatal("zig objcopy: cannot adjust program segments", .{}); |
| | 1033 | } |
| | 1034 | } |
| | 1035 | std.debug.assert(dest.sh_addr % addralign == dest.sh_offset % addralign); |
| | 1036 | |
| | 1037 | if (update.action == .empty) |
| | 1038 | dest.sh_type = elf.SHT_NOBITS; |
| | 1039 | |
| | 1040 | if (dest.sh_type != elf.SHT_NOBITS) { |
| | 1041 | if (payload) |src_data| { |
| | 1042 | // update sections payload and write |
| | 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); |
| | 1047 | |
| | 1048 | const defs = @ptrCast([*]Elf_Verdef, data)[0 .. @intCast(usize, src.sh_size) / @sizeOf(Elf_Verdef)]; |
| | 1049 | for (defs) |*def| { |
| | 1050 | if (def.vd_ndx != elf.SHN_UNDEF) |
| | 1051 | def.vd_ndx = sections_update[src.sh_info].remap_idx; |
| | 1052 | } |
| | 1053 | |
| | 1054 | break :dst_data data; |
| | 1055 | }, |
| | 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 | |
| | 1060 | const syms = @ptrCast([*]Elf_Sym, data)[0 .. @intCast(usize, src.sh_size) / @sizeOf(Elf_Sym)]; |
| | 1061 | for (syms) |*sym| { |
| | 1062 | if (sym.st_shndx != elf.SHN_UNDEF and sym.st_shndx < elf.SHN_LORESERVE) |
| | 1063 | sym.st_shndx = sections_update[sym.st_shndx].remap_idx; |
| | 1064 | } |
| | 1065 | |
| | 1066 | break :dst_data data; |
| | 1067 | }, |
| | 1068 | else => src_data, |
| | 1069 | }; |
| | 1070 | |
| | 1071 | std.debug.assert(dest_data.len == dest.sh_size); |
| | 1072 | cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = dest_data, .out_offset = dest.sh_offset } }); |
| | 1073 | eof_offset = dest.sh_offset + dest.sh_size; |
| | 1074 | } else { |
| | 1075 | // direct contents copy |
| | 1076 | cmdbuf.appendAssumeCapacity(.{ .copy_range = .{ .in_offset = src.sh_offset, .len = dest.sh_size, .out_offset = dest.sh_offset } }); |
| | 1077 | eof_offset = dest.sh_offset + dest.sh_size; |
| | 1078 | } |
| | 1079 | } else { |
| | 1080 | // account for alignment padding even in empty sections to keep logical section order |
| | 1081 | eof_offset = dest.sh_offset; |
| | 1082 | } |
| | 1083 | } |
| | 1084 | |
| | 1085 | // add a ".gnu_debuglink" section |
| | 1086 | if (options.debuglink) |link| { |
| | 1087 | const payload = payload: { |
| | 1088 | const crc_offset = std.mem.alignForward(link.name.len + 1, 4); |
| | 1089 | const buf = try allocator.alignedAlloc(u8, 4, crc_offset + 4); |
| | 1090 | std.mem.copy(u8, buf[0..link.name.len], link.name); |
| | 1091 | std.mem.set(u8, buf[link.name.len..crc_offset], 0); |
| | 1092 | std.mem.copy(u8, buf[crc_offset..], std.mem.asBytes(&link.crc32)); |
| | 1093 | break :payload buf; |
| | 1094 | }; |
| | 1095 | |
| | 1096 | dest_sections[dest_section_idx] = Elf_Shdr{ |
| | 1097 | .sh_name = debuglink_name, |
| | 1098 | .sh_type = elf.SHT_PROGBITS, |
| | 1099 | .sh_flags = 0, |
| | 1100 | .sh_addr = 0, |
| | 1101 | .sh_offset = eof_offset, |
| | 1102 | .sh_size = @intCast(Elf_OffSize, payload.len), |
| | 1103 | .sh_link = elf.SHN_UNDEF, |
| | 1104 | .sh_info = elf.SHN_UNDEF, |
| | 1105 | .sh_addralign = 4, |
| | 1106 | .sh_entsize = 0, |
| | 1107 | }; |
| | 1108 | dest_section_idx += 1; |
| | 1109 | |
| | 1110 | cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = payload, .out_offset = eof_offset } }); |
| | 1111 | eof_offset += @intCast(Elf_OffSize, payload.len); |
| | 1112 | } |
| | 1113 | |
| | 1114 | std.debug.assert(dest_section_idx == new_shnum); |
| | 1115 | break :blk dest_sections; |
| | 1116 | }; |
| | 1117 | |
| | 1118 | // write the section header at the tail |
| | 1119 | { |
| | 1120 | const offset = std.mem.alignForwardGeneric(Elf_OffSize, eof_offset, @alignOf(Elf_Shdr)); |
| | 1121 | |
| | 1122 | const data = std.mem.sliceAsBytes(updated_section_header); |
| | 1123 | std.debug.assert(data.len == @as(usize, updated_elf_header.e_shentsize) * new_shnum); |
| | 1124 | updated_elf_header.e_shoff = offset; |
| | 1125 | updated_elf_header.e_shnum = new_shnum; |
| | 1126 | |
| | 1127 | cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_shoff } }); |
| | 1128 | } |
| | 1129 | |
| | 1130 | try ElfFileHelper.write(allocator, out_file, in_file, cmdbuf.items); |
| | 1131 | } |
| | 1132 | |
| | 1133 | fn sectionWithinSegment(section: Elf_Shdr, segment: Elf_Phdr) bool { |
| | 1134 | const file_size = if (section.sh_type == elf.SHT_NOBITS) 0 else section.sh_size; |
| | 1135 | return segment.p_offset <= section.sh_offset and (segment.p_offset + segment.p_filesz) >= (section.sh_offset + file_size); |
| | 1136 | } |
| | 1137 | }; |
| | 1138 | } |
| | 1139 | |
| | 1140 | const 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 | }; |
| | 1163 | |
| | 1164 | if (cur.* != cat) { |
| | 1165 | cur.* = cat; |
| | 1166 | return 1; |
| | 1167 | } else { |
| | 1168 | return 0; |
| | 1169 | } |
| | 1170 | } |
| | 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 | }; |