| ... | ... | @@ -30,7 +30,6 @@ const TypedValue = @import("../TypedValue.zig"); |
| 30 | 30 | pub const base_tag: link.File.Tag = .coff; |
| 31 | 31 | |
| 32 | 32 | const msdos_stub = @embedFile("msdos-stub.bin"); |
| 33 | | const N_DATA_DIRS: u5 = 16; |
| 34 | 33 | |
| 35 | 34 | /// If this is not null, an object file is created by LLVM and linked with LLD afterwards. |
| 36 | 35 | llvm_object: ?*LlvmObject = null, |
| ... | ... | @@ -44,7 +43,7 @@ page_size: u32, |
| 44 | 43 | objects: std.ArrayListUnmanaged(Object) = .{}, |
| 45 | 44 | |
| 46 | 45 | sections: std.MultiArrayList(Section) = .{}, |
| 47 | | data_directories: [N_DATA_DIRS]coff.ImageDataDirectory, |
| 46 | data_directories: [coff.IMAGE_NUMBEROF_DIRECTORY_ENTRIES]coff.ImageDataDirectory, |
| 48 | 47 | |
| 49 | 48 | text_section_index: ?u16 = null, |
| 50 | 49 | got_section_index: ?u16 = null, |
| ... | ... | @@ -259,7 +258,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Coff { |
| 259 | 258 | }, |
| 260 | 259 | .ptr_width = ptr_width, |
| 261 | 260 | .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), |
| 263 | 262 | }; |
| 264 | 263 | |
| 265 | 264 | const use_llvm = build_options.have_llvm and options.use_llvm; |
| ... | ... | @@ -421,7 +420,12 @@ fn populateMissingMetadata(self: *Coff) !void { |
| 421 | 420 | fn allocateSection(self: *Coff, name: []const u8, size: u32, flags: coff.SectionHeaderFlags) !u16 { |
| 422 | 421 | const index = @intCast(u16, self.sections.slice().len); |
| 423 | 422 | 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 | }; |
| 425 | 429 | log.debug("found {s} free space 0x{x} to 0x{x} (0x{x} - 0x{x})", .{ |
| 426 | 430 | name, |
| 427 | 431 | off, |
| ... | ... | @@ -574,7 +578,7 @@ fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32) !u |
| 574 | 578 | header.pointer_to_raw_data = new_offset; |
| 575 | 579 | } |
| 576 | 580 | |
| 577 | | const sect_vm_capacity = self.allocatedSizeVM(header.virtual_address); |
| 581 | const sect_vm_capacity = self.allocatedVirtualSize(header.virtual_address); |
| 578 | 582 | if (needed_size > sect_vm_capacity) { |
| 579 | 583 | try self.growSectionVM(sect_id, needed_size); |
| 580 | 584 | self.markRelocsDirtyByAddress(header.virtual_address + needed_size); |
| ... | ... | @@ -857,10 +861,9 @@ fn resolveRelocs(self: *Coff, atom: *Atom) !void { |
| 857 | 861 | fn freeAtom(self: *Coff, atom: *Atom) void { |
| 858 | 862 | log.debug("freeAtom {*}", .{atom}); |
| 859 | 863 | |
| 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); |
| 864 | 867 | |
| 865 | 868 | const sym = atom.getSymbol(self); |
| 866 | 869 | const sect_id = @enumToInt(sym.section_number) - 1; |
| ... | ... | @@ -1577,7 +1580,7 @@ fn writeBaseRelocations(self: *Coff) !void { |
| 1577 | 1580 | }); |
| 1578 | 1581 | header.pointer_to_raw_data = new_offset; |
| 1579 | 1582 | |
| 1580 | | const sect_vm_capacity = self.allocatedSizeVM(header.virtual_address); |
| 1583 | const sect_vm_capacity = self.allocatedVirtualSize(header.virtual_address); |
| 1581 | 1584 | if (needed_size > sect_vm_capacity) { |
| 1582 | 1585 | // TODO: we want to enforce .reloc after every alloc section. |
| 1583 | 1586 | try self.growSectionVM(self.reloc_section_index.?, needed_size); |
| ... | ... | @@ -1862,7 +1865,7 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { |
| 1862 | 1865 | } |
| 1863 | 1866 | |
| 1864 | 1867 | fn 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); |
| 1866 | 1869 | if (start < headers_size) |
| 1867 | 1870 | return headers_size; |
| 1868 | 1871 | |
| ... | ... | @@ -1911,7 +1914,7 @@ fn findFreeSpace(self: *Coff, object_size: u32, min_alignment: u32) u32 { |
| 1911 | 1914 | return start; |
| 1912 | 1915 | } |
| 1913 | 1916 | |
| 1914 | | fn allocatedSizeVM(self: *Coff, start: u32) u32 { |
| 1917 | fn allocatedVirtualSize(self: *Coff, start: u32) u32 { |
| 1915 | 1918 | if (start == 0) |
| 1916 | 1919 | return 0; |
| 1917 | 1920 | var min_pos: u32 = std.math.maxInt(u32); |
| ... | ... | @@ -1922,39 +1925,6 @@ fn allocatedSizeVM(self: *Coff, start: u32) u32 { |
| 1922 | 1925 | return min_pos - start; |
| 1923 | 1926 | } |
| 1924 | 1927 | |
| 1925 | | fn 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 | | |
| 1950 | | fn 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 | | |
| 1958 | 1928 | inline fn getSizeOfHeaders(self: Coff) u32 { |
| 1959 | 1929 | const msdos_hdr_size = msdos_stub.len + 4; |
| 1960 | 1930 | return @intCast(u32, msdos_hdr_size + @sizeOf(coff.CoffHeader) + self.getOptionalHeaderSize() + |