Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Efreak/9e244392bbc472294b1bff314bd450cb to your computer and use it in GitHub Desktop.
Save Efreak/9e244392bbc472294b1bff314bd450cb to your computer and use it in GitHub Desktop.
From 0c30b3553af1992854e82eca00177183848cf94b Mon Sep 17 00:00:00 2001
From: Efreak <efreak@users.noreply.github.com>
Date: Sat, 29 Apr 2023 12:53:34 -0700
Subject: [PATCH] add alignment fixer step and align_fix.py
https://github.com/lzhiyong/termux-ndk/raw/master/patches/align_fix.py
---
Makefile | 1 +
align_fix.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+)
create mode 100644 align_fix.py
diff --git a/Makefile b/Makefile
index 4c751fa..e9f24e5 100644
--- a/Makefile
+++ b/Makefile
@@ -229,6 +229,7 @@ installdirs:
test -e $(DESTDIR)$(MAN_DIR) || $(MKDIR) $(DESTDIR)$(MAN_DIR)
install: $(PROGRAM_NAME) installdirs
+ python align_fix.py jdupes
$(INSTALL_PROGRAM) $(PROGRAM_NAME)$(SUFFIX) $(DESTDIR)$(BIN_DIR)/$(PROGRAM_NAME)$(SUFFIX)
$(INSTALL_DATA) $(PROGRAM_NAME).1 $(DESTDIR)$(MAN_DIR)/$(PROGRAM_NAME).$(MAN_EXT)
diff --git a/align_fix.py b/align_fix.py
new file mode 100644
index 0000000..f46c67c
--- /dev/null
+++ b/align_fix.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+
+import struct
+import sys
+import os
+
+if len(sys.argv) < 2:
+ print('Usage: ' + os.path.basename(sys.argv[0]) + ' input_file')
+ exit()
+
+with open(sys.argv[1], 'r+b') as f:
+ f.seek(0)
+ hdr = f.read(16)
+ if hdr[0] != 0x7f or hdr[1] != ord('E') or hdr[2] != ord('L') or hdr[3] != ord('F'):
+ raise Exception('Not an elf file')
+
+ if hdr[4] == 1:
+ # 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]
+ print('Found TLS segment with align = ' + str(align))
+ if (align < 32):
+ print('TLS segment is underaligned, patching')
+ f.seek(-4, 1)
+ f.write(struct.pack('<I', 32))
+
+ elif hdr[4] == 2:
+ # 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]
+ print('Found TLS segment with align = ' + str(align))
+ if (align < 64):
+ print('TLS segment is underaligned, patching')
+ f.seek(-8, 1)
+ f.write(struct.pack('<H', 64))
+
+ else:
+ raise Exception('Unknown file class')
--
2.40.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment