about summary refs log tree commit diff stats
path: root/subx/034compute_segment_address.cc
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2018-09-01 20:48:18 -0700
committerKartik Agaram <vc@akkartik.com>2018-09-01 20:48:18 -0700
commitf39c01128dfc6e46828cbab992d4b9e0ba1cb1e5 (patch)
treef1bc5787a126b1b6a107174eaf7079cb48d6d674 /subx/034compute_segment_address.cc
parent9473d0d9015ad4a886c60362cd8bd9b108bd1112 (diff)
downloadmu-f39c01128dfc6e46828cbab992d4b9e0ba1cb1e5.tar.gz
4534
I'd been planning to add segment address computation after all labels were
computed, including labels in the data segment (which isn't built yet).
But now I realize that won't work, because labels in the data segment will
require segment start addresses. We need to deal in absolute addresses
rather than relative offsets as with the jump instructions that use code
labels.

Layer 34 is now broken by this change in a way that isn't obvious right
now: it is oblivious to imm32 and disp32 operand tags that are now going
to be present in the programs it sees. It's a lucky accident that everything
still works, because we're only using segment names right now for the very
first (code) segment in a program.
Diffstat (limited to 'subx/034compute_segment_address.cc')
-rw-r--r--subx/034compute_segment_address.cc31
1 files changed, 31 insertions, 0 deletions
diff --git a/subx/034compute_segment_address.cc b/subx/034compute_segment_address.cc
new file mode 100644
index 00000000..ee2b1bb4
--- /dev/null
+++ b/subx/034compute_segment_address.cc
@@ -0,0 +1,31 @@
+//: Start allowing us to not specify precise addresses for the start of each
+//: segment.
+//: This gives up a measure of control in placing code and data.
+
+:(scenario segment_name)
+% Mem_offset = CODE_START;
+== code
+05/add 0x0d0c0b0a/imm32  # add 0x0d0c0b0a to EAX
+# code starts at 0x08048000 + p_offset, which is 0x54 for a single-segment binary
++load: 0x08048054 -> 05
++load: 0x08048055 -> 0a
++load: 0x08048056 -> 0b
++load: 0x08048057 -> 0c
++load: 0x08048058 -> 0d
++run: add imm32 0x0d0c0b0a to reg EAX
++run: storing 0x0d0c0b0a
+
+:(before "End Level-2 Transforms")
+Transform.push_back(compute_segment_starts);
+
+:(code)
+void compute_segment_starts(program& p) {
+  uint32_t p_offset = /*size of ehdr*/0x34 + SIZE(p.segments)*0x20/*size of each phdr*/;
+  for (size_t i = 0;  i < p.segments.size();  ++i) {
+    segment& curr = p.segments.at(i);
+    if (curr.start == 0)
+      curr.start = CODE_START + i*SEGMENT_SIZE + p_offset;
+    p_offset += num_words(curr);
+    assert(p_offset < SEGMENT_SIZE);  // for now we get less and less available space in each successive segment
+  }
+}