From 222e23c6786e0ac307524c6c91b41782111af82a Mon Sep 17 00:00:00 2001 From: Noam Preil Date: Fri, 21 Aug 2020 07:25:21 -0400 Subject: [PATCH] Linker: make defaults read-only --- src-self-hosted/link.zig | 2 +- src-self-hosted/link/Elf.zig | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src-self-hosted/link.zig b/src-self-hosted/link.zig index e8604038c2..59b01dc762 100644 --- a/src-self-hosted/link.zig +++ b/src-self-hosted/link.zig @@ -23,7 +23,7 @@ pub const Options = struct { /// Used for calculating how much space to reserve for executable program code in case /// the binary file deos not already have such a section. program_code_size_hint: u64 = 256 * 1024, - default_entry_addr: u64 = 0x8000000, + entry_addr: ?u64 = null, }; pub const File = struct { diff --git a/src-self-hosted/link/Elf.zig b/src-self-hosted/link/Elf.zig index 34f2243718..53798053c2 100644 --- a/src-self-hosted/link/Elf.zig +++ b/src-self-hosted/link/Elf.zig @@ -286,10 +286,6 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Elf }; errdefer self.deinit(); - if (options.target.cpu.arch == .spu_2) { - self.base.options.default_entry_addr = 0; - } - // Index 0 is always a null symbol. try self.local_symbols.append(allocator, .{ .st_name = 0, @@ -466,12 +462,13 @@ pub fn populateMissingMetadata(self: *Elf) !void { const p_align = 0x1000; const off = self.findFreeSpace(file_size, p_align); log.debug("found PT_LOAD free space 0x{x} to 0x{x}\n", .{ off, off + file_size }); + const entry_addr: u64 = self.entry_addr orelse if (self.base.options.target.cpu.arch == .spu_2) @as(u64, 0) else default_entry_addr; try self.program_headers.append(self.base.allocator, .{ .p_type = elf.PT_LOAD, .p_offset = off, .p_filesz = file_size, - .p_vaddr = self.base.options.default_entry_addr, - .p_paddr = self.base.options.default_entry_addr, + .p_vaddr = entry_addr, + .p_paddr = entry_addr, .p_memsz = file_size, .p_align = p_align, .p_flags = elf.PF_X | elf.PF_R, @@ -490,13 +487,13 @@ pub fn populateMissingMetadata(self: *Elf) !void { // TODO instead of hard coding the vaddr, make a function to find a vaddr to put things at. // we'll need to re-use that function anyway, in case the GOT grows and overlaps something // else in virtual memory. - const default_got_addr = if (self.base.options.target.cpu.arch.ptrBitWidth() == 16) @as(u32, 0x8000) else 0x4000000; + const got_addr = if (self.base.options.target.cpu.arch.ptrBitWidth() == 16) @as(u32, 0x8000) else 0x4000000; try self.program_headers.append(self.base.allocator, .{ .p_type = elf.PT_LOAD, .p_offset = off, .p_filesz = file_size, - .p_vaddr = default_got_addr, - .p_paddr = default_got_addr, + .p_vaddr = got_addr, + .p_paddr = got_addr, .p_memsz = file_size, .p_align = p_align, .p_flags = elf.PF_R,