Skip to content

Commit 3072b37

Browse files
committed
feat: enable LTO mode by default, even for macOS
1 parent 30f7aa5 commit 3072b37

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ jobs:
2626
uses: mlugg/setup-zig@v1
2727

2828
- name: Run `build`
29-
run: zig build -Doptimize=ReleaseFast
29+
run: zig build

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
```sh
1111
git clone https://github.com/allyourcodebase/AFLplusplus.git
1212
cd AFLplusplus/
13-
zig build -Doptimize=ReleaseFast
13+
zig build
1414
```
1515

1616
### Building instrumented executables
17-
For help building instrumented executables, see [kristoff-it/zig-afl-kit](https://github.com/kristoff-it/zig-afl-kit).
1817

18+
For help building instrumented executables, see [kristoff-it/zig-afl-kit](https://github.com/kristoff-it/zig-afl-kit).
1919

2020
<!-- MARKDOWN LINKS -->
2121

build.zig

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ pub fn build(b: *std.Build) !void {
1010

1111
// Custom options
1212
const use_z = b.option(bool, "use-z", "Use system zlib") orelse true;
13+
const enable_lto = b.option(bool, "enable-lto", "Enable LTO mode") orelse true;
1314
const build_nyx = b.option(bool, "build-nyx", "Build Nyx mode on Linux") orelse true;
1415
const enable_wafl = b.option(bool, "enable-wafl", "Enable WAFL mode on WASI") orelse false;
1516
const build_coresight = b.option(bool, "build-coresight", "Build CoreSight mode on ARM64 Linux") orelse true;
1617
const build_unicorn_aarch64 = b.option(bool, "build-unicorn-aarch64", "Build Unicorn mode on ARM64") orelse true;
17-
const enable_lto = b.option(bool, "enable-lto", "Enable LTO mode") orelse if (target.result.isDarwin()) false else true;
1818

1919
// Dependencies
2020
const AFLplusplus_dep = b.dependency("AFLplusplus", .{});
@@ -259,13 +259,9 @@ pub fn build(b: *std.Build) !void {
259259
b.fmt("-DCLANGPP_BIN=\"{s}/clang++\"", .{llvm_bin_dir}),
260260
});
261261
if (enable_lto) {
262-
llvm_c_flags.appendSliceAssumeCapacity(&.{
263-
"-DAFL_CLANG_FLTO=\"-flto\"",
264-
});
262+
llvm_c_flags.appendAssumeCapacity("-DAFL_CLANG_FLTO=\"-flto\"");
265263
} else {
266-
llvm_c_flags.appendSliceAssumeCapacity(&.{
267-
"-DAFL_CLANG_FLTO=\"\"",
268-
});
264+
llvm_c_flags.appendAssumeCapacity("-DAFL_CLANG_FLTO=\"\"");
269265
}
270266
if (target.query.isNative()) {
271267
llvm_c_flags.appendAssumeCapacity("-march=native");
@@ -281,11 +277,6 @@ pub fn build(b: *std.Build) !void {
281277
if (enable_wafl and target.result.isWasm()) {
282278
llvm_cpp_flags.appendSliceAssumeCapacity(&.{ "-DNDEBUG", "-DNO_TLS" });
283279
}
284-
if (target.result.isDarwin()) {
285-
llvm_cpp_flags.appendSliceAssumeCapacity(&.{ "-Wl,-flat_namespace", "-Wl,-undefined,suppress" });
286-
} else {
287-
llvm_cpp_flags.appendAssumeCapacity("-Wl,-znodelete");
288-
}
289280

290281
// LLVM instrumentation object suite
291282
const llvm_objs_step = b.step("llvm_objs", "Install LLVM instrumentation object suite");
@@ -336,9 +327,16 @@ pub fn build(b: *std.Build) !void {
336327
const llvm_libs_step = b.step("llvm_libs", "Install LLVM instrumentation library suite");
337328

338329
const llvm_inc_dir = std.mem.trimRight(u8, b.run(&.{ "llvm-config", "--includedir" }), "\n");
339-
const llvm_name = std.mem.trimRight(u8, b.run(&.{ "llvm-config", "--libs" }), "\n")[2..];
340330
const llvm_inc_path = std.Build.LazyPath{ .cwd_relative = llvm_inc_dir };
341331

332+
const llvm_libs = std.mem.trimRight(u8, b.run(&.{ "llvm-config", "--libs" }), "\n");
333+
const llvm_name: []const u8 = blk: {
334+
if (std.mem.indexOf(u8, llvm_libs, "-lLLVM-1")) |llvm_lib_name_idx| {
335+
break :blk llvm_libs[llvm_lib_name_idx + 2 .. llvm_lib_name_idx + 9];
336+
}
337+
unreachable;
338+
};
339+
342340
const llvm_common_obj = b.addObject(.{
343341
.name = "afl-llvm-common",
344342
.pic = true,
@@ -418,7 +416,7 @@ pub fn build(b: *std.Build) !void {
418416
cc_exe_install.step.dependOn(llvm_libs_step);
419417
llvm_exes_step.dependOn(&cc_exe_install.step);
420418

421-
if (!target.result.isDarwin()) {
419+
if (enable_lto) {
422420
const ld_lto_exe = b.addExecutable(.{
423421
.name = "afl-ld-lto",
424422
.target = target,
@@ -600,6 +598,7 @@ const LLVM_EXE_C_FLAGS = .{
600598

601599
const LLVM_EXE_CPP_FLAGS = .{
602600
"-fno-rtti",
601+
"-Wl,-znodelete",
603602
"-fno-exceptions",
604603
"-Wno-deprecated-declarations",
605604
};

0 commit comments

Comments
 (0)