Commit 3606b5df3f

Jakub Konka <kubkon@jakubkonka.com>
2023-11-01 23:08:50
elf: improve Symbol to handle emitting relocatable object files
1 parent ec2671d
Changed files (4)
src/link/Elf/file.zig
@@ -150,7 +150,7 @@ pub const File = union(enum) {
             if (file_ptr.index() != file.index()) continue;
             if (global.atom(elf_file)) |atom| if (!atom.flags.alive) continue;
             global.flags.output_symtab = true;
-            if (global.isLocal()) {
+            if (global.isLocal(elf_file)) {
                 output_symtab_size.nlocals += 1;
             } else {
                 output_symtab_size.nglobals += 1;
@@ -181,7 +181,7 @@ pub const File = union(enum) {
             const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
             elf_file.strtab.appendSliceAssumeCapacity(global.name(elf_file));
             elf_file.strtab.appendAssumeCapacity(0);
-            if (global.isLocal()) {
+            if (global.isLocal(elf_file)) {
                 const out_sym = &elf_file.symtab.items[ilocal];
                 out_sym.st_name = st_name;
                 global.setOutputSym(elf_file, out_sym);
src/link/Elf/Symbol.zig
@@ -42,7 +42,8 @@ pub fn outputShndx(symbol: Symbol) ?u16 {
     return symbol.output_section_index;
 }
 
-pub fn isLocal(symbol: Symbol) bool {
+pub fn isLocal(symbol: Symbol, elf_file: *Elf) bool {
+    if (elf_file.isObject()) return symbol.elfSym(elf_file).st_bind() == elf.STB_LOCAL;
     return !(symbol.flags.import or symbol.flags.@"export");
 }
 
@@ -208,7 +209,7 @@ pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void {
     const esym = symbol.elfSym(elf_file);
     const st_type = symbol.type(elf_file);
     const st_bind: u8 = blk: {
-        if (symbol.isLocal()) break :blk 0;
+        if (symbol.isLocal(elf_file)) break :blk 0;
         if (symbol.flags.weak) break :blk elf.STB_WEAK;
         if (file_ptr == .shared_object) break :blk elf.STB_GLOBAL;
         break :blk esym.st_bind();
src/link/Elf/ZigObject.zig
@@ -1192,6 +1192,7 @@ pub fn updateExports(
         global_esym.st_shndx = esym.st_shndx;
         global_esym.st_info = (stb_bits << 4) | stt_bits;
         global_esym.st_name = name_off;
+        global_esym.st_size = esym.st_size;
         self.global_esyms.items(.shndx)[actual_esym_index] = esym_shndx;
     }
 }
src/link/Elf.zig
@@ -1943,7 +1943,7 @@ fn scanRelocs(self: *Elf) !void {
 
     for (self.symbols.items, 0..) |*sym, i| {
         const index = @as(u32, @intCast(i));
-        if (!sym.isLocal() and !sym.flags.has_dynamic) {
+        if (!sym.isLocal(self) and !sym.flags.has_dynamic) {
             log.debug("'{s}' is non-local", .{sym.name(self)});
             try self.dynsym.addSymbol(index, self);
         }