【发布时间】:2015-03-11 04:50:28
【问题描述】:
更新:感谢 Greg Parker 的指点,回答下面的问题……
我在这里上传了一个示例项目,但我也会对其进行描述:https://github.com/tewha/getsectbyname-crash
我的(仅限 64 位)可执行文件崩溃了,但从 Xcode 运行时却没有。但是,如果我从 Terminal 或 Instruments 运行它,它会崩溃。
这不是调试与发布配置问题;在终端中运行 Debug 可执行文件也会崩溃。从 Xcode 运行 Release 可执行文件。
我正在尝试从 Mach-O 可执行文件中读取一个 inflict 部分,通过 CREATE_INFOPLIST_SECTION_IN_BINARY = YES 链接到应用程序。
const struct section_64 *plistSection = getsectbyname("__TEXT", "__info_plist");
NSLog(@"Found a section %s, %s", plistSection->segname, plistSection->sectname);
void *ptr = ((void *)plistSection->addr);
uint64_t size = plistSection->size;
NSLog(@"It has %zd bytes at %tx", size, plistSection->addr);
NSLog(@"Allocating %zd bytes", size);
void *buffer = malloc(size);
NSLog(@"Moving %zd bytes", size);
NSLog(@"(Crashes when doing the memmove.)");
memmove(buffer, ptr, size);
NSLog(@"Freeing %zd bytes", size);
free(buffer);
输出看起来像这样(我已经简化了一点以删除日期/时间戳、进程 ID):
bash-4.3$ ./getsectbyname
getsectbyname Found a section __TEXT, __info_plist
getsectbyname It has 658 bytes at 100000d07
getsectbyname Allocating 658 bytes
getsectbyname Moving 658 bytes
getsectbyname (Crashes when doing the memmove.)
Segmentation fault: 11
谁能告诉我如何解决这个问题?
答案:
#import <Foundation/Foundation.h>
#include <mach-o/getsect.h>
#include <mach-o/ldsyms.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSError *e;
unsigned long size;
void *ptr = getsectiondata(&_mh_execute_header, "__TEXT",
"__info_plist", &size);
NSData *plistData = [NSData dataWithBytesNoCopy:ptr length:size
freeWhenDone:NO];
NSPropertyListFormat format;
NSDictionary *infoPlistContents =
[NSPropertyListSerialization propertyListWithData:plistData
options:NSPropertyListImmutable format:&format error:&e];
NSLog(@"The value for Key is %@", infoPlistContents[@"Key"]);
}
return 0;
}
【问题讨论】:
-
我发布了一个关于在 Swift 中做同样事情的问题:stackoverflow.com/questions/39732016/…。任何帮助表示赞赏。