> compile NEMU

ysyx_22040000 李心杨
Linux calcite 6.6.19 #1-NixOS SMP PREEMPT_DYNAMIC Fri Mar  1 12:35:11 UTC 2024 x86_64 GNU/Linux
 01:52:09  up  15:28,  2 users,  load average: 0.54, 0.46, 0.44
This commit is contained in:
tracer-ysyx 2024-03-14 01:52:09 +08:00 committed by xinyangli
parent 061167cd10
commit 2f1f38a359
3 changed files with 54 additions and 683 deletions

File diff suppressed because one or more lines are too long

View file

@ -40,6 +40,7 @@ static void welcome() {
void sdb_set_batch_mode(); void sdb_set_batch_mode();
static char *log_file = NULL; static char *log_file = NULL;
static char *elf_file = NULL;
static char *diff_so_file = NULL; static char *diff_so_file = NULL;
static char *img_file = NULL; static char *img_file = NULL;
static int difftest_port = 1234; static int difftest_port = 1234;
@ -73,6 +74,7 @@ static int parse_args(int argc, char *argv[]) {
{"diff" , required_argument, NULL, 'd'}, {"diff" , required_argument, NULL, 'd'},
{"port" , required_argument, NULL, 'p'}, {"port" , required_argument, NULL, 'p'},
{"help" , no_argument , NULL, 'h'}, {"help" , no_argument , NULL, 'h'},
{"elf" , required_argument, NULL, 'f'},
{0 , 0 , NULL, 0 }, {0 , 0 , NULL, 0 },
}; };
int o; int o;
@ -82,6 +84,7 @@ static int parse_args(int argc, char *argv[]) {
case 'p': sscanf(optarg, "%d", &difftest_port); break; case 'p': sscanf(optarg, "%d", &difftest_port); break;
case 'l': log_file = optarg; break; case 'l': log_file = optarg; break;
case 'd': diff_so_file = optarg; break; case 'd': diff_so_file = optarg; break;
case 'f': elf_file = optarg; break;
case 1: img_file = optarg; return 0; case 1: img_file = optarg; return 0;
default: default:
printf("Usage: %s [OPTION...] IMAGE [args]\n\n", argv[0]); printf("Usage: %s [OPTION...] IMAGE [args]\n\n", argv[0]);
@ -89,6 +92,7 @@ static int parse_args(int argc, char *argv[]) {
printf("\t-l,--log=FILE output log to FILE\n"); printf("\t-l,--log=FILE output log to FILE\n");
printf("\t-d,--diff=REF_SO run DiffTest with reference REF_SO\n"); printf("\t-d,--diff=REF_SO run DiffTest with reference REF_SO\n");
printf("\t-p,--port=PORT run DiffTest with port PORT\n"); printf("\t-p,--port=PORT run DiffTest with port PORT\n");
printf("\t-f,--elf=PORT elf file with debug info\n");
printf("\n"); printf("\n");
exit(0); exit(0);
} }
@ -126,6 +130,11 @@ void init_monitor(int argc, char *argv[]) {
/* Initialize the simple debugger. */ /* Initialize the simple debugger. */
init_sdb(); init_sdb();
if(elf_file != NULL) {
void init_elf(const char *path);
init_elf(elf_file);
}
#ifndef CONFIG_ISA_loongarch32r #ifndef CONFIG_ISA_loongarch32r
IFDEF(CONFIG_ITRACE, init_disasm( IFDEF(CONFIG_ITRACE, init_disasm(
MUXDEF(CONFIG_ISA_x86, "i686", MUXDEF(CONFIG_ISA_x86, "i686",

View file

@ -1,2 +1,47 @@
#include <common.h>
#include <elf.h> #include <elf.h>
// Put this into another file
#ifdef CONFIG_FTRACE
// static vaddr_t g_function_stack[CONFIG_FTRACE_STACK_SIZE] = {0};
#endif
#define FAILED_GOTO(tag, exp) do {if((exp)) goto tag;} while(0)
void init_elf(const char *path) {
FILE *elf_file = fopen(path, "rb");
Elf32_Ehdr header;
Elf32_Shdr section_header, *psh;
FAILED_GOTO(failed_nosym, fread(&header, sizeof(Elf32_Ehdr), 1, elf_file) > 0);
FAILED_GOTO(failed_nosym, fseek(elf_file, header.e_shoff, SEEK_SET) != 0);
FAILED_GOTO(failed_nosym, fread(&section_header, header.e_shentsize, header.e_shnum, elf_file) > 0);
Elf32_Shdr *symtab = NULL, *strtab = NULL;
for(int i = 0; i < header.e_shnum; i++) {
psh = &section_header + i;
if (psh->sh_type == SHT_SYMTAB) {
symtab = psh;
} else if (psh->sh_type == SHT_STRTAB) {
strtab = psh;
}
}
int sym_length = symtab->sh_entsize / sizeof(Elf32_Sym);
Elf32_Sym *sym = calloc(sym_length, sizeof(Elf32_Sym));
FAILED_GOTO(failed, fseek(elf_file, symtab->sh_offset, SEEK_SET) != 0);
FAILED_GOTO(failed, fread(&sym, sizeof(Elf32_Sym), sym_length, elf_file) > 0);
for(int j = 0; j < sym_length; j++) {
if(ELF32_ST_TYPE(sym[j].st_info) != STT_FUNC) continue;
// Only read function type symbol
char func[30] = "";
FAILED_GOTO(failed, fseek(elf_file, sym[j].st_name + strtab->sh_offset, SEEK_SET) != 0);
FAILED_GOTO(failed, fgets(func, 30, elf_file) > 0);
puts(func);
}
failed:
free(sym);
failed_nosym:
;
}