如何将部分移出__TEXT
段并转换为mach-o二进制文件的新段?我问的原因是我试图让我的iPhone应用程序更小,并且iOS App Store在压缩之前加密它的__TEXT
段,因此它根本不压缩。如果我可以将所有非可执行部分移出该段并进入一个新的只读段,那么我可以将我的应用程序的大小减少大约9%。
ld
的-rename_section
旗帜完成了这项工作。
正如@meisel中已经提到的那样,这可以通过以下方式完成:因此设置的值将是:
-Wl,-rename_section,__TEXT,__sectionName,__NEW_SEGMENT_NAME,__newSectionName
如果需要调整段权限
-segprot,__NEW_SEGMENT_NAME,rx,rx
小心rwx
的可执行代码,如果检测到rxw
运行代码,应用程序将立即被iOS XNU内核杀死。它仍然适用于模拟器。这篇博客post涵盖了一些用法示例。
假设您使用的是Objective-C(或C),您可以使用__attribute__ section(...)
。
句法:
__attribute__ ((section("segmentName,sectionName,[optional]sectionType,[optional]sectionAttribute,[optional]stubSize")))
从https://opensource.apple.com/source/clang/clang-137/src/lib/MC/MCSectionMachO.cpp允许sectionTypes
:
{ "regular", "S_REGULAR" }, // 0x00
{ 0, "S_ZEROFILL" }, // 0x01
{ "cstring_literals", "S_CSTRING_LITERALS" }, // 0x02
{ "4byte_literals", "S_4BYTE_LITERALS" }, // 0x03
{ "8byte_literals", "S_8BYTE_LITERALS" }, // 0x04
{ "literal_pointers", "S_LITERAL_POINTERS" }, // 0x05
{ "non_lazy_symbol_pointers", "S_NON_LAZY_SYMBOL_POINTERS" }, // 0x06
{ "lazy_symbol_pointers", "S_LAZY_SYMBOL_POINTERS" }, // 0x07
{ "symbol_stubs", "S_SYMBOL_STUBS" }, // 0x08
{ "mod_init_funcs", "S_MOD_INIT_FUNC_POINTERS" }, // 0x09
{ "mod_term_funcs", "S_MOD_TERM_FUNC_POINTERS" }, // 0x0A
{ "coalesced", "S_COALESCED" }, // 0x0B
{ 0, /*FIXME??*/ "S_GB_ZEROFILL" }, // 0x0C
{ "interposing", "S_INTERPOSING" }, // 0x0D
{ "16byte_literals", "S_16BYTE_LITERALS" }, // 0x0E
{ 0, /*FIXME??*/ "S_DTRACE_DOF" }, // 0x0F
{ 0, /*FIXME??*/ "S_LAZY_DYLIB_SYMBOL_POINTERS" }, // 0x10
{ "thread_local_regular", "S_THREAD_LOCAL_REGULAR" }, // 0x11
{ "thread_local_zerofill", "S_THREAD_LOCAL_ZEROFILL" }, // 0x12
{ "thread_local_variables", "S_THREAD_LOCAL_VARIABLES" }, // 0x13
{ "thread_local_variable_pointers",
"S_THREAD_LOCAL_VARIABLE_POINTERS" }, // 0x14
{ "thread_local_init_function_pointers",
"S_THREAD_LOCAL_INIT_FUNCTION_POINTERS"}, // 0x15
允许的section attributes
:
ENTRY("pure_instructions", S_ATTR_PURE_INSTRUCTIONS)
ENTRY("no_toc", S_ATTR_NO_TOC)
ENTRY("strip_static_syms", S_ATTR_STRIP_STATIC_SYMS)
ENTRY("no_dead_strip", S_ATTR_NO_DEAD_STRIP)
ENTRY("live_support", S_ATTR_LIVE_SUPPORT)
ENTRY("self_modifying_code", S_ATTR_SELF_MODIFYING_CODE)
ENTRY("debug", S_ATTR_DEBUG)
ENTRY("" /*FIXME*/, S_ATTR_SOME_INSTRUCTIONS)
ENTRY("" /*FIXME*/, S_ATTR_EXT_RELOC)
ENTRY("" /*FIXME*/, S_ATTR_LOC_RELOC)
一些例子:
//Variable in brand new segment & section, segment VM access defaults to read/write
int intInCustomPlace __attribute__ ( (section ("__DATA2,__data2") ));
//Read only string constant outside of __TEXT in readonly __LINKEDIT
char *kString __attribute__ ( (section ("__LINKEDIT,__customSection") )) = "value";
//C function in custom segment & section
void foo() __attribute__ ( (section ("__TEXT_EXEC,__customSection") ));
//obj-c method in custom section
- (void) foo:(NSInteger)someArg __attribute__ ( (section ("__TEXT, __customSection") ));
请记住,您可以轻松破解内容,MachOView
是检查二进制文件的便捷工具。定制("__TEXT,__cstring")
更换很可能是你最好的一击。
编辑:默认情况下,一个不存在的段将以可读写的形式发出(就像__DATA
一样),因此这对可执行代码不起作用。
如果您想探索内联汇编路径,请在此处记录:https://developer.apple.com/library/content/documentation/DeveloperTools/Reference/Assembler/040-Assembler_Directives/asm_directives.html