Skip to content

Commit 4941a63

Browse files
committed
Incorporate @kvaster's Android Q patch into build script (fixes #370)
Thanks for the contributon!
1 parent 180df94 commit 4941a63

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

syncthing/build-syncthing.py

+54-16
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@
2222
'goarch': 'arm',
2323
'jni_dir': 'armeabi',
2424
'cc': 'arm-linux-androideabi-clang',
25-
'patch_tls_underalign_offset': '0x14c',
26-
'patch_tls_underalign_old_byte': '04',
27-
'patch_tls_underalign_new_byte': '20',
25+
'patch_underaligned_tls': 'yes',
2826
},
2927
{
3028
'arch': 'arm64',
3129
'goarch': 'arm64',
3230
'jni_dir': 'arm64-v8a',
3331
'cc': 'aarch64-linux-android-clang',
34-
'patch_tls_underalign_offset': '0x1c0',
35-
'patch_tls_underalign_old_byte': '08',
36-
'patch_tls_underalign_new_byte': '40',
32+
'patch_underaligned_tls': 'yes',
3733
'min_sdk': 21,
3834
},
3935
{
@@ -253,6 +249,55 @@ def install_ndk():
253249
os.environ["ANDROID_NDK_HOME"] = ndk_home_path
254250

255251

252+
def artifact_patch_underaligned_tls(artifact_fullfn):
253+
import struct
254+
255+
with open(artifact_fullfn, 'r+b') as f:
256+
f.seek(0)
257+
hdr = f.read(16)
258+
if hdr[0] != 0x7f or hdr[1] != ord('E') or hdr[2] != ord('L') or hdr[3] != ord('F'):
259+
print('artifact_patch_underaligned_tls: Not an ELF file')
260+
return None
261+
262+
if hdr[4] == 1:
263+
# 32 bit code
264+
f.seek(28)
265+
offset = struct.unpack('<I', f.read(4))[0]
266+
f.seek(42)
267+
phsize = struct.unpack('<H', f.read(2))[0]
268+
phnum = struct.unpack('<H', f.read(2))[0]
269+
for i in range(0, phnum):
270+
f.seek(offset + i * phsize)
271+
t = struct.unpack('<I', f.read(4))[0]
272+
if t == 7:
273+
f.seek(28 - 4, 1)
274+
align = struct.unpack('<I', f.read(4))[0]
275+
if (align < 32):
276+
print('artifact_patch_underaligned_tls: Patching underaligned TLS segment from ' + str(align) + ' to 32')
277+
f.seek(-4, 1)
278+
f.write(struct.pack('<I', 32))
279+
280+
elif hdr[4] == 2:
281+
# 64 bit code
282+
f.seek(32)
283+
offset = struct.unpack('<Q', f.read(8))[0]
284+
f.seek(54)
285+
phsize = struct.unpack('<H', f.read(2))[0]
286+
phnum = struct.unpack('<H', f.read(2))[0]
287+
for i in range(0, phnum):
288+
f.seek(offset + i * phsize)
289+
t = struct.unpack('<I', f.read(4))[0]
290+
if t == 7:
291+
f.seek(48 - 4, 1)
292+
align = struct.unpack('<Q', f.read(8))[0]
293+
if (align < 64):
294+
print('artifact_patch_underaligned_tls: Patching underaligned TLS segment from ' + str(align) + ' to 64')
295+
f.seek(-8, 1)
296+
f.write(struct.pack('<H', 64))
297+
298+
else:
299+
print('artifact_patch_underaligned_tls: Unknown ELF file class')
300+
256301

257302
#
258303
# BUILD SCRIPT MAIN.
@@ -366,19 +411,12 @@ def install_ndk():
366411
# Determine path of source artifact
367412
source_artifact = os.path.join(syncthing_dir, 'syncthing')
368413

369-
# Path artifact to work around golang bug.
414+
# Patch artifact to work around golang bug.
370415
# See issues:
371416
# - https://github.com/Catfriend1/syncthing-android/issues/370
372417
# - https://github.com/golang/go/issues/29674
373-
if 'patch_tls_underalign_offset' in target:
374-
fh = open(source_artifact, "r+b")
375-
fh.seek(int(target['patch_tls_underalign_offset'], 16))
376-
print('Checking if tls path is applicable ...')
377-
if fh.read(1) == bytes.fromhex(target['patch_tls_underalign_old_byte']):
378-
print('Patching tls alignment ...')
379-
fh.seek(int(target['patch_tls_underalign_offset'], 16))
380-
fh.write(bytes.fromhex(target['patch_tls_underalign_new_byte']))
381-
fh.close()
418+
if 'patch_underaligned_tls' in target:
419+
artifact_patch_underaligned_tls(source_artifact)
382420

383421
# Copy compiled binary to jniLibs folder
384422
target_dir = os.path.join(project_dir, 'app', 'src', 'main', 'jniLibs', target['jni_dir'])

0 commit comments

Comments
 (0)