| ... | @@ -9,7 +9,9 @@ strings: std.HashMapUnmanaged( | ... | @@ -9,7 +9,9 @@ strings: std.HashMapUnmanaged( |
| 9 | std.hash_map.default_max_load_percentage, | 9 | std.hash_map.default_max_load_percentage, |
| 10 | ), | 10 | ), |
| 11 | string_bytes: std.ArrayList(u8), | 11 | string_bytes: std.ArrayList(u8), |
| 12 | section_table: std.ArrayList(Symbol.Index), | 12 | image_section_table: std.ArrayList(Symbol.Index), |
| | 13 | pseudo_section_table: std.AutoArrayHashMapUnmanaged(String, Symbol.Index), |
| | 14 | object_section_table: std.AutoArrayHashMapUnmanaged(String, Symbol.Index), |
| 13 | symbol_table: std.ArrayList(Symbol), | 15 | symbol_table: std.ArrayList(Symbol), |
| 14 | globals: std.AutoArrayHashMapUnmanaged(GlobalName, Symbol.Index), | 16 | globals: std.AutoArrayHashMapUnmanaged(GlobalName, Symbol.Index), |
| 15 | global_pending_index: u32, | 17 | global_pending_index: u32, |
| ... | @@ -24,8 +26,6 @@ pending_uavs: std.AutoArrayHashMapUnmanaged(Node.UavMapIndex, struct { | ... | @@ -24,8 +26,6 @@ pending_uavs: std.AutoArrayHashMapUnmanaged(Node.UavMapIndex, struct { |
| 24 | src_loc: Zcu.LazySrcLoc, | 26 | src_loc: Zcu.LazySrcLoc, |
| 25 | }), | 27 | }), |
| 26 | relocs: std.ArrayList(Reloc), | 28 | relocs: std.ArrayList(Reloc), |
| 27 | /// This is hiding actual bugs with global symbols! Reconsider once they are implemented correctly. | | |
| 28 | entry_hack: Symbol.Index, | | |
| 29 | | 29 | |
| 30 | pub const default_file_alignment: u16 = 0x200; | 30 | pub const default_file_alignment: u16 = 0x200; |
| 31 | pub const default_size_of_stack_reserve: u32 = 0x1000000; | 31 | pub const default_size_of_stack_reserve: u32 = 0x1000000; |
| ... | @@ -102,17 +102,45 @@ pub const Node = union(enum) { | ... | @@ -102,17 +102,45 @@ pub const Node = union(enum) { |
| 102 | optional_header, | 102 | optional_header, |
| 103 | data_directories, | 103 | data_directories, |
| 104 | section_table, | 104 | section_table, |
| 105 | section: Symbol.Index, | 105 | image_section: Symbol.Index, |
| | 106 | |
| 106 | import_directory_table, | 107 | import_directory_table, |
| 107 | import_lookup_table: ImportTable.Index, | 108 | import_lookup_table: ImportTable.Index, |
| 108 | import_address_table: ImportTable.Index, | 109 | import_address_table: ImportTable.Index, |
| 109 | import_hint_name_table: ImportTable.Index, | 110 | import_hint_name_table: ImportTable.Index, |
| | 111 | |
| | 112 | pseudo_section: PseudoSectionMapIndex, |
| | 113 | object_section: ObjectSectionMapIndex, |
| 110 | global: GlobalMapIndex, | 114 | global: GlobalMapIndex, |
| 111 | nav: NavMapIndex, | 115 | nav: NavMapIndex, |
| 112 | uav: UavMapIndex, | 116 | uav: UavMapIndex, |
| 113 | lazy_code: LazyMapRef.Index(.code), | 117 | lazy_code: LazyMapRef.Index(.code), |
| 114 | lazy_const_data: LazyMapRef.Index(.const_data), | 118 | lazy_const_data: LazyMapRef.Index(.const_data), |
| 115 | | 119 | |
| | 120 | pub const PseudoSectionMapIndex = enum(u32) { |
| | 121 | _, |
| | 122 | |
| | 123 | pub fn name(psmi: PseudoSectionMapIndex, coff: *const Coff) String { |
| | 124 | return coff.pseudo_section_table.keys()[@intFromEnum(psmi)]; |
| | 125 | } |
| | 126 | |
| | 127 | pub fn symbol(psmi: PseudoSectionMapIndex, coff: *const Coff) Symbol.Index { |
| | 128 | return coff.pseudo_section_table.values()[@intFromEnum(psmi)]; |
| | 129 | } |
| | 130 | }; |
| | 131 | |
| | 132 | pub const ObjectSectionMapIndex = enum(u32) { |
| | 133 | _, |
| | 134 | |
| | 135 | pub fn name(osmi: ObjectSectionMapIndex, coff: *const Coff) String { |
| | 136 | return coff.object_section_table.keys()[@intFromEnum(osmi)]; |
| | 137 | } |
| | 138 | |
| | 139 | pub fn symbol(osmi: ObjectSectionMapIndex, coff: *const Coff) Symbol.Index { |
| | 140 | return coff.object_section_table.values()[@intFromEnum(osmi)]; |
| | 141 | } |
| | 142 | }; |
| | 143 | |
| 116 | pub const GlobalMapIndex = enum(u32) { | 144 | pub const GlobalMapIndex = enum(u32) { |
| 117 | _, | 145 | _, |
| 118 | | 146 | |
| ... | @@ -204,27 +232,6 @@ pub const Node = union(enum) { | ... | @@ -204,27 +232,6 @@ pub const Node = union(enum) { |
| 204 | } | 232 | } |
| 205 | }; | 233 | }; |
| 206 | | 234 | |
| 207 | pub const DataDirectory = enum { | | |
| 208 | export_table, | | |
| 209 | import_table, | | |
| 210 | resorce_table, | | |
| 211 | exception_table, | | |
| 212 | certificate_table, | | |
| 213 | base_relocation_table, | | |
| 214 | debug, | | |
| 215 | architecture, | | |
| 216 | global_ptr, | | |
| 217 | tls_table, | | |
| 218 | load_config_table, | | |
| 219 | bound_import, | | |
| 220 | import_address_table, | | |
| 221 | delay_import_descriptor, | | |
| 222 | clr_runtime_header, | | |
| 223 | reserved, | | |
| 224 | | | |
| 225 | pub const len = @typeInfo(DataDirectory).@"enum".fields.len; | | |
| 226 | }; | | |
| 227 | | | |
| 228 | pub const ImportTable = struct { | 235 | pub const ImportTable = struct { |
| 229 | ni: MappedFile.Node.Index, | 236 | ni: MappedFile.Node.Index, |
| 230 | entries: std.AutoArrayHashMapUnmanaged(void, Entry), | 237 | entries: std.AutoArrayHashMapUnmanaged(void, Entry), |
| ... | @@ -264,9 +271,18 @@ pub const ImportTable = struct { | ... | @@ -264,9 +271,18 @@ pub const ImportTable = struct { |
| 264 | }; | 271 | }; |
| 265 | | 272 | |
| 266 | pub const String = enum(u32) { | 273 | pub const String = enum(u32) { |
| | 274 | @".data" = 0, |
| | 275 | @".idata" = 6, |
| | 276 | @".rdata" = 13, |
| | 277 | @".text" = 20, |
| | 278 | @".tls$" = 26, |
| 267 | _, | 279 | _, |
| 268 | | 280 | |
| 269 | pub const Optional = enum(u32) { | 281 | pub const Optional = enum(u32) { |
| | 282 | @".data" = @intFromEnum(String.@".data"), |
| | 283 | @".rdata" = @intFromEnum(String.@".rdata"), |
| | 284 | @".text" = @intFromEnum(String.@".text"), |
| | 285 | @".tls$" = @intFromEnum(String.@".tls$"), |
| 270 | none = std.math.maxInt(u32), | 286 | none = std.math.maxInt(u32), |
| 271 | _, | 287 | _, |
| 272 | | 288 | |
| ... | @@ -318,7 +334,7 @@ pub const Symbol = struct { | ... | @@ -318,7 +334,7 @@ pub const Symbol = struct { |
| 318 | } | 334 | } |
| 319 | | 335 | |
| 320 | pub fn symbol(sn: SectionNumber, coff: *const Coff) Symbol.Index { | 336 | pub fn symbol(sn: SectionNumber, coff: *const Coff) Symbol.Index { |
| 321 | return coff.section_table.items[sn.toIndex()]; | 337 | return coff.image_section_table.items[sn.toIndex()]; |
| 322 | } | 338 | } |
| 323 | | 339 | |
| 324 | pub fn header(sn: SectionNumber, coff: *Coff) *std.coff.SectionHeader { | 340 | pub fn header(sn: SectionNumber, coff: *Coff) *std.coff.SectionHeader { |
| ... | @@ -329,7 +345,6 @@ pub const Symbol = struct { | ... | @@ -329,7 +345,6 @@ pub const Symbol = struct { |
| 329 | pub const Index = enum(u32) { | 345 | pub const Index = enum(u32) { |
| 330 | null, | 346 | null, |
| 331 | data, | 347 | data, |
| 332 | idata, | | |
| 333 | rdata, | 348 | rdata, |
| 334 | text, | 349 | text, |
| 335 | _, | 350 | _, |
| ... | @@ -349,10 +364,6 @@ pub const Symbol = struct { | ... | @@ -349,10 +364,6 @@ pub const Symbol = struct { |
| 349 | pub fn flushMoved(si: Symbol.Index, coff: *Coff) void { | 364 | pub fn flushMoved(si: Symbol.Index, coff: *Coff) void { |
| 350 | const sym = si.get(coff); | 365 | const sym = si.get(coff); |
| 351 | sym.rva = coff.computeNodeRva(sym.ni); | 366 | sym.rva = coff.computeNodeRva(sym.ni); |
| 352 | if (si == coff.entry_hack) { | | |
| 353 | @branchHint(.unlikely); | | |
| 354 | coff.targetStore(&coff.optionalHeaderStandardPtr().address_of_entry_point, sym.rva); | | |
| 355 | } | | |
| 356 | si.applyLocationRelocs(coff); | 367 | si.applyLocationRelocs(coff); |
| 357 | si.applyTargetRelocs(coff); | 368 | si.applyTargetRelocs(coff); |
| 358 | } | 369 | } |
| ... | @@ -493,6 +504,12 @@ pub const Reloc = extern struct { | ... | @@ -493,6 +504,12 @@ pub const Reloc = extern struct { |
| 493 | @intCast(@as(i64, @bitCast(target_rva -% (loc_sym.rva + reloc.offset + 9)))), | 504 | @intCast(@as(i64, @bitCast(target_rva -% (loc_sym.rva + reloc.offset + 9)))), |
| 494 | target_endian, | 505 | target_endian, |
| 495 | ), | 506 | ), |
| | 507 | .SECREL => std.mem.writeInt( |
| | 508 | u32, |
| | 509 | loc_slice[0..4], |
| | 510 | coff.computeNodeSectionOffset(target_sym.ni), |
| | 511 | target_endian, |
| | 512 | ), |
| 496 | }, | 513 | }, |
| 497 | .I386 => switch (reloc.type.I386) { | 514 | .I386 => switch (reloc.type.I386) { |
| 498 | else => |kind| @panic(@tagName(kind)), | 515 | else => |kind| @panic(@tagName(kind)), |
| ... | @@ -527,6 +544,12 @@ pub const Reloc = extern struct { | ... | @@ -527,6 +544,12 @@ pub const Reloc = extern struct { |
| 527 | @intCast(@as(i64, @bitCast(target_rva -% (loc_sym.rva + reloc.offset + 4)))), | 544 | @intCast(@as(i64, @bitCast(target_rva -% (loc_sym.rva + reloc.offset + 4)))), |
| 528 | target_endian, | 545 | target_endian, |
| 529 | ), | 546 | ), |
| | 547 | .SECREL => std.mem.writeInt( |
| | 548 | u32, |
| | 549 | loc_slice[0..4], |
| | 550 | coff.computeNodeSectionOffset(target_sym.ni), |
| | 551 | target_endian, |
| | 552 | ), |
| 530 | }, | 553 | }, |
| 531 | } | 554 | } |
| 532 | } | 555 | } |
| ... | @@ -634,7 +657,9 @@ fn create( | ... | @@ -634,7 +657,9 @@ fn create( |
| 634 | }, | 657 | }, |
| 635 | .strings = .empty, | 658 | .strings = .empty, |
| 636 | .string_bytes = .empty, | 659 | .string_bytes = .empty, |
| 637 | .section_table = .empty, | 660 | .image_section_table = .empty, |
| | 661 | .pseudo_section_table = .empty, |
| | 662 | .object_section_table = .empty, |
| 638 | .symbol_table = .empty, | 663 | .symbol_table = .empty, |
| 639 | .globals = .empty, | 664 | .globals = .empty, |
| 640 | .global_pending_index = 0, | 665 | .global_pending_index = 0, |
| ... | @@ -646,10 +671,17 @@ fn create( | ... | @@ -646,10 +671,17 @@ fn create( |
| 646 | }), | 671 | }), |
| 647 | .pending_uavs = .empty, | 672 | .pending_uavs = .empty, |
| 648 | .relocs = .empty, | 673 | .relocs = .empty, |
| 649 | .entry_hack = .null, | | |
| 650 | }; | 674 | }; |
| 651 | errdefer coff.deinit(); | 675 | errdefer coff.deinit(); |
| 652 | | 676 | |
| | 677 | { |
| | 678 | const strings = std.enums.values(String); |
| | 679 | try coff.strings.ensureTotalCapacityContext(comp.gpa, @intCast(strings.len), .{ |
| | 680 | .bytes = &coff.string_bytes, |
| | 681 | }); |
| | 682 | for (strings) |string| assert(try coff.getOrPutString(@tagName(string)) == string); |
| | 683 | } |
| | 684 | |
| 653 | try coff.initHeaders( | 685 | try coff.initHeaders( |
| 654 | is_image, | 686 | is_image, |
| 655 | machine, | 687 | machine, |
| ... | @@ -669,7 +701,9 @@ pub fn deinit(coff: *Coff) void { | ... | @@ -669,7 +701,9 @@ pub fn deinit(coff: *Coff) void { |
| 669 | coff.import_table.entries.deinit(gpa); | 701 | coff.import_table.entries.deinit(gpa); |
| 670 | coff.strings.deinit(gpa); | 702 | coff.strings.deinit(gpa); |
| 671 | coff.string_bytes.deinit(gpa); | 703 | coff.string_bytes.deinit(gpa); |
| 672 | coff.section_table.deinit(gpa); | 704 | coff.image_section_table.deinit(gpa); |
| | 705 | coff.pseudo_section_table.deinit(gpa); |
| | 706 | coff.object_section_table.deinit(gpa); |
| 673 | coff.symbol_table.deinit(gpa); | 707 | coff.symbol_table.deinit(gpa); |
| 674 | coff.globals.deinit(gpa); | 708 | coff.globals.deinit(gpa); |
| 675 | coff.navs.deinit(gpa); | 709 | coff.navs.deinit(gpa); |
| ... | @@ -692,19 +726,21 @@ fn initHeaders( | ... | @@ -692,19 +726,21 @@ fn initHeaders( |
| 692 | ) !void { | 726 | ) !void { |
| 693 | const comp = coff.base.comp; | 727 | const comp = coff.base.comp; |
| 694 | const gpa = comp.gpa; | 728 | const gpa = comp.gpa; |
| 695 | const file_align: std.mem.Alignment = comptime .fromByteUnits(default_file_alignment); | | |
| 696 | const target_endian = coff.targetEndian(); | 729 | const target_endian = coff.targetEndian(); |
| | 730 | const file_align: std.mem.Alignment = comptime .fromByteUnits(default_file_alignment); |
| 697 | | 731 | |
| 698 | const optional_header_size: u16 = if (is_image) switch (magic) { | 732 | const optional_header_size: u16 = if (is_image) switch (magic) { |
| 699 | _ => unreachable, | 733 | _ => unreachable, |
| 700 | inline else => |ct_magic| @sizeOf(@field(std.coff.OptionalHeader, @tagName(ct_magic))), | 734 | inline else => |ct_magic| @sizeOf(@field(std.coff.OptionalHeader, @tagName(ct_magic))), |
| 701 | } else 0; | 735 | } else 0; |
| 702 | const data_directories_size: u16 = if (is_image) | 736 | const data_directories_size: u16 = if (is_image) |
| 703 | @sizeOf(std.coff.ImageDataDirectory) * DataDirectory.len | 737 | @sizeOf(std.coff.ImageDataDirectory) * std.coff.IMAGE.DIRECTORY_ENTRY.len |
| 704 | else | 738 | else |
| 705 | 0; | 739 | 0; |
| 706 | | 740 | |
| 707 | try coff.nodes.ensureTotalCapacity(gpa, Node.known_count); | 741 | const expected_nodes_len = Node.known_count + 6 + |
| | 742 | @as(usize, @intFromBool(comp.config.any_non_single_threaded)) * 2; |
| | 743 | try coff.nodes.ensureTotalCapacity(gpa, expected_nodes_len); |
| 708 | coff.nodes.appendAssumeCapacity(.file); | 744 | coff.nodes.appendAssumeCapacity(.file); |
| 709 | | 745 | |
| 710 | const header_ni = Node.known.header; | 746 | const header_ni = Node.known.header; |
| ... | @@ -762,108 +798,110 @@ fn initHeaders( | ... | @@ -762,108 +798,110 @@ fn initHeaders( |
| 762 | .fixed = true, | 798 | .fixed = true, |
| 763 | })); | 799 | })); |
| 764 | coff.nodes.appendAssumeCapacity(.optional_header); | 800 | coff.nodes.appendAssumeCapacity(.optional_header); |
| 765 | coff.targetStore(&coff.optionalHeaderStandardPtr().magic, magic); | 801 | if (is_image) { |
| 766 | if (is_image) switch (coff.optionalHeaderPtr()) { | 802 | coff.targetStore(&coff.optionalHeaderStandardPtr().magic, magic); |
| 767 | .PE32 => |optional_header| { | 803 | switch (coff.optionalHeaderPtr()) { |
| 768 | optional_header.* = .{ | 804 | .PE32 => |optional_header| { |
| 769 | .standard = .{ | 805 | optional_header.* = .{ |
| 770 | .magic = .PE32, | 806 | .standard = .{ |
| 771 | .major_linker_version = 0, | 807 | .magic = .PE32, |
| 772 | .minor_linker_version = 0, | 808 | .major_linker_version = 0, |
| 773 | .size_of_code = 0, | 809 | .minor_linker_version = 0, |
| 774 | .size_of_initialized_data = 0, | 810 | .size_of_code = 0, |
| 775 | .size_of_uninitialized_data = 0, | 811 | .size_of_initialized_data = 0, |
| 776 | .address_of_entry_point = 0, | 812 | .size_of_uninitialized_data = 0, |
| 777 | .base_of_code = 0, | 813 | .address_of_entry_point = 0, |
| 778 | }, | 814 | .base_of_code = 0, |
| 779 | .base_of_data = 0, | | |
| 780 | .image_base = switch (coff.base.comp.config.output_mode) { | | |
| 781 | .Exe => 0x400000, | | |
| 782 | .Lib => switch (coff.base.comp.config.link_mode) { | | |
| 783 | .static => 0, | | |
| 784 | .dynamic => 0x10000000, | | |
| 785 | }, | 815 | }, |
| 786 | .Obj => 0, | 816 | .base_of_data = 0, |
| 787 | }, | 817 | .image_base = switch (coff.base.comp.config.output_mode) { |
| 788 | .section_alignment = @intCast(section_align.toByteUnits()), | 818 | .Exe => 0x400000, |
| 789 | .file_alignment = @intCast(file_align.toByteUnits()), | 819 | .Lib => switch (coff.base.comp.config.link_mode) { |
| 790 | .major_operating_system_version = 6, | 820 | .static => 0, |
| 791 | .minor_operating_system_version = 0, | 821 | .dynamic => 0x10000000, |
| 792 | .major_image_version = 0, | 822 | }, |
| 793 | .minor_image_version = 0, | 823 | .Obj => 0, |
| 794 | .major_subsystem_version = major_subsystem_version, | | |
| 795 | .minor_subsystem_version = minor_subsystem_version, | | |
| 796 | .win32_version_value = 0, | | |
| 797 | .size_of_image = 0, | | |
| 798 | .size_of_headers = 0, | | |
| 799 | .checksum = 0, | | |
| 800 | .subsystem = .WINDOWS_CUI, | | |
| 801 | .dll_flags = .{ | | |
| 802 | .HIGH_ENTROPY_VA = true, | | |
| 803 | .DYNAMIC_BASE = true, | | |
| 804 | .TERMINAL_SERVER_AWARE = true, | | |
| 805 | .NX_COMPAT = true, | | |
| 806 | }, | | |
| 807 | .size_of_stack_reserve = default_size_of_stack_reserve, | | |
| 808 | .size_of_stack_commit = default_size_of_stack_commit, | | |
| 809 | .size_of_heap_reserve = default_size_of_heap_reserve, | | |
| 810 | .size_of_heap_commit = default_size_of_heap_commit, | | |
| 811 | .loader_flags = 0, | | |
| 812 | .number_of_rva_and_sizes = DataDirectory.len, | | |
| 813 | }; | | |
| 814 | if (target_endian != native_endian) | | |
| 815 | std.mem.byteSwapAllFields(std.coff.OptionalHeader.PE32, optional_header); | | |
| 816 | }, | | |
| 817 | .@"PE32+" => |optional_header| { | | |
| 818 | optional_header.* = .{ | | |
| 819 | .standard = .{ | | |
| 820 | .magic = .@"PE32+", | | |
| 821 | .major_linker_version = 0, | | |
| 822 | .minor_linker_version = 0, | | |
| 823 | .size_of_code = 0, | | |
| 824 | .size_of_initialized_data = 0, | | |
| 825 | .size_of_uninitialized_data = 0, | | |
| 826 | .address_of_entry_point = 0, | | |
| 827 | .base_of_code = 0, | | |
| 828 | }, | | |
| 829 | .image_base = switch (coff.base.comp.config.output_mode) { | | |
| 830 | .Exe => 0x140000000, | | |
| 831 | .Lib => switch (coff.base.comp.config.link_mode) { | | |
| 832 | .static => 0, | | |
| 833 | .dynamic => 0x180000000, | | |
| 834 | }, | 824 | }, |
| 835 | .Obj => 0, | 825 | .section_alignment = @intCast(section_align.toByteUnits()), |
| 836 | }, | 826 | .file_alignment = @intCast(file_align.toByteUnits()), |
| 837 | .section_alignment = @intCast(section_align.toByteUnits()), | 827 | .major_operating_system_version = 6, |
| 838 | .file_alignment = @intCast(file_align.toByteUnits()), | 828 | .minor_operating_system_version = 0, |
| 839 | .major_operating_system_version = 6, | 829 | .major_image_version = 0, |
| 840 | .minor_operating_system_version = 0, | 830 | .minor_image_version = 0, |
| 841 | .major_image_version = 0, | 831 | .major_subsystem_version = major_subsystem_version, |
| 842 | .minor_image_version = 0, | 832 | .minor_subsystem_version = minor_subsystem_version, |
| 843 | .major_subsystem_version = major_subsystem_version, | 833 | .win32_version_value = 0, |
| 844 | .minor_subsystem_version = minor_subsystem_version, | 834 | .size_of_image = 0, |
| 845 | .win32_version_value = 0, | 835 | .size_of_headers = 0, |
| 846 | .size_of_image = 0, | 836 | .checksum = 0, |
| 847 | .size_of_headers = 0, | 837 | .subsystem = .WINDOWS_CUI, |
| 848 | .checksum = 0, | 838 | .dll_flags = .{ |
| 849 | .subsystem = .WINDOWS_CUI, | 839 | .HIGH_ENTROPY_VA = true, |
| 850 | .dll_flags = .{ | 840 | .DYNAMIC_BASE = true, |
| 851 | .HIGH_ENTROPY_VA = true, | 841 | .TERMINAL_SERVER_AWARE = true, |
| 852 | .DYNAMIC_BASE = true, | 842 | .NX_COMPAT = true, |
| 853 | .TERMINAL_SERVER_AWARE = true, | 843 | }, |
| 854 | .NX_COMPAT = true, | 844 | .size_of_stack_reserve = default_size_of_stack_reserve, |
| 855 | }, | 845 | .size_of_stack_commit = default_size_of_stack_commit, |
| 856 | .size_of_stack_reserve = default_size_of_stack_reserve, | 846 | .size_of_heap_reserve = default_size_of_heap_reserve, |
| 857 | .size_of_stack_commit = default_size_of_stack_commit, | 847 | .size_of_heap_commit = default_size_of_heap_commit, |
| 858 | .size_of_heap_reserve = default_size_of_heap_reserve, | 848 | .loader_flags = 0, |
| 859 | .size_of_heap_commit = default_size_of_heap_commit, | 849 | .number_of_rva_and_sizes = std.coff.IMAGE.DIRECTORY_ENTRY.len, |
| 860 | .loader_flags = 0, | 850 | }; |
| 861 | .number_of_rva_and_sizes = DataDirectory.len, | 851 | if (target_endian != native_endian) |
| 862 | }; | 852 | std.mem.byteSwapAllFields(std.coff.OptionalHeader.PE32, optional_header); |
| 863 | if (target_endian != native_endian) | 853 | }, |
| 864 | std.mem.byteSwapAllFields(std.coff.OptionalHeader.@"PE32+", optional_header); | 854 | .@"PE32+" => |optional_header| { |
| 865 | }, | 855 | optional_header.* = .{ |
| 866 | }; | 856 | .standard = .{ |
| | 857 | .magic = .@"PE32+", |
| | 858 | .major_linker_version = 0, |
| | 859 | .minor_linker_version = 0, |
| | 860 | .size_of_code = 0, |
| | 861 | .size_of_initialized_data = 0, |
| | 862 | .size_of_uninitialized_data = 0, |
| | 863 | .address_of_entry_point = 0, |
| | 864 | .base_of_code = 0, |
| | 865 | }, |
| | 866 | .image_base = switch (coff.base.comp.config.output_mode) { |
| | 867 | .Exe => 0x140000000, |
| | 868 | .Lib => switch (coff.base.comp.config.link_mode) { |
| | 869 | .static => 0, |
| | 870 | .dynamic => 0x180000000, |
| | 871 | }, |
| | 872 | .Obj => 0, |
| | 873 | }, |
| | 874 | .section_alignment = @intCast(section_align.toByteUnits()), |
| | 875 | .file_alignment = @intCast(file_align.toByteUnits()), |
| | 876 | .major_operating_system_version = 6, |
| | 877 | .minor_operating_system_version = 0, |
| | 878 | .major_image_version = 0, |
| | 879 | .minor_image_version = 0, |
| | 880 | .major_subsystem_version = major_subsystem_version, |
| | 881 | .minor_subsystem_version = minor_subsystem_version, |
| | 882 | .win32_version_value = 0, |
| | 883 | .size_of_image = 0, |
| | 884 | .size_of_headers = 0, |
| | 885 | .checksum = 0, |
| | 886 | .subsystem = .WINDOWS_CUI, |
| | 887 | .dll_flags = .{ |
| | 888 | .HIGH_ENTROPY_VA = true, |
| | 889 | .DYNAMIC_BASE = true, |
| | 890 | .TERMINAL_SERVER_AWARE = true, |
| | 891 | .NX_COMPAT = true, |
| | 892 | }, |
| | 893 | .size_of_stack_reserve = default_size_of_stack_reserve, |
| | 894 | .size_of_stack_commit = default_size_of_stack_commit, |
| | 895 | .size_of_heap_reserve = default_size_of_heap_reserve, |
| | 896 | .size_of_heap_commit = default_size_of_heap_commit, |
| | 897 | .loader_flags = 0, |
| | 898 | .number_of_rva_and_sizes = std.coff.IMAGE.DIRECTORY_ENTRY.len, |
| | 899 | }; |
| | 900 | if (target_endian != native_endian) |
| | 901 | std.mem.byteSwapAllFields(std.coff.OptionalHeader.@"PE32+", optional_header); |
| | 902 | }, |
| | 903 | } |
| | 904 | } |
| 867 | | 905 | |
| 868 | const data_directories_ni = Node.known.data_directories; | 906 | const data_directories_ni = Node.known.data_directories; |
| 869 | assert(data_directories_ni == try coff.mf.addLastChildNode(gpa, header_ni, .{ | 907 | assert(data_directories_ni == try coff.mf.addLastChildNode(gpa, header_ni, .{ |
| ... | @@ -875,8 +913,10 @@ fn initHeaders( | ... | @@ -875,8 +913,10 @@ fn initHeaders( |
| 875 | { | 913 | { |
| 876 | const data_directories = coff.dataDirectorySlice(); | 914 | const data_directories = coff.dataDirectorySlice(); |
| 877 | @memset(data_directories, .{ .virtual_address = 0, .size = 0 }); | 915 | @memset(data_directories, .{ .virtual_address = 0, .size = 0 }); |
| 878 | if (target_endian != native_endian) | 916 | if (target_endian != native_endian) std.mem.byteSwapAllFields( |
| 879 | std.mem.byteSwapAllFields([DataDirectory.len]std.coff.ImageDataDirectory, data_directories); | 917 | [std.coff.IMAGE.DIRECTORY_ENTRY.len]std.coff.ImageDataDirectory, |
| | 918 | data_directories, |
| | 919 | ); |
| 880 | } | 920 | } |
| 881 | | 921 | |
| 882 | const section_table_ni = Node.known.section_table; | 922 | const section_table_ni = Node.known.section_table; |
| ... | @@ -902,10 +942,6 @@ fn initHeaders( | ... | @@ -902,10 +942,6 @@ fn initHeaders( |
| 902 | .MEM_READ = true, | 942 | .MEM_READ = true, |
| 903 | .MEM_WRITE = true, | 943 | .MEM_WRITE = true, |
| 904 | }) == .data); | 944 | }) == .data); |
| 905 | assert(try coff.addSection(".idata", .{ | | |
| 906 | .CNT_INITIALIZED_DATA = true, | | |
| 907 | .MEM_READ = true, | | |
| 908 | }) == .idata); | | |
| 909 | assert(try coff.addSection(".rdata", .{ | 945 | assert(try coff.addSection(".rdata", .{ |
| 910 | .CNT_INITIALIZED_DATA = true, | 946 | .CNT_INITIALIZED_DATA = true, |
| 911 | .MEM_READ = true, | 947 | .MEM_READ = true, |
| ... | @@ -915,13 +951,26 @@ fn initHeaders( | ... | @@ -915,13 +951,26 @@ fn initHeaders( |
| 915 | .MEM_EXECUTE = true, | 951 | .MEM_EXECUTE = true, |
| 916 | .MEM_READ = true, | 952 | .MEM_READ = true, |
| 917 | }) == .text); | 953 | }) == .text); |
| | 954 | |
| 918 | coff.import_table.ni = try coff.mf.addLastChildNode( | 955 | coff.import_table.ni = try coff.mf.addLastChildNode( |
| 919 | gpa, | 956 | gpa, |
| 920 | Symbol.Index.idata.node(coff), | 957 | (try coff.objectSectionMapIndex( |
| 921 | .{ .alignment = .@"4" }, | 958 | .@".idata", |
| | 959 | coff.mf.flags.block_size, |
| | 960 | .{ .read = true }, |
| | 961 | )).symbol(coff).node(coff), |
| | 962 | .{ .alignment = .@"4", .moved = true }, |
| 922 | ); | 963 | ); |
| 923 | coff.nodes.appendAssumeCapacity(.import_directory_table); | 964 | coff.nodes.appendAssumeCapacity(.import_directory_table); |
| 924 | assert(coff.symbol_table.items.len == Symbol.Index.known_count); | 965 | |
| | 966 | // While tls variables allocated at runtime are writable, the template itself is not |
| | 967 | if (comp.config.any_non_single_threaded) _ = try coff.objectSectionMapIndex( |
| | 968 | .@".tls$", |
| | 969 | coff.mf.flags.block_size, |
| | 970 | .{ .read = true }, |
| | 971 | ); |
| | 972 | |
| | 973 | assert(coff.nodes.len == expected_nodes_len); |
| 925 | } | 974 | } |
| 926 | | 975 | |
| 927 | fn getNode(coff: *const Coff, ni: MappedFile.Node.Index) Node { | 976 | fn getNode(coff: *const Coff, ni: MappedFile.Node.Index) Node { |
| ... | @@ -938,8 +987,10 @@ fn computeNodeRva(coff: *Coff, ni: MappedFile.Node.Index) u32 { | ... | @@ -938,8 +987,10 @@ fn computeNodeRva(coff: *Coff, ni: MappedFile.Node.Index) u32 { |
| 938 | .data_directories, | 987 | .data_directories, |
| 939 | .section_table, | 988 | .section_table, |
| 940 | => unreachable, | 989 | => unreachable, |
| 941 | .section => |si| si, | 990 | .image_section => |si| si, |
| 942 | .import_directory_table => unreachable, | 991 | .import_directory_table => break :parent_rva coff.targetLoad( |
| | 992 | &coff.dataDirectoryPtr(.IMPORT).virtual_address, |
| | 993 | ), |
| 943 | .import_lookup_table => |import_index| break :parent_rva coff.targetLoad( | 994 | .import_lookup_table => |import_index| break :parent_rva coff.targetLoad( |
| 944 | &coff.importDirectoryEntryPtr(import_index).import_lookup_table_rva, | 995 | &coff.importDirectoryEntryPtr(import_index).import_lookup_table_rva, |
| 945 | ), | 996 | ), |
| ... | @@ -949,13 +1000,34 @@ fn computeNodeRva(coff: *Coff, ni: MappedFile.Node.Index) u32 { | ... | @@ -949,13 +1000,34 @@ fn computeNodeRva(coff: *Coff, ni: MappedFile.Node.Index) u32 { |
| 949 | .import_hint_name_table => |import_index| break :parent_rva coff.targetLoad( | 1000 | .import_hint_name_table => |import_index| break :parent_rva coff.targetLoad( |
| 950 | &coff.importDirectoryEntryPtr(import_index).name_rva, | 1001 | &coff.importDirectoryEntryPtr(import_index).name_rva, |
| 951 | ), | 1002 | ), |
| 952 | inline .global, .nav, .uav, .lazy_code, .lazy_const_data => |mi| mi.symbol(coff), | 1003 | inline .pseudo_section, |
| | 1004 | .object_section, |
| | 1005 | .global, |
| | 1006 | .nav, |
| | 1007 | .uav, |
| | 1008 | .lazy_code, |
| | 1009 | .lazy_const_data, |
| | 1010 | => |mi| mi.symbol(coff), |
| 953 | }; | 1011 | }; |
| 954 | break :parent_rva parent_si.get(coff).rva; | 1012 | break :parent_rva parent_si.get(coff).rva; |
| 955 | }; | 1013 | }; |
| 956 | const offset, _ = ni.location(&coff.mf).resolve(&coff.mf); | 1014 | const offset, _ = ni.location(&coff.mf).resolve(&coff.mf); |
| 957 | return @intCast(parent_rva + offset); | 1015 | return @intCast(parent_rva + offset); |
| 958 | } | 1016 | } |
| | 1017 | fn computeNodeSectionOffset(coff: *Coff, ni: MappedFile.Node.Index) u32 { |
| | 1018 | var section_offset: u32 = 0; |
| | 1019 | var parent_ni = ni; |
| | 1020 | while (true) { |
| | 1021 | const offset, _ = parent_ni.location(&coff.mf).resolve(&coff.mf); |
| | 1022 | section_offset += @intCast(offset); |
| | 1023 | parent_ni = parent_ni.parent(&coff.mf); |
| | 1024 | switch (coff.getNode(parent_ni)) { |
| | 1025 | else => unreachable, |
| | 1026 | .image_section, .pseudo_section => return section_offset, |
| | 1027 | .object_section => {}, |
| | 1028 | } |
| | 1029 | } |
| | 1030 | } |
| 959 | | 1031 | |
| 960 | pub inline fn targetEndian(_: *const Coff) std.builtin.Endian { | 1032 | pub inline fn targetEndian(_: *const Coff) std.builtin.Endian { |
| 961 | return .little; | 1033 | return .little; |
| ... | @@ -1021,11 +1093,16 @@ pub fn optionalHeaderField( | ... | @@ -1021,11 +1093,16 @@ pub fn optionalHeaderField( |
| 1021 | }; | 1093 | }; |
| 1022 | } | 1094 | } |
| 1023 | | 1095 | |
| 1024 | pub fn dataDirectorySlice(coff: *Coff) *[DataDirectory.len]std.coff.ImageDataDirectory { | 1096 | pub fn dataDirectorySlice( |
| | 1097 | coff: *Coff, |
| | 1098 | ) *[std.coff.IMAGE.DIRECTORY_ENTRY.len]std.coff.ImageDataDirectory { |
| 1025 | return @ptrCast(@alignCast(Node.known.data_directories.slice(&coff.mf))); | 1099 | return @ptrCast(@alignCast(Node.known.data_directories.slice(&coff.mf))); |
| 1026 | } | 1100 | } |
| 1027 | pub fn dataDirectoryPtr(coff: *Coff, data_directory: DataDirectory) *std.coff.ImageDataDirectory { | 1101 | pub fn dataDirectoryPtr( |
| 1028 | return &coff.dataDirectorySlice()[@intFromEnum(data_directory)]; | 1102 | coff: *Coff, |
| | 1103 | entry: std.coff.IMAGE.DIRECTORY_ENTRY, |
| | 1104 | ) *std.coff.ImageDataDirectory { |
| | 1105 | return &coff.dataDirectorySlice()[@intFromEnum(entry)]; |
| 1029 | } | 1106 | } |
| 1030 | | 1107 | |
| 1031 | pub fn sectionTableSlice(coff: *Coff) []std.coff.SectionHeader { | 1108 | pub fn sectionTableSlice(coff: *Coff) []std.coff.SectionHeader { |
| ... | @@ -1060,13 +1137,22 @@ fn initSymbolAssumeCapacity(coff: *Coff) !Symbol.Index { | ... | @@ -1060,13 +1137,22 @@ fn initSymbolAssumeCapacity(coff: *Coff) !Symbol.Index { |
| 1060 | } | 1137 | } |
| 1061 | | 1138 | |
| 1062 | fn getOrPutString(coff: *Coff, string: []const u8) !String { | 1139 | fn getOrPutString(coff: *Coff, string: []const u8) !String { |
| | 1140 | try coff.ensureUnusedStringCapacity(string.len); |
| | 1141 | return coff.getOrPutStringAssumeCapacity(string); |
| | 1142 | } |
| | 1143 | fn getOrPutOptionalString(coff: *Coff, string: ?[]const u8) !String.Optional { |
| | 1144 | return (try coff.getOrPutString(string orelse return .none)).toOptional(); |
| | 1145 | } |
| | 1146 | |
| | 1147 | fn ensureUnusedStringCapacity(coff: *Coff, len: usize) !void { |
| 1063 | const gpa = coff.base.comp.gpa; | 1148 | const gpa = coff.base.comp.gpa; |
| 1064 | try coff.string_bytes.ensureUnusedCapacity(gpa, string.len + 1); | 1149 | try coff.strings.ensureUnusedCapacityContext(gpa, 1, .{ .bytes = &coff.string_bytes }); |
| 1065 | const gop = try coff.strings.getOrPutContextAdapted( | 1150 | try coff.string_bytes.ensureUnusedCapacity(gpa, len + 1); |
| 1066 | gpa, | 1151 | } |
| | 1152 | fn getOrPutStringAssumeCapacity(coff: *Coff, string: []const u8) String { |
| | 1153 | const gop = coff.strings.getOrPutAssumeCapacityAdapted( |
| 1067 | string, | 1154 | string, |
| 1068 | std.hash_map.StringIndexAdapter{ .bytes = &coff.string_bytes }, | 1155 | std.hash_map.StringIndexAdapter{ .bytes = &coff.string_bytes }, |
| 1069 | .{ .bytes = &coff.string_bytes }, | | |
| 1070 | ); | 1156 | ); |
| 1071 | if (!gop.found_existing) { | 1157 | if (!gop.found_existing) { |
| 1072 | gop.key_ptr.* = @intCast(coff.string_bytes.items.len); | 1158 | gop.key_ptr.* = @intCast(coff.string_bytes.items.len); |
| ... | @@ -1077,10 +1163,6 @@ fn getOrPutString(coff: *Coff, string: []const u8) !String { | ... | @@ -1077,10 +1163,6 @@ fn getOrPutString(coff: *Coff, string: []const u8) !String { |
| 1077 | return @enumFromInt(gop.key_ptr.*); | 1163 | return @enumFromInt(gop.key_ptr.*); |
| 1078 | } | 1164 | } |
| 1079 | | 1165 | |
| 1080 | fn getOrPutOptionalString(coff: *Coff, string: ?[]const u8) !String.Optional { | | |
| 1081 | return (try coff.getOrPutString(string orelse return .none)).toOptional(); | | |
| 1082 | } | | |
| 1083 | | | |
| 1084 | pub fn globalSymbol(coff: *Coff, name: []const u8, lib_name: ?[]const u8) !Symbol.Index { | 1166 | pub fn globalSymbol(coff: *Coff, name: []const u8, lib_name: ?[]const u8) !Symbol.Index { |
| 1085 | const gpa = coff.base.comp.gpa; | 1167 | const gpa = coff.base.comp.gpa; |
| 1086 | try coff.symbol_table.ensureUnusedCapacity(gpa, 1); | 1168 | try coff.symbol_table.ensureUnusedCapacity(gpa, 1); |
| ... | @@ -1095,6 +1177,43 @@ pub fn globalSymbol(coff: *Coff, name: []const u8, lib_name: ?[]const u8) !Symbo | ... | @@ -1095,6 +1177,43 @@ pub fn globalSymbol(coff: *Coff, name: []const u8, lib_name: ?[]const u8) !Symbo |
| 1095 | return sym_gop.value_ptr.*; | 1177 | return sym_gop.value_ptr.*; |
| 1096 | } | 1178 | } |
| 1097 | | 1179 | |
| | 1180 | fn navSection( |
| | 1181 | coff: *Coff, |
| | 1182 | zcu: *Zcu, |
| | 1183 | nav_fr: @FieldType(@FieldType(InternPool.Nav, "status"), "fully_resolved"), |
| | 1184 | ) !Symbol.Index { |
| | 1185 | const ip = &zcu.intern_pool; |
| | 1186 | const default: String, const attributes: ObjectSectionAttributes = |
| | 1187 | switch (ip.indexToKey(nav_fr.val)) { |
| | 1188 | else => .{ .@".rdata", .{ .read = true } }, |
| | 1189 | .variable => |variable| if (variable.is_threadlocal and |
| | 1190 | coff.base.comp.config.any_non_single_threaded) |
| | 1191 | .{ .@".tls$", .{ .read = true, .write = true } } |
| | 1192 | else |
| | 1193 | .{ .@".data", .{ .read = true, .write = true } }, |
| | 1194 | .@"extern" => |@"extern"| if (@"extern".is_threadlocal and |
| | 1195 | coff.base.comp.config.any_non_single_threaded) |
| | 1196 | .{ .@".tls$", .{ .read = true, .write = true } } |
| | 1197 | else if (ip.isFunctionType(@"extern".ty)) |
| | 1198 | .{ .@".text", .{ .read = true, .execute = true } } |
| | 1199 | else if (@"extern".is_const) |
| | 1200 | .{ .@".rdata", .{ .read = true } } |
| | 1201 | else |
| | 1202 | .{ .@".data", .{ .read = true, .write = true } }, |
| | 1203 | .func => .{ .@".text", .{ .read = true, .execute = true } }, |
| | 1204 | }; |
| | 1205 | return (try coff.objectSectionMapIndex( |
| | 1206 | (try coff.getOrPutOptionalString(nav_fr.@"linksection".toSlice(ip))).unwrap() orelse default, |
| | 1207 | switch (nav_fr.@"linksection") { |
| | 1208 | .none => coff.mf.flags.block_size, |
| | 1209 | else => switch (nav_fr.alignment) { |
| | 1210 | .none => Type.fromInterned(ip.typeOf(nav_fr.val)).abiAlignment(zcu), |
| | 1211 | else => |alignment| alignment, |
| | 1212 | }.toStdMem(), |
| | 1213 | }, |
| | 1214 | attributes, |
| | 1215 | )).symbol(coff); |
| | 1216 | } |
| 1098 | fn navMapIndex(coff: *Coff, zcu: *Zcu, nav_index: InternPool.Nav.Index) !Node.NavMapIndex { | 1217 | fn navMapIndex(coff: *Coff, zcu: *Zcu, nav_index: InternPool.Nav.Index) !Node.NavMapIndex { |
| 1099 | const gpa = zcu.gpa; | 1218 | const gpa = zcu.gpa; |
| 1100 | try coff.symbol_table.ensureUnusedCapacity(gpa, 1); | 1219 | try coff.symbol_table.ensureUnusedCapacity(gpa, 1); |
| ... | @@ -1171,7 +1290,7 @@ pub fn getVAddr(coff: *Coff, reloc_info: link.File.RelocInfo, target_si: Symbol. | ... | @@ -1171,7 +1290,7 @@ pub fn getVAddr(coff: *Coff, reloc_info: link.File.RelocInfo, target_si: Symbol. |
| 1171 | fn addSection(coff: *Coff, name: []const u8, flags: std.coff.SectionHeader.Flags) !Symbol.Index { | 1290 | fn addSection(coff: *Coff, name: []const u8, flags: std.coff.SectionHeader.Flags) !Symbol.Index { |
| 1172 | const gpa = coff.base.comp.gpa; | 1291 | const gpa = coff.base.comp.gpa; |
| 1173 | try coff.nodes.ensureUnusedCapacity(gpa, 1); | 1292 | try coff.nodes.ensureUnusedCapacity(gpa, 1); |
| 1174 | try coff.section_table.ensureUnusedCapacity(gpa, 1); | 1293 | try coff.image_section_table.ensureUnusedCapacity(gpa, 1); |
| 1175 | try coff.symbol_table.ensureUnusedCapacity(gpa, 1); | 1294 | try coff.symbol_table.ensureUnusedCapacity(gpa, 1); |
| 1176 | | 1295 | |
| 1177 | const coff_header = coff.headerPtr(); | 1296 | const coff_header = coff.headerPtr(); |
| ... | @@ -1189,13 +1308,13 @@ fn addSection(coff: *Coff, name: []const u8, flags: std.coff.SectionHeader.Flags | ... | @@ -1189,13 +1308,13 @@ fn addSection(coff: *Coff, name: []const u8, flags: std.coff.SectionHeader.Flags |
| 1189 | .bubbles_moved = false, | 1308 | .bubbles_moved = false, |
| 1190 | }); | 1309 | }); |
| 1191 | const si = coff.addSymbolAssumeCapacity(); | 1310 | const si = coff.addSymbolAssumeCapacity(); |
| 1192 | coff.section_table.appendAssumeCapacity(si); | 1311 | coff.image_section_table.appendAssumeCapacity(si); |
| 1193 | coff.nodes.appendAssumeCapacity(.{ .section = si }); | 1312 | coff.nodes.appendAssumeCapacity(.{ .image_section = si }); |
| 1194 | const section_table = coff.sectionTableSlice(); | 1313 | const section_table = coff.sectionTableSlice(); |
| 1195 | const virtual_size = coff.optionalHeaderField(.section_alignment); | 1314 | const virtual_size = coff.optionalHeaderField(.section_alignment); |
| 1196 | const rva: u32 = switch (section_index) { | 1315 | const rva: u32 = switch (section_index) { |
| 1197 | 0 => @intCast(Node.known.header.location(&coff.mf).resolve(&coff.mf)[1]), | 1316 | 0 => @intCast(Node.known.header.location(&coff.mf).resolve(&coff.mf)[1]), |
| 1198 | else => coff.section_table.items[section_index - 1].get(coff).rva + | 1317 | else => coff.image_section_table.items[section_index - 1].get(coff).rva + |
| 1199 | coff.targetLoad(&section_table[section_index - 1].virtual_size), | 1318 | coff.targetLoad(&section_table[section_index - 1].virtual_size), |
| 1200 | }; | 1319 | }; |
| 1201 | { | 1320 | { |
| ... | @@ -1230,6 +1349,99 @@ fn addSection(coff: *Coff, name: []const u8, flags: std.coff.SectionHeader.Flags | ... | @@ -1230,6 +1349,99 @@ fn addSection(coff: *Coff, name: []const u8, flags: std.coff.SectionHeader.Flags |
| 1230 | return si; | 1349 | return si; |
| 1231 | } | 1350 | } |
| 1232 | | 1351 | |
| | 1352 | const ObjectSectionAttributes = packed struct { |
| | 1353 | read: bool = false, |
| | 1354 | write: bool = false, |
| | 1355 | execute: bool = false, |
| | 1356 | shared: bool = false, |
| | 1357 | nopage: bool = false, |
| | 1358 | nocache: bool = false, |
| | 1359 | discard: bool = false, |
| | 1360 | remove: bool = false, |
| | 1361 | }; |
| | 1362 | fn pseudoSectionMapIndex( |
| | 1363 | coff: *Coff, |
| | 1364 | name: String, |
| | 1365 | alignment: std.mem.Alignment, |
| | 1366 | attributes: ObjectSectionAttributes, |
| | 1367 | ) !Node.PseudoSectionMapIndex { |
| | 1368 | const gpa = coff.base.comp.gpa; |
| | 1369 | const pseudo_section_gop = try coff.pseudo_section_table.getOrPut(gpa, name); |
| | 1370 | const psmi: Node.PseudoSectionMapIndex = @enumFromInt(pseudo_section_gop.index); |
| | 1371 | if (!pseudo_section_gop.found_existing) { |
| | 1372 | const parent: Symbol.Index = if (attributes.execute) |
| | 1373 | .text |
| | 1374 | else if (attributes.write) |
| | 1375 | .data |
| | 1376 | else |
| | 1377 | .rdata; |
| | 1378 | try coff.nodes.ensureUnusedCapacity(gpa, 1); |
| | 1379 | try coff.symbol_table.ensureUnusedCapacity(gpa, 1); |
| | 1380 | const ni = try coff.mf.addLastChildNode(gpa, parent.node(coff), .{ .alignment = alignment }); |
| | 1381 | const si = coff.addSymbolAssumeCapacity(); |
| | 1382 | pseudo_section_gop.value_ptr.* = si; |
| | 1383 | const sym = si.get(coff); |
| | 1384 | sym.ni = ni; |
| | 1385 | sym.rva = coff.computeNodeRva(ni); |
| | 1386 | sym.section_number = parent.get(coff).section_number; |
| | 1387 | assert(sym.loc_relocs == .none); |
| | 1388 | sym.loc_relocs = @enumFromInt(coff.relocs.items.len); |
| | 1389 | coff.nodes.appendAssumeCapacity(.{ .pseudo_section = psmi }); |
| | 1390 | } |
| | 1391 | return psmi; |
| | 1392 | } |
| | 1393 | fn objectSectionMapIndex( |
| | 1394 | coff: *Coff, |
| | 1395 | name: String, |
| | 1396 | alignment: std.mem.Alignment, |
| | 1397 | attributes: ObjectSectionAttributes, |
| | 1398 | ) !Node.ObjectSectionMapIndex { |
| | 1399 | const gpa = coff.base.comp.gpa; |
| | 1400 | const object_section_gop = try coff.object_section_table.getOrPut(gpa, name); |
| | 1401 | const osmi: Node.ObjectSectionMapIndex = @enumFromInt(object_section_gop.index); |
| | 1402 | if (!object_section_gop.found_existing) { |
| | 1403 | try coff.ensureUnusedStringCapacity(name.toSlice(coff).len); |
| | 1404 | const name_slice = name.toSlice(coff); |
| | 1405 | const parent = (try coff.pseudoSectionMapIndex(coff.getOrPutStringAssumeCapacity( |
| | 1406 | name_slice[0 .. std.mem.indexOfScalar(u8, name_slice, '$') orelse name_slice.len], |
| | 1407 | ), alignment, attributes)).symbol(coff); |
| | 1408 | try coff.nodes.ensureUnusedCapacity(gpa, 1); |
| | 1409 | try coff.symbol_table.ensureUnusedCapacity(gpa, 1); |
| | 1410 | const parent_ni = parent.node(coff); |
| | 1411 | var prev_ni: MappedFile.Node.Index = .none; |
| | 1412 | var next_it = parent_ni.children(&coff.mf); |
| | 1413 | while (next_it.next()) |next_ni| switch (std.mem.order( |
| | 1414 | u8, |
| | 1415 | name_slice, |
| | 1416 | coff.getNode(next_ni).object_section.name(coff).toSlice(coff), |
| | 1417 | )) { |
| | 1418 | .lt => break, |
| | 1419 | .eq => unreachable, |
| | 1420 | .gt => prev_ni = next_ni, |
| | 1421 | }; |
| | 1422 | const ni = switch (prev_ni) { |
| | 1423 | .none => try coff.mf.addFirstChildNode(gpa, parent_ni, .{ |
| | 1424 | .alignment = alignment, |
| | 1425 | .fixed = true, |
| | 1426 | }), |
| | 1427 | else => try coff.mf.addNodeAfter(gpa, prev_ni, .{ |
| | 1428 | .alignment = alignment, |
| | 1429 | .fixed = true, |
| | 1430 | }), |
| | 1431 | }; |
| | 1432 | const si = coff.addSymbolAssumeCapacity(); |
| | 1433 | object_section_gop.value_ptr.* = si; |
| | 1434 | const sym = si.get(coff); |
| | 1435 | sym.ni = ni; |
| | 1436 | sym.rva = coff.computeNodeRva(ni); |
| | 1437 | sym.section_number = parent.get(coff).section_number; |
| | 1438 | assert(sym.loc_relocs == .none); |
| | 1439 | sym.loc_relocs = @enumFromInt(coff.relocs.items.len); |
| | 1440 | coff.nodes.appendAssumeCapacity(.{ .object_section = osmi }); |
| | 1441 | } |
| | 1442 | return osmi; |
| | 1443 | } |
| | 1444 | |
| 1233 | pub fn addReloc( | 1445 | pub fn addReloc( |
| 1234 | coff: *Coff, | 1446 | coff: *Coff, |
| 1235 | loc_si: Symbol.Index, | 1447 | loc_si: Symbol.Index, |
| ... | @@ -1279,53 +1491,73 @@ fn updateNavInner(coff: *Coff, pt: Zcu.PerThread, nav_index: InternPool.Nav.Inde | ... | @@ -1279,53 +1491,73 @@ fn updateNavInner(coff: *Coff, pt: Zcu.PerThread, nav_index: InternPool.Nav.Inde |
| 1279 | | 1491 | |
| 1280 | const nav = ip.getNav(nav_index); | 1492 | const nav = ip.getNav(nav_index); |
| 1281 | const nav_val = nav.status.fully_resolved.val; | 1493 | const nav_val = nav.status.fully_resolved.val; |
| 1282 | const nav_init, const is_threadlocal = switch (ip.indexToKey(nav_val)) { | 1494 | const nav_init = switch (ip.indexToKey(nav_val)) { |
| 1283 | else => .{ nav_val, false }, | 1495 | else => nav_val, |
| 1284 | .variable => |variable| .{ variable.init, variable.is_threadlocal }, | 1496 | .variable => |variable| variable.init, |
| 1285 | .@"extern" => return, | 1497 | .@"extern", .func => .none, |
| 1286 | .func => .{ .none, false }, | | |
| 1287 | }; | 1498 | }; |
| 1288 | if (nav_init == .none or !Type.fromInterned(ip.typeOf(nav_init)).hasRuntimeBits(zcu)) return; | 1499 | if (nav_init == .none or !Type.fromInterned(ip.typeOf(nav_init)).hasRuntimeBits(zcu)) return; |
| 1289 | | 1500 | |
| 1290 | const nmi = try coff.navMapIndex(zcu, nav_index); | 1501 | const nmi = try coff.navMapIndex(zcu, nav_index); |
| 1291 | const si = nmi.symbol(coff); | 1502 | const si = nmi.symbol(coff); |
| 1292 | const ni = ni: { | 1503 | const ni = ni: { |
| 1293 | const sym = si.get(coff); | 1504 | switch (si.get(coff).ni) { |
| 1294 | switch (sym.ni) { | | |
| 1295 | .none => { | 1505 | .none => { |
| | 1506 | const sec_si = try coff.navSection(zcu, nav.status.fully_resolved); |
| 1296 | try coff.nodes.ensureUnusedCapacity(gpa, 1); | 1507 | try coff.nodes.ensureUnusedCapacity(gpa, 1); |
| 1297 | _ = is_threadlocal; | 1508 | const ni = try coff.mf.addLastChildNode(gpa, sec_si.node(coff), .{ |
| 1298 | const ni = try coff.mf.addLastChildNode(gpa, Symbol.Index.data.node(coff), .{ | | |
| 1299 | .alignment = pt.navAlignment(nav_index).toStdMem(), | 1509 | .alignment = pt.navAlignment(nav_index).toStdMem(), |
| 1300 | .moved = true, | 1510 | .moved = true, |
| 1301 | }); | 1511 | }); |
| 1302 | coff.nodes.appendAssumeCapacity(.{ .nav = nmi }); | 1512 | coff.nodes.appendAssumeCapacity(.{ .nav = nmi }); |
| | 1513 | const sym = si.get(coff); |
| 1303 | sym.ni = ni; | 1514 | sym.ni = ni; |
| 1304 | sym.section_number = Symbol.Index.data.get(coff).section_number; | 1515 | sym.section_number = sec_si.get(coff).section_number; |
| 1305 | }, | 1516 | }, |
| 1306 | else => si.deleteLocationRelocs(coff), | 1517 | else => si.deleteLocationRelocs(coff), |
| 1307 | } | 1518 | } |
| | 1519 | const sym = si.get(coff); |
| 1308 | assert(sym.loc_relocs == .none); | 1520 | assert(sym.loc_relocs == .none); |
| 1309 | sym.loc_relocs = @enumFromInt(coff.relocs.items.len); | 1521 | sym.loc_relocs = @enumFromInt(coff.relocs.items.len); |
| 1310 | break :ni sym.ni; | 1522 | break :ni sym.ni; |
| 1311 | }; | 1523 | }; |
| 1312 | | 1524 | |
| 1313 | var nw: MappedFile.Node.Writer = undefined; | 1525 | { |
| 1314 | ni.writer(&coff.mf, gpa, &nw); | 1526 | var nw: MappedFile.Node.Writer = undefined; |
| 1315 | defer nw.deinit(); | 1527 | ni.writer(&coff.mf, gpa, &nw); |
| 1316 | codegen.generateSymbol( | 1528 | defer nw.deinit(); |
| 1317 | &coff.base, | 1529 | codegen.generateSymbol( |
| 1318 | pt, | 1530 | &coff.base, |
| 1319 | zcu.navSrcLoc(nav_index), | 1531 | pt, |
| 1320 | .fromInterned(nav_init), | 1532 | zcu.navSrcLoc(nav_index), |
| 1321 | &nw.interface, | 1533 | .fromInterned(nav_init), |
| 1322 | .{ .atom_index = @intFromEnum(si) }, | 1534 | &nw.interface, |
| 1323 | ) catch |err| switch (err) { | 1535 | .{ .atom_index = @intFromEnum(si) }, |
| 1324 | error.WriteFailed => return error.OutOfMemory, | 1536 | ) catch |err| switch (err) { |
| 1325 | else => |e| return e, | 1537 | error.WriteFailed => return error.OutOfMemory, |
| 1326 | }; | 1538 | else => |e| return e, |
| 1327 | si.get(coff).size = @intCast(nw.interface.end); | 1539 | }; |
| 1328 | si.applyLocationRelocs(coff); | 1540 | si.get(coff).size = @intCast(nw.interface.end); |
| | 1541 | si.applyLocationRelocs(coff); |
| | 1542 | } |
| | 1543 | |
| | 1544 | if (nav.status.fully_resolved.@"linksection".unwrap()) |_| { |
| | 1545 | try ni.resize(&coff.mf, gpa, si.get(coff).size); |
| | 1546 | var parent_ni = ni; |
| | 1547 | while (true) { |
| | 1548 | parent_ni = parent_ni.parent(&coff.mf); |
| | 1549 | switch (coff.getNode(parent_ni)) { |
| | 1550 | else => unreachable, |
| | 1551 | .image_section, .pseudo_section => break, |
| | 1552 | .object_section => { |
| | 1553 | var child_it = parent_ni.reverseChildren(&coff.mf); |
| | 1554 | const last_offset, const last_size = |
| | 1555 | child_it.next().?.location(&coff.mf).resolve(&coff.mf); |
| | 1556 | try parent_ni.resize(&coff.mf, gpa, last_offset + last_size); |
| | 1557 | }, |
| | 1558 | } |
| | 1559 | } |
| | 1560 | } |
| 1329 | } | 1561 | } |
| 1330 | | 1562 | |
| 1331 | pub fn lowerUav( | 1563 | pub fn lowerUav( |
| ... | @@ -1394,13 +1626,13 @@ fn updateFuncInner( | ... | @@ -1394,13 +1626,13 @@ fn updateFuncInner( |
| 1394 | const si = nmi.symbol(coff); | 1626 | const si = nmi.symbol(coff); |
| 1395 | log.debug("updateFunc({f}) = {d}", .{ nav.fqn.fmt(ip), si }); | 1627 | log.debug("updateFunc({f}) = {d}", .{ nav.fqn.fmt(ip), si }); |
| 1396 | const ni = ni: { | 1628 | const ni = ni: { |
| 1397 | const sym = si.get(coff); | 1629 | switch (si.get(coff).ni) { |
| 1398 | switch (sym.ni) { | | |
| 1399 | .none => { | 1630 | .none => { |
| | 1631 | const sec_si = try coff.navSection(zcu, nav.status.fully_resolved); |
| 1400 | try coff.nodes.ensureUnusedCapacity(gpa, 1); | 1632 | try coff.nodes.ensureUnusedCapacity(gpa, 1); |
| 1401 | const mod = zcu.navFileScope(func.owner_nav).mod.?; | 1633 | const mod = zcu.navFileScope(func.owner_nav).mod.?; |
| 1402 | const target = &mod.resolved_target.result; | 1634 | const target = &mod.resolved_target.result; |
| 1403 | const ni = try coff.mf.addLastChildNode(gpa, Symbol.Index.text.node(coff), .{ | 1635 | const ni = try coff.mf.addLastChildNode(gpa, sec_si.node(coff), .{ |
| 1404 | .alignment = switch (nav.status.fully_resolved.alignment) { | 1636 | .alignment = switch (nav.status.fully_resolved.alignment) { |
| 1405 | .none => switch (mod.optimize_mode) { | 1637 | .none => switch (mod.optimize_mode) { |
| 1406 | .Debug, | 1638 | .Debug, |
| ... | @@ -1414,11 +1646,13 @@ fn updateFuncInner( | ... | @@ -1414,11 +1646,13 @@ fn updateFuncInner( |
| 1414 | .moved = true, | 1646 | .moved = true, |
| 1415 | }); | 1647 | }); |
| 1416 | coff.nodes.appendAssumeCapacity(.{ .nav = nmi }); | 1648 | coff.nodes.appendAssumeCapacity(.{ .nav = nmi }); |
| | 1649 | const sym = si.get(coff); |
| 1417 | sym.ni = ni; | 1650 | sym.ni = ni; |
| 1418 | sym.section_number = Symbol.Index.text.get(coff).section_number; | 1651 | sym.section_number = sec_si.get(coff).section_number; |
| 1419 | }, | 1652 | }, |
| 1420 | else => si.deleteLocationRelocs(coff), | 1653 | else => si.deleteLocationRelocs(coff), |
| 1421 | } | 1654 | } |
| | 1655 | const sym = si.get(coff); |
| 1422 | assert(sym.loc_relocs == .none); | 1656 | assert(sym.loc_relocs == .none); |
| 1423 | sym.loc_relocs = @enumFromInt(coff.relocs.items.len); | 1657 | sym.loc_relocs = @enumFromInt(coff.relocs.items.len); |
| 1424 | break :ni sym.ni; | 1658 | break :ni sym.ni; |
| ... | @@ -1492,11 +1726,8 @@ pub fn idle(coff: *Coff, tid: Zcu.PerThread.Id) !bool { | ... | @@ -1492,11 +1726,8 @@ pub fn idle(coff: *Coff, tid: Zcu.PerThread.Id) !bool { |
| 1492 | const comp = coff.base.comp; | 1726 | const comp = coff.base.comp; |
| 1493 | task: { | 1727 | task: { |
| 1494 | while (coff.pending_uavs.pop()) |pending_uav| { | 1728 | while (coff.pending_uavs.pop()) |pending_uav| { |
| 1495 | const sub_prog_node = coff.idleProgNode( | 1729 | const sub_prog_node = |
| 1496 | tid, | 1730 | coff.idleProgNode(tid, comp.link_const_prog_node, .{ .uav = pending_uav.key }); |
| 1497 | comp.link_const_prog_node, | | |
| 1498 | .{ .uav = pending_uav.key }, | | |
| 1499 | ); | | |
| 1500 | defer sub_prog_node.end(); | 1731 | defer sub_prog_node.end(); |
| 1501 | coff.flushUav( | 1732 | coff.flushUav( |
| 1502 | .{ .zcu = coff.base.comp.zcu.?, .tid = tid }, | 1733 | .{ .zcu = coff.base.comp.zcu.?, .tid = tid }, |
| ... | @@ -1561,7 +1792,8 @@ pub fn idle(coff: *Coff, tid: Zcu.PerThread.Id) !bool { | ... | @@ -1561,7 +1792,8 @@ pub fn idle(coff: *Coff, tid: Zcu.PerThread.Id) !bool { |
| 1561 | const clean_moved = ni.cleanMoved(&coff.mf); | 1792 | const clean_moved = ni.cleanMoved(&coff.mf); |
| 1562 | const clean_resized = ni.cleanResized(&coff.mf); | 1793 | const clean_resized = ni.cleanResized(&coff.mf); |
| 1563 | if (clean_moved or clean_resized) { | 1794 | if (clean_moved or clean_resized) { |
| 1564 | const sub_prog_node = coff.idleProgNode(tid, coff.mf.update_prog_node, coff.getNode(ni)); | 1795 | const sub_prog_node = |
| | 1796 | coff.idleProgNode(tid, coff.mf.update_prog_node, coff.getNode(ni)); |
| 1565 | defer sub_prog_node.end(); | 1797 | defer sub_prog_node.end(); |
| 1566 | if (clean_moved) try coff.flushMoved(ni); | 1798 | if (clean_moved) try coff.flushMoved(ni); |
| 1567 | if (clean_resized) try coff.flushResized(ni); | 1799 | if (clean_resized) try coff.flushResized(ni); |
| ... | @@ -1584,7 +1816,9 @@ fn idleProgNode( | ... | @@ -1584,7 +1816,9 @@ fn idleProgNode( |
| 1584 | var name: [std.Progress.Node.max_name_len]u8 = undefined; | 1816 | var name: [std.Progress.Node.max_name_len]u8 = undefined; |
| 1585 | return prog_node.start(name: switch (node) { | 1817 | return prog_node.start(name: switch (node) { |
| 1586 | else => |tag| @tagName(tag), | 1818 | else => |tag| @tagName(tag), |
| 1587 | .section => |si| std.mem.sliceTo(&si.get(coff).section_number.header(coff).name, 0), | 1819 | .image_section => |si| std.mem.sliceTo(&si.get(coff).section_number.header(coff).name, 0), |
| | 1820 | inline .pseudo_section, .object_section => |smi| smi.name(coff).toSlice(coff), |
| | 1821 | .global => |gmi| gmi.globalName(coff).name.toSlice(coff), |
| 1588 | .nav => |nmi| { | 1822 | .nav => |nmi| { |
| 1589 | const ip = &coff.base.comp.zcu.?.intern_pool; | 1823 | const ip = &coff.base.comp.zcu.?.intern_pool; |
| 1590 | break :name ip.getNav(nmi.navIndex(coff)).fqn.toSlice(ip); | 1824 | break :name ip.getNav(nmi.navIndex(coff)).fqn.toSlice(ip); |
| ... | @@ -1611,23 +1845,30 @@ fn flushUav( | ... | @@ -1611,23 +1845,30 @@ fn flushUav( |
| 1611 | const uav_val = umi.uavValue(coff); | 1845 | const uav_val = umi.uavValue(coff); |
| 1612 | const si = umi.symbol(coff); | 1846 | const si = umi.symbol(coff); |
| 1613 | const ni = ni: { | 1847 | const ni = ni: { |
| 1614 | const sym = si.get(coff); | 1848 | switch (si.get(coff).ni) { |
| 1615 | switch (sym.ni) { | | |
| 1616 | .none => { | 1849 | .none => { |
| | 1850 | const sec_si = (try coff.objectSectionMapIndex( |
| | 1851 | .@".rdata", |
| | 1852 | coff.mf.flags.block_size, |
| | 1853 | .{ .read = true }, |
| | 1854 | )).symbol(coff); |
| 1617 | try coff.nodes.ensureUnusedCapacity(gpa, 1); | 1855 | try coff.nodes.ensureUnusedCapacity(gpa, 1); |
| 1618 | const ni = try coff.mf.addLastChildNode(gpa, Symbol.Index.data.node(coff), .{ | 1856 | const sym = si.get(coff); |
| | 1857 | const ni = try coff.mf.addLastChildNode(gpa, sec_si.node(coff), .{ |
| 1619 | .alignment = uav_align.toStdMem(), | 1858 | .alignment = uav_align.toStdMem(), |
| 1620 | .moved = true, | 1859 | .moved = true, |
| 1621 | }); | 1860 | }); |
| 1622 | coff.nodes.appendAssumeCapacity(.{ .uav = umi }); | 1861 | coff.nodes.appendAssumeCapacity(.{ .uav = umi }); |
| 1623 | sym.ni = ni; | 1862 | sym.ni = ni; |
| 1624 | sym.section_number = Symbol.Index.data.get(coff).section_number; | 1863 | sym.section_number = sec_si.get(coff).section_number; |
| 1625 | }, | 1864 | }, |
| 1626 | else => { | 1865 | else => { |
| 1627 | if (sym.ni.alignment(&coff.mf).order(uav_align.toStdMem()).compare(.gte)) return; | 1866 | if (si.get(coff).ni.alignment(&coff.mf).order(uav_align.toStdMem()).compare(.gte)) |
| | 1867 | return; |
| 1628 | si.deleteLocationRelocs(coff); | 1868 | si.deleteLocationRelocs(coff); |
| 1629 | }, | 1869 | }, |
| 1630 | } | 1870 | } |
| | 1871 | const sym = si.get(coff); |
| 1631 | assert(sym.loc_relocs == .none); | 1872 | assert(sym.loc_relocs == .none); |
| 1632 | sym.loc_relocs = @enumFromInt(coff.relocs.items.len); | 1873 | sym.loc_relocs = @enumFromInt(coff.relocs.items.len); |
| 1633 | break :ni sym.ni; | 1874 | break :ni sym.ni; |
| ... | @@ -1684,7 +1925,7 @@ fn flushGlobal(coff: *Coff, pt: Zcu.PerThread, gmi: Node.GlobalMapIndex) !void { | ... | @@ -1684,7 +1925,7 @@ fn flushGlobal(coff: *Coff, pt: Zcu.PerThread, gmi: Node.GlobalMapIndex) !void { |
| 1684 | ); | 1925 | ); |
| 1685 | const import_hint_name_table_len = | 1926 | const import_hint_name_table_len = |
| 1686 | import_hint_name_align.forward(lib_name.len + ".dll".len + 1); | 1927 | import_hint_name_align.forward(lib_name.len + ".dll".len + 1); |
| 1687 | const idata_section_ni = Symbol.Index.idata.node(coff); | 1928 | const idata_section_ni = coff.import_table.ni.parent(&coff.mf); |
| 1688 | const import_lookup_table_ni = try coff.mf.addLastChildNode(gpa, idata_section_ni, .{ | 1929 | const import_lookup_table_ni = try coff.mf.addLastChildNode(gpa, idata_section_ni, .{ |
| 1689 | .size = addr_size * 2, | 1930 | .size = addr_size * 2, |
| 1690 | .alignment = addr_align, | 1931 | .alignment = addr_align, |
| ... | @@ -1701,7 +1942,8 @@ fn flushGlobal(coff: *Coff, pt: Zcu.PerThread, gmi: Node.GlobalMapIndex) !void { | ... | @@ -1701,7 +1942,8 @@ fn flushGlobal(coff: *Coff, pt: Zcu.PerThread, gmi: Node.GlobalMapIndex) !void { |
| 1701 | import_address_table_sym.ni = import_address_table_ni; | 1942 | import_address_table_sym.ni = import_address_table_ni; |
| 1702 | assert(import_address_table_sym.loc_relocs == .none); | 1943 | assert(import_address_table_sym.loc_relocs == .none); |
| 1703 | import_address_table_sym.loc_relocs = @enumFromInt(coff.relocs.items.len); | 1944 | import_address_table_sym.loc_relocs = @enumFromInt(coff.relocs.items.len); |
| 1704 | import_address_table_sym.section_number = Symbol.Index.idata.get(coff).section_number; | 1945 | import_address_table_sym.section_number = |
| | 1946 | coff.getNode(idata_section_ni).object_section.symbol(coff).get(coff).section_number; |
| 1705 | } | 1947 | } |
| 1706 | const import_hint_name_table_ni = try coff.mf.addLastChildNode(gpa, idata_section_ni, .{ | 1948 | const import_hint_name_table_ni = try coff.mf.addLastChildNode(gpa, idata_section_ni, .{ |
| 1707 | .size = import_hint_name_table_len, | 1949 | .size = import_hint_name_table_len, |
| ... | @@ -1873,12 +2115,12 @@ fn flushMoved(coff: *Coff, ni: MappedFile.Node.Index) !void { | ... | @@ -1873,12 +2115,12 @@ fn flushMoved(coff: *Coff, ni: MappedFile.Node.Index) !void { |
| 1873 | .data_directories, | 2115 | .data_directories, |
| 1874 | .section_table, | 2116 | .section_table, |
| 1875 | => unreachable, | 2117 | => unreachable, |
| 1876 | .section => |si| return coff.targetStore( | 2118 | .image_section => |si| return coff.targetStore( |
| 1877 | &si.get(coff).section_number.header(coff).pointer_to_raw_data, | 2119 | &si.get(coff).section_number.header(coff).pointer_to_raw_data, |
| 1878 | @intCast(ni.fileLocation(&coff.mf, false).offset), | 2120 | @intCast(ni.fileLocation(&coff.mf, false).offset), |
| 1879 | ), | 2121 | ), |
| 1880 | .import_directory_table => coff.targetStore( | 2122 | .import_directory_table => coff.targetStore( |
| 1881 | &coff.dataDirectoryPtr(.import_table).virtual_address, | 2123 | &coff.dataDirectoryPtr(.IMPORT).virtual_address, |
| 1882 | coff.computeNodeRva(ni), | 2124 | coff.computeNodeRva(ni), |
| 1883 | ), | 2125 | ), |
| 1884 | .import_lookup_table => |import_index| coff.targetStore( | 2126 | .import_lookup_table => |import_index| coff.targetStore( |
| ... | @@ -1939,7 +2181,9 @@ fn flushMoved(coff: *Coff, ni: MappedFile.Node.Index) !void { | ... | @@ -1939,7 +2181,9 @@ fn flushMoved(coff: *Coff, ni: MappedFile.Node.Index) !void { |
| 1939 | import_hint_name_index += 2; | 2181 | import_hint_name_index += 2; |
| 1940 | } | 2182 | } |
| 1941 | }, | 2183 | }, |
| 1942 | inline .global, | 2184 | inline .pseudo_section, |
| | 2185 | .object_section, |
| | 2186 | .global, |
| 1943 | .nav, | 2187 | .nav, |
| 1944 | .uav, | 2188 | .uav, |
| 1945 | .lazy_code, | 2189 | .lazy_code, |
| ... | @@ -1960,7 +2204,7 @@ fn flushResized(coff: *Coff, ni: MappedFile.Node.Index) !void { | ... | @@ -1960,7 +2204,7 @@ fn flushResized(coff: *Coff, ni: MappedFile.Node.Index) !void { |
| 1960 | @intCast(size), | 2204 | @intCast(size), |
| 1961 | ), | 2205 | ), |
| 1962 | } | 2206 | } |
| 1963 | if (size > coff.section_table.items[0].get(coff).rva) try coff.virtualSlide( | 2207 | if (size > coff.image_section_table.items[0].get(coff).rva) try coff.virtualSlide( |
| 1964 | 0, | 2208 | 0, |
| 1965 | std.mem.alignForward( | 2209 | std.mem.alignForward( |
| 1966 | u32, | 2210 | u32, |
| ... | @@ -1971,7 +2215,7 @@ fn flushResized(coff: *Coff, ni: MappedFile.Node.Index) !void { | ... | @@ -1971,7 +2215,7 @@ fn flushResized(coff: *Coff, ni: MappedFile.Node.Index) !void { |
| 1971 | }, | 2215 | }, |
| 1972 | .signature, .coff_header, .optional_header, .data_directories => unreachable, | 2216 | .signature, .coff_header, .optional_header, .data_directories => unreachable, |
| 1973 | .section_table => {}, | 2217 | .section_table => {}, |
| 1974 | .section => |si| { | 2218 | .image_section => |si| { |
| 1975 | const sym = si.get(coff); | 2219 | const sym = si.get(coff); |
| 1976 | const section_index = sym.section_number.toIndex(); | 2220 | const section_index = sym.section_number.toIndex(); |
| 1977 | const section = &coff.sectionTableSlice()[section_index]; | 2221 | const section = &coff.sectionTableSlice()[section_index]; |
| ... | @@ -1987,24 +2231,20 @@ fn flushResized(coff: *Coff, ni: MappedFile.Node.Index) !void { | ... | @@ -1987,24 +2231,20 @@ fn flushResized(coff: *Coff, ni: MappedFile.Node.Index) !void { |
| 1987 | } | 2231 | } |
| 1988 | }, | 2232 | }, |
| 1989 | .import_directory_table => coff.targetStore( | 2233 | .import_directory_table => coff.targetStore( |
| 1990 | &coff.dataDirectoryPtr(.import_table).size, | 2234 | &coff.dataDirectoryPtr(.IMPORT).size, |
| 1991 | @intCast(size), | 2235 | @intCast(size), |
| 1992 | ), | 2236 | ), |
| 1993 | .import_lookup_table, | 2237 | .import_lookup_table, .import_address_table, .import_hint_name_table => {}, |
| 1994 | .import_address_table, | 2238 | inline .pseudo_section, |
| 1995 | .import_hint_name_table, | 2239 | .object_section, |
| 1996 | .global, | 2240 | => |smi| smi.symbol(coff).get(coff).size = @intCast(size), |
| 1997 | .nav, | 2241 | .global, .nav, .uav, .lazy_code, .lazy_const_data => {}, |
| 1998 | .uav, | | |
| 1999 | .lazy_code, | | |
| 2000 | .lazy_const_data, | | |
| 2001 | => {}, | | |
| 2002 | } | 2242 | } |
| 2003 | } | 2243 | } |
| 2004 | fn virtualSlide(coff: *Coff, start_section_index: usize, start_rva: u32) !void { | 2244 | fn virtualSlide(coff: *Coff, start_section_index: usize, start_rva: u32) !void { |
| 2005 | var rva = start_rva; | 2245 | var rva = start_rva; |
| 2006 | for ( | 2246 | for ( |
| 2007 | coff.section_table.items[start_section_index..], | 2247 | coff.image_section_table.items[start_section_index..], |
| 2008 | coff.sectionTableSlice()[start_section_index..], | 2248 | coff.sectionTableSlice()[start_section_index..], |
| 2009 | ) |section_si, *section| { | 2249 | ) |section_si, *section| { |
| 2010 | const section_sym = section_si.get(coff); | 2250 | const section_sym = section_si.get(coff); |
| ... | @@ -2078,8 +2318,12 @@ fn updateExportsInner( | ... | @@ -2078,8 +2318,12 @@ fn updateExportsInner( |
| 2078 | export_sym.section_number = exported_sym.section_number; | 2318 | export_sym.section_number = exported_sym.section_number; |
| 2079 | export_si.applyTargetRelocs(coff); | 2319 | export_si.applyTargetRelocs(coff); |
| 2080 | if (@"export".opts.name.eqlSlice("wWinMainCRTStartup", ip)) { | 2320 | if (@"export".opts.name.eqlSlice("wWinMainCRTStartup", ip)) { |
| 2081 | coff.entry_hack = exported_si; | | |
| 2082 | coff.optionalHeaderStandardPtr().address_of_entry_point = exported_sym.rva; | 2321 | coff.optionalHeaderStandardPtr().address_of_entry_point = exported_sym.rva; |
| | 2322 | } else if (@"export".opts.name.eqlSlice("_tls_used", ip)) { |
| | 2323 | const tls_directory = coff.dataDirectoryPtr(.TLS); |
| | 2324 | tls_directory.* = .{ .virtual_address = exported_sym.rva, .size = exported_sym.size }; |
| | 2325 | if (coff.targetEndian() != native_endian) |
| | 2326 | std.mem.byteSwapAllFields(std.coff.ImageDataDirectory, tls_directory); |
| 2083 | } | 2327 | } |
| 2084 | } | 2328 | } |
| 2085 | } | 2329 | } |
| ... | @@ -2108,7 +2352,7 @@ pub fn printNode( | ... | @@ -2108,7 +2352,7 @@ pub fn printNode( |
| 2108 | try w.writeAll(@tagName(node)); | 2352 | try w.writeAll(@tagName(node)); |
| 2109 | switch (node) { | 2353 | switch (node) { |
| 2110 | else => {}, | 2354 | else => {}, |
| 2111 | .section => |si| try w.print("({s})", .{ | 2355 | .image_section => |si| try w.print("({s})", .{ |
| 2112 | std.mem.sliceTo(&si.get(coff).section_number.header(coff).name, 0), | 2356 | std.mem.sliceTo(&si.get(coff).section_number.header(coff).name, 0), |
| 2113 | }), | 2357 | }), |
| 2114 | .import_lookup_table, | 2358 | .import_lookup_table, |
| ... | @@ -2117,6 +2361,9 @@ pub fn printNode( | ... | @@ -2117,6 +2361,9 @@ pub fn printNode( |
| 2117 | => |import_index| try w.print("({s})", .{ | 2361 | => |import_index| try w.print("({s})", .{ |
| 2118 | std.mem.sliceTo(import_index.get(coff).import_hint_name_table_ni.sliceConst(&coff.mf), 0), | 2362 | std.mem.sliceTo(import_index.get(coff).import_hint_name_table_ni.sliceConst(&coff.mf), 0), |
| 2119 | }), | 2363 | }), |
| | 2364 | inline .pseudo_section, .object_section => |smi| try w.print("({s})", .{ |
| | 2365 | smi.name(coff).toSlice(coff), |
| | 2366 | }), |
| 2120 | .global => |gmi| { | 2367 | .global => |gmi| { |
| 2121 | const gn = gmi.globalName(coff); | 2368 | const gn = gmi.globalName(coff); |
| 2122 | try w.writeByte('('); | 2369 | try w.writeByte('('); |