authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2022-06-09 00:06:16+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-06-09 18:51:43-04:00
log5816d3eaec3f3bb04e70c89aa402ba9e0e5e7b2c
tree2cccbd1ac1b95a10823fdd092d3d38ee84b76e8d
parentf1cff4fa4a28d42ac9055f94ee9a8f7fd2831cd7

linker: remove `-z noexecstack` option

Note that the current documentation for the `-z noexecstack` is incorrect. This indicates that an object *does not* require an executable stack. This is actually the default of LLD, and there has never been a way to override this default by passing `-z execstack` to LLD. This commit removes the redundant `-z noexecstack` option from zig build-exe/build-lib/build-obj and ignores the option if passed to zig cc for compatibility. As far as I can tell, there is no reason for code to require an executable stack. This option only exists because the stack was originally executable by default and some programs came to depend on that behavior. Instead, mprotect(2) may be used to make memory pages executable.

4 files changed, 1 insertions(+), 15 deletions(-)

src/Compilation.zig-3
...@@ -762,7 +762,6 @@ pub const InitOptions = struct {...@@ -762,7 +762,6 @@ pub const InitOptions = struct {
762 linker_z_notext: bool = false,762 linker_z_notext: bool = false,
763 linker_z_defs: bool = false,763 linker_z_defs: bool = false,
764 linker_z_origin: bool = false,764 linker_z_origin: bool = false,
765 linker_z_noexecstack: bool = false,
766 linker_z_now: bool = true,765 linker_z_now: bool = true,
767 linker_z_relro: bool = true,766 linker_z_relro: bool = true,
768 linker_z_nocopyreloc: bool = false,767 linker_z_nocopyreloc: bool = false,
...@@ -1602,7 +1601,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1602,7 +1601,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1602 .z_defs = options.linker_z_defs,1601 .z_defs = options.linker_z_defs,
1603 .z_origin = options.linker_z_origin,1602 .z_origin = options.linker_z_origin,
1604 .z_nocopyreloc = options.linker_z_nocopyreloc,1603 .z_nocopyreloc = options.linker_z_nocopyreloc,
1605 .z_noexecstack = options.linker_z_noexecstack,
1606 .z_now = options.linker_z_now,1604 .z_now = options.linker_z_now,
1607 .z_relro = options.linker_z_relro,1605 .z_relro = options.linker_z_relro,
1608 .tsaware = options.linker_tsaware,1606 .tsaware = options.linker_tsaware,
...@@ -2350,7 +2348,6 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes...@@ -2350,7 +2348,6 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
2350 man.hash.add(comp.bin_file.options.z_defs);2348 man.hash.add(comp.bin_file.options.z_defs);
2351 man.hash.add(comp.bin_file.options.z_origin);2349 man.hash.add(comp.bin_file.options.z_origin);
2352 man.hash.add(comp.bin_file.options.z_nocopyreloc);2350 man.hash.add(comp.bin_file.options.z_nocopyreloc);
2353 man.hash.add(comp.bin_file.options.z_noexecstack);
2354 man.hash.add(comp.bin_file.options.z_now);2351 man.hash.add(comp.bin_file.options.z_now);
2355 man.hash.add(comp.bin_file.options.z_relro);2352 man.hash.add(comp.bin_file.options.z_relro);
2356 man.hash.add(comp.bin_file.options.hash_style);2353 man.hash.add(comp.bin_file.options.hash_style);
src/link.zig-1
...@@ -113,7 +113,6 @@ pub const Options = struct {...@@ -113,7 +113,6 @@ pub const Options = struct {
113 z_defs: bool,113 z_defs: bool,
114 z_origin: bool,114 z_origin: bool,
115 z_nocopyreloc: bool,115 z_nocopyreloc: bool,
116 z_noexecstack: bool,
117 z_now: bool,116 z_now: bool,
118 z_relro: bool,117 z_relro: bool,
119 tsaware: bool,118 tsaware: bool,
src/link/Elf.zig-5
...@@ -1333,7 +1333,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v...@@ -1333,7 +1333,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
1333 man.hash.add(self.base.options.z_defs);1333 man.hash.add(self.base.options.z_defs);
1334 man.hash.add(self.base.options.z_origin);1334 man.hash.add(self.base.options.z_origin);
1335 man.hash.add(self.base.options.z_nocopyreloc);1335 man.hash.add(self.base.options.z_nocopyreloc);
1336 man.hash.add(self.base.options.z_noexecstack);
1337 man.hash.add(self.base.options.z_now);1336 man.hash.add(self.base.options.z_now);
1338 man.hash.add(self.base.options.z_relro);1337 man.hash.add(self.base.options.z_relro);
1339 man.hash.add(self.base.options.hash_style);1338 man.hash.add(self.base.options.hash_style);
...@@ -1512,10 +1511,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v...@@ -1512,10 +1511,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
1512 try argv.append("-z");1511 try argv.append("-z");
1513 try argv.append("nocopyreloc");1512 try argv.append("nocopyreloc");
1514 }1513 }
1515 if (self.base.options.z_noexecstack) {
1516 try argv.append("-z");
1517 try argv.append("noexecstack");
1518 }
1519 if (self.base.options.z_now) {1514 if (self.base.options.z_now) {
1520 // LLD defaults to -zlazy1515 // LLD defaults to -zlazy
1521 try argv.append("-znow");1516 try argv.append("-znow");
src/main.zig+1-6
...@@ -433,7 +433,6 @@ const usage_build_generic =...@@ -433,7 +433,6 @@ const usage_build_generic =
433 \\ defs Force a fatal error if any undefined symbols remain433 \\ defs Force a fatal error if any undefined symbols remain
434 \\ origin Indicate that the object must have its origin processed434 \\ origin Indicate that the object must have its origin processed
435 \\ nocopyreloc Disable the creation of copy relocations435 \\ nocopyreloc Disable the creation of copy relocations
436 \\ noexecstack Indicate that the object requires an executable stack
437 \\ now (default) Force all relocations to be processed on load436 \\ now (default) Force all relocations to be processed on load
438 \\ lazy Don't force all relocations to be processed on load437 \\ lazy Don't force all relocations to be processed on load
439 \\ relro (default) Force all relocations to be read-only after processing438 \\ relro (default) Force all relocations to be read-only after processing
...@@ -656,7 +655,6 @@ fn buildOutputType(...@@ -656,7 +655,6 @@ fn buildOutputType(
656 var linker_z_notext = false;655 var linker_z_notext = false;
657 var linker_z_defs = false;656 var linker_z_defs = false;
658 var linker_z_origin = false;657 var linker_z_origin = false;
659 var linker_z_noexecstack = false;
660 var linker_z_now = true;658 var linker_z_now = true;
661 var linker_z_relro = true;659 var linker_z_relro = true;
662 var linker_tsaware = false;660 var linker_tsaware = false;
...@@ -1207,8 +1205,6 @@ fn buildOutputType(...@@ -1207,8 +1205,6 @@ fn buildOutputType(
1207 linker_z_defs = true;1205 linker_z_defs = true;
1208 } else if (mem.eql(u8, z_arg, "origin")) {1206 } else if (mem.eql(u8, z_arg, "origin")) {
1209 linker_z_origin = true;1207 linker_z_origin = true;
1210 } else if (mem.eql(u8, z_arg, "noexecstack")) {
1211 linker_z_noexecstack = true;
1212 } else if (mem.eql(u8, z_arg, "now")) {1208 } else if (mem.eql(u8, z_arg, "now")) {
1213 linker_z_now = true;1209 linker_z_now = true;
1214 } else if (mem.eql(u8, z_arg, "lazy")) {1210 } else if (mem.eql(u8, z_arg, "lazy")) {
...@@ -1694,7 +1690,7 @@ fn buildOutputType(...@@ -1694,7 +1690,7 @@ fn buildOutputType(
1694 } else if (mem.eql(u8, z_arg, "origin")) {1690 } else if (mem.eql(u8, z_arg, "origin")) {
1695 linker_z_origin = true;1691 linker_z_origin = true;
1696 } else if (mem.eql(u8, z_arg, "noexecstack")) {1692 } else if (mem.eql(u8, z_arg, "noexecstack")) {
1697 linker_z_noexecstack = true;1693 // noexecstack is the default when linking with LLD
1698 } else if (mem.eql(u8, z_arg, "now")) {1694 } else if (mem.eql(u8, z_arg, "now")) {
1699 linker_z_now = true;1695 linker_z_now = true;
1700 } else if (mem.eql(u8, z_arg, "lazy")) {1696 } else if (mem.eql(u8, z_arg, "lazy")) {
...@@ -2719,7 +2715,6 @@ fn buildOutputType(...@@ -2719,7 +2715,6 @@ fn buildOutputType(
2719 .linker_z_notext = linker_z_notext,2715 .linker_z_notext = linker_z_notext,
2720 .linker_z_defs = linker_z_defs,2716 .linker_z_defs = linker_z_defs,
2721 .linker_z_origin = linker_z_origin,2717 .linker_z_origin = linker_z_origin,
2722 .linker_z_noexecstack = linker_z_noexecstack,
2723 .linker_z_now = linker_z_now,2718 .linker_z_now = linker_z_now,
2724 .linker_z_relro = linker_z_relro,2719 .linker_z_relro = linker_z_relro,
2725 .linker_tsaware = linker_tsaware,2720 .linker_tsaware = linker_tsaware,