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 {
29562956 assert(index == 16);
29572957
29582958 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,
29602960 .Obj => .REL,
29612961 .Lib => switch (self.base.options.link_mode) {
29622962 .Static => @as(elf.ET, .REL),
......@@ -4869,6 +4869,7 @@ fn allocateSpecialPhdrs(self: *Elf) void {
48694869 phdr.p_align = shdr.sh_addralign;
48704870 phdr.p_offset = shdr.sh_offset;
48714871 phdr.p_vaddr = shdr.sh_addr;
4872 phdr.p_paddr = shdr.sh_addr;
48724873 phdr.p_filesz = shdr.sh_size;
48734874 phdr.p_memsz = shdr.sh_size;
48744875 }
......@@ -5581,7 +5582,8 @@ const CsuObjects = struct {
55815582};
55825583
55835584pub fn calcImageBase(self: Elf) u64 {
5584 if (self.base.options.pic) return 0; // TODO flag an error if PIC and image_base_override
5585 if (self.isDynLib()) return 0;
5586 if (self.isExe() and self.base.options.pie) return 0;
55855587 return self.base.options.image_base_override orelse switch (self.ptr_width) {
55865588 .p32 => 0x1000,
55875589 .p64 => 0x1000000,
src/link/Elf/Atom.zig+1-1
......@@ -600,7 +600,7 @@ fn dynAbsRelocAction(symbol: *const Symbol, elf_file: *Elf) RelocAction {
600600}
601601
602602fn outputType(elf_file: *Elf) u2 {
603 return switch (elf_file.base.options.output_mode) {
603 return switch (elf_file.base.options.effectiveOutputMode()) {
604604 .Obj => unreachable,
605605 .Lib => 0,
606606 .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 {
5454 if (elf_file.base.options.z_now) {
5555 flags_1 |= elf.DF_1_NOW;
5656 }
57 if (elf_file.base.options.pie) {
57 if (elf_file.isExe() and elf_file.base.options.pie) {
5858 flags_1 |= elf.DF_1_PIE;
5959 }
6060 // if (elf_file.base.options.z_nodlopen) {
......@@ -226,7 +226,7 @@ pub const ZigGotSection = struct {
226226 flags: Flags = .{},
227227
228228 const Flags = packed struct {
229 needs_rela: bool = false, // TODO in prep for PIC/PIE and base relocations
229 needs_rela: bool = false,
230230 dirty: bool = false,
231231 };
232232
......@@ -251,7 +251,7 @@ pub const ZigGotSection = struct {
251251 entry.* = sym_index;
252252 const symbol = elf_file.symbol(sym_index);
253253 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)) {
255255 zig_got.flags.needs_rela = true;
256256 }
257257 if (symbol.extra(elf_file)) |extra| {
......@@ -491,7 +491,7 @@ pub const GotSection = struct {
491491 const symbol = elf_file.symbol(sym_index);
492492 symbol.flags.has_got = true;
493493 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)))
495495 {
496496 got.flags.needs_rela = true;
497497 }
......@@ -582,8 +582,11 @@ pub const GotSection = struct {
582582 if (symbol.?.flags.import) break :blk 0;
583583 if (symbol.?.isIFunc(elf_file))
584584 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 {
586588 break :blk if (apply_relocs) value else 0;
589 }
587590 break :blk value;
588591 };
589592 try writeInt(value, elf_file, writer);
......@@ -655,7 +658,9 @@ pub const GotSection = struct {
655658 });
656659 continue;
657660 }
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 {
659664 elf_file.addRelaDynAssumeCapacity(.{
660665 .offset = offset,
661666 .type = elf.R_X86_64_RELATIVE,
......@@ -734,8 +739,9 @@ pub const GotSection = struct {
734739 inline else => elf_file.symbol(entry.symbol_index),
735740 };
736741 switch (entry.tag) {
737 .got => if (symbol.?.flags.import or
738 symbol.?.isIFunc(elf_file) or (elf_file.base.options.pic and !symbol.?.isAbs(elf_file)))
742 .got => if (symbol.?.flags.import or symbol.?.isIFunc(elf_file) or
743 ((elf_file.isDynLib() or (elf_file.isExe() and elf_file.base.options.pie)) and
744 !symbol.?.isAbs(elf_file)))
739745 {
740746 num += 1;
741747 },