about summary refs log tree commit diff
path: root/pkgs/stdenv
diff options
context:
space:
mode:
authorSebastián Mancilla <238528+smancill@users.noreply.github.com>2021-08-21 15:33:03 -0400
committerGitHub <noreply@github.com>2021-08-21 15:33:03 -0400
commit488395c0f80e2863e9cf83ffb029a330c8e19573 (patch)
tree86f01366131164ff74cc8d5d4788a78785ec3592 /pkgs/stdenv
parentd95c1e9516c4761524227636cca98aa0af736c68 (diff)
stdenv: add isMachO helper function (#133808)
Detect if a binary is a Mach-O file.
Diffstat (limited to 'pkgs/stdenv')
-rw-r--r--pkgs/stdenv/generic/setup.sh23
1 files changed, 23 insertions, 0 deletions
diff --git a/pkgs/stdenv/generic/setup.sh b/pkgs/stdenv/generic/setup.sh
index ec5a5e6dcb9d8..4431936e3d40a 100644
--- a/pkgs/stdenv/generic/setup.sh
+++ b/pkgs/stdenv/generic/setup.sh
@@ -199,6 +199,29 @@ isELF() {
     if [ "$magic" = $'\177ELF' ]; then return 0; else return 1; fi
 }
 
+# Return success if the specified file is a Mach-O object.
+isMachO() {
+    local fn="$1"
+    local fd
+    local magic
+    exec {fd}< "$fn"
+    read -r -n 4 -u "$fd" magic
+    exec {fd}<&-
+    # https://opensource.apple.com/source/lldb/lldb-310.2.36/examples/python/mach_o.py.auto.html
+    if [[ "$magic" = $'\xfe\xed\xfa\xcf' || "$magic" = $'\xcf\xfa\xed\xfe' ]]; then
+        # MH_MAGIC_64 || MH_CIGAM_64
+        return 0;
+    elif [[ "$magic" = $'\xfe\xed\xfa\xce' || "$magic" = $'\xce\xfa\xed\xfe' ]]; then
+        # MH_MAGIC || MH_CIGAM
+        return 0;
+    elif [[ "$magic" = $'\xca\xfe\xba\xbe' || "$magic" = $'\xbe\xba\xfe\xca' ]]; then
+        # FAT_MAGIC || FAT_CIGAM
+        return 0;
+    else
+        return 1;
+    fi
+}
+
 # Return success if the specified file is a script (i.e. starts with
 # "#!").
 isScript() {