1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::collections::HashMap;
use crate::frontend::{
ao::Op,
ChunkContent,
FixedVec,
Pointer,
ProcessImage,
VAddr,
};
fn lookup_pointer(image: &ProcessImage, pointer: &Pointer, table: &mut HashMap<Pointer, VAddr>) {
if table.contains_key(pointer) {
return;
}
let addr = match pointer {
Pointer::Function(pointer) => {
let chunk = image
.elf(pointer.elf)
.unwrap()
.section(pointer.section)
.unwrap()
.symbol(pointer.symbol)
.unwrap()
.chunk(pointer.chunk)
.unwrap();
let ChunkContent::Code(func) = chunk.content() else { unreachable!() };
let entry = func.cfg().entry();
func.cfg().basic_block(entry).unwrap().vaddr().unwrap()
},
Pointer::BasicBlock(pointer) => {
let chunk = image
.elf(pointer.elf)
.unwrap()
.section(pointer.section)
.unwrap()
.symbol(pointer.symbol)
.unwrap()
.chunk(pointer.chunk)
.unwrap();
let ChunkContent::Code(func) = chunk.content() else { unreachable!() };
func.cfg().basic_block(pointer.bb).unwrap().vaddr().unwrap()
},
Pointer::Global(pointer) => {
let addr = image
.elf(pointer.elf)
.unwrap()
.section(pointer.section)
.unwrap()
.symbol(pointer.symbol)
.unwrap()
.chunk(pointer.chunk)
.unwrap()
.vaddr();
addr + pointer.offset as VAddr
},
Pointer::Null => 0,
};
table.insert(pointer.clone(), addr);
}
pub(crate) fn concretize(image: &mut ProcessImage) {
let mut table = HashMap::<Pointer, VAddr>::new();
/* Build lookup table for all pointers */
for elf in image.iter_elfs() {
for section in elf.iter_sections() {
for symbol in section.iter_symbols() {
for chunk in symbol.iter_chunks() {
match chunk.content() {
ChunkContent::Pointer(pointer) => {
lookup_pointer(image, pointer, &mut table);
},
ChunkContent::Code(func) => {
for bb in func.cfg().iter_basic_blocks() {
for op in bb.ops() {
if let Op::LoadPointer {
pointer,
..
} = op
{
lookup_pointer(image, pointer, &mut table);
}
}
}
},
ChunkContent::Data {
..
} => {},
}
}
}
}
}
/* Resolve every pointer */
for elf in image.iter_elfs_mut() {
for section in elf.iter_sections_mut() {
let perms = section.perms();
for symbol in section.iter_symbols_mut() {
for chunk in symbol.iter_chunks_mut() {
match chunk.content_mut() {
ChunkContent::Pointer(pointer) => {
let addr = *table.get(pointer).unwrap();
let bytes = FixedVec::lock(addr.to_le_bytes());
let perms = FixedVec::lock(vec![perms; bytes.len()]);
chunk.set_content(ChunkContent::Data {
bytes,
perms,
});
},
ChunkContent::Code(func) => {
for bb in func.cfg_mut().iter_basic_blocks_mut() {
bb.set_cursor(0);
while let Some(op) = bb.cursor_op() {
if let Op::LoadPointer {
dst,
pointer,
} = op
{
let vaddr = *table.get(pointer).unwrap();
bb.replace_op(Op::LoadVirtAddr {
dst: *dst,
vaddr,
});
}
if !bb.move_cursor_forward() {
break;
}
}
}
},
ChunkContent::Data {
..
} => {},
}
}
}
}
}
}