authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-15 08:37:13+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-08-15 08:37:13+02:00
logb8203fac1b272d4f13f668797cf356d995b7967f
tree7334553f2a3460ffaa1a90aa76367aee7ca50919
parentf26573fddfcebe44d4d69ea0e01d345fc1b15835

elf: check for relocs before deciding on shndx in getNavShdrIndex


2 files changed, 28 insertions(+), 9 deletions(-)

src/link/Elf.zig+5
...@@ -857,6 +857,11 @@ pub fn growAllocSection(self: *Elf, shdr_index: u32, needed_size: u64) !void {...@@ -857,6 +857,11 @@ pub fn growAllocSection(self: *Elf, shdr_index: u32, needed_size: u64) !void {
857 const shdr = &self.shdrs.items[shdr_index];857 const shdr = &self.shdrs.items[shdr_index];
858 const maybe_phdr = if (self.phdr_to_shdr_table.get(shdr_index)) |phndx| &self.phdrs.items[phndx] else null;858 const maybe_phdr = if (self.phdr_to_shdr_table.get(shdr_index)) |phndx| &self.phdrs.items[phndx] else null;
859 const is_zerofill = shdr.sh_type == elf.SHT_NOBITS;859 const is_zerofill = shdr.sh_type == elf.SHT_NOBITS;
860 log.debug("allocated size {x} of {s}, needed size {x}", .{
861 self.allocatedSize(shdr.sh_offset),
862 self.getShString(shdr.sh_name),
863 needed_size,
864 });
860865
861 if (needed_size > self.allocatedSize(shdr.sh_offset) and !is_zerofill) {866 if (needed_size > self.allocatedSize(shdr.sh_offset) and !is_zerofill) {
862 const existing_size = shdr.sh_size;867 const existing_size = shdr.sh_size;
src/link/Elf/ZigObject.zig+23-9
...@@ -816,9 +816,9 @@ fn getNavShdrIndex(...@@ -816,9 +816,9 @@ fn getNavShdrIndex(
816 elf_file: *Elf,816 elf_file: *Elf,
817 zcu: *Zcu,817 zcu: *Zcu,
818 nav_index: InternPool.Nav.Index,818 nav_index: InternPool.Nav.Index,
819 sym_index: Symbol.Index,
819 code: []const u8,820 code: []const u8,
820) error{OutOfMemory}!u32 {821) error{OutOfMemory}!u32 {
821 _ = self;
822 const ip = &zcu.intern_pool;822 const ip = &zcu.intern_pool;
823 const any_non_single_threaded = elf_file.base.comp.config.any_non_single_threaded;823 const any_non_single_threaded = elf_file.base.comp.config.any_non_single_threaded;
824 const nav_val = zcu.navValue(nav_index);824 const nav_val = zcu.navValue(nav_index);
...@@ -828,10 +828,12 @@ fn getNavShdrIndex(...@@ -828,10 +828,12 @@ fn getNavShdrIndex(
828 .@"extern" => |@"extern"| .{ @"extern".is_const, @"extern".is_threadlocal, .none },828 .@"extern" => |@"extern"| .{ @"extern".is_const, @"extern".is_threadlocal, .none },
829 else => .{ true, false, nav_val.toIntern() },829 else => .{ true, false, nav_val.toIntern() },
830 };830 };
831 const has_relocs = self.symbol(sym_index).atom(elf_file).?.relocs(elf_file).len > 0;
831 if (any_non_single_threaded and is_threadlocal) {832 if (any_non_single_threaded and is_threadlocal) {
832 for (code) |byte| {833 const is_bss = !has_relocs and for (code) |byte| {
833 if (byte != 0) break;834 if (byte != 0) break false;
834 } else return elf_file.sectionByName(".tbss") orelse try elf_file.addSection(.{835 } else true;
836 if (is_bss) return elf_file.sectionByName(".tbss") orelse try elf_file.addSection(.{
835 .type = elf.SHT_NOBITS,837 .type = elf.SHT_NOBITS,
836 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,838 .flags = elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_TLS,
837 .name = try elf_file.insertShString(".tbss"),839 .name = try elf_file.insertShString(".tbss"),
...@@ -850,9 +852,10 @@ fn getNavShdrIndex(...@@ -850,9 +852,10 @@ fn getNavShdrIndex(
850 .Debug, .ReleaseSafe => elf_file.zig_data_section_index.?,852 .Debug, .ReleaseSafe => elf_file.zig_data_section_index.?,
851 .ReleaseFast, .ReleaseSmall => elf_file.zig_bss_section_index.?,853 .ReleaseFast, .ReleaseSmall => elf_file.zig_bss_section_index.?,
852 };854 };
853 for (code) |byte| {855 const is_bss = !has_relocs and for (code) |byte| {
854 if (byte != 0) break;856 if (byte != 0) break false;
855 } else return elf_file.zig_bss_section_index.?;857 } else true;
858 if (is_bss) return elf_file.zig_bss_section_index.?;
856 return elf_file.zig_data_section_index.?;859 return elf_file.zig_data_section_index.?;
857}860}
858861
...@@ -944,6 +947,7 @@ fn updateNavCode(...@@ -944,6 +947,7 @@ fn updateNavCode(
944 if (shdr.sh_type != elf.SHT_NOBITS) {947 if (shdr.sh_type != elf.SHT_NOBITS) {
945 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));948 const file_offset = shdr.sh_offset + @as(u64, @intCast(atom_ptr.value));
946 try elf_file.base.file.?.pwriteAll(code, file_offset);949 try elf_file.base.file.?.pwriteAll(code, file_offset);
950 log.debug("writing {} from 0x{x} to 0x{x}", .{ nav.fqn.fmt(ip), file_offset, file_offset + code.len });
947 }951 }
948}952}
949953
...@@ -1052,7 +1056,12 @@ pub fn updateFunc(...@@ -1052,7 +1056,12 @@ pub fn updateFunc(
1052 },1056 },
1053 };1057 };
10541058
1055 const shndx = try self.getNavShdrIndex(elf_file, zcu, func.owner_nav, code);1059 const shndx = try self.getNavShdrIndex(elf_file, zcu, func.owner_nav, sym_index, code);
1060 log.debug("setting shdr({x},{s}) for {}", .{
1061 shndx,
1062 elf_file.getShString(elf_file.shdrs.items[shndx].sh_name),
1063 ip.getNav(func.owner_nav).fqn.fmt(ip),
1064 });
1056 const old_rva, const old_alignment = blk: {1065 const old_rva, const old_alignment = blk: {
1057 const atom_ptr = self.symbol(sym_index).atom(elf_file).?;1066 const atom_ptr = self.symbol(sym_index).atom(elf_file).?;
1058 break :blk .{ atom_ptr.value, atom_ptr.alignment };1067 break :blk .{ atom_ptr.value, atom_ptr.alignment };
...@@ -1172,7 +1181,12 @@ pub fn updateNav(...@@ -1172,7 +1181,12 @@ pub fn updateNav(
1172 },1181 },
1173 };1182 };
11741183
1175 const shndx = try self.getNavShdrIndex(elf_file, zcu, nav_index, code);1184 const shndx = try self.getNavShdrIndex(elf_file, zcu, nav_index, sym_index, code);
1185 log.debug("setting shdr({x},{s}) for {}", .{
1186 shndx,
1187 elf_file.getShString(elf_file.shdrs.items[shndx].sh_name),
1188 nav.fqn.fmt(ip),
1189 });
1176 if (elf_file.shdrs.items[shndx].sh_flags & elf.SHF_TLS != 0)1190 if (elf_file.shdrs.items[shndx].sh_flags & elf.SHF_TLS != 0)
1177 try self.updateTlv(elf_file, pt, nav_index, sym_index, shndx, code)1191 try self.updateTlv(elf_file, pt, nav_index, sym_index, shndx, code)
1178 else1192 else