authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-28 09:38:58+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-30 10:42:21+02:00
logff0abad2a9b9701287818684bb60d638a97172f2
tree80f63f6000e463f51ff89435d315845b7a09f41f
parent3aa99f45b8d9b22a2d2d09457f19ca3ef20764f6

coff: allow for strtab in final PE image

I believe this is going to be vital for section headers having names that require the use of a string table.

2 files changed, 34 insertions(+), 12 deletions(-)

src/link/Coff.zig+30-12
......@@ -49,6 +49,7 @@ globals: std.StringArrayHashMapUnmanaged(SymbolWithLoc) = .{},
4949locals_free_list: std.ArrayListUnmanaged(u32) = .{},
5050
5151strtab: StringTable(.strtab) = .{},
52strtab_offset: ?u32 = null,
5253
5354got_entries: std.AutoArrayHashMapUnmanaged(SymbolWithLoc, u32) = .{},
5455got_entries_free_list: std.ArrayListUnmanaged(u32) = .{},
......@@ -138,17 +139,6 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
138139 });
139140 self.base.file = file;
140141
141 // Index 0 is always a null symbol.
142 try self.locals.append(allocator, .{
143 .name = [_]u8{0} ** 8,
144 .value = 0,
145 .section_number = @intToEnum(coff.SectionNumber, 0),
146 .@"type" = .{ .base_type = .NULL, .complex_type = .NULL },
147 .storage_class = .NULL,
148 .number_of_aux_symbols = 0,
149 });
150 try self.strtab.buffer.append(allocator, 0);
151
152142 try self.populateMissingMetadata();
153143
154144 return self;
......@@ -209,8 +199,25 @@ pub fn deinit(self: *Coff) void {
209199}
210200
211201fn populateMissingMetadata(self: *Coff) !void {
202 const gpa = self.base.allocator;
203
212204 if (self.text_section_index == null) {}
205
213206 if (self.got_section_index == null) {}
207
208 if (self.strtab_offset == null) {
209 try self.strtab.buffer.append(gpa, 0);
210 }
211
212 // Index 0 is always a null symbol.
213 try self.locals.append(gpa, .{
214 .name = [_]u8{0} ** 8,
215 .value = 0,
216 .section_number = @intToEnum(coff.SectionNumber, 0),
217 .@"type" = .{ .base_type = .NULL, .complex_type = .NULL },
218 .storage_class = .NULL,
219 .number_of_aux_symbols = 0,
220 });
214221}
215222
216223pub fn allocateDeclIndexes(self: *Coff, decl_index: Module.Decl.Index) !void {
......@@ -733,7 +740,7 @@ fn writeHeader(self: *Coff) !void {
733740 .machine = coff.MachineType.fromTargetCpuArch(self.base.options.target.cpu.arch),
734741 .number_of_sections = @intCast(u16, self.sections.slice().len), // TODO what if we prune a section
735742 .time_date_stamp = 0, // TODO
736 .pointer_to_symbol_table = 0,
743 .pointer_to_symbol_table = self.strtab_offset orelse 0,
737744 .number_of_symbols = 0,
738745 .size_of_optional_header = size_of_optional_header,
739746 .flags = flags,
......@@ -846,6 +853,14 @@ fn detectAllocCollision(self: *Coff, start: u64, size: u64) ?u64 {
846853
847854 const end = start + padToIdeal(size);
848855
856 if (self.strtab_offset) |off| {
857 const increased_size = padToIdeal(self.strtab.len());
858 const test_end = off + increased_size;
859 if (end > off and start < test_end) {
860 return test_end;
861 }
862 }
863
849864 for (self.sections.items(.header)) |header| {
850865 const increased_size = padToIdeal(header.size_of_raw_data);
851866 const test_end = header.pointer_to_raw_data + increased_size;
......@@ -861,6 +876,9 @@ pub fn allocatedSize(self: *Coff, start: u64) u64 {
861876 if (start == 0)
862877 return 0;
863878 var min_pos: u64 = std.math.maxInt(u64);
879 if (self.strtab_offset) |off| {
880 if (off > start and off < min_pos) min_pos = off;
881 }
864882 for (self.sections.items(.header)) |header| {
865883 if (header.pointer_to_raw_data <= start) continue;
866884 if (header.pointer_to_raw_data < min_pos) min_pos = header.pointer_to_raw_data;
src/link/strtab.zig+4
......@@ -109,5 +109,9 @@ pub fn StringTable(comptime log_scope: @Type(.EnumLiteral)) type {
109109 pub fn getAssumeExists(self: Self, off: u32) []const u8 {
110110 return self.get(off) orelse unreachable;
111111 }
112
113 pub fn len(self: Self) usize {
114 return self.buffer.items.len;
115 }
112116 };
113117}