authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-28 10:15:27+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-30 10:42:21+02:00
logf36029a3851756d6e98eb486c89dc741db79752e
tree534018563a339ded81244e8ce7853b8bb53c9be0
parentff0abad2a9b9701287818684bb60d638a97172f2

coff: add helpers for setting section/symbol names


1 files changed, 36 insertions(+), 5 deletions(-)

src/link/Coff.zig+36-5
......@@ -5,6 +5,7 @@ const build_options = @import("build_options");
55const builtin = @import("builtin");
66const assert = std.debug.assert;
77const coff = std.coff;
8const fmt = std.fmt;
89const log = std.log.scoped(.link);
910const math = std.math;
1011const mem = std.mem;
......@@ -36,6 +37,7 @@ base: link.File,
3637error_flags: link.File.ErrorFlags = .{},
3738
3839ptr_width: PtrWidth,
40page_size: u32,
3941
4042sections: std.MultiArrayList(Section) = .{},
4143data_directories: [16]coff.ImageDataDirectory,
......@@ -69,7 +71,6 @@ managed_atoms: std.ArrayListUnmanaged(*Atom) = .{},
6971/// Table of atoms indexed by the symbol index.
7072atom_by_index_table: std.AutoHashMapUnmanaged(u32, *Atom) = .{},
7173
72const default_section_alignment: u16 = 0x1000;
7374const default_file_alignment: u16 = 0x200;
7475const default_image_base_dll: u64 = 0x10000000;
7576const default_image_base_exe: u64 = 0x10000;
......@@ -150,6 +151,9 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Coff {
150151 33...64 => .p64,
151152 else => return error.UnsupportedCOFFArchitecture,
152153 };
154 const page_size: u32 = switch (options.target.cpu.arch) {
155 else => 0x1000,
156 };
153157 const self = try gpa.create(Coff);
154158 errdefer gpa.destroy(self);
155159 self.* = .{
......@@ -160,6 +164,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Coff {
160164 .file = null,
161165 },
162166 .ptr_width = ptr_width,
167 .page_size = page_size,
163168 .data_directories = comptime mem.zeroes([16]coff.ImageDataDirectory),
164169 };
165170
......@@ -199,9 +204,15 @@ pub fn deinit(self: *Coff) void {
199204}
200205
201206fn populateMissingMetadata(self: *Coff) !void {
207 assert(self.llvm_object == null);
202208 const gpa = self.base.allocator;
203209
204 if (self.text_section_index == null) {}
210 if (self.text_section_index == null) {
211 self.text_section_index = @intCast(u16, self.sections.slice().len);
212 const file_size = self.base.options.program_code_size_hint;
213 const off = self.findFreeSpace(file_size, self.page_size); // TODO we are over-aligning in file; we should track both in file and in memory pointers
214 log.debug("found .text free space 0x{x} to 0x{x}", .{ off, off + file_size });
215 }
205216
206217 if (self.got_section_index == null) {}
207218
......@@ -756,7 +767,7 @@ fn writeHeader(self: *Coff) !void {
756767 };
757768 const subsystem: coff.Subsystem = .WINDOWS_CUI;
758769 const size_of_headers: u32 = @intCast(u32, self.getSizeOfHeaders());
759 const size_of_image_aligned: u32 = mem.alignForwardGeneric(u32, size_of_headers, default_section_alignment);
770 const size_of_image_aligned: u32 = mem.alignForwardGeneric(u32, size_of_headers, self.page_size);
760771 const size_of_headers_aligned: u32 = mem.alignForwardGeneric(u32, size_of_headers, default_file_alignment);
761772 const image_base = self.base.options.image_base_override orelse switch (self.base.options.output_mode) {
762773 .Exe => default_image_base_exe,
......@@ -777,7 +788,7 @@ fn writeHeader(self: *Coff) !void {
777788 .base_of_code = 0,
778789 .base_of_data = 0,
779790 .image_base = @intCast(u32, image_base),
780 .section_alignment = default_section_alignment,
791 .section_alignment = self.page_size,
781792 .file_alignment = default_file_alignment,
782793 .major_operating_system_version = 6,
783794 .minor_operating_system_version = 0,
......@@ -811,7 +822,7 @@ fn writeHeader(self: *Coff) !void {
811822 .address_of_entry_point = self.entry_addr orelse 0,
812823 .base_of_code = 0,
813824 .image_base = image_base,
814 .section_alignment = default_section_alignment,
825 .section_alignment = self.page_size,
815826 .file_alignment = default_file_alignment,
816827 .major_operating_system_version = 6,
817828 .minor_operating_system_version = 0,
......@@ -956,3 +967,23 @@ pub fn getGotAtomForSymbol(self: *Coff, sym_loc: SymbolWithLoc) ?*Atom {
956967 const got_index = self.got_entries.get(sym_loc) orelse return null;
957968 return self.atom_by_index_table.get(got_index);
958969}
970
971fn setSectionName(self: *Coff, header: *coff.SectionHeader, name: []const u8) !void {
972 if (name.len <= 8) {
973 mem.copy(u8, &header.name, name);
974 return;
975 }
976 const offset = try self.strtab.insert(self.base.allocator, name);
977 const name_offset = try fmt.bufPrint(&header.name, "/{d}", .{offset});
978 mem.set(u8, header.name[name_offset.len..], 0);
979}
980
981fn setSymbolName(self: *Coff, symbol: *coff.Symbol, name: []const u8) !void {
982 if (name.len <= 8) {
983 mem.copy(u8, &symbol.name, name);
984 return;
985 }
986 const offset = try self.strtab.insert(self.base.allocator, name);
987 mem.copy(u8, symbol.name[0..4], 0);
988 _ = try fmt.bufPrint(symbol.name[4..], "{d}", .{offset});
989}