Skip to content

Commit 3135eaa

Browse files
committed
[GR-14640] Implement bitcode file detection.
PullRequest: graalpython/451
2 parents 1c71d93 + 3afd431 commit 3135eaa

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonLD.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@
4242

4343
import java.io.BufferedReader;
4444
import java.io.File;
45+
import java.io.FileInputStream;
4546
import java.io.IOException;
47+
import java.io.InputStream;
4648
import java.lang.ProcessBuilder.Redirect;
49+
import java.nio.ByteBuffer;
50+
import java.nio.ByteOrder;
4751
import java.nio.file.Files;
4852
import java.nio.file.Path;
4953
import java.nio.file.Paths;
@@ -57,8 +61,10 @@
5761
import jline.internal.InputStreamReader;
5862

5963
public class GraalPythonLD extends GraalPythonCompiler {
60-
private static final String LLVM_IR_BITCODE = "llvm-ir-bitcode";
6164
private static final String LLVM_NM = "llvm-nm";
65+
private static final long BC_MAGIC_WORD = 0xdec04342L; // 'BC' c0de
66+
private static final long WRAPPER_MAGIC_WORD = 0x0B17C0DEL;
67+
6268
private static List<String> linkPrefix = Arrays.asList(new String[]{
6369
"llvm-link",
6470
"-o",
@@ -107,16 +113,11 @@ private void parseOptions(String[] args) {
107113
} else if (arg.startsWith("-l")) {
108114
List<String> bcFiles = searchLib(libraryDirs, arg.substring(2));
109115
for (String bcFile : bcFiles) {
110-
try {
111-
String contentType = Files.probeContentType(Paths.get(bcFile));
112-
if (contentType != null && contentType.contains(LLVM_IR_BITCODE)) {
113-
logV("library input:", bcFile);
114-
addFile(bcFile);
115-
} else {
116-
droppedArgs.add(bcFile + "(dropped as library input)");
117-
}
118-
} catch (IOException e) {
119-
throw new RuntimeException(e);
116+
if (probeContentType(Paths.get(bcFile))) {
117+
logV("library input:", bcFile);
118+
addFile(bcFile);
119+
} else {
120+
droppedArgs.add(bcFile + "(dropped as library input)");
120121
}
121122
}
122123
} else {
@@ -246,8 +247,7 @@ private Collection<? extends String> arMembers(String path) throws IOException,
246247
// seems ok to emulate this at least for the very common case of ar archives with symbol
247248
// definitions that overlap what's defined in explicitly include .o files
248249
outer: for (String f : members) {
249-
String contentType = Files.probeContentType(Paths.get(f));
250-
if (contentType != null && contentType.contains(LLVM_IR_BITCODE)) {
250+
if (probeContentType(Paths.get(f))) {
251251
HashSet<String> definedFuncs = new HashSet<>();
252252
HashSet<String> definedGlobals = new HashSet<>();
253253

@@ -310,4 +310,24 @@ private void launchLD() {
310310
ldArgs.addAll(fileInputs);
311311
exec(ldArgs);
312312
}
313+
314+
private static boolean probeContentType(Path path) {
315+
long magicWord = readMagicWord(path);
316+
if (magicWord == BC_MAGIC_WORD || magicWord == WRAPPER_MAGIC_WORD) {
317+
return true;
318+
}
319+
return false;
320+
}
321+
322+
private static long readMagicWord(Path path) {
323+
try (InputStream is = new FileInputStream(path.toString())) {
324+
byte[] buffer = new byte[4];
325+
if (is.read(buffer) != buffer.length) {
326+
return 0;
327+
}
328+
return Integer.toUnsignedLong(ByteBuffer.wrap(buffer).order(ByteOrder.nativeOrder()).getInt());
329+
} catch (IOException e) {
330+
return 0;
331+
}
332+
}
313333
}

mx.graalpython/mx_graalpython.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ def do_run_python(args, extra_vm_args=None, env=None, jdk=None, **kwargs):
123123
dists.append('SULONG')
124124
if mx.suite("sulong-managed", fatalIfMissing=False):
125125
dists.append('SULONG_MANAGED')
126-
graalpython_args.insert(0, mx_subst.path_substitutions.substitute('--llvm.libraryPath=<path:SULONG_MANAGED_LIBS>'))
127-
else:
128-
graalpython_args.insert(0, mx_subst.path_substitutions.substitute('--llvm.libraryPath=<path:SULONG_LIBS>'))
129126

130127
graalpython_args.insert(0, '--experimental-options=true')
131128

0 commit comments

Comments
 (0)