首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我如何从__TEXT中分割出一个新的mach-o二进制代码段?

我如何从__TEXT中分割出一个新的mach-o二进制代码段?
EN

Stack Overflow用户
提问于 2017-12-19 22:33:06
回答 2查看 2.2K关注 0票数 4

我如何将部分从__TEXT段移出,并进入一个新的mach-o二进制文件段?我提出这个问题的原因是,我试图使我的iPhone应用程序变小,而iOS应用商店在压缩之前对它的__TEXT段进行加密,这样它就完全不压缩了。如果我可以将所有不可执行的部分从该段移出并进入一个新的只读段,那么我可以将我的应用程序的大小减少约9%。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-27 23:27:18

ld-rename_section标志完成了这项工作。

票数 3
EN

Stack Overflow用户

发布于 2017-12-23 09:03:27

链接器水平

正如@meisel答复中已经提到的,这可以通过以下方式实现:

因此,要设置的值是:

代码语言:javascript
运行
复制
-Wl,-rename_section,__TEXT,__sectionName,__NEW_SEGMENT_NAME,__newSectionName

如果分段权限需要调整

代码语言:javascript
运行
复制
-segprot,__NEW_SEGMENT_NAME,rx,rx

小心rwx的可执行代码,如果检测到rxw运行代码,应用程序将立即被iOS XNU内核杀死。对x86-64模拟器来说还是不错的。这个博客帖子涵盖了一些使用示例。

论功能原型层次

假设您使用的是Objective(或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

代码语言:javascript
运行
复制
  { "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

代码语言:javascript
运行
复制
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)

下面是一些例子:

代码语言:javascript
运行
复制
//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

新功能/方法级使用范围

代码语言:javascript
运行
复制
#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一样)发出,因此对可执行代码不起作用。

如果您想探索内联程序集路径,请在这里文档化:directives.html

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47896174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档