@@ -22,10 +22,10 @@ const minimum_text_block_size = 64 * allocation_padding;
22
22
23
23
const section_alignment = 4096 ;
24
24
const file_alignment = 512 ;
25
- const image_base = 0x400_000 ;
25
+ const default_image_base = 0x400_000 ;
26
26
const section_table_size = 2 * 40 ;
27
27
comptime {
28
- assert (mem .isAligned (image_base , section_alignment ));
28
+ assert (mem .isAligned (default_image_base , section_alignment ));
29
29
}
30
30
31
31
pub const base_tag : link.File.Tag = .coff ;
@@ -55,7 +55,7 @@ offset_table: std.ArrayListUnmanaged(u64) = .{},
55
55
/// Free list of offset table indices
56
56
offset_table_free_list : std .ArrayListUnmanaged (u32 ) = .{},
57
57
58
- /// Virtual address of the entry point procedure relative to `image_base`
58
+ /// Virtual address of the entry point procedure relative to image base.
59
59
entry_addr : ? u32 = null ,
60
60
61
61
/// Absolute virtual address of the text section when the executable is loaded in memory.
@@ -183,14 +183,14 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
183
183
184
184
self .section_data_offset = mem .alignForwardGeneric (u32 , self .section_table_offset + section_table_size , file_alignment );
185
185
const section_data_relative_virtual_address = mem .alignForwardGeneric (u32 , self .section_table_offset + section_table_size , section_alignment );
186
- self .offset_table_virtual_address = image_base + section_data_relative_virtual_address ;
186
+ self .offset_table_virtual_address = default_image_base + section_data_relative_virtual_address ;
187
187
self .offset_table_size = default_offset_table_size ;
188
188
self .section_table_offset = section_table_offset ;
189
- self .text_section_virtual_address = image_base + section_data_relative_virtual_address + section_alignment ;
189
+ self .text_section_virtual_address = default_image_base + section_data_relative_virtual_address + section_alignment ;
190
190
self .text_section_size = default_size_of_code ;
191
191
192
192
// Size of file when loaded in memory
193
- const size_of_image = mem .alignForwardGeneric (u32 , self .text_section_virtual_address - image_base + default_size_of_code , section_alignment );
193
+ const size_of_image = mem .alignForwardGeneric (u32 , self .text_section_virtual_address - default_image_base + default_size_of_code , section_alignment );
194
194
195
195
mem .writeIntLittle (u16 , hdr_data [index .. ][0.. 2], optional_header_size );
196
196
index += 2 ;
@@ -234,11 +234,11 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
234
234
index += 4 ;
235
235
236
236
// Image base address
237
- mem .writeIntLittle (u32 , hdr_data [index .. ][0.. 4], image_base );
237
+ mem .writeIntLittle (u32 , hdr_data [index .. ][0.. 4], default_image_base );
238
238
index += 4 ;
239
239
} else {
240
240
// Image base address
241
- mem .writeIntLittle (u64 , hdr_data [index .. ][0.. 8], image_base );
241
+ mem .writeIntLittle (u64 , hdr_data [index .. ][0.. 8], default_image_base );
242
242
index += 8 ;
243
243
}
244
244
@@ -328,7 +328,7 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
328
328
mem .writeIntLittle (u32 , hdr_data [index .. ][0.. 4], default_offset_table_size );
329
329
index += 4 ;
330
330
// Virtual address (u32)
331
- mem .writeIntLittle (u32 , hdr_data [index .. ][0.. 4], self .offset_table_virtual_address - image_base );
331
+ mem .writeIntLittle (u32 , hdr_data [index .. ][0.. 4], self .offset_table_virtual_address - default_image_base );
332
332
index += 4 ;
333
333
} else {
334
334
mem .set (u8 , hdr_data [index .. ][0.. 8], 0 );
@@ -354,7 +354,7 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
354
354
mem .writeIntLittle (u32 , hdr_data [index .. ][0.. 4], default_size_of_code );
355
355
index += 4 ;
356
356
// Virtual address (u32)
357
- mem .writeIntLittle (u32 , hdr_data [index .. ][0.. 4], self .text_section_virtual_address - image_base );
357
+ mem .writeIntLittle (u32 , hdr_data [index .. ][0.. 4], self .text_section_virtual_address - default_image_base );
358
358
index += 4 ;
359
359
} else {
360
360
mem .set (u8 , hdr_data [index .. ][0.. 8], 0 );
@@ -601,7 +601,7 @@ fn writeOffsetTableEntry(self: *Coff, index: usize) !void {
601
601
602
602
// Write .text new virtual address
603
603
self .text_section_virtual_address = self .text_section_virtual_address + va_offset ;
604
- mem .writeIntLittle (u32 , buf [0.. 4], self .text_section_virtual_address - image_base );
604
+ mem .writeIntLittle (u32 , buf [0.. 4], self .text_section_virtual_address - default_image_base );
605
605
try self .base .file .? .pwriteAll (buf [0.. 4], self .section_table_offset + 40 + 12 );
606
606
607
607
// Fix the VAs in the offset table
@@ -716,7 +716,7 @@ pub fn updateDeclExports(self: *Coff, module: *Module, decl: *const Module.Decl,
716
716
}
717
717
}
718
718
if (mem .eql (u8 , exp .options .name , "_start" )) {
719
- self .entry_addr = decl .link .coff .getVAddr (self .* ) - image_base ;
719
+ self .entry_addr = decl .link .coff .getVAddr (self .* ) - default_image_base ;
720
720
} else {
721
721
try module .failed_exports .ensureCapacity (module .gpa , module .failed_exports .items ().len + 1 );
722
722
module .failed_exports .putAssumeCapacityNoClobber (
@@ -754,7 +754,7 @@ pub fn flushModule(self: *Coff, comp: *Compilation) !void {
754
754
}
755
755
756
756
if (self .base .options .output_mode == .Exe and self .size_of_image_dirty ) {
757
- const new_size_of_image = mem .alignForwardGeneric (u32 , self .text_section_virtual_address - image_base + self .text_section_size , section_alignment );
757
+ const new_size_of_image = mem .alignForwardGeneric (u32 , self .text_section_virtual_address - default_image_base + self .text_section_size , section_alignment );
758
758
var buf : [4 ]u8 = undefined ;
759
759
mem .writeIntLittle (u32 , & buf , new_size_of_image );
760
760
try self .base .file .? .pwriteAll (& buf , self .optional_header_offset + 56 );
@@ -832,6 +832,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
832
832
}
833
833
try man .addOptionalFile (module_obj_path );
834
834
man .hash .addOptional (self .base .options .stack_size_override );
835
+ man .hash .addOptional (self .base .options .image_base_override );
835
836
man .hash .addListOfBytes (self .base .options .extra_lld_args );
836
837
man .hash .addListOfBytes (self .base .options .lib_dirs );
837
838
man .hash .add (self .base .options .is_compiler_rt_or_libc );
@@ -914,6 +915,9 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
914
915
const stack_size = self .base .options .stack_size_override orelse 16777216 ;
915
916
try argv .append (try allocPrint (arena , "-STACK:{d}" , .{stack_size }));
916
917
}
918
+ if (self .base .options .image_base_override ) | image_base | {
919
+ try argv .append (try std .fmt .allocPrint (arena , "-BASE:{d}" , .{image_base }));
920
+ }
917
921
918
922
if (target .cpu .arch == .i386 ) {
919
923
try argv .append ("-MACHINE:X86" );
0 commit comments