| ... | @@ -13,6 +13,9 @@ const Server = std.zig.Server; | ... | @@ -13,6 +13,9 @@ const Server = std.zig.Server; |
| 13 | var stdin_buffer: [1024]u8 = undefined; | 13 | var stdin_buffer: [1024]u8 = undefined; |
| 14 | var stdout_buffer: [1024]u8 = undefined; | 14 | var stdout_buffer: [1024]u8 = undefined; |
| 15 | | 15 | |
| | 16 | var input_buffer: [1024]u8 = undefined; |
| | 17 | var output_buffer: [1024]u8 = undefined; |
| | 18 | |
| 16 | pub fn main() !void { | 19 | pub fn main() !void { |
| 17 | var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); | 20 | var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); |
| 18 | defer arena_instance.deinit(); | 21 | defer arena_instance.deinit(); |
| ... | @@ -145,13 +148,16 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void | ... | @@ -145,13 +148,16 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void |
| 145 | const input = opt_input orelse fatal("expected input parameter", .{}); | 148 | const input = opt_input orelse fatal("expected input parameter", .{}); |
| 146 | const output = opt_output orelse fatal("expected output parameter", .{}); | 149 | const output = opt_output orelse fatal("expected output parameter", .{}); |
| 147 | | 150 | |
| 148 | var in_file = fs.cwd().openFile(input, .{}) catch |err| | 151 | const input_file = fs.cwd().openFile(input, .{}) catch |err| fatal("failed to open {s}: {t}", .{ input, err }); |
| 149 | fatal("unable to open '{s}': {s}", .{ input, @errorName(err) }); | 152 | defer input_file.close(); |
| 150 | defer in_file.close(); | 153 | |
| | 154 | const stat = input_file.stat() catch |err| fatal("failed to stat {s}: {t}", .{ input, err }); |
| 151 | | 155 | |
| 152 | const elf_hdr = std.elf.Header.read(in_file) catch |err| switch (err) { | 156 | var in: File.Reader = .initSize(input_file, &input_buffer, stat.size); |
| 153 | error.InvalidElfMagic => fatal("not an ELF file: '{s}'", .{input}), | 157 | |
| 154 | else => fatal("unable to read '{s}': {s}", .{ input, @errorName(err) }), | 158 | const elf_hdr = std.elf.Header.read(&in.interface) catch |err| switch (err) { |
| | 159 | error.ReadFailed => fatal("unable to read {s}: {t}", .{ input, in.err.? }), |
| | 160 | else => |e| fatal("invalid elf file: {t}", .{e}), |
| 155 | }; | 161 | }; |
| 156 | | 162 | |
| 157 | const in_ofmt = .elf; | 163 | const in_ofmt = .elf; |
| ... | @@ -168,16 +174,12 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void | ... | @@ -168,16 +174,12 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void |
| 168 | } | 174 | } |
| 169 | }; | 175 | }; |
| 170 | | 176 | |
| 171 | const mode = mode: { | 177 | const mode = if (out_fmt != .elf or only_keep_debug) fs.File.default_mode else stat.mode; |
| 172 | if (out_fmt != .elf or only_keep_debug) | 178 | |
| 173 | break :mode fs.File.default_mode; | 179 | var output_file = try fs.cwd().createFile(output, .{ .mode = mode }); |
| 174 | if (in_file.stat()) |stat| | 180 | defer output_file.close(); |
| 175 | break :mode stat.mode | 181 | |
| 176 | else |_| | 182 | var out = output_file.writer(&output_buffer); |
| 177 | break :mode fs.File.default_mode; | | |
| 178 | }; | | |
| 179 | var out_file = try fs.cwd().createFile(output, .{ .mode = mode }); | | |
| 180 | defer out_file.close(); | | |
| 181 | | 183 | |
| 182 | switch (out_fmt) { | 184 | switch (out_fmt) { |
| 183 | .hex, .raw => { | 185 | .hex, .raw => { |
| ... | @@ -192,7 +194,7 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void | ... | @@ -192,7 +194,7 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void |
| 192 | if (set_section_flags != null) | 194 | if (set_section_flags != null) |
| 193 | fatal("zig objcopy: ELF to RAW or HEX copying does not support --set_section_flags", .{}); | 195 | fatal("zig objcopy: ELF to RAW or HEX copying does not support --set_section_flags", .{}); |
| 194 | | 196 | |
| 195 | try emitElf(arena, in_file, out_file, elf_hdr, .{ | 197 | try emitElf(arena, &in, &out, elf_hdr, .{ |
| 196 | .ofmt = out_fmt, | 198 | .ofmt = out_fmt, |
| 197 | .only_section = only_section, | 199 | .only_section = only_section, |
| 198 | .pad_to = pad_to, | 200 | .pad_to = pad_to, |
| ... | @@ -208,22 +210,13 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void | ... | @@ -208,22 +210,13 @@ fn cmdObjCopy(gpa: Allocator, arena: Allocator, args: []const []const u8) !void |
| 208 | if (pad_to) |_| | 210 | if (pad_to) |_| |
| 209 | fatal("zig objcopy: ELF to ELF copying does not support --pad-to", .{}); | 211 | fatal("zig objcopy: ELF to ELF copying does not support --pad-to", .{}); |
| 210 | | 212 | |
| 211 | try stripElf(arena, in_file, out_file, elf_hdr, .{ | 213 | fatal("unimplemented", .{}); |
| 212 | .strip_debug = strip_debug, | | |
| 213 | .strip_all = strip_all, | | |
| 214 | .only_keep_debug = only_keep_debug, | | |
| 215 | .add_debuglink = opt_add_debuglink, | | |
| 216 | .extract_to = opt_extract, | | |
| 217 | .compress_debug = compress_debug_sections, | | |
| 218 | .add_section = add_section, | | |
| 219 | .set_section_alignment = set_section_alignment, | | |
| 220 | .set_section_flags = set_section_flags, | | |
| 221 | }); | | |
| 222 | return std.process.cleanExit(); | | |
| 223 | }, | 214 | }, |
| 224 | else => fatal("unsupported output object format: {s}", .{@tagName(out_fmt)}), | 215 | else => fatal("unsupported output object format: {s}", .{@tagName(out_fmt)}), |
| 225 | } | 216 | } |
| 226 | | 217 | |
| | 218 | try out.end(); |
| | 219 | |
| 227 | if (listen) { | 220 | if (listen) { |
| 228 | var stdin_reader = fs.File.stdin().reader(&stdin_buffer); | 221 | var stdin_reader = fs.File.stdin().reader(&stdin_buffer); |
| 229 | var stdout_writer = fs.File.stdout().writer(&stdout_buffer); | 222 | var stdout_writer = fs.File.stdout().writer(&stdout_buffer); |
| ... | @@ -304,12 +297,12 @@ const SetSectionFlags = struct { | ... | @@ -304,12 +297,12 @@ const SetSectionFlags = struct { |
| 304 | | 297 | |
| 305 | fn emitElf( | 298 | fn emitElf( |
| 306 | arena: Allocator, | 299 | arena: Allocator, |
| 307 | in_file: File, | 300 | in: *File.Reader, |
| 308 | out_file: File, | 301 | out: *File.Writer, |
| 309 | elf_hdr: elf.Header, | 302 | elf_hdr: elf.Header, |
| 310 | options: EmitRawElfOptions, | 303 | options: EmitRawElfOptions, |
| 311 | ) !void { | 304 | ) !void { |
| 312 | var binary_elf_output = try BinaryElfOutput.parse(arena, in_file, elf_hdr); | 305 | var binary_elf_output = try BinaryElfOutput.parse(arena, in, elf_hdr); |
| 313 | defer binary_elf_output.deinit(); | 306 | defer binary_elf_output.deinit(); |
| 314 | | 307 | |
| 315 | if (options.ofmt == .elf) { | 308 | if (options.ofmt == .elf) { |
| ... | @@ -328,8 +321,8 @@ fn emitElf( | ... | @@ -328,8 +321,8 @@ fn emitElf( |
| 328 | continue; | 321 | continue; |
| 329 | } | 322 | } |
| 330 | | 323 | |
| 331 | try writeBinaryElfSection(in_file, out_file, section); | 324 | try writeBinaryElfSection(in, out, section); |
| 332 | try padFile(out_file, options.pad_to); | 325 | try padFile(out, options.pad_to); |
| 333 | return; | 326 | return; |
| 334 | } | 327 | } |
| 335 | }, | 328 | }, |
| ... | @@ -342,10 +335,10 @@ fn emitElf( | ... | @@ -342,10 +335,10 @@ fn emitElf( |
| 342 | switch (options.ofmt) { | 335 | switch (options.ofmt) { |
| 343 | .raw => { | 336 | .raw => { |
| 344 | for (binary_elf_output.sections.items) |section| { | 337 | for (binary_elf_output.sections.items) |section| { |
| 345 | try out_file.seekTo(section.binaryOffset); | 338 | try out.seekTo(section.binaryOffset); |
| 346 | try writeBinaryElfSection(in_file, out_file, section); | 339 | try writeBinaryElfSection(in, out, section); |
| 347 | } | 340 | } |
| 348 | try padFile(out_file, options.pad_to); | 341 | try padFile(out, options.pad_to); |
| 349 | }, | 342 | }, |
| 350 | .hex => { | 343 | .hex => { |
| 351 | if (binary_elf_output.segments.items.len == 0) return; | 344 | if (binary_elf_output.segments.items.len == 0) return; |
| ... | @@ -353,15 +346,15 @@ fn emitElf( | ... | @@ -353,15 +346,15 @@ fn emitElf( |
| 353 | return error.InvalidHexfileAddressRange; | 346 | return error.InvalidHexfileAddressRange; |
| 354 | } | 347 | } |
| 355 | | 348 | |
| 356 | var hex_writer = HexWriter{ .out_file = out_file }; | 349 | var hex_writer = HexWriter{ .out = out }; |
| 357 | for (binary_elf_output.segments.items) |segment| { | 350 | for (binary_elf_output.segments.items) |segment| { |
| 358 | try hex_writer.writeSegment(segment, in_file); | 351 | try hex_writer.writeSegment(segment, in); |
| 359 | } | 352 | } |
| 360 | if (options.pad_to) |_| { | 353 | if (options.pad_to) |_| { |
| 361 | // Padding to a size in hex files isn't applicable | 354 | // Padding to a size in hex files isn't applicable |
| 362 | return error.InvalidArgument; | 355 | return error.InvalidArgument; |
| 363 | } | 356 | } |
| 364 | try hex_writer.writeEOF(); | 357 | try hex_writer.writeEof(); |
| 365 | }, | 358 | }, |
| 366 | else => unreachable, | 359 | else => unreachable, |
| 367 | } | 360 | } |
| ... | @@ -399,7 +392,7 @@ const BinaryElfOutput = struct { | ... | @@ -399,7 +392,7 @@ const BinaryElfOutput = struct { |
| 399 | self.segments.deinit(self.allocator); | 392 | self.segments.deinit(self.allocator); |
| 400 | } | 393 | } |
| 401 | | 394 | |
| 402 | pub fn parse(allocator: Allocator, elf_file: File, elf_hdr: elf.Header) !Self { | 395 | pub fn parse(allocator: Allocator, in: *File.Reader, elf_hdr: elf.Header) !Self { |
| 403 | var self: Self = .{ | 396 | var self: Self = .{ |
| 404 | .segments = .{}, | 397 | .segments = .{}, |
| 405 | .sections = .{}, | 398 | .sections = .{}, |
| ... | @@ -412,7 +405,7 @@ const BinaryElfOutput = struct { | ... | @@ -412,7 +405,7 @@ const BinaryElfOutput = struct { |
| 412 | self.shstrtab = blk: { | 405 | self.shstrtab = blk: { |
| 413 | if (elf_hdr.shstrndx >= elf_hdr.shnum) break :blk null; | 406 | if (elf_hdr.shstrndx >= elf_hdr.shnum) break :blk null; |
| 414 | | 407 | |
| 415 | var section_headers = elf_hdr.section_header_iterator(&elf_file); | 408 | var section_headers = elf_hdr.iterateSectionHeaders(in); |
| 416 | | 409 | |
| 417 | var section_counter: usize = 0; | 410 | var section_counter: usize = 0; |
| 418 | while (section_counter < elf_hdr.shstrndx) : (section_counter += 1) { | 411 | while (section_counter < elf_hdr.shstrndx) : (section_counter += 1) { |
| ... | @@ -421,18 +414,13 @@ const BinaryElfOutput = struct { | ... | @@ -421,18 +414,13 @@ const BinaryElfOutput = struct { |
| 421 | | 414 | |
| 422 | const shstrtab_shdr = (try section_headers.next()).?; | 415 | const shstrtab_shdr = (try section_headers.next()).?; |
| 423 | | 416 | |
| 424 | const buffer = try allocator.alloc(u8, @intCast(shstrtab_shdr.sh_size)); | 417 | try in.seekTo(shstrtab_shdr.sh_offset); |
| 425 | errdefer allocator.free(buffer); | 418 | break :blk try in.interface.readAlloc(allocator, shstrtab_shdr.sh_size); |
| 426 | | | |
| 427 | const num_read = try elf_file.preadAll(buffer, shstrtab_shdr.sh_offset); | | |
| 428 | if (num_read != buffer.len) return error.EndOfStream; | | |
| 429 | | | |
| 430 | break :blk buffer; | | |
| 431 | }; | 419 | }; |
| 432 | | 420 | |
| 433 | errdefer if (self.shstrtab) |shstrtab| allocator.free(shstrtab); | 421 | errdefer if (self.shstrtab) |shstrtab| allocator.free(shstrtab); |
| 434 | | 422 | |
| 435 | var section_headers = elf_hdr.section_header_iterator(&elf_file); | 423 | var section_headers = elf_hdr.iterateSectionHeaders(in); |
| 436 | while (try section_headers.next()) |section| { | 424 | while (try section_headers.next()) |section| { |
| 437 | if (sectionValidForOutput(section)) { | 425 | if (sectionValidForOutput(section)) { |
| 438 | const newSection = try allocator.create(BinaryElfSection); | 426 | const newSection = try allocator.create(BinaryElfSection); |
| ... | @@ -451,7 +439,7 @@ const BinaryElfOutput = struct { | ... | @@ -451,7 +439,7 @@ const BinaryElfOutput = struct { |
| 451 | } | 439 | } |
| 452 | } | 440 | } |
| 453 | | 441 | |
| 454 | var program_headers = elf_hdr.program_header_iterator(&elf_file); | 442 | var program_headers = elf_hdr.iterateProgramHeaders(in); |
| 455 | while (try program_headers.next()) |phdr| { | 443 | while (try program_headers.next()) |phdr| { |
| 456 | if (phdr.p_type == elf.PT_LOAD) { | 444 | if (phdr.p_type == elf.PT_LOAD) { |
| 457 | const newSegment = try allocator.create(BinaryElfSegment); | 445 | const newSegment = try allocator.create(BinaryElfSegment); |
| ... | @@ -539,19 +527,17 @@ const BinaryElfOutput = struct { | ... | @@ -539,19 +527,17 @@ const BinaryElfOutput = struct { |
| 539 | } | 527 | } |
| 540 | }; | 528 | }; |
| 541 | | 529 | |
| 542 | fn writeBinaryElfSection(elf_file: File, out_file: File, section: *BinaryElfSection) !void { | 530 | fn writeBinaryElfSection(in: *File.Reader, out: *File.Writer, section: *BinaryElfSection) !void { |
| 543 | try out_file.writeFileAll(elf_file, .{ | 531 | try in.seekTo(section.elfOffset); |
| 544 | .in_offset = section.elfOffset, | 532 | _ = try out.interface.sendFileAll(in, .limited(section.fileSize)); |
| 545 | .in_len = section.fileSize, | | |
| 546 | }); | | |
| 547 | } | 533 | } |
| 548 | | 534 | |
| 549 | const HexWriter = struct { | 535 | const HexWriter = struct { |
| 550 | prev_addr: ?u32 = null, | 536 | prev_addr: ?u32 = null, |
| 551 | out_file: File, | 537 | out: *File.Writer, |
| 552 | | 538 | |
| 553 | /// Max data bytes per line of output | 539 | /// Max data bytes per line of output |
| 554 | const MAX_PAYLOAD_LEN: u8 = 16; | 540 | const max_payload_len: u8 = 16; |
| 555 | | 541 | |
| 556 | fn addressParts(address: u16) [2]u8 { | 542 | fn addressParts(address: u16) [2]u8 { |
| 557 | const msb: u8 = @truncate(address >> 8); | 543 | const msb: u8 = @truncate(address >> 8); |
| ... | @@ -627,13 +613,13 @@ const HexWriter = struct { | ... | @@ -627,13 +613,13 @@ const HexWriter = struct { |
| 627 | return (sum ^ 0xFF) +% 1; | 613 | return (sum ^ 0xFF) +% 1; |
| 628 | } | 614 | } |
| 629 | | 615 | |
| 630 | fn write(self: Record, file: File) File.WriteError!void { | 616 | fn write(self: Record, out: *File.Writer) !void { |
| 631 | const linesep = "\r\n"; | 617 | const linesep = "\r\n"; |
| 632 | // colon, (length, address, type, payload, checksum) as hex, CRLF | 618 | // colon, (length, address, type, payload, checksum) as hex, CRLF |
| 633 | const BUFSIZE = 1 + (1 + 2 + 1 + MAX_PAYLOAD_LEN + 1) * 2 + linesep.len; | 619 | const BUFSIZE = 1 + (1 + 2 + 1 + max_payload_len + 1) * 2 + linesep.len; |
| 634 | var outbuf: [BUFSIZE]u8 = undefined; | 620 | var outbuf: [BUFSIZE]u8 = undefined; |
| 635 | const payload_bytes = self.getPayloadBytes(); | 621 | const payload_bytes = self.getPayloadBytes(); |
| 636 | assert(payload_bytes.len <= MAX_PAYLOAD_LEN); | 622 | assert(payload_bytes.len <= max_payload_len); |
| 637 | | 623 | |
| 638 | const line = try std.fmt.bufPrint(&outbuf, ":{0X:0>2}{1X:0>4}{2X:0>2}{3X}{4X:0>2}" ++ linesep, .{ | 624 | const line = try std.fmt.bufPrint(&outbuf, ":{0X:0>2}{1X:0>4}{2X:0>2}{3X}{4X:0>2}" ++ linesep, .{ |
| 639 | @as(u8, @intCast(payload_bytes.len)), | 625 | @as(u8, @intCast(payload_bytes.len)), |
| ... | @@ -642,38 +628,37 @@ const HexWriter = struct { | ... | @@ -642,38 +628,37 @@ const HexWriter = struct { |
| 642 | payload_bytes, | 628 | payload_bytes, |
| 643 | self.checksum(), | 629 | self.checksum(), |
| 644 | }); | 630 | }); |
| 645 | try file.writeAll(line); | 631 | try out.interface.writeAll(line); |
| 646 | } | 632 | } |
| 647 | }; | 633 | }; |
| 648 | | 634 | |
| 649 | pub fn writeSegment(self: *HexWriter, segment: *const BinaryElfSegment, elf_file: File) !void { | 635 | pub fn writeSegment(self: *HexWriter, segment: *const BinaryElfSegment, in: *File.Reader) !void { |
| 650 | var buf: [MAX_PAYLOAD_LEN]u8 = undefined; | 636 | var buf: [max_payload_len]u8 = undefined; |
| 651 | var bytes_read: usize = 0; | 637 | var bytes_read: usize = 0; |
| 652 | while (bytes_read < segment.fileSize) { | 638 | while (bytes_read < segment.fileSize) { |
| 653 | const row_address: u32 = @intCast(segment.physicalAddress + bytes_read); | 639 | const row_address: u32 = @intCast(segment.physicalAddress + bytes_read); |
| 654 | | 640 | |
| 655 | const remaining = segment.fileSize - bytes_read; | 641 | const remaining = segment.fileSize - bytes_read; |
| 656 | const to_read: usize = @intCast(@min(remaining, MAX_PAYLOAD_LEN)); | 642 | const dest = buf[0..@min(remaining, max_payload_len)]; |
| 657 | const did_read = try elf_file.preadAll(buf[0..to_read], segment.elfOffset + bytes_read); | 643 | try in.seekTo(segment.elfOffset + bytes_read); |
| 658 | if (did_read < to_read) return error.UnexpectedEOF; | 644 | try in.interface.readSliceAll(dest); |
| | 645 | try self.writeDataRow(row_address, dest); |
| 659 | | 646 | |
| 660 | try self.writeDataRow(row_address, buf[0..did_read]); | 647 | bytes_read += dest.len; |
| 661 | | | |
| 662 | bytes_read += did_read; | | |
| 663 | } | 648 | } |
| 664 | } | 649 | } |
| 665 | | 650 | |
| 666 | fn writeDataRow(self: *HexWriter, address: u32, data: []const u8) File.WriteError!void { | 651 | fn writeDataRow(self: *HexWriter, address: u32, data: []const u8) !void { |
| 667 | const record = Record.Data(address, data); | 652 | const record = Record.Data(address, data); |
| 668 | if (address > 0xFFFF and (self.prev_addr == null or record.address != self.prev_addr.?)) { | 653 | if (address > 0xFFFF and (self.prev_addr == null or record.address != self.prev_addr.?)) { |
| 669 | try Record.Address(address).write(self.out_file); | 654 | try Record.Address(address).write(self.out); |
| 670 | } | 655 | } |
| 671 | try record.write(self.out_file); | 656 | try record.write(self.out); |
| 672 | self.prev_addr = @intCast(record.address + data.len); | 657 | self.prev_addr = @intCast(record.address + data.len); |
| 673 | } | 658 | } |
| 674 | | 659 | |
| 675 | fn writeEOF(self: HexWriter) File.WriteError!void { | 660 | fn writeEof(self: HexWriter) !void { |
| 676 | try Record.EOF().write(self.out_file); | 661 | try Record.EOF().write(self.out); |
| 677 | } | 662 | } |
| 678 | }; | 663 | }; |
| 679 | | 664 | |
| ... | @@ -686,9 +671,9 @@ fn containsValidAddressRange(segments: []*BinaryElfSegment) bool { | ... | @@ -686,9 +671,9 @@ fn containsValidAddressRange(segments: []*BinaryElfSegment) bool { |
| 686 | return true; | 671 | return true; |
| 687 | } | 672 | } |
| 688 | | 673 | |
| 689 | fn padFile(f: File, opt_size: ?u64) !void { | 674 | fn padFile(out: *File.Writer, opt_size: ?u64) !void { |
| 690 | const size = opt_size orelse return; | 675 | const size = opt_size orelse return; |
| 691 | try f.setEndPos(size); | 676 | try out.file.setEndPos(size); |
| 692 | } | 677 | } |
| 693 | | 678 | |
| 694 | test "HexWriter.Record.Address has correct payload and checksum" { | 679 | test "HexWriter.Record.Address has correct payload and checksum" { |
| ... | @@ -732,836 +717,6 @@ test "containsValidAddressRange" { | ... | @@ -732,836 +717,6 @@ test "containsValidAddressRange" { |
| 732 | try std.testing.expect(containsValidAddressRange(&buf)); | 717 | try std.testing.expect(containsValidAddressRange(&buf)); |
| 733 | } | 718 | } |
| 734 | | 719 | |
| 735 | // ------------- | | |
| 736 | // ELF to ELF stripping | | |
| 737 | | | |
| 738 | const StripElfOptions = struct { | | |
| 739 | extract_to: ?[]const u8 = null, | | |
| 740 | add_debuglink: ?[]const u8 = null, | | |
| 741 | strip_all: bool = false, | | |
| 742 | strip_debug: bool = false, | | |
| 743 | only_keep_debug: bool = false, | | |
| 744 | compress_debug: bool = false, | | |
| 745 | add_section: ?AddSection, | | |
| 746 | set_section_alignment: ?SetSectionAlignment, | | |
| 747 | set_section_flags: ?SetSectionFlags, | | |
| 748 | }; | | |
| 749 | | | |
| 750 | fn stripElf( | | |
| 751 | allocator: Allocator, | | |
| 752 | in_file: File, | | |
| 753 | out_file: File, | | |
| 754 | elf_hdr: elf.Header, | | |
| 755 | options: StripElfOptions, | | |
| 756 | ) !void { | | |
| 757 | const Filter = ElfFileHelper.Filter; | | |
| 758 | const DebugLink = ElfFileHelper.DebugLink; | | |
| 759 | | | |
| 760 | const filter: Filter = filter: { | | |
| 761 | if (options.only_keep_debug) break :filter .debug; | | |
| 762 | if (options.strip_all) break :filter .program; | | |
| 763 | if (options.strip_debug) break :filter .program_and_symbols; | | |
| 764 | break :filter .all; | | |
| 765 | }; | | |
| 766 | | | |
| 767 | const filter_complement: ?Filter = blk: { | | |
| 768 | if (options.extract_to) |_| { | | |
| 769 | break :blk switch (filter) { | | |
| 770 | .program => .debug_and_symbols, | | |
| 771 | .debug => .program_and_symbols, | | |
| 772 | .program_and_symbols => .debug, | | |
| 773 | .debug_and_symbols => .program, | | |
| 774 | .all => fatal("zig objcopy: nothing to extract", .{}), | | |
| 775 | }; | | |
| 776 | } else { | | |
| 777 | break :blk null; | | |
| 778 | } | | |
| 779 | }; | | |
| 780 | const debuglink_path = path: { | | |
| 781 | if (options.add_debuglink) |path| break :path path; | | |
| 782 | if (options.extract_to) |path| break :path path; | | |
| 783 | break :path null; | | |
| 784 | }; | | |
| 785 | | | |
| 786 | switch (elf_hdr.is_64) { | | |
| 787 | inline else => |is_64| { | | |
| 788 | var elf_file = try ElfFile(is_64).parse(allocator, in_file, elf_hdr); | | |
| 789 | defer elf_file.deinit(); | | |
| 790 | | | |
| 791 | if (options.add_section) |user_section| { | | |
| 792 | for (elf_file.sections) |section| { | | |
| 793 | if (std.mem.eql(u8, section.name, user_section.section_name)) { | | |
| 794 | fatal("zig objcopy: unable to add section '{s}'. Section already exists in input", .{user_section.section_name}); | | |
| 795 | } | | |
| 796 | } | | |
| 797 | } | | |
| 798 | | | |
| 799 | if (filter_complement) |flt| { | | |
| 800 | // write the .dbg file and close it, so it can be read back to compute the debuglink checksum. | | |
| 801 | const path = options.extract_to.?; | | |
| 802 | const dbg_file = std.fs.cwd().createFile(path, .{}) catch |err| { | | |
| 803 | fatal("zig objcopy: unable to create '{s}': {s}", .{ path, @errorName(err) }); | | |
| 804 | }; | | |
| 805 | defer dbg_file.close(); | | |
| 806 | | | |
| 807 | try elf_file.emit(allocator, dbg_file, in_file, .{ .section_filter = flt, .compress_debug = options.compress_debug }); | | |
| 808 | } | | |
| 809 | | | |
| 810 | const debuglink: ?DebugLink = if (debuglink_path) |path| ElfFileHelper.createDebugLink(path) else null; | | |
| 811 | try elf_file.emit(allocator, out_file, in_file, .{ | | |
| 812 | .section_filter = filter, | | |
| 813 | .debuglink = debuglink, | | |
| 814 | .compress_debug = options.compress_debug, | | |
| 815 | .add_section = options.add_section, | | |
| 816 | .set_section_alignment = options.set_section_alignment, | | |
| 817 | .set_section_flags = options.set_section_flags, | | |
| 818 | }); | | |
| 819 | }, | | |
| 820 | } | | |
| 821 | } | | |
| 822 | | | |
| 823 | // note: this is "a minimal effort implementation" | | |
| 824 | // It doesn't support all possibile elf files: some sections type may need fixups, the program header may need fix up, ... | | |
| 825 | // It was written for a specific use case (strip debug info to a sperate file, for linux 64-bits executables built with `zig` or `zig c++` ) | | |
| 826 | // It moves and reoders the sections as little as possible to avoid having to do fixups. | | |
| 827 | // TODO: support non-native endianess | | |
| 828 | | | |
| 829 | fn ElfFile(comptime is_64: bool) type { | | |
| 830 | const Elf_Ehdr = if (is_64) elf.Elf64_Ehdr else elf.Elf32_Ehdr; | | |
| 831 | const Elf_Phdr = if (is_64) elf.Elf64_Phdr else elf.Elf32_Phdr; | | |
| 832 | const Elf_Shdr = if (is_64) elf.Elf64_Shdr else elf.Elf32_Shdr; | | |
| 833 | const Elf_Chdr = if (is_64) elf.Elf64_Chdr else elf.Elf32_Chdr; | | |
| 834 | const Elf_Sym = if (is_64) elf.Elf64_Sym else elf.Elf32_Sym; | | |
| 835 | const Elf_OffSize = if (is_64) elf.Elf64_Off else elf.Elf32_Off; | | |
| 836 | | | |
| 837 | return struct { | | |
| 838 | raw_elf_header: Elf_Ehdr, | | |
| 839 | program_segments: []const Elf_Phdr, | | |
| 840 | sections: []const Section, | | |
| 841 | arena: std.heap.ArenaAllocator, | | |
| 842 | | | |
| 843 | const SectionCategory = ElfFileHelper.SectionCategory; | | |
| 844 | const section_memory_align: std.mem.Alignment = .of(Elf_Sym); // most restrictive of what we may load in memory | | |
| 845 | const Section = struct { | | |
| 846 | section: Elf_Shdr, | | |
| 847 | name: []const u8 = "", | | |
| 848 | segment: ?*const Elf_Phdr = null, // if the section is used by a program segment (there can be more than one) | | |
| 849 | payload: ?[]align(section_memory_align.toByteUnits()) const u8 = null, // if we need the data in memory | | |
| 850 | category: SectionCategory = .none, // should the section be kept in the exe or stripped to the debug database, or both. | | |
| 851 | }; | | |
| 852 | | | |
| 853 | const Self = @This(); | | |
| 854 | | | |
| 855 | pub fn parse(gpa: Allocator, in_file: File, header: elf.Header) !Self { | | |
| 856 | var arena = std.heap.ArenaAllocator.init(gpa); | | |
| 857 | errdefer arena.deinit(); | | |
| 858 | const allocator = arena.allocator(); | | |
| 859 | | | |
| 860 | var raw_header: Elf_Ehdr = undefined; | | |
| 861 | { | | |
| 862 | const bytes_read = try in_file.preadAll(std.mem.asBytes(&raw_header), 0); | | |
| 863 | if (bytes_read < @sizeOf(Elf_Ehdr)) | | |
| 864 | return error.TRUNCATED_ELF; | | |
| 865 | } | | |
| 866 | | | |
| 867 | // program header: list of segments | | |
| 868 | const program_segments = blk: { | | |
| 869 | if (@sizeOf(Elf_Phdr) != header.phentsize) | | |
| 870 | fatal("zig objcopy: unsupported ELF file, unexpected phentsize ({d})", .{header.phentsize}); | | |
| 871 | | | |
| 872 | const program_header = try allocator.alloc(Elf_Phdr, header.phnum); | | |
| 873 | const bytes_read = try in_file.preadAll(std.mem.sliceAsBytes(program_header), header.phoff); | | |
| 874 | if (bytes_read < @sizeOf(Elf_Phdr) * header.phnum) | | |
| 875 | return error.TRUNCATED_ELF; | | |
| 876 | break :blk program_header; | | |
| 877 | }; | | |
| 878 | | | |
| 879 | // section header | | |
| 880 | const sections = blk: { | | |
| 881 | if (@sizeOf(Elf_Shdr) != header.shentsize) | | |
| 882 | fatal("zig objcopy: unsupported ELF file, unexpected shentsize ({d})", .{header.shentsize}); | | |
| 883 | | | |
| 884 | const section_header = try allocator.alloc(Section, header.shnum); | | |
| 885 | | | |
| 886 | const raw_section_header = try allocator.alloc(Elf_Shdr, header.shnum); | | |
| 887 | defer allocator.free(raw_section_header); | | |
| 888 | const bytes_read = try in_file.preadAll(std.mem.sliceAsBytes(raw_section_header), header.shoff); | | |
| 889 | if (bytes_read < @sizeOf(Elf_Phdr) * header.shnum) | | |
| 890 | return error.TRUNCATED_ELF; | | |
| 891 | | | |
| 892 | for (section_header, raw_section_header) |*section, hdr| { | | |
| 893 | section.* = .{ .section = hdr }; | | |
| 894 | } | | |
| 895 | break :blk section_header; | | |
| 896 | }; | | |
| 897 | | | |
| 898 | // load data to memory for some sections: | | |
| 899 | // string tables for access | | |
| 900 | // sections than need modifications when other sections move. | | |
| 901 | for (sections, 0..) |*section, idx| { | | |
| 902 | const need_data = switch (section.section.sh_type) { | | |
| 903 | elf.DT_VERSYM => true, | | |
| 904 | elf.SHT_SYMTAB, elf.SHT_DYNSYM => true, | | |
| 905 | else => false, | | |
| 906 | }; | | |
| 907 | const need_strings = (idx == header.shstrndx); | | |
| 908 | | | |
| 909 | if (need_data or need_strings) { | | |
| 910 | const buffer = try allocator.alignedAlloc(u8, section_memory_align, @intCast(section.section.sh_size)); | | |
| 911 | const bytes_read = try in_file.preadAll(buffer, section.section.sh_offset); | | |
| 912 | if (bytes_read != section.section.sh_size) return error.TRUNCATED_ELF; | | |
| 913 | section.payload = buffer; | | |
| 914 | } | | |
| 915 | } | | |
| 916 | | | |
| 917 | // fill-in sections info: | | |
| 918 | // resolve the name | | |
| 919 | // find if a program segment uses the section | | |
| 920 | // categorize sections usage (used by program segments, debug datadase, common metadata, symbol table) | | |
| 921 | for (sections) |*section| { | | |
| 922 | section.segment = for (program_segments) |*seg| { | | |
| 923 | if (sectionWithinSegment(section.section, seg.*)) break seg; | | |
| 924 | } else null; | | |
| 925 | | | |
| 926 | if (section.section.sh_name != 0 and header.shstrndx != elf.SHN_UNDEF) | | |
| 927 | section.name = std.mem.span(@as([*:0]const u8, @ptrCast(&sections[header.shstrndx].payload.?[section.section.sh_name]))); | | |
| 928 | | | |
| 929 | const category_from_program: SectionCategory = if (section.segment != null) .exe else .debug; | | |
| 930 | section.category = switch (section.section.sh_type) { | | |
| 931 | elf.SHT_NOTE => .common, | | |
| 932 | elf.SHT_SYMTAB => .symbols, // "strip all" vs "strip only debug" | | |
| 933 | elf.SHT_DYNSYM => .exe, | | |
| 934 | elf.SHT_PROGBITS => cat: { | | |
| 935 | if (std.mem.eql(u8, section.name, ".comment")) break :cat .exe; | | |
| 936 | if (std.mem.eql(u8, section.name, ".gnu_debuglink")) break :cat .none; | | |
| 937 | break :cat category_from_program; | | |
| 938 | }, | | |
| 939 | elf.SHT_LOPROC...elf.SHT_HIPROC => .common, // don't strip unknown sections | | |
| 940 | elf.SHT_LOUSER...elf.SHT_HIUSER => .common, // don't strip unknown sections | | |
| 941 | else => category_from_program, | | |
| 942 | }; | | |
| 943 | } | | |
| 944 | | | |
| 945 | sections[0].category = .common; // mandatory null section | | |
| 946 | if (header.shstrndx != elf.SHN_UNDEF) | | |
| 947 | sections[header.shstrndx].category = .common; // string table for the headers | | |
| 948 | | | |
| 949 | // recursively propagate section categories to their linked sections, so that they are kept together | | |
| 950 | var dirty: u1 = 1; | | |
| 951 | while (dirty != 0) { | | |
| 952 | dirty = 0; | | |
| 953 | | | |
| 954 | for (sections) |*section| { | | |
| 955 | if (section.section.sh_link != elf.SHN_UNDEF) | | |
| 956 | dirty |= ElfFileHelper.propagateCategory(&sections[section.section.sh_link].category, section.category); | | |
| 957 | if ((section.section.sh_flags & elf.SHF_INFO_LINK) != 0 and section.section.sh_info != elf.SHN_UNDEF) | | |
| 958 | dirty |= ElfFileHelper.propagateCategory(&sections[section.section.sh_info].category, section.category); | | |
| 959 | } | | |
| 960 | } | | |
| 961 | | | |
| 962 | return Self{ | | |
| 963 | .arena = arena, | | |
| 964 | .raw_elf_header = raw_header, | | |
| 965 | .program_segments = program_segments, | | |
| 966 | .sections = sections, | | |
| 967 | }; | | |
| 968 | } | | |
| 969 | | | |
| 970 | pub fn deinit(self: *Self) void { | | |
| 971 | self.arena.deinit(); | | |
| 972 | } | | |
| 973 | | | |
| 974 | const Filter = ElfFileHelper.Filter; | | |
| 975 | const DebugLink = ElfFileHelper.DebugLink; | | |
| 976 | const EmitElfOptions = struct { | | |
| 977 | section_filter: Filter = .all, | | |
| 978 | debuglink: ?DebugLink = null, | | |
| 979 | compress_debug: bool = false, | | |
| 980 | add_section: ?AddSection = null, | | |
| 981 | set_section_alignment: ?SetSectionAlignment = null, | | |
| 982 | set_section_flags: ?SetSectionFlags = null, | | |
| 983 | }; | | |
| 984 | fn emit(self: *const Self, gpa: Allocator, out_file: File, in_file: File, options: EmitElfOptions) !void { | | |
| 985 | var arena = std.heap.ArenaAllocator.init(gpa); | | |
| 986 | defer arena.deinit(); | | |
| 987 | const allocator = arena.allocator(); | | |
| 988 | | | |
| 989 | // when emitting the stripped exe: | | |
| 990 | // - unused sections are removed | | |
| 991 | // when emitting the debug file: | | |
| 992 | // - all sections are kept, but some are emptied and their types is changed to SHT_NOBITS | | |
| 993 | // the program header is kept unchanged. (`strip` does update it, but `eu-strip` does not, and it still works) | | |
| 994 | | | |
| 995 | const Update = struct { | | |
| 996 | action: ElfFileHelper.Action, | | |
| 997 | | | |
| 998 | // remap the indexs after omitting the filtered sections | | |
| 999 | remap_idx: u16, | | |
| 1000 | | | |
| 1001 | // optionally overrides the payload from the source file | | |
| 1002 | payload: ?[]align(section_memory_align.toByteUnits()) const u8 = null, | | |
| 1003 | section: ?Elf_Shdr = null, | | |
| 1004 | }; | | |
| 1005 | const sections_update = try allocator.alloc(Update, self.sections.len); | | |
| 1006 | const new_shnum = blk: { | | |
| 1007 | var next_idx: u16 = 0; | | |
| 1008 | for (self.sections, sections_update) |section, *update| { | | |
| 1009 | const action = ElfFileHelper.selectAction(section.category, options.section_filter); | | |
| 1010 | const remap_idx = idx: { | | |
| 1011 | if (action == .strip) break :idx elf.SHN_UNDEF; | | |
| 1012 | next_idx += 1; | | |
| 1013 | break :idx next_idx - 1; | | |
| 1014 | }; | | |
| 1015 | update.* = Update{ .action = action, .remap_idx = remap_idx }; | | |
| 1016 | } | | |
| 1017 | | | |
| 1018 | if (options.debuglink != null) | | |
| 1019 | next_idx += 1; | | |
| 1020 | | | |
| 1021 | if (options.add_section != null) { | | |
| 1022 | next_idx += 1; | | |
| 1023 | } | | |
| 1024 | | | |
| 1025 | break :blk next_idx; | | |
| 1026 | }; | | |
| 1027 | | | |
| 1028 | // add a ".gnu_debuglink" to the string table if needed | | |
| 1029 | const debuglink_name: u32 = blk: { | | |
| 1030 | if (options.debuglink == null) break :blk elf.SHN_UNDEF; | | |
| 1031 | if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF) | | |
| 1032 | fatal("zig objcopy: no strtab, cannot add the debuglink section", .{}); // TODO add the section if needed? | | |
| 1033 | | | |
| 1034 | const strtab = &self.sections[self.raw_elf_header.e_shstrndx]; | | |
| 1035 | const update = &sections_update[self.raw_elf_header.e_shstrndx]; | | |
| 1036 | | | |
| 1037 | const name: []const u8 = ".gnu_debuglink"; | | |
| 1038 | const new_offset: u32 = @intCast(strtab.payload.?.len); | | |
| 1039 | const buf = try allocator.alignedAlloc(u8, section_memory_align, new_offset + name.len + 1); | | |
| 1040 | @memcpy(buf[0..new_offset], strtab.payload.?); | | |
| 1041 | @memcpy(buf[new_offset..][0..name.len], name); | | |
| 1042 | buf[new_offset + name.len] = 0; | | |
| 1043 | | | |
| 1044 | assert(update.action == .keep); | | |
| 1045 | update.payload = buf; | | |
| 1046 | | | |
| 1047 | break :blk new_offset; | | |
| 1048 | }; | | |
| 1049 | | | |
| 1050 | // add user section to the string table if needed | | |
| 1051 | const user_section_name: u32 = blk: { | | |
| 1052 | if (options.add_section == null) break :blk elf.SHN_UNDEF; | | |
| 1053 | if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF) | | |
| 1054 | fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed? | | |
| 1055 | | | |
| 1056 | const strtab = &self.sections[self.raw_elf_header.e_shstrndx]; | | |
| 1057 | const update = &sections_update[self.raw_elf_header.e_shstrndx]; | | |
| 1058 | | | |
| 1059 | const name = options.add_section.?.section_name; | | |
| 1060 | const new_offset: u32 = @intCast(strtab.payload.?.len); | | |
| 1061 | const buf = try allocator.alignedAlloc(u8, section_memory_align, new_offset + name.len + 1); | | |
| 1062 | @memcpy(buf[0..new_offset], strtab.payload.?); | | |
| 1063 | @memcpy(buf[new_offset..][0..name.len], name); | | |
| 1064 | buf[new_offset + name.len] = 0; | | |
| 1065 | | | |
| 1066 | assert(update.action == .keep); | | |
| 1067 | update.payload = buf; | | |
| 1068 | | | |
| 1069 | break :blk new_offset; | | |
| 1070 | }; | | |
| 1071 | | | |
| 1072 | // maybe compress .debug sections | | |
| 1073 | if (options.compress_debug) { | | |
| 1074 | for (self.sections[1..], sections_update[1..]) |section, *update| { | | |
| 1075 | if (update.action != .keep) continue; | | |
| 1076 | if (!std.mem.startsWith(u8, section.name, ".debug_")) continue; | | |
| 1077 | if ((section.section.sh_flags & elf.SHF_COMPRESSED) != 0) continue; // already compressed | | |
| 1078 | | | |
| 1079 | const chdr = Elf_Chdr{ | | |
| 1080 | .ch_type = elf.COMPRESS.ZLIB, | | |
| 1081 | .ch_size = section.section.sh_size, | | |
| 1082 | .ch_addralign = section.section.sh_addralign, | | |
| 1083 | }; | | |
| 1084 | | | |
| 1085 | const compressed_payload = try ElfFileHelper.tryCompressSection(allocator, in_file, section.section.sh_offset, section.section.sh_size, std.mem.asBytes(&chdr)); | | |
| 1086 | if (compressed_payload) |payload| { | | |
| 1087 | update.payload = payload; | | |
| 1088 | update.section = section.section; | | |
| 1089 | update.section.?.sh_addralign = @alignOf(Elf_Chdr); | | |
| 1090 | update.section.?.sh_size = @intCast(payload.len); | | |
| 1091 | update.section.?.sh_flags |= elf.SHF_COMPRESSED; | | |
| 1092 | } | | |
| 1093 | } | | |
| 1094 | } | | |
| 1095 | | | |
| 1096 | var cmdbuf = std.ArrayList(ElfFileHelper.WriteCmd).init(allocator); | | |
| 1097 | defer cmdbuf.deinit(); | | |
| 1098 | try cmdbuf.ensureUnusedCapacity(3 + new_shnum); | | |
| 1099 | var eof_offset: Elf_OffSize = 0; // track the end of the data written so far. | | |
| 1100 | | | |
| 1101 | // build the updated headers | | |
| 1102 | // nb: updated_elf_header will be updated before the actual write | | |
| 1103 | var updated_elf_header = self.raw_elf_header; | | |
| 1104 | if (updated_elf_header.e_shstrndx != elf.SHN_UNDEF) | | |
| 1105 | updated_elf_header.e_shstrndx = sections_update[updated_elf_header.e_shstrndx].remap_idx; | | |
| 1106 | cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = std.mem.asBytes(&updated_elf_header), .out_offset = 0 } }); | | |
| 1107 | eof_offset = @sizeOf(Elf_Ehdr); | | |
| 1108 | | | |
| 1109 | // program header as-is. | | |
| 1110 | // nb: for only-debug files, removing it appears to work, but is invalid by ELF specifcation. | | |
| 1111 | { | | |
| 1112 | assert(updated_elf_header.e_phoff == @sizeOf(Elf_Ehdr)); | | |
| 1113 | const data = std.mem.sliceAsBytes(self.program_segments); | | |
| 1114 | assert(data.len == @as(usize, updated_elf_header.e_phentsize) * updated_elf_header.e_phnum); | | |
| 1115 | cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_phoff } }); | | |
| 1116 | eof_offset = updated_elf_header.e_phoff + @as(Elf_OffSize, @intCast(data.len)); | | |
| 1117 | } | | |
| 1118 | | | |
| 1119 | // update sections and queue payload writes | | |
| 1120 | const updated_section_header = blk: { | | |
| 1121 | const dest_sections = try allocator.alloc(Elf_Shdr, new_shnum); | | |
| 1122 | | | |
| 1123 | { | | |
| 1124 | // the ELF format doesn't specify the order for all sections. | | |
| 1125 | // this code only supports when they are in increasing file order. | | |
| 1126 | var offset: u64 = eof_offset; | | |
| 1127 | for (self.sections[1..]) |section| { | | |
| 1128 | if (section.section.sh_type == elf.SHT_NOBITS) | | |
| 1129 | continue; | | |
| 1130 | if (section.section.sh_offset < offset) { | | |
| 1131 | fatal("zig objcopy: unsupported ELF file", .{}); | | |
| 1132 | } | | |
| 1133 | offset = section.section.sh_offset; | | |
| 1134 | } | | |
| 1135 | } | | |
| 1136 | | | |
| 1137 | dest_sections[0] = self.sections[0].section; | | |
| 1138 | | | |
| 1139 | var dest_section_idx: u32 = 1; | | |
| 1140 | for (self.sections[1..], sections_update[1..]) |section, update| { | | |
| 1141 | if (update.action == .strip) continue; | | |
| 1142 | assert(update.remap_idx == dest_section_idx); | | |
| 1143 | | | |
| 1144 | const src = if (update.section) |*s| s else &section.section; | | |
| 1145 | const dest = &dest_sections[dest_section_idx]; | | |
| 1146 | const payload = if (update.payload) |data| data else section.payload; | | |
| 1147 | dest_section_idx += 1; | | |
| 1148 | | | |
| 1149 | dest.* = src.*; | | |
| 1150 | | | |
| 1151 | if (src.sh_link != elf.SHN_UNDEF) | | |
| 1152 | dest.sh_link = sections_update[src.sh_link].remap_idx; | | |
| 1153 | if ((src.sh_flags & elf.SHF_INFO_LINK) != 0 and src.sh_info != elf.SHN_UNDEF) | | |
| 1154 | dest.sh_info = sections_update[src.sh_info].remap_idx; | | |
| 1155 | | | |
| 1156 | if (payload) |data| | | |
| 1157 | dest.sh_size = @intCast(data.len); | | |
| 1158 | | | |
| 1159 | const addralign = if (src.sh_addralign == 0 or dest.sh_type == elf.SHT_NOBITS) 1 else src.sh_addralign; | | |
| 1160 | dest.sh_offset = std.mem.alignForward(Elf_OffSize, eof_offset, addralign); | | |
| 1161 | if (src.sh_offset != dest.sh_offset and section.segment != null and update.action != .empty and dest.sh_type != elf.SHT_NOTE and dest.sh_type != elf.SHT_NOBITS) { | | |
| 1162 | if (src.sh_offset > dest.sh_offset) { | | |
| 1163 | dest.sh_offset = src.sh_offset; // add padding to avoid modifing the program segments | | |
| 1164 | } else { | | |
| 1165 | fatal("zig objcopy: cannot adjust program segments", .{}); | | |
| 1166 | } | | |
| 1167 | } | | |
| 1168 | assert(dest.sh_addr % addralign == dest.sh_offset % addralign); | | |
| 1169 | | | |
| 1170 | if (update.action == .empty) | | |
| 1171 | dest.sh_type = elf.SHT_NOBITS; | | |
| 1172 | | | |
| 1173 | if (dest.sh_type != elf.SHT_NOBITS) { | | |
| 1174 | if (payload) |src_data| { | | |
| 1175 | // update sections payload and write | | |
| 1176 | const dest_data = switch (src.sh_type) { | | |
| 1177 | elf.DT_VERSYM => dst_data: { | | |
| 1178 | const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len); | | |
| 1179 | @memcpy(data, src_data); | | |
| 1180 | | | |
| 1181 | const defs = @as([*]elf.Verdef, @ptrCast(data))[0 .. @as(usize, @intCast(src.sh_size)) / @sizeOf(elf.Verdef)]; | | |
| 1182 | for (defs) |*def| switch (def.ndx) { | | |
| 1183 | .LOCAL, .GLOBAL => {}, | | |
| 1184 | else => def.ndx = @enumFromInt(sections_update[src.sh_info].remap_idx), | | |
| 1185 | }; | | |
| 1186 | | | |
| 1187 | break :dst_data data; | | |
| 1188 | }, | | |
| 1189 | elf.SHT_SYMTAB, elf.SHT_DYNSYM => dst_data: { | | |
| 1190 | const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len); | | |
| 1191 | @memcpy(data, src_data); | | |
| 1192 | | | |
| 1193 | const syms = @as([*]Elf_Sym, @ptrCast(data))[0 .. @as(usize, @intCast(src.sh_size)) / @sizeOf(Elf_Sym)]; | | |
| 1194 | for (syms) |*sym| { | | |
| 1195 | if (sym.st_shndx != elf.SHN_UNDEF and sym.st_shndx < elf.SHN_LORESERVE) | | |
| 1196 | sym.st_shndx = sections_update[sym.st_shndx].remap_idx; | | |
| 1197 | } | | |
| 1198 | | | |
| 1199 | break :dst_data data; | | |
| 1200 | }, | | |
| 1201 | else => src_data, | | |
| 1202 | }; | | |
| 1203 | | | |
| 1204 | assert(dest_data.len == dest.sh_size); | | |
| 1205 | cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = dest_data, .out_offset = dest.sh_offset } }); | | |
| 1206 | eof_offset = dest.sh_offset + dest.sh_size; | | |
| 1207 | } else { | | |
| 1208 | // direct contents copy | | |
| 1209 | cmdbuf.appendAssumeCapacity(.{ .copy_range = .{ .in_offset = src.sh_offset, .len = dest.sh_size, .out_offset = dest.sh_offset } }); | | |
| 1210 | eof_offset = dest.sh_offset + dest.sh_size; | | |
| 1211 | } | | |
| 1212 | } else { | | |
| 1213 | // account for alignment padding even in empty sections to keep logical section order | | |
| 1214 | eof_offset = dest.sh_offset; | | |
| 1215 | } | | |
| 1216 | } | | |
| 1217 | | | |
| 1218 | // add a ".gnu_debuglink" section | | |
| 1219 | if (options.debuglink) |link| { | | |
| 1220 | const payload = payload: { | | |
| 1221 | const crc_offset = std.mem.alignForward(usize, link.name.len + 1, 4); | | |
| 1222 | const buf = try allocator.alignedAlloc(u8, .@"4", crc_offset + 4); | | |
| 1223 | @memcpy(buf[0..link.name.len], link.name); | | |
| 1224 | @memset(buf[link.name.len..crc_offset], 0); | | |
| 1225 | @memcpy(buf[crc_offset..], std.mem.asBytes(&link.crc32)); | | |
| 1226 | break :payload buf; | | |
| 1227 | }; | | |
| 1228 | | | |
| 1229 | dest_sections[dest_section_idx] = Elf_Shdr{ | | |
| 1230 | .sh_name = debuglink_name, | | |
| 1231 | .sh_type = elf.SHT_PROGBITS, | | |
| 1232 | .sh_flags = 0, | | |
| 1233 | .sh_addr = 0, | | |
| 1234 | .sh_offset = eof_offset, | | |
| 1235 | .sh_size = @intCast(payload.len), | | |
| 1236 | .sh_link = elf.SHN_UNDEF, | | |
| 1237 | .sh_info = elf.SHN_UNDEF, | | |
| 1238 | .sh_addralign = 4, | | |
| 1239 | .sh_entsize = 0, | | |
| 1240 | }; | | |
| 1241 | dest_section_idx += 1; | | |
| 1242 | | | |
| 1243 | cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = payload, .out_offset = eof_offset } }); | | |
| 1244 | eof_offset += @as(Elf_OffSize, @intCast(payload.len)); | | |
| 1245 | } | | |
| 1246 | | | |
| 1247 | // --add-section | | |
| 1248 | if (options.add_section) |add_section| { | | |
| 1249 | var section_file = fs.cwd().openFile(add_section.file_path, .{}) catch |err| | | |
| 1250 | fatal("unable to open '{s}': {s}", .{ add_section.file_path, @errorName(err) }); | | |
| 1251 | defer section_file.close(); | | |
| 1252 | | | |
| 1253 | const payload = try section_file.readToEndAlloc(arena.allocator(), std.math.maxInt(usize)); | | |
| 1254 | | | |
| 1255 | dest_sections[dest_section_idx] = Elf_Shdr{ | | |
| 1256 | .sh_name = user_section_name, | | |
| 1257 | .sh_type = elf.SHT_PROGBITS, | | |
| 1258 | .sh_flags = 0, | | |
| 1259 | .sh_addr = 0, | | |
| 1260 | .sh_offset = eof_offset, | | |
| 1261 | .sh_size = @intCast(payload.len), | | |
| 1262 | .sh_link = elf.SHN_UNDEF, | | |
| 1263 | .sh_info = elf.SHN_UNDEF, | | |
| 1264 | .sh_addralign = 4, | | |
| 1265 | .sh_entsize = 0, | | |
| 1266 | }; | | |
| 1267 | dest_section_idx += 1; | | |
| 1268 | | | |
| 1269 | cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = payload, .out_offset = eof_offset } }); | | |
| 1270 | eof_offset += @as(Elf_OffSize, @intCast(payload.len)); | | |
| 1271 | } | | |
| 1272 | | | |
| 1273 | assert(dest_section_idx == new_shnum); | | |
| 1274 | break :blk dest_sections; | | |
| 1275 | }; | | |
| 1276 | | | |
| 1277 | // --set-section-alignment: overwrite alignment | | |
| 1278 | if (options.set_section_alignment) |set_align| { | | |
| 1279 | if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF) | | |
| 1280 | fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed? | | |
| 1281 | | | |
| 1282 | const strtab = &sections_update[self.raw_elf_header.e_shstrndx]; | | |
| 1283 | for (updated_section_header) |*section| { | | |
| 1284 | const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name]))); | | |
| 1285 | if (std.mem.eql(u8, section_name, set_align.section_name)) { | | |
| 1286 | section.sh_addralign = set_align.alignment; | | |
| 1287 | break; | | |
| 1288 | } | | |
| 1289 | } else std.log.warn("Skipping --set-section-alignment. Section '{s}' not found", .{set_align.section_name}); | | |
| 1290 | } | | |
| 1291 | | | |
| 1292 | // --set-section-flags: overwrite flags | | |
| 1293 | if (options.set_section_flags) |set_flags| { | | |
| 1294 | if (self.raw_elf_header.e_shstrndx == elf.SHN_UNDEF) | | |
| 1295 | fatal("zig objcopy: no strtab, cannot add the user section", .{}); // TODO add the section if needed? | | |
| 1296 | | | |
| 1297 | const strtab = &sections_update[self.raw_elf_header.e_shstrndx]; | | |
| 1298 | for (updated_section_header) |*section| { | | |
| 1299 | const section_name = std.mem.span(@as([*:0]const u8, @ptrCast(&strtab.payload.?[section.sh_name]))); | | |
| 1300 | if (std.mem.eql(u8, section_name, set_flags.section_name)) { | | |
| 1301 | section.sh_flags = std.elf.SHF_WRITE; // default is writable cleared by "readonly" | | |
| 1302 | const f = set_flags.flags; | | |
| 1303 | | | |
| 1304 | // Supporting a subset of GNU and LLVM objcopy for ELF only | | |
| 1305 | // GNU: | | |
| 1306 | // alloc: add SHF_ALLOC | | |
| 1307 | // contents: if section is SHT_NOBITS, set SHT_PROGBITS, otherwise do nothing | | |
| 1308 | // load: if section is SHT_NOBITS, set SHT_PROGBITS, otherwise do nothing (same as contents) | | |
| 1309 | // noload: not ELF relevant | | |
| 1310 | // readonly: clear default SHF_WRITE flag | | |
| 1311 | // code: add SHF_EXECINSTR | | |
| 1312 | // data: not ELF relevant | | |
| 1313 | // rom: ignored | | |
| 1314 | // exclude: add SHF_EXCLUDE | | |
| 1315 | // share: not ELF relevant | | |
| 1316 | // debug: not ELF relevant | | |
| 1317 | // large: add SHF_X86_64_LARGE. Fatal error if target is not x86_64 | | |
| 1318 | if (f.alloc) section.sh_flags |= std.elf.SHF_ALLOC; | | |
| 1319 | if (f.contents or f.load) { | | |
| 1320 | if (section.sh_type == std.elf.SHT_NOBITS) section.sh_type = std.elf.SHT_PROGBITS; | | |
| 1321 | } | | |
| 1322 | if (f.readonly) section.sh_flags &= ~@as(@TypeOf(section.sh_type), std.elf.SHF_WRITE); | | |
| 1323 | if (f.code) section.sh_flags |= std.elf.SHF_EXECINSTR; | | |
| 1324 | if (f.exclude) section.sh_flags |= std.elf.SHF_EXCLUDE; | | |
| 1325 | if (f.large) { | | |
| 1326 | if (updated_elf_header.e_machine != std.elf.EM.X86_64) | | |
| 1327 | fatal("zig objcopy: 'large' section flag is only supported on x86_64 targets", .{}); | | |
| 1328 | section.sh_flags |= std.elf.SHF_X86_64_LARGE; | | |
| 1329 | } | | |
| 1330 | | | |
| 1331 | // LLVM: | | |
| 1332 | // merge: add SHF_MERGE | | |
| 1333 | // strings: add SHF_STRINGS | | |
| 1334 | if (f.merge) section.sh_flags |= std.elf.SHF_MERGE; | | |
| 1335 | if (f.strings) section.sh_flags |= std.elf.SHF_STRINGS; | | |
| 1336 | break; | | |
| 1337 | } | | |
| 1338 | } else std.log.warn("Skipping --set-section-flags. Section '{s}' not found", .{set_flags.section_name}); | | |
| 1339 | } | | |
| 1340 | | | |
| 1341 | // write the section header at the tail | | |
| 1342 | { | | |
| 1343 | const offset = std.mem.alignForward(Elf_OffSize, eof_offset, @alignOf(Elf_Shdr)); | | |
| 1344 | | | |
| 1345 | const data = std.mem.sliceAsBytes(updated_section_header); | | |
| 1346 | assert(data.len == @as(usize, updated_elf_header.e_shentsize) * new_shnum); | | |
| 1347 | updated_elf_header.e_shoff = offset; | | |
| 1348 | updated_elf_header.e_shnum = new_shnum; | | |
| 1349 | | | |
| 1350 | cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_shoff } }); | | |
| 1351 | } | | |
| 1352 | | | |
| 1353 | try ElfFileHelper.write(allocator, out_file, in_file, cmdbuf.items); | | |
| 1354 | } | | |
| 1355 | | | |
| 1356 | fn sectionWithinSegment(section: Elf_Shdr, segment: Elf_Phdr) bool { | | |
| 1357 | const file_size = if (section.sh_type == elf.SHT_NOBITS) 0 else section.sh_size; | | |
| 1358 | return segment.p_offset <= section.sh_offset and (segment.p_offset + segment.p_filesz) >= (section.sh_offset + file_size); | | |
| 1359 | } | | |
| 1360 | }; | | |
| 1361 | } | | |
| 1362 | | | |
| 1363 | const ElfFileHelper = struct { | | |
| 1364 | const DebugLink = struct { name: []const u8, crc32: u32 }; | | |
| 1365 | const Filter = enum { all, program, debug, program_and_symbols, debug_and_symbols }; | | |
| 1366 | | | |
| 1367 | const SectionCategory = enum { common, exe, debug, symbols, none }; | | |
| 1368 | fn propagateCategory(cur: *SectionCategory, new: SectionCategory) u1 { | | |
| 1369 | const cat: SectionCategory = switch (cur.*) { | | |
| 1370 | .none => new, | | |
| 1371 | .common => .common, | | |
| 1372 | .debug => switch (new) { | | |
| 1373 | .none, .debug => .debug, | | |
| 1374 | else => new, | | |
| 1375 | }, | | |
| 1376 | .exe => switch (new) { | | |
| 1377 | .common => .common, | | |
| 1378 | .none, .debug, .exe => .exe, | | |
| 1379 | .symbols => .exe, | | |
| 1380 | }, | | |
| 1381 | .symbols => switch (new) { | | |
| 1382 | .none, .common, .debug, .exe => unreachable, | | |
| 1383 | .symbols => .symbols, | | |
| 1384 | }, | | |
| 1385 | }; | | |
| 1386 | | | |
| 1387 | if (cur.* != cat) { | | |
| 1388 | cur.* = cat; | | |
| 1389 | return 1; | | |
| 1390 | } else { | | |
| 1391 | return 0; | | |
| 1392 | } | | |
| 1393 | } | | |
| 1394 | | | |
| 1395 | const Action = enum { keep, strip, empty }; | | |
| 1396 | fn selectAction(category: SectionCategory, filter: Filter) Action { | | |
| 1397 | if (category == .none) return .strip; | | |
| 1398 | return switch (filter) { | | |
| 1399 | .all => switch (category) { | | |
| 1400 | .none => .strip, | | |
| 1401 | else => .keep, | | |
| 1402 | }, | | |
| 1403 | .program => switch (category) { | | |
| 1404 | .common, .exe => .keep, | | |
| 1405 | else => .strip, | | |
| 1406 | }, | | |
| 1407 | .program_and_symbols => switch (category) { | | |
| 1408 | .common, .exe, .symbols => .keep, | | |
| 1409 | else => .strip, | | |
| 1410 | }, | | |
| 1411 | .debug => switch (category) { | | |
| 1412 | .exe, .symbols => .empty, | | |
| 1413 | .none => .strip, | | |
| 1414 | else => .keep, | | |
| 1415 | }, | | |
| 1416 | .debug_and_symbols => switch (category) { | | |
| 1417 | .exe => .empty, | | |
| 1418 | .none => .strip, | | |
| 1419 | else => .keep, | | |
| 1420 | }, | | |
| 1421 | }; | | |
| 1422 | } | | |
| 1423 | | | |
| 1424 | const WriteCmd = union(enum) { | | |
| 1425 | copy_range: struct { in_offset: u64, len: u64, out_offset: u64 }, | | |
| 1426 | write_data: struct { data: []const u8, out_offset: u64 }, | | |
| 1427 | }; | | |
| 1428 | fn write(allocator: Allocator, out_file: File, in_file: File, cmds: []const WriteCmd) !void { | | |
| 1429 | // consolidate holes between writes: | | |
| 1430 | // by coping original padding data from in_file (by fusing contiguous ranges) | | |
| 1431 | // by writing zeroes otherwise | | |
| 1432 | const zeroes = [1]u8{0} ** 4096; | | |
| 1433 | var consolidated = std.ArrayList(WriteCmd).init(allocator); | | |
| 1434 | defer consolidated.deinit(); | | |
| 1435 | try consolidated.ensureUnusedCapacity(cmds.len * 2); | | |
| 1436 | var offset: u64 = 0; | | |
| 1437 | var fused_cmd: ?WriteCmd = null; | | |
| 1438 | for (cmds) |cmd| { | | |
| 1439 | switch (cmd) { | | |
| 1440 | .write_data => |data| { | | |
| 1441 | assert(data.out_offset >= offset); | | |
| 1442 | if (fused_cmd) |prev| { | | |
| 1443 | consolidated.appendAssumeCapacity(prev); | | |
| 1444 | fused_cmd = null; | | |
| 1445 | } | | |
| 1446 | if (data.out_offset > offset) { | | |
| 1447 | consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(data.out_offset - offset)], .out_offset = offset } }); | | |
| 1448 | } | | |
| 1449 | consolidated.appendAssumeCapacity(cmd); | | |
| 1450 | offset = data.out_offset + data.data.len; | | |
| 1451 | }, | | |
| 1452 | .copy_range => |range| { | | |
| 1453 | assert(range.out_offset >= offset); | | |
| 1454 | if (fused_cmd) |prev| { | | |
| 1455 | if (range.in_offset >= prev.copy_range.in_offset + prev.copy_range.len and (range.out_offset - prev.copy_range.out_offset == range.in_offset - prev.copy_range.in_offset)) { | | |
| 1456 | fused_cmd = .{ .copy_range = .{ | | |
| 1457 | .in_offset = prev.copy_range.in_offset, | | |
| 1458 | .out_offset = prev.copy_range.out_offset, | | |
| 1459 | .len = (range.out_offset + range.len) - prev.copy_range.out_offset, | | |
| 1460 | } }; | | |
| 1461 | } else { | | |
| 1462 | consolidated.appendAssumeCapacity(prev); | | |
| 1463 | if (range.out_offset > offset) { | | |
| 1464 | consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(range.out_offset - offset)], .out_offset = offset } }); | | |
| 1465 | } | | |
| 1466 | fused_cmd = cmd; | | |
| 1467 | } | | |
| 1468 | } else { | | |
| 1469 | fused_cmd = cmd; | | |
| 1470 | } | | |
| 1471 | offset = range.out_offset + range.len; | | |
| 1472 | }, | | |
| 1473 | } | | |
| 1474 | } | | |
| 1475 | if (fused_cmd) |cmd| { | | |
| 1476 | consolidated.appendAssumeCapacity(cmd); | | |
| 1477 | } | | |
| 1478 | | | |
| 1479 | // write the output file | | |
| 1480 | for (consolidated.items) |cmd| { | | |
| 1481 | switch (cmd) { | | |
| 1482 | .write_data => |data| { | | |
| 1483 | var iovec = [_]std.posix.iovec_const{.{ .base = data.data.ptr, .len = data.data.len }}; | | |
| 1484 | try out_file.pwritevAll(&iovec, data.out_offset); | | |
| 1485 | }, | | |
| 1486 | .copy_range => |range| { | | |
| 1487 | const copied_bytes = try in_file.copyRangeAll(range.in_offset, out_file, range.out_offset, range.len); | | |
| 1488 | if (copied_bytes < range.len) return error.TRUNCATED_ELF; | | |
| 1489 | }, | | |
| 1490 | } | | |
| 1491 | } | | |
| 1492 | } | | |
| 1493 | | | |
| 1494 | fn tryCompressSection(allocator: Allocator, in_file: File, offset: u64, size: u64, prefix: []const u8) !?[]align(8) const u8 { | | |
| 1495 | if (size < prefix.len) return null; | | |
| 1496 | | | |
| 1497 | try in_file.seekTo(offset); | | |
| 1498 | var section_reader = std.io.limitedReader(in_file.deprecatedReader(), size); | | |
| 1499 | | | |
| 1500 | // allocate as large as decompressed data. if the compression doesn't fit, keep the data uncompressed. | | |
| 1501 | const compressed_data = try allocator.alignedAlloc(u8, .@"8", @intCast(size)); | | |
| 1502 | var compressed_stream = std.io.fixedBufferStream(compressed_data); | | |
| 1503 | | | |
| 1504 | try compressed_stream.writer().writeAll(prefix); | | |
| 1505 | | | |
| 1506 | { | | |
| 1507 | var compressor = try std.compress.zlib.compressor(compressed_stream.writer(), .{}); | | |
| 1508 | | | |
| 1509 | var buf: [8000]u8 = undefined; | | |
| 1510 | while (true) { | | |
| 1511 | const bytes_read = try section_reader.read(&buf); | | |
| 1512 | if (bytes_read == 0) break; | | |
| 1513 | const bytes_written = compressor.write(buf[0..bytes_read]) catch |err| switch (err) { | | |
| 1514 | error.NoSpaceLeft => { | | |
| 1515 | allocator.free(compressed_data); | | |
| 1516 | return null; | | |
| 1517 | }, | | |
| 1518 | else => return err, | | |
| 1519 | }; | | |
| 1520 | std.debug.assert(bytes_written == bytes_read); | | |
| 1521 | } | | |
| 1522 | compressor.finish() catch |err| switch (err) { | | |
| 1523 | error.NoSpaceLeft => { | | |
| 1524 | allocator.free(compressed_data); | | |
| 1525 | return null; | | |
| 1526 | }, | | |
| 1527 | else => return err, | | |
| 1528 | }; | | |
| 1529 | } | | |
| 1530 | | | |
| 1531 | const compressed_len: usize = @intCast(compressed_stream.getPos() catch unreachable); | | |
| 1532 | const data = allocator.realloc(compressed_data, compressed_len) catch compressed_data; | | |
| 1533 | return data[0..compressed_len]; | | |
| 1534 | } | | |
| 1535 | | | |
| 1536 | fn createDebugLink(path: []const u8) DebugLink { | | |
| 1537 | const file = std.fs.cwd().openFile(path, .{}) catch |err| { | | |
| 1538 | fatal("zig objcopy: could not open `{s}`: {s}\n", .{ path, @errorName(err) }); | | |
| 1539 | }; | | |
| 1540 | defer file.close(); | | |
| 1541 | | | |
| 1542 | const crc = ElfFileHelper.computeFileCrc(file) catch |err| { | | |
| 1543 | fatal("zig objcopy: could not read `{s}`: {s}\n", .{ path, @errorName(err) }); | | |
| 1544 | }; | | |
| 1545 | return .{ | | |
| 1546 | .name = std.fs.path.basename(path), | | |
| 1547 | .crc32 = crc, | | |
| 1548 | }; | | |
| 1549 | } | | |
| 1550 | | | |
| 1551 | fn computeFileCrc(file: File) !u32 { | | |
| 1552 | var buf: [8000]u8 = undefined; | | |
| 1553 | | | |
| 1554 | try file.seekTo(0); | | |
| 1555 | var hasher = std.hash.Crc32.init(); | | |
| 1556 | while (true) { | | |
| 1557 | const bytes_read = try file.read(&buf); | | |
| 1558 | if (bytes_read == 0) break; | | |
| 1559 | hasher.update(buf[0..bytes_read]); | | |
| 1560 | } | | |
| 1561 | return hasher.final(); | | |
| 1562 | } | | |
| 1563 | }; | | |
| 1564 | | | |
| 1565 | const SectionFlags = packed struct { | 720 | const SectionFlags = packed struct { |
| 1566 | alloc: bool = false, | 721 | alloc: bool = false, |
| 1567 | contents: bool = false, | 722 | contents: bool = false, |