| ... | @@ -8,7 +8,7 @@ const fs = std.fs; | ... | @@ -8,7 +8,7 @@ const fs = std.fs; |
| 8 | const elf = std.elf; | 8 | const elf = std.elf; |
| 9 | const codegen = @import("codegen.zig"); | 9 | const codegen = @import("codegen.zig"); |
| 10 | const c_codegen = @import("codegen/c.zig"); | 10 | const c_codegen = @import("codegen/c.zig"); |
| 11 | const log = std.log; | 11 | const log = std.log.scoped(.link); |
| 12 | const DW = std.dwarf; | 12 | const DW = std.dwarf; |
| 13 | const trace = @import("tracy.zig").trace; | 13 | const trace = @import("tracy.zig").trace; |
| 14 | const leb128 = std.debug.leb; | 14 | const leb128 = std.debug.leb; |
| ... | @@ -726,7 +726,7 @@ pub const File = struct { | ... | @@ -726,7 +726,7 @@ pub const File = struct { |
| 726 | const file_size = self.base.options.program_code_size_hint; | 726 | const file_size = self.base.options.program_code_size_hint; |
| 727 | const p_align = 0x1000; | 727 | const p_align = 0x1000; |
| 728 | const off = self.findFreeSpace(file_size, p_align); | 728 | const off = self.findFreeSpace(file_size, p_align); |
| 729 | log.debug(.link, "found PT_LOAD free space 0x{x} to 0x{x}\n", .{ off, off + file_size }); | 729 | log.debug("found PT_LOAD free space 0x{x} to 0x{x}\n", .{ off, off + file_size }); |
| 730 | try self.program_headers.append(self.base.allocator, .{ | 730 | try self.program_headers.append(self.base.allocator, .{ |
| 731 | .p_type = elf.PT_LOAD, | 731 | .p_type = elf.PT_LOAD, |
| 732 | .p_offset = off, | 732 | .p_offset = off, |
| ... | @@ -747,7 +747,7 @@ pub const File = struct { | ... | @@ -747,7 +747,7 @@ pub const File = struct { |
| 747 | // page align. | 747 | // page align. |
| 748 | const p_align = if (self.base.options.target.os.tag == .linux) 0x1000 else @as(u16, ptr_size); | 748 | const p_align = if (self.base.options.target.os.tag == .linux) 0x1000 else @as(u16, ptr_size); |
| 749 | const off = self.findFreeSpace(file_size, p_align); | 749 | const off = self.findFreeSpace(file_size, p_align); |
| 750 | log.debug(.link, "found PT_LOAD free space 0x{x} to 0x{x}\n", .{ off, off + file_size }); | 750 | log.debug("found PT_LOAD free space 0x{x} to 0x{x}\n", .{ off, off + file_size }); |
| 751 | // TODO instead of hard coding the vaddr, make a function to find a vaddr to put things at. | 751 | // TODO instead of hard coding the vaddr, make a function to find a vaddr to put things at. |
| 752 | // we'll need to re-use that function anyway, in case the GOT grows and overlaps something | 752 | // we'll need to re-use that function anyway, in case the GOT grows and overlaps something |
| 753 | // else in virtual memory. | 753 | // else in virtual memory. |
| ... | @@ -769,7 +769,7 @@ pub const File = struct { | ... | @@ -769,7 +769,7 @@ pub const File = struct { |
| 769 | assert(self.shstrtab.items.len == 0); | 769 | assert(self.shstrtab.items.len == 0); |
| 770 | try self.shstrtab.append(self.base.allocator, 0); // need a 0 at position 0 | 770 | try self.shstrtab.append(self.base.allocator, 0); // need a 0 at position 0 |
| 771 | const off = self.findFreeSpace(self.shstrtab.items.len, 1); | 771 | const off = self.findFreeSpace(self.shstrtab.items.len, 1); |
| 772 | log.debug(.link, "found shstrtab free space 0x{x} to 0x{x}\n", .{ off, off + self.shstrtab.items.len }); | 772 | log.debug("found shstrtab free space 0x{x} to 0x{x}\n", .{ off, off + self.shstrtab.items.len }); |
| 773 | try self.sections.append(self.base.allocator, .{ | 773 | try self.sections.append(self.base.allocator, .{ |
| 774 | .sh_name = try self.makeString(".shstrtab"), | 774 | .sh_name = try self.makeString(".shstrtab"), |
| 775 | .sh_type = elf.SHT_STRTAB, | 775 | .sh_type = elf.SHT_STRTAB, |
| ... | @@ -827,7 +827,7 @@ pub const File = struct { | ... | @@ -827,7 +827,7 @@ pub const File = struct { |
| 827 | const each_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym); | 827 | const each_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym); |
| 828 | const file_size = self.base.options.symbol_count_hint * each_size; | 828 | const file_size = self.base.options.symbol_count_hint * each_size; |
| 829 | const off = self.findFreeSpace(file_size, min_align); | 829 | const off = self.findFreeSpace(file_size, min_align); |
| 830 | log.debug(.link, "found symtab free space 0x{x} to 0x{x}\n", .{ off, off + file_size }); | 830 | log.debug("found symtab free space 0x{x} to 0x{x}\n", .{ off, off + file_size }); |
| 831 | | 831 | |
| 832 | try self.sections.append(self.base.allocator, .{ | 832 | try self.sections.append(self.base.allocator, .{ |
| 833 | .sh_name = try self.makeString(".symtab"), | 833 | .sh_name = try self.makeString(".symtab"), |
| ... | @@ -869,7 +869,7 @@ pub const File = struct { | ... | @@ -869,7 +869,7 @@ pub const File = struct { |
| 869 | const file_size_hint = 200; | 869 | const file_size_hint = 200; |
| 870 | const p_align = 1; | 870 | const p_align = 1; |
| 871 | const off = self.findFreeSpace(file_size_hint, p_align); | 871 | const off = self.findFreeSpace(file_size_hint, p_align); |
| 872 | log.debug(.link, "found .debug_info free space 0x{x} to 0x{x}\n", .{ | 872 | log.debug("found .debug_info free space 0x{x} to 0x{x}\n", .{ |
| 873 | off, | 873 | off, |
| 874 | off + file_size_hint, | 874 | off + file_size_hint, |
| 875 | }); | 875 | }); |
| ... | @@ -894,7 +894,7 @@ pub const File = struct { | ... | @@ -894,7 +894,7 @@ pub const File = struct { |
| 894 | const file_size_hint = 128; | 894 | const file_size_hint = 128; |
| 895 | const p_align = 1; | 895 | const p_align = 1; |
| 896 | const off = self.findFreeSpace(file_size_hint, p_align); | 896 | const off = self.findFreeSpace(file_size_hint, p_align); |
| 897 | log.debug(.link, "found .debug_abbrev free space 0x{x} to 0x{x}\n", .{ | 897 | log.debug("found .debug_abbrev free space 0x{x} to 0x{x}\n", .{ |
| 898 | off, | 898 | off, |
| 899 | off + file_size_hint, | 899 | off + file_size_hint, |
| 900 | }); | 900 | }); |
| ... | @@ -919,7 +919,7 @@ pub const File = struct { | ... | @@ -919,7 +919,7 @@ pub const File = struct { |
| 919 | const file_size_hint = 160; | 919 | const file_size_hint = 160; |
| 920 | const p_align = 16; | 920 | const p_align = 16; |
| 921 | const off = self.findFreeSpace(file_size_hint, p_align); | 921 | const off = self.findFreeSpace(file_size_hint, p_align); |
| 922 | log.debug(.link, "found .debug_aranges free space 0x{x} to 0x{x}\n", .{ | 922 | log.debug("found .debug_aranges free space 0x{x} to 0x{x}\n", .{ |
| 923 | off, | 923 | off, |
| 924 | off + file_size_hint, | 924 | off + file_size_hint, |
| 925 | }); | 925 | }); |
| ... | @@ -944,7 +944,7 @@ pub const File = struct { | ... | @@ -944,7 +944,7 @@ pub const File = struct { |
| 944 | const file_size_hint = 250; | 944 | const file_size_hint = 250; |
| 945 | const p_align = 1; | 945 | const p_align = 1; |
| 946 | const off = self.findFreeSpace(file_size_hint, p_align); | 946 | const off = self.findFreeSpace(file_size_hint, p_align); |
| 947 | log.debug(.link, "found .debug_line free space 0x{x} to 0x{x}\n", .{ | 947 | log.debug("found .debug_line free space 0x{x} to 0x{x}\n", .{ |
| 948 | off, | 948 | off, |
| 949 | off + file_size_hint, | 949 | off + file_size_hint, |
| 950 | }); | 950 | }); |
| ... | @@ -1071,7 +1071,7 @@ pub const File = struct { | ... | @@ -1071,7 +1071,7 @@ pub const File = struct { |
| 1071 | debug_abbrev_sect.sh_offset = self.findFreeSpace(needed_size, 1); | 1071 | debug_abbrev_sect.sh_offset = self.findFreeSpace(needed_size, 1); |
| 1072 | } | 1072 | } |
| 1073 | debug_abbrev_sect.sh_size = needed_size; | 1073 | debug_abbrev_sect.sh_size = needed_size; |
| 1074 | log.debug(.link, ".debug_abbrev start=0x{x} end=0x{x}\n", .{ | 1074 | log.debug(".debug_abbrev start=0x{x} end=0x{x}\n", .{ |
| 1075 | debug_abbrev_sect.sh_offset, | 1075 | debug_abbrev_sect.sh_offset, |
| 1076 | debug_abbrev_sect.sh_offset + needed_size, | 1076 | debug_abbrev_sect.sh_offset + needed_size, |
| 1077 | }); | 1077 | }); |
| ... | @@ -1218,7 +1218,7 @@ pub const File = struct { | ... | @@ -1218,7 +1218,7 @@ pub const File = struct { |
| 1218 | debug_aranges_sect.sh_offset = self.findFreeSpace(needed_size, 16); | 1218 | debug_aranges_sect.sh_offset = self.findFreeSpace(needed_size, 16); |
| 1219 | } | 1219 | } |
| 1220 | debug_aranges_sect.sh_size = needed_size; | 1220 | debug_aranges_sect.sh_size = needed_size; |
| 1221 | log.debug(.link, ".debug_aranges start=0x{x} end=0x{x}\n", .{ | 1221 | log.debug(".debug_aranges start=0x{x} end=0x{x}\n", .{ |
| 1222 | debug_aranges_sect.sh_offset, | 1222 | debug_aranges_sect.sh_offset, |
| 1223 | debug_aranges_sect.sh_offset + needed_size, | 1223 | debug_aranges_sect.sh_offset + needed_size, |
| 1224 | }); | 1224 | }); |
| ... | @@ -1386,7 +1386,7 @@ pub const File = struct { | ... | @@ -1386,7 +1386,7 @@ pub const File = struct { |
| 1386 | shstrtab_sect.sh_offset = self.findFreeSpace(needed_size, 1); | 1386 | shstrtab_sect.sh_offset = self.findFreeSpace(needed_size, 1); |
| 1387 | } | 1387 | } |
| 1388 | shstrtab_sect.sh_size = needed_size; | 1388 | shstrtab_sect.sh_size = needed_size; |
| 1389 | log.debug(.link, "writing shstrtab start=0x{x} end=0x{x}\n", .{ shstrtab_sect.sh_offset, shstrtab_sect.sh_offset + needed_size }); | 1389 | log.debug("writing shstrtab start=0x{x} end=0x{x}\n", .{ shstrtab_sect.sh_offset, shstrtab_sect.sh_offset + needed_size }); |
| 1390 | | 1390 | |
| 1391 | try self.base.file.?.pwriteAll(self.shstrtab.items, shstrtab_sect.sh_offset); | 1391 | try self.base.file.?.pwriteAll(self.shstrtab.items, shstrtab_sect.sh_offset); |
| 1392 | if (!self.shdr_table_dirty) { | 1392 | if (!self.shdr_table_dirty) { |
| ... | @@ -1407,7 +1407,7 @@ pub const File = struct { | ... | @@ -1407,7 +1407,7 @@ pub const File = struct { |
| 1407 | debug_strtab_sect.sh_offset = self.findFreeSpace(needed_size, 1); | 1407 | debug_strtab_sect.sh_offset = self.findFreeSpace(needed_size, 1); |
| 1408 | } | 1408 | } |
| 1409 | debug_strtab_sect.sh_size = needed_size; | 1409 | debug_strtab_sect.sh_size = needed_size; |
| 1410 | log.debug(.link, "debug_strtab start=0x{x} end=0x{x}\n", .{ debug_strtab_sect.sh_offset, debug_strtab_sect.sh_offset + needed_size }); | 1410 | log.debug("debug_strtab start=0x{x} end=0x{x}\n", .{ debug_strtab_sect.sh_offset, debug_strtab_sect.sh_offset + needed_size }); |
| 1411 | | 1411 | |
| 1412 | try self.base.file.?.pwriteAll(self.debug_strtab.items, debug_strtab_sect.sh_offset); | 1412 | try self.base.file.?.pwriteAll(self.debug_strtab.items, debug_strtab_sect.sh_offset); |
| 1413 | if (!self.shdr_table_dirty) { | 1413 | if (!self.shdr_table_dirty) { |
| ... | @@ -1441,7 +1441,7 @@ pub const File = struct { | ... | @@ -1441,7 +1441,7 @@ pub const File = struct { |
| 1441 | | 1441 | |
| 1442 | for (buf) |*shdr, i| { | 1442 | for (buf) |*shdr, i| { |
| 1443 | shdr.* = sectHeaderTo32(self.sections.items[i]); | 1443 | shdr.* = sectHeaderTo32(self.sections.items[i]); |
| 1444 | std.log.debug(.link, "writing section {}\n", .{shdr.*}); | 1444 | log.debug("writing section {}\n", .{shdr.*}); |
| 1445 | if (foreign_endian) { | 1445 | if (foreign_endian) { |
| 1446 | bswapAllFields(elf.Elf32_Shdr, shdr); | 1446 | bswapAllFields(elf.Elf32_Shdr, shdr); |
| 1447 | } | 1447 | } |
| ... | @@ -1454,7 +1454,7 @@ pub const File = struct { | ... | @@ -1454,7 +1454,7 @@ pub const File = struct { |
| 1454 | | 1454 | |
| 1455 | for (buf) |*shdr, i| { | 1455 | for (buf) |*shdr, i| { |
| 1456 | shdr.* = self.sections.items[i]; | 1456 | shdr.* = self.sections.items[i]; |
| 1457 | log.debug(.link, "writing section {}\n", .{shdr.*}); | 1457 | log.debug("writing section {}\n", .{shdr.*}); |
| 1458 | if (foreign_endian) { | 1458 | if (foreign_endian) { |
| 1459 | bswapAllFields(elf.Elf64_Shdr, shdr); | 1459 | bswapAllFields(elf.Elf64_Shdr, shdr); |
| 1460 | } | 1460 | } |
| ... | @@ -1465,10 +1465,10 @@ pub const File = struct { | ... | @@ -1465,10 +1465,10 @@ pub const File = struct { |
| 1465 | self.shdr_table_dirty = false; | 1465 | self.shdr_table_dirty = false; |
| 1466 | } | 1466 | } |
| 1467 | if (self.entry_addr == null and self.base.options.output_mode == .Exe) { | 1467 | if (self.entry_addr == null and self.base.options.output_mode == .Exe) { |
| 1468 | log.debug(.link, "flushing. no_entry_point_found = true\n", .{}); | 1468 | log.debug("flushing. no_entry_point_found = true\n", .{}); |
| 1469 | self.error_flags.no_entry_point_found = true; | 1469 | self.error_flags.no_entry_point_found = true; |
| 1470 | } else { | 1470 | } else { |
| 1471 | log.debug(.link, "flushing. no_entry_point_found = false\n", .{}); | 1471 | log.debug("flushing. no_entry_point_found = false\n", .{}); |
| 1472 | self.error_flags.no_entry_point_found = false; | 1472 | self.error_flags.no_entry_point_found = false; |
| 1473 | try self.writeElfHeader(); | 1473 | try self.writeElfHeader(); |
| 1474 | } | 1474 | } |
| ... | @@ -1797,10 +1797,10 @@ pub const File = struct { | ... | @@ -1797,10 +1797,10 @@ pub const File = struct { |
| 1797 | try self.offset_table.ensureCapacity(self.base.allocator, self.offset_table.items.len + 1); | 1797 | try self.offset_table.ensureCapacity(self.base.allocator, self.offset_table.items.len + 1); |
| 1798 | | 1798 | |
| 1799 | if (self.local_symbol_free_list.popOrNull()) |i| { | 1799 | if (self.local_symbol_free_list.popOrNull()) |i| { |
| 1800 | log.debug(.link, "reusing symbol index {} for {}\n", .{ i, decl.name }); | 1800 | log.debug("reusing symbol index {} for {}\n", .{ i, decl.name }); |
| 1801 | decl.link.elf.local_sym_index = i; | 1801 | decl.link.elf.local_sym_index = i; |
| 1802 | } else { | 1802 | } else { |
| 1803 | log.debug(.link, "allocating symbol index {} for {}\n", .{ self.local_symbols.items.len, decl.name }); | 1803 | log.debug("allocating symbol index {} for {}\n", .{ self.local_symbols.items.len, decl.name }); |
| 1804 | decl.link.elf.local_sym_index = @intCast(u32, self.local_symbols.items.len); | 1804 | decl.link.elf.local_sym_index = @intCast(u32, self.local_symbols.items.len); |
| 1805 | _ = self.local_symbols.addOneAssumeCapacity(); | 1805 | _ = self.local_symbols.addOneAssumeCapacity(); |
| 1806 | } | 1806 | } |
| ... | @@ -1993,11 +1993,11 @@ pub const File = struct { | ... | @@ -1993,11 +1993,11 @@ pub const File = struct { |
| 1993 | !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment); | 1993 | !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment); |
| 1994 | if (need_realloc) { | 1994 | if (need_realloc) { |
| 1995 | const vaddr = try self.growTextBlock(&decl.link.elf, code.len, required_alignment); | 1995 | const vaddr = try self.growTextBlock(&decl.link.elf, code.len, required_alignment); |
| 1996 | log.debug(.link, "growing {} from 0x{x} to 0x{x}\n", .{ decl.name, local_sym.st_value, vaddr }); | 1996 | log.debug("growing {} from 0x{x} to 0x{x}\n", .{ decl.name, local_sym.st_value, vaddr }); |
| 1997 | if (vaddr != local_sym.st_value) { | 1997 | if (vaddr != local_sym.st_value) { |
| 1998 | local_sym.st_value = vaddr; | 1998 | local_sym.st_value = vaddr; |
| 1999 | | 1999 | |
| 2000 | log.debug(.link, " (writing new offset table entry)\n", .{}); | 2000 | log.debug(" (writing new offset table entry)\n", .{}); |
| 2001 | self.offset_table.items[decl.link.elf.offset_table_index] = vaddr; | 2001 | self.offset_table.items[decl.link.elf.offset_table_index] = vaddr; |
| 2002 | try self.writeOffsetTableEntry(decl.link.elf.offset_table_index); | 2002 | try self.writeOffsetTableEntry(decl.link.elf.offset_table_index); |
| 2003 | } | 2003 | } |
| ... | @@ -2015,7 +2015,7 @@ pub const File = struct { | ... | @@ -2015,7 +2015,7 @@ pub const File = struct { |
| 2015 | const decl_name = mem.spanZ(decl.name); | 2015 | const decl_name = mem.spanZ(decl.name); |
| 2016 | const name_str_index = try self.makeString(decl_name); | 2016 | const name_str_index = try self.makeString(decl_name); |
| 2017 | const vaddr = try self.allocateTextBlock(&decl.link.elf, code.len, required_alignment); | 2017 | const vaddr = try self.allocateTextBlock(&decl.link.elf, code.len, required_alignment); |
| 2018 | log.debug(.link, "allocated text block for {} at 0x{x}\n", .{ decl_name, vaddr }); | 2018 | log.debug("allocated text block for {} at 0x{x}\n", .{ decl_name, vaddr }); |
| 2019 | errdefer self.freeTextBlock(&decl.link.elf); | 2019 | errdefer self.freeTextBlock(&decl.link.elf); |
| 2020 | | 2020 | |
| 2021 | local_sym.* = .{ | 2021 | local_sym.* = .{ |
| ... | @@ -2125,7 +2125,7 @@ pub const File = struct { | ... | @@ -2125,7 +2125,7 @@ pub const File = struct { |
| 2125 | if (needed_size > self.allocatedSize(debug_line_sect.sh_offset)) { | 2125 | if (needed_size > self.allocatedSize(debug_line_sect.sh_offset)) { |
| 2126 | const new_offset = self.findFreeSpace(needed_size, 1); | 2126 | const new_offset = self.findFreeSpace(needed_size, 1); |
| 2127 | const existing_size = last_src_fn.off; | 2127 | const existing_size = last_src_fn.off; |
| 2128 | log.debug(.link, "moving .debug_line section: {} bytes from 0x{x} to 0x{x}\n", .{ | 2128 | log.debug("moving .debug_line section: {} bytes from 0x{x} to 0x{x}\n", .{ |
| 2129 | existing_size, | 2129 | existing_size, |
| 2130 | debug_line_sect.sh_offset, | 2130 | debug_line_sect.sh_offset, |
| 2131 | new_offset, | 2131 | new_offset, |
| ... | @@ -2204,7 +2204,7 @@ pub const File = struct { | ... | @@ -2204,7 +2204,7 @@ pub const File = struct { |
| 2204 | try dbg_info_buffer.writer().print("{}\x00", .{ty}); | 2204 | try dbg_info_buffer.writer().print("{}\x00", .{ty}); |
| 2205 | }, | 2205 | }, |
| 2206 | else => { | 2206 | else => { |
| 2207 | log.err(.compiler, "TODO implement .debug_info for type '{}'", .{ty}); | 2207 | std.log.scoped(.compiler).err("TODO implement .debug_info for type '{}'", .{ty}); |
| 2208 | try dbg_info_buffer.append(abbrev_pad1); | 2208 | try dbg_info_buffer.append(abbrev_pad1); |
| 2209 | }, | 2209 | }, |
| 2210 | } | 2210 | } |
| ... | @@ -2276,7 +2276,7 @@ pub const File = struct { | ... | @@ -2276,7 +2276,7 @@ pub const File = struct { |
| 2276 | if (needed_size > self.allocatedSize(debug_info_sect.sh_offset)) { | 2276 | if (needed_size > self.allocatedSize(debug_info_sect.sh_offset)) { |
| 2277 | const new_offset = self.findFreeSpace(needed_size, 1); | 2277 | const new_offset = self.findFreeSpace(needed_size, 1); |
| 2278 | const existing_size = last_decl.dbg_info_off; | 2278 | const existing_size = last_decl.dbg_info_off; |
| 2279 | log.debug(.link, "moving .debug_info section: {} bytes from 0x{x} to 0x{x}\n", .{ | 2279 | log.debug("moving .debug_info section: {} bytes from 0x{x} to 0x{x}\n", .{ |
| 2280 | existing_size, | 2280 | existing_size, |
| 2281 | debug_info_sect.sh_offset, | 2281 | debug_info_sect.sh_offset, |
| 2282 | new_offset, | 2282 | new_offset, |