-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
611 lines (537 loc) · 19.6 KB
/
main.rs
File metadata and controls
611 lines (537 loc) · 19.6 KB
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
use arwen::elf::ElfContainer;
use arwen::macho::MachoContainer;
use std::{
collections::HashMap,
env,
fs::{self, File},
path::Path,
};
fn is_elf_binary(file_contents: &[u8]) -> bool {
file_contents.len() >= 4 && &file_contents[0..4] == b"\x7fELF"
}
fn is_macho_binary(file_contents: &[u8]) -> bool {
if file_contents.len() < 4 {
return false;
}
let magic = u32::from_ne_bytes([
file_contents[0],
file_contents[1],
file_contents[2],
file_contents[3],
]);
// Mach-O magic numbers
magic == 0xfeedface || // 32-bit
magic == 0xfeedfacf || // 64-bit
magic == 0xcafebabe || // Fat binary
magic == 0xcefaedfe || // 32-bit swapped
magic == 0xcffaedfe // 64-bit swapped
}
fn find_python_library_macos() -> Result<String, String> {
eprintln!("fix-python-soname: Looking for Python framework on macOS...");
// Python versions from 3.20 down to 3.8
let mut python_versions = Vec::new();
for major in (8..=20).rev() {
// Framework paths (highest priority)
python_versions.push(format!("Python.framework/Versions/3.{}/Python", major));
}
eprintln!(
"fix-python-soname: Looking for versions: {:?}",
&python_versions[0..6]
);
// macOS Python search paths (ordered by priority)
let mut lib_paths = vec![
// Homebrew paths (most common first)
"/opt/homebrew/opt/python@3.13/Frameworks",
"/opt/homebrew/opt/python@3.12/Frameworks",
"/opt/homebrew/opt/python@3.11/Frameworks",
"/opt/homebrew/opt/python@3.10/Frameworks",
"/opt/homebrew/opt/python@3.9/Frameworks",
"/opt/homebrew/opt/python@3.8/Frameworks",
// Intel Mac Homebrew
"/usr/local/opt/python@3.13/Frameworks",
"/usr/local/opt/python@3.12/Frameworks",
"/usr/local/opt/python@3.11/Frameworks",
"/usr/local/opt/python@3.10/Frameworks",
"/usr/local/opt/python@3.9/Frameworks",
"/usr/local/opt/python@3.8/Frameworks",
// System Python frameworks
"/Library/Frameworks",
"/System/Library/Frameworks",
];
// Check for active virtual environments first
if let Ok(venv) = env::var("VIRTUAL_ENV") {
let venv_fw = format!("{}/Frameworks", venv);
lib_paths.insert(0, Box::leak(venv_fw.into_boxed_str()));
}
// Add user-specific paths
if let Ok(home) = env::var("HOME") {
// pyenv installations
let pyenv_versions = format!("{}/.pyenv/versions", home);
if let Ok(entries) = fs::read_dir(&pyenv_versions) {
for entry in entries.flatten() {
if entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
let version_fw = format!("{}/Frameworks", entry.path().display());
lib_paths.push(Box::leak(version_fw.into_boxed_str()));
}
}
}
}
eprintln!(
"fix-python-soname: Searching in {} framework directories...",
lib_paths.len()
);
// First try exact version matches
for lib_name in &python_versions {
for lib_path in &lib_paths {
let full_path = format!("{}/{}", lib_path, lib_name);
if std::path::Path::new(&full_path).exists() {
eprintln!(
"fix-python-soname: Found Python framework: {} at {}",
lib_name, full_path
);
return Ok(full_path);
}
}
}
eprintln!("fix-python-soname: No exact match found, searching for any Python.framework...");
// If no exact match found, search directories for any Python frameworks
for lib_path in &lib_paths {
if let Ok(entries) = fs::read_dir(lib_path) {
let mut found_frameworks: Vec<(String, u32, u32)> = Vec::new();
for entry in entries.flatten() {
if let Some(name) = entry.file_name().to_str() {
if name == "Python.framework" {
// Check for version directories
let versions_dir = entry.path().join("Versions");
if let Ok(version_entries) = fs::read_dir(&versions_dir) {
for version_entry in version_entries.flatten() {
if let Some(version_name) = version_entry.file_name().to_str() {
if let Some(version_start) = version_name.find("3.") {
let version_part = &version_name[version_start + 2..];
if let Ok(minor) = version_part.parse::<u32>() {
let python_path = version_entry.path().join("Python");
if python_path.exists() {
found_frameworks.push((
python_path.to_string_lossy().to_string(),
3,
minor,
));
}
}
}
}
}
}
}
}
}
// Sort by version (newest first)
found_frameworks.sort_by(|a, b| b.2.cmp(&a.2).then(b.1.cmp(&a.1)));
if let Some((framework_path, _, _)) = found_frameworks.first() {
eprintln!(
"fix-python-soname: Found Python framework: {} in {}",
framework_path, lib_path
);
return Ok(framework_path.clone());
}
}
}
Err(
"No Python framework found on the system. Searched in:\n".to_string()
+ &lib_paths[..10].join("\n ")
+ "\n ... and more",
)
}
fn find_python_library() -> Result<String, String> {
// Generate Python versions from 3.20 down to 3.8
let mut python_versions = Vec::new();
for major in (8..=20).rev() {
// Standard versioned libraries
python_versions.push(format!("libpython3.{major}.so.1.0"));
python_versions.push(format!("libpython3.{major}.so.1"));
python_versions.push(format!("libpython3.{major}.so"));
// Some distributions include 'm' suffix
python_versions.push(format!("libpython3.{major}m.so.1.0"));
python_versions.push(format!("libpython3.{major}m.so.1"));
python_versions.push(format!("libpython3.{major}m.so"));
}
eprintln!(
"fix-python-soname: Looking for versions: {:?}",
&python_versions[0..6]
);
// Get system architecture
let arch = std::env::consts::ARCH;
let arch_triplet = match arch {
"x86_64" => "x86_64-linux-gnu",
"x86" => "i386-linux-gnu",
"aarch64" => "aarch64-linux-gnu",
"arm" => "arm-linux-gnueabihf",
"powerpc64" => "powerpc64le-linux-gnu",
"s390x" => "s390x-linux-gnu",
_ => "",
};
// Comprehensive list of library paths
let mut lib_paths = vec![
// Standard system paths (most common first)
"/usr/lib",
"/usr/lib64",
"/usr/local/lib",
"/usr/local/lib64",
"/lib",
"/lib64",
"/lib32",
// Debian/Ubuntu multiarch paths
"/usr/lib/x86_64-linux-gnu",
"/usr/lib/i386-linux-gnu",
"/usr/lib/aarch64-linux-gnu",
"/usr/lib/arm-linux-gnueabihf",
// RedHat/CentOS/Fedora Software Collections
"/opt/rh/rh-python38/root/usr/lib64",
"/opt/rh/rh-python39/root/usr/lib64",
"/opt/rh/rh-python310/root/usr/lib64",
"/opt/rh/rh-python311/root/usr/lib64",
// Python built from source
"/usr/local/python/lib",
"/usr/local/python3/lib",
"/opt/python/lib",
"/opt/python3/lib",
"/opt/python-3.11/lib",
"/opt/python-3.12/lib",
// Container/Docker common paths
"/usr/lib/python3",
"/usr/local/lib/python3",
"/opt/lib",
// Snap packages
"/snap/core18/current/usr/lib",
"/snap/core20/current/usr/lib",
"/snap/core22/current/usr/lib",
"/snap/python38/current/usr/lib",
"/snap/python39/current/usr/lib",
"/snap/python310/current/usr/lib",
// Flatpak runtime paths
"/var/lib/flatpak/runtime/org.freedesktop.Platform/x86_64/21.08/active/files/lib",
"/var/lib/flatpak/runtime/org.freedesktop.Platform/x86_64/22.08/active/files/lib",
"/var/lib/flatpak/runtime/org.freedesktop.Platform/x86_64/23.08/active/files/lib",
// Alpine Linux (musl)
"/usr/lib/apk/db",
// Homebrew on Linux
"/home/linuxbrew/.linuxbrew/lib",
"/opt/homebrew/lib",
// macOS paths (for cross-platform support)
"/System/Library/Frameworks/Python.framework/Versions/3.11/lib",
"/System/Library/Frameworks/Python.framework/Versions/3.12/lib",
"/Library/Frameworks/Python.framework/Versions/3.11/lib",
"/Library/Frameworks/Python.framework/Versions/3.12/lib",
// Nix/NixOS paths
"/nix/var/nix/profiles/default/lib",
"/run/current-system/sw/lib",
// Gentoo
"/usr/lib/python-exec/python3.11",
"/usr/lib/python-exec/python3.12",
// System Python config directories
"/usr/lib/python3.8/config-3.8-x86_64-linux-gnu",
"/usr/lib/python3.9/config-3.9-x86_64-linux-gnu",
"/usr/lib/python3.10/config-3.10-x86_64-linux-gnu",
"/usr/lib/python3.11/config-3.11-x86_64-linux-gnu",
"/usr/lib/python3.12/config-3.12-x86_64-linux-gnu",
];
// Add architecture-specific paths if we detected the architecture
if !arch_triplet.is_empty() {
lib_paths.insert(
0,
Box::leak(format!("/usr/lib/{}", arch_triplet).into_boxed_str()),
);
lib_paths.insert(
1,
Box::leak(format!("/usr/local/lib/{}", arch_triplet).into_boxed_str()),
);
lib_paths.insert(
2,
Box::leak(format!("/lib/{}", arch_triplet).into_boxed_str()),
);
}
// Add conda/anaconda paths from common locations
let conda_paths = vec![
"/opt/conda/lib",
"/opt/anaconda/lib",
"/opt/anaconda3/lib",
"/opt/miniconda/lib",
"/opt/miniconda3/lib",
];
lib_paths.extend(conda_paths);
// Check for active virtual environments first
if let Ok(venv) = env::var("VIRTUAL_ENV") {
lib_paths.insert(0, Box::leak(format!("{}/lib", venv).into_boxed_str()));
lib_paths.insert(0, Box::leak(format!("{}/lib64", venv).into_boxed_str()));
}
// Check for active conda environment
if let Ok(conda_prefix) = env::var("CONDA_PREFIX") {
lib_paths.insert(
0,
Box::leak(format!("{}/lib", conda_prefix).into_boxed_str()),
);
}
// Add user-specific paths
if let Ok(home) = env::var("HOME") {
// Conda/Mamba installations
lib_paths.push(Box::leak(
format!("{}/anaconda3/lib", home).into_boxed_str(),
));
lib_paths.push(Box::leak(
format!("{}/miniconda3/lib", home).into_boxed_str(),
));
lib_paths.push(Box::leak(
format!("{}/miniforge3/lib", home).into_boxed_str(),
));
lib_paths.push(Box::leak(
format!("{}/mambaforge/lib", home).into_boxed_str(),
));
lib_paths.push(Box::leak(
format!("{}/.conda/envs/base/lib", home).into_boxed_str(),
));
// Pyenv - search all installed versions
let pyenv_root = format!("{}/.pyenv/versions", home);
if let Ok(entries) = fs::read_dir(&pyenv_root) {
for entry in entries.flatten() {
if entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
let version_lib = format!("{}/lib", entry.path().display());
lib_paths.push(Box::leak(version_lib.into_boxed_str()));
}
}
}
// Local installations
lib_paths.push(Box::leak(format!("{}/.local/lib", home).into_boxed_str()));
lib_paths.push(Box::leak(format!("{}/.local/lib64", home).into_boxed_str()));
// asdf version manager
let asdf_python = format!("{}/.asdf/installs/python", home);
if let Ok(entries) = fs::read_dir(&asdf_python) {
for entry in entries.flatten() {
if entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
let version_lib = format!("{}/lib", entry.path().display());
lib_paths.push(Box::leak(version_lib.into_boxed_str()));
}
}
}
}
// Add paths from environment variables
if let Ok(ld_library_path) = env::var("LD_LIBRARY_PATH") {
for path in ld_library_path.split(':') {
if !path.is_empty() {
lib_paths.push(Box::leak(path.to_string().into_boxed_str()));
}
}
}
eprintln!(
"fix-python-soname: Searching in {} directories...",
lib_paths.len()
);
eprintln!(
"fix-python-soname: First 5 paths: {:?}",
&lib_paths[0..5.min(lib_paths.len())]
);
// First try exact version matches
for lib_name in &python_versions {
for lib_path in &lib_paths {
let full_path = format!("{}/{}", lib_path, lib_name);
if Path::new(&full_path).exists() {
eprintln!(
"fix-python-soname: Found Python library: {} at {}",
lib_name, full_path
);
return Ok(lib_name.to_string());
}
}
}
eprintln!("fix-python-soname: No exact match found, searching for any libpython*.so files...");
// If no exact match found, search directories for any libpython*.so
for lib_path in &lib_paths {
if let Ok(entries) = fs::read_dir(lib_path) {
let mut found_libs: Vec<(String, u32, u32)> = Vec::new();
for entry in entries.flatten() {
if let Some(name) = entry.file_name().to_str() {
if name.starts_with("libpython") && name.contains(".so") {
// Try to extract version numbers
if let Some(version_start) = name.find("3.") {
let version_part = &name[version_start + 2..];
if let Some(major_end) = version_part.find(|c: char| !c.is_numeric()) {
if let Ok(minor) = version_part[..major_end].parse::<u32>() {
found_libs.push((name.to_string(), 3, minor));
}
}
}
}
}
}
// Sort by version (newest first)
found_libs.sort_by(|a, b| b.2.cmp(&a.2).then(b.1.cmp(&a.1)));
if let Some((lib_name, _, _)) = found_libs.first() {
eprintln!(
"fix-python-soname: Found Python library: {} in {}",
lib_name, lib_path
);
return Ok(lib_name.clone());
}
}
}
Err(
"No Python library found on the system. Searched in:\n".to_string()
+ &lib_paths[..10].join("\n ")
+ "\n ... and more",
)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("fix-python-soname: Starting binary patcher...");
let args: Vec<String> = env::args().collect();
eprintln!("fix-python-soname: Arguments: {:?}", args);
if args.len() != 2 {
return Err(format!("Usage: {} <path-to-node-file>", args[0]).into());
}
let node_file_path = &args[1];
eprintln!("fix-python-soname: Processing file: {}", node_file_path);
// Read the file first to detect format
eprintln!("fix-python-soname: Reading binary file...");
let file_contents =
fs::read(node_file_path).map_err(|error| format!("Failed to read file: {error}"))?;
eprintln!(
"fix-python-soname: Binary file size: {} bytes",
file_contents.len()
);
// Detect binary format and process accordingly
if is_elf_binary(&file_contents) {
eprintln!("fix-python-soname: Detected ELF binary (Linux)");
process_elf_binary(&file_contents, node_file_path)
} else if is_macho_binary(&file_contents) {
eprintln!("fix-python-soname: Detected Mach-O binary (macOS)");
process_macho_binary(&file_contents, node_file_path)
} else {
Err("Unsupported binary format. Only ELF (Linux) and Mach-O (macOS) are supported.".into())
}
}
fn process_elf_binary(
file_contents: &[u8],
node_file_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
// Find the local Python library (Linux)
let new_python_lib = find_python_library()?;
// Parse the ELF file
eprintln!("fix-python-soname: Parsing ELF file...");
let mut elf =
ElfContainer::parse(file_contents).map_err(|error| format!("Failed to parse ELF: {error}"))?;
// Get the list of needed libraries
eprintln!("fix-python-soname: Getting needed libraries...");
let needed_libs: Vec<String> = elf
.inner
.elf_needed()
.map(|lib| String::from_utf8_lossy(lib).to_string())
.collect();
eprintln!("fix-python-soname: Needed libraries: {:?}", needed_libs);
// Find the existing Python dependency
let python_lib = needed_libs
.iter()
.find(|lib| lib.starts_with("libpython") && lib.contains(".so"))
.ok_or("No Python library dependency found in the binary")?;
eprintln!(
"fix-python-soname: Current Python dependency: {}",
python_lib
);
// Check if already pointing to the correct library
if python_lib == &new_python_lib {
eprintln!("fix-python-soname: Already using the correct Python library");
return Ok(());
}
eprintln!("fix-python-soname: Replacing with: {}", new_python_lib);
// Create a map for replacement
let mut replacements = HashMap::new();
replacements.insert(python_lib.clone(), new_python_lib);
// Replace the needed dependency
eprintln!("fix-python-soname: Replacing dependency...");
elf
.replace_needed(&replacements)
.map_err(|error| format!("Failed to replace needed dependency: {error}"))?;
// Create backup
let file_path = Path::new(node_file_path);
let backup_path = file_path.with_extension("node.bak");
eprintln!(
"fix-python-soname: Creating backup at: {}",
backup_path.display()
);
fs::copy(file_path, &backup_path).map_err(|error| format!("Failed to create backup: {error}"))?;
eprintln!("fix-python-soname: Backup created successfully");
// Write the modified file
eprintln!("fix-python-soname: Writing modified ELF file...");
let output_file = File::create(node_file_path)
.map_err(|error| format!("Failed to create output file: {error}"))?;
elf
.write(&output_file)
.map_err(|error| format!("Failed to write ELF: {error}"))?;
eprintln!(
"fix-python-soname: Successfully updated: {}",
node_file_path
);
Ok(())
}
fn process_macho_binary(
file_contents: &[u8],
node_file_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
// Find the local Python framework (macOS)
let new_python_framework = find_python_library_macos()?;
// Parse the Mach-O file
eprintln!("fix-python-soname: Parsing Mach-O file...");
let mut macho = MachoContainer::parse(file_contents)
.map_err(|error| format!("Failed to parse Mach-O: {error}"))?;
// Get the list of linked libraries (equivalent to needed libs on ELF)
eprintln!("fix-python-soname: Getting linked libraries...");
// Access the libs field based on the macho type
let libs = match &macho.inner {
arwen::macho::MachoType::SingleArch(single) => &single.inner.libs,
arwen::macho::MachoType::Fat(fat) => {
if fat.archs.is_empty() {
return Err("No architectures found in fat binary".into());
}
&fat.archs[0].inner.inner.libs // Use first architecture
}
};
eprintln!("fix-python-soname: Linked libraries: {:?}", libs);
// Find the existing Python framework dependency
let python_framework = libs
.iter()
.find(|lib| lib.contains("Python.framework") || lib.contains("Python"))
.ok_or("No Python framework dependency found in the binary")?;
eprintln!(
"fix-python-soname: Current Python framework: {}",
python_framework
);
// Check if already pointing to the correct framework
if python_framework == &new_python_framework {
eprintln!("fix-python-soname: Already using the correct Python framework");
return Ok(());
}
eprintln!(
"fix-python-soname: Replacing with: {}",
new_python_framework
);
// Use change_install_name to replace the Python framework path
eprintln!("fix-python-soname: Changing install name...");
macho
.change_install_name(python_framework, &new_python_framework)
.map_err(|error| format!("Failed to change install name: {error}"))?;
// Create backup
let file_path = Path::new(node_file_path);
let backup_path = file_path.with_extension("node.bak");
eprintln!(
"fix-python-soname: Creating backup at: {}",
backup_path.display()
);
fs::copy(file_path, &backup_path).map_err(|error| format!("Failed to create backup: {error}"))?;
eprintln!("fix-python-soname: Backup created successfully");
// Write the modified file
eprintln!("fix-python-soname: Writing modified Mach-O file...");
fs::write(node_file_path, &macho.data)
.map_err(|error| format!("Failed to write Mach-O: {error}"))?;
eprintln!(
"fix-python-soname: Successfully updated: {}",
node_file_path
);
Ok(())
}