vxBoot/src/loader/info.c

108 lines
2.8 KiB
C

#include "vxBoot/loader.h"
#include "vxBoot/terminal.h"
#include "vxBoot/fs/smemfs.h"
#include <stdio.h>
#include <string.h>
/* section_get_id() : generate id string */
static char *section_get_id(int id)
{
static char buff[6];
snprintf(buff, 6, "[%2d]", id);
return (buff);
}
/* section_get_name() : get the section name*/
static char *section_get_name(void const *inode, int nameoff, off_t shstroff)
{
static char buff[10];
smemfs_pread(inode, buff, 9, nameoff + shstroff);
if (strlen(buff) >= 8) {
buff[8] = '\0';
buff[7] = '.';
}
return (buff);
}
/* section_get_type() : return the section type*/
static char const *section_get_type(int type)
{
static struct {
int type;
char const * const name;
} const types[] = {
{.type = SHT_NULL, .name = "NULL"},
{.type = SHT_PROGBITS, .name = "PROGBITS"},
{.type = SHT_SYMTAB, .name = "SYMTAB"},
{.type = SHT_STRTAB, .name = "STRTAB"},
{.type = SHT_RELA, .name = "RELA"},
{.type = SHT_HASH, .name = "HASH"},
{.type = SHT_DYNAMIC, .name = "DYNAMIC"},
{.type = SHT_NOTE, .name = "NOTE"},
{.type = SHT_NOBITS, .name = "NOBITS"},
{.type = SHT_REL, .name = "REL"},
{.type = SHT_SHLIB, .name = "SHLIB"},
{.type = SHT_DYNSYM, .name = "DYNSYM"},
{.type = SHT_INIT_ARRAY, .name = "INIT_ARRAY"},
{.type = SHT_FINI_ARRAY, .name = "FINI_ARRAY"},
{.type = SHT_PREINIT_ARRAY, .name = "PREINIT_ARRAY"},
{.type = SHT_GROUP, .name = "GROUP"},
{.type = SHT_SYMTAB_SHNDX, .name = "SYMTAB SECTION INDICES"},
{.type = -1, .name = NULL},
};
for (int i = 0; types[i].name != NULL; ++i) {
if (types[i].type == type)
return (types[i].name);
}
return ("UNKNOWN");
}
/* loader_info() : display ELF PIE file information */
//TODO: secure
int loader_info(struct smemfs_inode const * inode, Elf32_Ehdr *hdr)
{
Elf32_Shdr shdr;
off_t stroff;
off_t shoff;
/* find the section header table */
shoff = hdr->e_shoff;
/* find the section string table ".strtable" */
smemfs_pread(
inode,
&shdr, sizeof(Elf32_Shdr),
shoff + (sizeof(Elf32_Shdr) * hdr->e_shstrndx)
);
stroff = shdr.sh_offset;
/* display section information */
terminal_write(
"There are %d section headers, startting at offset %#x\n",
hdr->e_shnum, hdr->e_shoff
);
terminal_write("Section Headers:\n");
terminal_write(
"%4s %-8s %-8s %-8s %-8s\n",
"[Nr]", "Name", "Type", "Address", "Offset"
);
for (int i = 0; i < hdr->e_shnum; ++i) {
smemfs_pread(inode, &shdr, sizeof(Elf32_Shdr), shoff);
terminal_write(
"%4s %-8s %-8s %08x %08x\n",
section_get_id(i),
section_get_name(inode, shdr.sh_name, stroff),
section_get_type(shdr.sh_type),
shdr.sh_addr,
shdr.sh_offset
);
shoff += sizeof(Elf32_Shdr);
}
return (0);
}