authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-03-07 12:06:31+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-03-17 19:59:13+01:00
log62f43fbc068ae63b6492af20a7cfd38b7426cb92
tree45090e6bd83b527241bed1375d4bf2112341c774
parentd484b3b3cbebddc3e1e8b160152e3f8e3be93b63

zld: clean up use of commands.zig module


2 files changed, 13 insertions(+), 64 deletions(-)

src/link/MachO/Zld.zig+13-59
...@@ -377,7 +377,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {...@@ -377,7 +377,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {
377 if (self.text_const_section_index != null) continue;377 if (self.text_const_section_index != null) continue;
378378
379 self.text_const_section_index = @intCast(u16, text_seg.sections.items.len);379 self.text_const_section_index = @intCast(u16, text_seg.sections.items.len);
380 try text_seg.append(self.allocator, .{380 try text_seg.addSection(self.allocator, .{
381 .sectname = makeStaticString("__const"),381 .sectname = makeStaticString("__const"),
382 .segname = makeStaticString("__TEXT"),382 .segname = makeStaticString("__TEXT"),
383 .addr = 0,383 .addr = 0,
...@@ -396,7 +396,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {...@@ -396,7 +396,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {
396 if (self.data_const_section_index != null) continue;396 if (self.data_const_section_index != null) continue;
397397
398 self.data_const_section_index = @intCast(u16, data_seg.sections.items.len);398 self.data_const_section_index = @intCast(u16, data_seg.sections.items.len);
399 try data_seg.append(self.allocator, .{399 try data_seg.addSection(self.allocator, .{
400 .sectname = makeStaticString("__const"),400 .sectname = makeStaticString("__const"),
401 .segname = makeStaticString("__DATA"),401 .segname = makeStaticString("__DATA"),
402 .addr = 0,402 .addr = 0,
...@@ -417,7 +417,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {...@@ -417,7 +417,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {
417 if (self.cstring_section_index != null) continue;417 if (self.cstring_section_index != null) continue;
418418
419 self.cstring_section_index = @intCast(u16, text_seg.sections.items.len);419 self.cstring_section_index = @intCast(u16, text_seg.sections.items.len);
420 try text_seg.append(self.allocator, .{420 try text_seg.addSection(self.allocator, .{
421 .sectname = makeStaticString("__cstring"),421 .sectname = makeStaticString("__cstring"),
422 .segname = makeStaticString("__TEXT"),422 .segname = makeStaticString("__TEXT"),
423 .addr = 0,423 .addr = 0,
...@@ -437,7 +437,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {...@@ -437,7 +437,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {
437 if (self.bss_section_index != null) continue;437 if (self.bss_section_index != null) continue;
438438
439 self.bss_section_index = @intCast(u16, data_seg.sections.items.len);439 self.bss_section_index = @intCast(u16, data_seg.sections.items.len);
440 try data_seg.append(self.allocator, .{440 try data_seg.addSection(self.allocator, .{
441 .sectname = makeStaticString("__bss"),441 .sectname = makeStaticString("__bss"),
442 .segname = makeStaticString("__DATA"),442 .segname = makeStaticString("__DATA"),
443 .addr = 0,443 .addr = 0,
...@@ -457,7 +457,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {...@@ -457,7 +457,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {
457 if (self.tlv_section_index != null) continue;457 if (self.tlv_section_index != null) continue;
458458
459 self.tlv_section_index = @intCast(u16, data_seg.sections.items.len);459 self.tlv_section_index = @intCast(u16, data_seg.sections.items.len);
460 try data_seg.append(self.allocator, .{460 try data_seg.addSection(self.allocator, .{
461 .sectname = makeStaticString("__thread_vars"),461 .sectname = makeStaticString("__thread_vars"),
462 .segname = makeStaticString("__DATA"),462 .segname = makeStaticString("__DATA"),
463 .addr = 0,463 .addr = 0,
...@@ -477,7 +477,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {...@@ -477,7 +477,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {
477 if (self.tlv_data_section_index != null) continue;477 if (self.tlv_data_section_index != null) continue;
478478
479 self.tlv_data_section_index = @intCast(u16, data_seg.sections.items.len);479 self.tlv_data_section_index = @intCast(u16, data_seg.sections.items.len);
480 try data_seg.append(self.allocator, .{480 try data_seg.addSection(self.allocator, .{
481 .sectname = makeStaticString("__thread_data"),481 .sectname = makeStaticString("__thread_data"),
482 .segname = makeStaticString("__DATA"),482 .segname = makeStaticString("__DATA"),
483 .addr = 0,483 .addr = 0,
...@@ -497,7 +497,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {...@@ -497,7 +497,7 @@ fn updateMetadata(self: *Zld, object_id: u16) !void {
497 if (self.tlv_bss_section_index != null) continue;497 if (self.tlv_bss_section_index != null) continue;
498498
499 self.tlv_bss_section_index = @intCast(u16, data_seg.sections.items.len);499 self.tlv_bss_section_index = @intCast(u16, data_seg.sections.items.len);
500 try data_seg.append(self.allocator, .{500 try data_seg.addSection(self.allocator, .{
501 .sectname = makeStaticString("__thread_bss"),501 .sectname = makeStaticString("__thread_bss"),
502 .segname = makeStaticString("__DATA"),502 .segname = makeStaticString("__DATA"),
503 .addr = 0,503 .addr = 0,
...@@ -1842,7 +1842,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -1842,7 +1842,7 @@ fn populateMetadata(self: *Zld) !void {
1842 .aarch64 => 2,1842 .aarch64 => 2,
1843 else => unreachable, // unhandled architecture type1843 else => unreachable, // unhandled architecture type
1844 };1844 };
1845 try text_seg.append(self.allocator, .{1845 try text_seg.addSection(self.allocator, .{
1846 .sectname = makeStaticString("__text"),1846 .sectname = makeStaticString("__text"),
1847 .segname = makeStaticString("__TEXT"),1847 .segname = makeStaticString("__TEXT"),
1848 .addr = 0,1848 .addr = 0,
...@@ -1871,7 +1871,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -1871,7 +1871,7 @@ fn populateMetadata(self: *Zld) !void {
1871 .aarch64 => 3 * @sizeOf(u32),1871 .aarch64 => 3 * @sizeOf(u32),
1872 else => unreachable, // unhandled architecture type1872 else => unreachable, // unhandled architecture type
1873 };1873 };
1874 try text_seg.append(self.allocator, .{1874 try text_seg.addSection(self.allocator, .{
1875 .sectname = makeStaticString("__stubs"),1875 .sectname = makeStaticString("__stubs"),
1876 .segname = makeStaticString("__TEXT"),1876 .segname = makeStaticString("__TEXT"),
1877 .addr = 0,1877 .addr = 0,
...@@ -1900,7 +1900,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -1900,7 +1900,7 @@ fn populateMetadata(self: *Zld) !void {
1900 .aarch64 => 6 * @sizeOf(u32),1900 .aarch64 => 6 * @sizeOf(u32),
1901 else => unreachable,1901 else => unreachable,
1902 };1902 };
1903 try text_seg.append(self.allocator, .{1903 try text_seg.addSection(self.allocator, .{
1904 .sectname = makeStaticString("__stub_helper"),1904 .sectname = makeStaticString("__stub_helper"),
1905 .segname = makeStaticString("__TEXT"),1905 .segname = makeStaticString("__TEXT"),
1906 .addr = 0,1906 .addr = 0,
...@@ -1938,7 +1938,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -1938,7 +1938,7 @@ fn populateMetadata(self: *Zld) !void {
1938 if (self.got_section_index == null) {1938 if (self.got_section_index == null) {
1939 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;1939 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
1940 self.got_section_index = @intCast(u16, data_seg.sections.items.len);1940 self.got_section_index = @intCast(u16, data_seg.sections.items.len);
1941 try data_seg.append(self.allocator, .{1941 try data_seg.addSection(self.allocator, .{
1942 .sectname = makeStaticString("__got"),1942 .sectname = makeStaticString("__got"),
1943 .segname = makeStaticString("__DATA"),1943 .segname = makeStaticString("__DATA"),
1944 .addr = 0,1944 .addr = 0,
...@@ -1957,7 +1957,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -1957,7 +1957,7 @@ fn populateMetadata(self: *Zld) !void {
1957 if (self.la_symbol_ptr_section_index == null) {1957 if (self.la_symbol_ptr_section_index == null) {
1958 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;1958 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
1959 self.la_symbol_ptr_section_index = @intCast(u16, data_seg.sections.items.len);1959 self.la_symbol_ptr_section_index = @intCast(u16, data_seg.sections.items.len);
1960 try data_seg.append(self.allocator, .{1960 try data_seg.addSection(self.allocator, .{
1961 .sectname = makeStaticString("__la_symbol_ptr"),1961 .sectname = makeStaticString("__la_symbol_ptr"),
1962 .segname = makeStaticString("__DATA"),1962 .segname = makeStaticString("__DATA"),
1963 .addr = 0,1963 .addr = 0,
...@@ -1976,7 +1976,7 @@ fn populateMetadata(self: *Zld) !void {...@@ -1976,7 +1976,7 @@ fn populateMetadata(self: *Zld) !void {
1976 if (self.data_section_index == null) {1976 if (self.data_section_index == null) {
1977 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;1977 const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment;
1978 self.data_section_index = @intCast(u16, data_seg.sections.items.len);1978 self.data_section_index = @intCast(u16, data_seg.sections.items.len);
1979 try data_seg.append(self.allocator, .{1979 try data_seg.addSection(self.allocator, .{
1980 .sectname = makeStaticString("__data"),1980 .sectname = makeStaticString("__data"),
1981 .segname = makeStaticString("__DATA"),1981 .segname = makeStaticString("__DATA"),
1982 .addr = 0,1982 .addr = 0,
...@@ -2290,41 +2290,8 @@ fn writeRebaseInfoTable(self: *Zld) !void {...@@ -2290,41 +2290,8 @@ fn writeRebaseInfoTable(self: *Zld) !void {
2290 try pointers.ensureCapacity(pointers.items.len + self.local_rebases.items.len);2290 try pointers.ensureCapacity(pointers.items.len + self.local_rebases.items.len);
2291 pointers.appendSliceAssumeCapacity(self.local_rebases.items);2291 pointers.appendSliceAssumeCapacity(self.local_rebases.items);
22922292
2293 // const text_seg = self.load_commands.items[self.text_segment_cmd_index.?].Segment;
2294 // const base_id = text_seg.sections.items.len;
2295 // for (self.locals.items()) |entry| {
2296 // for (entry.value.items) |symbol| {
2297 // const local = symbol.inner;
2298
2299 // if (self.data_const_section_index) |index| {
2300 // if (local.n_sect == base_id + index) {
2301 // const offset = local.n_value - data_seg.inner.vmaddr;
2302 // try pointers.append(.{
2303 // .offset = offset,
2304 // .segment_id = @intCast(u16, self.data_segment_cmd_index.?),
2305 // });
2306 // }
2307 // }
2308 // if (self.data_section_index) |index| {
2309 // if (local.n_sect == base_id + index) {
2310 // const offset = local.n_value - data_seg.inner.vmaddr;
2311 // try pointers.append(.{
2312 // .offset = offset,
2313 // .segment_id = @intCast(u16, self.data_segment_cmd_index.?),
2314 // });
2315 // }
2316 // }
2317 // }
2318 // }
2319
2320 std.sort.sort(Pointer, pointers.items, {}, pointerCmp);2293 std.sort.sort(Pointer, pointers.items, {}, pointerCmp);
23212294
2322 // const nlocals = self.local_rebases.items.len;
2323 // var i = nlocals;
2324 // while (i > 0) : (i -= 1) {
2325 // pointers.appendAssumeCapacity(self.local_rebases.items[i - 1]);
2326 // }
2327
2328 const size = try rebaseInfoSize(pointers.items);2295 const size = try rebaseInfoSize(pointers.items);
2329 var buffer = try self.allocator.alloc(u8, @intCast(usize, size));2296 var buffer = try self.allocator.alloc(u8, @intCast(usize, size));
2330 defer self.allocator.free(buffer);2297 defer self.allocator.free(buffer);
...@@ -2698,21 +2665,8 @@ fn writeSymbolTable(self: *Zld) !void {...@@ -2698,21 +2665,8 @@ fn writeSymbolTable(self: *Zld) !void {
2698 log.debug(" | {}", .{entry.inner});2665 log.debug(" | {}", .{entry.inner});
2699 log.debug(" | {}", .{entry.tt});2666 log.debug(" | {}", .{entry.tt});
2700 log.debug(" | {s}", .{self.objects.items[entry.object_id].name});2667 log.debug(" | {s}", .{self.objects.items[entry.object_id].name});
2701 // switch (entry.tt) {
2702 // .Global => {
2703 // symbol = entry.inner;
2704 // break;
2705 // },
2706 // .WeakGlobal => {
2707 // symbol = entry.inner;
2708 // },
2709 // .Local => {},
2710 // }
2711 try locals.append(entry.inner);2668 try locals.append(entry.inner);
2712 }2669 }
2713 // if (symbol) |s| {
2714 // try locals.append(s);
2715 // }
2716 }2670 }
2717 const nlocals = locals.items.len;2671 const nlocals = locals.items.len;
27182672
src/link/MachO/commands.zig-5
...@@ -166,11 +166,6 @@ pub const SegmentCommand = struct {...@@ -166,11 +166,6 @@ pub const SegmentCommand = struct {
166 return .{ .inner = inner };166 return .{ .inner = inner };
167 }167 }
168168
169 // TODO remove me, I'm just a temp!
170 pub fn append(self: *SegmentCommand, alloc: *Allocator, section: macho.section_64) !void {
171 return self.addSection(alloc, section);
172 }
173
174 pub fn addSection(self: *SegmentCommand, alloc: *Allocator, section: macho.section_64) !void {169 pub fn addSection(self: *SegmentCommand, alloc: *Allocator, section: macho.section_64) !void {
175 try self.sections.append(alloc, section);170 try self.sections.append(alloc, section);
176 self.inner.cmdsize += @sizeOf(macho.section_64);171 self.inner.cmdsize += @sizeOf(macho.section_64);