| ... | @@ -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 | } |
| 858 | | 861 | |
| ... | @@ -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 | } |
| 949 | | 953 | |
| ... | @@ -1052,7 +1056,12 @@ pub fn updateFunc( | ... | @@ -1052,7 +1056,12 @@ pub fn updateFunc( |
| 1052 | }, | 1056 | }, |
| 1053 | }; | 1057 | }; |
| 1054 | | 1058 | |
| 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 | }; |
| 1174 | | 1183 | |
| 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 | else | 1192 | else |