69 lines
2.7 KiB
C
69 lines
2.7 KiB
C
#include "unix.h"
|
|
#include "debug.h"
|
|
|
|
uint8_t elf_main_header_byte[ELF_MAIN_HEADER_SIZE] = { /// TOTALLY HARDCODED.
|
|
0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x03,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
|
|
0xb0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00,
|
|
0x02, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
};
|
|
|
|
uint8_t elf_text_sector_byte[ELF_TEXT_SECTOR_SIZE] = {
|
|
0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
};
|
|
|
|
uint8_t elf_data_sector_byte[ELF_DATA_SECTOR_SIZE] = {
|
|
0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x10, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x10, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
};
|
|
|
|
void elf_main_header(uint32_t entry_point,
|
|
bool has_program,
|
|
bool for_linux,
|
|
bool for_x86_64) { /// THIS IS A JOKE.
|
|
uint32_t enter = entry_point + 0x4000b0;
|
|
|
|
// Latter 3 variables are determined by system, architecture and '-c' flag.
|
|
elf_main_header_byte[16] = (has_program) ? 0x02 : 0x03;
|
|
elf_main_header_byte[ 7] = (for_linux) ? 0x03 : 0x00;
|
|
elf_main_header_byte[18] = (for_x86_64) ? 0x3e : 0x00;
|
|
|
|
memcpy(&elf_main_header_byte[24], &enter, sizeof(enter));
|
|
}
|
|
|
|
void elf_text_sector(uint32_t text_size,
|
|
uint32_t data_size) {
|
|
uint32_t text = ELF_HEADER_SIZE + text_size - data_size;
|
|
|
|
memcpy(&elf_text_sector_byte[32], &text, sizeof(text));
|
|
memcpy(&elf_text_sector_byte[40], &text, sizeof(text));
|
|
}
|
|
|
|
void elf_data_sector(uint32_t text_size,
|
|
uint32_t data_size) {
|
|
uint32_t data = data_size;
|
|
uint32_t core = ELF_HEADER_SIZE + text_size - data_size;
|
|
uint32_t move = 0x401000 + core;
|
|
|
|
memcpy(&elf_data_sector_byte[ 8], &core, sizeof(core));
|
|
memcpy(&elf_data_sector_byte[16], &move, sizeof(move));
|
|
memcpy(&elf_data_sector_byte[24], &move, sizeof(move));
|
|
memcpy(&elf_data_sector_byte[32], &data, sizeof(data));
|
|
memcpy(&elf_data_sector_byte[40], &data, sizeof(data));
|
|
}
|