ios - 如何将 __TEXT 分段并放入 mach-o 二进制文件的新段?
<p><p>如何将部分从 <code>__TEXT</code> 段移到 mach-o 二进制文件的新段中?我问的原因是我试图让我的 iPhone 应用程序更小,并且 iOS App Store 在压缩之前对其的 <code>__TEXT</code> 段进行加密,使其根本不压缩。如果我可以将所有不可执行的部分移出该段并进入一个新的只读段,那么我可以将我的应用程序的大小减少大约 9%。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><h1>在链接器级别</h1>
<p>正如@meisel 回答中已经提到的,这可以通过以下方式完成:
<a href="/image/7nlPO.png" rel="noreferrer noopener nofollow"><img src="/image/7nlPO.png" alt="enter image description here"/></a>
所以要设置的值是:</p>
<pre><code>-Wl,-rename_section,__TEXT,__sectionName,__NEW_SEGMENT_NAME,__newSectionName
</code></pre>
<p>如果段权限需要调整</p>
<pre><code>-segprot,__NEW_SEGMENT_NAME,rx,rx
</code></pre>
<p>小心 <code>rwx</code> 执行代码,如果检测到 <code>rxw</code> 运行代码,应用程序将立即被 iOS XNU 内核杀死。 x86-64 模拟器还是可以的。本博客 <a href="https://medium.com/@michael.eisel/one-trick-to-drastically-reduce-your-ios-apps-downlsize-ae68aad0d369" rel="noreferrer noopener nofollow">post</a>涵盖了一些使用示例。</p>
<h1>在函数原型(prototype)层面</h1>
<p>假设您使用的是 <strong>Objective-C</strong>(或 <strong>C</strong>),您可以使用 <code>__attribute__ section(...)</code>。 <br/>
<strong>语法:</strong><br/>
<code>__attribute__ ((section("segmentName,sectionName,sectionType,sectionAttribute,stubSize")))</code><br/>
来自 <a href="https://opensource.apple.com/source/clang/clang-137/src/lib/MC/MCSectionMachO.cpp" rel="noreferrer noopener nofollow">https://opensource.apple.com/source/clang/clang-137/src/lib/MC/MCSectionMachO.cpp</a>允许的 <code>sectionTypes</code>:</p>
<pre><code>{ "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
</code></pre>
<p>允许的<code>部分属性</code>:</p>
<pre><code>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)
</code></pre>
<p>一些例子:</p>
<pre><code>//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
@interface YourClass
- (void) foo:(NSInteger)someArg__attribute__ ( (section ("__TEXT, __customSection") ));
@end
@interface YourClass ()
- (void) foo2:(NSInteger)someArg__attribute__ ( (section ("__TEXT, __customSection") ));
@end
</code></pre>
<h1>[新] 在函数/方法级别使用范围</h1>
<pre><code>#pragma clang attribute push(__attribute__ ( (section ("__TEXT_EXEC,__customSection") )), apply_to=objc_method)
@interface AppDelegate ()
-(void)test;
-(void)test2;
@end
@interface AppDelegate ()
-(void)test3;
-(void)test4;
@end
#pragma clang attribute pop
#pragma clang attribute push(__attribute__ ( (section ("__TEXT,__customTextSection") )), apply_to=objc_method)
@interface AppDelegate
-(void)test5;
-(void)test6;
@end
#pragma clang attribute pop
#pragma clang attribute push(__attribute__ ( (section ("__TEXT_EXEC,__customSection") )), apply_to=function)
void func1();
void func2();
#pragma clang attribute pop
</code></pre>
<p>请记住,您可以轻松破坏内容,<code>MachOView</code> 是检查二进制文件的便捷工具。自定义 <code>("__TEXT,__cstring")</code> 替换很可能是您的最佳选择。</p>
<p>编辑:默认情况下不存在的段将作为可读和可写发出(就像 <code>__DATA</code> 一样)因此这不适用于可执行代码。</p>
<p>如果您想探索这里记录的内联汇编路径:<a href="https://developer.apple.com/library/content/documentation/DeveloperTools/Reference/Assembler/040-Assembler_Directives/asm_directives.html" rel="noreferrer noopener nofollow">https://developer.apple.com/library/content/documentation/DeveloperTools/Reference/Assembler/040-Assembler_Directives/asm_directives.html</a> </p></p>
<p style="font-size: 20px;">关于ios - 如何将 __TEXT 分段并放入 mach-o 二进制文件的新段?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47896174/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47896174/
</a>
</p>
页:
[1]