gpt4 book ai didi

ios - 我如何从 __TEXT 中分离出来并进入 mach-o 二进制文件的新段?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:21:47 25 4
gpt4 key购买 nike

如何将部分从 __TEXT 段中移出并移入 mach-o 二进制文件的新段中?我问的原因是我试图让我的 iPhone 应用程序更小,并且 iOS App Store 在压缩之前加密了它的 __TEXT 部分,因此它根本不会压缩。如果我可以将所有不可执行的部分移出该段并移至一个新的只读段中,那么我可以将应用程序的大小减少大约 9%。

最佳答案

在链接器级别

正如@meisel 回答中已经提到的,这可以通过以下方式完成: enter image description here所以要设置的值是:

-Wl,-rename_section,__TEXT,__sectionName,__NEW_SEGMENT_NAME,__newSectionName

如果段权限需要调整

-segprot,__NEW_SEGMENT_NAME,rx,rx

小心 rwx 的可执行代码,如果检测到 rxw 运行代码,应用程序将立即被 iOS XNU 内核杀死。它仍然适用于 x86-64 模拟器。本博post涵盖了一些使用示例。

在函数原型(prototype)层面

假设您正在使用 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

允许的部分属性:

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
@interface YourClass
- (void) foo:(NSInteger)someArg __attribute__ ( (section ("__TEXT, __customSection") ));
@end

@interface YourClass ()
- (void) foo2:(NSInteger)someArg __attribute__ ( (section ("__TEXT, __customSection") ));
@end

[新] 在函数/方法级别使用范围

#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

请记住,您很容易破坏东西,MachOView 是检查二进制文件的便捷工具。自定义 ("__TEXT,__cstring") 替换很可能是您的最佳选择。

编辑:默认情况下,不存在的段将作为可读和可写的(就像 __DATA)发出,因此这不适用于可执行代码。

如果您想探索内联程序集路径,请在此处记录:https://developer.apple.com/library/content/documentation/DeveloperTools/Reference/Assembler/040-Assembler_Directives/asm_directives.html

关于ios - 我如何从 __TEXT 中分离出来并进入 mach-o 二进制文件的新段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47896174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com