|
22 | 22 | 'goarch': 'arm',
|
23 | 23 | 'jni_dir': 'armeabi',
|
24 | 24 | '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', |
28 | 26 | },
|
29 | 27 | {
|
30 | 28 | 'arch': 'arm64',
|
31 | 29 | 'goarch': 'arm64',
|
32 | 30 | 'jni_dir': 'arm64-v8a',
|
33 | 31 | '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', |
37 | 33 | 'min_sdk': 21,
|
38 | 34 | },
|
39 | 35 | {
|
@@ -253,6 +249,55 @@ def install_ndk():
|
253 | 249 | os.environ["ANDROID_NDK_HOME"] = ndk_home_path
|
254 | 250 |
|
255 | 251 |
|
| 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 | + |
256 | 301 |
|
257 | 302 | #
|
258 | 303 | # BUILD SCRIPT MAIN.
|
@@ -366,19 +411,12 @@ def install_ndk():
|
366 | 411 | # Determine path of source artifact
|
367 | 412 | source_artifact = os.path.join(syncthing_dir, 'syncthing')
|
368 | 413 |
|
369 |
| - # Path artifact to work around golang bug. |
| 414 | + # Patch artifact to work around golang bug. |
370 | 415 | # See issues:
|
371 | 416 | # - https://github.com/Catfriend1/syncthing-android/issues/370
|
372 | 417 | # - 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) |
382 | 420 |
|
383 | 421 | # Copy compiled binary to jniLibs folder
|
384 | 422 | target_dir = os.path.join(project_dir, 'app', 'src', 'main', 'jniLibs', target['jni_dir'])
|
|
0 commit comments