1
2
3
4
5
6 :(scenario segment_name)
7 % Mem_offset = CODE_START;
8 == code
9 05/add 0x0d0c0b0a/imm32
10
11 +load: 0x08048054 -> 05
12 +load: 0x08048055 -> 0a
13 +load: 0x08048056 -> 0b
14 +load: 0x08048057 -> 0c
15 +load: 0x08048058 -> 0d
16 +run: add imm32 0x0d0c0b0a to reg EAX
17 +run: storing 0x0d0c0b0a
18
19 :(before "End Level-2 Transforms")
20 Transform.push_back(compute_segment_starts);
21
22 :(code)
23 void compute_segment_starts(program& p) {
24 trace(99, "transform") << "-- compute segment addresses" << end();
25 uint32_t p_offset = 0x34 + SIZE(p.segments)*0x20;
26 for (size_t i = 0; i < p.segments.size(); ++i) {
27 segment& curr = p.segments.at(i);
28 if (curr.start == 0) {
29 curr.start = CODE_START + i*SEGMENT_SIZE + p_offset;
30 trace(99, "transform") << "segment " << i << " begins at address " << curr.start << end();
31 }
32 p_offset += size_of(curr);
33 assert(p_offset < SEGMENT_SIZE);
34 }
35 }
36
37 uint32_t size_of(const segment& s) {
38 uint32_t sum = 0;
39 for (int i = 0; i < SIZE(s.lines); ++i)
40 sum += num_bytes(s.lines.at(i));
41 return sum;
42 }
43
44
45 uint32_t num_bytes(const line& inst) {
46 uint32_t sum = 0;
47 for (int i = 0; i < SIZE(inst.words); ++i) {
48 const word& curr = inst.words.at(i);
49 if (has_metadata(curr, "disp32") || has_metadata(curr, "imm32"))
50 sum += 4;
51 else
52 sum++;
53 }
54 return sum;
55 }