authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-20 22:33:44+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-10-20 22:33:44+02:00
log6dc45e7d3186f81b1329c71b6380ff3ddd5dec41
tree4cee00278b17b4c5bb1079042c9acf54b2de2ce6
parent7de893c085c94934b2923a9659c9cf70b70d71b2
parentb8ff989fa0863984604591bfe1c54ddbf4e59806
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #17621 from ziglang/elf-pic-pie

elf: actually check for dynamic executables

4 files changed, 95 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),
......@@ -4874,6 +4874,7 @@ fn allocateSpecialPhdrs(self: *Elf) void {
48744874 phdr.p_align = shdr.sh_addralign;
48754875 phdr.p_offset = shdr.sh_offset;
48764876 phdr.p_vaddr = shdr.sh_addr;
4877 phdr.p_paddr = shdr.sh_addr;
48774878 phdr.p_filesz = shdr.sh_size;
48784879 phdr.p_memsz = shdr.sh_size;
48794880 }
......@@ -5586,7 +5587,8 @@ const CsuObjects = struct {
55865587};
55875588
55885589pub fn calcImageBase(self: Elf) u64 {
5589 if (self.base.options.pic) return 0; // TODO flag an error if PIC and image_base_override
5590 if (self.isDynLib()) return 0;
5591 if (self.isExe() and self.base.options.pie) return 0;
55905592 return self.base.options.image_base_override orelse switch (self.ptr_width) {
55915593 .p32 => 0x1000,
55925594 .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 },
test/link/elf.zig+76
......@@ -185,6 +185,8 @@ fn testAsNeeded(b: *Build, opts: Options) *Step {
185185 exe.addLibraryPath(libbaz.getEmittedBinDirectory());
186186 exe.addRPath(libbaz.getEmittedBinDirectory());
187187 exe.linkLibC();
188 // https://github.com/ziglang/zig/issues/17619
189 exe.pie = true;
188190
189191 const run = addRunArtifact(exe);
190192 run.expectStdOutEqual("42\n");
......@@ -211,6 +213,8 @@ fn testAsNeeded(b: *Build, opts: Options) *Step {
211213 exe.addLibraryPath(libbaz.getEmittedBinDirectory());
212214 exe.addRPath(libbaz.getEmittedBinDirectory());
213215 exe.linkLibC();
216 // https://github.com/ziglang/zig/issues/17619
217 exe.pie = true;
214218
215219 const run = addRunArtifact(exe);
216220 run.expectStdOutEqual("42\n");
......@@ -396,6 +400,8 @@ fn testCopyrel(b: *Build, opts: Options) *Step {
396400 , &.{});
397401 exe.linkLibrary(dso);
398402 exe.linkLibC();
403 // https://github.com/ziglang/zig/issues/17619
404 exe.pie = true;
399405
400406 const run = addRunArtifact(exe);
401407 run.expectStdOutEqual("3 5\n");
......@@ -556,6 +562,8 @@ fn testDsoPlt(b: *Build, opts: Options) *Step {
556562 , &.{});
557563 exe.linkLibrary(dso);
558564 exe.linkLibC();
565 // https://github.com/ziglang/zig/issues/17619
566 exe.pie = true;
559567
560568 const run = addRunArtifact(exe);
561569 run.expectStdOutEqual("Hello WORLD\n");
......@@ -591,6 +599,8 @@ fn testDsoUndef(b: *Build, opts: Options) *Step {
591599 \\}
592600 , &.{});
593601 exe.linkLibC();
602 // https://github.com/ziglang/zig/issues/17619
603 exe.pie = true;
594604
595605 const run = addRunArtifact(exe);
596606 run.expectExitCode(0);
......@@ -896,6 +906,8 @@ fn testIFuncAlias(b: *Build, opts: Options) *Step {
896906 , &.{});
897907 exe.force_pic = true;
898908 exe.linkLibC();
909 // https://github.com/ziglang/zig/issues/17619
910 exe.pie = true;
899911
900912 const run = addRunArtifact(exe);
901913 run.expectExitCode(0);
......@@ -1006,6 +1018,8 @@ fn testIFuncDynamic(b: *Build, opts: Options) *Step {
10061018 addCSourceBytes(exe, main_c, &.{});
10071019 exe.linkLibC();
10081020 exe.link_z_lazy = true;
1021 // https://github.com/ziglang/zig/issues/17619
1022 exe.pie = true;
10091023
10101024 const run = addRunArtifact(exe);
10111025 run.expectStdOutEqual("Hello world\n");
......@@ -1015,6 +1029,8 @@ fn testIFuncDynamic(b: *Build, opts: Options) *Step {
10151029 const exe = addExecutable(b, "other", opts);
10161030 addCSourceBytes(exe, main_c, &.{});
10171031 exe.linkLibC();
1032 // https://github.com/ziglang/zig/issues/17619
1033 exe.pie = true;
10181034
10191035 const run = addRunArtifact(exe);
10201036 run.expectStdOutEqual("Hello world\n");
......@@ -1078,6 +1094,8 @@ fn testIFuncFuncPtr(b: *Build, opts: Options) *Step {
10781094 , &.{});
10791095 exe.force_pic = true;
10801096 exe.linkLibC();
1097 // https://github.com/ziglang/zig/issues/17619
1098 exe.pie = true;
10811099
10821100 const run = addRunArtifact(exe);
10831101 run.expectStdOutEqual("3\n");
......@@ -1107,6 +1125,8 @@ fn testIFuncNoPlt(b: *Build, opts: Options) *Step {
11071125 , &.{"-fno-plt"});
11081126 exe.force_pic = true;
11091127 exe.linkLibC();
1128 // https://github.com/ziglang/zig/issues/17619
1129 exe.pie = true;
11101130
11111131 const run = addRunArtifact(exe);
11121132 run.expectStdOutEqual("Hello world\n");
......@@ -1413,6 +1433,8 @@ fn testLargeAlignmentDso(b: *Build, opts: Options) *Step {
14131433 , &.{});
14141434 exe.linkLibrary(dso);
14151435 exe.linkLibC();
1436 // https://github.com/ziglang/zig/issues/17619
1437 exe.pie = true;
14161438
14171439 const run = addRunArtifact(exe);
14181440 run.expectStdOutEqual("Hello world");
......@@ -1447,6 +1469,8 @@ fn testLargeAlignmentExe(b: *Build, opts: Options) *Step {
14471469 , &.{});
14481470 exe.link_function_sections = true;
14491471 exe.linkLibC();
1472 // https://github.com/ziglang/zig/issues/17619
1473 exe.pie = true;
14501474
14511475 const check = exe.checkObject();
14521476 check.checkInSymtab();
......@@ -1475,6 +1499,8 @@ fn testLargeBss(b: *Build, opts: Options) *Step {
14751499 \\}
14761500 , &.{});
14771501 exe.linkLibC();
1502 // https://github.com/ziglang/zig/issues/17619
1503 exe.pie = true;
14781504
14791505 const run = addRunArtifact(exe);
14801506 run.expectExitCode(0);
......@@ -1698,6 +1724,8 @@ fn testPltGot(b: *Build, opts: Options) *Step {
16981724 exe.linkLibrary(dso);
16991725 exe.force_pic = true;
17001726 exe.linkLibC();
1727 // https://github.com/ziglang/zig/issues/17619
1728 exe.pie = true;
17011729
17021730 const run = addRunArtifact(exe);
17031731 run.expectStdOutEqual("Hello world\n");
......@@ -1912,6 +1940,8 @@ fn testTlsDso(b: *Build, opts: Options) *Step {
19121940 , &.{});
19131941 exe.linkLibrary(dso);
19141942 exe.linkLibC();
1943 // https://github.com/ziglang/zig/issues/17619
1944 exe.pie = true;
19151945
19161946 const run = addRunArtifact(exe);
19171947 run.expectStdOutEqual("5 3 5 3 5 3\n");
......@@ -2061,6 +2091,8 @@ fn testTlsGdNoPlt(b: *Build, opts: Options) *Step {
20612091 exe.linkLibrary(a_so);
20622092 exe.linkLibrary(b_so);
20632093 exe.linkLibC();
2094 // https://github.com/ziglang/zig/issues/17619
2095 exe.pie = true;
20642096
20652097 const run = addRunArtifact(exe);
20662098 run.expectStdOutEqual("1 2 3 4 5 6\n");
......@@ -2074,6 +2106,8 @@ fn testTlsGdNoPlt(b: *Build, opts: Options) *Step {
20742106 exe.linkLibrary(b_so);
20752107 exe.linkLibC();
20762108 // exe.link_relax = false; // TODO
2109 // https://github.com/ziglang/zig/issues/17619
2110 exe.pie = true;
20772111
20782112 const run = addRunArtifact(exe);
20792113 run.expectStdOutEqual("1 2 3 4 5 6\n");
......@@ -2117,6 +2151,8 @@ fn testTlsGdToIe(b: *Build, opts: Options) *Step {
21172151 exe.addObject(b_o);
21182152 exe.linkLibrary(dso);
21192153 exe.linkLibC();
2154 // https://github.com/ziglang/zig/issues/17619
2155 exe.pie = true;
21202156
21212157 const run = addRunArtifact(exe);
21222158 run.expectStdOutEqual("1 2 3\n");
......@@ -2132,6 +2168,8 @@ fn testTlsGdToIe(b: *Build, opts: Options) *Step {
21322168 exe.addObject(b_o);
21332169 exe.linkLibrary(dso);
21342170 exe.linkLibC();
2171 // https://github.com/ziglang/zig/issues/17619
2172 exe.pie = true;
21352173
21362174 const run = addRunArtifact(exe);
21372175 run.expectStdOutEqual("1 2 3\n");
......@@ -2211,6 +2249,8 @@ fn testTlsIe(b: *Build, opts: Options) *Step {
22112249 exe.addObject(main_o);
22122250 exe.linkLibrary(dso);
22132251 exe.linkLibC();
2252 // https://github.com/ziglang/zig/issues/17619
2253 exe.pie = true;
22142254
22152255 const run = addRunArtifact(exe);
22162256 run.expectStdOutEqual(exp_stdout);
......@@ -2223,6 +2263,8 @@ fn testTlsIe(b: *Build, opts: Options) *Step {
22232263 exe.linkLibrary(dso);
22242264 exe.linkLibC();
22252265 // exe.link_relax = false; // TODO
2266 // https://github.com/ziglang/zig/issues/17619
2267 exe.pie = true;
22262268
22272269 const run = addRunArtifact(exe);
22282270 run.expectStdOutEqual(exp_stdout);
......@@ -2270,6 +2312,8 @@ fn testTlsLargeAlignment(b: *Build, opts: Options) *Step {
22702312 exe.addObject(c_o);
22712313 exe.linkLibrary(dso);
22722314 exe.linkLibC();
2315 // https://github.com/ziglang/zig/issues/17619
2316 exe.pie = true;
22732317
22742318 const run = addRunArtifact(exe);
22752319 run.expectStdOutEqual("42 1 2 3\n");
......@@ -2282,6 +2326,8 @@ fn testTlsLargeAlignment(b: *Build, opts: Options) *Step {
22822326 exe.addObject(b_o);
22832327 exe.addObject(c_o);
22842328 exe.linkLibC();
2329 // https://github.com/ziglang/zig/issues/17619
2330 exe.pie = true;
22852331
22862332 const run = addRunArtifact(exe);
22872333 run.expectStdOutEqual("42 1 2 3\n");
......@@ -2315,6 +2361,8 @@ fn testTlsLargeTbss(b: *Build, opts: Options) *Step {
23152361 \\}
23162362 , &.{});
23172363 exe.linkLibC();
2364 // https://github.com/ziglang/zig/issues/17619
2365 exe.pie = true;
23182366
23192367 const run = addRunArtifact(exe);
23202368 run.expectStdOutEqual("3 0 5 0 0 0\n");
......@@ -2337,6 +2385,8 @@ fn testTlsLargeStaticImage(b: *Build, opts: Options) *Step {
23372385 , &.{});
23382386 exe.force_pic = true;
23392387 exe.linkLibC();
2388 // https://github.com/ziglang/zig/issues/17619
2389 exe.pie = true;
23402390
23412391 const run = addRunArtifact(exe);
23422392 run.expectStdOutEqual("1 2 3 0 5\n");
......@@ -2375,6 +2425,8 @@ fn testTlsLd(b: *Build, opts: Options) *Step {
23752425 exe.addObject(main_o);
23762426 exe.addObject(a_o);
23772427 exe.linkLibC();
2428 // https://github.com/ziglang/zig/issues/17619
2429 exe.pie = true;
23782430
23792431 const run = addRunArtifact(exe);
23802432 run.expectStdOutEqual(exp_stdout);
......@@ -2387,6 +2439,8 @@ fn testTlsLd(b: *Build, opts: Options) *Step {
23872439 exe.addObject(a_o);
23882440 exe.linkLibC();
23892441 // exe.link_relax = false; // TODO
2442 // https://github.com/ziglang/zig/issues/17619
2443 exe.pie = true;
23902444
23912445 const run = addRunArtifact(exe);
23922446 run.expectStdOutEqual(exp_stdout);
......@@ -2420,6 +2474,8 @@ fn testTlsLdDso(b: *Build, opts: Options) *Step {
24202474 , &.{});
24212475 exe.linkLibrary(dso);
24222476 exe.linkLibC();
2477 // https://github.com/ziglang/zig/issues/17619
2478 exe.pie = true;
24232479
24242480 const run = addRunArtifact(exe);
24252481 run.expectStdOutEqual("1 2\n");
......@@ -2457,6 +2513,8 @@ fn testTlsLdNoPlt(b: *Build, opts: Options) *Step {
24572513 exe.addObject(a_o);
24582514 exe.addObject(b_o);
24592515 exe.linkLibC();
2516 // https://github.com/ziglang/zig/issues/17619
2517 exe.pie = true;
24602518
24612519 const run = addRunArtifact(exe);
24622520 run.expectStdOutEqual("3 5 3 5\n");
......@@ -2469,6 +2527,8 @@ fn testTlsLdNoPlt(b: *Build, opts: Options) *Step {
24692527 exe.addObject(b_o);
24702528 exe.linkLibC();
24712529 // exe.link_relax = false; // TODO
2530 // https://github.com/ziglang/zig/issues/17619
2531 exe.pie = true;
24722532
24732533 const run = addRunArtifact(exe);
24742534 run.expectStdOutEqual("3 5 3 5\n");
......@@ -2554,6 +2614,8 @@ fn testTlsOffsetAlignment(b: *Build, opts: Options) *Step {
25542614 exe.addRPath(dso.getEmittedBinDirectory());
25552615 exe.linkLibC();
25562616 exe.force_pic = true;
2617 // https://github.com/ziglang/zig/issues/17619
2618 exe.pie = true;
25572619
25582620 const run = addRunArtifact(exe);
25592621 run.expectExitCode(0);
......@@ -2588,6 +2650,8 @@ fn testTlsPic(b: *Build, opts: Options) *Step {
25882650 , &.{});
25892651 exe.addObject(obj);
25902652 exe.linkLibC();
2653 // https://github.com/ziglang/zig/issues/17619
2654 exe.pie = true;
25912655
25922656 const run = addRunArtifact(exe);
25932657 run.expectStdOutEqual("3 5 3 5\n");
......@@ -2627,6 +2691,8 @@ fn testTlsSmallAlignment(b: *Build, opts: Options) *Step {
26272691 exe.addObject(b_o);
26282692 exe.addObject(c_o);
26292693 exe.linkLibC();
2694 // https://github.com/ziglang/zig/issues/17619
2695 exe.pie = true;
26302696
26312697 const run = addRunArtifact(exe);
26322698 run.expectStdOutEqual("42\n");
......@@ -2642,6 +2708,8 @@ fn testTlsSmallAlignment(b: *Build, opts: Options) *Step {
26422708 exe.addObject(c_o);
26432709 exe.linkLibrary(dso);
26442710 exe.linkLibC();
2711 // https://github.com/ziglang/zig/issues/17619
2712 exe.pie = true;
26452713
26462714 const run = addRunArtifact(exe);
26472715 run.expectStdOutEqual("42\n");
......@@ -2711,6 +2779,8 @@ fn testWeakExports(b: *Build, opts: Options) *Step {
27112779 const exe = addExecutable(b, "main", opts);
27122780 exe.addObject(obj);
27132781 exe.linkLibC();
2782 // https://github.com/ziglang/zig/issues/17619
2783 exe.pie = true;
27142784
27152785 const check = exe.checkObject();
27162786 check.checkInDynamicSymtab();
......@@ -2743,6 +2813,8 @@ fn testWeakUndefsDso(b: *Build, opts: Options) *Step {
27432813 , &.{});
27442814 exe.linkLibrary(dso);
27452815 exe.linkLibC();
2816 // https://github.com/ziglang/zig/issues/17619
2817 exe.pie = true;
27462818
27472819 const run = addRunArtifact(exe);
27482820 run.expectStdOutEqual("bar=-1\n");
......@@ -2759,6 +2831,8 @@ fn testWeakUndefsDso(b: *Build, opts: Options) *Step {
27592831 , &.{});
27602832 exe.linkLibrary(dso);
27612833 exe.linkLibC();
2834 // https://github.com/ziglang/zig/issues/17619
2835 exe.pie = true;
27622836
27632837 const run = addRunArtifact(exe);
27642838 run.expectStdOutEqual("bar=5\n");
......@@ -2866,6 +2940,8 @@ fn testZText(b: *Build, opts: Options) *Step {
28662940 , &.{});
28672941 exe.linkLibrary(dso);
28682942 exe.linkLibC();
2943 // https://github.com/ziglang/zig/issues/17619
2944 exe.pie = true;
28692945
28702946 const run = addRunArtifact(exe);
28712947 run.expectStdOutEqual("3\n");