【Pvvn】ELF文件基础

第一个标题

1
vim hello.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// hello.c

#include <stdio.h>

int global_int_84;
int global_uninit_var;

void func1(int i) {
printf("%d\n", i);
}

int main() {
static int static_var = 85;
static int static_var2;
int a = 1;
int b;
func1(static_var + static_var2 + a + b);
return a;
}
1
2
3
gcc -c hello.c -m32  # 生成32位程序
# ls
# hello.c hello.o
1
2
3
# 查看基本信息
# 显示名为 hello.o 的目标文件的头部信息(header)
objdump -h hello.o
1
2
# 用来显示名为 hello.o 的目标文件的ELF头部信息
readelf -h hello.o
  • 010editor的使用

链接(LINKING)

  • 链接是将一些指令对其他符号地址的引用加以修正
  • 链接过程包括了
    • 地址和空间分配(Address and Storage Allocation)
    • 符号决议 (Symbol Resolution) 符号绑定 (Symbol Binding) 名称绑定(Name Binding)
    • 重定位 (Relocation)

静态链接

  • 将多个目标文件链接在一起并生成可执行文件的过程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// a.c
extern int shared;
extern void swap(int *a, int *b);
int main() {
int a = 100;
swap(&a, &shared);
}

// b.c
int shared = 1;

void swap(int *a. int *b) {
*a ^= *b ^= *a ^= *b;
}
1
2
gcc -c a.c b.c -fno-stack-protector -m32
# 执行完获得a.o和b.o两个文件
  • 一个目标文件引用了另一个目标文件,就需要使用静态链接
1
chmod +x ./a.o    # 给名为 a.o 的文件添加执行权限
  • 使用ld -m elf_i386 a.o b.o -e main -o ab命令进行链接
1
ld -m elf_i386 a.o b.o -e main -o ab   # 使用 GNU linker (ld) 将两个目标文件 a.o 和 b.o 链接成一个可执行文件 ab
  • Copyrights © 2024-2025 brocademaple
  • 访问人数: | 浏览次数:

      请我喝杯咖啡吧~

      支付宝
      微信