Skip to content

Commit f258e19

Browse files
authored
Merge pull request #35 from superwall/ir/fix/16-kb-alignment-cross
Unset flags from previous compilations, add common page size and page test script
2 parents 7477e0c + f44a397 commit f258e19

File tree

7 files changed

+168
-3
lines changed

7 files changed

+168
-3
lines changed

.cargo/config.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
[target.x86_64-linux-android]
22
rustflags = [
3+
"-C", "link-arg=-Wl,-z,common-page-size=16384",
34
"-C", "link-arg=-Wl,-z,max-page-size=16384"
45
]
56

67
[target.armv7-linux-androideabi]
78
rustflags = [
9+
"-C", "link-arg=-Wl,-z,common-page-size=16384",
810
"-C", "link-arg=-Wl,-z,max-page-size=16384"
911
]
1012

1113
[target.aarch64-linux-android]
1214
rustflags = [
15+
"-C", "link-arg=-Wl,-z,common-page-size=16384",
1316
"-C", "link-arg=-Wl,-z,max-page-size=16384"
1417
]
1518

1619
[target.i686-linux-android]
1720
rustflags = [
21+
"-C", "link-arg=-Wl,-z,common-page-size=16384",
1822
"-C", "link-arg=-Wl,-z,max-page-size=16384"
19-
]
23+
]

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 1.0.3
4+
5+
## Enhancements
6+
- Ensure that previously set compilation flags do not affect Android compilation
7+
- Add flags to ensure common page size is being passed to Cross
8+
39
## 1.0.2.
410

511
## Enhancements

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cel-eval"
3-
version = "1.0.2"
3+
version = "1.0.3"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.htmlž

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ To build the library, run:
2828

2929
(note: for the first run you will need to `chmod +x build.sh` and wait a bit until the docker images are downloaded)
3030

31+
### Verifying 16KB Page Alignment
32+
33+
To verify that the built libraries are properly aligned for Android's 16KB page size requirement:
34+
35+
```shell
36+
./test_16kb_build.sh
37+
```
38+
39+
Or to verify alignment of existing libraries without rebuilding:
40+
41+
```shell
42+
./check_elf_alignment.sh target/android/jniLibs/
43+
```
44+
3145
This will:
3246

3347
- Clear the previously built jniLibs

build_android.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -e
44

55
echo "Building for Android x86_64, armv7, aarch64:"
6-
6+
unset RUSTFLAGS
77
export ANDROID_NDK=r28b ANDROID_SDK=26 ANDROID_VERSION=11.0.0_r48
88

99
build_targets=(

check_elf_alignment.sh

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/bin/bash
2+
progname="${0##*/}"
3+
progname="${progname%.sh}"
4+
5+
# usage: check_elf_alignment.sh [path to *.so files|path to *.apk]
6+
7+
cleanup_trap() {
8+
if [ -n "${tmp}" -a -d "${tmp}" ]; then
9+
rm -rf ${tmp}
10+
fi
11+
exit $1
12+
}
13+
14+
usage() {
15+
echo "Host side script to check the ELF alignment of shared libraries."
16+
echo "Shared libraries are reported ALIGNED when their ELF regions are"
17+
echo "16 KB or 64 KB aligned. Otherwise they are reported as UNALIGNED."
18+
echo
19+
echo "Usage: ${progname} [input-path|input-APK|input-APEX]"
20+
}
21+
22+
if [ ${#} -ne 1 ]; then
23+
usage
24+
exit
25+
fi
26+
27+
case ${1} in
28+
--help | -h | -\?)
29+
usage
30+
exit
31+
;;
32+
33+
*)
34+
dir="${1}"
35+
;;
36+
esac
37+
38+
if ! [ -f "${dir}" -o -d "${dir}" ]; then
39+
echo "Invalid file: ${dir}" >&2
40+
exit 1
41+
fi
42+
43+
if [[ "${dir}" == *.apk ]]; then
44+
trap 'cleanup_trap' EXIT
45+
46+
echo
47+
echo "Recursively analyzing $dir"
48+
echo
49+
50+
if { zipalign --help 2>&1 | grep -q "\-P <pagesize_kb>"; }; then
51+
echo "=== APK zip-alignment ==="
52+
zipalign -v -c -P 16 4 "${dir}" | egrep 'lib/arm64-v8a|lib/x86_64|Verification'
53+
echo "========================="
54+
else
55+
echo "NOTICE: Zip alignment check requires build-tools version 35.0.0-rc3 or higher."
56+
echo " You can install the latest build-tools by running the below command"
57+
echo " and updating your \$PATH:"
58+
echo
59+
echo " sdkmanager \"build-tools;35.0.0-rc3\""
60+
fi
61+
62+
dir_filename=$(basename "${dir}")
63+
tmp=$(mktemp -d -t "${dir_filename%.apk}_out_XXXXX")
64+
unzip "${dir}" lib/* -d "${tmp}" >/dev/null 2>&1
65+
dir="${tmp}"
66+
fi
67+
68+
if [[ "${dir}" == *.apex ]]; then
69+
trap 'cleanup_trap' EXIT
70+
71+
echo
72+
echo "Recursively analyzing $dir"
73+
echo
74+
75+
dir_filename=$(basename "${dir}")
76+
tmp=$(mktemp -d -t "${dir_filename%.apex}_out_XXXXX")
77+
deapexer extract "${dir}" "${tmp}" || { echo "Failed to deapex." && exit 1; }
78+
dir="${tmp}"
79+
fi
80+
81+
unaligned_libs=()
82+
83+
matches="$(find "${dir}" -type f)"
84+
IFS=$'\n'
85+
for match in $matches; do
86+
# We could recursively call this script or rewrite it to though.
87+
[[ "${match}" == *".apk" ]] && echo "WARNING: doesn't recursively inspect .apk file: ${match}"
88+
[[ "${match}" == *".apex" ]] && echo "WARNING: doesn't recursively inspect .apex file: ${match}"
89+
90+
[[ $(file "${match}") == *"ELF"* ]] || continue
91+
92+
res="$(objdump -p "${match}" | grep LOAD | awk '{ print $NF }' | head -1)"
93+
if [[ $res =~ 2\*\*(1[4-9]|[2-9][0-9]|[1-9][0-9]{2,}) ]]; then
94+
echo "${match}: ALIGNED"
95+
else
96+
echo "${match}: UNALIGNED ($res)"
97+
unaligned_libs+=("${match}")
98+
fi
99+
done
100+
101+
if [ ${#unaligned_libs[@]} -gt 0 ]; then
102+
echo "Found ${#unaligned_libs[@]} unaligned libs (only arm64-v8a/x86_64 libs need to be aligned)."
103+
exit 1
104+
else
105+
echo "ALIGNED"
106+
fi

test_16kb_build.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
echo "=== Testing 16KB page size alignment ==="
6+
echo
7+
8+
# Check if libraries exist, if not suggest building
9+
if [ ! -d "target/android/jniLibs" ] || [ -z "$(find target/android/jniLibs -name "*.so" 2>/dev/null)" ]; then
10+
echo "No built libraries found. Please run: ./build_android.sh first"
11+
echo "Or run with --build to build automatically:"
12+
echo " $0 --build"
13+
if [ "$1" = "--build" ]; then
14+
echo
15+
echo "Building Android libraries with 16KB page size..."
16+
./build_android.sh
17+
else
18+
exit 1
19+
fi
20+
fi
21+
22+
echo
23+
echo "=== Checking ELF alignment of built libraries ==="
24+
25+
# Check the built libraries using the external script
26+
./check_elf_alignment.sh target/android/jniLibs/
27+
28+
echo
29+
echo "=== Test Complete ==="
30+
if [ $? -eq 0 ]; then
31+
echo "✅ All libraries are properly aligned for 16KB page size!"
32+
else
33+
echo "❌ Some libraries are not properly aligned for 16KB page size."
34+
exit 1
35+
fi

0 commit comments

Comments
 (0)