73 lines
2.6 KiB
C
73 lines
2.6 KiB
C
#include "unix.h"
|
|
#include "debug.h"
|
|
|
|
#include <string.h>
|
|
|
|
char elf_main_header_byte[ELF_MAIN_HEADER_SIZE] = {
|
|
0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x03,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
|
|
// Redo this part...
|
|
// These should not be hardcoded.
|
|
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
|
|
};
|
|
|
|
char 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
|
|
};
|
|
|
|
char 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(int entry_point,
|
|
int has_program,
|
|
int for_linux,
|
|
int for_x86_64) {
|
|
int 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(int text_size,
|
|
int data_size) {
|
|
int 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(int text_size,
|
|
int data_size) {
|
|
int data = data_size;
|
|
int core = ELF_HEADER_SIZE + text_size - data_size;
|
|
int 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));
|
|
}
|