--- nono/vm/ram.cpp 2026/04/29 17:04:42 1.1.1.4 +++ nono/vm/ram.cpp 2026/04/29 17:04:45 1.1.1.5 @@ -362,6 +362,65 @@ RAMDevice::Load_aout(const char *name, c #define elf32toh(x) ((ehdr->e_ident[EI_DATA] == ELFDATA2MSB) ? \ be32toh(x) : le32toh(x)) +// XXX このへん、そのうちきれいにする +#define GetElfStr1(array, v) GetElfStr(array, countof(array), v) +static const char * +GetElfStr(const std::pair *map, uint mapcount, uint v) +{ + for (int i = 0; i < mapcount; i++) { + if (map[i].first == v) { + return map[i].second; + } + } + return "?"; +} + +static std::pair class_str[] = { + { ELFCLASSNONE, "NONE" }, + { ELFCLASS32, "32bit" }, + { ELFCLASS64, "64bit" }, +}; +#define GetElfClassStr(x) GetElfStr1(class_str, (x)) + +static std::pair data_str[] = { + { ELFDATANONE, "NONE" }, + { ELFDATA2LSB, "LE" }, + { ELFDATA2MSB, "BE" }, +}; +#define GetElfDataStr(x) GetElfStr1(data_str, (x)) + +static std::pair etype_str[] = { + { ET_NONE, "ET_NONE" }, + { ET_REL, "ET_REL" }, + { ET_EXEC, "ET_EXEC" }, + { ET_DYN, "ET_DYN" }, + { ET_CORE, "ET_CORE" }, +}; +#define GetElfETypeStr(x) GetElfStr1(etype_str, (x)) + +static std::pair machine_str[] = { + // 多いので関係分のみ + { EM_NONE, "NONE" }, + { EM_68K, "Motorola 68000" }, + { EM_88K, "Motorola 88000" }, +}; +#define GetElfMachineStr(x) GetElfStr1(machine_str, (x)) + +static std::pair ptype_str[] = { + { PT_NULL, "PT_NULL" }, + { PT_LOAD, "PT_LOAD" }, + { PT_DYNAMIC, "PT_DYNAMIC" }, + { PT_INTERP, "PT_INTERP" }, + { PT_NOTE, "PT_NOTE" }, + { PT_SHLIB, "PT_SHLIB" }, + { PT_PHDR, "PT_PHDR" }, + { PT_GNU_RELRO, "PT_GNU_RELRO" }, // GNU specific + { PT_OPENBSD_RANDOMIZE, "PT_OPENBSD_RANDOMIZE" }, + { PT_OPENBSD_WXNEEDED, "PT_OPENBSD_WXNEEDED" }, + { PT_OPENBSD_BOOTDATA, "PT_OPENBSD_BOOTDATA" }, +}; +#define GetElfPTypeStr(x) GetElfStr1(ptype_str, (x)) + // ホストの a.out をゲストの RAM に読み込む。 // target_aout_mid は a.out での machine id (機種情報) で、こっちで ELF の // machine type に読み替える。 @@ -394,11 +453,18 @@ RAMDevice::Load_elf32(const char *name, uint16 e_type = elf16toh(ehdr->e_type); uint16 e_machine = elf16toh(ehdr->e_machine); uint32 e_entry = elf32toh(ehdr->e_entry); - putlog(1, "EI_CLASS = $%02x", ehdr->e_ident[EI_CLASS]); - putlog(1, "EI_DATA = $%02x", ehdr->e_ident[EI_DATA]); - putlog(1, "e_type = $%02x", e_type); - putlog(1, "e_machine = $%02x", e_machine); - putlog(1, "e_entry = $%08x", e_entry); + if (loglevel >= 1) { + uint n; + n = ehdr->e_ident[EI_CLASS]; + putlog("EI_CLASS = $%02x (%s)", n, GetElfClassStr(n)); + n = ehdr->e_ident[EI_DATA]; + putlog("EI_DATA = $%02x (%s)", n, GetElfDataStr(n)); + + putlog("e_type = $%02x (%s)", e_type, GetElfETypeStr(e_type)); + putlog("e_machine = $%02x (%s)", e_machine, + GetElfMachineStr(e_machine)); + putlog("e_entry = $%08x", e_entry); + } if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) { warnx("Load_elf32 \"%s\" not ELF32 format", name); @@ -422,9 +488,11 @@ RAMDevice::Load_elf32(const char *name, uint32 e_phoff = elf32toh(ehdr->e_phoff); uint16 e_phentsize = elf16toh(ehdr->e_phentsize); uint16 e_phnum = elf16toh(ehdr->e_phnum); - putlog(1, "e_phoff = $%08x", e_phoff); - putlog(1, "e_phentsize = $%04x", e_phentsize); - putlog(1, "e_phnum = %d", e_phnum); + if (loglevel >= 1) { + putlog("e_phoff = $%08x", e_phoff); + putlog("e_phentsize = $%04x", e_phentsize); + putlog("e_phnum = %d", e_phnum); + } if (e_phentsize != sizeof(Elf32_Phdr)) { warnx("Load_elf32 \"%s\" unknown program header size 0x%x", name, e_phentsize); @@ -442,11 +510,14 @@ RAMDevice::Load_elf32(const char *name, uint32 p_vaddr = elf32toh(phdr->p_vaddr); // 仮想アドレス uint32 p_filesz = elf32toh(phdr->p_filesz); // 読み込み元ファイルサイズ uint32 p_memsz = elf32toh(phdr->p_memsz); // 書き込み先サイズ - putlog(1, "[%d] p_type = $%08x", j, p_type); - putlog(1, "[%d] p_offset = $%08x", j, p_offset); - putlog(1, "[%d] p_vaddr = $%08x", j, p_vaddr); - putlog(1, "[%d] p_filesz = $%08x", j, p_filesz); - putlog(1, "[%d] p_memsz = $%08x", j, p_memsz); + if (loglevel >= 1) { + putlog("[%d] p_type = $%08x (%s)", j, + p_type, GetElfPTypeStr(p_type)); + putlog(" p_offset = $%08x", p_offset); + putlog(" p_vaddr = $%08x", p_vaddr); + putlog(" p_filesz = $%08x", p_filesz); + putlog(" p_memsz = $%08x", p_memsz); + } if (p_type == PT_LOAD) { ds.SetAddr(p_vaddr); @@ -458,12 +529,16 @@ RAMDevice::Load_elf32(const char *name, for (; i < p_memsz; i++) { ds.Write8(0); } - } else if (p_type == 0x65a3dbe6) { // PT_OPENBSD_RANDOMIZE + } else if (p_type == PT_NOTE) { + // ベンダー文字列みたいなのらしいので無視してよい + continue; + } else if (p_type == PT_OPENBSD_RANDOMIZE) { // XXX どうする? continue; } else { - warnx("Load_elf32 \"%s\" p_type %d not supported", name, p_type); + warnx("Load_elf32 \"%s\" p_type 0x%x(%s) not supported", + name, p_type, GetElfPTypeStr(p_type)); return 0; } }