diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2023-10-20 22:33:44 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-20 22:33:44 +0200 |
| commit | 6dc45e7d3186f81b1329c71b6380ff3ddd5dec41 (patch) | |
| tree | 4cee00278b17b4c5bb1079042c9acf54b2de2ce6 | |
| parent | 7de893c085c94934b2923a9659c9cf70b70d71b2 (diff) | |
| parent | b8ff989fa0863984604591bfe1c54ddbf4e59806 (diff) | |
| download | zig-6dc45e7d3186f81b1329c71b6380ff3ddd5dec41.tar.gz zig-6dc45e7d3186f81b1329c71b6380ff3ddd5dec41.zip | |
Merge pull request #17621 from ziglang/elf-pic-pie
elf: actually check for dynamic executables
| -rw-r--r-- | src/link/Elf.zig | 6 | ||||
| -rw-r--r-- | src/link/Elf/Atom.zig | 2 | ||||
| -rw-r--r-- | src/link/Elf/synthetic_sections.zig | 22 | ||||
| -rw-r--r-- | test/link/elf.zig | 76 |
4 files changed, 95 insertions, 11 deletions
diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 4f0312fd27..fba24d0e3a 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -2956,7 +2956,7 @@ fn writeHeader(self: *Elf) !void { assert(index == 16); const elf_type: elf.ET = switch (self.base.options.effectiveOutputMode()) { - .Exe => if (self.base.options.pic) .DYN else .EXEC, + .Exe => if (self.base.options.pie) .DYN else .EXEC, .Obj => .REL, .Lib => switch (self.base.options.link_mode) { .Static => @as(elf.ET, .REL), @@ -4874,6 +4874,7 @@ fn allocateSpecialPhdrs(self: *Elf) void { phdr.p_align = shdr.sh_addralign; phdr.p_offset = shdr.sh_offset; phdr.p_vaddr = shdr.sh_addr; + phdr.p_paddr = shdr.sh_addr; phdr.p_filesz = shdr.sh_size; phdr.p_memsz = shdr.sh_size; } @@ -5586,7 +5587,8 @@ const CsuObjects = struct { }; pub fn calcImageBase(self: Elf) u64 { - if (self.base.options.pic) return 0; // TODO flag an error if PIC and image_base_override + if (self.isDynLib()) return 0; + if (self.isExe() and self.base.options.pie) return 0; return self.base.options.image_base_override orelse switch (self.ptr_width) { .p32 => 0x1000, .p64 => 0x1000000, diff --git a/src/link/Elf/Atom.zig b/src/link/Elf/Atom.zig index 6201bd1644..c4dc8fd65b 100644 --- a/src/link/Elf/Atom.zig +++ b/src/link/Elf/Atom.zig @@ -600,7 +600,7 @@ fn dynAbsRelocAction(symbol: *const Symbol, elf_file: *Elf) RelocAction { } fn outputType(elf_file: *Elf) u2 { - return switch (elf_file.base.options.output_mode) { + return switch (elf_file.base.options.effectiveOutputMode()) { .Obj => unreachable, .Lib => 0, .Exe => if (elf_file.base.options.pie) 1 else 2, diff --git a/src/link/Elf/synthetic_sections.zig b/src/link/Elf/synthetic_sections.zig index 3524b981fd..91b9cc1248 100644 --- a/src/link/Elf/synthetic_sections.zig +++ b/src/link/Elf/synthetic_sections.zig @@ -54,7 +54,7 @@ pub const DynamicSection = struct { if (elf_file.base.options.z_now) { flags_1 |= elf.DF_1_NOW; } - if (elf_file.base.options.pie) { + if (elf_file.isExe() and elf_file.base.options.pie) { flags_1 |= elf.DF_1_PIE; } // if (elf_file.base.options.z_nodlopen) { @@ -226,7 +226,7 @@ pub const ZigGotSection = struct { flags: Flags = .{}, const Flags = packed struct { - needs_rela: bool = false, // TODO in prep for PIC/PIE and base relocations + needs_rela: bool = false, dirty: bool = false, }; @@ -251,7 +251,7 @@ pub const ZigGotSection = struct { entry.* = sym_index; const symbol = elf_file.symbol(sym_index); symbol.flags.has_zig_got = true; - if (elf_file.base.options.pic) { + if (elf_file.isDynLib() or (elf_file.isExe() and elf_file.base.options.pie)) { zig_got.flags.needs_rela = true; } if (symbol.extra(elf_file)) |extra| { @@ -491,7 +491,7 @@ pub const GotSection = struct { const symbol = elf_file.symbol(sym_index); symbol.flags.has_got = true; if (symbol.flags.import or symbol.isIFunc(elf_file) or - (elf_file.base.options.pic and !symbol.isAbs(elf_file))) + ((elf_file.isDynLib() or (elf_file.isExe() and elf_file.base.options.pie)) and !symbol.isAbs(elf_file))) { got.flags.needs_rela = true; } @@ -582,8 +582,11 @@ pub const GotSection = struct { if (symbol.?.flags.import) break :blk 0; if (symbol.?.isIFunc(elf_file)) break :blk if (apply_relocs) value else 0; - if (elf_file.base.options.pic and !symbol.?.isAbs(elf_file)) + if ((elf_file.isDynLib() or (elf_file.isExe() and elf_file.base.options.pie)) and + !symbol.?.isAbs(elf_file)) + { break :blk if (apply_relocs) value else 0; + } break :blk value; }; try writeInt(value, elf_file, writer); @@ -655,7 +658,9 @@ pub const GotSection = struct { }); continue; } - if (elf_file.base.options.pic and !symbol.?.isAbs(elf_file)) { + if ((elf_file.isDynLib() or (elf_file.isExe() and elf_file.base.options.pie)) and + !symbol.?.isAbs(elf_file)) + { elf_file.addRelaDynAssumeCapacity(.{ .offset = offset, .type = elf.R_X86_64_RELATIVE, @@ -734,8 +739,9 @@ pub const GotSection = struct { inline else => elf_file.symbol(entry.symbol_index), }; switch (entry.tag) { - .got => if (symbol.?.flags.import or - symbol.?.isIFunc(elf_file) or (elf_file.base.options.pic and !symbol.?.isAbs(elf_file))) + .got => if (symbol.?.flags.import or symbol.?.isIFunc(elf_file) or + ((elf_file.isDynLib() or (elf_file.isExe() and elf_file.base.options.pie)) and + !symbol.?.isAbs(elf_file))) { num += 1; }, diff --git a/test/link/elf.zig b/test/link/elf.zig index 35b1409a55..eabc0d5f17 100644 --- a/test/link/elf.zig +++ b/test/link/elf.zig @@ -185,6 +185,8 @@ fn testAsNeeded(b: *Build, opts: Options) *Step { exe.addLibraryPath(libbaz.getEmittedBinDirectory()); exe.addRPath(libbaz.getEmittedBinDirectory()); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("42\n"); @@ -211,6 +213,8 @@ fn testAsNeeded(b: *Build, opts: Options) *Step { exe.addLibraryPath(libbaz.getEmittedBinDirectory()); exe.addRPath(libbaz.getEmittedBinDirectory()); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("42\n"); @@ -396,6 +400,8 @@ fn testCopyrel(b: *Build, opts: Options) *Step { , &.{}); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("3 5\n"); @@ -556,6 +562,8 @@ fn testDsoPlt(b: *Build, opts: Options) *Step { , &.{}); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("Hello WORLD\n"); @@ -591,6 +599,8 @@ fn testDsoUndef(b: *Build, opts: Options) *Step { \\} , &.{}); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectExitCode(0); @@ -896,6 +906,8 @@ fn testIFuncAlias(b: *Build, opts: Options) *Step { , &.{}); exe.force_pic = true; exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectExitCode(0); @@ -1006,6 +1018,8 @@ fn testIFuncDynamic(b: *Build, opts: Options) *Step { addCSourceBytes(exe, main_c, &.{}); exe.linkLibC(); exe.link_z_lazy = true; + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("Hello world\n"); @@ -1015,6 +1029,8 @@ fn testIFuncDynamic(b: *Build, opts: Options) *Step { const exe = addExecutable(b, "other", opts); addCSourceBytes(exe, main_c, &.{}); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("Hello world\n"); @@ -1078,6 +1094,8 @@ fn testIFuncFuncPtr(b: *Build, opts: Options) *Step { , &.{}); exe.force_pic = true; exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("3\n"); @@ -1107,6 +1125,8 @@ fn testIFuncNoPlt(b: *Build, opts: Options) *Step { , &.{"-fno-plt"}); exe.force_pic = true; exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("Hello world\n"); @@ -1413,6 +1433,8 @@ fn testLargeAlignmentDso(b: *Build, opts: Options) *Step { , &.{}); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("Hello world"); @@ -1447,6 +1469,8 @@ fn testLargeAlignmentExe(b: *Build, opts: Options) *Step { , &.{}); exe.link_function_sections = true; exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const check = exe.checkObject(); check.checkInSymtab(); @@ -1475,6 +1499,8 @@ fn testLargeBss(b: *Build, opts: Options) *Step { \\} , &.{}); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectExitCode(0); @@ -1698,6 +1724,8 @@ fn testPltGot(b: *Build, opts: Options) *Step { exe.linkLibrary(dso); exe.force_pic = true; exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("Hello world\n"); @@ -1912,6 +1940,8 @@ fn testTlsDso(b: *Build, opts: Options) *Step { , &.{}); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("5 3 5 3 5 3\n"); @@ -2061,6 +2091,8 @@ fn testTlsGdNoPlt(b: *Build, opts: Options) *Step { exe.linkLibrary(a_so); exe.linkLibrary(b_so); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("1 2 3 4 5 6\n"); @@ -2074,6 +2106,8 @@ fn testTlsGdNoPlt(b: *Build, opts: Options) *Step { exe.linkLibrary(b_so); exe.linkLibC(); // exe.link_relax = false; // TODO + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("1 2 3 4 5 6\n"); @@ -2117,6 +2151,8 @@ fn testTlsGdToIe(b: *Build, opts: Options) *Step { exe.addObject(b_o); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("1 2 3\n"); @@ -2132,6 +2168,8 @@ fn testTlsGdToIe(b: *Build, opts: Options) *Step { exe.addObject(b_o); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("1 2 3\n"); @@ -2211,6 +2249,8 @@ fn testTlsIe(b: *Build, opts: Options) *Step { exe.addObject(main_o); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual(exp_stdout); @@ -2223,6 +2263,8 @@ fn testTlsIe(b: *Build, opts: Options) *Step { exe.linkLibrary(dso); exe.linkLibC(); // exe.link_relax = false; // TODO + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual(exp_stdout); @@ -2270,6 +2312,8 @@ fn testTlsLargeAlignment(b: *Build, opts: Options) *Step { exe.addObject(c_o); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("42 1 2 3\n"); @@ -2282,6 +2326,8 @@ fn testTlsLargeAlignment(b: *Build, opts: Options) *Step { exe.addObject(b_o); exe.addObject(c_o); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("42 1 2 3\n"); @@ -2315,6 +2361,8 @@ fn testTlsLargeTbss(b: *Build, opts: Options) *Step { \\} , &.{}); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("3 0 5 0 0 0\n"); @@ -2337,6 +2385,8 @@ fn testTlsLargeStaticImage(b: *Build, opts: Options) *Step { , &.{}); exe.force_pic = true; exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("1 2 3 0 5\n"); @@ -2375,6 +2425,8 @@ fn testTlsLd(b: *Build, opts: Options) *Step { exe.addObject(main_o); exe.addObject(a_o); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual(exp_stdout); @@ -2387,6 +2439,8 @@ fn testTlsLd(b: *Build, opts: Options) *Step { exe.addObject(a_o); exe.linkLibC(); // exe.link_relax = false; // TODO + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual(exp_stdout); @@ -2420,6 +2474,8 @@ fn testTlsLdDso(b: *Build, opts: Options) *Step { , &.{}); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("1 2\n"); @@ -2457,6 +2513,8 @@ fn testTlsLdNoPlt(b: *Build, opts: Options) *Step { exe.addObject(a_o); exe.addObject(b_o); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("3 5 3 5\n"); @@ -2469,6 +2527,8 @@ fn testTlsLdNoPlt(b: *Build, opts: Options) *Step { exe.addObject(b_o); exe.linkLibC(); // exe.link_relax = false; // TODO + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("3 5 3 5\n"); @@ -2554,6 +2614,8 @@ fn testTlsOffsetAlignment(b: *Build, opts: Options) *Step { exe.addRPath(dso.getEmittedBinDirectory()); exe.linkLibC(); exe.force_pic = true; + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectExitCode(0); @@ -2588,6 +2650,8 @@ fn testTlsPic(b: *Build, opts: Options) *Step { , &.{}); exe.addObject(obj); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("3 5 3 5\n"); @@ -2627,6 +2691,8 @@ fn testTlsSmallAlignment(b: *Build, opts: Options) *Step { exe.addObject(b_o); exe.addObject(c_o); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("42\n"); @@ -2642,6 +2708,8 @@ fn testTlsSmallAlignment(b: *Build, opts: Options) *Step { exe.addObject(c_o); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("42\n"); @@ -2711,6 +2779,8 @@ fn testWeakExports(b: *Build, opts: Options) *Step { const exe = addExecutable(b, "main", opts); exe.addObject(obj); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const check = exe.checkObject(); check.checkInDynamicSymtab(); @@ -2743,6 +2813,8 @@ fn testWeakUndefsDso(b: *Build, opts: Options) *Step { , &.{}); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("bar=-1\n"); @@ -2759,6 +2831,8 @@ fn testWeakUndefsDso(b: *Build, opts: Options) *Step { , &.{}); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("bar=5\n"); @@ -2866,6 +2940,8 @@ fn testZText(b: *Build, opts: Options) *Step { , &.{}); exe.linkLibrary(dso); exe.linkLibC(); + // https://github.com/ziglang/zig/issues/17619 + exe.pie = true; const run = addRunArtifact(exe); run.expectStdOutEqual("3\n"); |
