| ... | ... | @@ -839,58 +839,42 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { |
| 839 | 839 | math.maxInt(@TypeOf(actual_size)); |
| 840 | 840 | } |
| 841 | 841 | |
| 842 | | // fn detectAllocCollision(self: *Coff, start: u64, size: u64) ?u64 { |
| 843 | | // const headers_size = self.getSizeOfHeaders(); |
| 844 | | // if (start < headers_size) |
| 845 | | // return headers_size; |
| 846 | | |
| 847 | | // const end = start + padToIdeal(size); |
| 848 | | |
| 849 | | // for (self.sections.items(.header)) |header| { |
| 850 | | // const increased_size = padToIdeal(section.sh_size); |
| 851 | | // const test_end = section.sh_offset + increased_size; |
| 852 | | // if (end > section.sh_offset and start < test_end) { |
| 853 | | // return test_end; |
| 854 | | // } |
| 855 | | // } |
| 856 | | // for (self.program_headers.items) |program_header| { |
| 857 | | // const increased_size = padToIdeal(program_header.p_filesz); |
| 858 | | // const test_end = program_header.p_offset + increased_size; |
| 859 | | // if (end > program_header.p_offset and start < test_end) { |
| 860 | | // return test_end; |
| 861 | | // } |
| 862 | | // } |
| 863 | | // return null; |
| 864 | | // } |
| 865 | | |
| 866 | | // // pub fn allocatedSize(self: *Coff, start: u64) u64 { |
| 867 | | // // if (start == 0) |
| 868 | | // // return 0; |
| 869 | | // // var min_pos: u64 = std.math.maxInt(u64); |
| 870 | | // // if (self.shdr_table_offset) |off| { |
| 871 | | // // if (off > start and off < min_pos) min_pos = off; |
| 872 | | // // } |
| 873 | | // // if (self.phdr_table_offset) |off| { |
| 874 | | // // if (off > start and off < min_pos) min_pos = off; |
| 875 | | // // } |
| 876 | | // // for (self.sections.items) |section| { |
| 877 | | // // if (section.sh_offset <= start) continue; |
| 878 | | // // if (section.sh_offset < min_pos) min_pos = section.sh_offset; |
| 879 | | // // } |
| 880 | | // // for (self.program_headers.items) |program_header| { |
| 881 | | // // if (program_header.p_offset <= start) continue; |
| 882 | | // // if (program_header.p_offset < min_pos) min_pos = program_header.p_offset; |
| 883 | | // // } |
| 884 | | // // return min_pos - start; |
| 885 | | // // } |
| 886 | | |
| 887 | | // pub fn findFreeSpace(self: *Coff, object_size: u64, min_alignment: u32) u64 { |
| 888 | | // var start: u64 = 0; |
| 889 | | // while (self.detectAllocCollision(start, object_size)) |item_end| { |
| 890 | | // start = mem.alignForwardGeneric(u64, item_end, min_alignment); |
| 891 | | // } |
| 892 | | // return start; |
| 893 | | // } |
| 842 | fn detectAllocCollision(self: *Coff, start: u64, size: u64) ?u64 { |
| 843 | const headers_size = self.getSizeOfHeaders(); |
| 844 | if (start < headers_size) |
| 845 | return headers_size; |
| 846 | |
| 847 | const end = start + padToIdeal(size); |
| 848 | |
| 849 | for (self.sections.items(.header)) |header| { |
| 850 | const increased_size = padToIdeal(header.size_of_raw_data); |
| 851 | const test_end = header.pointer_to_raw_data + increased_size; |
| 852 | if (end > header.pointer_to_raw_data and start < test_end) { |
| 853 | return test_end; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | return null; |
| 858 | } |
| 859 | |
| 860 | pub fn allocatedSize(self: *Coff, start: u64) u64 { |
| 861 | if (start == 0) |
| 862 | return 0; |
| 863 | var min_pos: u64 = std.math.maxInt(u64); |
| 864 | for (self.sections.items(.header)) |header| { |
| 865 | if (header.pointer_to_raw_data <= start) continue; |
| 866 | if (header.pointer_to_raw_data < min_pos) min_pos = header.pointer_to_raw_data; |
| 867 | } |
| 868 | return min_pos - start; |
| 869 | } |
| 870 | |
| 871 | pub fn findFreeSpace(self: *Coff, object_size: u64, min_alignment: u32) u64 { |
| 872 | var start: u64 = 0; |
| 873 | while (self.detectAllocCollision(start, object_size)) |item_end| { |
| 874 | start = mem.alignForwardGeneric(u64, item_end, min_alignment); |
| 875 | } |
| 876 | return start; |
| 877 | } |
| 894 | 878 | |
| 895 | 879 | inline fn getSizeOfHeaders(self: Coff) usize { |
| 896 | 880 | const msdos_hdr_size = msdos_stub.len + 8; |