Normal ELF64 headers without hacks...

This commit is contained in:
xolatile 2024-07-16 19:42:26 -04:00
parent 5ac9e9a3f9
commit 18a35c1c6d
7 changed files with 138 additions and 38 deletions

View File

@ -347,7 +347,8 @@ static void assemble_clean_up (void) {
next text_sector_size = 0; next text_sector_size = 0;
byte * text_sector_byte = NULL; byte * text_sector_byte = NULL;
int was_instruction_array_empty = 0; int was_instruction_array_empty = 0;
unsigned int text_entry_point = 0;
void assemble (next count, void assemble (next count,
next * array) { next * array) {
@ -433,6 +434,8 @@ void assemble (next count,
} }
} }
text_entry_point = empty_store [0];
for (index = 0; index < empty_holes; ++index) { for (index = 0; index < empty_holes; ++index) {
next set = 0, get = empty_array [index]; next set = 0, get = empty_array [index];
memcpy (& set, & text_sector_byte [get], sizeof (set)); memcpy (& set, & text_sector_byte [get], sizeof (set));

View File

@ -71,7 +71,8 @@ typedef enum {
extern next text_sector_size; extern next text_sector_size;
extern byte * text_sector_byte; extern byte * text_sector_byte;
extern int was_instruction_array_empty; extern int was_instruction_array_empty;
extern unsigned int text_entry_point;
extern void assemble (next count, next * array); extern void assemble (next count, next * array);

View File

@ -54,9 +54,9 @@ void dump_variables_to_assembler(void) {
static static
int write_output(FILE * file) { int write_output(FILE * file) {
// XXX Where can i move these? // XXX Where can i move these?
elf_main_header (1, 1, 1, 0); elf_main_header (1, 1, 1);
elf_text_sector (text_sector_size); elf_text_sector (text_sector_size, 0x27); // HACK
elf_data_sector (text_sector_size, 12); elf_data_sector (text_sector_size, 0x27); // HACK
checked_fwrite(elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file); checked_fwrite(elf_main_header_byte, 1UL, ELF_MAIN_HEADER_SIZE, file);
checked_fwrite(elf_text_sector_byte, 1UL, ELF_TEXT_SECTOR_SIZE, file); checked_fwrite(elf_text_sector_byte, 1UL, ELF_TEXT_SECTOR_SIZE, file);
@ -71,7 +71,7 @@ int write_output(FILE * file) {
static static
int make_executable(const char * const filename) { int make_executable(const char * const filename) {
int r = 0; int r = 0;
#if defined(__unix__) #if defined(__unix__)
r = chmod(filename, 0755); r = chmod(filename, 0755);
#endif #endif

View File

@ -1,3 +1,4 @@
#include "assembler.h"
#include "unix.h" #include "unix.h"
uint8_t elf_main_header_byte [ELF_MAIN_HEADER_SIZE] = { uint8_t elf_main_header_byte [ELF_MAIN_HEADER_SIZE] = {
@ -33,23 +34,21 @@ uint8_t elf_data_sector_byte [ELF_DATA_SECTOR_SIZE] = {
void elf_main_header (uint8_t has_program, void elf_main_header (uint8_t has_program,
uint8_t for_linux, uint8_t for_linux,
uint8_t for_x86_64, uint8_t for_x86_64) {
uint64_t entry_point) { //
/* */ uint32_t enter = text_entry_point + 0x4000b0u;
uint64_t enter = entry_point; // TEST
elf_main_header_byte [16] = (has_program) ? 0x02 : 0x03; // library elf_main_header_byte [16] = (has_program) ? 0x02 : 0x03; // library
elf_main_header_byte [ 7] = (for_linux) ? 0x03 : 0x00; // system v elf_main_header_byte [ 7] = (for_linux) ? 0x03 : 0x00; // system v
elf_main_header_byte [18] = (for_x86_64) ? 0x3e : 0x00; elf_main_header_byte [18] = (for_x86_64) ? 0x3e : 0x00;
if (entry_point != 0) { memcpy (& elf_main_header_byte [24], & enter, sizeof (enter));
memcpy (& elf_main_header_byte [24], & enter, sizeof (enter));
}
} }
void elf_text_sector (uint64_t text_size) { void elf_text_sector (uint64_t text_size,
/* */ uint64_t data_size) {
uint64_t text = ELF_HEADER_SIZE + text_size; // TEST //
uint64_t text = ELF_HEADER_SIZE + text_size - data_size;
memcpy (& elf_text_sector_byte [32], & text, sizeof (text)); memcpy (& elf_text_sector_byte [32], & text, sizeof (text));
memcpy (& elf_text_sector_byte [40], & text, sizeof (text)); memcpy (& elf_text_sector_byte [40], & text, sizeof (text));
@ -57,9 +56,9 @@ void elf_text_sector (uint64_t text_size) {
void elf_data_sector (uint64_t text_size, void elf_data_sector (uint64_t text_size,
uint64_t data_size) { uint64_t data_size) {
/* */ //
uint64_t data = data_size; // TEST uint64_t data = data_size;
uint64_t core = ELF_HEADER_SIZE + text_size; uint64_t core = ELF_HEADER_SIZE + text_size - data_size;
uint64_t move = 0x401000 + core; uint64_t move = 0x401000 + core;
memcpy (& elf_data_sector_byte [ 8], & core, sizeof (core)); memcpy (& elf_data_sector_byte [ 8], & core, sizeof (core));

View File

@ -17,10 +17,10 @@ extern uint8_t elf_data_sector_byte [ELF_DATA_SECTOR_SIZE];
extern void elf_main_header (uint8_t has_program, extern void elf_main_header (uint8_t has_program,
uint8_t for_linux, uint8_t for_linux,
uint8_t for_x86_64, uint8_t for_x86_64);
uint64_t entry_point);
extern void elf_text_sector (uint64_t text_size); extern void elf_text_sector (uint64_t text_size,
uint64_t data_size);
extern void elf_data_sector (uint64_t text_size, extern void elf_data_sector (uint64_t text_size,
uint64_t data_size); uint64_t data_size);

97
test/simple_procedure.asm Normal file
View File

@ -0,0 +1,97 @@
; fasm proc.asm proc && chmod +x proc && ./proc
format ELF64 executable 3
segment readable executable
entry main
heyo:
nop
mov eax, 1
nop
mov edi, 1
nop
mov esi, h
nop
mov edx, 12
nop
syscall
ret
cyaa:
nop
mov eax, 1
nop
mov edi, 1
nop
mov esi, c
nop
mov edx, 14
nop
syscall
ret
main:
nop
call heyo
nop
mov eax, 1
nop
mov edi, 1
nop
mov esi, m
nop
mov edx, 13
nop
syscall
nop
call cyaa
nop
mov eax, 60
nop
mov edi, 60
nop
syscall
segment readable writable
h: db "Heyo world!", 10
m: db "Meme world!!", 10
c: db "Cyaa world!!!", 10
;~FASM EAXHLA
;~7F 45 4C 46 02 01 01 03 00 00 00 00 00 00 00 00 7F 45 4C 46 02 01 01 03 00 00 00 00 00 00 00 00
;~02 00 3E 00 01 00 00 00 EA 00 40 00 00 00 00 00 02 00 3E 00 01 00 00 00 7C 00 00 00 00 00 00 00
;~40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
;~00 00 00 00 40 00 38 00 02 00 40 00 00 00 00 00 00 00 00 00 40 00 38 00 02 00 40 00 00 00 00 00
;~01 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00
;~00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00
;~20 01 00 00 00 00 00 00 20 01 00 00 00 00 00 00 53 01 00 00 00 00 00 00 53 01 00 00 00 00 00 00
;~00 10 00 00 00 00 00 00 01 00 00 00 06 00 00 00 00 10 00 00 00 00 00 00 01 00 00 00 06 00 00 00
;~20 01 00 00 00 00 00 00 20 11 40 00 00 00 00 00 53 01 00 00 00 00 00 00 53 11 40 00 00 00 00 00
;~20 11 40 00 00 00 00 00 27 00 00 00 00 00 00 00 53 11 40 00 00 00 00 00 0C 00 00 00 00 00 00 00
;~27 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 0C 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00
;~90 B8 01 00 00 00 90 48 B8 01 00 00 00
;~90 BF 01 00 00 00 90 48 BF 01 00 00 00
;~90 BE 20 11 40 00 90 48 BE 2C 11 40 00
;~90 BA 0C 00 00 00 90 48 BA 0C 00 00 00
;~90 0F 05 C3 90 0F 05 C3
;~90 B8 01 00 00 00 90 48 B8 01 00 00 00
;~90 BF 01 00 00 00 90 48 BF 01 00 00 00
;~90 BE 39 11 40 00 90 48 BE 45 11 40 00
;~90 BA 0E 00 00 00 90 48 BA 0E 00 00 00
;~90 0F 05 C3 90 0F 05 C3
;~90 E8 C0 FF FF FF 90 E8 BA FF FF FF
;~90 B8 01 00 00 00 90 48 B8 01 00 00 00
;~90 BF 01 00 00 00 90 48 BF 01 00 00 00
;~90 BE 2C 11 40 00 90 48 BE 38 11 40 00
;~90 BA 0D 00 00 00 90 48 BA 0D 00 00 00
;~90 0F 05 90 0F 05
;~90 E8 BC FF FF FF 90 E8 B5 FF FF FF
;~90 B8 3C 00 00 00 90 48 B8 3C 00 00 00
;~90 BF 3C 00 00 00 90 48 BF 3C 00 00 00
;~90 0F 05 90 0F 05
;~48 65 79 6F 20 77 6F 72 6C 64 21 0A 48 65 79 6F 20 77 6F 72 6C 64 21 0A
;~4D 65 6D 65 20 77 6F 72 6C 64 21 21 0A 4D 65 6D 65 20 77 6F 72 6C 64 21 21 0A
;~43 79 61 61 20 77 6F 72 6C 64 21 21 21 0A 43 79 61 61 20 77 6F 72 6C 64 21 21 21 0A

View File

@ -1,34 +1,34 @@
fast procedure heyo fast procedure heyo
s8 <> h = "Heyo world!\n" s8 <> h = "Heyo world!\n"
begin begin
nop mov rax 1 nop mov eax 1
nop mov rdi 1 nop mov edi 1
nop mov rsi h nop mov esi h
nop mov rdx 12 nop mov edx 12
nop syscall nop syscall
end procedure end procedure
fast procedure cyaa fast procedure cyaa
s8 <> c = "Cyaa world!\n" s8 <> c = "Cyaa world!!!\n"
begin begin
nop mov rax 1 nop mov eax 1
nop mov rdi 1 nop mov edi 1
nop mov rsi c nop mov esi c
nop mov rdx 12 nop mov edx 14
nop syscall nop syscall
end procedure end procedure
unix program main unix program main
s8 <> m = "Meme world!\n" s8 <> m = "Meme world!!\n"
begin begin
nop fastcall heyo nop fastcall heyo
nop mov rax 1 nop mov eax 1
nop mov rdi 1 nop mov edi 1
nop mov rsi m nop mov esi m
nop mov rdx 12 nop mov edx 13
nop syscall nop syscall
nop fastcall cyaa nop fastcall cyaa
nop mov rax 60 nop mov eax 60
nop mov rdi 60 nop mov edi 60
nop syscall nop syscall
end program end program