From f39c01128dfc6e46828cbab992d4b9e0ba1cb1e5 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Sat, 1 Sep 2018 20:48:18 -0700 Subject: 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. --- subx/034compute_segment_address.cc | 31 +++++++++++++++++++++++++++++++ subx/037compute_segment_address.cc | 31 ------------------------------- 2 files changed, 31 insertions(+), 31 deletions(-) create mode 100644 subx/034compute_segment_address.cc delete mode 100644 subx/037compute_segment_address.cc 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 + } +} diff --git a/subx/037compute_segment_address.cc b/subx/037compute_segment_address.cc deleted file mode 100644 index ee2b1bb4..00000000 --- a/subx/037compute_segment_address.cc +++ /dev/null @@ -1,31 +0,0 @@ -//: 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 - } -} -- cgit 1.4.1-2-gfad0