insert_dylib
============
Command line utility for inserting a dylib load command into a Mach-O binary.
Does the following (to each arch if the binary is fat):
- Adds a `LC_LOAD_DYLIB` load command to the end of the load commands
- Increments the mach header's `ncmds` and adjusts its `sizeofcmds`
- ([Removes code signature if present](#removing-code-signature))
Usage
-----
```
Usage: insert_dylib dylib_path binary_path [new_binary_path]
Option flags: --inplace --weak --overwrite --strip-codesig --no-strip-codesig --all-yes
```
`insert_dylib` inserts a load command to load the `dylib_path` in `binary_path`.
Unless `--inplace` option is specified, `insert_dylib` will produce a new binary at `new_binary_path`.
If neither `--inplace` nor `new_binary_path` is specified, the output binary will be located at the same location as the input binary with `_patched` prepended to the name.
If the `--weak` option is specified, `insert_dylib` will insert a `LC_LOAD_WEAK_DYLIB` load command instead of `LC_LOAD_DYLIB`.
### Example
```
$ cat > test.c
int main(void) {
printf("Testing\n");
return 0;
}
^D
$ clang test.c -o test &> /dev/null
$ insert_dylib /usr/lib/libfoo.dylib test
The provided dylib path doesn't exist. Continue anyway? [y/n] y
Added LC_LOAD_DYLIB to test_patched
$ ./test
Testing
$ ./test_patched
dyld: Library not loaded: /usr/lib/libfoo.dylib
Referenced from: /Users/Tyilo/./test_patched
Reason: image not found
Trace/BPT trap: 5
```
#### `otool` `diff` between original and patched binary
```
$ diff -u <(otool -hl test) <(otool -hl test_patched)
--- /dev/fd/63 2014-07-30 04:08:40.000000000 +0200
+++ /dev/fd/62 2014-07-30 04:08:40.000000000 +0200
@@ -1,7 +1,7 @@
-test:
+test_patched:
Mach header
magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
- 0xfeedfacf 16777223 3 0x80 2 16 1296 0x00200085
+ 0xfeedfacf 16777223 3 0x80 2 17 1344 0x00200085
Load command 0
cmd LC_SEGMENT_64
cmdsize 72
@@ -231,3 +231,10 @@
cmdsize 16
dataoff 8296
datasize 64
+Load command 16
+ cmd LC_LOAD_DYLIB
+ cmdsize 48
+ name /usr/lib/libfoo.dylib (offset 24)
+ time stamp 0 Thu Jan 1 01:00:00 1970
+ current version 0.0.0
+compatibility version 0.0.0
```
#### `--weak` option
```
$ insert_dylib --weak /usr/lib/libfoo.dylib test test_patched2
The provided dylib path doesn't exist. Continue anyway? [y/n] y
Added LC_LOAD_WEAK_DYLIB to test_patched2
$ ./test_patched2
Testing
```
Removing code signature
----
To remove the code signature it is enough to delete the `LC_CODE_SIGNATURE` load command and fixup the mach header's `ncmds` and `sizeofcmds`, assuming it is the last load command.
However if you just do this `codesign_allocate` (used by `codesign` and `ldid`) will fail with the error:
```
.../codesign_allocate: file not in an order that can be processed (link edit information does not fill the __LINKEDIT segment):
```
To fix this `insert_dylib` assumes that the code signature that `LC_CODE_SIGNATURE` is in the end of the `__LINKEDIT` segment and the that the segment is in the end of the architectures slice.
It then truncate that slice to remove the code signature part of the `__LINKEDIT` segment. It also updates the `LC_SEGMENT` (or `LC_SEGMENT64`) load command for the `__LINKEDIT` segment from the new file size. If the binary is fat we also update the size and we might also move the slice and so the offset should also be updated.
After removing the code signature from the `__LINKEDIT` segment, the last thing in that segment is typically the string table. As the code signature seems to be aligned by `0x10`, and so after removing the code signature, nothing points to the padding at the end of the segment, which `codesign_allocate` doesn't like either. To fix this we just increase the size of the string table in the `LC_SYMTAB` command so it also includes the padding.
Todo
----
- Improved checking for free space to insert the new load command
- Allow removal of `LC_CODE_SIGNATURE` if it isn't the last load command
- Remove `__RESTRICT,__restrict` if not enough space (suggesion by dirkg)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Command line utility for inserting a dylib load command into a Mach-O binary. Does the following (to each arch if the binary is fat): Adds a LC_LOAD_DYLIB load command to the end of the load commands Increments the mach header's ncmds and adjusts its sizeofcmds (Removes code signature if present)
资源推荐
资源详情
资源评论
收起资源包目录
insert_dylib-master.zip (10个子文件)
insert_dylib-master
.gitignore 34B
insert_dylib_tests
test.c.foo 167B
Info.plist 748B
insert_dylib_tests.m 6KB
.DS_Store 6KB
README.md 4KB
insert_dylib
main.c 14KB
insert_dylib.xcodeproj
project.xcworkspace
contents.xcworkspacedata 157B
project.pbxproj 12KB
products
insert_dylib 24KB
共 10 条
- 1
资源评论
笨小孩2001
- 粉丝: 3
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功