| ... | ... | @@ -104,9 +104,6 @@ pub fn cmdObjCopy( |
| 104 | 104 | fatal("unable to open '{s}': {s}", .{ input, @errorName(err) }); |
| 105 | 105 | defer in_file.close(); |
| 106 | 106 | |
| 107 | | var out_file = try fs.cwd().createFile(output, .{}); |
| 108 | | defer out_file.close(); |
| 109 | | |
| 110 | 107 | const elf_hdr = std.elf.Header.read(in_file) catch |err| switch (err) { |
| 111 | 108 | error.InvalidElfMagic => fatal("not an ELF file: '{s}'", .{input}), |
| 112 | 109 | else => fatal("unable to read '{s}': {s}", .{ input, @errorName(err) }), |
| ... | ... | @@ -126,6 +123,17 @@ pub fn cmdObjCopy( |
| 126 | 123 | } |
| 127 | 124 | }; |
| 128 | 125 | |
| 126 | const mode = mode: { |
| 127 | if (out_fmt != .elf or !only_keep_debug) |
| 128 | break :mode fs.File.default_mode; |
| 129 | if (in_file.stat()) |stat| |
| 130 | break :mode stat.mode |
| 131 | else |_| |
| 132 | break :mode fs.File.default_mode; |
| 133 | }; |
| 134 | var out_file = try fs.cwd().createFile(output, .{ .mode = mode }); |
| 135 | defer out_file.close(); |
| 136 | |
| 129 | 137 | switch (out_fmt) { |
| 130 | 138 | .hex, .raw => { |
| 131 | 139 | if (strip_debug or strip_all or only_keep_debug) |
| ... | ... | @@ -345,7 +353,7 @@ const BinaryElfOutput = struct { |
| 345 | 353 | |
| 346 | 354 | const shstrtab_shdr = (try section_headers.next()).?; |
| 347 | 355 | |
| 348 | | const buffer = try allocator.alloc(u8, @as(usize, @intCast(shstrtab_shdr.sh_size))); |
| 356 | const buffer = try allocator.alloc(u8, @intCast(shstrtab_shdr.sh_size)); |
| 349 | 357 | errdefer allocator.free(buffer); |
| 350 | 358 | |
| 351 | 359 | const num_read = try elf_file.preadAll(buffer, shstrtab_shdr.sh_offset); |
| ... | ... | @@ -363,7 +371,7 @@ const BinaryElfOutput = struct { |
| 363 | 371 | |
| 364 | 372 | newSection.binaryOffset = 0; |
| 365 | 373 | newSection.elfOffset = section.sh_offset; |
| 366 | | newSection.fileSize = @as(usize, @intCast(section.sh_size)); |
| 374 | newSection.fileSize = @intCast(section.sh_size); |
| 367 | 375 | newSection.segment = null; |
| 368 | 376 | |
| 369 | 377 | newSection.name = if (self.shstrtab) |shstrtab| |
| ... | ... | @@ -382,7 +390,7 @@ const BinaryElfOutput = struct { |
| 382 | 390 | |
| 383 | 391 | newSegment.physicalAddress = if (phdr.p_paddr != 0) phdr.p_paddr else phdr.p_vaddr; |
| 384 | 392 | newSegment.virtualAddress = phdr.p_vaddr; |
| 385 | | newSegment.fileSize = @as(usize, @intCast(phdr.p_filesz)); |
| 393 | newSegment.fileSize = @intCast(phdr.p_filesz); |
| 386 | 394 | newSegment.elfOffset = phdr.p_offset; |
| 387 | 395 | newSegment.binaryOffset = 0; |
| 388 | 396 | newSegment.firstSection = null; |
| ... | ... | @@ -478,8 +486,8 @@ const HexWriter = struct { |
| 478 | 486 | const MAX_PAYLOAD_LEN: u8 = 16; |
| 479 | 487 | |
| 480 | 488 | fn addressParts(address: u16) [2]u8 { |
| 481 | | const msb = @as(u8, @truncate(address >> 8)); |
| 482 | | const lsb = @as(u8, @truncate(address)); |
| 489 | const msb: u8 = @truncate(address >> 8); |
| 490 | const lsb: u8 = @truncate(address); |
| 483 | 491 | return [2]u8{ msb, lsb }; |
| 484 | 492 | } |
| 485 | 493 | |
| ... | ... | @@ -508,14 +516,14 @@ const HexWriter = struct { |
| 508 | 516 | |
| 509 | 517 | fn Data(address: u32, data: []const u8) Record { |
| 510 | 518 | return Record{ |
| 511 | | .address = @as(u16, @intCast(address % 0x10000)), |
| 519 | .address = @intCast(address % 0x10000), |
| 512 | 520 | .payload = .{ .Data = data }, |
| 513 | 521 | }; |
| 514 | 522 | } |
| 515 | 523 | |
| 516 | 524 | fn Address(address: u32) Record { |
| 517 | 525 | assert(address > 0xFFFF); |
| 518 | | const segment = @as(u16, @intCast(address / 0x10000)); |
| 526 | const segment: u16 = @intCast(address / 0x10000); |
| 519 | 527 | if (address > 0xFFFFF) { |
| 520 | 528 | return Record{ |
| 521 | 529 | .address = 0, |
| ... | ... | @@ -540,7 +548,7 @@ const HexWriter = struct { |
| 540 | 548 | fn checksum(self: Record) u8 { |
| 541 | 549 | const payload_bytes = self.getPayloadBytes(); |
| 542 | 550 | |
| 543 | | var sum: u8 = @as(u8, @intCast(payload_bytes.len)); |
| 551 | var sum: u8 = @intCast(payload_bytes.len); |
| 544 | 552 | const parts = addressParts(self.address); |
| 545 | 553 | sum +%= parts[0]; |
| 546 | 554 | sum +%= parts[1]; |
| ... | ... | @@ -574,10 +582,10 @@ const HexWriter = struct { |
| 574 | 582 | var buf: [MAX_PAYLOAD_LEN]u8 = undefined; |
| 575 | 583 | var bytes_read: usize = 0; |
| 576 | 584 | while (bytes_read < segment.fileSize) { |
| 577 | | const row_address = @as(u32, @intCast(segment.physicalAddress + bytes_read)); |
| 585 | const row_address: u32 = @intCast(segment.physicalAddress + bytes_read); |
| 578 | 586 | |
| 579 | 587 | const remaining = segment.fileSize - bytes_read; |
| 580 | | const to_read = @as(usize, @intCast(@min(remaining, MAX_PAYLOAD_LEN))); |
| 588 | const to_read: usize = @intCast(@min(remaining, MAX_PAYLOAD_LEN)); |
| 581 | 589 | const did_read = try elf_file.preadAll(buf[0..to_read], segment.elfOffset + bytes_read); |
| 582 | 590 | if (did_read < to_read) return error.UnexpectedEOF; |
| 583 | 591 | |
| ... | ... | @@ -593,7 +601,7 @@ const HexWriter = struct { |
| 593 | 601 | try Record.Address(address).write(self.out_file); |
| 594 | 602 | } |
| 595 | 603 | try record.write(self.out_file); |
| 596 | | self.prev_addr = @as(u32, @intCast(record.address + data.len)); |
| 604 | self.prev_addr = @intCast(record.address + data.len); |
| 597 | 605 | } |
| 598 | 606 | |
| 599 | 607 | fn writeEOF(self: HexWriter) File.WriteError!void { |
| ... | ... | @@ -814,7 +822,7 @@ fn ElfFile(comptime is_64: bool) type { |
| 814 | 822 | const need_strings = (idx == header.shstrndx); |
| 815 | 823 | |
| 816 | 824 | if (need_data or need_strings) { |
| 817 | | const buffer = try allocator.alignedAlloc(u8, section_memory_align, @as(usize, @intCast(section.section.sh_size))); |
| 825 | const buffer = try allocator.alignedAlloc(u8, section_memory_align, @intCast(section.section.sh_size)); |
| 818 | 826 | const bytes_read = try in_file.preadAll(buffer, section.section.sh_offset); |
| 819 | 827 | if (bytes_read != section.section.sh_size) return error.TRUNCATED_ELF; |
| 820 | 828 | section.payload = buffer; |
| ... | ... | @@ -935,7 +943,7 @@ fn ElfFile(comptime is_64: bool) type { |
| 935 | 943 | const update = &sections_update[self.raw_elf_header.e_shstrndx]; |
| 936 | 944 | |
| 937 | 945 | const name: []const u8 = ".gnu_debuglink"; |
| 938 | | const new_offset = @as(u32, @intCast(strtab.payload.?.len)); |
| 946 | const new_offset: u32 = @intCast(strtab.payload.?.len); |
| 939 | 947 | const buf = try allocator.alignedAlloc(u8, section_memory_align, new_offset + name.len + 1); |
| 940 | 948 | @memcpy(buf[0..new_offset], strtab.payload.?); |
| 941 | 949 | @memcpy(buf[new_offset..][0..name.len], name); |
| ... | ... | @@ -965,7 +973,7 @@ fn ElfFile(comptime is_64: bool) type { |
| 965 | 973 | update.payload = payload; |
| 966 | 974 | update.section = section.section; |
| 967 | 975 | update.section.?.sh_addralign = @alignOf(Elf_Chdr); |
| 968 | | update.section.?.sh_size = @as(Elf_OffSize, @intCast(payload.len)); |
| 976 | update.section.?.sh_size = @intCast(payload.len); |
| 969 | 977 | update.section.?.sh_flags |= elf.SHF_COMPRESSED; |
| 970 | 978 | } |
| 971 | 979 | } |
| ... | ... | @@ -1032,7 +1040,7 @@ fn ElfFile(comptime is_64: bool) type { |
| 1032 | 1040 | dest.sh_info = sections_update[src.sh_info].remap_idx; |
| 1033 | 1041 | |
| 1034 | 1042 | if (payload) |data| |
| 1035 | | dest.sh_size = @as(Elf_OffSize, @intCast(data.len)); |
| 1043 | dest.sh_size = @intCast(data.len); |
| 1036 | 1044 | |
| 1037 | 1045 | const addralign = if (src.sh_addralign == 0 or dest.sh_type == elf.SHT_NOBITS) 1 else src.sh_addralign; |
| 1038 | 1046 | dest.sh_offset = std.mem.alignForward(Elf_OffSize, eof_offset, addralign); |
| ... | ... | @@ -1110,7 +1118,7 @@ fn ElfFile(comptime is_64: bool) type { |
| 1110 | 1118 | .sh_flags = 0, |
| 1111 | 1119 | .sh_addr = 0, |
| 1112 | 1120 | .sh_offset = eof_offset, |
| 1113 | | .sh_size = @as(Elf_OffSize, @intCast(payload.len)), |
| 1121 | .sh_size = @intCast(payload.len), |
| 1114 | 1122 | .sh_link = elf.SHN_UNDEF, |
| 1115 | 1123 | .sh_info = elf.SHN_UNDEF, |
| 1116 | 1124 | .sh_addralign = 4, |
| ... | ... | @@ -1232,7 +1240,7 @@ const ElfFileHelper = struct { |
| 1232 | 1240 | fused_cmd = null; |
| 1233 | 1241 | } |
| 1234 | 1242 | if (data.out_offset > offset) { |
| 1235 | | consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@as(usize, @intCast(data.out_offset - offset))], .out_offset = offset } }); |
| 1243 | consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(data.out_offset - offset)], .out_offset = offset } }); |
| 1236 | 1244 | } |
| 1237 | 1245 | consolidated.appendAssumeCapacity(cmd); |
| 1238 | 1246 | offset = data.out_offset + data.data.len; |
| ... | ... | @@ -1249,7 +1257,7 @@ const ElfFileHelper = struct { |
| 1249 | 1257 | } else { |
| 1250 | 1258 | consolidated.appendAssumeCapacity(prev); |
| 1251 | 1259 | if (range.out_offset > offset) { |
| 1252 | | consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@as(usize, @intCast(range.out_offset - offset))], .out_offset = offset } }); |
| 1260 | consolidated.appendAssumeCapacity(.{ .write_data = .{ .data = zeroes[0..@intCast(range.out_offset - offset)], .out_offset = offset } }); |
| 1253 | 1261 | } |
| 1254 | 1262 | fused_cmd = cmd; |
| 1255 | 1263 | } |
| ... | ... | @@ -1286,7 +1294,7 @@ const ElfFileHelper = struct { |
| 1286 | 1294 | var section_reader = std.io.limitedReader(in_file.reader(), size); |
| 1287 | 1295 | |
| 1288 | 1296 | // allocate as large as decompressed data. if the compression doesn't fit, keep the data uncompressed. |
| 1289 | | const compressed_data = try allocator.alignedAlloc(u8, 8, @as(usize, @intCast(size))); |
| 1297 | const compressed_data = try allocator.alignedAlloc(u8, 8, @intCast(size)); |
| 1290 | 1298 | var compressed_stream = std.io.fixedBufferStream(compressed_data); |
| 1291 | 1299 | |
| 1292 | 1300 | try compressed_stream.writer().writeAll(prefix); |
| ... | ... | @@ -1317,7 +1325,7 @@ const ElfFileHelper = struct { |
| 1317 | 1325 | }; |
| 1318 | 1326 | } |
| 1319 | 1327 | |
| 1320 | | const compressed_len = @as(usize, @intCast(compressed_stream.getPos() catch unreachable)); |
| 1328 | const compressed_len: usize = @intCast(compressed_stream.getPos() catch unreachable); |
| 1321 | 1329 | const data = allocator.realloc(compressed_data, compressed_len) catch compressed_data; |
| 1322 | 1330 | return data[0..compressed_len]; |
| 1323 | 1331 | } |