about summary refs log tree commit diff stats
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
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.
-rw-r--r--subx/010core.cc1
-rw-r--r--subx/020elf.cc7
-rw-r--r--subx/021translate.cc2
-rwxr-xr-xsubx/teensy/test6bin0 -> 5588 bytes
-rw-r--r--subx/teensy/test6-global.s13
-rwxr-xr-xsubx/teensy/test7bin0 -> 4100 bytes
-rw-r--r--subx/teensy/test7-global.s58
7 files changed, 77 insertions, 4 deletions
diff --git a/subx/010core.cc b/subx/010core.cc
index 1dc9f16f..3b987ffb 100644
--- a/subx/010core.cc
+++ b/subx/010core.cc
@@ -111,6 +111,7 @@ void run(string text_bytes) {
 void run_one_instruction() {
   uint8_t op=0, op2=0, op3=0;
   trace(2, "run") << "inst: 0x" << HEXWORD << EIP << end();
+//?   cerr << "inst: 0x" << EIP << '\n';
   switch (op = next()) {
   case 0xf4:  // hlt
     EIP = End_of_program;
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)
diff --git a/subx/021translate.cc b/subx/021translate.cc
index 40d75a8a..2f34f8dd 100644
--- a/subx/021translate.cc
+++ b/subx/021translate.cc
@@ -112,7 +112,7 @@ void dump_elf_header(ostream& out) {
   // p_memsz
   emit(size);
   // p_flags
-  uint32_t p_flags = 0x5;
+  uint32_t p_flags = 0x5;  // r-x
   emit(p_flags);
   // p_align
   uint32_t p_align = 0x1000;
diff --git a/subx/teensy/test6 b/subx/teensy/test6
new file mode 100755
index 00000000..0c08219f
--- /dev/null
+++ b/subx/teensy/test6
Binary files differdiff --git a/subx/teensy/test6-global.s b/subx/teensy/test6-global.s
new file mode 100644
index 00000000..9b981b62
--- /dev/null
+++ b/subx/teensy/test6-global.s
@@ -0,0 +1,13 @@
+; Example with a data segment.
+; nasm -f elf test6-global.s
+; gcc -Wall -s test6-global.o -o test6
+BITS 32
+
+SECTION .data
+foo: dd 42
+
+SECTION .text
+GLOBAL main
+main:
+  mov eax, foo
+  ret
diff --git a/subx/teensy/test7 b/subx/teensy/test7
new file mode 100755
index 00000000..0d44519d
--- /dev/null
+++ b/subx/teensy/test7
Binary files differdiff --git a/subx/teensy/test7-global.s b/subx/teensy/test7-global.s
new file mode 100644
index 00000000..4cb4e8f3
--- /dev/null
+++ b/subx/teensy/test7-global.s
@@ -0,0 +1,58 @@
+; https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
+; nasm -f bin test7-global.s -o test7
+; chmod +x test7
+BITS 32
+
+              org     0x08048000
+
+ehdr:                                                 ; Elf32_Ehdr
+              db      0x7F, "ELF", 1, 1, 1, 0         ;   e_ident
+      times 8 db      0
+              dw      2                               ;   e_type
+              dw      3                               ;   e_machine
+              dd      1                               ;   e_version
+              dd      _start                          ;   e_entry
+              dd      phdr1 - $$                      ;   e_phoff
+              dd      0                               ;   e_shoff
+              dd      0                               ;   e_flags
+              dw      ehdrsize                        ;   e_ehsize
+              dw      phdrsize                        ;   e_phentsize
+              dw      2                               ;   e_phnum
+              dw      0                               ;   e_shentsize
+              dw      0                               ;   e_shnum
+              dw      0                               ;   e_shstrndx
+ehdrsize  equ  $ - ehdr
+
+phdr1:                                                ; Elf32_Phdr
+              dd      1                               ;   p_type
+              dd      0                               ;   p_offset
+              dd      $$                              ;   p_vaddr
+              dd      $$                              ;   p_paddr
+              dd      codesize                        ;   p_filesz
+              dd      codesize                        ;   p_memsz
+              dd      5                               ;   p_flags = r-x
+              dd      0x1000                          ;   p_align
+phdrsize  equ  $ - phdr1
+
+phdr2:
+              dd      1                               ;   p_type
+              dd      _data - $$                      ;   p_offset
+              dd      _data                           ;   p_vaddr
+              dd      _data                           ;   p_paddr
+              dd      datasize                        ;   p_filesz
+              dd      datasize                        ;   p_memsz
+              dd      6                               ;   p_flags = rw-
+              dd      0x1000                          ;   p_align
+
+_start:
+  mov ebx, [foo]
+  mov eax, 1
+  int 0x80
+
+codesize      equ     $ - $$  ; TODO: why include the headers?!
+
+alignb 0x1000
+_data:
+  foo:        dd      42
+
+datasize      equ     $ - _data