Commit 8ca809d928

Jakub Konka <kubkon@jakubkonka.com>
2024-08-02 18:32:07
elf: move getStartStopBasename into Object
1 parent 41e9b8b
Changed files (3)
src/link/Elf/LinkerDefined.zig
@@ -95,8 +95,9 @@ pub fn initSymbols(self: *LinkerDefined, elf_file: *Elf) !void {
 
     var start_stop_count: usize = 0;
     for (elf_file.objects.items) |index| {
-        for (elf_file.file(index).?.object.shdrs.items) |shdr| {
-            if (elf_file.getStartStopBasename(shdr)) |_| {
+        const object = elf_file.file(index).?.object;
+        for (object.shdrs.items) |shdr| {
+            if (object.getStartStopBasename(shdr)) |_| {
                 start_stop_count += 2; // __start_, __stop_
             }
         }
@@ -140,8 +141,9 @@ pub fn initSymbols(self: *LinkerDefined, elf_file: *Elf) !void {
     }
 
     for (elf_file.objects.items) |index| {
-        for (elf_file.file(index).?.object.shdrs.items) |shdr| {
-            if (elf_file.getStartStopBasename(shdr)) |name| {
+        const object = elf_file.file(index).?.object;
+        for (object.shdrs.items) |shdr| {
+            if (object.getStartStopBasename(shdr)) |name| {
                 const start_name = try std.fmt.allocPrintZ(gpa, "__start_{s}", .{name});
                 defer gpa.free(start_name);
                 const stop_name = try std.fmt.allocPrintZ(gpa, "__stop_{s}", .{name});
src/link/Elf/Object.zig
@@ -1444,6 +1444,14 @@ pub fn comdatGroup(self: *Object, index: Elf.ComdatGroup.Index) *Elf.ComdatGroup
     return &self.comdat_groups.items[index];
 }
 
+pub fn getStartStopBasename(self: Object, shdr: elf.Elf64_Shdr) ?[]const u8 {
+    const name = self.getString(shdr.sh_name);
+    if (shdr.sh_flags & elf.SHF_ALLOC != 0 and name.len > 0) {
+        if (Elf.isCIdentifier(name)) return name;
+    }
+    return null;
+}
+
 pub fn format(
     self: *Object,
     comptime unused_fmt_string: []const u8,
src/link/Elf.zig
@@ -5252,14 +5252,6 @@ pub fn isCIdentifier(name: []const u8) bool {
     return true;
 }
 
-pub fn getStartStopBasename(self: *Elf, shdr: elf.Elf64_Shdr) ?[]const u8 {
-    const name = self.getShString(shdr.sh_name);
-    if (shdr.sh_flags & elf.SHF_ALLOC != 0 and name.len > 0) {
-        if (isCIdentifier(name)) return name;
-    }
-    return null;
-}
-
 pub fn addThunk(self: *Elf) !Thunk.Index {
     const index = @as(Thunk.Index, @intCast(self.thunks.items.len));
     const th = try self.thunks.addOne(self.base.comp.gpa);