forked from frida/frida
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevkit.py
executable file
·735 lines (577 loc) · 25.7 KB
/
devkit.py
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
#!/usr/bin/env python3
import argparse
from collections import OrderedDict
from glob import glob
import machine_file
from pathlib import Path
import platform
import re
import shlex
import shutil
import subprocess
import tempfile
if platform.system() == "Windows":
import winenv
from xml.etree import ElementTree
from xml.etree.ElementTree import QName
FRIDA_ROOT = Path(__file__).resolve().parent.parent
INCLUDE_PATTERN = re.compile("#include\s+[<\"](.*?)[>\"]")
DEVKITS = {
"frida-gum": ("frida-gum-1.0", Path("frida-1.0") / "gum" / "gum.h"),
"frida-gumjs": ("frida-gumjs-1.0", Path("frida-1.0") / "gumjs" / "gumscriptbackend.h"),
"frida-core": ("frida-core-1.0", Path("frida-1.0") / "frida-core.h"),
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("kit")
parser.add_argument("host")
parser.add_argument("outdir")
parser.add_argument("-t", "--thin", help="build without cross-arch support", action="store_true")
arguments = parser.parse_args()
kit = arguments.kit
host = arguments.host
outdir = Path(arguments.outdir).resolve()
if arguments.thin:
flavor = "_thin"
else:
flavor = ""
fat_machine_file = query_machine_file_path(host, flavor)
if fat_machine_file is not None \
and not fat_machine_file.exists() \
and query_machine_file_path(host, "_thin").exists():
flavor = "_thin"
outdir.mkdir(parents=True, exist_ok=True)
generate_devkit(kit, host, flavor, outdir)
def generate_devkit(kit, host, flavor, output_dir):
package, umbrella_header = DEVKITS[kit]
meson_config = load_meson_config(host, flavor)
library_filename = compute_library_filename(kit)
(extra_ldflags, thirdparty_symbol_mappings) = generate_library(package,
host,
flavor,
meson_config,
output_dir,
library_filename)
umbrella_header_path = compute_umbrella_header_path(host, flavor, package, umbrella_header)
header_file = output_dir / f"{kit}.h"
if not umbrella_header_path.exists():
raise Exception(f"Header not found: {umbrella_header_path}")
header_source = generate_header(package,
host,
kit,
flavor,
meson_config,
umbrella_header_path,
thirdparty_symbol_mappings)
header_file.write_text(header_source, encoding="utf-8")
example_file = output_dir / f"{kit}-example.c"
example_source = generate_example(example_file,
package,
host,
kit,
flavor,
meson_config,
extra_ldflags)
example_file.write_text(example_source, encoding="utf-8")
extra_files = []
if platform.system() == "Windows":
for msvs_asset in glob(str(asset_path(f"{kit}-*.sln"))) + glob(str(asset_path(f"{kit}-*.vcxproj*"))):
shutil.copy(msvs_asset, output_dir)
extra_files.append(Path(msvs_asset).name)
return [header_file.name, library_filename, example_file.name] + extra_files
def generate_header(package, host, kit, flavor, meson_config, umbrella_header_path, thirdparty_symbol_mappings):
if platform.system() == "Windows":
(win_sdk_dir, win_sdk_version) = winenv.get_windows_sdk()
include_dirs = [
winenv.get_msvc_tool_dir() / "include",
win_sdk_dir / "Include" / win_sdk_version / "ucrt",
FRIDA_ROOT / "build" / "sdk-windows" / msvs_arch_config(host) / "lib" / "glib-2.0" / "include",
FRIDA_ROOT / "build" / "sdk-windows" / msvs_arch_config(host) / "include" / "glib-2.0",
FRIDA_ROOT / "build" / "sdk-windows" / msvs_arch_config(host) / "include" / "json-glib-1.0",
FRIDA_ROOT / "build" / "sdk-windows" / msvs_arch_config(host) / "include" / "capstone",
internal_include_path("gum", host),
FRIDA_ROOT / "frida-gum",
FRIDA_ROOT / "frida-gum" / "bindings",
]
includes = ["/I" + str(include_dir) for include_dir in include_dirs]
preprocessor = subprocess.run([msvs_cl_exe(host), "/nologo", "/E", umbrella_header_path] + includes,
cwd=msvs_runtime_path(host),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8")
if preprocessor.returncode != 0:
raise Exception("Failed to spawn preprocessor: {preprocessor.stderr}")
lines = preprocessor.stdout.split("\n")
mapping_prefix = "#line "
header_refs = [line[line.index("\"") + 1:line.rindex("\"")].replace("\\\\", "/") for line in lines if line.startswith(mapping_prefix)]
header_files = deduplicate(header_refs)
frida_root_slashed = FRIDA_ROOT.as_posix()
header_files = [Path(h) for h in header_files if bool(re.match("^" + frida_root_slashed, h, re.I))]
else:
pkg_cflags = query_pkgconfig_cflags(package, meson_config)
header_dependencies = subprocess.run(
meson_config["c"] + meson_config["c_args"] + pkg_cflags + ["-E", "-M", umbrella_header_path],
capture_output=True,
encoding="utf-8",
check=True).stdout
header_lines = header_dependencies.strip().split("\n")[1:]
header_files = [Path(line.rstrip("\\").strip()) for line in header_lines]
header_files = [h for h in header_files if h.is_relative_to(FRIDA_ROOT)]
devkit_header_lines = []
umbrella_header = header_files[0]
processed_header_files = {umbrella_header}
ingest_header(umbrella_header, header_files, processed_header_files, devkit_header_lines)
if kit == "frida-gumjs":
inspector_server_header = umbrella_header_path.parent / "guminspectorserver.h"
ingest_header(inspector_server_header, header_files, processed_header_files, devkit_header_lines)
if kit == "frida-core" and host.startswith("android-"):
selinux_header = umbrella_header_path.parent / "frida-selinux.h"
ingest_header(selinux_header, header_files, processed_header_files, devkit_header_lines)
devkit_header = u"".join(devkit_header_lines)
if package.startswith("frida-gum"):
config = """#ifndef GUM_STATIC
# define GUM_STATIC
#endif
"""
else:
config = ""
if platform.system() == "Windows":
deps = ["dnsapi", "iphlpapi", "psapi", "shlwapi", "winmm", "ws2_32"]
if package == "frida-core-1.0":
deps.extend(["advapi32", "crypt32", "gdi32", "kernel32", "ole32", "secur32", "shell32", "user32"])
deps.sort()
frida_pragmas = f"#pragma comment(lib, \"{compute_library_filename(kit)}\")"
dep_pragmas = "\n".join([f"#pragma comment(lib, \"{dep}.lib\")" for dep in deps])
config += frida_pragmas + "\n\n" + dep_pragmas + "\n\n"
if len(thirdparty_symbol_mappings) > 0:
public_mappings = []
for original, renamed in extract_public_thirdparty_symbol_mappings(thirdparty_symbol_mappings):
public_mappings.append((original, renamed))
if f"define {original}" not in devkit_header and f"define {original}" not in devkit_header:
continue
def fixup_macro(match):
prefix = match.group(1)
suffix = re.sub(f"\\b{original}\\b", renamed, match.group(2))
return f"#undef {original}\n{prefix}{original}{suffix}"
devkit_header = re.sub(r"^([ \t]*#[ \t]*define[ \t]*){0}\b((.*\\\n)*.*)$".format(original), fixup_macro, devkit_header, flags=re.MULTILINE)
config += "#ifndef __FRIDA_SYMBOL_MAPPINGS__\n"
config += "#define __FRIDA_SYMBOL_MAPPINGS__\n\n"
config += "\n".join([f"#define {original} {renamed}" for original, renamed in public_mappings]) + "\n\n"
config += "#endif\n\n"
return (config + devkit_header).replace("\r\n", "\n")
def ingest_header(header, all_header_files, processed_header_files, result):
with header.open(encoding="utf-8") as f:
for line in f:
match = INCLUDE_PATTERN.match(line.strip())
if match is not None:
name_parts = tuple(match.group(1).split("/"))
num_parts = len(name_parts)
inline = False
for other_header in all_header_files:
if other_header.parts[-num_parts:] == name_parts:
inline = True
if other_header not in processed_header_files:
processed_header_files.add(other_header)
ingest_header(other_header, all_header_files, processed_header_files, result)
break
if not inline:
result.append(line)
else:
result.append(line)
def generate_library(package, host, flavor, meson_config, output_dir, library_filename):
if platform.system() == "Windows":
return generate_library_windows(package, host, flavor, output_dir, library_filename)
else:
return generate_library_unix(package, host, flavor, meson_config, output_dir, library_filename)
def generate_library_windows(package, host, flavor, output_dir, library_filename):
pcre2 = [
sdk_lib_path("libpcre2-8.a", host),
]
libffi = [
sdk_lib_path("libffi.a", host),
]
zlib = [
sdk_lib_path("libz.a", host),
]
libbrotlidec = [
sdk_lib_path("libbrotlicommon.a", host),
sdk_lib_path("libbrotlidec.a", host),
]
glib = pcre2 + [
sdk_lib_path("libglib-2.0.a", host),
]
gobject = glib + libffi + [
sdk_lib_path("libgobject-2.0.a", host),
]
gmodule = glib + [
sdk_lib_path("libgmodule-2.0.a", host),
]
gio = glib + gobject + gmodule + zlib + [
sdk_lib_path("libgio-2.0.a", host),
]
openssl = [
sdk_lib_path("libssl.a", host),
sdk_lib_path("libcrypto.a", host),
]
tls_provider = openssl + [
sdk_lib_path(Path("gio") / "modules" / "libgioopenssl.a", host),
]
nice = [
sdk_lib_path("libnice.a", host),
]
usrsctp = [
sdk_lib_path("libusrsctp.a", host),
]
json_glib = glib + gobject + [
sdk_lib_path("libjson-glib-1.0.a", host),
]
gee = glib + gobject + [
sdk_lib_path("libgee-0.8.a", host),
]
nghttp2 = [
sdk_lib_path("libnghttp2.a", host),
]
sqlite = [
sdk_lib_path("libsqlite3.a", host),
]
libpsl = [
sdk_lib_path("libpsl.a", host),
]
libsoup = nghttp2 + sqlite + libbrotlidec + libpsl + [
sdk_lib_path("libsoup-3.0.a", host),
]
capstone = [
sdk_lib_path("libcapstone.a", host)
]
quickjs = [
sdk_lib_path("libquickjs.a", host)
]
tinycc = [
sdk_lib_path("libtcc.a", host)
]
v8 = []
build_props = ElementTree.parse(FRIDA_ROOT / "releng" / "frida.props")
frida_v8_tag = str(QName("http://schemas.microsoft.com/developer/msbuild/2003", "FridaV8"))
for elem in build_props.iter():
if elem.tag == frida_v8_tag:
if elem.text == "Enabled":
v8 += [
sdk_lib_path("libv8-10.0.a", host),
]
break
gum_lib = internal_arch_lib_path("gum", host)
gum_deps = deduplicate(glib + gobject + capstone)
gum = [gum_lib] + gum_deps
gumjs_lib = internal_arch_lib_path("gumjs", host)
gumjs_deps = deduplicate(gum + quickjs + v8 + gio + tls_provider + json_glib + tinycc + sqlite)
gumjs = [gumjs_lib] + gumjs_deps
gumjs_inspector_lib = internal_noarch_lib_path("gumjs-inspector", host)
gumjs_inspector_deps = deduplicate(gum + json_glib + libsoup)
gumjs_inspector = [gumjs_inspector_lib] + gumjs_inspector_deps
frida_core_lib = internal_noarch_lib_path("frida-core", host)
frida_core_deps = deduplicate(glib
+ gobject
+ gio
+ tls_provider
+ nice
+ openssl
+ usrsctp
+ json_glib
+ gmodule
+ gee
+ libsoup
+ gum
+ gumjs_inspector
+ libbrotlidec
+ capstone)
frida_core = [frida_core_lib] + frida_core_deps
if package == "frida-gum-1.0":
input_libs = gum
elif package == "frida-gumjs-1.0":
input_libs = gumjs
elif package == "frida-core-1.0":
input_libs = frida_core
else:
raise Exception("Unhandled package")
subprocess.run([msvs_lib_exe(host), "/nologo", "/out:" + str(output_dir / library_filename)] + input_libs,
cwd=msvs_runtime_path(host),
capture_output=True,
check=True)
extra_flags = [lib_path.name for lib_path in input_libs]
thirdparty_symbol_mappings = []
return (extra_flags, thirdparty_symbol_mappings)
def generate_library_unix(package, host, flavor, meson_config, output_dir, library_filename):
output_path = output_dir / library_filename
output_path.unlink(missing_ok=True)
library_flags = subprocess.run(meson_config["pkgconfig"] + ["--static", "--libs", package],
capture_output=True,
encoding="utf-8",
check=True).stdout.strip().split(" ")
library_dirs = infer_library_dirs(library_flags)
library_names = infer_library_names(library_flags)
library_paths, extra_flags = resolve_library_paths(library_names, library_dirs)
extra_flags += infer_linker_flags(library_flags)
v8_libs = [path for path in library_paths if path.name.startswith("libv8")]
if len(v8_libs) > 0:
v8_libdir = v8_libs[0].parent
libcxx_libs = [Path(p) for p in glob(str(v8_libdir / "c++" / "*.a"))]
library_paths.extend(libcxx_libs)
ar = meson_config["ar"]
ar_help = subprocess.run(ar + ["--help"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8").stdout
mri_supported = "-M [<mri-script]" in ar_help
if mri_supported:
mri = ["create " + str(output_path)]
mri += [f"addlib {path}" for path in library_paths]
mri += ["save", "end"]
subprocess.run(ar + ["-M"],
input="\n".join(mri),
encoding="utf-8",
check=True)
elif host.startswith("macos-") or host.startswith("ios-"):
subprocess.run(["xcrun", "libtool", "-static", "-o", output_path] + library_paths,
capture_output=True,
check=True)
else:
combined_dir = Path(tempfile.mkdtemp(prefix="devkit"))
object_names = set()
for library_path in library_paths:
scratch_dir = Path(tempfile.mkdtemp(prefix="devkit"))
subprocess.run(ar + ["x", library_path],
cwd=scratch_dir,
capture_output=True,
check=True)
for object_name in [entry.name for entry in scratch_dir.iterdir() if entry.name.endswith(".o")]:
object_path = scratch_dir / object_name
while object_name in object_names:
object_name = "_" + object_name
object_names.add(object_name)
shutil.move(object_path, combined_dir / object_name)
shutil.rmtree(scratch_dir)
subprocess.run(ar + ["rcs", output_path] + list(object_names),
cwd=combined_dir,
capture_output=True,
check=True)
shutil.rmtree(combined_dir)
objcopy = meson_config.get("objcopy", None)
if objcopy is not None:
thirdparty_symbol_mappings = get_thirdparty_symbol_mappings(output_path, meson_config)
renames = "\n".join([f"{original} {renamed}" for original, renamed in thirdparty_symbol_mappings]) + "\n"
with tempfile.NamedTemporaryFile() as renames_file:
renames_file.write(renames.encode("utf-8"))
renames_file.flush()
subprocess.run(objcopy + ["--redefine-syms=" + renames_file.name, output_path],
check=True)
else:
thirdparty_symbol_mappings = []
return (extra_flags, thirdparty_symbol_mappings)
def extract_public_thirdparty_symbol_mappings(mappings):
public_prefixes = ["g_", "glib_", "gobject_", "gio_", "gee_", "json_", "cs_"]
return [(original, renamed) for original, renamed in mappings if any([original.startswith(prefix) for prefix in public_prefixes])]
def get_thirdparty_symbol_mappings(library, meson_config):
return [(name, "_frida_" + name) for name in get_thirdparty_symbol_names(library, meson_config)]
def get_thirdparty_symbol_names(library, meson_config):
visible_names = list(set([name for kind, name in get_symbols(library, meson_config) if kind in ("T", "D", "B", "R", "C")]))
visible_names.sort()
frida_prefixes = ["frida", "_frida", "gum", "_gum"]
thirdparty_names = [name for name in visible_names if not any([name.startswith(prefix) for prefix in frida_prefixes])]
return thirdparty_names
def get_symbols(library, meson_config):
result = []
for line in subprocess.run(meson_config["nm"] + [library],
capture_output=True,
encoding="utf-8",
check=True).stdout.split("\n"):
tokens = line.split(" ")
if len(tokens) < 3:
continue
(kind, name) = tokens[-2:]
result.append((kind, name))
return result
def infer_library_dirs(flags):
return [Path(flag[2:]) for flag in flags if flag.startswith("-L")]
def infer_library_names(flags):
return [flag[2:] for flag in flags if flag.startswith("-l")]
def infer_linker_flags(flags):
return [flag for flag in flags if flag.startswith("-Wl") or flag == "-pthread"]
def resolve_library_paths(names, dirs):
paths = []
flags = []
for name in names:
library_path = None
for d in dirs:
candidate = d / f"lib{name}.a"
if candidate.exists():
library_path = candidate
break
if library_path is not None:
paths.append(library_path)
else:
flags.append(f"-l{name}")
return (deduplicate(paths), flags)
def generate_example(source_file, package, host, kit, flavor, meson_config, extra_ldflags):
os_flavor = "windows" if platform.system() == "Windows" else "unix"
example_code = asset_path(f"{kit}-example-{os_flavor}.c").read_text(encoding="utf-8")
if platform.system() == "Windows":
return example_code
else:
if host.split("-")[0] in ["macos", "ios", "android"]:
cc = "clang++" if kit == "frida-gumjs" else "clang"
else:
cc = "g++" if kit == "frida-gumjs" else "gcc"
cflags = meson_config["common_flags"] + meson_config["c_args"]
ldflags = meson_config["c_link_args"]
(cflags, ldflags) = tweak_flags(cflags, extra_ldflags + ldflags)
if cc == "g++":
ldflags.append("-static-libstdc++")
params = {
"cc": cc,
"cflags": shlex.join(cflags),
"ldflags": shlex.join(ldflags),
"source_filename": source_file.name,
"program_filename": source_file.stem,
"library_name": kit
}
preamble = """\
/*
* Compile with:
*
* %(cc)s %(cflags)s %(source_filename)s -o %(program_filename)s -L. -l%(library_name)s %(ldflags)s
*
* Visit https://frida.re to learn more about Frida.
*/""" % params
return preamble + "\n\n" + example_code
def asset_path(name):
return Path(__file__).parent / "devkit-assets" / name
def load_meson_config(host, flavor):
path = query_machine_file_path(host, flavor)
if path is None:
return None
return machine_file.load(path)
def query_machine_file_path(host, flavor):
if platform.system() == "Windows":
return None
return FRIDA_ROOT / "build" / f"frida{flavor}-{host}.txt"
def query_pkgconfig_cflags(package, meson_config):
raw_flags = subprocess.run(meson_config["pkgconfig"] + ["--cflags", package],
capture_output=True,
encoding="utf-8",
check=True).stdout
return shlex.split(raw_flags)
def msvs_cl_exe(host):
return msvs_tool_path(host, "cl.exe")
def msvs_lib_exe(host):
return msvs_tool_path(host, "lib.exe")
def msvs_tool_path(host, tool):
if host == "windows-x86_64":
return Path(winenv.get_msvc_tool_dir()) / "bin" / "HostX86" / "x64" / tool
else:
return Path(winenv.get_msvc_tool_dir()) / "bin" / "HostX86" / "x86" / tool
def msvs_runtime_path(host):
return Path(winenv.get_msvc_tool_dir()) / "bin" / "HostX86" / "x86"
def msvs_arch_config(host):
if host == "windows-x86_64":
return "x64-Release"
else:
return "Win32-Release"
def msvs_arch_suffix(host):
if host == "windows-x86_64":
return "-64"
else:
return "-32"
def compute_library_filename(kit):
if platform.system() == "Windows":
return f"{kit}.lib"
else:
return f"lib{kit}.a"
def compute_umbrella_header_path(host, flavor, package, umbrella_header):
if platform.system() == "Windows":
if package == "frida-gum-1.0":
return FRIDA_ROOT / "frida-gum" / "gum" / "gum.h"
elif package == "frida-gumjs-1.0":
return FRIDA_ROOT / "frida-gum" / "bindings" / "gumjs" / umbrella_header.name
elif package == "frida-core-1.0":
return FRIDA_ROOT / "build" / "tmp-windows" / msvs_arch_config(host) / "frida-core" / "api" / "frida-core.h"
else:
raise Exception("Unhandled package")
else:
p = FRIDA_ROOT / "build" / ("frida" + flavor + "-" + host)
if host.startswith("ios-"):
p = p / "usr"
return p / "include" / umbrella_header
def sdk_lib_path(name, host):
return FRIDA_ROOT / "build" / "sdk-windows" / msvs_arch_config(host) / "lib" / name
def internal_include_path(name, host):
return FRIDA_ROOT / "build" / "tmp-windows" / msvs_arch_config(host) / (name + msvs_arch_suffix(host))
def internal_noarch_lib_path(name, host):
return FRIDA_ROOT / "build" / "tmp-windows" / msvs_arch_config(host) / name / f"{name}.lib"
def internal_arch_lib_path(name, host):
lib_name = name + msvs_arch_suffix(host)
return FRIDA_ROOT / "build" / "tmp-windows" / msvs_arch_config(host) / lib_name / f"{lib_name}.lib"
def tweak_flags(cflags, ldflags):
tweaked_cflags = []
tweaked_ldflags = []
pending_cflags = cflags[:]
while len(pending_cflags) > 0:
flag = pending_cflags.pop(0)
if flag == "-include":
pending_cflags.pop(0)
else:
tweaked_cflags.append(flag)
tweaked_cflags = deduplicate(tweaked_cflags)
existing_cflags = set(tweaked_cflags)
pending_ldflags = ldflags[:]
seen_libs = set()
seen_flags = set()
while len(pending_ldflags) > 0:
flag = pending_ldflags.pop(0)
if flag in ("-arch", "-isysroot") and flag in existing_cflags:
pending_ldflags.pop(0)
else:
if flag == "-isysroot":
sysroot = pending_ldflags.pop(0)
if "MacOSX" in sysroot:
tweaked_ldflags.append("-isysroot \"$(xcrun --sdk macosx --show-sdk-path)\"")
elif "iPhoneOS" in sysroot:
tweaked_ldflags.append("-isysroot \"$(xcrun --sdk iphoneos --show-sdk-path)\"")
continue
elif flag == "-L":
pending_ldflags.pop(0)
continue
elif flag.startswith("-L"):
continue
elif flag.startswith("-l"):
if flag in seen_libs:
continue
seen_libs.add(flag)
elif flag == "-pthread":
if flag in seen_flags:
continue
seen_flags.add(flag)
tweaked_ldflags.append(flag)
pending_ldflags = tweaked_ldflags
tweaked_ldflags = []
while len(pending_ldflags) > 0:
flag = pending_ldflags.pop(0)
raw_flags = []
while flag.startswith("-Wl,"):
raw_flags.append(flag[4:])
if len(pending_ldflags) > 0:
flag = pending_ldflags.pop(0)
else:
flag = None
break
if len(raw_flags) > 0:
merged_flags = "-Wl," + ",".join(raw_flags)
if "--icf=" in merged_flags:
tweaked_ldflags.append("-fuse-ld=gold")
tweaked_ldflags.append(merged_flags)
if flag is not None and flag not in existing_cflags:
tweaked_ldflags.append(flag)
return (tweaked_cflags, tweaked_ldflags)
def deduplicate(items):
return list(OrderedDict.fromkeys(items))
if __name__ == "__main__":
main()