首页 > 解决方案 > 为什么编辑 .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 test000000000000119e T fini_exec

cleenup0000000000001187 T cleanup

运行objdump -s -j .fini_array test

Contents of section .fini_array:
 3db0 20110000 00000000 87110000 00000000   ...............

使用十六进制编辑器并转到2db0我从中获得的字节readelf并在编辑再次运行后编辑871100009e110000(印度语)objdump

Contents of section .fini_array:
 3db0 20110000 00000000 9e110000 00000000   ...............

然后保存并重新运行仍然会导致cleanup运行,我不知道为什么。不.fini_array应该在后面运行,.fini但似乎还有其他cleanup地方的另一个参考导致fini_exec无法运行。

标签: linuxelfglibcdynamic-linking

解决方案


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

推荐阅读