Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit 964a40d

Browse files
authored
Merge pull request #3 from HexRabbit/fix-segment-load
Correct elf segments loading
2 parents 27807df + 1a37c23 commit 964a40d

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

emu-rv32i.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ uint64_t mtimecmp;
199199
/* virtual start address for index 0 in the ram array */
200200
uint32_t ram_start;
201201

202+
/* program entry point */
203+
uint32_t start;
204+
202205
/* last byte of the memory initialized and temporary value */
203206
uint32_t ram_last = 0;
204207
uint32_t ram_curr = 0;
@@ -2036,26 +2039,45 @@ int main(int argc, char** argv)
20362039

20372040
/* for compliance test */
20382041
if (strcmp(name, "_start") == 0) {
2039-
ram_start = sym.st_value;
2042+
start = sym.st_value;
20402043
}
20412044

20422045
/* for zephyr */
20432046
if (strcmp(name, "__reset") == 0) {
2044-
ram_start = sym.st_value;
2047+
start = sym.st_value;
20452048
}
20462049
if (strcmp(name, "__irq_wrapper") == 0) {
20472050
mtvec = sym.st_value;
20482051
}
20492052
}
20502053
}
20512054
}
2055+
2056+
/* set .text segment as the base address */
2057+
scn = NULL;
2058+
size_t shstrndx;
2059+
elf_getshdrstrndx(elf, &shstrndx);
2060+
while ((scn = elf_nextscn(elf, scn)) != NULL) {
2061+
gelf_getshdr(scn, &shdr);
2062+
const char *name = elf_strptr(elf, shstrndx, shdr.sh_name);
2063+
2064+
if (shdr.sh_type == SHT_PROGBITS) {
2065+
if (strcmp(name, ".text") == 0) {
2066+
ram_start = shdr.sh_addr;
2067+
break;
2068+
}
2069+
}
2070+
}
2071+
20522072
#ifdef DEBUG_OUTPUT
20532073
printf("begin_signature: 0x%08x\n", begin_signature);
20542074
printf("end_signature: 0x%08x\n", end_signature);
2055-
printf("start: 0x%08x\n", ram_start);
2075+
printf("ram_start: 0x%08x\n", ram_start);
2076+
printf("entry point: 0x%08x\n", start);
20562077
#endif
20572078

20582079
/* scan for program */
2080+
scn = NULL;
20592081
while ((scn = elf_nextscn(elf, scn)) != NULL) {
20602082
gelf_getshdr(scn, &shdr);
20612083
if (shdr.sh_type == SHT_PROGBITS) {
@@ -2144,7 +2166,8 @@ int main(int argc, char** argv)
21442166
uint64_t ns1 = get_clock();
21452167

21462168
/* run program in emulator */
2147-
pc = ram_start;
2169+
pc = start;
2170+
reg[2] = ram_start + RAM_SIZE;
21482171
riscv_cpu_interp_x32();
21492172

21502173
uint64_t ns2 = get_clock();

0 commit comments

Comments
 (0)