Skip to content

Commit f7ad125

Browse files
committed
Also track the offset of the code section + adjustments to toHex to support little endian
1 parent 7ed7f6c commit f7ad125

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/main/java/be/ugent/topl/mio/GdbStub.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,20 @@ private String toHex(byte[] data, int offset, int length) {
5252
return sb.toString();
5353
}
5454

55-
private String toHex(long data, int maxLen) {
55+
private String toHex(long data, int maxLen, boolean bigEndian) {
5656
StringBuilder result = new StringBuilder();
5757
for (int i = 0; i < maxLen; i++) {
5858
long b = (data >> i * 8) & 0xff;
59+
if (!bigEndian) {
60+
b = (data >> (maxLen - i - 1) * 8) & 0xff;
61+
}
5962
result.append(String.format("%02x", b));
6063
}
6164
return result.toString();
6265
}
6366

6467
private String toHex(long data) {
65-
return toHex(data, 8);
68+
return toHex(data, 8, true);
6669
}
6770

6871
private long toWasmAddr(long addr) {
@@ -78,7 +81,9 @@ private long stripAddrType(long addr) {
7881
return addr & Long.MAX_VALUE;
7982
}
8083

81-
private byte[] getCodeSection(String filename) throws IOException {
84+
record CodeSection(byte[] data, int offset) {}
85+
86+
private CodeSection getCodeSection(String filename) throws IOException {
8287
byte[] wasmBytes = Files.readAllBytes(Path.of(filename));
8388
int i = 8; // skip header
8489

@@ -96,7 +101,8 @@ private byte[] getCodeSection(String filename) throws IOException {
96101
} while ((b & 0x80) != 0);
97102

98103
if (sectionId == 10) { // code section
99-
return Arrays.copyOfRange(wasmBytes, i, i + size);
104+
System.out.println("Found code section at address " + i);
105+
return new CodeSection(Arrays.copyOfRange(wasmBytes, i, i + size), i);
100106
}
101107

102108
i += size;
@@ -122,7 +128,7 @@ public void start() throws IOException {
122128
debugger.pause();
123129

124130
System.out.println(toHex(4508));
125-
byte[] rawCodeSection = getCodeSection(binaryLocation);
131+
CodeSection codeSection = getCodeSection(binaryLocation);
126132

127133
for (int i = 0; i < regs.length; i++) {
128134
regs[i] = 0x11111111 * (i + 1);
@@ -160,7 +166,7 @@ public void start() throws IOException {
160166
continue;
161167
}
162168

163-
byte[] memory = rawCodeSection;
169+
byte[] memory = codeSection.data;
164170
if (addrType == 1) {
165171
log("Reading from wasm linear memory");
166172
memory = getCurrentState().getMemory().getBytes();
@@ -205,7 +211,10 @@ else if (pkt.startsWith("qXfer:libraries:read")) {
205211
// +--------------------+--------------------+
206212
// <----- 32 bit -----> <----- 32 bit ----->
207213
// Offset 0, module id 0
208-
sendPacket(out, String.format("l<library-list><library name=\"%s\"><section address=\"0x00000000\"/></library></library-list>", new File(binaryLocation).getAbsolutePath()));
214+
// toHex(codeSection.offset, 4)
215+
//"0x00044444"
216+
//sendPacket(out, String.format("l<library-list><library name=\"%s\"><section address=\"0x" + toHex(codeSection.offset, 4, false) + "\"/></library></library-list>", new File(binaryLocation).getAbsolutePath()));
217+
sendPacket(out, String.format("l<library-list><library name=\"%s\"><section address=\"" + "0x00000000" + "\"/></library></library-list>", new File(binaryLocation).getAbsolutePath()));
209218
continue;
210219
}
211220
// TODO: We can probably remove this:
@@ -292,6 +301,7 @@ else if (pkt.startsWith("z")) {
292301
case "qWasmCallStack:1": // Get the callstack for thread 1.
293302
Checkpoint lastCheckpoint = debugger.getCheckpoints().getLast();
294303
WOODDumpResponse state = getCurrentState();
304+
//long currentPc = (long) state.getPc() - codeSection.offset;
295305
long currentPc = (long) state.getPc();
296306
String result = toHex(currentPc);
297307
for (int i = 0; i < state.getCallstack().size() - 1; i++) {

src/main/kotlin/be/ugent/topl/mio/Main.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,8 @@ fun main(args: Array<String>) {
153153
}
154154
"gdbstub" -> {
155155
//val wasmFilename = "main.wasm"
156-
val wasmFilename = "test-dbg.wasm"
157-
val debugger = Debugger(ProcessConnection(config.wdcliPath, wasmFilename, "--no-socket", "--paused")) {
158-
println("Hit breakpoint!")
159-
}
156+
val wasmFilename = "tmp/test-dbg.wasm"
157+
val debugger = Debugger(ProcessConnection(config.wdcliPath, wasmFilename, "--no-socket", "--paused"))
160158
debugger.pause()
161159
debugger.setSnapshotPolicy(Debugger.SnapshotPolicy.Checkpointing())
162160
val stub = GdbStub(debugger, wasmFilename)

0 commit comments

Comments
 (0)