1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 06:11:19 +00:00

Patch wrong tls alignment for Android Q compatibility (fixes #370) (#374)

* Patch go wrong tls alignment for Android Q compatibility (fixes #370)

* Remove wrong x86 patch

* Incorporate @kvaster's Android Q patch into build script (fixes #370)

Thanks for the contributon!

* Update build script for Python 2 compatibility

* Second attempt for python 2
This commit is contained in:
Catfriend1 2019-03-17 13:58:50 +01:00 committed by GitHub
parent 2486612270
commit 3a188d457c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,12 +22,14 @@ BUILD_TARGETS = [
'goarch': 'arm', 'goarch': 'arm',
'jni_dir': 'armeabi', 'jni_dir': 'armeabi',
'cc': 'arm-linux-androideabi-clang', 'cc': 'arm-linux-androideabi-clang',
'patch_underaligned_tls': 'yes',
}, },
{ {
'arch': 'arm64', 'arch': 'arm64',
'goarch': 'arm64', 'goarch': 'arm64',
'jni_dir': 'arm64-v8a', 'jni_dir': 'arm64-v8a',
'cc': 'aarch64-linux-android-clang', 'cc': 'aarch64-linux-android-clang',
'patch_underaligned_tls': 'yes',
'min_sdk': 21, 'min_sdk': 21,
}, },
{ {
@ -247,6 +249,55 @@ def install_ndk():
os.environ["ANDROID_NDK_HOME"] = ndk_home_path os.environ["ANDROID_NDK_HOME"] = ndk_home_path
def artifact_patch_underaligned_tls(artifact_fullfn):
import struct
with open(artifact_fullfn, 'r+b') as f:
f.seek(0)
hdr = struct.unpack('16c', f.read(16))
if hdr[0] != b'\x7f' or hdr[1] != b'E' or hdr[2] != b'L' or hdr[3] != b'F':
print('artifact_patch_underaligned_tls: Not an ELF file')
return None
if hdr[4] == b'\x01':
# 32 bit code
f.seek(28)
offset = struct.unpack('<I', f.read(4))[0]
f.seek(42)
phsize = struct.unpack('<H', f.read(2))[0]
phnum = struct.unpack('<H', f.read(2))[0]
for i in range(0, phnum):
f.seek(offset + i * phsize)
t = struct.unpack('<I', f.read(4))[0]
if t == 7:
f.seek(28 - 4, 1)
align = struct.unpack('<I', f.read(4))[0]
if (align < 32):
print('artifact_patch_underaligned_tls: Patching underaligned TLS segment from ' + str(align) + ' to 32')
f.seek(-4, 1)
f.write(struct.pack('<I', 32))
elif hdr[4] == b'\x02':
# 64 bit code
f.seek(32)
offset = struct.unpack('<Q', f.read(8))[0]
f.seek(54)
phsize = struct.unpack('<H', f.read(2))[0]
phnum = struct.unpack('<H', f.read(2))[0]
for i in range(0, phnum):
f.seek(offset + i * phsize)
t = struct.unpack('<I', f.read(4))[0]
if t == 7:
f.seek(48 - 4, 1)
align = struct.unpack('<Q', f.read(8))[0]
if (align < 64):
print('artifact_patch_underaligned_tls: Patching underaligned TLS segment from ' + str(align) + ' to 64')
f.seek(-8, 1)
f.write(struct.pack('<H', 64))
else:
print('artifact_patch_underaligned_tls: Unknown ELF file class')
# #
# BUILD SCRIPT MAIN. # BUILD SCRIPT MAIN.
@ -313,6 +364,7 @@ syncthingVersion = subprocess.check_output([
'--always' '--always'
]).strip(); ]).strip();
syncthingVersion = syncthingVersion.decode().replace("rc", "preview"); syncthingVersion = syncthingVersion.decode().replace("rc", "preview");
print('Building syncthing version', syncthingVersion);
for target in BUILD_TARGETS: for target in BUILD_TARGETS:
target_min_sdk = str(target.get('min_sdk', min_sdk)) target_min_sdk = str(target.get('min_sdk', min_sdk))
@ -349,7 +401,6 @@ for target in BUILD_TARGETS:
'CGO_ENABLED': '1', 'CGO_ENABLED': '1',
}) })
print('Building syncthing version', syncthingVersion);
subprocess.check_call([go_bin, 'mod', 'download'], cwd=syncthing_dir) subprocess.check_call([go_bin, 'mod', 'download'], cwd=syncthing_dir)
subprocess.check_call([ subprocess.check_call([
go_bin, 'run', 'build.go', '-goos', 'android', '-goarch', target['goarch'], go_bin, 'run', 'build.go', '-goos', 'android', '-goarch', target['goarch'],
@ -357,6 +408,16 @@ for target in BUILD_TARGETS:
'-version', syncthingVersion '-version', syncthingVersion
] + pkg_argument + ['-no-upgrade', 'build'], env=environ, cwd=syncthing_dir) ] + pkg_argument + ['-no-upgrade', 'build'], env=environ, cwd=syncthing_dir)
# Determine path of source artifact
source_artifact = os.path.join(syncthing_dir, 'syncthing')
# Patch artifact to work around golang bug.
# See issues:
# - https://github.com/Catfriend1/syncthing-android/issues/370
# - https://github.com/golang/go/issues/29674
if 'patch_underaligned_tls' in target:
artifact_patch_underaligned_tls(source_artifact)
# Copy compiled binary to jniLibs folder # Copy compiled binary to jniLibs folder
target_dir = os.path.join(project_dir, 'app', 'src', 'main', 'jniLibs', target['jni_dir']) target_dir = os.path.join(project_dir, 'app', 'src', 'main', 'jniLibs', target['jni_dir'])
if not os.path.isdir(target_dir): if not os.path.isdir(target_dir):