| ... | ... | @@ -1625,12 +1625,10 @@ fn writeBaseRelocations(self: *Coff) !void { |
| 1625 | 1625 | const needed_size = @intCast(u32, buffer.items.len); |
| 1626 | 1626 | if (needed_size > sect_capacity) { |
| 1627 | 1627 | const new_offset = self.findFreeSpace(needed_size, default_file_alignment); |
| 1628 | | log.debug("writing {s} at 0x{x} to 0x{x} (0x{x} - 0x{x})", .{ |
| 1628 | log.debug("moving {s} from 0x{x} to 0x{x}", .{ |
| 1629 | 1629 | self.getSectionName(header), |
| 1630 | 1630 | header.pointer_to_raw_data, |
| 1631 | | header.pointer_to_raw_data + needed_size, |
| 1632 | 1631 | new_offset, |
| 1633 | | new_offset + needed_size, |
| 1634 | 1632 | }); |
| 1635 | 1633 | header.pointer_to_raw_data = new_offset; |
| 1636 | 1634 | |
| ... | ... | @@ -1656,11 +1654,11 @@ fn writeImportTable(self: *Coff) !void { |
| 1656 | 1654 | |
| 1657 | 1655 | const gpa = self.base.allocator; |
| 1658 | 1656 | |
| 1659 | | const section = self.sections.get(self.idata_section_index.?); |
| 1660 | | const last_atom_index = section.last_atom_index orelse return; |
| 1657 | const last_atom_index = self.sections.items(.last_atom_index)[self.idata_section_index.?] orelse return; |
| 1658 | const header = &self.sections.items(.header)[self.idata_section_index.?]; |
| 1661 | 1659 | const last_atom = self.getAtom(last_atom_index); |
| 1662 | 1660 | |
| 1663 | | const iat_rva = section.header.virtual_address; |
| 1661 | const iat_rva = header.virtual_address; |
| 1664 | 1662 | const iat_size = last_atom.getSymbol(self).value + last_atom.size * 2 - iat_rva; // account for sentinel zero pointer |
| 1665 | 1663 | |
| 1666 | 1664 | const dll_name = "KERNEL32.dll"; |
| ... | ... | @@ -1696,9 +1694,18 @@ fn writeImportTable(self: *Coff) !void { |
| 1696 | 1694 | try lookup_table.append(.{ .name_table_rva = 0 }); // the sentinel |
| 1697 | 1695 | |
| 1698 | 1696 | const dir_entry_size = @sizeOf(coff.ImportDirectoryEntry) + lookup_table.items.len * @sizeOf(coff.ImportLookupEntry64.ByName) + names_table.items.len + dll_name.len + 1; |
| 1699 | | const needed_size = iat_size + dir_entry_size + @sizeOf(coff.ImportDirectoryEntry); |
| 1700 | | const sect_capacity = self.allocatedSize(section.header.pointer_to_raw_data); |
| 1701 | | assert(needed_size < sect_capacity); // TODO: implement expanding .idata section |
| 1697 | const sect_capacity = self.allocatedSize(header.pointer_to_raw_data); |
| 1698 | const needed_size = @intCast(u32, iat_size + dir_entry_size + @sizeOf(coff.ImportDirectoryEntry)); |
| 1699 | if (needed_size > sect_capacity) { |
| 1700 | const new_offset = self.findFreeSpace(needed_size, default_file_alignment); |
| 1701 | log.debug("moving .idata from 0x{x} to 0x{x}", .{ header.pointer_to_raw_data, new_offset }); |
| 1702 | header.pointer_to_raw_data = new_offset; |
| 1703 | |
| 1704 | const sect_vm_capacity = self.allocatedVirtualSize(header.virtual_address); |
| 1705 | if (needed_size > sect_vm_capacity) { |
| 1706 | try self.growSectionVM(self.idata_section_index.?, needed_size); |
| 1707 | } |
| 1708 | } |
| 1702 | 1709 | |
| 1703 | 1710 | // Fixup offsets |
| 1704 | 1711 | const base_rva = iat_rva + iat_size; |
| ... | ... | @@ -1719,10 +1726,10 @@ fn writeImportTable(self: *Coff) !void { |
| 1719 | 1726 | buffer.appendSliceAssumeCapacity(dll_name); |
| 1720 | 1727 | buffer.appendAssumeCapacity(0); |
| 1721 | 1728 | |
| 1722 | | try self.base.file.?.pwriteAll(buffer.items, section.header.pointer_to_raw_data + iat_size); |
| 1729 | try self.base.file.?.pwriteAll(buffer.items, header.pointer_to_raw_data + iat_size); |
| 1723 | 1730 | // Override the IAT atoms |
| 1724 | 1731 | // TODO: we should rewrite only dirtied atoms, but that's for way later |
| 1725 | | try self.base.file.?.pwriteAll(mem.sliceAsBytes(lookup_table.items), section.header.pointer_to_raw_data); |
| 1732 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(lookup_table.items), header.pointer_to_raw_data); |
| 1726 | 1733 | |
| 1727 | 1734 | self.data_directories[@enumToInt(coff.DirectoryEntry.IMPORT)] = .{ |
| 1728 | 1735 | .virtual_address = iat_rva + iat_size, |