| ... | ... | @@ -1451,18 +1451,10 @@ fn getStringGeneric(opt_str: ?[]const u8, offset: u64) ![:0]const u8 { |
| 1451 | 1451 | |
| 1452 | 1452 | // MLUGG TODO: i am dubious of this whole thing being here atp. look closely and see if it depends on being the self process |
| 1453 | 1453 | pub const ElfModule = struct { |
| 1454 | | unwind: Dwarf.Unwind, |
| 1455 | 1454 | dwarf: Dwarf, |
| 1456 | | mapped_memory: ?[]align(std.heap.page_size_min) const u8, |
| 1455 | mapped_memory: []align(std.heap.page_size_min) const u8, |
| 1457 | 1456 | external_mapped_memory: ?[]align(std.heap.page_size_min) const u8, |
| 1458 | 1457 | |
| 1459 | | pub const init: ElfModule = .{ |
| 1460 | | .unwind = .init, |
| 1461 | | .dwarf = .{}, |
| 1462 | | .mapped_memory = null, |
| 1463 | | .external_mapped_memory = null, |
| 1464 | | }; |
| 1465 | | |
| 1466 | 1458 | pub fn deinit(self: *@This(), allocator: Allocator) void { |
| 1467 | 1459 | self.dwarf.deinit(allocator); |
| 1468 | 1460 | std.posix.munmap(self.mapped_memory); |
| ... | ... | @@ -1476,12 +1468,6 @@ pub const ElfModule = struct { |
| 1476 | 1468 | return self.dwarf.getSymbol(allocator, endian, vaddr); |
| 1477 | 1469 | } |
| 1478 | 1470 | |
| 1479 | | pub fn getDwarfUnwindForAddress(self: *@This(), allocator: Allocator, address: usize) !?*Dwarf.Unwind { |
| 1480 | | _ = allocator; |
| 1481 | | _ = address; |
| 1482 | | return &self.unwind; |
| 1483 | | } |
| 1484 | | |
| 1485 | 1471 | pub const LoadError = error{ |
| 1486 | 1472 | InvalidDebugInfo, |
| 1487 | 1473 | MissingDebugInfo, |
| ... | ... | @@ -1506,10 +1492,7 @@ pub const ElfModule = struct { |
| 1506 | 1492 | /// If the required sections aren't present but a reference to external debug |
| 1507 | 1493 | /// info is, then this this function will recurse to attempt to load the debug |
| 1508 | 1494 | /// sections from an external file. |
| 1509 | | /// |
| 1510 | | /// MLUGG TODO: this should *return* a thing |
| 1511 | 1495 | pub fn load( |
| 1512 | | em: *ElfModule, |
| 1513 | 1496 | gpa: Allocator, |
| 1514 | 1497 | mapped_mem: []align(std.heap.page_size_min) const u8, |
| 1515 | 1498 | build_id: ?[]const u8, |
| ... | ... | @@ -1517,9 +1500,7 @@ pub const ElfModule = struct { |
| 1517 | 1500 | parent_sections: ?*Dwarf.SectionArray, |
| 1518 | 1501 | parent_mapped_mem: ?[]align(std.heap.page_size_min) const u8, |
| 1519 | 1502 | elf_filename: ?[]const u8, |
| 1520 | | ) LoadError!void { |
| 1521 | | assert(em.mapped_memory == null); |
| 1522 | | |
| 1503 | ) LoadError!ElfModule { |
| 1523 | 1504 | if (expected_crc) |crc| if (crc != std.hash.crc.Crc32.hash(mapped_mem)) return error.InvalidDebugInfo; |
| 1524 | 1505 | |
| 1525 | 1506 | const hdr: *const elf.Ehdr = @ptrCast(&mapped_mem[0]); |
| ... | ... | @@ -1657,7 +1638,7 @@ pub const ElfModule = struct { |
| 1657 | 1638 | .sub_path = filename, |
| 1658 | 1639 | }; |
| 1659 | 1640 | |
| 1660 | | return em.loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem) catch break :blk; |
| 1641 | return loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem) catch break :blk; |
| 1661 | 1642 | } |
| 1662 | 1643 | |
| 1663 | 1644 | const global_debug_directories = [_][]const u8{ |
| ... | ... | @@ -1685,7 +1666,7 @@ pub const ElfModule = struct { |
| 1685 | 1666 | }; |
| 1686 | 1667 | defer gpa.free(path.sub_path); |
| 1687 | 1668 | |
| 1688 | | return em.loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem) catch continue; |
| 1669 | return loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem) catch continue; |
| 1689 | 1670 | } |
| 1690 | 1671 | } |
| 1691 | 1672 | |
| ... | ... | @@ -1701,7 +1682,7 @@ pub const ElfModule = struct { |
| 1701 | 1682 | defer exe_dir.close(); |
| 1702 | 1683 | |
| 1703 | 1684 | // <exe_dir>/<gnu_debuglink> |
| 1704 | | if (em.loadPath( |
| 1685 | if (loadPath( |
| 1705 | 1686 | gpa, |
| 1706 | 1687 | .{ |
| 1707 | 1688 | .root_dir = .{ .path = null, .handle = exe_dir }, |
| ... | ... | @@ -1711,9 +1692,8 @@ pub const ElfModule = struct { |
| 1711 | 1692 | separate_debug_crc, |
| 1712 | 1693 | &sections, |
| 1713 | 1694 | mapped_mem, |
| 1714 | | )) |v| { |
| 1715 | | v; |
| 1716 | | return; |
| 1695 | )) |em| { |
| 1696 | return em; |
| 1717 | 1697 | } else |_| {} |
| 1718 | 1698 | |
| 1719 | 1699 | // <exe_dir>/.debug/<gnu_debuglink> |
| ... | ... | @@ -1723,7 +1703,9 @@ pub const ElfModule = struct { |
| 1723 | 1703 | }; |
| 1724 | 1704 | defer gpa.free(path.sub_path); |
| 1725 | 1705 | |
| 1726 | | if (em.loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem)) |debug_info| return debug_info else |_| {} |
| 1706 | if (loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem)) |em| { |
| 1707 | return em; |
| 1708 | } else |_| {} |
| 1727 | 1709 | } |
| 1728 | 1710 | |
| 1729 | 1711 | var cwd_buf: [std.fs.max_path_bytes]u8 = undefined; |
| ... | ... | @@ -1736,28 +1718,32 @@ pub const ElfModule = struct { |
| 1736 | 1718 | .sub_path = try std.fs.path.join(gpa, &.{ global_directory, cwd_path, separate_filename }), |
| 1737 | 1719 | }; |
| 1738 | 1720 | defer gpa.free(path.sub_path); |
| 1739 | | if (em.loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem)) |debug_info| return debug_info else |_| {} |
| 1721 | if (loadPath(gpa, path, null, separate_debug_crc, &sections, mapped_mem)) |em| { |
| 1722 | return em; |
| 1723 | } else |_| {} |
| 1740 | 1724 | } |
| 1741 | 1725 | } |
| 1742 | 1726 | |
| 1743 | 1727 | return error.MissingDebugInfo; |
| 1744 | 1728 | } |
| 1745 | 1729 | |
| 1746 | | em.mapped_memory = parent_mapped_mem orelse mapped_mem; |
| 1747 | | em.external_mapped_memory = if (parent_mapped_mem != null) mapped_mem else null; |
| 1748 | | em.dwarf.sections = sections; |
| 1749 | | try em.dwarf.open(gpa, endian); |
| 1730 | var dwarf: Dwarf = .{ .sections = sections }; |
| 1731 | try dwarf.open(gpa, endian); |
| 1732 | return .{ |
| 1733 | .mapped_memory = parent_mapped_mem orelse mapped_mem, |
| 1734 | .external_mapped_memory = if (parent_mapped_mem != null) mapped_mem else null, |
| 1735 | .dwarf = dwarf, |
| 1736 | }; |
| 1750 | 1737 | } |
| 1751 | 1738 | |
| 1752 | 1739 | pub fn loadPath( |
| 1753 | | em: *ElfModule, |
| 1754 | 1740 | gpa: Allocator, |
| 1755 | 1741 | elf_file_path: Path, |
| 1756 | 1742 | build_id: ?[]const u8, |
| 1757 | 1743 | expected_crc: ?u32, |
| 1758 | 1744 | parent_sections: *Dwarf.SectionArray, |
| 1759 | 1745 | parent_mapped_mem: ?[]align(std.heap.page_size_min) const u8, |
| 1760 | | ) LoadError!void { |
| 1746 | ) LoadError!ElfModule { |
| 1761 | 1747 | const elf_file = elf_file_path.root_dir.handle.openFile(elf_file_path.sub_path, .{}) catch |err| switch (err) { |
| 1762 | 1748 | error.FileNotFound => return missing(), |
| 1763 | 1749 | else => return err, |
| ... | ... | @@ -1780,7 +1766,7 @@ pub const ElfModule = struct { |
| 1780 | 1766 | }; |
| 1781 | 1767 | errdefer std.posix.munmap(mapped_mem); |
| 1782 | 1768 | |
| 1783 | | return em.load( |
| 1769 | return load( |
| 1784 | 1770 | gpa, |
| 1785 | 1771 | mapped_mem, |
| 1786 | 1772 | build_id, |