| ... | ... | @@ -1,39 +1,30 @@ |
| 1 | 1 | const Coff = @This(); |
| 2 | 2 | |
| 3 | 3 | const std = @import("std"); |
| 4 | const build_options = @import("build_options"); |
| 4 | 5 | const builtin = @import("builtin"); |
| 5 | | const log = std.log.scoped(.link); |
| 6 | | const Allocator = std.mem.Allocator; |
| 7 | 6 | const assert = std.debug.assert; |
| 8 | | const fs = std.fs; |
| 9 | | const allocPrint = std.fmt.allocPrint; |
| 7 | const coff = std.coff; |
| 8 | const log = std.log.scoped(.link); |
| 9 | const math = std.math; |
| 10 | 10 | const mem = std.mem; |
| 11 | 11 | |
| 12 | | const lldMain = @import("../main.zig").lldMain; |
| 13 | | const trace = @import("../tracy.zig").trace; |
| 14 | | const Module = @import("../Module.zig"); |
| 15 | | const Compilation = @import("../Compilation.zig"); |
| 12 | const Allocator = std.mem.Allocator; |
| 13 | |
| 16 | 14 | const codegen = @import("../codegen.zig"); |
| 17 | 15 | const link = @import("../link.zig"); |
| 18 | | const build_options = @import("build_options"); |
| 19 | | const Cache = @import("../Cache.zig"); |
| 20 | | const mingw = @import("../mingw.zig"); |
| 16 | const lld = @import("Coff/lld.zig"); |
| 17 | const trace = @import("../tracy.zig").trace; |
| 18 | |
| 21 | 19 | const Air = @import("../Air.zig"); |
| 20 | pub const Atom = @import("Coff/Atom.zig"); |
| 21 | const Compilation = @import("../Compilation.zig"); |
| 22 | 22 | const Liveness = @import("../Liveness.zig"); |
| 23 | 23 | const LlvmObject = @import("../codegen/llvm.zig").Object; |
| 24 | const Module = @import("../Module.zig"); |
| 25 | const StringTable = @import("strtab.zig").StringTable; |
| 24 | 26 | const TypedValue = @import("../TypedValue.zig"); |
| 25 | 27 | |
| 26 | | const allocation_padding = 4 / 3; |
| 27 | | const minimum_text_block_size = 64 * allocation_padding; |
| 28 | | |
| 29 | | const section_alignment = 4096; |
| 30 | | const file_alignment = 512; |
| 31 | | const default_image_base = 0x400_000; |
| 32 | | const section_table_size = 2 * 40; |
| 33 | | comptime { |
| 34 | | assert(mem.isAligned(default_image_base, section_alignment)); |
| 35 | | } |
| 36 | | |
| 37 | 28 | pub const base_tag: link.File.Tag = .coff; |
| 38 | 29 | |
| 39 | 30 | const msdos_stub = @embedFile("msdos-stub.bin"); |
| ... | ... | @@ -42,91 +33,94 @@ const msdos_stub = @embedFile("msdos-stub.bin"); |
| 42 | 33 | llvm_object: ?*LlvmObject = null, |
| 43 | 34 | |
| 44 | 35 | base: link.File, |
| 45 | | ptr_width: PtrWidth, |
| 46 | 36 | error_flags: link.File.ErrorFlags = .{}, |
| 47 | 37 | |
| 48 | | text_block_free_list: std.ArrayListUnmanaged(*TextBlock) = .{}, |
| 49 | | last_text_block: ?*TextBlock = null, |
| 50 | | |
| 51 | | /// Section table file pointer. |
| 52 | | section_table_offset: u32 = 0, |
| 53 | | /// Section data file pointer. |
| 54 | | section_data_offset: u32 = 0, |
| 55 | | /// Optional header file pointer. |
| 56 | | optional_header_offset: u32 = 0, |
| 57 | | |
| 58 | | /// Absolute virtual address of the offset table when the executable is loaded in memory. |
| 59 | | offset_table_virtual_address: u32 = 0, |
| 60 | | /// Current size of the offset table on disk, must be a multiple of `file_alignment` |
| 61 | | offset_table_size: u32 = 0, |
| 62 | | /// Contains absolute virtual addresses |
| 63 | | offset_table: std.ArrayListUnmanaged(u64) = .{}, |
| 64 | | /// Free list of offset table indices |
| 65 | | offset_table_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 66 | | |
| 67 | | /// Virtual address of the entry point procedure relative to image base. |
| 68 | | entry_addr: ?u32 = null, |
| 38 | ptr_width: PtrWidth, |
| 69 | 39 | |
| 70 | | /// Absolute virtual address of the text section when the executable is loaded in memory. |
| 71 | | text_section_virtual_address: u32 = 0, |
| 72 | | /// Current size of the `.text` section on disk, must be a multiple of `file_alignment` |
| 73 | | text_section_size: u32 = 0, |
| 40 | sections: std.MultiArrayList(Section) = .{}, |
| 74 | 41 | |
| 75 | | offset_table_size_dirty: bool = false, |
| 76 | | text_section_size_dirty: bool = false, |
| 77 | | /// This flag is set when the virtual size of the whole image file when loaded in memory has changed |
| 78 | | /// and needs to be updated in the optional header. |
| 79 | | size_of_image_dirty: bool = false, |
| 42 | text_section_index: ?u16 = null, |
| 43 | got_section_index: ?u16 = null, |
| 80 | 44 | |
| 81 | | pub const PtrWidth = enum { p32, p64 }; |
| 45 | locals: std.ArrayListUnmanaged(coff.Symbol) = .{}, |
| 46 | globals: std.StringArrayHashMapUnmanaged(SymbolWithLoc) = .{}, |
| 82 | 47 | |
| 83 | | pub const TextBlock = struct { |
| 84 | | /// Offset of the code relative to the start of the text section |
| 85 | | text_offset: u32, |
| 86 | | /// Used size of the text block |
| 87 | | size: u32, |
| 88 | | /// This field is undefined for symbols with size = 0. |
| 89 | | offset_table_index: u32, |
| 90 | | /// Points to the previous and next neighbors, based on the `text_offset`. |
| 91 | | /// This can be used to find, for example, the capacity of this `TextBlock`. |
| 92 | | prev: ?*TextBlock, |
| 93 | | next: ?*TextBlock, |
| 94 | | |
| 95 | | pub const empty = TextBlock{ |
| 96 | | .text_offset = 0, |
| 97 | | .size = 0, |
| 98 | | .offset_table_index = undefined, |
| 99 | | .prev = null, |
| 100 | | .next = null, |
| 101 | | }; |
| 48 | locals_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 102 | 49 | |
| 103 | | /// Returns how much room there is to grow in virtual address space. |
| 104 | | fn capacity(self: TextBlock) u64 { |
| 105 | | if (self.next) |next| { |
| 106 | | return next.text_offset - self.text_offset; |
| 107 | | } |
| 108 | | // This is the last block, the capacity is only limited by the address space. |
| 109 | | return std.math.maxInt(u32) - self.text_offset; |
| 110 | | } |
| 50 | strtab: StringTable(.strtab) = .{}, |
| 111 | 51 | |
| 112 | | fn freeListEligible(self: TextBlock) bool { |
| 113 | | // No need to keep a free list node for the last block. |
| 114 | | const next = self.next orelse return false; |
| 115 | | const cap = next.text_offset - self.text_offset; |
| 116 | | const ideal_cap = self.size * allocation_padding; |
| 117 | | if (cap <= ideal_cap) return false; |
| 118 | | const surplus = cap - ideal_cap; |
| 119 | | return surplus >= minimum_text_block_size; |
| 120 | | } |
| 52 | got_entries: std.AutoArrayHashMapUnmanaged(SymbolWithLoc, u32) = .{}, |
| 53 | got_entries_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 121 | 54 | |
| 122 | | /// Absolute virtual address of the text block when the file is loaded in memory. |
| 123 | | fn getVAddr(self: TextBlock, coff: Coff) u32 { |
| 124 | | return coff.text_section_virtual_address + self.text_offset; |
| 125 | | } |
| 55 | /// Virtual address of the entry point procedure relative to image base. |
| 56 | entry_addr: ?u64 = null, |
| 57 | |
| 58 | /// Table of Decls that are currently alive. |
| 59 | /// We store them here so that we can properly dispose of any allocated |
| 60 | /// memory within the atom in the incremental linker. |
| 61 | /// TODO consolidate this. |
| 62 | decls: std.AutoHashMapUnmanaged(Module.Decl.Index, ?u16) = .{}, |
| 63 | |
| 64 | /// List of atoms that are either synthetic or map directly to the Zig source program. |
| 65 | managed_atoms: std.ArrayListUnmanaged(*Atom) = .{}, |
| 66 | |
| 67 | /// Table of atoms indexed by the symbol index. |
| 68 | atom_by_index_table: std.AutoHashMapUnmanaged(u32, *Atom) = .{}, |
| 69 | |
| 70 | const page_size: u16 = 0x1000; |
| 71 | |
| 72 | const Section = struct { |
| 73 | header: coff.SectionHeader, |
| 74 | |
| 75 | last_atom: ?*Atom = null, |
| 76 | |
| 77 | /// A list of atoms that have surplus capacity. This list can have false |
| 78 | /// positives, as functions grow and shrink over time, only sometimes being added |
| 79 | /// or removed from the freelist. |
| 80 | /// |
| 81 | /// An atom has surplus capacity when its overcapacity value is greater than |
| 82 | /// padToIdeal(minimum_atom_size). That is, when it has so |
| 83 | /// much extra capacity, that we could fit a small new symbol in it, itself with |
| 84 | /// ideal_capacity or more. |
| 85 | /// |
| 86 | /// Ideal capacity is defined by size + (size / ideal_factor). |
| 87 | /// |
| 88 | /// Overcapacity is measured by actual_capacity - ideal_capacity. Note that |
| 89 | /// overcapacity can be negative. A simple way to have negative overcapacity is to |
| 90 | /// allocate a fresh atom, which will have ideal capacity, and then grow it |
| 91 | /// by 1 byte. It will then have -1 overcapacity. |
| 92 | free_list: std.ArrayListUnmanaged(*Atom) = .{}, |
| 126 | 93 | }; |
| 127 | 94 | |
| 95 | pub const PtrWidth = enum { p32, p64 }; |
| 128 | 96 | pub const SrcFn = void; |
| 129 | 97 | |
| 98 | pub const Export = struct { |
| 99 | sym_index: ?u32 = null, |
| 100 | }; |
| 101 | |
| 102 | pub const SymbolWithLoc = struct { |
| 103 | // Index into the respective symbol table. |
| 104 | sym_index: u32, |
| 105 | |
| 106 | // null means it's a synthetic global or Zig source. |
| 107 | file: ?u32 = null, |
| 108 | }; |
| 109 | |
| 110 | /// When allocating, the ideal_capacity is calculated by |
| 111 | /// actual_capacity + (actual_capacity / ideal_factor) |
| 112 | const ideal_factor = 3; |
| 113 | |
| 114 | /// In order for a slice of bytes to be considered eligible to keep metadata pointing at |
| 115 | /// it as a possible place to put new symbols, it must have enough room for this many bytes |
| 116 | /// (plus extra for reserved capacity). |
| 117 | const minimum_text_block_size = 64; |
| 118 | pub const min_text_capacity = padToIdeal(minimum_text_block_size); |
| 119 | |
| 120 | /// We commit 0x1000 = 4096 bytes of space to the headers. |
| 121 | /// This should be plenty for any potential future extensions. |
| 122 | const default_headerpad_size: u32 = 0x1000; |
| 123 | |
| 130 | 124 | pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Options) !*Coff { |
| 131 | 125 | assert(options.target.ofmt == .coff); |
| 132 | 126 | |
| ... | ... | @@ -144,25 +138,18 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option |
| 144 | 138 | }); |
| 145 | 139 | self.base.file = file; |
| 146 | 140 | |
| 147 | | const coff_file_header_offset: u32 = if (options.output_mode == .Exe) msdos_stub.len + 4 else 0; |
| 148 | | const default_offset_table_size = file_alignment; |
| 149 | | const data_directory_count = 0; |
| 150 | | const default_size_of_code = 0; |
| 151 | | const optional_header_size = switch (options.output_mode) { |
| 152 | | .Exe => data_directory_count * 8 + switch (self.ptr_width) { |
| 153 | | .p32 => @as(u16, 96), |
| 154 | | .p64 => 112, |
| 155 | | }, |
| 156 | | else => 0, |
| 157 | | }; |
| 158 | | const section_table_offset = coff_file_header_offset + 20 + optional_header_size; |
| 159 | | self.section_data_offset = mem.alignForwardGeneric(u32, section_table_offset + section_table_size, file_alignment); |
| 160 | | const section_data_relative_virtual_address = mem.alignForwardGeneric(u32, section_table_offset + section_table_size, section_alignment); |
| 161 | | self.offset_table_virtual_address = default_image_base + section_data_relative_virtual_address; |
| 162 | | self.offset_table_size = default_offset_table_size; |
| 163 | | self.section_table_offset = section_table_offset; |
| 164 | | self.text_section_virtual_address = default_image_base + section_data_relative_virtual_address + section_alignment; |
| 165 | | self.text_section_size = default_size_of_code; |
| 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 | |
| 152 | try self.populateMissingMetadata(); |
| 166 | 153 | |
| 167 | 154 | return self; |
| 168 | 155 | } |
| ... | ... | @@ -193,245 +180,259 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Coff { |
| 193 | 180 | return self; |
| 194 | 181 | } |
| 195 | 182 | |
| 196 | | pub fn allocateDeclIndexes(self: *Coff, decl_index: Module.Decl.Index) !void { |
| 197 | | if (self.llvm_object) |_| return; |
| 183 | pub fn deinit(self: *Coff) void { |
| 184 | const gpa = self.base.allocator; |
| 198 | 185 | |
| 199 | | try self.offset_table.ensureUnusedCapacity(self.base.allocator, 1); |
| 186 | if (build_options.have_llvm) { |
| 187 | if (self.llvm_object) |llvm_object| llvm_object.destroy(gpa); |
| 188 | } |
| 200 | 189 | |
| 201 | | const decl = self.base.options.module.?.declPtr(decl_index); |
| 202 | | if (self.offset_table_free_list.popOrNull()) |i| { |
| 203 | | decl.link.coff.offset_table_index = i; |
| 204 | | } else { |
| 205 | | decl.link.coff.offset_table_index = @intCast(u32, self.offset_table.items.len); |
| 206 | | _ = self.offset_table.addOneAssumeCapacity(); |
| 190 | for (self.sections.items(.free_list)) |*free_list| { |
| 191 | free_list.deinit(gpa); |
| 192 | } |
| 193 | self.sections.deinit(gpa); |
| 207 | 194 | |
| 208 | | const entry_size = self.base.options.target.cpu.arch.ptrBitWidth() / 8; |
| 209 | | if (self.offset_table.items.len > self.offset_table_size / entry_size) { |
| 210 | | self.offset_table_size_dirty = true; |
| 211 | | } |
| 195 | for (self.managed_atoms.items) |atom| { |
| 196 | gpa.destroy(atom); |
| 212 | 197 | } |
| 198 | self.managed_atoms.deinit(gpa); |
| 213 | 199 | |
| 214 | | self.offset_table.items[decl.link.coff.offset_table_index] = 0; |
| 200 | self.locals.deinit(gpa); |
| 201 | self.globals.deinit(gpa); |
| 202 | self.locals_free_list.deinit(gpa); |
| 203 | self.strtab.deinit(gpa); |
| 204 | self.got_entries.deinit(gpa); |
| 205 | self.got_entries_free_list.deinit(gpa); |
| 206 | self.decls.deinit(gpa); |
| 207 | self.atom_by_index_table.deinit(gpa); |
| 215 | 208 | } |
| 216 | 209 | |
| 217 | | fn allocateTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 { |
| 218 | | const new_block_min_capacity = new_block_size * allocation_padding; |
| 210 | fn populateMissingMetadata(self: *Coff) !void { |
| 211 | _ = self; |
| 212 | @panic("TODO populateMissingMetadata"); |
| 213 | } |
| 214 | |
| 215 | pub fn allocateDeclIndexes(self: *Coff, decl_index: Module.Decl.Index) !void { |
| 216 | if (self.llvm_object) |_| return; |
| 217 | const decl = self.base.options.module.?.declPtr(decl_index); |
| 218 | if (decl.link.coff.sym_index != 0) return; |
| 219 | decl.link.coff.sym_index = try self.allocateSymbol(); |
| 220 | const gpa = self.base.allocator; |
| 221 | try self.atom_by_index_table.putNoClobber(gpa, decl.link.coff.sym_index, &decl.link.coff); |
| 222 | try self.decls.putNoClobber(gpa, decl_index, null); |
| 223 | } |
| 224 | |
| 225 | fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u64, alignment: u64, sect_id: u16) !u64 { |
| 226 | const tracy = trace(@src()); |
| 227 | defer tracy.end(); |
| 219 | 228 | |
| 220 | | // We use these to indicate our intention to update metadata, placing the new block, |
| 229 | const header = &self.sections.items(.header)[sect_id]; |
| 230 | const free_list = &self.sections.items(.free_list)[sect_id]; |
| 231 | const maybe_last_atom = &self.sections.items(.last_atom)[sect_id]; |
| 232 | const new_atom_ideal_capacity = if (header.isCode()) padToIdeal(new_atom_size) else new_atom_size; |
| 233 | |
| 234 | // We use these to indicate our intention to update metadata, placing the new atom, |
| 221 | 235 | // and possibly removing a free list node. |
| 222 | 236 | // It would be simpler to do it inside the for loop below, but that would cause a |
| 223 | 237 | // problem if an error was returned later in the function. So this action |
| 224 | 238 | // is actually carried out at the end of the function, when errors are no longer possible. |
| 225 | | var block_placement: ?*TextBlock = null; |
| 239 | var atom_placement: ?*Atom = null; |
| 226 | 240 | var free_list_removal: ?usize = null; |
| 227 | 241 | |
| 228 | | const vaddr = blk: { |
| 242 | // First we look for an appropriately sized free list node. |
| 243 | // The list is unordered. We'll just take the first thing that works. |
| 244 | var vaddr = blk: { |
| 229 | 245 | var i: usize = 0; |
| 230 | | while (i < self.text_block_free_list.items.len) { |
| 231 | | const free_block = self.text_block_free_list.items[i]; |
| 232 | | |
| 233 | | const next_block_text_offset = free_block.text_offset + free_block.capacity(); |
| 234 | | const new_block_text_offset = mem.alignForwardGeneric(u64, free_block.getVAddr(self.*) + free_block.size, alignment) - self.text_section_virtual_address; |
| 235 | | if (new_block_text_offset < next_block_text_offset and next_block_text_offset - new_block_text_offset >= new_block_min_capacity) { |
| 236 | | block_placement = free_block; |
| 237 | | |
| 238 | | const remaining_capacity = next_block_text_offset - new_block_text_offset - new_block_min_capacity; |
| 239 | | if (remaining_capacity < minimum_text_block_size) { |
| 240 | | free_list_removal = i; |
| 241 | | } |
| 242 | | |
| 243 | | break :blk new_block_text_offset + self.text_section_virtual_address; |
| 244 | | } else { |
| 245 | | if (!free_block.freeListEligible()) { |
| 246 | | _ = self.text_block_free_list.swapRemove(i); |
| 246 | while (i < free_list.items.len) { |
| 247 | const big_atom = free_list.items[i]; |
| 248 | // We now have a pointer to a live atom that has too much capacity. |
| 249 | // Is it enough that we could fit this new atom? |
| 250 | const sym = big_atom.getSymbol(self); |
| 251 | const capacity = big_atom.capacity(self); |
| 252 | const ideal_capacity = if (header.isCode()) padToIdeal(capacity) else capacity; |
| 253 | const ideal_capacity_end_vaddr = math.add(u64, sym.n_value, ideal_capacity) catch ideal_capacity; |
| 254 | const capacity_end_vaddr = sym.n_value + capacity; |
| 255 | const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity; |
| 256 | const new_start_vaddr = mem.alignBackwardGeneric(u64, new_start_vaddr_unaligned, alignment); |
| 257 | if (new_start_vaddr < ideal_capacity_end_vaddr) { |
| 258 | // Additional bookkeeping here to notice if this free list node |
| 259 | // should be deleted because the atom that it points to has grown to take up |
| 260 | // more of the extra capacity. |
| 261 | if (!big_atom.freeListEligible(self)) { |
| 262 | _ = free_list.swapRemove(i); |
| 247 | 263 | } else { |
| 248 | 264 | i += 1; |
| 249 | 265 | } |
| 250 | 266 | continue; |
| 251 | 267 | } |
| 252 | | } else if (self.last_text_block) |last| { |
| 253 | | const new_block_vaddr = mem.alignForwardGeneric(u64, last.getVAddr(self.*) + last.size, alignment); |
| 254 | | block_placement = last; |
| 255 | | break :blk new_block_vaddr; |
| 268 | // At this point we know that we will place the new atom here. But the |
| 269 | // remaining question is whether there is still yet enough capacity left |
| 270 | // over for there to still be a free list node. |
| 271 | const remaining_capacity = new_start_vaddr - ideal_capacity_end_vaddr; |
| 272 | const keep_free_list_node = remaining_capacity >= min_text_capacity; |
| 273 | |
| 274 | // Set up the metadata to be updated, after errors are no longer possible. |
| 275 | atom_placement = big_atom; |
| 276 | if (!keep_free_list_node) { |
| 277 | free_list_removal = i; |
| 278 | } |
| 279 | break :blk new_start_vaddr; |
| 280 | } else if (maybe_last_atom.*) |last| { |
| 281 | const last_symbol = last.getSymbol(self); |
| 282 | const ideal_capacity = if (header.isCode()) padToIdeal(last.size) else last.size; |
| 283 | const ideal_capacity_end_vaddr = last_symbol.n_value + ideal_capacity; |
| 284 | const new_start_vaddr = mem.alignForwardGeneric(u64, ideal_capacity_end_vaddr, alignment); |
| 285 | atom_placement = last; |
| 286 | break :blk new_start_vaddr; |
| 256 | 287 | } else { |
| 257 | | break :blk self.text_section_virtual_address; |
| 288 | break :blk mem.alignForwardGeneric(u64, header.addr, alignment); |
| 258 | 289 | } |
| 259 | 290 | }; |
| 260 | 291 | |
| 261 | | const expand_text_section = block_placement == null or block_placement.?.next == null; |
| 262 | | if (expand_text_section) { |
| 263 | | const needed_size = @intCast(u32, mem.alignForwardGeneric(u64, vaddr + new_block_size - self.text_section_virtual_address, file_alignment)); |
| 264 | | if (needed_size > self.text_section_size) { |
| 265 | | const current_text_section_virtual_size = mem.alignForwardGeneric(u32, self.text_section_size, section_alignment); |
| 266 | | const new_text_section_virtual_size = mem.alignForwardGeneric(u32, needed_size, section_alignment); |
| 267 | | if (current_text_section_virtual_size != new_text_section_virtual_size) { |
| 268 | | self.size_of_image_dirty = true; |
| 269 | | // Write new virtual size |
| 270 | | var buf: [4]u8 = undefined; |
| 271 | | mem.writeIntLittle(u32, &buf, new_text_section_virtual_size); |
| 272 | | try self.base.file.?.pwriteAll(&buf, self.section_table_offset + 40 + 8); |
| 273 | | } |
| 292 | const expand_section = atom_placement == null or atom_placement.?.next == null; |
| 293 | if (expand_section) { |
| 294 | @panic("TODO expand section in allocateAtom"); |
| 295 | } |
| 274 | 296 | |
| 275 | | self.text_section_size = needed_size; |
| 276 | | self.text_section_size_dirty = true; |
| 277 | | } |
| 278 | | self.last_text_block = text_block; |
| 297 | if (header.getAlignment() < alignment) { |
| 298 | header.setAlignment(alignment); |
| 279 | 299 | } |
| 280 | | text_block.text_offset = @intCast(u32, vaddr - self.text_section_virtual_address); |
| 281 | | text_block.size = @intCast(u32, new_block_size); |
| 282 | | |
| 283 | | // This function can also reallocate a text block. |
| 284 | | // In this case we need to "unplug" it from its previous location before |
| 285 | | // plugging it in to its new location. |
| 286 | | if (text_block.prev) |prev| { |
| 287 | | prev.next = text_block.next; |
| 300 | atom.size = new_atom_size; |
| 301 | atom.alignment = alignment; |
| 302 | |
| 303 | if (atom.prev) |prev| { |
| 304 | prev.next = atom.next; |
| 288 | 305 | } |
| 289 | | if (text_block.next) |next| { |
| 290 | | next.prev = text_block.prev; |
| 306 | if (atom.next) |next| { |
| 307 | next.prev = atom.prev; |
| 291 | 308 | } |
| 292 | 309 | |
| 293 | | if (block_placement) |big_block| { |
| 294 | | text_block.prev = big_block; |
| 295 | | text_block.next = big_block.next; |
| 296 | | big_block.next = text_block; |
| 310 | if (atom_placement) |big_atom| { |
| 311 | atom.prev = big_atom; |
| 312 | atom.next = big_atom.next; |
| 313 | big_atom.next = atom; |
| 297 | 314 | } else { |
| 298 | | text_block.prev = null; |
| 299 | | text_block.next = null; |
| 315 | atom.prev = null; |
| 316 | atom.next = null; |
| 300 | 317 | } |
| 301 | 318 | if (free_list_removal) |i| { |
| 302 | | _ = self.text_block_free_list.swapRemove(i); |
| 319 | _ = free_list.swapRemove(i); |
| 303 | 320 | } |
| 321 | |
| 304 | 322 | return vaddr; |
| 305 | 323 | } |
| 306 | 324 | |
| 307 | | fn growTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 { |
| 308 | | const block_vaddr = text_block.getVAddr(self.*); |
| 309 | | const align_ok = mem.alignBackwardGeneric(u64, block_vaddr, alignment) == block_vaddr; |
| 310 | | const need_realloc = !align_ok or new_block_size > text_block.capacity(); |
| 311 | | if (!need_realloc) return @as(u64, block_vaddr); |
| 312 | | return self.allocateTextBlock(text_block, new_block_size, alignment); |
| 325 | fn allocateSymbol(self: *Coff) !u32 { |
| 326 | const gpa = self.base.allocator; |
| 327 | try self.locals.ensureUnusedCapacity(gpa, 1); |
| 328 | |
| 329 | const index = blk: { |
| 330 | if (self.locals_free_list.popOrNull()) |index| { |
| 331 | log.debug(" (reusing symbol index {d})", .{index}); |
| 332 | break :blk index; |
| 333 | } else { |
| 334 | log.debug(" (allocating symbol index {d})", .{self.locals.items.len}); |
| 335 | const index = @intCast(u32, self.locals.items.len); |
| 336 | _ = self.locals.addOneAssumeCapacity(); |
| 337 | break :blk index; |
| 338 | } |
| 339 | }; |
| 340 | |
| 341 | self.locals.items[index] = .{ |
| 342 | .name = [_]u8{0} ** 8, |
| 343 | .value = 0, |
| 344 | .section_number = @intToEnum(coff.SectionNumber, 0), |
| 345 | .@"type" = .{ .base_type = .NULL, .complex_type = .NULL }, |
| 346 | .storage_class = .NULL, |
| 347 | .number_of_aux_symbols = 0, |
| 348 | }; |
| 349 | |
| 350 | return index; |
| 313 | 351 | } |
| 314 | 352 | |
| 315 | | fn shrinkTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64) void { |
| 316 | | text_block.size = @intCast(u32, new_block_size); |
| 317 | | if (text_block.capacity() - text_block.size >= minimum_text_block_size) { |
| 318 | | self.text_block_free_list.append(self.base.allocator, text_block) catch {}; |
| 353 | pub fn allocateGotEntry(self: *Coff, target: SymbolWithLoc) !u32 { |
| 354 | const gpa = self.base.allocator; |
| 355 | try self.got_entries.ensureUnusedCapacity(gpa, 1); |
| 356 | if (self.got_entries_free_list.popOrNull()) |index| { |
| 357 | log.debug(" (reusing GOT entry index {d})", .{index}); |
| 358 | if (self.got_entries.getIndex(target)) |existing| { |
| 359 | assert(existing == index); |
| 360 | } |
| 361 | self.got_entries.keys()[index] = target; |
| 362 | return index; |
| 363 | } else { |
| 364 | log.debug(" (allocating GOT entry at index {d})", .{self.got_entries.keys().len}); |
| 365 | const index = @intCast(u32, self.got_entries.keys().len); |
| 366 | try self.got_entries.putAssumeCapacityNoClobber(target, 0); |
| 367 | return index; |
| 319 | 368 | } |
| 320 | 369 | } |
| 321 | 370 | |
| 322 | | fn freeTextBlock(self: *Coff, text_block: *TextBlock) void { |
| 371 | fn growAtom(self: *Coff, atom: *Atom, new_atom_size: u64, alignment: u64, sect_id: u16) !u64 { |
| 372 | const sym = atom.getSymbol(self); |
| 373 | const align_ok = mem.alignBackwardGeneric(u64, sym.value, alignment) == sym.value; |
| 374 | const need_realloc = !align_ok or new_atom_size > atom.capacity(self); |
| 375 | if (!need_realloc) return sym.value; |
| 376 | return self.allocateAtom(atom, new_atom_size, alignment, sect_id); |
| 377 | } |
| 378 | |
| 379 | fn shrinkAtom(self: *Coff, atom: *Atom, new_block_size: u64, sect_id: u16) void { |
| 380 | _ = self; |
| 381 | _ = atom; |
| 382 | _ = new_block_size; |
| 383 | _ = sect_id; |
| 384 | // TODO check the new capacity, and if it crosses the size threshold into a big enough |
| 385 | // capacity, insert a free list node for it. |
| 386 | } |
| 387 | |
| 388 | fn freeAtom(self: *Coff, atom: *Atom, sect_id: u16) void { |
| 389 | log.debug("freeAtom {*}", .{atom}); |
| 390 | |
| 391 | const free_list = &self.sections.items(.free_list)[sect_id]; |
| 323 | 392 | var already_have_free_list_node = false; |
| 324 | 393 | { |
| 325 | 394 | var i: usize = 0; |
| 326 | | // TODO turn text_block_free_list into a hash map |
| 327 | | while (i < self.text_block_free_list.items.len) { |
| 328 | | if (self.text_block_free_list.items[i] == text_block) { |
| 329 | | _ = self.text_block_free_list.swapRemove(i); |
| 395 | // TODO turn free_list into a hash map |
| 396 | while (i < free_list.items.len) { |
| 397 | if (free_list.items[i] == atom) { |
| 398 | _ = free_list.swapRemove(i); |
| 330 | 399 | continue; |
| 331 | 400 | } |
| 332 | | if (self.text_block_free_list.items[i] == text_block.prev) { |
| 401 | if (free_list.items[i] == atom.prev) { |
| 333 | 402 | already_have_free_list_node = true; |
| 334 | 403 | } |
| 335 | 404 | i += 1; |
| 336 | 405 | } |
| 337 | 406 | } |
| 338 | | if (self.last_text_block == text_block) { |
| 339 | | self.last_text_block = text_block.prev; |
| 340 | | } |
| 341 | | if (text_block.prev) |prev| { |
| 342 | | prev.next = text_block.next; |
| 343 | 407 | |
| 344 | | if (!already_have_free_list_node and prev.freeListEligible()) { |
| 345 | | // The free list is heuristics, it doesn't have to be perfect, so we can |
| 346 | | // ignore the OOM here. |
| 347 | | self.text_block_free_list.append(self.base.allocator, prev) catch {}; |
| 408 | const maybe_last_atom = &self.sections.items(.last_atom)[sect_id]; |
| 409 | if (maybe_last_atom.*) |last_atom| { |
| 410 | if (last_atom == atom) { |
| 411 | if (atom.prev) |prev| { |
| 412 | // TODO shrink the section size here |
| 413 | maybe_last_atom.* = prev; |
| 414 | } else { |
| 415 | maybe_last_atom.* = null; |
| 416 | } |
| 348 | 417 | } |
| 349 | 418 | } |
| 350 | 419 | |
| 351 | | if (text_block.next) |next| { |
| 352 | | next.prev = text_block.prev; |
| 353 | | } |
| 354 | | } |
| 420 | if (atom.prev) |prev| { |
| 421 | prev.next = atom.next; |
| 355 | 422 | |
| 356 | | fn writeOffsetTableEntry(self: *Coff, index: usize) !void { |
| 357 | | const entry_size = self.base.options.target.cpu.arch.ptrBitWidth() / 8; |
| 358 | | const endian = self.base.options.target.cpu.arch.endian(); |
| 359 | | |
| 360 | | const offset_table_start = self.section_data_offset; |
| 361 | | if (self.offset_table_size_dirty) { |
| 362 | | const current_raw_size = self.offset_table_size; |
| 363 | | const new_raw_size = self.offset_table_size * 2; |
| 364 | | log.debug("growing offset table from raw size {} to {}\n", .{ current_raw_size, new_raw_size }); |
| 365 | | |
| 366 | | // Move the text section to a new place in the executable |
| 367 | | const current_text_section_start = self.section_data_offset + current_raw_size; |
| 368 | | const new_text_section_start = self.section_data_offset + new_raw_size; |
| 369 | | |
| 370 | | const amt = try self.base.file.?.copyRangeAll(current_text_section_start, self.base.file.?, new_text_section_start, self.text_section_size); |
| 371 | | if (amt != self.text_section_size) return error.InputOutput; |
| 372 | | |
| 373 | | // Write the new raw size in the .got header |
| 374 | | var buf: [8]u8 = undefined; |
| 375 | | mem.writeIntLittle(u32, buf[0..4], new_raw_size); |
| 376 | | try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 16); |
| 377 | | // Write the new .text section file offset in the .text section header |
| 378 | | mem.writeIntLittle(u32, buf[0..4], new_text_section_start); |
| 379 | | try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 40 + 20); |
| 380 | | |
| 381 | | const current_virtual_size = mem.alignForwardGeneric(u32, self.offset_table_size, section_alignment); |
| 382 | | const new_virtual_size = mem.alignForwardGeneric(u32, new_raw_size, section_alignment); |
| 383 | | // If we had to move in the virtual address space, we need to fix the VAs in the offset table, as well as the virtual address of the `.text` section |
| 384 | | // and the virtual size of the `.got` section |
| 385 | | |
| 386 | | if (new_virtual_size != current_virtual_size) { |
| 387 | | log.debug("growing offset table from virtual size {} to {}\n", .{ current_virtual_size, new_virtual_size }); |
| 388 | | self.size_of_image_dirty = true; |
| 389 | | const va_offset = new_virtual_size - current_virtual_size; |
| 390 | | |
| 391 | | // Write .got virtual size |
| 392 | | mem.writeIntLittle(u32, buf[0..4], new_virtual_size); |
| 393 | | try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 8); |
| 394 | | |
| 395 | | // Write .text new virtual address |
| 396 | | self.text_section_virtual_address = self.text_section_virtual_address + va_offset; |
| 397 | | mem.writeIntLittle(u32, buf[0..4], self.text_section_virtual_address - default_image_base); |
| 398 | | try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 40 + 12); |
| 399 | | |
| 400 | | // Fix the VAs in the offset table |
| 401 | | for (self.offset_table.items) |*va, idx| { |
| 402 | | if (va.* != 0) { |
| 403 | | va.* += va_offset; |
| 404 | | |
| 405 | | switch (entry_size) { |
| 406 | | 4 => { |
| 407 | | mem.writeInt(u32, buf[0..4], @intCast(u32, va.*), endian); |
| 408 | | try self.base.file.?.pwriteAll(buf[0..4], offset_table_start + idx * entry_size); |
| 409 | | }, |
| 410 | | 8 => { |
| 411 | | mem.writeInt(u64, &buf, va.*, endian); |
| 412 | | try self.base.file.?.pwriteAll(&buf, offset_table_start + idx * entry_size); |
| 413 | | }, |
| 414 | | else => unreachable, |
| 415 | | } |
| 416 | | } |
| 417 | | } |
| 423 | if (!already_have_free_list_node and prev.freeListEligible(self)) { |
| 424 | // The free list is heuristics, it doesn't have to be perfect, so we can |
| 425 | // ignore the OOM here. |
| 426 | free_list.append(self.base.allocator, prev) catch {}; |
| 418 | 427 | } |
| 419 | | self.offset_table_size = new_raw_size; |
| 420 | | self.offset_table_size_dirty = false; |
| 428 | } else { |
| 429 | atom.prev = null; |
| 421 | 430 | } |
| 422 | | // Write the new entry |
| 423 | | switch (entry_size) { |
| 424 | | 4 => { |
| 425 | | var buf: [4]u8 = undefined; |
| 426 | | mem.writeInt(u32, &buf, @intCast(u32, self.offset_table.items[index]), endian); |
| 427 | | try self.base.file.?.pwriteAll(&buf, offset_table_start + index * entry_size); |
| 428 | | }, |
| 429 | | 8 => { |
| 430 | | var buf: [8]u8 = undefined; |
| 431 | | mem.writeInt(u64, &buf, self.offset_table.items[index], endian); |
| 432 | | try self.base.file.?.pwriteAll(&buf, offset_table_start + index * entry_size); |
| 433 | | }, |
| 434 | | else => unreachable, |
| 431 | |
| 432 | if (atom.next) |next| { |
| 433 | next.prev = atom.prev; |
| 434 | } else { |
| 435 | atom.next = null; |
| 435 | 436 | } |
| 436 | 437 | } |
| 437 | 438 | |
| ... | ... | @@ -470,15 +471,19 @@ pub fn updateFunc(self: *Coff, module: *Module, func: *Module.Fn, air: Air, live |
| 470 | 471 | }, |
| 471 | 472 | }; |
| 472 | 473 | |
| 473 | | return self.finishUpdateDecl(module, func.owner_decl, code); |
| 474 | const sym = try self.updateDeclCode(decl_index, code); |
| 475 | log.debug("updated decl code has sym {}", .{sym}); |
| 476 | |
| 477 | // Since we updated the vaddr and the size, each corresponding export symbol also needs to be updated. |
| 478 | const decl_exports = module.decl_exports.get(decl_index) orelse &[0]*Module.Export{}; |
| 479 | return self.updateDeclExports(module, decl_index, decl_exports); |
| 474 | 480 | } |
| 475 | 481 | |
| 476 | 482 | pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.Index) !u32 { |
| 477 | 483 | _ = self; |
| 478 | 484 | _ = tv; |
| 479 | 485 | _ = decl_index; |
| 480 | | log.debug("TODO lowerUnnamedConst for Coff", .{}); |
| 481 | | return error.AnalysisFail; |
| 486 | @panic("TODO lowerUnnamedConst"); |
| 482 | 487 | } |
| 483 | 488 | |
| 484 | 489 | pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !void { |
| ... | ... | @@ -503,9 +508,6 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) ! |
| 503 | 508 | } |
| 504 | 509 | } |
| 505 | 510 | |
| 506 | | // TODO COFF/PE debug information |
| 507 | | // TODO Implement exports |
| 508 | | |
| 509 | 511 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 510 | 512 | defer code_buffer.deinit(); |
| 511 | 513 | |
| ... | ... | @@ -526,49 +528,21 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) ! |
| 526 | 528 | }, |
| 527 | 529 | }; |
| 528 | 530 | |
| 529 | | return self.finishUpdateDecl(module, decl_index, code); |
| 530 | | } |
| 531 | | |
| 532 | | fn finishUpdateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index, code: []const u8) !void { |
| 533 | | const decl = module.declPtr(decl_index); |
| 534 | | const required_alignment = decl.ty.abiAlignment(self.base.options.target); |
| 535 | | const curr_size = decl.link.coff.size; |
| 536 | | if (curr_size != 0) { |
| 537 | | const capacity = decl.link.coff.capacity(); |
| 538 | | const need_realloc = code.len > capacity or |
| 539 | | !mem.isAlignedGeneric(u32, decl.link.coff.text_offset, required_alignment); |
| 540 | | if (need_realloc) { |
| 541 | | const curr_vaddr = self.text_section_virtual_address + decl.link.coff.text_offset; |
| 542 | | const vaddr = try self.growTextBlock(&decl.link.coff, code.len, required_alignment); |
| 543 | | log.debug("growing {s} from 0x{x} to 0x{x}\n", .{ decl.name, curr_vaddr, vaddr }); |
| 544 | | if (vaddr != curr_vaddr) { |
| 545 | | log.debug(" (writing new offset table entry)\n", .{}); |
| 546 | | self.offset_table.items[decl.link.coff.offset_table_index] = vaddr; |
| 547 | | try self.writeOffsetTableEntry(decl.link.coff.offset_table_index); |
| 548 | | } |
| 549 | | } else if (code.len < curr_size) { |
| 550 | | self.shrinkTextBlock(&decl.link.coff, code.len); |
| 551 | | } |
| 552 | | } else { |
| 553 | | const vaddr = try self.allocateTextBlock(&decl.link.coff, code.len, required_alignment); |
| 554 | | log.debug("allocated text block for {s} at 0x{x} (size: {Bi})\n", .{ |
| 555 | | mem.sliceTo(decl.name, 0), |
| 556 | | vaddr, |
| 557 | | std.fmt.fmtIntSizeDec(code.len), |
| 558 | | }); |
| 559 | | errdefer self.freeTextBlock(&decl.link.coff); |
| 560 | | self.offset_table.items[decl.link.coff.offset_table_index] = vaddr; |
| 561 | | try self.writeOffsetTableEntry(decl.link.coff.offset_table_index); |
| 562 | | } |
| 563 | | |
| 564 | | // Write the code into the file |
| 565 | | try self.base.file.?.pwriteAll(code, self.section_data_offset + self.offset_table_size + decl.link.coff.text_offset); |
| 531 | const sym = try self.updateDeclCode(decl_index, code); |
| 532 | log.debug("updated decl code for {}", .{sym}); |
| 566 | 533 | |
| 567 | 534 | // Since we updated the vaddr and the size, each corresponding export symbol also needs to be updated. |
| 568 | 535 | const decl_exports = module.decl_exports.get(decl_index) orelse &[0]*Module.Export{}; |
| 569 | 536 | return self.updateDeclExports(module, decl_index, decl_exports); |
| 570 | 537 | } |
| 571 | 538 | |
| 539 | fn updateDeclCode(self: *Coff, decl_index: Module.Decl.Index, code: []const u8) !*coff.Symbol { |
| 540 | _ = self; |
| 541 | _ = decl_index; |
| 542 | _ = code; |
| 543 | @panic("TODO updateDeclCode"); |
| 544 | } |
| 545 | |
| 572 | 546 | pub fn freeDecl(self: *Coff, decl_index: Module.Decl.Index) void { |
| 573 | 547 | if (build_options.have_llvm) { |
| 574 | 548 | if (self.llvm_object) |llvm_object| return llvm_object.freeDecl(decl_index); |
| ... | ... | @@ -577,9 +551,31 @@ pub fn freeDecl(self: *Coff, decl_index: Module.Decl.Index) void { |
| 577 | 551 | const mod = self.base.options.module.?; |
| 578 | 552 | const decl = mod.declPtr(decl_index); |
| 579 | 553 | |
| 554 | log.debug("freeDecl {*}", .{decl}); |
| 555 | |
| 556 | const kv = self.decls.fetchRemove(decl_index); |
| 557 | if (kv.?.value) |index| { |
| 558 | self.freeAtom(&decl.link.coff, index); |
| 559 | } |
| 560 | |
| 580 | 561 | // Appending to free lists is allowed to fail because the free lists are heuristics based anyway. |
| 581 | | self.freeTextBlock(&decl.link.coff); |
| 582 | | self.offset_table_free_list.append(self.base.allocator, decl.link.coff.offset_table_index) catch {}; |
| 562 | const gpa = self.base.allocator; |
| 563 | const sym_index = decl.link.coff.sym_index; |
| 564 | if (sym_index != 0) { |
| 565 | self.locals_free_list.append(gpa, sym_index) catch {}; |
| 566 | |
| 567 | // Try freeing GOT atom if this decl had one |
| 568 | const got_target = SymbolWithLoc{ .sym_index = sym_index, .file = null }; |
| 569 | if (self.got_entries.getIndex(got_target)) |got_index| { |
| 570 | self.got_entries_free_list.append(gpa, @intCast(u32, got_index)) catch {}; |
| 571 | self.got_entries.values()[got_index] = 0; |
| 572 | log.debug(" adding GOT index {d} to free list (target local@{d})", .{ got_index, sym_index }); |
| 573 | } |
| 574 | |
| 575 | self.locals.items[sym_index].section_number = @intToEnum(coff.SectionNumber, 0); |
| 576 | _ = self.atom_by_index_table.remove(sym_index); |
| 577 | decl.link.coff.sym_index = 0; |
| 578 | } |
| 583 | 579 | } |
| 584 | 580 | |
| 585 | 581 | pub fn updateDeclExports( |
| ... | ... | @@ -625,28 +621,7 @@ pub fn updateDeclExports( |
| 625 | 621 | if (self.llvm_object) |llvm_object| return llvm_object.updateDeclExports(module, decl_index, exports); |
| 626 | 622 | } |
| 627 | 623 | |
| 628 | | const decl = module.declPtr(decl_index); |
| 629 | | for (exports) |exp| { |
| 630 | | if (exp.options.section) |section_name| { |
| 631 | | if (!mem.eql(u8, section_name, ".text")) { |
| 632 | | try module.failed_exports.ensureUnusedCapacity(module.gpa, 1); |
| 633 | | module.failed_exports.putAssumeCapacityNoClobber( |
| 634 | | exp, |
| 635 | | try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: ExportOptions.section", .{}), |
| 636 | | ); |
| 637 | | continue; |
| 638 | | } |
| 639 | | } |
| 640 | | if (mem.eql(u8, exp.options.name, "wWinMainCRTStartup")) { |
| 641 | | self.entry_addr = decl.link.coff.getVAddr(self.*) - default_image_base; |
| 642 | | } else { |
| 643 | | try module.failed_exports.ensureUnusedCapacity(module.gpa, 1); |
| 644 | | module.failed_exports.putAssumeCapacityNoClobber( |
| 645 | | exp, |
| 646 | | try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: Exports other than 'wWinMainCRTStartup'", .{}), |
| 647 | | ); |
| 648 | | } |
| 649 | | } |
| 624 | @panic("TODO updateDeclExports"); |
| 650 | 625 | } |
| 651 | 626 | |
| 652 | 627 | pub fn flush(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| ... | ... | @@ -660,7 +635,7 @@ pub fn flush(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Node) !vo |
| 660 | 635 | } |
| 661 | 636 | const use_lld = build_options.have_llvm and self.base.options.use_lld; |
| 662 | 637 | if (use_lld) { |
| 663 | | return self.linkWithLLD(comp, prog_node); |
| 638 | return lld.linkWithLLD(self, comp, prog_node); |
| 664 | 639 | } |
| 665 | 640 | switch (self.base.options.output_mode) { |
| 666 | 641 | .Exe, .Obj => return self.flushModule(comp, prog_node), |
| ... | ... | @@ -682,888 +657,68 @@ pub fn flushModule(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod |
| 682 | 657 | sub_prog_node.activate(); |
| 683 | 658 | defer sub_prog_node.end(); |
| 684 | 659 | |
| 685 | | const output_mode = self.base.options.output_mode; |
| 686 | | log.debug("in flushModule with {}", .{output_mode}); |
| 687 | | |
| 688 | | var coff_file_header_offset: u32 = 0; |
| 689 | | if (output_mode == .Exe) { |
| 690 | | // Write the MS-DOS stub and the PE signature |
| 691 | | try self.base.file.?.pwriteAll(msdos_stub ++ "PE\x00\x00", 0); |
| 692 | | coff_file_header_offset = msdos_stub.len + 4; |
| 693 | | } |
| 694 | | |
| 695 | | // COFF file header |
| 696 | | const data_directory_count = 0; |
| 697 | | var hdr_data: [112 + data_directory_count * 8 + section_table_size]u8 = undefined; |
| 698 | | var index: usize = 0; |
| 699 | | |
| 700 | | const machine = self.base.options.target.cpu.arch.toCoffMachine(); |
| 701 | | if (machine == .Unknown) { |
| 702 | | return error.UnsupportedCOFFArchitecture; |
| 703 | | } |
| 704 | | mem.writeIntLittle(u16, hdr_data[0..2], @enumToInt(machine)); |
| 705 | | index += 2; |
| 706 | | |
| 707 | | // Number of sections (we only use .got, .text) |
| 708 | | mem.writeIntLittle(u16, hdr_data[index..][0..2], 2); |
| 709 | | index += 2; |
| 710 | | // TimeDateStamp (u32), PointerToSymbolTable (u32), NumberOfSymbols (u32) |
| 711 | | mem.set(u8, hdr_data[index..][0..12], 0); |
| 712 | | index += 12; |
| 713 | | |
| 714 | | const optional_header_size = switch (output_mode) { |
| 715 | | .Exe => data_directory_count * 8 + switch (self.ptr_width) { |
| 716 | | .p32 => @as(u16, 96), |
| 717 | | .p64 => 112, |
| 718 | | }, |
| 719 | | else => 0, |
| 720 | | }; |
| 721 | | |
| 722 | | const default_offset_table_size = file_alignment; |
| 723 | | const default_size_of_code = 0; |
| 724 | | |
| 725 | | // Size of file when loaded in memory |
| 726 | | const size_of_image = mem.alignForwardGeneric(u32, self.text_section_virtual_address - default_image_base + default_size_of_code, section_alignment); |
| 727 | | |
| 728 | | mem.writeIntLittle(u16, hdr_data[index..][0..2], optional_header_size); |
| 729 | | index += 2; |
| 730 | | |
| 731 | | // Characteristics |
| 732 | | var flags: std.coff.CoffHeaderFlags = .{ |
| 733 | | // TODO Remove debug info stripped flag when necessary |
| 734 | | .DEBUG_STRIPPED = 1, |
| 735 | | .RELOCS_STRIPPED = 1, |
| 736 | | }; |
| 737 | | if (output_mode == .Exe) { |
| 738 | | flags.EXECUTABLE_IMAGE = 1; |
| 739 | | } |
| 740 | | switch (self.ptr_width) { |
| 741 | | .p32 => flags.@"32BIT_MACHINE" = 1, |
| 742 | | .p64 => flags.LARGE_ADDRESS_AWARE = 1, |
| 743 | | } |
| 744 | | mem.writeIntLittle(u16, hdr_data[index..][0..2], @bitCast(u16, flags)); |
| 745 | | index += 2; |
| 746 | | |
| 747 | | assert(index == 20); |
| 748 | | try self.base.file.?.pwriteAll(hdr_data[0..index], coff_file_header_offset); |
| 749 | | |
| 750 | | if (output_mode == .Exe) { |
| 751 | | self.optional_header_offset = coff_file_header_offset + 20; |
| 752 | | // Optional header |
| 753 | | index = 0; |
| 754 | | mem.writeIntLittle(u16, hdr_data[0..2], switch (self.ptr_width) { |
| 755 | | .p32 => @as(u16, 0x10b), |
| 756 | | .p64 => 0x20b, |
| 757 | | }); |
| 758 | | index += 2; |
| 759 | | |
| 760 | | // Linker version (u8 + u8) |
| 761 | | mem.set(u8, hdr_data[index..][0..2], 0); |
| 762 | | index += 2; |
| 763 | | |
| 764 | | // SizeOfCode (UNUSED, u32), SizeOfInitializedData (u32), SizeOfUninitializedData (u32), AddressOfEntryPoint (u32), BaseOfCode (UNUSED, u32) |
| 765 | | mem.set(u8, hdr_data[index..][0..20], 0); |
| 766 | | index += 20; |
| 767 | | |
| 768 | | if (self.ptr_width == .p32) { |
| 769 | | // Base of data relative to the image base (UNUSED) |
| 770 | | mem.set(u8, hdr_data[index..][0..4], 0); |
| 771 | | index += 4; |
| 772 | | |
| 773 | | // Image base address |
| 774 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], default_image_base); |
| 775 | | index += 4; |
| 776 | | } else { |
| 777 | | // Image base address |
| 778 | | mem.writeIntLittle(u64, hdr_data[index..][0..8], default_image_base); |
| 779 | | index += 8; |
| 780 | | } |
| 781 | | |
| 782 | | // Section alignment |
| 783 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], section_alignment); |
| 784 | | index += 4; |
| 785 | | // File alignment |
| 786 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], file_alignment); |
| 787 | | index += 4; |
| 788 | | // Required OS version, 6.0 is vista |
| 789 | | mem.writeIntLittle(u16, hdr_data[index..][0..2], 6); |
| 790 | | index += 2; |
| 791 | | mem.writeIntLittle(u16, hdr_data[index..][0..2], 0); |
| 792 | | index += 2; |
| 793 | | // Image version |
| 794 | | mem.set(u8, hdr_data[index..][0..4], 0); |
| 795 | | index += 4; |
| 796 | | // Required subsystem version, same as OS version |
| 797 | | mem.writeIntLittle(u16, hdr_data[index..][0..2], 6); |
| 798 | | index += 2; |
| 799 | | mem.writeIntLittle(u16, hdr_data[index..][0..2], 0); |
| 800 | | index += 2; |
| 801 | | // Reserved zeroes (u32) |
| 802 | | mem.set(u8, hdr_data[index..][0..4], 0); |
| 803 | | index += 4; |
| 804 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], size_of_image); |
| 805 | | index += 4; |
| 806 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset); |
| 807 | | index += 4; |
| 808 | | // CheckSum (u32) |
| 809 | | mem.set(u8, hdr_data[index..][0..4], 0); |
| 810 | | index += 4; |
| 811 | | // Subsystem, TODO: Let users specify the subsystem, always CUI for now |
| 812 | | mem.writeIntLittle(u16, hdr_data[index..][0..2], 3); |
| 813 | | index += 2; |
| 814 | | // DLL characteristics |
| 815 | | mem.writeIntLittle(u16, hdr_data[index..][0..2], 0x0); |
| 816 | | index += 2; |
| 817 | | |
| 818 | | switch (self.ptr_width) { |
| 819 | | .p32 => { |
| 820 | | // Size of stack reserve + commit |
| 821 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000_000); |
| 822 | | index += 4; |
| 823 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000); |
| 824 | | index += 4; |
| 825 | | // Size of heap reserve + commit |
| 826 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x100_000); |
| 827 | | index += 4; |
| 828 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000); |
| 829 | | index += 4; |
| 830 | | }, |
| 831 | | .p64 => { |
| 832 | | // Size of stack reserve + commit |
| 833 | | mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x1_000_000); |
| 834 | | index += 8; |
| 835 | | mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x1_000); |
| 836 | | index += 8; |
| 837 | | // Size of heap reserve + commit |
| 838 | | mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x100_000); |
| 839 | | index += 8; |
| 840 | | mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x1_000); |
| 841 | | index += 8; |
| 842 | | }, |
| 843 | | } |
| 844 | | |
| 845 | | // Reserved zeroes |
| 846 | | mem.set(u8, hdr_data[index..][0..4], 0); |
| 847 | | index += 4; |
| 848 | | |
| 849 | | // Number of data directories |
| 850 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], data_directory_count); |
| 851 | | index += 4; |
| 852 | | // Initialize data directories to zero |
| 853 | | mem.set(u8, hdr_data[index..][0 .. data_directory_count * 8], 0); |
| 854 | | index += data_directory_count * 8; |
| 855 | | |
| 856 | | assert(index == optional_header_size); |
| 857 | | } |
| 858 | | |
| 859 | | // Write section table. |
| 860 | | // First, the .got section |
| 861 | | hdr_data[index..][0..8].* = ".got\x00\x00\x00\x00".*; |
| 862 | | index += 8; |
| 863 | | if (output_mode == .Exe) { |
| 864 | | // Virtual size (u32) |
| 865 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], default_offset_table_size); |
| 866 | | index += 4; |
| 867 | | // Virtual address (u32) |
| 868 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], self.offset_table_virtual_address - default_image_base); |
| 869 | | index += 4; |
| 870 | | } else { |
| 871 | | mem.set(u8, hdr_data[index..][0..8], 0); |
| 872 | | index += 8; |
| 873 | | } |
| 874 | | // Size of raw data (u32) |
| 875 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], default_offset_table_size); |
| 876 | | index += 4; |
| 877 | | // File pointer to the start of the section |
| 878 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset); |
| 879 | | index += 4; |
| 880 | | // Pointer to relocations (u32), PointerToLinenumbers (u32), NumberOfRelocations (u16), NumberOfLinenumbers (u16) |
| 881 | | mem.set(u8, hdr_data[index..][0..12], 0); |
| 882 | | index += 12; |
| 883 | | // Section flags |
| 884 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], @bitCast(u32, std.coff.SectionHeaderFlags{ |
| 885 | | .CNT_INITIALIZED_DATA = 1, |
| 886 | | .MEM_READ = 1, |
| 887 | | })); |
| 888 | | index += 4; |
| 889 | | // Then, the .text section |
| 890 | | hdr_data[index..][0..8].* = ".text\x00\x00\x00".*; |
| 891 | | index += 8; |
| 892 | | if (output_mode == .Exe) { |
| 893 | | // Virtual size (u32) |
| 894 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], default_size_of_code); |
| 895 | | index += 4; |
| 896 | | // Virtual address (u32) |
| 897 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], self.text_section_virtual_address - default_image_base); |
| 898 | | index += 4; |
| 899 | | } else { |
| 900 | | mem.set(u8, hdr_data[index..][0..8], 0); |
| 901 | | index += 8; |
| 902 | | } |
| 903 | | // Size of raw data (u32) |
| 904 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], default_size_of_code); |
| 905 | | index += 4; |
| 906 | | // File pointer to the start of the section |
| 907 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset + default_offset_table_size); |
| 908 | | index += 4; |
| 909 | | // Pointer to relocations (u32), PointerToLinenumbers (u32), NumberOfRelocations (u16), NumberOfLinenumbers (u16) |
| 910 | | mem.set(u8, hdr_data[index..][0..12], 0); |
| 911 | | index += 12; |
| 912 | | // Section flags |
| 913 | | mem.writeIntLittle(u32, hdr_data[index..][0..4], @bitCast(u32, std.coff.SectionHeaderFlags{ |
| 914 | | .CNT_CODE = 1, |
| 915 | | .MEM_EXECUTE = 1, |
| 916 | | .MEM_READ = 1, |
| 917 | | .MEM_WRITE = 1, |
| 918 | | })); |
| 919 | | index += 4; |
| 920 | | |
| 921 | | assert(index == optional_header_size + section_table_size); |
| 922 | | try self.base.file.?.pwriteAll(hdr_data[0..index], self.optional_header_offset); |
| 923 | | try self.base.file.?.setEndPos(self.section_data_offset + default_offset_table_size + default_size_of_code); |
| 924 | | |
| 925 | | if (self.text_section_size_dirty) { |
| 926 | | // Write the new raw size in the .text header |
| 927 | | var buf: [4]u8 = undefined; |
| 928 | | mem.writeIntLittle(u32, &buf, self.text_section_size); |
| 929 | | try self.base.file.?.pwriteAll(&buf, self.section_table_offset + 40 + 16); |
| 930 | | try self.base.file.?.setEndPos(self.section_data_offset + self.offset_table_size + self.text_section_size); |
| 931 | | self.text_section_size_dirty = false; |
| 932 | | } |
| 933 | | |
| 934 | | if (self.base.options.output_mode == .Exe and self.size_of_image_dirty) { |
| 935 | | const new_size_of_image = mem.alignForwardGeneric(u32, self.text_section_virtual_address - default_image_base + self.text_section_size, section_alignment); |
| 936 | | var buf: [4]u8 = undefined; |
| 937 | | mem.writeIntLittle(u32, &buf, new_size_of_image); |
| 938 | | try self.base.file.?.pwriteAll(&buf, self.optional_header_offset + 56); |
| 939 | | self.size_of_image_dirty = false; |
| 940 | | } |
| 941 | | |
| 942 | 660 | if (self.entry_addr == null and self.base.options.output_mode == .Exe) { |
| 943 | 661 | log.debug("flushing. no_entry_point_found = true\n", .{}); |
| 944 | 662 | self.error_flags.no_entry_point_found = true; |
| 945 | 663 | } else { |
| 946 | 664 | log.debug("flushing. no_entry_point_found = false\n", .{}); |
| 947 | 665 | self.error_flags.no_entry_point_found = false; |
| 948 | | |
| 949 | | if (self.base.options.output_mode == .Exe) { |
| 950 | | // Write AddressOfEntryPoint |
| 951 | | var buf: [4]u8 = undefined; |
| 952 | | mem.writeIntLittle(u32, &buf, self.entry_addr.?); |
| 953 | | try self.base.file.?.pwriteAll(&buf, self.optional_header_offset + 16); |
| 954 | | } |
| 955 | 666 | } |
| 956 | 667 | } |
| 957 | 668 | |
| 958 | | fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Node) !void { |
| 959 | | const tracy = trace(@src()); |
| 960 | | defer tracy.end(); |
| 961 | | |
| 962 | | var arena_allocator = std.heap.ArenaAllocator.init(self.base.allocator); |
| 963 | | defer arena_allocator.deinit(); |
| 964 | | const arena = arena_allocator.allocator(); |
| 965 | | |
| 966 | | const directory = self.base.options.emit.?.directory; // Just an alias to make it shorter to type. |
| 967 | | const full_out_path = try directory.join(arena, &[_][]const u8{self.base.options.emit.?.sub_path}); |
| 968 | | |
| 969 | | // If there is no Zig code to compile, then we should skip flushing the output file because it |
| 970 | | // will not be part of the linker line anyway. |
| 971 | | const module_obj_path: ?[]const u8 = if (self.base.options.module) |module| blk: { |
| 972 | | const use_stage1 = build_options.have_stage1 and self.base.options.use_stage1; |
| 973 | | if (use_stage1) { |
| 974 | | const obj_basename = try std.zig.binNameAlloc(arena, .{ |
| 975 | | .root_name = self.base.options.root_name, |
| 976 | | .target = self.base.options.target, |
| 977 | | .output_mode = .Obj, |
| 978 | | }); |
| 979 | | switch (self.base.options.cache_mode) { |
| 980 | | .incremental => break :blk try module.zig_cache_artifact_directory.join( |
| 981 | | arena, |
| 982 | | &[_][]const u8{obj_basename}, |
| 983 | | ), |
| 984 | | .whole => break :blk try fs.path.join(arena, &.{ |
| 985 | | fs.path.dirname(full_out_path).?, obj_basename, |
| 986 | | }), |
| 987 | | } |
| 988 | | } |
| 989 | | |
| 990 | | try self.flushModule(comp, prog_node); |
| 991 | | |
| 992 | | if (fs.path.dirname(full_out_path)) |dirname| { |
| 993 | | break :blk try fs.path.join(arena, &.{ dirname, self.base.intermediary_basename.? }); |
| 994 | | } else { |
| 995 | | break :blk self.base.intermediary_basename.?; |
| 996 | | } |
| 997 | | } else null; |
| 998 | | |
| 999 | | var sub_prog_node = prog_node.start("LLD Link", 0); |
| 1000 | | sub_prog_node.activate(); |
| 1001 | | sub_prog_node.context.refresh(); |
| 1002 | | defer sub_prog_node.end(); |
| 1003 | | |
| 1004 | | const is_lib = self.base.options.output_mode == .Lib; |
| 1005 | | const is_dyn_lib = self.base.options.link_mode == .Dynamic and is_lib; |
| 1006 | | const is_exe_or_dyn_lib = is_dyn_lib or self.base.options.output_mode == .Exe; |
| 1007 | | const link_in_crt = self.base.options.link_libc and is_exe_or_dyn_lib; |
| 1008 | | const target = self.base.options.target; |
| 1009 | | |
| 1010 | | // See link/Elf.zig for comments on how this mechanism works. |
| 1011 | | const id_symlink_basename = "lld.id"; |
| 1012 | | |
| 1013 | | var man: Cache.Manifest = undefined; |
| 1014 | | defer if (!self.base.options.disable_lld_caching) man.deinit(); |
| 1015 | | |
| 1016 | | var digest: [Cache.hex_digest_len]u8 = undefined; |
| 1017 | | |
| 1018 | | if (!self.base.options.disable_lld_caching) { |
| 1019 | | man = comp.cache_parent.obtain(); |
| 1020 | | self.base.releaseLock(); |
| 1021 | | |
| 1022 | | comptime assert(Compilation.link_hash_implementation_version == 7); |
| 1023 | | |
| 1024 | | for (self.base.options.objects) |obj| { |
| 1025 | | _ = try man.addFile(obj.path, null); |
| 1026 | | man.hash.add(obj.must_link); |
| 1027 | | } |
| 1028 | | for (comp.c_object_table.keys()) |key| { |
| 1029 | | _ = try man.addFile(key.status.success.object_path, null); |
| 1030 | | } |
| 1031 | | try man.addOptionalFile(module_obj_path); |
| 1032 | | man.hash.addOptionalBytes(self.base.options.entry); |
| 1033 | | man.hash.addOptional(self.base.options.stack_size_override); |
| 1034 | | man.hash.addOptional(self.base.options.image_base_override); |
| 1035 | | man.hash.addListOfBytes(self.base.options.lib_dirs); |
| 1036 | | man.hash.add(self.base.options.skip_linker_dependencies); |
| 1037 | | if (self.base.options.link_libc) { |
| 1038 | | man.hash.add(self.base.options.libc_installation != null); |
| 1039 | | if (self.base.options.libc_installation) |libc_installation| { |
| 1040 | | man.hash.addBytes(libc_installation.crt_dir.?); |
| 1041 | | if (target.abi == .msvc) { |
| 1042 | | man.hash.addBytes(libc_installation.msvc_lib_dir.?); |
| 1043 | | man.hash.addBytes(libc_installation.kernel32_lib_dir.?); |
| 1044 | | } |
| 1045 | | } |
| 1046 | | } |
| 1047 | | link.hashAddSystemLibs(&man.hash, self.base.options.system_libs); |
| 1048 | | man.hash.addListOfBytes(self.base.options.force_undefined_symbols.keys()); |
| 1049 | | man.hash.addOptional(self.base.options.subsystem); |
| 1050 | | man.hash.add(self.base.options.is_test); |
| 1051 | | man.hash.add(self.base.options.tsaware); |
| 1052 | | man.hash.add(self.base.options.nxcompat); |
| 1053 | | man.hash.add(self.base.options.dynamicbase); |
| 1054 | | // strip does not need to go into the linker hash because it is part of the hash namespace |
| 1055 | | man.hash.addOptional(self.base.options.major_subsystem_version); |
| 1056 | | man.hash.addOptional(self.base.options.minor_subsystem_version); |
| 1057 | | |
| 1058 | | // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock. |
| 1059 | | _ = try man.hit(); |
| 1060 | | digest = man.final(); |
| 1061 | | var prev_digest_buf: [digest.len]u8 = undefined; |
| 1062 | | const prev_digest: []u8 = Cache.readSmallFile( |
| 1063 | | directory.handle, |
| 1064 | | id_symlink_basename, |
| 1065 | | &prev_digest_buf, |
| 1066 | | ) catch |err| blk: { |
| 1067 | | log.debug("COFF LLD new_digest={s} error: {s}", .{ std.fmt.fmtSliceHexLower(&digest), @errorName(err) }); |
| 1068 | | // Handle this as a cache miss. |
| 1069 | | break :blk prev_digest_buf[0..0]; |
| 1070 | | }; |
| 1071 | | if (mem.eql(u8, prev_digest, &digest)) { |
| 1072 | | log.debug("COFF LLD digest={s} match - skipping invocation", .{std.fmt.fmtSliceHexLower(&digest)}); |
| 1073 | | // Hot diggity dog! The output binary is already there. |
| 1074 | | self.base.lock = man.toOwnedLock(); |
| 1075 | | return; |
| 1076 | | } |
| 1077 | | log.debug("COFF LLD prev_digest={s} new_digest={s}", .{ std.fmt.fmtSliceHexLower(prev_digest), std.fmt.fmtSliceHexLower(&digest) }); |
| 1078 | | |
| 1079 | | // We are about to change the output file to be different, so we invalidate the build hash now. |
| 1080 | | directory.handle.deleteFile(id_symlink_basename) catch |err| switch (err) { |
| 1081 | | error.FileNotFound => {}, |
| 1082 | | else => |e| return e, |
| 1083 | | }; |
| 1084 | | } |
| 1085 | | |
| 1086 | | if (self.base.options.output_mode == .Obj) { |
| 1087 | | // LLD's COFF driver does not support the equivalent of `-r` so we do a simple file copy |
| 1088 | | // here. TODO: think carefully about how we can avoid this redundant operation when doing |
| 1089 | | // build-obj. See also the corresponding TODO in linkAsArchive. |
| 1090 | | const the_object_path = blk: { |
| 1091 | | if (self.base.options.objects.len != 0) |
| 1092 | | break :blk self.base.options.objects[0].path; |
| 1093 | | |
| 1094 | | if (comp.c_object_table.count() != 0) |
| 1095 | | break :blk comp.c_object_table.keys()[0].status.success.object_path; |
| 1096 | | |
| 1097 | | if (module_obj_path) |p| |
| 1098 | | break :blk p; |
| 1099 | | |
| 1100 | | // TODO I think this is unreachable. Audit this situation when solving the above TODO |
| 1101 | | // regarding eliding redundant object -> object transformations. |
| 1102 | | return error.NoObjectsToLink; |
| 1103 | | }; |
| 1104 | | // This can happen when using --enable-cache and using the stage1 backend. In this case |
| 1105 | | // we can skip the file copy. |
| 1106 | | if (!mem.eql(u8, the_object_path, full_out_path)) { |
| 1107 | | try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{}); |
| 1108 | | } |
| 1109 | | } else { |
| 1110 | | // Create an LLD command line and invoke it. |
| 1111 | | var argv = std.ArrayList([]const u8).init(self.base.allocator); |
| 1112 | | defer argv.deinit(); |
| 1113 | | // We will invoke ourselves as a child process to gain access to LLD. |
| 1114 | | // This is necessary because LLD does not behave properly as a library - |
| 1115 | | // it calls exit() and does not reset all global data between invocations. |
| 1116 | | try argv.appendSlice(&[_][]const u8{ comp.self_exe_path.?, "lld-link" }); |
| 1117 | | |
| 1118 | | try argv.append("-ERRORLIMIT:0"); |
| 1119 | | try argv.append("-NOLOGO"); |
| 1120 | | if (!self.base.options.strip) { |
| 1121 | | try argv.append("-DEBUG"); |
| 1122 | | } |
| 1123 | | if (self.base.options.lto) { |
| 1124 | | switch (self.base.options.optimize_mode) { |
| 1125 | | .Debug => {}, |
| 1126 | | .ReleaseSmall => try argv.append("-OPT:lldlto=2"), |
| 1127 | | .ReleaseFast, .ReleaseSafe => try argv.append("-OPT:lldlto=3"), |
| 1128 | | } |
| 1129 | | } |
| 1130 | | if (self.base.options.output_mode == .Exe) { |
| 1131 | | const stack_size = self.base.options.stack_size_override orelse 16777216; |
| 1132 | | try argv.append(try allocPrint(arena, "-STACK:{d}", .{stack_size})); |
| 1133 | | } |
| 1134 | | if (self.base.options.image_base_override) |image_base| { |
| 1135 | | try argv.append(try std.fmt.allocPrint(arena, "-BASE:{d}", .{image_base})); |
| 1136 | | } |
| 1137 | | |
| 1138 | | if (target.cpu.arch == .i386) { |
| 1139 | | try argv.append("-MACHINE:X86"); |
| 1140 | | } else if (target.cpu.arch == .x86_64) { |
| 1141 | | try argv.append("-MACHINE:X64"); |
| 1142 | | } else if (target.cpu.arch.isARM()) { |
| 1143 | | if (target.cpu.arch.ptrBitWidth() == 32) { |
| 1144 | | try argv.append("-MACHINE:ARM"); |
| 1145 | | } else { |
| 1146 | | try argv.append("-MACHINE:ARM64"); |
| 1147 | | } |
| 1148 | | } |
| 1149 | | |
| 1150 | | for (self.base.options.force_undefined_symbols.keys()) |symbol| { |
| 1151 | | try argv.append(try allocPrint(arena, "-INCLUDE:{s}", .{symbol})); |
| 1152 | | } |
| 1153 | | |
| 1154 | | if (is_dyn_lib) { |
| 1155 | | try argv.append("-DLL"); |
| 1156 | | } |
| 1157 | | |
| 1158 | | if (self.base.options.entry) |entry| { |
| 1159 | | try argv.append(try allocPrint(arena, "-ENTRY:{s}", .{entry})); |
| 1160 | | } |
| 1161 | | |
| 1162 | | if (self.base.options.tsaware) { |
| 1163 | | try argv.append("-tsaware"); |
| 1164 | | } |
| 1165 | | if (self.base.options.nxcompat) { |
| 1166 | | try argv.append("-nxcompat"); |
| 1167 | | } |
| 1168 | | if (self.base.options.dynamicbase) { |
| 1169 | | try argv.append("-dynamicbase"); |
| 1170 | | } |
| 1171 | | |
| 1172 | | try argv.append(try allocPrint(arena, "-OUT:{s}", .{full_out_path})); |
| 1173 | | |
| 1174 | | if (self.base.options.implib_emit) |emit| { |
| 1175 | | const implib_out_path = try emit.directory.join(arena, &[_][]const u8{emit.sub_path}); |
| 1176 | | try argv.append(try allocPrint(arena, "-IMPLIB:{s}", .{implib_out_path})); |
| 1177 | | } |
| 1178 | | |
| 1179 | | if (self.base.options.link_libc) { |
| 1180 | | if (self.base.options.libc_installation) |libc_installation| { |
| 1181 | | try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{libc_installation.crt_dir.?})); |
| 1182 | | |
| 1183 | | if (target.abi == .msvc) { |
| 1184 | | try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{libc_installation.msvc_lib_dir.?})); |
| 1185 | | try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{libc_installation.kernel32_lib_dir.?})); |
| 1186 | | } |
| 1187 | | } |
| 1188 | | } |
| 1189 | | |
| 1190 | | for (self.base.options.lib_dirs) |lib_dir| { |
| 1191 | | try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{lib_dir})); |
| 1192 | | } |
| 1193 | | |
| 1194 | | try argv.ensureUnusedCapacity(self.base.options.objects.len); |
| 1195 | | for (self.base.options.objects) |obj| { |
| 1196 | | if (obj.must_link) { |
| 1197 | | argv.appendAssumeCapacity(try allocPrint(arena, "-WHOLEARCHIVE:{s}", .{obj.path})); |
| 1198 | | } else { |
| 1199 | | argv.appendAssumeCapacity(obj.path); |
| 1200 | | } |
| 1201 | | } |
| 1202 | | |
| 1203 | | for (comp.c_object_table.keys()) |key| { |
| 1204 | | try argv.append(key.status.success.object_path); |
| 1205 | | } |
| 1206 | | |
| 1207 | | if (module_obj_path) |p| { |
| 1208 | | try argv.append(p); |
| 1209 | | } |
| 1210 | | |
| 1211 | | const resolved_subsystem: ?std.Target.SubSystem = blk: { |
| 1212 | | if (self.base.options.subsystem) |explicit| break :blk explicit; |
| 1213 | | switch (target.os.tag) { |
| 1214 | | .windows => { |
| 1215 | | if (self.base.options.module) |module| { |
| 1216 | | if (module.stage1_flags.have_dllmain_crt_startup or is_dyn_lib) |
| 1217 | | break :blk null; |
| 1218 | | if (module.stage1_flags.have_c_main or self.base.options.is_test or |
| 1219 | | module.stage1_flags.have_winmain_crt_startup or |
| 1220 | | module.stage1_flags.have_wwinmain_crt_startup) |
| 1221 | | { |
| 1222 | | break :blk .Console; |
| 1223 | | } |
| 1224 | | if (module.stage1_flags.have_winmain or module.stage1_flags.have_wwinmain) |
| 1225 | | break :blk .Windows; |
| 1226 | | } |
| 1227 | | }, |
| 1228 | | .uefi => break :blk .EfiApplication, |
| 1229 | | else => {}, |
| 1230 | | } |
| 1231 | | break :blk null; |
| 1232 | | }; |
| 1233 | | |
| 1234 | | const Mode = enum { uefi, win32 }; |
| 1235 | | const mode: Mode = mode: { |
| 1236 | | if (resolved_subsystem) |subsystem| { |
| 1237 | | const subsystem_suffix = ss: { |
| 1238 | | if (self.base.options.major_subsystem_version) |major| { |
| 1239 | | if (self.base.options.minor_subsystem_version) |minor| { |
| 1240 | | break :ss try allocPrint(arena, ",{d}.{d}", .{ major, minor }); |
| 1241 | | } else { |
| 1242 | | break :ss try allocPrint(arena, ",{d}", .{major}); |
| 1243 | | } |
| 1244 | | } |
| 1245 | | break :ss ""; |
| 1246 | | }; |
| 1247 | | |
| 1248 | | switch (subsystem) { |
| 1249 | | .Console => { |
| 1250 | | try argv.append(try allocPrint(arena, "-SUBSYSTEM:console{s}", .{ |
| 1251 | | subsystem_suffix, |
| 1252 | | })); |
| 1253 | | break :mode .win32; |
| 1254 | | }, |
| 1255 | | .EfiApplication => { |
| 1256 | | try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_application{s}", .{ |
| 1257 | | subsystem_suffix, |
| 1258 | | })); |
| 1259 | | break :mode .uefi; |
| 1260 | | }, |
| 1261 | | .EfiBootServiceDriver => { |
| 1262 | | try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_boot_service_driver{s}", .{ |
| 1263 | | subsystem_suffix, |
| 1264 | | })); |
| 1265 | | break :mode .uefi; |
| 1266 | | }, |
| 1267 | | .EfiRom => { |
| 1268 | | try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_rom{s}", .{ |
| 1269 | | subsystem_suffix, |
| 1270 | | })); |
| 1271 | | break :mode .uefi; |
| 1272 | | }, |
| 1273 | | .EfiRuntimeDriver => { |
| 1274 | | try argv.append(try allocPrint(arena, "-SUBSYSTEM:efi_runtime_driver{s}", .{ |
| 1275 | | subsystem_suffix, |
| 1276 | | })); |
| 1277 | | break :mode .uefi; |
| 1278 | | }, |
| 1279 | | .Native => { |
| 1280 | | try argv.append(try allocPrint(arena, "-SUBSYSTEM:native{s}", .{ |
| 1281 | | subsystem_suffix, |
| 1282 | | })); |
| 1283 | | break :mode .win32; |
| 1284 | | }, |
| 1285 | | .Posix => { |
| 1286 | | try argv.append(try allocPrint(arena, "-SUBSYSTEM:posix{s}", .{ |
| 1287 | | subsystem_suffix, |
| 1288 | | })); |
| 1289 | | break :mode .win32; |
| 1290 | | }, |
| 1291 | | .Windows => { |
| 1292 | | try argv.append(try allocPrint(arena, "-SUBSYSTEM:windows{s}", .{ |
| 1293 | | subsystem_suffix, |
| 1294 | | })); |
| 1295 | | break :mode .win32; |
| 1296 | | }, |
| 1297 | | } |
| 1298 | | } else if (target.os.tag == .uefi) { |
| 1299 | | break :mode .uefi; |
| 1300 | | } else { |
| 1301 | | break :mode .win32; |
| 1302 | | } |
| 1303 | | }; |
| 1304 | | |
| 1305 | | switch (mode) { |
| 1306 | | .uefi => try argv.appendSlice(&[_][]const u8{ |
| 1307 | | "-BASE:0", |
| 1308 | | "-ENTRY:EfiMain", |
| 1309 | | "-OPT:REF", |
| 1310 | | "-SAFESEH:NO", |
| 1311 | | "-MERGE:.rdata=.data", |
| 1312 | | "-ALIGN:32", |
| 1313 | | "-NODEFAULTLIB", |
| 1314 | | "-SECTION:.xdata,D", |
| 1315 | | }), |
| 1316 | | .win32 => { |
| 1317 | | if (link_in_crt) { |
| 1318 | | if (target.abi.isGnu()) { |
| 1319 | | try argv.append("-lldmingw"); |
| 1320 | | |
| 1321 | | if (target.cpu.arch == .i386) { |
| 1322 | | try argv.append("-ALTERNATENAME:__image_base__=___ImageBase"); |
| 1323 | | } else { |
| 1324 | | try argv.append("-ALTERNATENAME:__image_base__=__ImageBase"); |
| 1325 | | } |
| 1326 | | |
| 1327 | | if (is_dyn_lib) { |
| 1328 | | try argv.append(try comp.get_libc_crt_file(arena, "dllcrt2.obj")); |
| 1329 | | if (target.cpu.arch == .i386) { |
| 1330 | | try argv.append("-ALTERNATENAME:__DllMainCRTStartup@12=_DllMainCRTStartup@12"); |
| 1331 | | } else { |
| 1332 | | try argv.append("-ALTERNATENAME:_DllMainCRTStartup=DllMainCRTStartup"); |
| 1333 | | } |
| 1334 | | } else { |
| 1335 | | try argv.append(try comp.get_libc_crt_file(arena, "crt2.obj")); |
| 1336 | | } |
| 1337 | | |
| 1338 | | try argv.append(try comp.get_libc_crt_file(arena, "mingw32.lib")); |
| 1339 | | try argv.append(try comp.get_libc_crt_file(arena, "mingwex.lib")); |
| 1340 | | try argv.append(try comp.get_libc_crt_file(arena, "msvcrt-os.lib")); |
| 1341 | | |
| 1342 | | for (mingw.always_link_libs) |name| { |
| 1343 | | if (!self.base.options.system_libs.contains(name)) { |
| 1344 | | const lib_basename = try allocPrint(arena, "{s}.lib", .{name}); |
| 1345 | | try argv.append(try comp.get_libc_crt_file(arena, lib_basename)); |
| 1346 | | } |
| 1347 | | } |
| 1348 | | } else { |
| 1349 | | const lib_str = switch (self.base.options.link_mode) { |
| 1350 | | .Dynamic => "", |
| 1351 | | .Static => "lib", |
| 1352 | | }; |
| 1353 | | const d_str = switch (self.base.options.optimize_mode) { |
| 1354 | | .Debug => "d", |
| 1355 | | else => "", |
| 1356 | | }; |
| 1357 | | switch (self.base.options.link_mode) { |
| 1358 | | .Static => try argv.append(try allocPrint(arena, "libcmt{s}.lib", .{d_str})), |
| 1359 | | .Dynamic => try argv.append(try allocPrint(arena, "msvcrt{s}.lib", .{d_str})), |
| 1360 | | } |
| 1361 | | |
| 1362 | | try argv.append(try allocPrint(arena, "{s}vcruntime{s}.lib", .{ lib_str, d_str })); |
| 1363 | | try argv.append(try allocPrint(arena, "{s}ucrt{s}.lib", .{ lib_str, d_str })); |
| 1364 | | |
| 1365 | | //Visual C++ 2015 Conformance Changes |
| 1366 | | //https://msdn.microsoft.com/en-us/library/bb531344.aspx |
| 1367 | | try argv.append("legacy_stdio_definitions.lib"); |
| 1368 | | |
| 1369 | | // msvcrt depends on kernel32 and ntdll |
| 1370 | | try argv.append("kernel32.lib"); |
| 1371 | | try argv.append("ntdll.lib"); |
| 1372 | | } |
| 1373 | | } else { |
| 1374 | | try argv.append("-NODEFAULTLIB"); |
| 1375 | | if (!is_lib) { |
| 1376 | | if (self.base.options.module) |module| { |
| 1377 | | if (module.stage1_flags.have_winmain_crt_startup) { |
| 1378 | | try argv.append("-ENTRY:WinMainCRTStartup"); |
| 1379 | | } else { |
| 1380 | | try argv.append("-ENTRY:wWinMainCRTStartup"); |
| 1381 | | } |
| 1382 | | } else { |
| 1383 | | try argv.append("-ENTRY:wWinMainCRTStartup"); |
| 1384 | | } |
| 1385 | | } |
| 1386 | | } |
| 1387 | | }, |
| 1388 | | } |
| 1389 | | |
| 1390 | | // libc++ dep |
| 1391 | | if (self.base.options.link_libcpp) { |
| 1392 | | try argv.append(comp.libcxxabi_static_lib.?.full_object_path); |
| 1393 | | try argv.append(comp.libcxx_static_lib.?.full_object_path); |
| 1394 | | } |
| 1395 | | |
| 1396 | | // libunwind dep |
| 1397 | | if (self.base.options.link_libunwind) { |
| 1398 | | try argv.append(comp.libunwind_static_lib.?.full_object_path); |
| 1399 | | } |
| 1400 | | |
| 1401 | | if (is_exe_or_dyn_lib and !self.base.options.skip_linker_dependencies) { |
| 1402 | | if (!self.base.options.link_libc) { |
| 1403 | | if (comp.libc_static_lib) |lib| { |
| 1404 | | try argv.append(lib.full_object_path); |
| 1405 | | } |
| 1406 | | } |
| 1407 | | // MinGW doesn't provide libssp symbols |
| 1408 | | if (target.abi.isGnu()) { |
| 1409 | | if (comp.libssp_static_lib) |lib| { |
| 1410 | | try argv.append(lib.full_object_path); |
| 1411 | | } |
| 1412 | | } |
| 1413 | | // MSVC compiler_rt is missing some stuff, so we build it unconditionally but |
| 1414 | | // and rely on weak linkage to allow MSVC compiler_rt functions to override ours. |
| 1415 | | if (comp.compiler_rt_lib) |lib| { |
| 1416 | | try argv.append(lib.full_object_path); |
| 1417 | | } |
| 1418 | | } |
| 1419 | | |
| 1420 | | try argv.ensureUnusedCapacity(self.base.options.system_libs.count()); |
| 1421 | | for (self.base.options.system_libs.keys()) |key| { |
| 1422 | | const lib_basename = try allocPrint(arena, "{s}.lib", .{key}); |
| 1423 | | if (comp.crt_files.get(lib_basename)) |crt_file| { |
| 1424 | | argv.appendAssumeCapacity(crt_file.full_object_path); |
| 1425 | | continue; |
| 1426 | | } |
| 1427 | | if (try self.findLib(arena, lib_basename)) |full_path| { |
| 1428 | | argv.appendAssumeCapacity(full_path); |
| 1429 | | continue; |
| 1430 | | } |
| 1431 | | if (target.abi.isGnu()) { |
| 1432 | | const fallback_name = try allocPrint(arena, "lib{s}.dll.a", .{key}); |
| 1433 | | if (try self.findLib(arena, fallback_name)) |full_path| { |
| 1434 | | argv.appendAssumeCapacity(full_path); |
| 1435 | | continue; |
| 1436 | | } |
| 1437 | | } |
| 1438 | | log.err("DLL import library for -l{s} not found", .{key}); |
| 1439 | | return error.DllImportLibraryNotFound; |
| 1440 | | } |
| 1441 | | |
| 1442 | | if (self.base.options.verbose_link) { |
| 1443 | | // Skip over our own name so that the LLD linker name is the first argv item. |
| 1444 | | Compilation.dump_argv(argv.items[1..]); |
| 1445 | | } |
| 1446 | | |
| 1447 | | if (std.process.can_spawn) { |
| 1448 | | // If possible, we run LLD as a child process because it does not always |
| 1449 | | // behave properly as a library, unfortunately. |
| 1450 | | // https://github.com/ziglang/zig/issues/3825 |
| 1451 | | var child = std.ChildProcess.init(argv.items, arena); |
| 1452 | | if (comp.clang_passthrough_mode) { |
| 1453 | | child.stdin_behavior = .Inherit; |
| 1454 | | child.stdout_behavior = .Inherit; |
| 1455 | | child.stderr_behavior = .Inherit; |
| 1456 | | |
| 1457 | | const term = child.spawnAndWait() catch |err| { |
| 1458 | | log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) }); |
| 1459 | | return error.UnableToSpawnSelf; |
| 1460 | | }; |
| 1461 | | switch (term) { |
| 1462 | | .Exited => |code| { |
| 1463 | | if (code != 0) { |
| 1464 | | std.process.exit(code); |
| 1465 | | } |
| 1466 | | }, |
| 1467 | | else => std.process.abort(), |
| 1468 | | } |
| 1469 | | } else { |
| 1470 | | child.stdin_behavior = .Ignore; |
| 1471 | | child.stdout_behavior = .Ignore; |
| 1472 | | child.stderr_behavior = .Pipe; |
| 1473 | | |
| 1474 | | try child.spawn(); |
| 1475 | | |
| 1476 | | const stderr = try child.stderr.?.reader().readAllAlloc(arena, 10 * 1024 * 1024); |
| 1477 | | |
| 1478 | | const term = child.wait() catch |err| { |
| 1479 | | log.err("unable to spawn {s}: {s}", .{ argv.items[0], @errorName(err) }); |
| 1480 | | return error.UnableToSpawnSelf; |
| 1481 | | }; |
| 1482 | | |
| 1483 | | switch (term) { |
| 1484 | | .Exited => |code| { |
| 1485 | | if (code != 0) { |
| 1486 | | // TODO parse this output and surface with the Compilation API rather than |
| 1487 | | // directly outputting to stderr here. |
| 1488 | | std.debug.print("{s}", .{stderr}); |
| 1489 | | return error.LLDReportedFailure; |
| 1490 | | } |
| 1491 | | }, |
| 1492 | | else => { |
| 1493 | | log.err("{s} terminated with stderr:\n{s}", .{ argv.items[0], stderr }); |
| 1494 | | return error.LLDCrashed; |
| 1495 | | }, |
| 1496 | | } |
| 1497 | | |
| 1498 | | if (stderr.len != 0) { |
| 1499 | | log.warn("unexpected LLD stderr:\n{s}", .{stderr}); |
| 1500 | | } |
| 1501 | | } |
| 1502 | | } else { |
| 1503 | | const exit_code = try lldMain(arena, argv.items, false); |
| 1504 | | if (exit_code != 0) { |
| 1505 | | if (comp.clang_passthrough_mode) { |
| 1506 | | std.process.exit(exit_code); |
| 1507 | | } else { |
| 1508 | | return error.LLDReportedFailure; |
| 1509 | | } |
| 1510 | | } |
| 1511 | | } |
| 1512 | | } |
| 1513 | | |
| 1514 | | if (!self.base.options.disable_lld_caching) { |
| 1515 | | // Update the file with the digest. If it fails we can continue; it only |
| 1516 | | // means that the next invocation will have an unnecessary cache miss. |
| 1517 | | Cache.writeSmallFile(directory.handle, id_symlink_basename, &digest) catch |err| { |
| 1518 | | log.warn("failed to save linking hash digest file: {s}", .{@errorName(err)}); |
| 1519 | | }; |
| 1520 | | // Again failure here only means an unnecessary cache miss. |
| 1521 | | man.writeManifest() catch |err| { |
| 1522 | | log.warn("failed to write cache manifest when linking: {s}", .{@errorName(err)}); |
| 1523 | | }; |
| 1524 | | // We hang on to this lock so that the output file path can be used without |
| 1525 | | // other processes clobbering it. |
| 1526 | | self.base.lock = man.toOwnedLock(); |
| 1527 | | } |
| 1528 | | } |
| 1529 | | |
| 1530 | | fn findLib(self: *Coff, arena: Allocator, name: []const u8) !?[]const u8 { |
| 1531 | | for (self.base.options.lib_dirs) |lib_dir| { |
| 1532 | | const full_path = try fs.path.join(arena, &.{ lib_dir, name }); |
| 1533 | | fs.cwd().access(full_path, .{}) catch |err| switch (err) { |
| 1534 | | error.FileNotFound => continue, |
| 1535 | | else => |e| return e, |
| 1536 | | }; |
| 1537 | | return full_path; |
| 1538 | | } |
| 1539 | | return null; |
| 1540 | | } |
| 1541 | | |
| 1542 | 669 | pub fn getDeclVAddr( |
| 1543 | 670 | self: *Coff, |
| 1544 | 671 | decl_index: Module.Decl.Index, |
| 1545 | 672 | reloc_info: link.File.RelocInfo, |
| 1546 | 673 | ) !u64 { |
| 674 | _ = self; |
| 675 | _ = decl_index; |
| 1547 | 676 | _ = reloc_info; |
| 1548 | | const mod = self.base.options.module.?; |
| 1549 | | const decl = mod.declPtr(decl_index); |
| 1550 | | assert(self.llvm_object == null); |
| 1551 | | return self.text_section_virtual_address + decl.link.coff.text_offset; |
| 677 | @panic("TODO getDeclVAddr"); |
| 1552 | 678 | } |
| 1553 | 679 | |
| 1554 | 680 | pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl: *Module.Decl) !void { |
| 1555 | 681 | _ = self; |
| 1556 | 682 | _ = module; |
| 1557 | 683 | _ = decl; |
| 1558 | | // TODO Implement this |
| 684 | log.debug("TODO implement updateDeclLineNumber", .{}); |
| 1559 | 685 | } |
| 1560 | 686 | |
| 1561 | | pub fn deinit(self: *Coff) void { |
| 1562 | | if (build_options.have_llvm) { |
| 1563 | | if (self.llvm_object) |llvm_object| llvm_object.destroy(self.base.allocator); |
| 1564 | | } |
| 687 | pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { |
| 688 | // TODO https://github.com/ziglang/zig/issues/1284 |
| 689 | return math.add(@TypeOf(actual_size), actual_size, actual_size / ideal_factor) catch |
| 690 | math.maxInt(@TypeOf(actual_size)); |
| 691 | } |
| 692 | |
| 693 | /// Returns pointer-to-symbol described by `sym_with_loc` descriptor. |
| 694 | pub fn getSymbolPtr(self: *Coff, sym_loc: SymbolWithLoc) *coff.Symbol { |
| 695 | assert(sym_loc.file == null); // TODO linking object files |
| 696 | return &self.locals.items[sym_loc.sym_index]; |
| 697 | } |
| 698 | |
| 699 | /// Returns symbol described by `sym_with_loc` descriptor. |
| 700 | pub fn getSymbol(self: *Coff, sym_loc: SymbolWithLoc) coff.Symbol { |
| 701 | return self.getSymbolPtr(sym_loc).*; |
| 702 | } |
| 703 | |
| 704 | /// Returns name of the symbol described by `sym_with_loc` descriptor. |
| 705 | pub fn getSymbolName(self: *Coff, sym_loc: SymbolWithLoc) []const u8 { |
| 706 | assert(sym_loc.file == null); // TODO linking object files |
| 707 | const sym = self.locals.items[sym_loc.sym_index]; |
| 708 | const offset = sym.getNameOffset() orelse return sym.getName().?; |
| 709 | return self.strtab.get(offset).?; |
| 710 | } |
| 711 | |
| 712 | /// Returns atom if there is an atom referenced by the symbol described by `sym_with_loc` descriptor. |
| 713 | /// Returns null on failure. |
| 714 | pub fn getAtomForSymbol(self: *Coff, sym_loc: SymbolWithLoc) ?*Atom { |
| 715 | assert(sym_loc.file == null); // TODO linking with object files |
| 716 | return self.atom_by_index_table.get(sym_loc.sym_index); |
| 717 | } |
| 1565 | 718 | |
| 1566 | | self.text_block_free_list.deinit(self.base.allocator); |
| 1567 | | self.offset_table.deinit(self.base.allocator); |
| 1568 | | self.offset_table_free_list.deinit(self.base.allocator); |
| 719 | /// Returns GOT atom that references `sym_with_loc` if one exists. |
| 720 | /// Returns null otherwise. |
| 721 | pub fn getGotAtomForSymbol(self: *Coff, sym_loc: SymbolWithLoc) ?*Atom { |
| 722 | const got_index = self.got_entries.get(sym_loc) orelse return null; |
| 723 | return self.atom_by_index_table.get(got_index); |
| 1569 | 724 | } |