| ... | @@ -421,11 +421,14 @@ fn allocateSection(self: *Coff, name: []const u8, size: u32, flags: coff.Section | ... | @@ -421,11 +421,14 @@ fn allocateSection(self: *Coff, name: []const u8, size: u32, flags: coff.Section |
| 421 | const index = @intCast(u16, self.sections.slice().len); | 421 | const index = @intCast(u16, self.sections.slice().len); |
| 422 | const off = self.findFreeSpace(size, default_file_alignment); | 422 | const off = self.findFreeSpace(size, default_file_alignment); |
| 423 | // Memory is always allocated in sequence | 423 | // Memory is always allocated in sequence |
| | 424 | // TODO: investigate if we can allocate .text last; this way it would never need to grow in memory! |
| 424 | const vaddr = blk: { | 425 | const vaddr = blk: { |
| 425 | if (index == 0) break :blk self.page_size; | 426 | if (index == 0) break :blk self.page_size; |
| 426 | const prev_header = self.sections.items(.header)[index - 1]; | 427 | const prev_header = self.sections.items(.header)[index - 1]; |
| 427 | break :blk mem.alignForwardGeneric(u32, prev_header.virtual_address + prev_header.virtual_size, self.page_size); | 428 | break :blk mem.alignForwardGeneric(u32, prev_header.virtual_address + prev_header.virtual_size, self.page_size); |
| 428 | }; | 429 | }; |
| | 430 | // We commit more memory than needed upfront so that we don't have to reallocate too soon. |
| | 431 | const memsz = mem.alignForwardGeneric(u32, size, self.page_size) * 100; |
| 429 | log.debug("found {s} free space 0x{x} to 0x{x} (0x{x} - 0x{x})", .{ | 432 | log.debug("found {s} free space 0x{x} to 0x{x} (0x{x} - 0x{x})", .{ |
| 430 | name, | 433 | name, |
| 431 | off, | 434 | off, |
| ... | @@ -435,7 +438,7 @@ fn allocateSection(self: *Coff, name: []const u8, size: u32, flags: coff.Section | ... | @@ -435,7 +438,7 @@ fn allocateSection(self: *Coff, name: []const u8, size: u32, flags: coff.Section |
| 435 | }); | 438 | }); |
| 436 | var header = coff.SectionHeader{ | 439 | var header = coff.SectionHeader{ |
| 437 | .name = undefined, | 440 | .name = undefined, |
| 438 | .virtual_size = size, | 441 | .virtual_size = memsz, |
| 439 | .virtual_address = vaddr, | 442 | .virtual_address = vaddr, |
| 440 | .size_of_raw_data = size, | 443 | .size_of_raw_data = size, |
| 441 | .pointer_to_raw_data = off, | 444 | .pointer_to_raw_data = off, |
| ... | @@ -456,6 +459,7 @@ fn growSectionVM(self: *Coff, sect_id: u32, needed_size: u32) !void { | ... | @@ -456,6 +459,7 @@ fn growSectionVM(self: *Coff, sect_id: u32, needed_size: u32) !void { |
| 456 | const old_aligned_end = header.virtual_address + mem.alignForwardGeneric(u32, header.virtual_size, self.page_size); | 459 | const old_aligned_end = header.virtual_address + mem.alignForwardGeneric(u32, header.virtual_size, self.page_size); |
| 457 | const new_aligned_end = header.virtual_address + mem.alignForwardGeneric(u32, increased_size, self.page_size); | 460 | const new_aligned_end = header.virtual_address + mem.alignForwardGeneric(u32, increased_size, self.page_size); |
| 458 | const diff = new_aligned_end - old_aligned_end; | 461 | const diff = new_aligned_end - old_aligned_end; |
| | 462 | log.debug("growing {s} in virtual memory by {x}", .{ self.getSectionName(header), diff }); |
| 459 | | 463 | |
| 460 | // TODO: enforce order by increasing VM addresses in self.sections container. | 464 | // TODO: enforce order by increasing VM addresses in self.sections container. |
| 461 | // This is required by the loader anyhow as far as I can tell. | 465 | // This is required by the loader anyhow as far as I can tell. |