authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-06 15:13:06+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-09-07 22:42:58+02:00
log16ca47b9b81c67c88d678b6230dd02ce9dad7f07
tree98f40d9eaad531311ac3b75182ce794ab1c6fea6
parent2b373b05793d617f308c7f99b35be7ad1ca0321f

coff: remove redundant bits and clean up


2 files changed, 15 insertions(+), 52 deletions(-)

src/link/Coff.zig+15-45
......@@ -30,7 +30,6 @@ const TypedValue = @import("../TypedValue.zig");
3030pub const base_tag: link.File.Tag = .coff;
3131
3232const msdos_stub = @embedFile("msdos-stub.bin");
33const N_DATA_DIRS: u5 = 16;
3433
3534/// If this is not null, an object file is created by LLVM and linked with LLD afterwards.
3635llvm_object: ?*LlvmObject = null,
......@@ -44,7 +43,7 @@ page_size: u32,
4443objects: std.ArrayListUnmanaged(Object) = .{},
4544
4645sections: std.MultiArrayList(Section) = .{},
47data_directories: [N_DATA_DIRS]coff.ImageDataDirectory,
46data_directories: [coff.IMAGE_NUMBEROF_DIRECTORY_ENTRIES]coff.ImageDataDirectory,
4847
4948text_section_index: ?u16 = null,
5049got_section_index: ?u16 = null,
......@@ -259,7 +258,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Coff {
259258 },
260259 .ptr_width = ptr_width,
261260 .page_size = page_size,
262 .data_directories = comptime mem.zeroes([N_DATA_DIRS]coff.ImageDataDirectory),
261 .data_directories = comptime mem.zeroes([coff.IMAGE_NUMBEROF_DIRECTORY_ENTRIES]coff.ImageDataDirectory),
263262 };
264263
265264 const use_llvm = build_options.have_llvm and options.use_llvm;
......@@ -421,7 +420,12 @@ fn populateMissingMetadata(self: *Coff) !void {
421420fn allocateSection(self: *Coff, name: []const u8, size: u32, flags: coff.SectionHeaderFlags) !u16 {
422421 const index = @intCast(u16, self.sections.slice().len);
423422 const off = self.findFreeSpace(size, default_file_alignment);
424 const vaddr = self.findFreeSpaceVM(size, self.page_size);
423 // Memory is always allocated in sequence
424 const vaddr = blk: {
425 if (index == 0) break :blk self.page_size;
426 const prev_header = self.sections.items(.header)[index - 1];
427 break :blk mem.alignForwardGeneric(u32, prev_header.virtual_address + prev_header.virtual_size, self.page_size);
428 };
425429 log.debug("found {s} free space 0x{x} to 0x{x} (0x{x} - 0x{x})", .{
426430 name,
427431 off,
......@@ -574,7 +578,7 @@ fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32) !u
574578 header.pointer_to_raw_data = new_offset;
575579 }
576580
577 const sect_vm_capacity = self.allocatedSizeVM(header.virtual_address);
581 const sect_vm_capacity = self.allocatedVirtualSize(header.virtual_address);
578582 if (needed_size > sect_vm_capacity) {
579583 try self.growSectionVM(sect_id, needed_size);
580584 self.markRelocsDirtyByAddress(header.virtual_address + needed_size);
......@@ -857,10 +861,9 @@ fn resolveRelocs(self: *Coff, atom: *Atom) !void {
857861fn freeAtom(self: *Coff, atom: *Atom) void {
858862 log.debug("freeAtom {*}", .{atom});
859863
860 // TODO hashmap
861 for (self.managed_atoms.items) |owned| {
862 if (owned == atom) break;
863 } else atom.deinit(self.base.allocator);
864 // Remove any relocs and base relocs associated with this Atom
865 _ = self.relocs.remove(atom);
866 _ = self.base_relocs.remove(atom);
864867
865868 const sym = atom.getSymbol(self);
866869 const sect_id = @enumToInt(sym.section_number) - 1;
......@@ -1577,7 +1580,7 @@ fn writeBaseRelocations(self: *Coff) !void {
15771580 });
15781581 header.pointer_to_raw_data = new_offset;
15791582
1580 const sect_vm_capacity = self.allocatedSizeVM(header.virtual_address);
1583 const sect_vm_capacity = self.allocatedVirtualSize(header.virtual_address);
15811584 if (needed_size > sect_vm_capacity) {
15821585 // TODO: we want to enforce .reloc after every alloc section.
15831586 try self.growSectionVM(self.reloc_section_index.?, needed_size);
......@@ -1862,7 +1865,7 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
18621865}
18631866
18641867fn detectAllocCollision(self: *Coff, start: u32, size: u32) ?u32 {
1865 const headers_size = @maximum(self.getSizeOfHeaders(), 0x1000);
1868 const headers_size = @maximum(self.getSizeOfHeaders(), self.page_size);
18661869 if (start < headers_size)
18671870 return headers_size;
18681871
......@@ -1911,7 +1914,7 @@ fn findFreeSpace(self: *Coff, object_size: u32, min_alignment: u32) u32 {
19111914 return start;
19121915}
19131916
1914fn allocatedSizeVM(self: *Coff, start: u32) u32 {
1917fn allocatedVirtualSize(self: *Coff, start: u32) u32 {
19151918 if (start == 0)
19161919 return 0;
19171920 var min_pos: u32 = std.math.maxInt(u32);
......@@ -1922,39 +1925,6 @@ fn allocatedSizeVM(self: *Coff, start: u32) u32 {
19221925 return min_pos - start;
19231926}
19241927
1925fn detectAllocCollisionVM(self: *Coff, start: u32, size: u32) ?u32 {
1926 const headers_size = @maximum(self.getSizeOfHeaders(), 0x1000);
1927 if (start < headers_size)
1928 return headers_size;
1929
1930 const end = start + size;
1931
1932 if (self.strtab_offset) |off| {
1933 const increased_size = @intCast(u32, self.strtab.len());
1934 const test_end = off + increased_size;
1935 if (end > off and start < test_end) {
1936 return test_end;
1937 }
1938 }
1939
1940 for (self.sections.items(.header)) |header| {
1941 const increased_size = header.virtual_size;
1942 const test_end = header.virtual_address + increased_size;
1943 if (end > header.virtual_address and start < test_end) {
1944 return test_end;
1945 }
1946 }
1947 return null;
1948}
1949
1950fn findFreeSpaceVM(self: *Coff, object_size: u32, min_alignment: u32) u32 {
1951 var start: u32 = 0;
1952 while (self.detectAllocCollisionVM(start, object_size)) |item_end| {
1953 start = mem.alignForwardGeneric(u32, item_end, min_alignment);
1954 }
1955 return start;
1956}
1957
19581928inline fn getSizeOfHeaders(self: Coff) u32 {
19591929 const msdos_hdr_size = msdos_stub.len + 4;
19601930 return @intCast(u32, msdos_hdr_size + @sizeOf(coff.CoffHeader) + self.getOptionalHeaderSize() +
src/link/Coff/Atom.zig-7
......@@ -4,8 +4,6 @@ const std = @import("std");
44const coff = std.coff;
55const log = std.log.scoped(.link);
66
7const Allocator = std.mem.Allocator;
8
97const Coff = @import("../Coff.zig");
108const Reloc = Coff.Reloc;
119const SymbolWithLoc = Coff.SymbolWithLoc;
......@@ -41,11 +39,6 @@ pub const empty = Atom{
4139 .next = null,
4240};
4341
44pub fn deinit(self: *Atom, gpa: Allocator) void {
45 _ = self;
46 _ = gpa;
47}
48
4942/// Returns symbol referencing this atom.
5043pub fn getSymbol(self: Atom, coff_file: *const Coff) *const coff.Symbol {
5144 return coff_file.getSymbol(.{