From 3db126e018388facfab368ecb2c8f8bc566afe1a Mon Sep 17 00:00:00 2001 From: XmiliaH <45106915+XmiliaH@users.noreply.github.com> Date: Mon, 24 Mar 2025 00:09:33 +0100 Subject: First run as elf binary (#178) --- .../com/seaofnodes/simple/codegen/ElfFile.java | 22 ++++++++++-------- .../test/java/com/seaofnodes/simple/Compile.java | 27 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 chapter21/src/test/java/com/seaofnodes/simple/Compile.java (limited to 'chapter21') diff --git a/chapter21/src/main/java/com/seaofnodes/simple/codegen/ElfFile.java b/chapter21/src/main/java/com/seaofnodes/simple/codegen/ElfFile.java index 019cdf90..5bf83542 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/codegen/ElfFile.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/codegen/ElfFile.java @@ -133,7 +133,7 @@ public class ElfFile { public static class SymbolSection extends Section { Ary _symbols = new Ary<>(Symbol.class); - int _nonlocal_symbols; + Ary _loc = new Ary<>(Symbol.class); SymbolSection(String name, int type) { super(name, type); @@ -145,19 +145,17 @@ public class ElfFile { _link = 1; // number of non-local symbols - _info = _nonlocal_symbols; + _info = _loc.len() + 1; super.writeHeader(out); } void push(Symbol s) { if ((s._info >> 4) != SYM_BIND_LOCAL) { - _nonlocal_symbols += 1; + _symbols.push(s); + } else { + _loc.push(s); } - - _symbols.push(s); - // symbols are effectively "1-based" (NULL symbol) - s._index = _symbols.len(); } @Override @@ -165,15 +163,20 @@ public class ElfFile { for( int i = 0; i < SYMBOL_SIZE/4; i++ ) { out.putInt(0); } - + int num=1; + for( Symbol s : _loc ) { + s._index = num++; + s.writeHeader(out); + } for( Symbol s : _symbols ) { + s._index = num++; s.writeHeader(out); } } @Override int size() { - return (1 + _symbols.len()) * SYMBOL_SIZE; + return (1 + _symbols.len() + _loc.len()) * SYMBOL_SIZE; } } @@ -318,6 +321,7 @@ public class ElfFile { // populate string table for( Section s : _sections ) { s._name_pos = writeCString(strtab, s._name); } for( Symbol s : symbols._symbols ) { s._name_pos = writeCString(strtab, s._name); } + for( Symbol s : symbols._loc ) { s._name_pos = writeCString(strtab, s._name); } int idx = 1; for( Section s : _sections ) { diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Compile.java b/chapter21/src/test/java/com/seaofnodes/simple/Compile.java new file mode 100644 index 00000000..5286029a --- /dev/null +++ b/chapter21/src/test/java/com/seaofnodes/simple/Compile.java @@ -0,0 +1,27 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.codegen.CodeGen; +import org.junit.Ignore; +import org.junit.Test; + +import java.io.IOException; + +import static com.seaofnodes.simple.Main.PORTS; + +public class Compile { + + @Test + @Ignore + public void testCompile() throws IOException, InterruptedException { + boolean USE_WSL = System.getProperty("os.name").startsWith("Windows"); + CodeGen code = new CodeGen("return 13;"); + code.parse().opto().typeCheck().instSelect(PORTS,"x86_64_v2", "SystemV").GCM().localSched().regAlloc().encode(); + code.exportELF("test.o"); + Process p = Runtime.getRuntime().exec(USE_WSL ? new String[]{"wsl.exe", "gcc", "-o", "test", "test.o"}:new String[]{"gcc", "-o", "test", "test.o"}); + assert p.waitFor() == 0; + p = Runtime.getRuntime().exec(USE_WSL ? new String[]{"wsl.exe", "./test"}:new String[]{"./test"}); + System.out.println(p.waitFor()); + assert p.waitFor() == 13; + } + +} -- cgit v1.2.3