Commit 5381bfd348

Jakub Konka <kubkon@jakubkonka.com>
2023-09-29 22:18:38
elf: fix setting st_value of _end synthetic symbol
1 parent 8e90b41
Changed files (2)
src
src/link/Elf/Symbol.zig
@@ -198,7 +198,8 @@ pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void {
         // }
         if (st_shndx == elf.SHN_ABS) break :blk symbol.value;
         const shdr = &elf_file.shdrs.items[st_shndx];
-        if (shdr.sh_flags & elf.SHF_TLS != 0) break :blk symbol.value - elf_file.tlsAddress();
+        if (shdr.sh_flags & elf.SHF_TLS != 0 and file_ptr != .linker_defined)
+            break :blk symbol.value - elf_file.tlsAddress();
         break :blk symbol.value;
     };
     out.* = .{
src/link/Elf.zig
@@ -3433,9 +3433,14 @@ fn allocateLinkerDefinedSymbols(self: *Elf) void {
     // _end
     {
         const end_symbol = self.symbol(self.end_index.?);
+        end_symbol.value = 0;
         for (self.shdrs.items, 0..) |*shdr, shndx| {
-            if (shdr.sh_flags & elf.SHF_ALLOC != 0) {
-                end_symbol.value = shdr.sh_addr + shdr.sh_size;
+            if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;
+            const phdr_index = self.phdr_to_shdr_table.get(@intCast(shndx)).?;
+            const phdr = self.phdrs.items[phdr_index];
+            const value = phdr.p_vaddr + phdr.p_memsz;
+            if (end_symbol.value < value) {
+                end_symbol.value = value;
                 end_symbol.output_section_index = @intCast(shndx);
             }
         }