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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
//! Automatically inspect the programs generated by the examples.
//!
//! Do not refer to this as a specification for the runtime. These values
//! are subject to change.
#![allow(clippy::unusual_byte_groupings)] // Spacing delimits ITCM / DTCM / OCRAM banks.
use goblin::elf::Elf;
use std::{fs, path::PathBuf, process::Command};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
/// Build an example, returning a path to the ELF.
fn cargo_build(board: &str) -> Result<PathBuf> {
Command::new("cargo")
.arg("build")
.arg("--example=blink-rtic")
.arg(format!("--features=board/{},board/rtic", board))
.arg("--target=thumbv7em-none-eabihf")
.arg(format!("--target-dir=target/{}", board))
.arg("--quiet")
.spawn()?
.wait()?;
let path = PathBuf::from(format!(
"target/{}/thumbv7em-none-eabihf/debug/examples/blink-rtic",
board
));
Ok(path)
}
struct ImxrtBinary<'a> {
elf: &'a Elf<'a>,
}
impl<'a> ImxrtBinary<'a> {
fn new(elf: &'a Elf<'a>) -> Self {
Self { elf }
}
fn symbol(&self, symbol_name: &str) -> Option<goblin::elf::Sym> {
self.elf
.syms
.iter()
.flat_map(|sym| self.elf.strtab.get_at(sym.st_name).map(|name| (sym, name)))
.find(|(_, name)| symbol_name == *name)
.map(|(sym, _)| sym)
}
fn fcb(&self) -> Result<Fcb> {
self.symbol("FLEXSPI_CONFIGURATION_BLOCK")
.map(|sym| Fcb {
address: sym.st_value,
size: sym.st_size,
})
.ok_or_else(|| {
String::from("Could not find FLEXSPI_CONFIGURATION_BLOCK in program").into()
})
}
fn flexram_config(&self) -> Result<u64> {
self.symbol("__flexram_config")
.map(|sym| sym.st_value)
.ok_or_else(|| String::from("Could not find FlexRAM configuration in program").into())
}
fn section(&self, section_name: &str) -> Result<Section> {
self.elf
.section_headers
.iter()
.flat_map(|sec| {
self.elf
.shdr_strtab
.get_at(sec.sh_name)
.map(|name| (sec, name))
})
.find(|(_, name)| section_name == *name)
.map(|(sec, _)| Section {
address: sec.sh_addr,
size: sec.sh_size,
})
.ok_or_else(|| format!("Could not find {section_name} in program").into())
}
fn section_lma(&self, section: &Section) -> u64 {
self.elf
.program_headers
.iter()
.filter(|phdr| goblin::elf::program_header::PT_LOAD == phdr.p_type)
.find(|phdr| {
phdr.p_vaddr <= section.address && (phdr.p_vaddr + phdr.p_memsz) > section.address
})
.map(|phdr| section.address - phdr.p_vaddr + phdr.p_paddr)
.unwrap_or(section.address) // VMA == LMA
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Fcb {
address: u64,
size: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Section {
address: u64,
size: u64,
}
const DTCM: u64 = 0x2000_0000;
const ITCM: u64 = 0x0000_0000;
const fn aligned(value: u64, alignment: u64) -> u64 {
(value + (alignment - 1)) & !(alignment - 1)
}
#[test]
#[ignore = "building an example can take time"]
fn imxrt1010evk() {
let path = cargo_build("imxrt1010evk").expect("Unable to build example");
let contents = fs::read(path).expect("Could not read ELF file");
let elf = Elf::parse(&contents).expect("Could not parse ELF");
let binary = ImxrtBinary::new(&elf);
assert_eq!(
Fcb {
address: 0x6000_0400,
size: 512
},
binary.fcb().unwrap()
);
assert_eq!(binary.flexram_config().unwrap(), 0b11_10_0101);
let stack = binary.section(".stack").unwrap();
assert_eq!(
Section {
address: DTCM,
size: 8 * 1024
},
stack,
"stack not at ORIGIN(DTCM), or not 8 KiB large"
);
assert_eq!(binary.section_lma(&stack), stack.address);
let vector_table = binary.section(".vector_table").unwrap();
assert_eq!(
Section {
address: stack.address + stack.size,
size: 16 * 4 + 240 * 4
},
vector_table,
"vector table not at expected VMA behind the stack"
);
assert!(
vector_table.address % 1024 == 0,
"vector table is not 1024-byte aligned"
);
assert_eq!(binary.section_lma(&vector_table), 0x6000_2000);
let text = binary.section(".text").unwrap();
assert_eq!(text.address, ITCM, "text");
assert_eq!(
binary.section_lma(&text),
0x6000_2000 + vector_table.size,
"text VMA expected behind vector table"
);
let rodata = binary.section(".rodata").unwrap();
assert_eq!(
rodata.address,
0x6000_2000 + vector_table.size + aligned(text.size, 16),
"rodata LMA & VMA expected behind text"
);
assert_eq!(rodata.address, binary.section_lma(&rodata));
let data = binary.section(".data").unwrap();
assert_eq!(data.address, 0x2020_0000, "data VMA in OCRAM");
assert_eq!(
data.size, 4,
"blink-rtic expected to have a single static mut u32"
);
assert_eq!(
binary.section_lma(&data),
rodata.address + aligned(rodata.size, 4),
"data LMA starts behind rodata"
);
let bss = binary.section(".bss").unwrap();
assert_eq!(
bss.address,
data.address + aligned(data.size, 4),
"bss in OCRAM behind data"
);
assert_eq!(binary.section_lma(&bss), bss.address, "bss is NOLOAD");
let uninit = binary.section(".uninit").unwrap();
assert_eq!(
uninit.address,
bss.address + aligned(bss.size, 4),
"uninit in OCRAM behind bss"
);
assert_eq!(
binary.section_lma(&uninit),
uninit.address,
"uninit is NOLOAD"
);
let heap = binary.section(".heap").unwrap();
assert_eq!(
Section {
address: vector_table.address + vector_table.size,
size: 1024
},
heap,
"1 KiB heap in DTCM behind vector table"
);
assert_eq!(heap.size, 1024);
assert_eq!(binary.section_lma(&heap), heap.address, "Heap is NOLOAD");
}
#[test]
#[ignore = "building an example can take time"]
fn teensy4() {
let path = cargo_build("teensy4").expect("Unable to build example");
let contents = fs::read(path).expect("Could not read ELF file");
let elf = Elf::parse(&contents).expect("Could not parse ELF");
let binary = ImxrtBinary::new(&elf);
assert_eq!(
Fcb {
address: 0x6000_0000,
size: 512
},
binary.fcb().unwrap()
);
assert_eq!(
binary.flexram_config().unwrap(),
0b11111111_101010101010101010101010
);
let stack = binary.section(".stack").unwrap();
assert_eq!(
Section {
address: DTCM,
size: 8 * 1024
},
stack,
"stack not at ORIGIN(DTCM), or not 8 KiB large"
);
assert_eq!(binary.section_lma(&stack), stack.address);
let vector_table = binary.section(".vector_table").unwrap();
assert_eq!(
Section {
address: stack.address + stack.size,
size: 16 * 4 + 240 * 4
},
vector_table,
"vector table not at expected VMA behind the stack"
);
assert!(
vector_table.address % 1024 == 0,
"vector table is not 1024-byte aligned"
);
assert_eq!(binary.section_lma(&vector_table), 0x6000_2000);
let text = binary.section(".text").unwrap();
assert_eq!(
text.address,
binary.section_lma(&vector_table) + vector_table.size,
"text"
);
assert_eq!(
binary.section_lma(&text),
0x6000_2000 + vector_table.size,
"text VMA expected behind vector table"
);
let rodata = binary.section(".rodata").unwrap();
assert_eq!(
rodata.address,
vector_table.address + vector_table.size,
"rodata LMA & VMA expected behind text"
);
assert_eq!(
binary.section_lma(&rodata),
binary.section_lma(&text) + aligned(text.size, 16)
);
let data = binary.section(".data").unwrap();
assert_eq!(
data.address,
rodata.address + rodata.size,
"data VMA in DTCM behind rodata"
);
assert_eq!(
data.size, 4,
"blink-rtic expected to have a single static mut u32"
);
assert_eq!(
binary.section_lma(&data),
binary.section_lma(&rodata) + aligned(rodata.size, 4),
"data LMA starts behind rodata"
);
let bss = binary.section(".bss").unwrap();
assert_eq!(
bss.address,
data.address + aligned(data.size, 4),
"bss in DTCM behind data"
);
assert_eq!(binary.section_lma(&bss), bss.address, "bss is NOLOAD");
let uninit = binary.section(".uninit").unwrap();
assert_eq!(
uninit.address,
bss.address + aligned(bss.size, 4),
"uninit in DTCM behind bss"
);
assert_eq!(
binary.section_lma(&uninit),
uninit.address,
"uninit is NOLOAD"
);
let heap = binary.section(".heap").unwrap();
assert_eq!(
Section {
address: uninit.address + aligned(uninit.size, 4),
size: 1024
},
heap,
"1 KiB heap in DTCM behind uninit"
);
assert_eq!(binary.section_lma(&heap), heap.address, "Heap is NOLOAD");
}
#[test]
#[ignore = "building an example can take time"]
fn imxrt1170evk_cm7() {
let path = cargo_build("imxrt1170evk-cm7").expect("Unable to build example");
let contents = fs::read(path).expect("Could not read ELF file");
let elf = Elf::parse(&contents).expect("Could not parse ELF");
let binary = ImxrtBinary::new(&elf);
assert_eq!(
Fcb {
address: 0x3000_0400,
size: 512
},
binary.fcb().unwrap()
);
assert_eq!(
binary.flexram_config().unwrap(),
0b1111111111111111_1010101010101010
);
let stack = binary.section(".stack").unwrap();
assert_eq!(
Section {
address: DTCM,
size: 8 * 1024
},
stack,
"stack not at ORIGIN(DTCM), or not 8 KiB large"
);
assert_eq!(binary.section_lma(&stack), stack.address);
let vector_table = binary.section(".vector_table").unwrap();
assert_eq!(
Section {
address: stack.address + stack.size,
size: 16 * 4 + 240 * 4
},
vector_table,
"vector table not at expected VMA behind the stack"
);
assert!(
vector_table.address % 1024 == 0,
"vector table is not 1024-byte aligned"
);
assert_eq!(binary.section_lma(&vector_table), 0x3000_2000);
let text = binary.section(".text").unwrap();
assert_eq!(text.address, ITCM, "text");
assert_eq!(
binary.section_lma(&text),
0x3000_2000 + vector_table.size,
"text VMA expected behind vector table"
);
let rodata = binary.section(".rodata").unwrap();
assert_eq!(
rodata.address,
vector_table.address + vector_table.size,
"rodata moved to DTCM behind vector table"
);
assert_eq!(
binary.section_lma(&rodata),
0x3000_2000 + vector_table.size + aligned(text.size, 16),
);
let data = binary.section(".data").unwrap();
assert_eq!(data.address, 0x2024_0000, "data VMA in OCRAM");
assert_eq!(
data.size, 4,
"blink-rtic expected to have a single static mut u32"
);
assert_eq!(
binary.section_lma(&data),
binary.section_lma(&rodata) + aligned(rodata.size, 4),
"data LMA starts behind rodata"
);
let bss = binary.section(".bss").unwrap();
assert_eq!(
bss.address,
data.address + aligned(data.size, 4),
"bss in OCRAM behind data"
);
assert_eq!(binary.section_lma(&bss), bss.address, "bss is NOLOAD");
let uninit = binary.section(".uninit").unwrap();
assert_eq!(
uninit.address,
bss.address + aligned(bss.size, 4),
"uninit in OCRAM behind bss"
);
assert_eq!(
binary.section_lma(&uninit),
uninit.address,
"uninit is NOLOAD"
);
let heap = binary.section(".heap").unwrap();
assert_eq!(
Section {
address: rodata.address + aligned(rodata.size, 4),
size: 0,
},
heap,
"0 byte heap in DTCM behind rodata table"
);
assert_eq!(binary.section_lma(&heap), heap.address, "Heap is NOLOAD");
}
|