authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-04-09 22:13:08+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-04-13 10:56:03+02:00
log717d38287144e3b5d8210c609092487ba5d751a5
tree0152ef1c80336208c5f4afbdfcf4e9c66adad876
parentb377e0b370252e08d55dd384e23ed3fd05aea5c5

zld: streamline tags for Symbol


3 files changed, 45 insertions(+), 42 deletions(-)

src/link/MachO/Object.zig+6-6
......@@ -334,16 +334,16 @@ pub fn parseSymtab(self: *Object) !void {
334334 const tag: Symbol.Tag = tag: {
335335 if (Symbol.isLocal(symbol)) {
336336 if (Symbol.isStab(symbol))
337 break :tag .Stab
337 break :tag .stab
338338 else
339 break :tag .Local;
339 break :tag .local;
340340 } else if (Symbol.isGlobal(symbol)) {
341341 if (Symbol.isWeakDef(symbol))
342 break :tag .Weak
342 break :tag .weak
343343 else
344 break :tag .Strong;
344 break :tag .strong;
345345 } else {
346 break :tag .Undef;
346 break :tag .undef;
347347 }
348348 };
349349 self.symtab.appendAssumeCapacity(.{
......@@ -388,7 +388,7 @@ pub fn parseDebugInfo(self: *Object) !void {
388388 };
389389
390390 for (self.symtab.items) |sym, index| {
391 if (sym.tag == .Undef) continue;
391 if (sym.tag == .undef) continue;
392392
393393 const sym_name = self.getString(sym.inner.n_strx);
394394 const size = blk: for (debug_info.inner.func_list.items) |func| {
src/link/MachO/Symbol.zig+6-6
......@@ -4,12 +4,12 @@ const std = @import("std");
44const macho = std.macho;
55
66pub const Tag = enum {
7 Stab,
8 Local,
9 Weak,
10 Strong,
11 Import,
12 Undef,
7 stab,
8 local,
9 weak,
10 strong,
11 import,
12 undef,
1313};
1414
1515tag: Tag,
src/link/MachO/Zld.zig+33-30
......@@ -876,8 +876,8 @@ fn allocateSymbols(self: *Zld) !void {
876876 for (self.objects.items) |*object, object_id| {
877877 for (object.symtab.items) |*sym| {
878878 switch (sym.tag) {
879 .Import => unreachable,
880 .Undef => continue,
879 .import => unreachable,
880 .undef => continue,
881881 else => {},
882882 }
883883
......@@ -924,7 +924,7 @@ fn allocateSymbols(self: *Zld) !void {
924924 }
925925
926926 for (self.symtab.items()) |*entry| {
927 if (entry.value.tag == .Import) continue;
927 if (entry.value.tag == .import) continue;
928928
929929 const object_id = entry.value.file orelse unreachable;
930930 const index = entry.value.index orelse unreachable;
......@@ -949,7 +949,7 @@ fn allocateStubsAndGotEntries(self: *Zld) !void {
949949 assert(mem.eql(u8, sym_name, entry.key));
950950
951951 entry.value.target_addr = target_addr: {
952 if (sym.tag == .Undef) {
952 if (sym.tag == .undef) {
953953 const glob = self.symtab.get(sym_name) orelse unreachable;
954954 break :target_addr glob.inner.n_value;
955955 }
......@@ -1236,8 +1236,8 @@ fn resolveSymbolsInObject(self: *Zld, object_id: u16) !void {
12361236
12371237 for (object.symtab.items) |sym, sym_id| {
12381238 switch (sym.tag) {
1239 .Local, .Stab => continue, // If symbol is local to CU, we don't put it in the global symbol table.
1240 .Weak, .Strong => {
1239 .local, .stab => continue, // If symbol is local to CU, we don't put it in the global symbol table.
1240 .weak, .strong => {
12411241 const sym_name = object.getString(sym.inner.n_strx);
12421242 const global = self.symtab.getEntry(sym_name) orelse {
12431243 // Put new global symbol into the symbol table.
......@@ -1257,14 +1257,17 @@ fn resolveSymbolsInObject(self: *Zld, object_id: u16) !void {
12571257 continue;
12581258 };
12591259
1260 if (global.value.tag == .Weak) continue; // If symbol is weak, nothing to do.
1261 if (global.value.tag == .Strong) { // If both symbols are strong, we have a collision.
1262 log.err("symbol '{s}' defined multiple times", .{sym_name});
1263 return error.MultipleSymbolDefinitions;
1260 switch (global.value.tag) {
1261 .weak => continue, // If symbol is weak, nothing to do.
1262 .strong => {
1263 log.err("symbol '{s}' defined multiple times", .{sym_name});
1264 return error.MultipleSymbolDefinitions;
1265 },
1266 else => {},
12641267 }
12651268
12661269 global.value = .{
1267 .tag = .Strong,
1270 .tag = .strong,
12681271 .inner = .{
12691272 .n_strx = 0, // This will be populated later.
12701273 .n_value = 0, // This will be populated later,
......@@ -1276,13 +1279,13 @@ fn resolveSymbolsInObject(self: *Zld, object_id: u16) !void {
12761279 .index = @intCast(u32, sym_id),
12771280 };
12781281 },
1279 .Undef => {
1282 .undef => {
12801283 const sym_name = object.getString(sym.inner.n_strx);
12811284 if (self.symtab.contains(sym_name)) continue; // Nothing to do if we already found a definition.
12821285
12831286 const name = try self.allocator.dupe(u8, sym_name);
12841287 try self.symtab.putNoClobber(self.allocator, name, .{
1285 .tag = .Undef,
1288 .tag = .undef,
12861289 .inner = .{
12871290 .n_strx = 0,
12881291 .n_value = 0,
......@@ -1292,7 +1295,7 @@ fn resolveSymbolsInObject(self: *Zld, object_id: u16) !void {
12921295 },
12931296 });
12941297 },
1295 .Import => unreachable, // We don't expect any imports just yet.
1298 .import => unreachable, // We don't expect any imports just yet.
12961299 }
12971300 }
12981301}
......@@ -1311,7 +1314,7 @@ fn resolveSymbols(self: *Zld) !void {
13111314 hit = false;
13121315
13131316 for (self.symtab.items()) |entry| {
1314 if (entry.value.tag != .Undef) continue;
1317 if (entry.value.tag != .undef) continue;
13151318
13161319 const sym_name = entry.key;
13171320
......@@ -1345,9 +1348,9 @@ fn resolveSymbols(self: *Zld) !void {
13451348 // TODO Implement libSystem as a hard-coded library, or ship with
13461349 // a libSystem.B.tbd definition file?
13471350 for (self.symtab.items()) |*entry| {
1348 if (entry.value.tag != .Undef) continue;
1351 if (entry.value.tag != .undef) continue;
13491352 entry.value = .{
1350 .tag = .Import,
1353 .tag = .import,
13511354 .inner = .{
13521355 .n_strx = 0, // This will be populated once we write the string table.
13531356 .n_type = macho.N_UNDF | macho.N_EXT,
......@@ -1362,7 +1365,7 @@ fn resolveSymbols(self: *Zld) !void {
13621365 // If there are any undefs left, flag an error.
13631366 var has_unresolved = false;
13641367 for (self.symtab.items()) |entry| {
1365 if (entry.value.tag != .Undef) continue;
1368 if (entry.value.tag != .undef) continue;
13661369 has_unresolved = true;
13671370 log.err("undefined reference to symbol '{s}'", .{entry.key});
13681371 }
......@@ -1373,7 +1376,7 @@ fn resolveSymbols(self: *Zld) !void {
13731376 // Finally put dyld_stub_binder as an Import
13741377 var name = try self.allocator.dupe(u8, "dyld_stub_binder");
13751378 try self.symtab.putNoClobber(self.allocator, name, .{
1376 .tag = .Import,
1379 .tag = .import,
13771380 .inner = .{
13781381 .n_strx = 0, // This will be populated once we write the string table.
13791382 .n_type = macho.N_UNDF | macho.N_EXT,
......@@ -1401,7 +1404,7 @@ fn resolveStubsAndGotEntries(self: *Zld) !void {
14011404 if (self.got_entries.contains(sym_name)) continue;
14021405
14031406 // TODO clean this up
1404 const is_import = self.symtab.get(sym_name).?.tag == .Import;
1407 const is_import = self.symtab.get(sym_name).?.tag == .import;
14051408 var name = try self.allocator.dupe(u8, sym_name);
14061409 const index = @intCast(u32, self.got_entries.items().len);
14071410 try self.got_entries.putNoClobber(self.allocator, name, .{
......@@ -1418,11 +1421,11 @@ fn resolveStubsAndGotEntries(self: *Zld) !void {
14181421 const sym = object.symtab.items[reloc.target.symbol];
14191422 const sym_name = object.getString(sym.inner.n_strx);
14201423
1421 if (sym.tag != .Undef) continue;
1424 if (sym.tag != .undef) continue;
14221425
14231426 const in_globals = self.symtab.get(sym_name) orelse unreachable;
14241427
1425 if (in_globals.tag != .Import) continue;
1428 if (in_globals.tag != .import) continue;
14261429 if (self.stubs.contains(sym_name)) continue;
14271430
14281431 var name = try self.allocator.dupe(u8, sym_name);
......@@ -1589,7 +1592,7 @@ fn relocTargetAddr(self: *Zld, object_id: u16, target: Relocation.Target) !u64 {
15891592 const sym_name = object.getString(sym.inner.n_strx);
15901593
15911594 switch (sym.tag) {
1592 .Stab, .Local, .Weak, .Strong => {
1595 .stab, .local, .weak, .strong => {
15931596 log.warn(" | local symbol '{s}'", .{sym_name});
15941597 break :blk sym.inner.n_value;
15951598 },
......@@ -2245,7 +2248,7 @@ fn writeBindInfoTable(self: *Zld) !void {
22452248
22462249 const dylib_ordinal = dylib_ordinal: {
22472250 const sym = self.symtab.get(entry.key) orelse continue; // local indirection
2248 if (sym.tag != .Import) continue; // local indirection
2251 if (sym.tag != .import) continue; // local indirection
22492252 break :dylib_ordinal sym.file.? + 1;
22502253 };
22512254
......@@ -2308,7 +2311,7 @@ fn writeLazyBindInfoTable(self: *Zld) !void {
23082311 for (self.stubs.items()) |entry| {
23092312 const dylib_ordinal = dylib_ordinal: {
23102313 const sym = self.symtab.get(entry.key) orelse unreachable;
2311 assert(sym.tag == .Import);
2314 assert(sym.tag == .import);
23122315 break :dylib_ordinal sym.file.? + 1;
23132316 };
23142317
......@@ -2562,7 +2565,7 @@ fn populateStringTable(self: *Zld) !void {
25622565 for (self.objects.items) |*object| {
25632566 for (object.symtab.items) |*sym| {
25642567 switch (sym.tag) {
2565 .Undef, .Import => continue,
2568 .undef, .import => continue,
25662569 else => {},
25672570 }
25682571 const sym_name = object.getString(sym.inner.n_strx);
......@@ -2572,7 +2575,7 @@ fn populateStringTable(self: *Zld) !void {
25722575 }
25732576
25742577 for (self.symtab.items()) |*entry| {
2575 if (entry.value.tag != .Import) continue;
2578 if (entry.value.tag != .import) continue;
25762579
25772580 const n_strx = try self.makeString(entry.key);
25782581 entry.value.inner.n_strx = n_strx;
......@@ -2589,7 +2592,7 @@ fn writeSymbolTable(self: *Zld) !void {
25892592 for (self.objects.items) |object| {
25902593 for (object.symtab.items) |sym| {
25912594 switch (sym.tag) {
2592 .Stab, .Local => {},
2595 .stab, .local => {},
25932596 else => continue,
25942597 }
25952598
......@@ -2609,10 +2612,10 @@ fn writeSymbolTable(self: *Zld) !void {
26092612 var undef_id: u32 = 0;
26102613 for (self.symtab.items()) |entry| {
26112614 switch (entry.value.tag) {
2612 .Weak, .Strong => {
2615 .weak, .strong => {
26132616 try exports.append(entry.value.inner);
26142617 },
2615 .Import => {
2618 .import => {
26162619 try undefs.append(entry.value.inner);
26172620 try undefs_ids.putNoClobber(entry.key, undef_id);
26182621 undef_id += 1;