CẬP NHẬT: Trả lời câu dưới đây trong câu hỏi, nhờ con trỏ Greg Parker ...byte sụp đổ đọc từ getsectbyname
Tôi đã tải lên một dự án mẫu ở đây, nhưng tôi sẽ mô tả nó là tốt: https://github.com/tewha/getsectbyname-crash
tôi nhận được một sự cố từ tệp thực thi (chỉ có 64 bit) của tôi, nhưng không phải khi chạy từ Xcode. Nếu tôi chạy nó từ Thiết bị đầu cuối hoặc Cụ, tuy nhiên, lỗi đó sẽ bị treo.
Đây là không sự cố về gỡ lỗi và phát hành cấu hình; chạy Debug thực thi trong Terminal cũng bị treo. Chạy bản phát hành Bản phát hành từ Xcode hoạt động.
Tôi đang cố gắng đọc một phần gây ra từ thực thi Mach-O, được liên kết vào ứng dụng qua 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);
Kết quả trông như thế này (tôi đã đơn giản hóa này một chút để loại bỏ tem ngày/giờ, quá trình 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
bất cứ ai có thể cho tôi biết làm thế nào để sửa lỗi này?
Câu trả lời:
#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;
}
Tôi đã đăng câu hỏi về cách thực hiện tương tự trong Swift: http://stackoverflow.com/questions/39732016/calling-getsectiondata-from-swift. Bất kỳ trợ giúp nào được đánh giá cao. –