authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-19 15:57:58+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-19 15:57:58+02:00
log3768c9537111b85c2ee99470fbb1615bc5233516
tree5483851d8c8d88bad776842fc5efc5ee1bfdfef2
parentae2cd5fe263e9d8ddd5719b4795f9cd39b5c0324

elf: actually check for dynamic executables


3 files changed, 19 insertions(+), 11 deletions(-)

src/link/Elf.zig+4-2
...@@ -2956,7 +2956,7 @@ fn writeHeader(self: *Elf) !void {...@@ -2956,7 +2956,7 @@ fn writeHeader(self: *Elf) !void {
2956 assert(index == 16);2956 assert(index == 16);
29572957
2958 const elf_type: elf.ET = switch (self.base.options.effectiveOutputMode()) {2958 const elf_type: elf.ET = switch (self.base.options.effectiveOutputMode()) {
2959 .Exe => if (self.base.options.pic) .DYN else .EXEC,2959 .Exe => if (self.base.options.pie) .DYN else .EXEC,
2960 .Obj => .REL,2960 .Obj => .REL,
2961 .Lib => switch (self.base.options.link_mode) {2961 .Lib => switch (self.base.options.link_mode) {
2962 .Static => @as(elf.ET, .REL),2962 .Static => @as(elf.ET, .REL),
...@@ -4869,6 +4869,7 @@ fn allocateSpecialPhdrs(self: *Elf) void {...@@ -4869,6 +4869,7 @@ fn allocateSpecialPhdrs(self: *Elf) void {
4869 phdr.p_align = shdr.sh_addralign;4869 phdr.p_align = shdr.sh_addralign;
4870 phdr.p_offset = shdr.sh_offset;4870 phdr.p_offset = shdr.sh_offset;
4871 phdr.p_vaddr = shdr.sh_addr;4871 phdr.p_vaddr = shdr.sh_addr;
4872 phdr.p_paddr = shdr.sh_addr;
4872 phdr.p_filesz = shdr.sh_size;4873 phdr.p_filesz = shdr.sh_size;
4873 phdr.p_memsz = shdr.sh_size;4874 phdr.p_memsz = shdr.sh_size;
4874 }4875 }
...@@ -5581,7 +5582,8 @@ const CsuObjects = struct {...@@ -5581,7 +5582,8 @@ const CsuObjects = struct {
5581};5582};
55825583
5583pub fn calcImageBase(self: Elf) u64 {5584pub fn calcImageBase(self: Elf) u64 {
5584 if (self.base.options.pic) return 0; // TODO flag an error if PIC and image_base_override5585 if (self.isDynLib()) return 0;
5586 if (self.isExe() and self.base.options.pie) return 0;
5585 return self.base.options.image_base_override orelse switch (self.ptr_width) {5587 return self.base.options.image_base_override orelse switch (self.ptr_width) {
5586 .p32 => 0x1000,5588 .p32 => 0x1000,
5587 .p64 => 0x1000000,5589 .p64 => 0x1000000,
src/link/Elf/Atom.zig+1-1
...@@ -600,7 +600,7 @@ fn dynAbsRelocAction(symbol: *const Symbol, elf_file: *Elf) RelocAction {...@@ -600,7 +600,7 @@ fn dynAbsRelocAction(symbol: *const Symbol, elf_file: *Elf) RelocAction {
600}600}
601601
602fn outputType(elf_file: *Elf) u2 {602fn outputType(elf_file: *Elf) u2 {
603 return switch (elf_file.base.options.output_mode) {603 return switch (elf_file.base.options.effectiveOutputMode()) {
604 .Obj => unreachable,604 .Obj => unreachable,
605 .Lib => 0,605 .Lib => 0,
606 .Exe => if (elf_file.base.options.pie) 1 else 2,606 .Exe => if (elf_file.base.options.pie) 1 else 2,
src/link/Elf/synthetic_sections.zig+14-8
...@@ -54,7 +54,7 @@ pub const DynamicSection = struct {...@@ -54,7 +54,7 @@ pub const DynamicSection = struct {
54 if (elf_file.base.options.z_now) {54 if (elf_file.base.options.z_now) {
55 flags_1 |= elf.DF_1_NOW;55 flags_1 |= elf.DF_1_NOW;
56 }56 }
57 if (elf_file.base.options.pie) {57 if (elf_file.isExe() and elf_file.base.options.pie) {
58 flags_1 |= elf.DF_1_PIE;58 flags_1 |= elf.DF_1_PIE;
59 }59 }
60 // if (elf_file.base.options.z_nodlopen) {60 // if (elf_file.base.options.z_nodlopen) {
...@@ -226,7 +226,7 @@ pub const ZigGotSection = struct {...@@ -226,7 +226,7 @@ pub const ZigGotSection = struct {
226 flags: Flags = .{},226 flags: Flags = .{},
227227
228 const Flags = packed struct {228 const Flags = packed struct {
229 needs_rela: bool = false, // TODO in prep for PIC/PIE and base relocations229 needs_rela: bool = false,
230 dirty: bool = false,230 dirty: bool = false,
231 };231 };
232232
...@@ -251,7 +251,7 @@ pub const ZigGotSection = struct {...@@ -251,7 +251,7 @@ pub const ZigGotSection = struct {
251 entry.* = sym_index;251 entry.* = sym_index;
252 const symbol = elf_file.symbol(sym_index);252 const symbol = elf_file.symbol(sym_index);
253 symbol.flags.has_zig_got = true;253 symbol.flags.has_zig_got = true;
254 if (elf_file.base.options.pic) {254 if (elf_file.isDynLib() or (elf_file.isExe() and elf_file.base.options.pie)) {
255 zig_got.flags.needs_rela = true;255 zig_got.flags.needs_rela = true;
256 }256 }
257 if (symbol.extra(elf_file)) |extra| {257 if (symbol.extra(elf_file)) |extra| {
...@@ -491,7 +491,7 @@ pub const GotSection = struct {...@@ -491,7 +491,7 @@ pub const GotSection = struct {
491 const symbol = elf_file.symbol(sym_index);491 const symbol = elf_file.symbol(sym_index);
492 symbol.flags.has_got = true;492 symbol.flags.has_got = true;
493 if (symbol.flags.import or symbol.isIFunc(elf_file) or493 if (symbol.flags.import or symbol.isIFunc(elf_file) or
494 (elf_file.base.options.pic and !symbol.isAbs(elf_file)))494 ((elf_file.isDynLib() or (elf_file.isExe() and elf_file.base.options.pie)) and !symbol.isAbs(elf_file)))
495 {495 {
496 got.flags.needs_rela = true;496 got.flags.needs_rela = true;
497 }497 }
...@@ -582,8 +582,11 @@ pub const GotSection = struct {...@@ -582,8 +582,11 @@ pub const GotSection = struct {
582 if (symbol.?.flags.import) break :blk 0;582 if (symbol.?.flags.import) break :blk 0;
583 if (symbol.?.isIFunc(elf_file))583 if (symbol.?.isIFunc(elf_file))
584 break :blk if (apply_relocs) value else 0;584 break :blk if (apply_relocs) value else 0;
585 if (elf_file.base.options.pic and !symbol.?.isAbs(elf_file))585 if ((elf_file.isDynLib() or (elf_file.isExe() and elf_file.base.options.pie)) and
586 !symbol.?.isAbs(elf_file))
587 {
586 break :blk if (apply_relocs) value else 0;588 break :blk if (apply_relocs) value else 0;
589 }
587 break :blk value;590 break :blk value;
588 };591 };
589 try writeInt(value, elf_file, writer);592 try writeInt(value, elf_file, writer);
...@@ -655,7 +658,9 @@ pub const GotSection = struct {...@@ -655,7 +658,9 @@ pub const GotSection = struct {
655 });658 });
656 continue;659 continue;
657 }660 }
658 if (elf_file.base.options.pic and !symbol.?.isAbs(elf_file)) {661 if ((elf_file.isDynLib() or (elf_file.isExe() and elf_file.base.options.pie)) and
662 !symbol.?.isAbs(elf_file))
663 {
659 elf_file.addRelaDynAssumeCapacity(.{664 elf_file.addRelaDynAssumeCapacity(.{
660 .offset = offset,665 .offset = offset,
661 .type = elf.R_X86_64_RELATIVE,666 .type = elf.R_X86_64_RELATIVE,
...@@ -734,8 +739,9 @@ pub const GotSection = struct {...@@ -734,8 +739,9 @@ pub const GotSection = struct {
734 inline else => elf_file.symbol(entry.symbol_index),739 inline else => elf_file.symbol(entry.symbol_index),
735 };740 };
736 switch (entry.tag) {741 switch (entry.tag) {
737 .got => if (symbol.?.flags.import or742 .got => if (symbol.?.flags.import or symbol.?.isIFunc(elf_file) or
738 symbol.?.isIFunc(elf_file) or (elf_file.base.options.pic and !symbol.?.isAbs(elf_file)))743 ((elf_file.isDynLib() or (elf_file.isExe() and elf_file.base.options.pie)) and
744 !symbol.?.isAbs(elf_file)))
739 {745 {
740 num += 1;746 num += 1;
741 },747 },