about summary refs log tree commit diff stats
path: root/subx/020elf.cc
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2018-07-03 16:36:37 -0700
committerKartik Agaram <vc@akkartik.com>2018-07-03 16:36:37 -0700
commitc1100182501e2dca7893b3c891470b33c43a71b1 (patch)
tree16226daf6178ce1356637b4c5278f5664a3cd5de /subx/020elf.cc
parentc833fbad65e39c12dde44bdff09fadd822d2b52c (diff)
downloadmu-c1100182501e2dca7893b3c891470b33c43a71b1.tar.gz
4311 - subx running binaries with global variables
Learning to use the data segment.

Currently, subx can only run the teensy files generated from flat
assembler:
  test4
  test5
  test7

This is not a priority to fix. These files are just useful references to
have around.
Diffstat (limited to 'subx/020elf.cc')
-rw-r--r--subx/020elf.cc7
1 files changed, 4 insertions, 3 deletions
diff --git a/subx/020elf.cc b/subx/020elf.cc
index 24ec1fcc..a67bfdfe 100644
--- a/subx/020elf.cc
+++ b/subx/020elf.cc
@@ -54,7 +54,7 @@ void load_elf_contents(uint8_t* elf_contents, size_t size) {
   // unused: e_shstrndx
 
   for (size_t i = 0;  i < e_phnum;  ++i)
-    load_program_header(elf_contents, size, e_phoff + i*e_phentsize, e_ehsize);
+    load_segment_from_program_header(elf_contents, size, e_phoff + i*e_phentsize, e_ehsize);
 
   // TODO: need to set up real stack somewhere
 
@@ -62,7 +62,7 @@ void load_elf_contents(uint8_t* elf_contents, size_t size) {
   EIP = e_entry;
 }
 
-void load_program_header(uint8_t* elf_contents, size_t size, uint32_t offset, uint32_t e_ehsize) {
+void load_segment_from_program_header(uint8_t* elf_contents, size_t size, uint32_t offset, uint32_t e_ehsize) {
   uint32_t p_type = u32_in(&elf_contents[offset]);
   info << "program header at offset " << offset << ": type " << p_type << '\n';
   if (p_type != 1) {
@@ -80,7 +80,8 @@ void load_program_header(uint8_t* elf_contents, size_t size, uint32_t offset, ui
 
   if (p_offset + p_filesz > size)
     raise << "Invalid binary; segment at offset " << offset << " is too large: wants to end at " << p_offset+p_filesz << " but the file ends at " << size << '\n' << die();
-  Mem.resize(p_vaddr + p_memsz);
+  if (Mem.size() < p_vaddr + p_memsz)
+    Mem.resize(p_vaddr + p_memsz);
   if (size > p_memsz) size = p_memsz;
   info << "blitting file offsets (" << p_offset << ", " << (p_offset+p_filesz) << ") to addresses (" << p_vaddr << ", " << (p_vaddr+p_memsz) << ")\n";
   for (size_t i = 0;  i < p_filesz;  ++i)