-
Notifications
You must be signed in to change notification settings - Fork 13
/
relocation.rs
213 lines (168 loc) · 6.23 KB
/
relocation.rs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! libuser CRT0 relocation
//! Contains stuffs to handle basic dynamic relocation.
// Assembly blob can't get documented, but clippy requires it.
#[allow(clippy::missing_docs_in_private_items)]
mod module_header {
global_asm!(r#"
.section .rodata.mod0
.global module_header
module_header:
.ascii "MOD0"
.int _DYNAMIC - module_header
.int __bss_start__ - module_header
.int __bss_end__ - module_header
.int __eh_frame_hdr_start__ - module_header
.int __eh_frame_hdr_end__ - module_header
.int 0 // TODO: runtime-generated module object offset for rtld
"#);
}
/// The definition of a module header.
/// This is used by RTLD to do dynamic linking.
/// See https://switchbrew.org/wiki/NSO#MOD and https://switchbrew.org/wiki/Rtld
#[repr(C)]
#[derive(Debug)]
pub struct ModuleHeader {
/// The magic value of the MOD header. "MOD0"
pub magic: u32,
/// The offset of the dynamic section relative to ModuleHeader address.
pub dynamic_off: u32,
/// The offset of the begining of the bss section relative to ModuleHeader address.
pub bss_start_off: u32,
/// The offset of the end of the bss section relative to ModuleHeader address.
pub bss_end_off: u32,
/// The offset of the begining of the eh_frame_hdr section relative to ModuleHeader address.
pub unwind_start_off: u32,
/// The offset of the end of the eh_frame_hdr section relative to ModuleHeader address.
pub unwind_end_off: u32,
/// The offset of the module object that will be used by the rtld.
/// This offset is relative to ModuleHeader address.
pub module_object_off: u32
}
impl ModuleHeader {
/// Module Header Magic.
pub const MAGIC: u32 = 0x30444F4D;
}
/// A simple definition of a ELF Dynamic section entry.
#[repr(C)]
#[derive(Debug)]
struct ElfDyn {
/// The tag of the dynamic entry.
tag: isize,
/// The value of the dynamic entry.
val: usize,
}
/// Marks the end of the _DYNAMIC array.
const DT_NULL: isize = 0;
/// The address of a relocation table.
/// This element requires the DT_RELASZ and DT_RELAENT elements also be present.
/// When relocation is mandatory for a file, either DT_RELA or DT_REL can occur.
const DT_RELA: isize = 7;
/// The total size, in bytes, of the DT_RELA relocation table.
const DT_RELASZ: isize = 8;
/// The size, in bytes, of the DT_RELA relocation entry.
const DT_RELAENT: isize = 9;
/// Indicates that all ElfRela RELATIVE relocations have been concatenated together, and specifies the RELATIVE relocation count.
const DT_RELACOUNT: isize = 0x6ffffff9;
/// Similar to DT_RELA, except its table has implicit addends.
/// This element requires that the DT_RELSZ and DT_RELENT elements also be present.
const DT_REL: isize = 17;
/// The total size, in bytes, of the DT_REL relocation table.
const DT_RELSZ: isize = 18;
/// The size, in bytes, of the DT_REL relocation entry.
const DT_RELENT: isize = 19;
/// Indicates that all ElfRel RELATIVE relocations have been concatenated together, and specifies the RELATIVE relocation count.
const DT_RELCOUNT: isize = 0x6ffffffa;
/// Relocation table entry without addend.
#[repr(C)]
struct ElfRel {
/// The offset of the entry to relocate.
offset: usize,
/// The info about of the relocation to perform and its symbol offset.
info: usize
}
/// Relocation table entry with addend.
#[repr(C)]
struct ElfRela {
/// The offset of the entry to relocate.
offset: usize,
/// The info about of the relocation to perform and its symbol offset.
info: usize,
/// Addend.
addend: isize
}
/// The runtime linker computes the corresponding virtual address by adding the virtual address at which the shared object is loaded to the relative address.
const R_386_RELATIVE: usize = 8;
/// Handle basic relocation. Return a non zero value if failed.
#[cfg(target_os = "none")]
#[no_mangle]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe extern fn relocate_self(aslr_base: *mut u8, module_header: *const ModuleHeader) -> u32 {
let module_header_address = module_header as *const u8;
let module_header = &(*module_header);
if module_header.magic != ModuleHeader::MAGIC {
return 1;
}
let mut dynamic = module_header_address.add(module_header.dynamic_off as usize) as *const ElfDyn;
let mut rela_offset = None;
let mut rela_entry_size = 0;
let mut rela_count = 0;
let mut rel_offset = None;
let mut rel_entry_size = 0;
let mut rel_count = 0;
while (*dynamic).tag != DT_NULL {
match (*dynamic).tag {
DT_RELA => {
rela_offset = Some((*dynamic).val);
},
DT_RELAENT => {
rela_entry_size = (*dynamic).val;
},
DT_REL => {
rel_offset = Some((*dynamic).val);
},
DT_RELENT => {
rel_entry_size = (*dynamic).val;
},
DT_RELACOUNT => {
rela_count = (*dynamic).val;
},
DT_RELCOUNT => {
rel_count = (*dynamic).val;
},
_ => {}
}
dynamic = dynamic.offset(1);
}
if let Some(rela_offset) = rela_offset {
if rela_entry_size != core::mem::size_of::<ElfRela>() {
return 2;
}
let rela_base = (aslr_base.add(rela_offset)) as *mut ElfRela;
for i in 0..rela_count {
let rela = rela_base.add(i);
let reloc_type = (*rela).info & 0xff;
if let R_386_RELATIVE = reloc_type {
*(aslr_base.add((*rela).offset) as *mut *mut ()) = aslr_base.offset((*rela).addend) as _;
} else {
return 4;
}
}
}
if let Some(rel_offset) = rel_offset {
if rel_entry_size != core::mem::size_of::<ElfRel>() {
return 3;
}
let rel_base = (aslr_base.add(rel_offset)) as *mut ElfRel;
for i in 0..rel_count {
let rel = rel_base.add(i);
let reloc_type = (*rel).info & 0xff;
if let R_386_RELATIVE = reloc_type {
let ptr = aslr_base.add((*rel).offset) as *mut usize;
*ptr += aslr_base as usize;
} else {
return 4;
}
}
}
0
}