From bdab60d74d14eeea7a5e35a983e5e319686ae192 Mon Sep 17 00:00:00 2001 From: onelin Date: Wed, 10 Jun 2026 18:31:09 +0200 Subject: Add shstrtab --- .clang-format | 4 +- Makefile | 5 ++ main.c | 10 +++- write_elf.c | 145 ++++++++++++++++++++++++++-------------------------------- 4 files changed, 82 insertions(+), 82 deletions(-) diff --git a/.clang-format b/.clang-format index 451482f..04e3364 100644 --- a/.clang-format +++ b/.clang-format @@ -1,9 +1,9 @@ --- -Language: Cpp +Language: C # BasedOnStyle: LLVM AccessModifierOffset: -2 AlignAfterOpenBracket: Align -AlignArrayOfStructures: Right +AlignArrayOfStructures: None AlignConsecutiveAssignments: Enabled: true AcrossEmptyLines: true diff --git a/Makefile b/Makefile index 9f3c9a5..35738ae 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,13 @@ .PHONY: fmt +all: fmt test.out + write-elf: main.c write_elf.c registers.h instructions.h gcc -static-pie main.c -o write-elf +test.out: write-elf + ./write-elf + tiny: tiny.s nasm -f elf64 tiny.s diff --git a/main.c b/main.c index da8eb0f..d08d0e3 100644 --- a/main.c +++ b/main.c @@ -1,8 +1,16 @@ +#include "instructions.h" +#include "registers.h" #include "write_elf.c" +static const unsigned char some_opcodes[] = {MOVDI(REG_EAX, 0x3c), + MOVDI(REG_EDI, 0x41), SYSCALL}; + +static const unsigned int some_opcodes_len = + sizeof(some_opcodes) / sizeof(some_opcodes[0]); + int main(int argc, char* argv[]) { char opcodes[] = {}; - write_elf("test.out", some_opcodes_len, some_opcodes); + write_elf("test.out", some_opcodes_len, (char*)some_opcodes); return 0; } diff --git a/write_elf.c b/write_elf.c index 62b6cd3..adb11a8 100644 --- a/write_elf.c +++ b/write_elf.c @@ -1,18 +1,9 @@ -#include "instructions.h" -#include "registers.h" #include #include #include #include #include -unsigned char some_opcodes[] = {MOVDI(REG_EAX, 0x3c), - // 0xb8, 0x3c, 0x00, 0x00, 0x00, - MOVDI(REG_EDI, 0x41), - // 0xbf, 0x41, 0x00, 0x00, 0x00, - SYSCALL}; -unsigned int some_opcodes_len = sizeof(some_opcodes) / sizeof(some_opcodes[0]); - int write_elf(char* restrict file, size_t opcodes_len, char* restrict opcodes) { int error = 0; @@ -29,83 +20,70 @@ write_elf(char* restrict file, size_t opcodes_len, char* restrict opcodes) { // .p_align = 0x1000, /* Segment alignment */ //}, { - .p_type = PT_LOAD, - .p_flags = PF_X | PF_R, - .p_offset = - sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr), /* Segment file offset */ - .p_vaddr = 0x400000 + sizeof(Elf64_Ehdr) + - sizeof(Elf64_Phdr), /* Segment virtual address */ - .p_paddr = 0x400000 + sizeof(Elf64_Ehdr) + - sizeof(Elf64_Phdr), /* Segment physical address */ - .p_filesz = opcodes_len, /* Segment size in file */ - .p_memsz = opcodes_len, /* Segment size in memory */ - .p_align = 0x1, /* Segment alignment */ + .p_type = PT_LOAD, + .p_flags = PF_X | PF_R, + .p_offset = 0x1000, /* Segment file offset */ + .p_vaddr = 0x400000 + 0x1000, /* Segment virtual address */ + .p_paddr = 0x400000 + 0x1000, /* Segment physical address */ + .p_filesz = opcodes_len, /* Segment size in file */ + .p_memsz = opcodes_len, /* Segment size in memory */ + .p_align = 0x1, /* Segment alignment */ }, }; - Elf64_Shdr header_section[] = { - { - // .init - // Index in section header string table - .sh_name = 0, - .sh_type = 0, - .sh_flags = 0, - .sh_addr = 0, - .sh_offset = 0, - .sh_size = 0, - .sh_link = 0, - .sh_info = 0, - .sh_addralign = 0, - .sh_entsize = 0, - }, + unsigned char string_header_table[] = {0, '.', 'i', 'n', 'i', 't', + 0, '.', 's', 'h', 's', 't', + 'r', 't', 'a', 'b', 0}; + + Elf64_Shdr header_section[] = { + {0}, // NULL { - // .init - // Index in section header string table - .sh_name = 0, - .sh_type = SHT_PROGBITS, - .sh_flags = SHF_ALLOC | SHF_EXECINSTR, - .sh_addr = 0x401000, - .sh_offset = 0x1000, - .sh_size = opcodes_len, - /* todo: deps on type */ - .sh_link = 0, - /* todo: deps on type */ - .sh_info = 0, - .sh_addralign = 4, - .sh_entsize = 0, - }, + // .init + // Index in section header string table + .sh_name = 1, + .sh_type = SHT_PROGBITS, + .sh_flags = SHF_ALLOC | SHF_EXECINSTR, + .sh_addr = 0x401000, + .sh_offset = 0x1000, + .sh_size = opcodes_len, + /* todo: deps on type */ + .sh_link = 0, + /* todo: deps on type */ + .sh_info = 0, + .sh_addralign = 4, + .sh_entsize = 0, + }, { - // .shstrtab Section Header String Table - // Index in section header string table - .sh_name = 1, - .sh_type = SHT_STRTAB, - .sh_flags = 0, - .sh_addr = 0x100, - .sh_offset = 0x2000, - .sh_size = opcodes_len, - /* todo: deps on type */ - .sh_link = 0, - /* todo: deps on type */ - .sh_info = 0, - .sh_addralign = 1, - .sh_entsize = 0, - }, + // .shstrtab Section Header String Table + // Index in section header string table + .sh_name = 7, + .sh_type = SHT_STRTAB, + .sh_flags = 0, + .sh_addr = 0x0, + .sh_offset = 0x2000, + .sh_size = sizeof(string_header_table) / sizeof(string_header_table[0]), + /* todo: deps on type */ + .sh_link = 0, + /* todo: deps on type */ + .sh_info = 0, + .sh_addralign = 1, + .sh_entsize = 0, + }, }; - unsigned char string_header_table[] = {0, '.', 'i', 'n', 'i', 't', 0}; - - Elf64_Ehdr header = { + Elf64_Ehdr header = { // memset(&header, 0, sizeof(Elf64_Ehdr)); .e_ident = {ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ELFOSABI_SYSV}, .e_type = ET_EXEC, .e_machine = EM_X86_64, .e_version = EV_CURRENT, - .e_entry = 0x400000 + sizeof(Elf64_Ehdr) + - sizeof(Elf64_Phdr), // point to VIRTUAL address of _start - .e_phoff = sizeof(Elf64_Ehdr) /* Program header table offset */, - .e_shoff = 0, // sizeof(Elf64_Ehdr) + sizeof(header_program) /* Section - // header table offset */, + // point to VIRTUAL address of _start + .e_entry = 0x400000 + 0x1000, + // Program header table offset + .e_phoff = sizeof(Elf64_Ehdr), + // Section header table offset + .e_shoff = sizeof(Elf64_Ehdr) + sizeof(header_program), .e_flags = 0, .e_ehsize = sizeof(Elf64_Ehdr), .e_phentsize = sizeof(Elf64_Phdr), @@ -115,11 +93,12 @@ write_elf(char* restrict file, size_t opcodes_len, char* restrict opcodes) { .e_shentsize = sizeof(header_section[0]), // If larger than SHN_LORESERVE, then set to 0 and store real number in // sh_size of first section header entry - .e_shnum = 0, // sizeof(header_section) / sizeof(header_section[0]), - // If larger than SHN_LORESERVE, then set to SHN_XINDEX and store real index - // in - // sh_link of first section header entry - .e_shstrndx = 0, + .e_shnum = sizeof(header_section) / sizeof(header_section[0]), + // If larger than SHN_LORESERVE, then set to SHN_XINDEX and store real + // index + // in sh_link of first section header entry + // "Section Header string-table-index + .e_shstrndx = 2, }; size_t bytes_written = 0; @@ -128,11 +107,19 @@ write_elf(char* restrict file, size_t opcodes_len, char* restrict opcodes) { fwrite(&header, sizeof(Elf64_Ehdr), 1, fd); fwrite(&header_program, sizeof(Elf64_Phdr), sizeof(header_program) / sizeof(header_program[0]), fd); - // fseek(fd, 0x1000, SEEK_SET); + fwrite(&header_section, sizeof(Elf64_Shdr), + sizeof(header_section) / sizeof(header_section[0]), fd); + + fseek(fd, 0x1000, SEEK_SET); do { bytes_written += fwrite(opcodes, sizeof(char), opcodes_len, fd); } while (bytes_written != opcodes_len); + + fseek(fd, 0x2000, SEEK_SET); + fwrite(string_header_table, sizeof(char), + sizeof(string_header_table) / sizeof(string_header_table[0]), fd); + error = fclose(fd); if (error) { err(EXIT_FAILURE, "no way"); -- cgit v1.3