因为这是我在 SO 上的第一个条目:大家好 :)
现在到了好的部分......我正在努力从 ELF 文件中找出一种全局变量。我编译前的源代码是这样的:
#include "stdint.h"
uint8_t variable_global1;
uint8_t variable_global2 = 1;
uint8_t variable_global4;
uint16_t variable_global16b = 1;
int16_t variable_global16b8;
uint16_t variable_global16b9;
int main(void)
{
variable_global1 = 2;
static uint8_t variable_global3;
static uint8_t variable_global5 = 8;
static uint8_t variable_global6;
variable_global6 = 7;
variable_global16b9 = 500;
}
当我用 readelf 或 objectdump 试试运气时,这是我得到的最远距离。结果readelf -s LEKCJA2.elf
是:
Symbol table '.symtab' contains 72 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 SECTION LOCAL DEFAULT 1
2: 00800060 0 SECTION LOCAL DEFAULT 2
3: 00800064 0 SECTION LOCAL DEFAULT 3
4: 00000000 0 SECTION LOCAL DEFAULT 4
5: 00000000 0 SECTION LOCAL DEFAULT 5
6: 00000042 0 NOTYPE LOCAL DEFAULT 1 .do_copy_data_start
7: 0000003e 0 NOTYPE LOCAL DEFAULT 1 .do_copy_data_loop
8: 00000052 0 NOTYPE LOCAL DEFAULT 1 .do_clear_bss_start
9: 00000050 0 NOTYPE LOCAL DEFAULT 1 .do_clear_bss_loop
10: 00000000 0 FILE LOCAL DEFAULT ABS main.c
11: 0000003f 0 NOTYPE LOCAL DEFAULT ABS __SREG__
12: 0000003e 0 NOTYPE LOCAL DEFAULT ABS __SP_H__
13: 0000003d 0 NOTYPE LOCAL DEFAULT ABS __SP_L__
14: 00000034 0 NOTYPE LOCAL DEFAULT ABS __CCP__
15: 00000000 0 NOTYPE LOCAL DEFAULT ABS __tmp_reg__
16: 00000001 0 NOTYPE LOCAL DEFAULT ABS __zero_reg__
17: 00800064 1 OBJECT LOCAL DEFAULT 3 variable_global6.1217
18: 00800063 1 OBJECT LOCAL DEFAULT 2 variable_global5.1216
19: 00800065 1 OBJECT LOCAL DEFAULT 3 variable_global3.1215
20: 0000008a 0 NOTYPE LOCAL DEFAULT 1 __stop_program
21: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_1
22: 00000026 0 NOTYPE GLOBAL DEFAULT 1 __trampolines_start
23: 0000008c 0 NOTYPE GLOBAL DEFAULT 1 _etext
24: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_12
25: 0000005c 0 NOTYPE GLOBAL DEFAULT 1 __bad_interrupt
26: 00000090 0 NOTYPE GLOBAL DEFAULT ABS __data_load_end
27: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_6
28: 00000026 0 NOTYPE GLOBAL DEFAULT 1 __trampolines_end
29: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_3
30: 00800066 1 OBJECT GLOBAL DEFAULT 3 variable_global1
31: 0000008c 0 NOTYPE GLOBAL DEFAULT ABS __data_load_start
32: 00000026 0 NOTYPE GLOBAL DEFAULT 1 __dtors_end
33: 0080006c 0 NOTYPE GLOBAL DEFAULT 3 __bss_end
34: 00800067 2 OBJECT GLOBAL DEFAULT 3 variable_global16b9
35: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_11
36: 00000026 0 NOTYPE WEAK DEFAULT 1 __init
37: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_13
38: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_17
39: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_7
40: 00000048 0 NOTYPE GLOBAL DEFAULT 1 __do_clear_bss
41: 00810000 0 NOTYPE GLOBAL DEFAULT 4 __eeprom_end
42: 00000000 0 NOTYPE GLOBAL DEFAULT 1 __vectors
43: 00800064 0 NOTYPE GLOBAL DEFAULT 2 __data_end
44: 00000000 0 NOTYPE WEAK DEFAULT 1 __vector_default
45: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_5
46: 00000026 0 NOTYPE GLOBAL DEFAULT 1 __ctors_start
47: 00000032 0 NOTYPE GLOBAL DEFAULT 1 __do_copy_data
48: 00800064 0 NOTYPE GLOBAL DEFAULT 3 __bss_start
49: 0000005e 42 FUNC GLOBAL DEFAULT 1 main
50: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_4
51: 00000000 0 NOTYPE WEAK DEFAULT ABS __heap_end
52: 00800060 1 OBJECT GLOBAL DEFAULT 2 variable_global2
53: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_9
54: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_2
55: 00800061 2 OBJECT GLOBAL DEFAULT 2 variable_global16b
56: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_15
57: 00800069 1 OBJECT GLOBAL DEFAULT 3 variable_global4
58: 00000026 0 NOTYPE GLOBAL DEFAULT 1 __dtors_start
59: 00000026 0 NOTYPE GLOBAL DEFAULT 1 __ctors_end
60: 0000045f 0 NOTYPE WEAK DEFAULT ABS __stack
61: 00800064 0 NOTYPE GLOBAL DEFAULT 2 _edata
62: 0080006c 0 NOTYPE GLOBAL DEFAULT 4 _end
63: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_8
64: 0080006a 2 OBJECT GLOBAL DEFAULT 3 variable_global16b8
65: 00000088 0 NOTYPE WEAK DEFAULT 1 exit
66: 00000088 0 NOTYPE GLOBAL DEFAULT 1 _exit
67: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_14
68: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_10
69: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_16
70: 00800060 0 NOTYPE GLOBAL DEFAULT 2 __data_start
71: 0000005c 0 NOTYPE WEAK DEFAULT 1 __vector_18
如您所见,我可以列出我的全局变量(例如variable_global1
),我也知道它在内存中的大小,但不幸的是不知道类型 - 无论是 unsigned int、signed int、UBYTE 等等。
最后我的问题:是否甚至可以从 ELF 文件中找出一种全局变量(未初始化或已初始化),如果可以,我该怎么做?现在我正在使用不同的 CMD 工具和 Python 库来解析 ELF 文件,但我无法更接近我想要做的事情。
我感谢你所有的帮助:)