feature (ELF): implemented basic elf loading architecture and symbol extraction to be able to implement stack-tracing
This commit is contained in:
parent
d05e5cc878
commit
9f81658a90
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the “Software”), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef NOX_ELF_H
|
||||
#define NOX_ELF_H
|
||||
|
||||
#include "drivers/elf/header.h"
|
||||
#include "drivers/elf/section.h"
|
||||
#include "utils/symbol.h"
|
||||
|
||||
typedef struct {
|
||||
elf_header_x64_T header;
|
||||
uint64_t num_symbols;
|
||||
symbol_T* symbols;
|
||||
void* string_table;
|
||||
} elf_executable_T;
|
||||
|
||||
typedef struct {
|
||||
elf_executable_T* executable;
|
||||
elf_section_T* symbol_table;
|
||||
elf_section_T* section_header_string_table;
|
||||
|
||||
uint8_t* buffer;
|
||||
} elf_executable_temp_T;
|
||||
|
||||
elf_executable_T* elf_executable_create (uint8_t* buffer);
|
||||
void elf_executable_destruct (elf_executable_T* executable);
|
||||
|
||||
#endif //NOX_ELF_H
|
|
@ -0,0 +1,172 @@
|
|||
/*
|
||||
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the “Software”), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef NOX_ELF_HEADER_H
|
||||
#define NOX_ELF_HEADER_H
|
||||
|
||||
#include "utils/stdtypes.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
typedef enum {
|
||||
ELF_ARCH_X32 = 0x01,
|
||||
ELF_ARCH_X64 = 0x02,
|
||||
ELF_ARCH_UNKNOWN = 0xFF
|
||||
} elf_target_architecture_E;
|
||||
|
||||
typedef enum {
|
||||
ELF_ENDIAN_LITTLE = 0x01,
|
||||
ELF_ENDIAN_BIG = 0x02,
|
||||
ELF_ENDIAN_UNKNOWN = 0xFF
|
||||
} elf_endianness_E;
|
||||
|
||||
typedef enum {
|
||||
ELF_SYSABI_SYSTEM_V = 0x00,
|
||||
ELF_SYSABI_HP_UX = 0x01,
|
||||
ELF_SYSABI_NETBSD = 0x02,
|
||||
ELF_SYSABI_LINUX = 0x03,
|
||||
ELF_SYSABI_GNU_HURD = 0x04,
|
||||
ELF_SYSABI_SOLARIS = 0x06,
|
||||
ELF_SYSABI_AIX = 0x07,
|
||||
ELF_SYSABI_IRIX = 0x08,
|
||||
ELF_SYSABI_FREEBSD = 0x09,
|
||||
ELF_SYSABI_TRU64 = 0x0A,
|
||||
ELF_SYSABI_NOVELL_MODESTO = 0x0B,
|
||||
ELF_SYSABI_OPENBSD = 0x0C,
|
||||
ELF_SYSABI_OPENVMS = 0x0D,
|
||||
ELF_SYSABI_NONSTOP_KERNEL = 0x0E,
|
||||
ELF_SYSABI_AROS = 0x0F,
|
||||
ELF_SYSABI_FENIX_OS = 0x10,
|
||||
ELF_SYSABI_NUXI_CLOUD_ABI = 0x11,
|
||||
ELF_SYSABI_STRATUS_OPENVOS = 0x12,
|
||||
|
||||
ELF_SYSABI_UNKNOWN = 0xFF
|
||||
} elf_sysabi_E;
|
||||
|
||||
typedef enum {
|
||||
ELF_OBJECT_NONE = 0x00,
|
||||
ELF_OBJECT_RELOCATABLE = 0x01,
|
||||
ELF_OBJECT_EXECUTABLE = 0x02,
|
||||
ELF_OBJECT_DYNAMIC_LIB = 0x03,
|
||||
ELF_OBJECT_CORE = 0x04
|
||||
} elf_object_type_E;
|
||||
|
||||
typedef enum {
|
||||
ELF_ISA_UNDEFINED = 0x0000,
|
||||
ELF_ISA_WE_32100 = 0x0001,
|
||||
ELF_ISA_SPARC = 0x0002,
|
||||
ELF_ISA_X86_32 = 0x0003,
|
||||
ELF_ISA_MOTOROLA_68000 = 0x0004,
|
||||
ELF_ISA_MOTOROLA_88000 = 0x0005,
|
||||
ELF_ISA_INTEL_MCU = 0x0006,
|
||||
ELF_ISA_INTEL_80860 = 0x0007,
|
||||
ELF_ISA_MIPS = 0x0008,
|
||||
ELF_ISA_IBM_SYSTEM_370 = 0x0009,
|
||||
ELF_ISA_MIPS_RS3000_LE = 0x000A,
|
||||
ELF_ISA_HP_PA_RISC = 0x000E,
|
||||
ELF_ISA_INTEL_80960 = 0x0013,
|
||||
ELF_ISA_POWERPC = 0x0014,
|
||||
ELF_ISA_POWERPC_64 = 0x0015,
|
||||
ELF_ISA_S390 = 0x0016,
|
||||
ELF_ISA_IBM_SPU_SPC = 0x0017,
|
||||
ELF_ISA_NEC_V800 = 0x0024,
|
||||
ELF_ISA_FUJITSU_FR20 = 0x0025,
|
||||
ELF_ISA_TRW_RH32 = 0x0026,
|
||||
ELF_ISA_MOTOROLA_RCE = 0x0027,
|
||||
ELF_ISA_ARM32 = 0x0028,
|
||||
ELF_ISA_DIGITAL_ALPHA = 0x0029,
|
||||
ELF_ISA_SUPER_H = 0x002A,
|
||||
ELF_ISA_SPARC_V9 = 0x002B,
|
||||
ELF_ISA_SIEMENS_TRICORE = 0x002C,
|
||||
ELF_ISA_ARGONAUT_RISC = 0x002D,
|
||||
ELF_ISA_HITACHI_H8_300 = 0x002E,
|
||||
ELF_ISA_HITACHI_H8_300H = 0x002F,
|
||||
ELF_ISA_HITACHI_H8S = 0x0030,
|
||||
ELF_ISA_HITACHI_H8_500 = 0x0031,
|
||||
ELF_ISA_IA_64 = 0x0032,
|
||||
ELF_ISA_STANFORD_MIPS_X = 0x0033,
|
||||
ELF_ISA_MOTOROLA_COLDFIRE = 0x0034,
|
||||
ELF_ISA_MOTOROLA_68HC12 = 0x0035,
|
||||
ELF_ISA_FUJITSU_MMA_ACCEL = 0x0036,
|
||||
ELF_ISA_SIEMENS_PCP = 0x0037,
|
||||
ELF_ISA_SONY_NCPU_RISC = 0x0038,
|
||||
ELF_ISA_DENSO_NDR1 = 0x0039,
|
||||
ELF_ISA_MOTOROLA_STARCORE = 0x003A,
|
||||
ELF_ISA_TOYOTA_ME16 = 0x003B,
|
||||
ELF_ISA_ST100 = 0x003C,
|
||||
ELF_ISA_TINY_J = 0x003D,
|
||||
ELF_ISA_X86_64 = 0x003E,
|
||||
ELF_ISA_SONY_DSP = 0x003F,
|
||||
ELF_ISA_PDP_10 = 0x0040,
|
||||
ELF_ISA_PDP_11 = 0x0041,
|
||||
ELF_ISA_SIEMENS_FX66 = 0x0042,
|
||||
ELF_ISA_STM_ST9_PLUS = 0x0043,
|
||||
ELF_ISA_STM_ST7 = 0x0044,
|
||||
ELF_ISA_MOTOROLA_MC68HC16 = 0x0045,
|
||||
ELF_ISA_MOTOROLA_MC68HC11 = 0x0046,
|
||||
ELF_ISA_MOTOROLA_MC68HC08 = 0x0047,
|
||||
ELF_ISA_MOTOROLA_MC68HC05 = 0x0048,
|
||||
ELF_ISA_SGI_SVX = 0x0049,
|
||||
ELF_ISA_STM_ST19 = 0x004A,
|
||||
ELF_ISA_DIGITAL_VAX = 0x004B,
|
||||
ELF_ISA_AXIS_EMBEDDED_32 = 0x004C,
|
||||
ELF_ISA_INFINEON_EMBEDDED_32 = 0x004D,
|
||||
ELF_ISA_ELEMENT_14_DSP = 0x004E,
|
||||
ELF_ISA_LSI_LOGIC_16_DSP = 0x004F,
|
||||
ELF_ISA_TMS_S320_C6000 = 0x008C,
|
||||
ELF_ISA_MCST_ELBRUS_E2K = 0x00AF,
|
||||
ELF_ISA_ARM64 = 0x00B7,
|
||||
ELF_ISA_ZILOG_Z80 = 0x00DC,
|
||||
ELF_ISA_RISC_V = 0x00F3,
|
||||
ELF_ISA_BERKELEY_PACKET_FILTER = 0x00F7,
|
||||
ELF_ISA_WDC65C816 = 0x0101,
|
||||
|
||||
ELF_ISA_UNKNOWN = 0xFFFF
|
||||
} cpu_instruction_set_E;
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint8_t identity[16];
|
||||
uint16_t type;
|
||||
uint16_t isa;
|
||||
uint32_t version;
|
||||
uint64_t address_entry_point;
|
||||
uint64_t offset_program_header;
|
||||
uint64_t offset_section_header;
|
||||
uint32_t flags;
|
||||
uint16_t len_header;
|
||||
uint16_t len_program_header_entry;
|
||||
uint16_t num_program_header_entries;
|
||||
uint16_t len_section_header_entry;
|
||||
uint16_t num_section_header_entries;
|
||||
uint16_t string_section_index;
|
||||
} elf_header_x64_T;
|
||||
|
||||
extern string_t g_elf_target_architecture_strings [3];
|
||||
extern string_t g_elf_endianness_strings [3];
|
||||
extern string_t g_elf_sysabi_strings [18];
|
||||
extern string_t g_elf_object_type_strings [5];
|
||||
extern string_t g_elf_instruction_set_strings [79];
|
||||
|
||||
|
||||
|
||||
#endif //NOX_ELF_HEADER_H
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the “Software”), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef NOX_SECTION_H
|
||||
#define NOX_SECTION_H
|
||||
|
||||
#include "utils/stdtypes.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
typedef enum {
|
||||
ELF_SECTION_NULL,
|
||||
ELF_SECTION_PROGRAM_DATA,
|
||||
ELF_SECTION_SYMBOL_TABLE,
|
||||
ELF_SECTION_STRING_TABLE,
|
||||
ELF_SECTION_RELOCATION_A,
|
||||
ELF_SECTION_HASH,
|
||||
ELF_SECTION_DYNAMIC_LINK,
|
||||
ELF_SECTION_NOTE,
|
||||
ELF_SECTION_NOBITS
|
||||
} elf_section_type_E;
|
||||
|
||||
typedef struct {
|
||||
uint32_t name_offset;
|
||||
uint32_t type;
|
||||
uint64_t flags;
|
||||
uint64_t virtual_address;
|
||||
uint64_t offset;
|
||||
uint64_t length;
|
||||
uint32_t link;
|
||||
uint32_t info;
|
||||
uint64_t alignment;
|
||||
uint64_t entry_size;
|
||||
} elf_section_T;
|
||||
|
||||
extern string_t g_elf_section_type_strings[9];
|
||||
|
||||
#endif //NOX_SECTION_H
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the “Software”), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef NOX_ELF_SYMBOL_H
|
||||
#define NOX_ELF_SYMBOL_H
|
||||
|
||||
#include "utils/stdtypes.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
#define ELF_SYMBOL_TYPE(info) ((info) & 0xF)
|
||||
|
||||
extern string_t g_elf_symbol_type_strings[7];
|
||||
|
||||
typedef enum {
|
||||
ELF_SYMBOL_NONE,
|
||||
ELF_SYMBOL_OBJECT,
|
||||
ELF_SYMBOL_FUNC,
|
||||
ELF_SYMBOL_SECTION,
|
||||
ELF_SYMBOL_FILE,
|
||||
ELF_SYMBOL_COMMON,
|
||||
ELF_SYMBOL_TLS
|
||||
} elf_symbol_type_E;
|
||||
|
||||
typedef struct {
|
||||
uint32_t name_offset;
|
||||
uint8_t info;
|
||||
uint8_t other;
|
||||
uint16_t related_section_index;
|
||||
uint64_t value;
|
||||
uint64_t length;
|
||||
} elf_symbol_T;
|
||||
|
||||
#endif //NOX_ELF_SYMBOL_H
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the “Software”), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef NOX_SYMBOLS_H
|
||||
#define NOX_SYMBOLS_H
|
||||
|
||||
#include "utils/string.h"
|
||||
|
||||
typedef enum {
|
||||
SYMBOL_FUNCTION,
|
||||
SYMBOL_VARIABLE,
|
||||
SYMBOL_UNKNOWN
|
||||
} symbol_type_E;
|
||||
|
||||
typedef struct {
|
||||
string_t name;
|
||||
symbol_type_E type;
|
||||
uint64_t address;
|
||||
} symbol_T;
|
||||
|
||||
#endif //NOX_SYMBOLS_H
|
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the “Software”), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "drivers/elf/elf.h"
|
||||
#include "drivers/elf/symbol.h"
|
||||
#include "utils/memory.h"
|
||||
#include "utils/logger.h"
|
||||
|
||||
bool elf_executable_validate(elf_executable_T* executable) {
|
||||
if (executable->header.identity[0] != 0x7F ||
|
||||
executable->header.identity[1] != 'E' ||
|
||||
executable->header.identity[2] != 'L' ||
|
||||
executable->header.identity[3] != 'F')
|
||||
{
|
||||
log(LOG_ERROR, "ELF <0x%x> is invalid (magic number mismatch)", executable);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (executable->header.identity[5] != ELF_ENDIAN_LITTLE) {
|
||||
log(LOG_ERROR, "ELF <0x%x> is invalid (little endian required)", executable);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (executable->header.identity[7] != ELF_SYSABI_SYSTEM_V) {
|
||||
log(LOG_ERROR, "ELF <0x%x> is invalid (SystemV SysABI required)", executable);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (executable->header.isa != ELF_ISA_X86_64) {
|
||||
log(LOG_ERROR, "ELF <0x%x> is invalid (x86_64 ISA required)", executable);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void elf_executable_extract_symbols(elf_executable_temp_T* executable_temp) {
|
||||
executable_temp->executable->num_symbols = executable_temp->symbol_table->length / executable_temp->symbol_table->entry_size;
|
||||
executable_temp->executable->symbols = memory_allocate(executable_temp->executable->num_symbols * sizeof(symbol_T));
|
||||
|
||||
for (int i = 0; i < executable_temp->executable->num_symbols; i++) {
|
||||
symbol_T* symbol = &executable_temp->executable->symbols[i];
|
||||
elf_symbol_T* symbol_elf = &((elf_symbol_T*)&executable_temp->buffer[executable_temp->symbol_table->offset])[i];
|
||||
elf_symbol_type_E type = ELF_SYMBOL_TYPE(symbol_elf->info);
|
||||
|
||||
symbol->name = &executable_temp->executable->string_table[symbol_elf->name_offset];
|
||||
symbol->address = symbol_elf->value;
|
||||
|
||||
switch (type) {
|
||||
case ELF_SYMBOL_FUNC: {
|
||||
symbol->type = SYMBOL_FUNCTION;
|
||||
break;
|
||||
}
|
||||
case ELF_SYMBOL_OBJECT: {
|
||||
symbol->type = SYMBOL_VARIABLE;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
symbol->type = SYMBOL_UNKNOWN;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
elf_executable_T* elf_executable_create(uint8_t* buffer) {
|
||||
elf_executable_temp_T executable_temp;
|
||||
executable_temp.executable = memory_allocate(sizeof(elf_executable_T));
|
||||
memory_copy(buffer, &executable_temp.executable->header, sizeof(elf_header_x64_T));
|
||||
|
||||
if (!elf_executable_validate(executable_temp.executable)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
log(LOG_INFO, "ELF header:");
|
||||
log(LOG_INFO, " -> Architecture: %s", g_elf_target_architecture_strings[executable_temp.executable->header.identity[4]]);
|
||||
log(LOG_INFO, " -> Endian: %s", g_elf_endianness_strings[executable_temp.executable->header.identity[5]]);
|
||||
log(LOG_INFO, " -> SysABI: %s", g_elf_sysabi_strings[executable_temp.executable->header.identity[7]]);
|
||||
log(LOG_INFO, " -> Type: %s", g_elf_object_type_strings[executable_temp.executable->header.type]);
|
||||
log(LOG_INFO, " -> ISA: %s", g_elf_instruction_set_strings[executable_temp.executable->header.isa]);
|
||||
log(LOG_INFO, " -> Entry: 0x%x", executable_temp.executable->header.address_entry_point);
|
||||
log(LOG_INFO, " -> PHDR offset: 0x%x", executable_temp.executable->header.offset_program_header);
|
||||
log(LOG_INFO, " -> PHDR num: %d", executable_temp.executable->header.num_program_header_entries);
|
||||
log(LOG_INFO, " -> SHDR offset: 0x%x", executable_temp.executable->header.offset_section_header);
|
||||
log(LOG_INFO, " -> SHDR num: %d\n", executable_temp.executable->header.num_section_header_entries);
|
||||
|
||||
executable_temp.buffer = buffer;
|
||||
|
||||
log(LOG_INFO, "Section Headers:");
|
||||
elf_section_T* sections = (elf_section_T*)&buffer[executable_temp.executable->header.offset_section_header];
|
||||
|
||||
executable_temp.section_header_string_table = §ions[executable_temp.executable->header.string_section_index];
|
||||
|
||||
for (int i = 0; i < executable_temp.executable->header.num_section_header_entries; i++) {
|
||||
string_t name = (string_t)&(&buffer[executable_temp.section_header_string_table->offset])[sections[i].name_offset];
|
||||
log(LOG_INFO, " [%s] '%s'", g_elf_section_type_strings[sections[i].type], name);
|
||||
|
||||
switch (sections[i].type) {
|
||||
case ELF_SECTION_SYMBOL_TABLE: {
|
||||
executable_temp.symbol_table = §ions[i];
|
||||
break;
|
||||
}
|
||||
case ELF_SECTION_STRING_TABLE: {
|
||||
if (string_compare(name, ".strtab")) {
|
||||
// copy the string table, so it stays accessible as long as the elf_executable_T exists
|
||||
executable_temp.executable->string_table = memory_allocate(sections[i].length);
|
||||
memory_copy(&buffer[sections[i].offset], executable_temp.executable->string_table, sections[i].length);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
elf_executable_extract_symbols(&executable_temp);
|
||||
|
||||
return executable_temp.executable;
|
||||
}
|
||||
|
||||
void elf_executable_destruct(elf_executable_T* executable) {
|
||||
memory_free(executable->string_table);
|
||||
memory_free(executable->symbols);
|
||||
memory_free(executable);
|
||||
}
|
|
@ -0,0 +1,155 @@
|
|||
/*
|
||||
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the “Software”), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "drivers/elf/header.h"
|
||||
|
||||
string_t g_elf_target_architecture_strings[3] = {
|
||||
"Invalid",
|
||||
"32-bit",
|
||||
"64-bit"
|
||||
};
|
||||
|
||||
string_t g_elf_endianness_strings[3] = {
|
||||
"Invalid",
|
||||
"Little endian",
|
||||
"Big Endian"
|
||||
};
|
||||
|
||||
string_t g_elf_sysabi_strings[18] = {
|
||||
"System V",
|
||||
"HP-UX",
|
||||
"NetBSD",
|
||||
"Linux",
|
||||
"GNU Hurd",
|
||||
"Solaris",
|
||||
"AIX",
|
||||
"IRIX",
|
||||
"FreeBSD",
|
||||
"Tru64",
|
||||
"Novell Modesto",
|
||||
"OpenBSD",
|
||||
"OpenVMS",
|
||||
"NonStop Kernel",
|
||||
"AROS",
|
||||
"FenixOS",
|
||||
"Nuxi CloudABI",
|
||||
"Stratus Technologies OpenVOS"
|
||||
};
|
||||
|
||||
string_t g_elf_object_type_strings[5] = {
|
||||
"None",
|
||||
"Relocatable Object",
|
||||
"Executable",
|
||||
"Shared Library",
|
||||
"Core Object"
|
||||
};
|
||||
|
||||
string_t g_elf_instruction_set_strings[79] = {
|
||||
"Undefined",
|
||||
"AT&T WE 32100",
|
||||
"SPARC",
|
||||
"x86_32",
|
||||
"Motorola 68000",
|
||||
"Motorola 88000",
|
||||
"Intel MCU",
|
||||
"Intel 80860",
|
||||
"MIPS",
|
||||
"IBM System/370",
|
||||
"MIPS RS3000 Little-Endian",
|
||||
"Reserved 0x0B",
|
||||
"Reserved 0x0C",
|
||||
"Reserved 0x0D",
|
||||
"HP PA-RISC",
|
||||
"Reserved 0x0F",
|
||||
"Unknown 0x10",
|
||||
"Unknown 0x11",
|
||||
"Unknown 0x12",
|
||||
"Intel 80960",
|
||||
"PowerPC",
|
||||
"PowerPC 64",
|
||||
"S390(x)",
|
||||
"IBM SPU/SPC",
|
||||
"Reserved 0x18",
|
||||
"Reserved 0x19",
|
||||
"Reserved 0x1A",
|
||||
"Reserved 0x1B",
|
||||
"Reserved 0x1C",
|
||||
"Reserved 0x1D",
|
||||
"Reserved 0x1E",
|
||||
"Reserved 0x1F",
|
||||
"Reserved 0x20",
|
||||
"Reserved 0x21",
|
||||
"Reserved 0x22",
|
||||
"Reserved 0x23",
|
||||
"NEC V800",
|
||||
"Fujitsu FR20",
|
||||
"TRW RH-32",
|
||||
"Motorola RCE",
|
||||
"ARM-32",
|
||||
"Digital Alpha",
|
||||
"SuperH",
|
||||
"SPARC v9",
|
||||
"Siemens TriCore",
|
||||
"Argonaut RISC",
|
||||
"Hitachi H8/300",
|
||||
"Hitachi H8/300H",
|
||||
"Hitachi H8S",
|
||||
"Hitachi H8/500",
|
||||
"IA-64",
|
||||
"Stanford MIPS-X",
|
||||
"Motorola ColdFire",
|
||||
"Motorola M68HC12",
|
||||
"Fujitsu MultiMedia Accelerator",
|
||||
"Siemens PCP",
|
||||
"Sony nCPU",
|
||||
"Denso NDR1",
|
||||
"Motorola Star*Core",
|
||||
"Toyota ME16",
|
||||
"STMicroelectronis ST100",
|
||||
"Advanced Logic TinyJ",
|
||||
"x86_64",
|
||||
"Sony DSP",
|
||||
"Digital Equipment PDP-10",
|
||||
"Digital Equipment PDP-11",
|
||||
"Siemens FX66",
|
||||
"STMicroelectronis ST9+",
|
||||
"STMicroelectronis ST7",
|
||||
"Motorola MC68HC16",
|
||||
"Motorola MC68HC11",
|
||||
"Motorola MC68HC08",
|
||||
"Motorola MC68HC05",
|
||||
"Silicon Graphics SVx",
|
||||
"STMicroelectronics ST19",
|
||||
"Digital VAX",
|
||||
"Axis Communications 32-bit",
|
||||
"Infineon Communications 32-bit",
|
||||
"Element 14 64-bit DSP",
|
||||
// "LSI Logic 16-bit DSP",
|
||||
// "TMS320C6000 Family",
|
||||
// "MCST Elbrus e2k",
|
||||
// "ARM-64",
|
||||
// "Zilog Z80",
|
||||
// "RISC-V",
|
||||
// "Berkeley Packet Filter",
|
||||
// "WDC 64C816"
|
||||
};
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the “Software”), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "drivers/elf/section.h"
|
||||
|
||||
string_t g_elf_section_type_strings[9] = {
|
||||
"Null",
|
||||
"Program Data",
|
||||
"Symbol Table",
|
||||
"String Table",
|
||||
"Relocation (Addends)",
|
||||
"Hash",
|
||||
"Dynamic Linker Info",
|
||||
"Note",
|
||||
"No Bits"
|
||||
};
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright 2023 Antifallobst <antifallobst@systemausfall.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the “Software”), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "drivers/elf/symbol.h"
|
||||
|
||||
string_t g_elf_symbol_type_strings[7] = {
|
||||
"None",
|
||||
"Object",
|
||||
"Function",
|
||||
"Section",
|
||||
"File",
|
||||
"Common",
|
||||
"TLS"
|
||||
};
|
|
@ -33,8 +33,7 @@
|
|||
#include "drivers/fs/vfs.h"
|
||||
#include "proc/scheduler.h"
|
||||
|
||||
#include "utils/io.h"
|
||||
#include "proc/thread.h"
|
||||
#include "drivers/elf/elf.h"
|
||||
|
||||
void limine_terminal_print(boot_info_T* boot_info, string_t string) {
|
||||
boot_info->terminal->write(boot_info->terminal->terminals[0], string, string_length(string));
|
||||
|
@ -89,13 +88,7 @@ void kmain(boot_info_T boot_info) {
|
|||
limine_terminal_print(&boot_info, "Kernel initialized\n");
|
||||
log(LOG_INFO, "!=====[ Kernel Initialized ]=====!\n");
|
||||
|
||||
vfs_node_dump_info(g_root_fs.root_node, 0);
|
||||
|
||||
char buffer [129]; buffer[128] = 0;
|
||||
|
||||
vfs_file_read(vfs_resolve_path(&g_root_fs, "/initrd/test.txt"), 0, 128, (uint8_t*)buffer);
|
||||
|
||||
log(LOG_DEBUG, "Reading 128 bytes from '/initrd/test.txt':\n%s", buffer);
|
||||
elf_executable_T* executable = elf_executable_create(boot_info.kernel_file->address);
|
||||
|
||||
CORE_HALT_FOREVER
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue