authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-30 15:11:10+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-09-04 13:34:25+02:00
log3e100c5daba0f64695eab0bb4216b9d242229ba6
treeeb9e1be7edf581852d9740b9c405c2aac0e2df2b
parentda60159d85f2b23e35b62f0626163d798413916f

dwarf: make Section.off a function


4 files changed, 56 insertions(+), 49 deletions(-)

src/link/Dwarf.zig+35-28
...@@ -261,7 +261,6 @@ pub const Section = struct {...@@ -261,7 +261,6 @@ pub const Section = struct {
261 index: u32,261 index: u32,
262 first: Unit.Index.Optional,262 first: Unit.Index.Optional,
263 last: Unit.Index.Optional,263 last: Unit.Index.Optional,
264 off: u64,
265 len: u64,264 len: u64,
266 units: std.ArrayListUnmanaged(Unit),265 units: std.ArrayListUnmanaged(Unit),
267266
...@@ -284,9 +283,8 @@ pub const Section = struct {...@@ -284,9 +283,8 @@ pub const Section = struct {
284 .index = std.math.maxInt(u32),283 .index = std.math.maxInt(u32),
285 .first = .none,284 .first = .none,
286 .last = .none,285 .last = .none,
287 .off = 0,
288 .len = 0,
289 .units = .{},286 .units = .{},
287 .len = 0,
290 };288 };
291289
292 fn deinit(sec: *Section, gpa: std.mem.Allocator) void {290 fn deinit(sec: *Section, gpa: std.mem.Allocator) void {
...@@ -295,6 +293,20 @@ pub const Section = struct {...@@ -295,6 +293,20 @@ pub const Section = struct {
295 sec.* = undefined;293 sec.* = undefined;
296 }294 }
297295
296 fn off(sec: Section, dwarf: *Dwarf) u64 {
297 if (dwarf.bin_file.cast(.elf)) |elf_file| {
298 const zo = elf_file.zigObjectPtr().?;
299 const atom = zo.symbol(sec.index).atom(elf_file).?;
300 return atom.offset(elf_file);
301 } else if (dwarf.bin_file.cast(.macho)) |macho_file| {
302 const header = if (macho_file.d_sym) |d_sym|
303 d_sym.sections.items[sec.index]
304 else
305 macho_file.sections.items(.header)[sec.index];
306 return header.offset;
307 } else unreachable;
308 }
309
298 fn addUnit(sec: *Section, header_len: u32, trailer_len: u32, dwarf: *Dwarf) UpdateError!Unit.Index {310 fn addUnit(sec: *Section, header_len: u32, trailer_len: u32, dwarf: *Dwarf) UpdateError!Unit.Index {
299 const unit: Unit.Index = @enumFromInt(sec.units.items.len);311 const unit: Unit.Index = @enumFromInt(sec.units.items.len);
300 const unit_ptr = try sec.units.addOne(dwarf.gpa);312 const unit_ptr = try sec.units.addOne(dwarf.gpa);
...@@ -306,9 +318,9 @@ pub const Section = struct {...@@ -306,9 +318,9 @@ pub const Section = struct {
306 .next = .none,318 .next = .none,
307 .first = .none,319 .first = .none,
308 .last = .none,320 .last = .none,
309 .off = 0,
310 .header_len = aligned_header_len,321 .header_len = aligned_header_len,
311 .trailer_len = aligned_trailer_len,322 .trailer_len = aligned_trailer_len,
323 .off = 0,
312 .len = aligned_header_len + aligned_trailer_len,324 .len = aligned_header_len + aligned_trailer_len,
313 .entries = .{},325 .entries = .{},
314 .cross_unit_relocs = .{},326 .cross_unit_relocs = .{},
...@@ -385,7 +397,6 @@ pub const Section = struct {...@@ -385,7 +397,6 @@ pub const Section = struct {
385 const shdr = elf_file.sections.items(.shdr)[shndx];397 const shdr = elf_file.sections.items(.shdr)[shndx];
386 atom.size = shdr.sh_size;398 atom.size = shdr.sh_size;
387 atom.alignment = InternPool.Alignment.fromNonzeroByteUnits(shdr.sh_addralign);399 atom.alignment = InternPool.Alignment.fromNonzeroByteUnits(shdr.sh_addralign);
388 sec.off = shdr.sh_offset + @as(u64, @intCast(atom.value));
389 sec.len = shdr.sh_size;400 sec.len = shdr.sh_size;
390 } else if (dwarf.bin_file.cast(.macho)) |macho_file| {401 } else if (dwarf.bin_file.cast(.macho)) |macho_file| {
391 const header = if (macho_file.d_sym) |*d_sym| header: {402 const header = if (macho_file.d_sym) |*d_sym| header: {
...@@ -395,7 +406,6 @@ pub const Section = struct {...@@ -395,7 +406,6 @@ pub const Section = struct {
395 try macho_file.growSection(@intCast(sec.index), len);406 try macho_file.growSection(@intCast(sec.index), len);
396 break :header &macho_file.sections.items(.header)[sec.index];407 break :header &macho_file.sections.items(.header)[sec.index];
397 };408 };
398 sec.off = header.offset;
399 sec.len = header.size;409 sec.len = header.size;
400 }410 }
401 }411 }
...@@ -404,7 +414,6 @@ pub const Section = struct {...@@ -404,7 +414,6 @@ pub const Section = struct {
404 const len = sec.getUnit(sec.first.unwrap() orelse return).off;414 const len = sec.getUnit(sec.first.unwrap() orelse return).off;
405 if (len == 0) return;415 if (len == 0) return;
406 for (sec.units.items) |*unit| unit.off -= len;416 for (sec.units.items) |*unit| unit.off -= len;
407 sec.off += len;
408 sec.len -= len;417 sec.len -= len;
409 if (dwarf.bin_file.cast(.elf)) |elf_file| {418 if (dwarf.bin_file.cast(.elf)) |elf_file| {
410 const zo = elf_file.zigObjectPtr().?;419 const zo = elf_file.zigObjectPtr().?;
...@@ -412,14 +421,14 @@ pub const Section = struct {...@@ -412,14 +421,14 @@ pub const Section = struct {
412 const shndx = atom.output_section_index;421 const shndx = atom.output_section_index;
413 const shdr = &elf_file.sections.items(.shdr)[shndx];422 const shdr = &elf_file.sections.items(.shdr)[shndx];
414 atom.size = sec.len;423 atom.size = sec.len;
415 shdr.sh_offset = sec.off;424 shdr.sh_offset += len;
416 shdr.sh_size = sec.len;425 shdr.sh_size = sec.len;
417 } else if (dwarf.bin_file.cast(.macho)) |macho_file| {426 } else if (dwarf.bin_file.cast(.macho)) |macho_file| {
418 const header = if (macho_file.d_sym) |*d_sym|427 const header = if (macho_file.d_sym) |*d_sym|
419 &d_sym.sections.items[sec.index]428 &d_sym.sections.items[sec.index]
420 else429 else
421 &macho_file.sections.items(.header)[sec.index];430 &macho_file.sections.items(.header)[sec.index];
422 header.offset = @intCast(sec.off);431 header.offset += @intCast(len);
423 header.size = sec.len;432 header.size = sec.len;
424 }433 }
425 }434 }
...@@ -548,9 +557,9 @@ const Unit = struct {...@@ -548,9 +557,9 @@ const Unit = struct {
548 fn move(unit: *Unit, sec: *Section, dwarf: *Dwarf, new_off: u32) UpdateError!void {557 fn move(unit: *Unit, sec: *Section, dwarf: *Dwarf, new_off: u32) UpdateError!void {
549 if (unit.off == new_off) return;558 if (unit.off == new_off) return;
550 if (try dwarf.getFile().?.copyRangeAll(559 if (try dwarf.getFile().?.copyRangeAll(
551 sec.off + unit.off,560 sec.off(dwarf) + unit.off,
552 dwarf.getFile().?,561 dwarf.getFile().?,
553 sec.off + new_off,562 sec.off(dwarf) + new_off,
554 unit.len,563 unit.len,
555 ) != unit.len) return error.InputOutput;564 ) != unit.len) return error.InputOutput;
556 unit.off = new_off;565 unit.off = new_off;
...@@ -582,7 +591,7 @@ const Unit = struct {...@@ -582,7 +591,7 @@ const Unit = struct {
582591
583 fn replaceHeader(unit: *Unit, sec: *Section, dwarf: *Dwarf, contents: []const u8) UpdateError!void {592 fn replaceHeader(unit: *Unit, sec: *Section, dwarf: *Dwarf, contents: []const u8) UpdateError!void {
584 assert(contents.len == unit.header_len);593 assert(contents.len == unit.header_len);
585 try dwarf.getFile().?.pwriteAll(contents, sec.off + unit.off);594 try dwarf.getFile().?.pwriteAll(contents, sec.off(dwarf) + unit.off);
586 }595 }
587596
588 fn writeTrailer(unit: *Unit, sec: *Section, dwarf: *Dwarf) UpdateError!void {597 fn writeTrailer(unit: *Unit, sec: *Section, dwarf: *Dwarf) UpdateError!void {
...@@ -614,7 +623,7 @@ const Unit = struct {...@@ -614,7 +623,7 @@ const Unit = struct {
614 assert(fbs.pos == extended_op_bytes + op_len_bytes);623 assert(fbs.pos == extended_op_bytes + op_len_bytes);
615 writer.writeByte(DW.LNE.padding) catch unreachable;624 writer.writeByte(DW.LNE.padding) catch unreachable;
616 assert(fbs.pos >= unit.trailer_len and fbs.pos <= len);625 assert(fbs.pos >= unit.trailer_len and fbs.pos <= len);
617 return dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off + start);626 return dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off(dwarf) + start);
618 }627 }
619 var trailer = try std.ArrayList(u8).initCapacity(dwarf.gpa, len);628 var trailer = try std.ArrayList(u8).initCapacity(dwarf.gpa, len);
620 defer trailer.deinit();629 defer trailer.deinit();
...@@ -673,11 +682,11 @@ const Unit = struct {...@@ -673,11 +682,11 @@ const Unit = struct {
673 assert(trailer.items.len == unit.trailer_len);682 assert(trailer.items.len == unit.trailer_len);
674 trailer.appendNTimesAssumeCapacity(fill_byte, len - unit.trailer_len);683 trailer.appendNTimesAssumeCapacity(fill_byte, len - unit.trailer_len);
675 assert(trailer.items.len == len);684 assert(trailer.items.len == len);
676 try dwarf.getFile().?.pwriteAll(trailer.items, sec.off + start);685 try dwarf.getFile().?.pwriteAll(trailer.items, sec.off(dwarf) + start);
677 }686 }
678687
679 fn resolveRelocs(unit: *Unit, sec: *Section, dwarf: *Dwarf) RelocError!void {688 fn resolveRelocs(unit: *Unit, sec: *Section, dwarf: *Dwarf) RelocError!void {
680 const unit_off = sec.off + unit.off;689 const unit_off = sec.off(dwarf) + unit.off;
681 for (unit.cross_unit_relocs.items) |reloc| {690 for (unit.cross_unit_relocs.items) |reloc| {
682 const target_unit = sec.getUnit(reloc.target_unit);691 const target_unit = sec.getUnit(reloc.target_unit);
683 try dwarf.resolveReloc(692 try dwarf.resolveReloc(
...@@ -764,12 +773,12 @@ const Entry = struct {...@@ -764,12 +773,12 @@ const Entry = struct {
764 dwarf.writeInt(unit_len[0..dwarf.sectionOffsetBytes()], len - dwarf.unitLengthBytes());773 dwarf.writeInt(unit_len[0..dwarf.sectionOffsetBytes()], len - dwarf.unitLengthBytes());
765 try dwarf.getFile().?.pwriteAll(774 try dwarf.getFile().?.pwriteAll(
766 unit_len[0..dwarf.sectionOffsetBytes()],775 unit_len[0..dwarf.sectionOffsetBytes()],
767 sec.off + unit.off + unit.header_len + entry.off,776 sec.off(dwarf) + unit.off + unit.header_len + entry.off,
768 );777 );
769 const buf = try dwarf.gpa.alloc(u8, len - entry.len);778 const buf = try dwarf.gpa.alloc(u8, len - entry.len);
770 defer dwarf.gpa.free(buf);779 defer dwarf.gpa.free(buf);
771 @memset(buf, DW.CFA.nop);780 @memset(buf, DW.CFA.nop);
772 try dwarf.getFile().?.pwriteAll(buf, sec.off + unit.off + unit.header_len + start);781 try dwarf.getFile().?.pwriteAll(buf, sec.off(dwarf) + unit.off + unit.header_len + start);
773 return;782 return;
774 }783 }
775 const len = unit.getEntry(entry.next.unwrap() orelse return).off - start;784 const len = unit.getEntry(entry.next.unwrap() orelse return).off - start;
...@@ -825,7 +834,7 @@ const Entry = struct {...@@ -825,7 +834,7 @@ const Entry = struct {
825 },834 },
826 } else assert(!sec.pad_to_ideal and len == 0);835 } else assert(!sec.pad_to_ideal and len == 0);
827 assert(fbs.pos <= len);836 assert(fbs.pos <= len);
828 try dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off + unit.off + unit.header_len + start);837 try dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off(dwarf) + unit.off + unit.header_len + start);
829 }838 }
830839
831 fn resize(entry_ptr: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf, len: u32) UpdateError!void {840 fn resize(entry_ptr: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf, len: u32) UpdateError!void {
...@@ -860,15 +869,15 @@ const Entry = struct {...@@ -860,15 +869,15 @@ const Entry = struct {
860869
861 fn replace(entry_ptr: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf, contents: []const u8) UpdateError!void {870 fn replace(entry_ptr: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf, contents: []const u8) UpdateError!void {
862 assert(contents.len == entry_ptr.len);871 assert(contents.len == entry_ptr.len);
863 try dwarf.getFile().?.pwriteAll(contents, sec.off + unit.off + unit.header_len + entry_ptr.off);872 try dwarf.getFile().?.pwriteAll(contents, sec.off(dwarf) + unit.off + unit.header_len + entry_ptr.off);
864 if (false) {873 if (false) {
865 const buf = try dwarf.gpa.alloc(u8, sec.len);874 const buf = try dwarf.gpa.alloc(u8, sec.len);
866 defer dwarf.gpa.free(buf);875 defer dwarf.gpa.free(buf);
867 _ = try dwarf.getFile().?.preadAll(buf, sec.off);876 _ = try dwarf.getFile().?.preadAll(buf, sec.off(dwarf));
868 log.info("Section{{ .first = {}, .last = {}, .off = 0x{x}, .len = 0x{x} }}", .{877 log.info("Section{{ .first = {}, .last = {}, .off = 0x{x}, .len = 0x{x} }}", .{
869 @intFromEnum(sec.first),878 @intFromEnum(sec.first),
870 @intFromEnum(sec.last),879 @intFromEnum(sec.last),
871 sec.off,880 sec.off(dwarf),
872 sec.len,881 sec.len,
873 });882 });
874 for (sec.units.items) |*unit_ptr| {883 for (sec.units.items) |*unit_ptr| {
...@@ -935,7 +944,7 @@ const Entry = struct {...@@ -935,7 +944,7 @@ const Entry = struct {
935 }944 }
936945
937 fn resolveRelocs(entry: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf) RelocError!void {946 fn resolveRelocs(entry: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf) RelocError!void {
938 const entry_off = sec.off + unit.off + unit.header_len + entry.off;947 const entry_off = sec.off(dwarf) + unit.off + unit.header_len + entry.off;
939 for (entry.cross_entry_relocs.items) |reloc| {948 for (entry.cross_entry_relocs.items) |reloc| {
940 try dwarf.resolveReloc(949 try dwarf.resolveReloc(
941 entry_off + reloc.source_off,950 entry_off + reloc.source_off,
...@@ -973,7 +982,7 @@ const Entry = struct {...@@ -973,7 +982,7 @@ const Entry = struct {
973 .eh_frame => return if (dwarf.bin_file.cast(.elf)) |elf_file| {982 .eh_frame => return if (dwarf.bin_file.cast(.elf)) |elf_file| {
974 const zo = elf_file.zigObjectPtr().?;983 const zo = elf_file.zigObjectPtr().?;
975 const shndx = zo.symbol(sec.index).atom(elf_file).?.output_section_index;984 const shndx = zo.symbol(sec.index).atom(elf_file).?.output_section_index;
976 const entry_addr: i64 = @intCast(entry_off - sec.off + elf_file.shdrs.items[shndx].sh_addr);985 const entry_addr: i64 = @intCast(entry_off - sec.off(dwarf) + elf_file.shdrs.items[shndx].sh_addr);
977 for (entry.external_relocs.items) |reloc| {986 for (entry.external_relocs.items) |reloc| {
978 const symbol = zo.symbol(reloc.target_sym);987 const symbol = zo.symbol(reloc.target_sym);
979 try dwarf.resolveReloc(988 try dwarf.resolveReloc(
...@@ -1912,7 +1921,6 @@ pub fn reloadSectionMetadata(dwarf: *Dwarf) void {...@@ -1912,7 +1921,6 @@ pub fn reloadSectionMetadata(dwarf: *Dwarf) void {
1912 }) |sec, sect_index| {1921 }) |sec, sect_index| {
1913 const header = &d_sym.sections.items[sect_index];1922 const header = &d_sym.sections.items[sect_index];
1914 sec.index = sect_index;1923 sec.index = sect_index;
1915 sec.off = header.offset;
1916 sec.len = header.size;1924 sec.len = header.size;
1917 }1925 }
1918 } else {1926 } else {
...@@ -1937,7 +1945,6 @@ pub fn reloadSectionMetadata(dwarf: *Dwarf) void {...@@ -1937,7 +1945,6 @@ pub fn reloadSectionMetadata(dwarf: *Dwarf) void {
1937 }) |sec, sect_index| {1945 }) |sec, sect_index| {
1938 const header = &macho_file.sections.items(.header)[sect_index];1946 const header = &macho_file.sections.items(.header)[sect_index];
1939 sec.index = sect_index;1947 sec.index = sect_index;
1940 sec.off = header.offset;
1941 sec.len = header.size;1948 sec.len = header.size;
1942 }1949 }
1943 }1950 }
...@@ -2534,7 +2541,7 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool...@@ -2534,7 +2541,7 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
2534 var abbrev_code_buf: [AbbrevCode.decl_bytes]u8 = undefined;2541 var abbrev_code_buf: [AbbrevCode.decl_bytes]u8 = undefined;
2535 if (try dwarf.getFile().?.preadAll(2542 if (try dwarf.getFile().?.preadAll(
2536 &abbrev_code_buf,2543 &abbrev_code_buf,
2537 dwarf.debug_info.section.off + unit_ptr.off + unit_ptr.header_len + entry_ptr.off,2544 dwarf.debug_info.section.off(dwarf) + unit_ptr.off + unit_ptr.header_len + entry_ptr.off,
2538 ) != abbrev_code_buf.len) return error.InputOutput;2545 ) != abbrev_code_buf.len) return error.InputOutput;
2539 var abbrev_code_fbs = std.io.fixedBufferStream(&abbrev_code_buf);2546 var abbrev_code_fbs = std.io.fixedBufferStream(&abbrev_code_buf);
2540 const abbrev_code: AbbrevCode = @enumFromInt(2547 const abbrev_code: AbbrevCode = @enumFromInt(
...@@ -3945,7 +3952,7 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {...@@ -3945,7 +3952,7 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
3945 if (dwarf.debug_str.section.dirty) {3952 if (dwarf.debug_str.section.dirty) {
3946 const contents = dwarf.debug_str.contents.items;3953 const contents = dwarf.debug_str.contents.items;
3947 try dwarf.debug_str.section.resize(dwarf, contents.len);3954 try dwarf.debug_str.section.resize(dwarf, contents.len);
3948 try dwarf.getFile().?.pwriteAll(contents, dwarf.debug_str.section.off);3955 try dwarf.getFile().?.pwriteAll(contents, dwarf.debug_str.section.off(dwarf));
3949 dwarf.debug_str.section.dirty = false;3956 dwarf.debug_str.section.dirty = false;
3950 }3957 }
3951 if (dwarf.debug_line.section.dirty) {3958 if (dwarf.debug_line.section.dirty) {
...@@ -4051,7 +4058,7 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {...@@ -4051,7 +4058,7 @@ pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
4051 if (dwarf.debug_line_str.section.dirty) {4058 if (dwarf.debug_line_str.section.dirty) {
4052 const contents = dwarf.debug_line_str.contents.items;4059 const contents = dwarf.debug_line_str.contents.items;
4053 try dwarf.debug_line_str.section.resize(dwarf, contents.len);4060 try dwarf.debug_line_str.section.resize(dwarf, contents.len);
4054 try dwarf.getFile().?.pwriteAll(contents, dwarf.debug_line_str.section.off);4061 try dwarf.getFile().?.pwriteAll(contents, dwarf.debug_line_str.section.off(dwarf));
4055 dwarf.debug_line_str.section.dirty = false;4062 dwarf.debug_line_str.section.dirty = false;
4056 }4063 }
4057 if (dwarf.debug_loclists.section.dirty) {4064 if (dwarf.debug_loclists.section.dirty) {
src/link/Elf.zig+1-1
...@@ -1070,7 +1070,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod...@@ -1070,7 +1070,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
1070 if (shdr.sh_type == elf.SHT_NOBITS) continue;1070 if (shdr.sh_type == elf.SHT_NOBITS) continue;
1071 const code = try zo.codeAlloc(self, atom_index);1071 const code = try zo.codeAlloc(self, atom_index);
1072 defer gpa.free(code);1072 defer gpa.free(code);
1073 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));1073 const file_offset = atom_ptr.offset(self);
1074 atom_ptr.resolveRelocsAlloc(self, code) catch |err| switch (err) {1074 atom_ptr.resolveRelocsAlloc(self, code) catch |err| switch (err) {
1075 error.RelocFailure, error.RelaxFailure => has_reloc_errors = true,1075 error.RelocFailure, error.RelaxFailure => has_reloc_errors = true,
1076 error.UnsupportedCpuArch => {1076 error.UnsupportedCpuArch => {
src/link/Elf/Atom.zig+15-10
...@@ -51,6 +51,11 @@ pub fn address(self: Atom, elf_file: *Elf) i64 {...@@ -51,6 +51,11 @@ pub fn address(self: Atom, elf_file: *Elf) i64 {
51 return @as(i64, @intCast(shdr.sh_addr)) + self.value;51 return @as(i64, @intCast(shdr.sh_addr)) + self.value;
52}52}
5353
54pub fn offset(self: Atom, elf_file: *Elf) u64 {
55 const shdr = elf_file.sections.items(.shdr)[self.output_section_index];
56 return shdr.sh_offset + @as(u64, @intCast(self.value));
57}
58
54pub fn ref(self: Atom) Elf.Ref {59pub fn ref(self: Atom) Elf.Ref {
55 return .{ .index = self.atom_index, .file = self.file_index };60 return .{ .index = self.atom_index, .file = self.file_index };
56}61}
...@@ -1673,7 +1678,7 @@ const aarch64 = struct {...@@ -1673,7 +1678,7 @@ const aarch64 = struct {
1673 => {1678 => {
1674 // TODO: NC means no overflow check1679 // TODO: NC means no overflow check
1675 const taddr = @as(u64, @intCast(S + A));1680 const taddr = @as(u64, @intCast(S + A));
1676 const offset: u12 = switch (r_type) {1681 const off: u12 = switch (r_type) {
1677 .LDST8_ABS_LO12_NC => @truncate(taddr),1682 .LDST8_ABS_LO12_NC => @truncate(taddr),
1678 .LDST16_ABS_LO12_NC => @divExact(@as(u12, @truncate(taddr)), 2),1683 .LDST16_ABS_LO12_NC => @divExact(@as(u12, @truncate(taddr)), 2),
1679 .LDST32_ABS_LO12_NC => @divExact(@as(u12, @truncate(taddr)), 4),1684 .LDST32_ABS_LO12_NC => @divExact(@as(u12, @truncate(taddr)), 4),
...@@ -1681,7 +1686,7 @@ const aarch64 = struct {...@@ -1681,7 +1686,7 @@ const aarch64 = struct {
1681 .LDST128_ABS_LO12_NC => @divExact(@as(u12, @truncate(taddr)), 16),1686 .LDST128_ABS_LO12_NC => @divExact(@as(u12, @truncate(taddr)), 16),
1682 else => unreachable,1687 else => unreachable,
1683 };1688 };
1684 aarch64_util.writeLoadStoreRegInst(offset, code);1689 aarch64_util.writeLoadStoreRegInst(off, code);
1685 },1690 },
16861691
1687 .TLSLE_ADD_TPREL_HI12 => {1692 .TLSLE_ADD_TPREL_HI12 => {
...@@ -1705,8 +1710,8 @@ const aarch64 = struct {...@@ -1705,8 +1710,8 @@ const aarch64 = struct {
1705 .TLSIE_LD64_GOTTPREL_LO12_NC => {1710 .TLSIE_LD64_GOTTPREL_LO12_NC => {
1706 const S_ = target.gotTpAddress(elf_file);1711 const S_ = target.gotTpAddress(elf_file);
1707 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });1712 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });
1708 const offset: u12 = try math.divExact(u12, @truncate(@as(u64, @bitCast(S_ + A))), 8);1713 const off: u12 = try math.divExact(u12, @truncate(@as(u64, @bitCast(S_ + A))), 8);
1709 aarch64_util.writeLoadStoreRegInst(offset, code);1714 aarch64_util.writeLoadStoreRegInst(off, code);
1710 },1715 },
17111716
1712 .TLSGD_ADR_PAGE21 => {1717 .TLSGD_ADR_PAGE21 => {
...@@ -1719,8 +1724,8 @@ const aarch64 = struct {...@@ -1719,8 +1724,8 @@ const aarch64 = struct {
1719 .TLSGD_ADD_LO12_NC => {1724 .TLSGD_ADD_LO12_NC => {
1720 const S_ = target.tlsGdAddress(elf_file);1725 const S_ = target.tlsGdAddress(elf_file);
1721 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });1726 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });
1722 const offset: u12 = @truncate(@as(u64, @bitCast(S_ + A)));1727 const off: u12 = @truncate(@as(u64, @bitCast(S_ + A)));
1723 aarch64_util.writeAddImmInst(offset, code);1728 aarch64_util.writeAddImmInst(off, code);
1724 },1729 },
17251730
1726 .TLSDESC_ADR_PAGE21 => {1731 .TLSDESC_ADR_PAGE21 => {
...@@ -1739,8 +1744,8 @@ const aarch64 = struct {...@@ -1739,8 +1744,8 @@ const aarch64 = struct {
1739 if (target.flags.has_tlsdesc) {1744 if (target.flags.has_tlsdesc) {
1740 const S_ = target.tlsDescAddress(elf_file);1745 const S_ = target.tlsDescAddress(elf_file);
1741 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });1746 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });
1742 const offset: u12 = try math.divExact(u12, @truncate(@as(u64, @bitCast(S_ + A))), 8);1747 const off: u12 = try math.divExact(u12, @truncate(@as(u64, @bitCast(S_ + A))), 8);
1743 aarch64_util.writeLoadStoreRegInst(offset, code);1748 aarch64_util.writeLoadStoreRegInst(off, code);
1744 } else {1749 } else {
1745 relocs_log.debug(" relaxing ldr => nop", .{});1750 relocs_log.debug(" relaxing ldr => nop", .{});
1746 mem.writeInt(u32, code, Instruction.nop().toU32(), .little);1751 mem.writeInt(u32, code, Instruction.nop().toU32(), .little);
...@@ -1751,8 +1756,8 @@ const aarch64 = struct {...@@ -1751,8 +1756,8 @@ const aarch64 = struct {
1751 if (target.flags.has_tlsdesc) {1756 if (target.flags.has_tlsdesc) {
1752 const S_ = target.tlsDescAddress(elf_file);1757 const S_ = target.tlsDescAddress(elf_file);
1753 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });1758 relocs_log.debug(" [{x} => {x}]", .{ P, S_ + A });
1754 const offset: u12 = @truncate(@as(u64, @bitCast(S_ + A)));1759 const off: u12 = @truncate(@as(u64, @bitCast(S_ + A)));
1755 aarch64_util.writeAddImmInst(offset, code);1760 aarch64_util.writeAddImmInst(off, code);
1756 } else {1761 } else {
1757 const old_inst = Instruction{1762 const old_inst = Instruction{
1758 .add_subtract_immediate = mem.bytesToValue(std.meta.TagPayload(1763 .add_subtract_immediate = mem.bytesToValue(std.meta.TagPayload(
src/link/Elf/ZigObject.zig+5-10
...@@ -906,7 +906,7 @@ pub fn codeAlloc(self: *ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8...@@ -906,7 +906,7 @@ pub fn codeAlloc(self: *ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8
906 return code;906 return code;
907 }907 }
908908
909 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));909 const file_offset = atom_ptr.offset(elf_file);
910 const size = std.math.cast(usize, atom_ptr.size) orelse return error.Overflow;910 const size = std.math.cast(usize, atom_ptr.size) orelse return error.Overflow;
911 const code = try gpa.alloc(u8, size);911 const code = try gpa.alloc(u8, size);
912 errdefer gpa.free(code);912 errdefer gpa.free(code);
...@@ -1338,7 +1338,7 @@ fn updateNavCode(...@@ -1338,7 +1338,7 @@ fn updateNavCode(
13381338
1339 const shdr = elf_file.sections.items(.shdr)[shdr_index];1339 const shdr = elf_file.sections.items(.shdr)[shdr_index];
1340 if (shdr.sh_type != elf.SHT_NOBITS) {1340 if (shdr.sh_type != elf.SHT_NOBITS) {
1341 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));1341 const file_offset = atom_ptr.offset(elf_file);
1342 try elf_file.base.file.?.pwriteAll(code, file_offset);1342 try elf_file.base.file.?.pwriteAll(code, file_offset);
1343 log.debug("writing {} from 0x{x} to 0x{x}", .{ nav.fqn.fmt(ip), file_offset, file_offset + code.len });1343 log.debug("writing {} from 0x{x} to 0x{x}", .{ nav.fqn.fmt(ip), file_offset, file_offset + code.len });
1344 }1344 }
...@@ -1716,9 +1716,7 @@ fn updateLazySymbol(...@@ -1716,9 +1716,7 @@ fn updateLazySymbol(
1716 local_sym.value = 0;1716 local_sym.value = 0;
1717 local_esym.st_value = 0;1717 local_esym.st_value = 0;
17181718
1719 const shdr = elf_file.sections.items(.shdr)[output_section_index];1719 try elf_file.base.file.?.pwriteAll(code, atom_ptr.offset(elf_file));
1720 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));
1721 try elf_file.base.file.?.pwriteAll(code, file_offset);
1722}1720}
17231721
1724const LowerConstResult = union(enum) {1722const LowerConstResult = union(enum) {
...@@ -1771,9 +1769,7 @@ fn lowerConst(...@@ -1771,9 +1769,7 @@ fn lowerConst(
1771 try self.allocateAtom(atom_ptr, elf_file);1769 try self.allocateAtom(atom_ptr, elf_file);
1772 errdefer self.freeNavMetadata(elf_file, sym_index);1770 errdefer self.freeNavMetadata(elf_file, sym_index);
17731771
1774 const shdr = elf_file.sections.items(.shdr)[output_section_index];1772 try elf_file.base.file.?.pwriteAll(code, atom_ptr.offset(elf_file));
1775 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));
1776 try elf_file.base.file.?.pwriteAll(code, file_offset);
17771773
1778 return .{ .ok = sym_index };1774 return .{ .ok = sym_index };
1779}1775}
...@@ -1935,8 +1931,7 @@ fn trampolineSize(cpu_arch: std.Target.Cpu.Arch) u64 {...@@ -1935,8 +1931,7 @@ fn trampolineSize(cpu_arch: std.Target.Cpu.Arch) u64 {
19351931
1936fn writeTrampoline(tr_sym: Symbol, target: Symbol, elf_file: *Elf) !void {1932fn writeTrampoline(tr_sym: Symbol, target: Symbol, elf_file: *Elf) !void {
1937 const atom_ptr = tr_sym.atom(elf_file).?;1933 const atom_ptr = tr_sym.atom(elf_file).?;
1938 const shdr = elf_file.sections.items(.shdr)[atom_ptr.output_section_index];1934 const fileoff = atom_ptr.offset(elf_file);
1939 const fileoff = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));
1940 const source_addr = tr_sym.address(.{}, elf_file);1935 const source_addr = tr_sym.address(.{}, elf_file);
1941 const target_addr = target.address(.{ .trampoline = false }, elf_file);1936 const target_addr = target.address(.{ .trampoline = false }, elf_file);
1942 var buf: [max_trampoline_len]u8 = undefined;1937 var buf: [max_trampoline_len]u8 = undefined;