1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const gloves_module = b.createModule(.{
.root_source_file = b.path("src/gloves.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
const elfo = b.addExecutable(.{
.name = "elfo",
.root_module = b.createModule(.{
.root_source_file = b.path("src/elfo.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
const capstone_lib = capstone(b, .{
.optimize = optimize,
.target = target,
});
const frida_lib = gum_stalker(b, .{
.optimize = optimize,
.target = target,
});
frida_lib.step.dependOn(&capstone_lib.step);
frida_lib.root_module.linkLibrary(capstone_lib);
b.installArtifact(capstone_lib);
b.installArtifact(frida_lib);
elfo.root_module.linkLibrary(capstone_lib);
elfo.root_module.addImport("capstone", capstone_lib.root_module);
const gloves = b.addExecutable(.{
.name = "gloves",
.root_module = gloves_module,
});
b.installArtifact(elfo);
b.installArtifact(gloves);
const gdb_opt = b.option(bool, "gdb", "Run executable under gdb") orelse false;
const comp_opts = b.addOptions();
comp_opts.addOption(bool, "gdb", gdb_opt);
const unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.optimize = optimize,
.target = target,
.root_source_file = b.path("tests/test.zig"),
}),
});
const test_step = b.step("test", "Test the library");
const run_elfo_step = b.step("elfo", "See the pretty elfo!");
const run_gloves_step = b.step("gloves", "See the pretty gloves!");
const run_steps = [_]struct { *std.Build.Step, *std.Build.Step.Compile }{
.{ test_step, unit_tests },
.{ run_elfo_step, elfo },
.{ run_gloves_step, gloves },
};
const gdbscript = b.addWriteFile("gdbscript",
\\r
);
for (run_steps) |run| {
run.@"1".root_module.addOptions("meta", comp_opts);
if (gdb_opt) {
const run_gdb = b.addSystemCommand(&.{"gdb"});
run_gdb.addArg("-x");
run_gdb.addFileArg(gdbscript.getDirectory().path(b, "gdbscript"));
run_gdb.addArtifactArg(run.@"1");
run.@"0".dependOn(&run_gdb.step);
} else {
const run_unit = b.addRunArtifact(run.@"1");
run.@"0".dependOn(&run_unit.step);
}
}
}
fn gum_stalker(b: *std.Build, opts: struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
}) *std.Build.Step.Compile {
const dep = b.dependency("frida-gum", .{});
const capstone_dep = b.dependency("capstone", .{});
// stub gumenumtypes.h — normally generated by gnome.mkenums
const wf = b.addWriteFiles();
_ = wf.add("gum/gumenumtypes.h",
\\#ifndef __GUM_ENUM_TYPES_H__
\\#define __GUM_ENUM_TYPES_H__
\\#include <glib-object.h>
\\G_BEGIN_DECLS
\\GType gum_elf_type_get_type (void) G_GNUC_CONST;
\\#define GUM_TYPE_ELF_TYPE (gum_elf_type_get_type ())
\\GType gum_elf_osabi_get_type (void) G_GNUC_CONST;
\\#define GUM_TYPE_ELF_OSABI (gum_elf_osabi_get_type ())
\\GType gum_elf_machine_get_type (void) G_GNUC_CONST;
\\#define GUM_TYPE_ELF_MACHINE (gum_elf_machine_get_type ())
\\GType gum_elf_source_mode_get_type (void) G_GNUC_CONST;
\\#define GUM_TYPE_ELF_SOURCE_MODE (gum_elf_source_mode_get_type ())
\\G_END_DECLS
\\#endif
);
// C-only module (translate-c can't handle GLib's _Pragma macros)
const c_mod = b.createModule(.{
.target = opts.target,
// .optimize = opts.optimize,
.optimize = .Debug,
.link_libc = true,
});
// include paths for C source compilation
c_mod.addIncludePath(dep.path(""));
c_mod.addIncludePath(dep.path("gum"));
c_mod.addIncludePath(dep.path("gum/arch-x86"));
c_mod.addIncludePath(dep.path("gum/arch-arm"));
c_mod.addIncludePath(dep.path("gum/arch-arm64"));
c_mod.addIncludePath(dep.path("gum/arch-mips"));
c_mod.addIncludePath(dep.path("gum/backend-linux/include"));
c_mod.addIncludePath(dep.path("gum/backend-elf"));
c_mod.addIncludePath(dep.path("gum/backend-posix"));
c_mod.addIncludePath(dep.path("libs"));
c_mod.addIncludePath(dep.path("libs/gum/heap"));
c_mod.addIncludePath(dep.path("libs/gum/prof"));
c_mod.addIncludePath(capstone_dep.path("include/capstone"));
c_mod.addIncludePath(capstone_dep.path("include"));
c_mod.addIncludePath(wf.getDirectory());
// system libraries
c_mod.linkSystemLibrary("glib-2.0", .{});
c_mod.linkSystemLibrary("gobject-2.0", .{});
// config defines — replaces meson's config.h + add_project_arguments
// platform / arch
c_mod.addCMacro("HAVE_LINUX", "1");
c_mod.addCMacro("HAVE_I386", "1");
c_mod.addCMacro("HAVE_ELF", "1");
c_mod.addCMacro("HAVE_GLIBC", "1");
// frida-gum options
c_mod.addCMacro("GUM_STATIC", "1");
c_mod.addCMacro("GUM_USE_SYSTEM_ALLOC", "1");
c_mod.addCMacro("FRIDA_VERSION", "\"16.x\"");
// compiler feature detection
c_mod.addCMacro("HAVE_SYNC_LOCK", "1");
c_mod.addCMacro("HAVE_CLEAR_CACHE", "1");
c_mod.addCMacro("HAVE_CLTZ", "1");
c_mod.addCMacro("HAVE_POPCOUNT", "1");
c_mod.addCMacro("HAVE_PACK_PRAGMA", "1");
// system headers
c_mod.addCMacro("HAVE_ELF_H", "1");
c_mod.addCMacro("HAVE_LINK_H", "1");
c_mod.addCMacro("HAVE_STDINT_H", "1");
c_mod.addCMacro("HAVE_SYS_AUXV_H", "1");
c_mod.addCMacro("HAVE_ASM_PRCTL_H", "1");
c_mod.addCMacro("HAVE_SYS_USER_H", "1");
// system functions / types
c_mod.addCMacro("HAVE_MADVISE", "1");
c_mod.addCMacro("HAVE_LONG_DOUBLE", "1");
c_mod.addCMacro("HAVE_LONG_LONG_INT", "1");
c_mod.addCMacro("HAVE_UNSIGNED_LONG_LONG_INT", "1");
c_mod.addCMacro("HAVE_PTHREAD_ATTR_GETSTACK", "1");
c_mod.addCMacro("HAVE_PTHREAD_SETNAME_NP", "1");
// NOTE: HAVE_FRIDA_GLIB omitted — system GLib lacks frida's patches
// capstone compat (frida-gum uses capstone 5 type names)
c_mod.addCMacro("CAPSTONE_ARM_COMPAT_HEADER", "1");
c_mod.addCMacro("CAPSTONE_AARCH64_COMPAT_HEADER", "1");
c_mod.addCMacro("CAPSTONE_SYSTEMZ_COMPAT_HEADER", "1");
// GLib / compiler flags
c_mod.addCMacro("_GNU_SOURCE", "1");
c_mod.addCMacro("G_LOG_DOMAIN", "\"Frida\"");
c_mod.addCMacro("GLIB_VERSION_MIN_REQUIRED", "GLIB_VERSION_2_56");
c_mod.addCMacro("G_DISABLE_DEPRECATED", "");
// core + arch writers (unconditional in upstream meson)
c_mod.addCSourceFiles(.{
.root = dep.path("gum"),
.files = &.{
"gum.c",
"gumapiresolver.c",
"gumbacktracer.c",
"gumcloak.c",
"gumcodeallocator.c",
"gumcodesegment.c",
// gumdarwingrafter.c and gumdarwinmodule.c omitted (darwin only)
"gumelfmodule.c",
"gumeventsink.c",
"gumexceptor.c",
"gumheapapi.c",
"guminterceptor.c",
"guminvocationcontext.c",
"guminvocationlistener.c",
"gumkernel.c",
"gumleb.c",
"gumlibc.c",
"gummemory.c",
"gummemorymap.c",
"gummetalarray.c",
"gummetalhash.c",
"gummoduleapiresolver.c",
"gummodulemap.c",
"gumprintf.c",
"gumprocess.c",
"gumthreadregistry.c",
"gummodule.c",
"gummodulefacade.c",
"gummoduleregistry.c",
"gumreturnaddress.c",
"gumspinlock.c",
"gumstalker.c",
"gumswiftapiresolver.c",
// arch writers (x86 only — arm/arm64/mips need frida's capstone fork)
"arch-x86/gumx86writer.c",
"arch-x86/gumx86relocator.c",
"arch-x86/gumx86reader.c",
// x86 backend
"arch-x86/gumx86backtracer.c",
"backend-x86/gumcpucontext-x86.c",
"backend-x86/gumprocess-x86.c",
"backend-x86/guminterceptor-x86.c",
"backend-x86/gumstalker-x86.c",
// linux + posix backend
"backend-linux/gummemory-linux.c",
"backend-posix/gummemory-posix.c",
"backend-linux/gumprocess-linux.c",
"backend-linux/gumthreadregistry-linux.c",
"backend-linux/gummoduleregistry-linux.c",
"backend-linux/gummodule-linux.c",
"backend-posix/gumtls-posix.c",
"backend-posix/gumexceptor-posix.c",
"backend-posix/gummemoryaccessmonitor-posix.c",
// elf backend
"backend-elf/gummodule-elf.c",
"backend-elf/gummoduleregistry-elf.c",
},
});
// x86 assembly glue
c_mod.addAssemblyFile(dep.path("gum/backend-x86/gumstalker-x86-glue.S"));
const stalker_lib = b.addLibrary(.{
.name = "gumstalker",
.linkage = .static,
.root_module = c_mod,
});
stalker_lib.installHeadersDirectory(capstone_dep.path("include/capstone"), "capstone", .{});
stalker_lib.installHeader(capstone_dep.path("include/platform.h"), "capstone/platform.h");
return stalker_lib;
}
fn capstone(b: *std.Build, opts: struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
}) *std.Build.Step.Compile {
const capstone_dep = b.dependency("capstone", .{});
const translate_c = b.addTranslateC(.{
.root_source_file = capstone_dep.path("include/capstone/capstone.h"),
.target = opts.target,
// .optimize = opts.optimize,
.optimize = .Debug,
.link_libc = true,
});
// translate
const c_mod = translate_c.createModule();
const capstone_lib = b.addLibrary(.{
.name = "capstone",
.linkage = .static,
.root_module = c_mod,
});
capstone_lib.root_module.addIncludePath(capstone_dep.path("include"));
capstone_lib.root_module.addIncludePath(capstone_dep.path("include/capstone"));
capstone_lib.root_module.addIncludePath(capstone_dep.path("include/capstone/X86"));
capstone_lib.root_module.addIncludePath(capstone_dep.path("include/capstone/ARM"));
capstone_lib.installHeadersDirectory(capstone_dep.path("include/capstone"), "capstone", .{});
capstone_lib.installHeader(capstone_dep.path("include/platform.h"), "capstone/platform.h");
if (opts.optimize == .Debug) capstone_lib.root_module.addCMacro("CAPSTONE_DEBUG", "");
capstone_lib.root_module.addCMacro("CAPSTONE_HAS_X86", "");
capstone_lib.root_module.addCMacro("CAPSTONE_HAS_ARM", "");
capstone_lib.root_module.addCMacro("CAPSTONE_HAS_AArch64", "");
capstone_lib.root_module.addCMacro("CAPSTONE_BUILD_CSTOOL", "OFF");
capstone_lib.root_module.addCMacro("CAPSTONE_USE_SYS_DYN_MEM", "1");
capstone_lib.root_module.addCMacro("CAPSTONE_BUILD_DIET", "1");
capstone_lib.root_module.addCMacro("CAPSTONE_X86_REDUCE", "1");
capstone_lib.root_module.addCSourceFiles(.{ .root = capstone_dep.path(""), .files = common_sources });
capstone_lib.root_module.addCSourceFiles(.{
.root = capstone_dep.path("arch/ARM"),
.files = &.{
"ARMBaseInfo.c",
"ARMDisassembler.c",
"ARMDisassemblerExtension.c",
"ARMInstPrinter.c",
"ARMMapping.c",
"ARMModule.c",
},
});
capstone_lib.root_module.addCSourceFiles(.{
.root = capstone_dep.path("arch/X86"),
.files = &.{
"X86Disassembler.c",
"X86DisassemblerDecoder.c",
"X86IntelInstPrinter.c",
"X86ATTInstPrinter.c",
"X86InstPrinterCommon.c",
"X86Mapping.c",
"X86Module.c",
},
});
return capstone_lib;
}
const common_sources: []const []const u8 = &.{
"cs.c",
"Mapping.c",
"MCInst.c",
"MCInstrDesc.c",
"MCInstPrinter.c",
"MCRegisterInfo.c",
"SStream.c",
"utils.c",
};
// .AArch64
// .ARC
// .ARM
// .Alpha
// .BPF
// .EVM
// .HPPA
// .LoongArch
// .M680X
// .M68K
// .MOS65XX
// .Mips
// .PowerPC
// .RISCV
// .SH
// .Sparc
// .SystemZ
// .TMS320C64x
// .TriCore
// .WASM
// .X86
// .XCore
// .Xtensa
|