Dynamic ELF stub

This commit is contained in:
Alex 2022-12-14 14:54:13 +02:00
parent 3828186c56
commit 83a5b2a4ee
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
2 changed files with 300 additions and 46 deletions

View File

@ -172,8 +172,10 @@ namespace Execute
memset(MemoryImage, 0, ElfAppSize); memset(MemoryImage, 0, ElfAppSize);
for (uint64_t i = 0; i < TO_PAGES(ElfAppSize); i++) for (uint64_t i = 0; i < TO_PAGES(ElfAppSize); i++)
{ {
pva.Remap((void *)((uint64_t)ProgramHeader->p_vaddr + (i * PAGE_SIZE)), (void *)((uint64_t)MemoryImage + (i * PAGE_SIZE)), Memory::PTFlag::RW | Memory::PTFlag::US); uint64_t Address = (uint64_t)ProgramHeader->p_vaddr;
debug("Mapping: %#lx -> %#lx", (uint64_t)ProgramHeader->p_vaddr + (i * PAGE_SIZE), (uint64_t)MemoryImage + (i * PAGE_SIZE)); Address &= 0xFFFFFFFFFFFFF000;
pva.Remap((void *)((uint64_t)Address + (i * PAGE_SIZE)), (void *)((uint64_t)MemoryImage + (i * PAGE_SIZE)), Memory::PTFlag::RW | Memory::PTFlag::US);
debug("Mapping: %#lx -> %#lx", (uint64_t)Address + (i * PAGE_SIZE), (uint64_t)MemoryImage + (i * PAGE_SIZE));
} }
} }
@ -201,6 +203,210 @@ namespace Execute
debug("memcpy operation: %#lx to %#lx for length %ld", (uint8_t *)BaseImage + ItrProgramHeader.p_offset, MemoryImage + MAddr, ItrProgramHeader.p_filesz); debug("memcpy operation: %#lx to %#lx for length %ld", (uint8_t *)BaseImage + ItrProgramHeader.p_offset, MemoryImage + MAddr, ItrProgramHeader.p_filesz);
break; break;
} }
case PT_DYNAMIC:
{
debug("PT_DYNAMIC - Offset: %#lx VirtAddr: %#lx FileSiz: %ld MemSiz: %ld Align: %#lx",
ItrProgramHeader.p_offset, ItrProgramHeader.p_vaddr,
ItrProgramHeader.p_filesz, ItrProgramHeader.p_memsz, ItrProgramHeader.p_align);
Elf64_Dyn *Dynamic = (Elf64_Dyn *)((uint8_t *)BaseImage + ItrProgramHeader.p_offset);
for (uint64_t i = 0; i < ItrProgramHeader.p_filesz / sizeof(Elf64_Dyn); i++)
{
switch (Dynamic[i].d_tag)
{
case DT_NULL:
debug("DT_NULL");
break;
case DT_NEEDED:
{
fixme("DT_NEEDED - Name: %s", Dynamic[i].d_un.d_ptr);
break;
}
case DT_PLTRELSZ:
{
fixme("DT_PLTRELSZ - Size: %ld", Dynamic[i].d_un.d_val);
}
case DT_PLTGOT:
{
fixme("DT_PLTGOT - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_HASH:
{
fixme("DT_HASH - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_STRTAB:
{
fixme("DT_STRTAB - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_SYMTAB:
{
fixme("DT_SYMTAB - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_RELA:
{
fixme("DT_RELA - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_RELASZ:
{
fixme("DT_RELASZ - Size: %ld", Dynamic[i].d_un.d_val);
break;
}
case DT_RELAENT:
{
fixme("DT_RELAENT - Size: %ld", Dynamic[i].d_un.d_val);
break;
}
case DT_STRSZ:
{
fixme("DT_STRSZ - Size: %ld", Dynamic[i].d_un.d_val);
break;
}
case DT_SYMENT:
{
fixme("DT_SYMENT - Size: %ld", Dynamic[i].d_un.d_val);
break;
}
case DT_INIT:
{
fixme("DT_INIT - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_FINI:
{
fixme("DT_FINI - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_SONAME:
{
fixme("DT_SONAME - Name: %s", Dynamic[i].d_un.d_ptr);
break;
}
case DT_RPATH:
{
fixme("DT_RPATH - Name: %s", Dynamic[i].d_un.d_ptr);
break;
}
case DT_SYMBOLIC:
{
fixme("DT_SYMBOLIC - Name: %s", Dynamic[i].d_un.d_ptr);
break;
}
case DT_REL:
{
fixme("DT_REL - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_RELSZ:
{
fixme("DT_RELSZ - Size: %ld", Dynamic[i].d_un.d_val);
break;
}
case DT_RELENT:
{
fixme("DT_RELENT - Size: %ld", Dynamic[i].d_un.d_val);
break;
}
case DT_PLTREL:
{
fixme("DT_PLTREL - Type: %ld", Dynamic[i].d_un.d_val);
break;
}
case DT_DEBUG:
{
fixme("DT_DEBUG - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_TEXTREL:
{
fixme("DT_TEXTREL - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_JMPREL:
{
fixme("DT_JMPREL - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_BIND_NOW:
{
fixme("DT_BIND_NOW - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_INIT_ARRAY:
{
fixme("DT_INIT_ARRAY - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_FINI_ARRAY:
{
fixme("DT_FINI_ARRAY - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_INIT_ARRAYSZ:
{
fixme("DT_INIT_ARRAYSZ - Size: %ld", Dynamic[i].d_un.d_val);
break;
}
case DT_FINI_ARRAYSZ:
{
fixme("DT_FINI_ARRAYSZ - Size: %ld", Dynamic[i].d_un.d_val);
break;
}
case DT_RUNPATH:
{
fixme("DT_RUNPATH - Name: %s", Dynamic[i].d_un.d_ptr);
break;
}
case DT_FLAGS:
{
fixme("DT_FLAGS - Flags: %#lx", Dynamic[i].d_un.d_val);
break;
}
case DT_PREINIT_ARRAY:
{
fixme("DT_PREINIT_ARRAY - Address: %#lx", Dynamic[i].d_un.d_ptr);
break;
}
case DT_PREINIT_ARRAYSZ:
{
fixme("DT_PREINIT_ARRAYSZ - Size: %ld", Dynamic[i].d_un.d_val);
break;
}
/* ... */
default:
fixme("DT: %ld", Dynamic[i].d_tag);
break;
}
if (Dynamic[i].d_tag == DT_NULL)
break;
}
break;
}
case PT_INTERP: // Do I have to do anything here?
{
debug("PT_INTERP - Offset: %#lx VirtAddr: %#lx FileSiz: %ld MemSiz: %ld Align: %#lx",
ItrProgramHeader.p_offset, ItrProgramHeader.p_vaddr,
ItrProgramHeader.p_filesz, ItrProgramHeader.p_memsz, ItrProgramHeader.p_align);
char *Interpreter = (char *)KernelAllocator.RequestPages(TO_PAGES(ItrProgramHeader.p_filesz));
memcpy(Interpreter, (uint8_t *)BaseImage + ItrProgramHeader.p_offset, ItrProgramHeader.p_filesz);
fixme("Interpreter: %s", Interpreter);
KernelAllocator.FreePages(Interpreter, TO_PAGES(ItrProgramHeader.p_filesz));
break;
}
/* ... */
case PT_PHDR:
{
debug("PT_PHDR - Offset: %#lx VirtAddr: %#lx FileSiz: %ld MemSiz: %ld Align: %#lx",
ItrProgramHeader.p_offset, ItrProgramHeader.p_vaddr,
ItrProgramHeader.p_filesz, ItrProgramHeader.p_memsz, ItrProgramHeader.p_align);
break;
}
default: default:
{ {
warn("Unknown or unsupported program header type: %d", ItrProgramHeader.p_type); warn("Unknown or unsupported program header type: %d", ItrProgramHeader.p_type);

View File

@ -133,6 +133,26 @@ typedef struct elf64_sym
Elf64_Xword st_size; /* Associated symbol size */ Elf64_Xword st_size; /* Associated symbol size */
} Elf64_Sym; } Elf64_Sym;
struct Elf32_Dyn
{
Elf32_Sword d_tag; /* Type of dynamic table entry. */
union
{
Elf32_Word d_val; /* Integer value of entry. */
Elf32_Addr d_ptr; /* Pointer value of entry. */
} d_un;
};
struct Elf64_Dyn
{
Elf64_Sxword d_tag; /* Type of dynamic table entry. */
union
{
Elf64_Xword d_val; /* Integer value of entry. */
Elf64_Addr d_ptr; /* Pointer value of entry. */
} d_un;
};
enum Elf_Ident enum Elf_Ident
{ {
EI_MAG0 = 0, // 0x7F EI_MAG0 = 0, // 0x7F
@ -248,50 +268,78 @@ enum SegmentTypes
PT_HIPROC = 0x7fffffff PT_HIPROC = 0x7fffffff
}; };
#define DT_NULL 0 /* Marks end of dynamic section */ /* https://docs.oracle.com/cd/E19683-01/817-3677/chapter6-42444/index.html */
#define DT_NEEDED 1 /* Name of needed library */ enum DynamicArrayTags
#define DT_PLTRELSZ 2 /* Size in bytes of PLT relocs */ {
#define DT_PLTGOT 3 /* Processor defined value */ DT_NULL = 0,
#define DT_HASH 4 /* Address of symbol hash table */ DT_NEEDED = 1,
#define DT_STRTAB 5 /* Address of string table */ DT_PLTRELSZ = 2,
#define DT_SYMTAB 6 /* Address of symbol table */ DT_PLTGOT = 3,
#define DT_RELA 7 /* Address of Rela relocs */ DT_HASH = 4,
#define DT_RELASZ 8 /* Total size of Rela relocs */ DT_STRTAB = 5,
#define DT_RELAENT 9 /* Size of one Rela reloc */ DT_SYMTAB = 6,
#define DT_STRSZ 10 /* Size of string table */ DT_RELA = 7,
#define DT_SYMENT 11 /* Size of one symbol table entry */ DT_RELASZ = 8,
#define DT_INIT 12 /* Address of init function */ DT_RELAENT = 9,
#define DT_FINI 13 /* Address of termination function */ DT_STRSZ = 10,
#define DT_SONAME 14 /* Name of shared object */ DT_SYMENT = 11,
#define DT_RPATH 15 /* Library search path (deprecated) */ DT_INIT = 12,
#define DT_SYMBOLIC 16 /* Start symbol search here */ DT_FINI = 13,
#define DT_REL 17 /* Address of Rel relocs */ DT_SONAME = 14,
#define DT_RELSZ 18 /* Total size of Rel relocs */ DT_RPATH = 15,
#define DT_RELENT 19 /* Size of one Rel reloc */ DT_SYMBOLIC = 16,
#define DT_PLTREL 20 /* Type of reloc in PLT */ DT_REL = 17,
#define DT_DEBUG 21 /* For debugging; unspecified */ DT_RELSZ = 18,
#define DT_TEXTREL 22 /* Reloc might modify .text */ DT_RELENT = 19,
#define DT_JMPREL 23 /* Address of PLT relocs */ DT_PLTREL = 20,
#define DT_BIND_NOW 24 /* Process relocations of object */ DT_DEBUG = 21,
#define DT_INIT_ARRAY 25 /* Array with addresses of init fct */ DT_TEXTREL = 22,
#define DT_FINI_ARRAY 26 /* Array with addresses of fini fct */ DT_JMPREL = 23,
#define DT_INIT_ARRAYSZ 27 /* Size in bytes of DT_INIT_ARRAY */ DT_BIND_NOW = 24,
#define DT_FINI_ARRAYSZ 28 /* Size in bytes of DT_FINI_ARRAY */ DT_INIT_ARRAY = 25,
#define DT_RUNPATH 29 /* Library search path */ DT_FINI_ARRAY = 26,
#define DT_FLAGS 30 /* Flags for the object being loaded */ DT_INIT_ARRAYSZ = 27,
#define DT_ENCODING 32 /* Start of encoded range */ DT_FINI_ARRAYSZ = 28,
#define DT_PREINIT_ARRAY 32 /* Array with addresses of preinit fct*/ DT_RUNPATH = 29,
#define DT_PREINIT_ARRAYSZ 33 /* size in bytes of DT_PREINIT_ARRAY */ DT_FLAGS = 30,
#define DT_SYMTAB_SHNDX 34 /* Address of SYMTAB_SHNDX section */ DT_ENCODING = 32,
#define DT_RELRSZ 35 /* Total size of RELR relative relocations */ DT_PREINIT_ARRAY = 32,
#define DT_RELR 36 /* Address of RELR relative relocations */ DT_PREINIT_ARRAYSZ = 33,
#define DT_RELRENT 37 /* Size of one RELR relative relocaction */ DT_LOOS = 0x6000000d,
#define DT_NUM 38 /* Number used */ DT_SUNW_RTLDINF = 0x6000000e,
#define DT_LOOS 0x6000000d /* Start of OS-specific */ DT_HIOS = 0x6ffff000,
#define DT_HIOS 0x6ffff000 /* End of OS-specific */ DT_VALRNGLO = 0x6ffffd00,
#define DT_LOPROC 0x70000000 /* Start of processor-specific */ DT_CHECKSUM = 0x6ffffdf8,
#define DT_HIPROC 0x7fffffff /* End of processor-specific */ DT_PLTPADSZ = 0x6ffffdf9,
#define DT_PROCNUM DT_MIPS_NUM /* Most used by any processor */ DT_MOVEENT = 0x6ffffdfa,
DT_MOVESZ = 0x6ffffdfb,
DT_FEATURE_1 = 0x6ffffdfc,
DT_POSFLAG_1 = 0x6ffffdfd,
DT_SYMINSZ = 0x6ffffdfe,
DT_SYMINENT = 0x6ffffdff,
DT_VALRNGHI = 0x6ffffdff,
DT_ADDRRNGLO = 0x6ffffe00,
DT_CONFIG = 0x6ffffefa,
DT_DEPAUDIT = 0x6ffffefb,
DT_AUDIT = 0x6ffffefc,
DT_PLTPAD = 0x6ffffefd,
DT_MOVETAB = 0x6ffffefe,
DT_SYMINFO = 0x6ffffeff,
DT_ADDRRNGHI = 0x6ffffeff,
DT_RELACOUNT = 0x6ffffff9,
DT_RELCOUNT = 0x6ffffffa,
DT_FLAGS_1 = 0x6ffffffb,
DT_VERDEF = 0x6ffffffc,
DT_VERDEFNUM = 0x6ffffffd,
DT_VERNEED = 0x6ffffffe,
DT_VERNEEDNUM = 0x6fffffff,
DT_LOPROC = 0x70000000,
DT_SPARC_REGISTER = 0x70000001,
DT_AUXILIARY = 0x7ffffffd,
DT_USED = 0x7ffffffe,
DT_FILTER = 0x7fffffff,
DT_HIPROC = 0x7fffffff
};
// used for Elf64_Sym st_info // used for Elf64_Sym st_info
#define ELF32_ST_BIND(info) ((info) >> 4) #define ELF32_ST_BIND(info) ((info) >> 4)