linux - 为什么编辑 .fini_array 不会改变程序的进程?
问题描述
我试图了解编辑 of.fini_array
将如何导致程序流程发生变化,但似乎更改此条目不会导致任何更改。我main
的是:
void cleanup() __attribute__((destructor));
int main() {
printf("In main\n");
exit(1);
}
void cleanup(){
printf("Cleanning Up\n");
}
void fini_exec(){
printf("FINI\n");
}
运行nm test
:000000000000119e T fini_exec
而cleenup
:0000000000001187 T cleanup
运行objdump -s -j .fini_array test
:
Contents of section .fini_array:
3db0 20110000 00000000 87110000 00000000 ...............
使用十六进制编辑器并转到2db0
我从中获得的字节readelf
并在编辑再次运行后编辑87110000
为9e110000
(印度语)objdump
Contents of section .fini_array:
3db0 20110000 00000000 9e110000 00000000 ...............
然后保存并重新运行仍然会导致cleanup
运行,我不知道为什么。不.fini_array
应该在后面运行,.fini
但似乎还有其他cleanup
地方的另一个参考导致fini_exec
无法运行。
解决方案
void cleanup() __attribute__((destructor));
我分别用and制作了两个单独的可执行文件 a.out.1 和 a.out.2 void fini_exec() __attribute__((destructor));
:
#include <stdio.h>
#include <stdlib.h>
void cleanup() __attribute__((destructor));
//void fini_exec() __attribute__((destructor));
int main() {
printf("In main\n");
exit(1);
}
void cleanup(){
printf("Cleanning Up\n");
}
void fini_exec(){
printf("FINI\n");
}
我生成了十六进制转储:
$ hexdump a.out.1 > a.out.1.hex
$ hexdump a.out.2 > a.out.2.hex
我比较了十六进制转储,可以看到标记为析构函数的函数的偏移量(0x119e 和 0x1187)出现在 2 个位置(偏移量 0x580 和 0x2db8):
$ diff a.out.1.hex a.out.2.hex
55,56c55,56
< 0000360 0003 0000 4e47 0055 5b7f a631 6798 4fc1
< 0000370 d865 85fc 1cc6 c187 eaab 06ab 0004 0000
---
> 0000360 0003 0000 4e47 0055 4bec d49a 80fc b24e
> 0000370 0d44 2dd5 81c9 82cb fe22 f498 0004 0000
89c89
< 0000580 119e 0000 0000 0000 4008 0000 0000 0000
---
> 0000580 1187 0000 0000 0000 4008 0000 0000 0000
173c173
< 0002db0 1120 0000 0000 0000 119e 0000 0000 0000
---
> 0002db0 1120 0000 0000 0000 1187 0000 0000 0000
所以,我写了一个简单的补丁工具,它接受一个可执行文件名称和一组偏移量作为参数,用于修补字节(即 open(executable)、lseek(offset)、write(byte)、close(executable)):
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int ac, char *av[])
{
int fd;
char *fname = av[1];
unsigned char byte;
int offset;
int i;
int rc;
if (!fname) {
fprintf(stderr, "Usage: %s {offseth byteh...}\n", av[0]);
return 1;
}
fd = open(fname, O_RDWR);
if (fd < 0) {
fprintf(stderr, "open(%s): '%m' (%d)\n", fname, errno);
return 1;
}
i = 2;
while (av[i] && av[i + 1]) {
offset = strtol(av[i], NULL, 0);
byte = strtol(av[i + 1], NULL, 0);
printf("Patching @0x%x: 0x%02x\n", offset, byte);
rc = lseek(fd, offset, SEEK_SET);
if (rc != offset) {
fprintf(stderr, "lseek(%s): '%m' (%d)\n", fname, errno);
return 1;
}
rc = write(fd, &byte, 1);
if (rc != 1) {
fprintf(stderr, "write(%s): '%m' (%d)\n", fname, errno);
return 1;
}
i += 2;
} // End while
close(fd);
return 0;
} // main
当我用它来修补这两个地方时,析构函数被更改并按预期工作:
$ gcc patch.c -o patch
$ patch ./a.out.1 0x580 0x87 0x2db8 0x87 # cleanup()'s offset (0x1187)
Patching @0x580: 0x87
Patching @0x2db8: 0x87
$ ./a.out.1
In main
Cleanning Up
$ ./patch ./a.out.1 0x580 0x9e 0x2db8 0x9e # fini_exec()'s offset (0x119e)
Patching @0x580: 0x9e
Patching @0x2db8: 0x9e
$ ./a.out.1
In main
FINI
推荐阅读
- c - 如何使用正则表达式使用 sscanf 进行提取?(在 C 中)
- c# - 将 Powershell Object[] 传递给 C# 方法的最佳方法
- selenium - 在继续测试的同时等待 Web 元素
- excel - 在 sumif 和 sumifs 之间切换
- google-analytics - 缩小 _gaq.push 和 gtag 之间的差距
- c++ - 函数调用中的 C++ Int 数组类型
- winforms - 如何使用 SAML 对 Windows 窗体客户端进行身份验证?
- video - FFMPEG 忽略“-shortest”
- php - 如何在 SuiteCRM 中基于自定义模块“ProductInvestigation”子面板创建逻辑挂钩
- css - 布局在 iOS/Safari 中无法正确缩放