commit e5868e1b7a0412cc31a0bca2ab8801af3a6f494d
parent cbcd41da9f384500dc4eb6ad0b7e2a0dd41c3c8e
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Fri, 19 Nov 2021 10:14:15 -0600
extract elf
Diffstat:
M | Makefile | | | 4 | ++-- |
A | elf.c | | | 67 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | elf.h | | | 1 | + |
M | main.c | | | 62 | +------------------------------------------------------------- |
4 files changed, 71 insertions(+), 63 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,5 @@
.c.o:
$(CC) -c $< -o $@
-nooc: main.o array.o util.o x64.o
- $(CC) main.o array.o x64.o util.o -o nooc
+nooc: main.o array.o util.o x64.o elf.o
+ $(CC) main.o array.o x64.o util.o elf.o -o nooc
diff --git a/elf.c b/elf.c
@@ -0,0 +1,67 @@
+#include <elf.h>
+#include <stdio.h>
+
+#include "nooc.h"
+#include "elf.h"
+
+int
+elf(char *text, size_t len, char* data, size_t dlen, FILE *f)
+{
+ Elf64_Ehdr ehdr = { 0 };
+ Elf64_Phdr phdr_text = { 0 };
+ Elf64_Phdr phdr_data = { 0 };
+
+ ehdr.e_ident[0] = ELFMAG0;
+ ehdr.e_ident[1] = ELFMAG1;
+ ehdr.e_ident[2] = ELFMAG2;
+ ehdr.e_ident[3] = ELFMAG3;
+ ehdr.e_ident[4] = ELFCLASS64;
+ ehdr.e_ident[5] = ELFDATA2LSB;
+ ehdr.e_ident[6] = EV_CURRENT;
+ ehdr.e_ident[7] = ELFOSABI_LINUX;
+
+ ehdr.e_type = ET_EXEC;
+ ehdr.e_machine = EM_X86_64;
+ ehdr.e_version = EV_CURRENT;
+
+ ehdr.e_entry = 0x1000;
+ ehdr.e_phoff = sizeof(ehdr);
+ ehdr.e_phentsize = sizeof(phdr_text);
+ ehdr.e_phnum = 2;
+
+ ehdr.e_ehsize = sizeof(ehdr);
+
+ size_t pretextlen = sizeof(ehdr) + sizeof(phdr_text) + sizeof(phdr_data);
+
+ phdr_text.p_type = PT_LOAD;
+ phdr_text.p_offset = 0x1000;
+ phdr_text.p_vaddr = 0x1000;
+ phdr_text.p_paddr = 0x1000;
+ phdr_text.p_filesz = len;
+ phdr_text.p_memsz = len;
+ phdr_text.p_flags = PF_R | PF_X;
+ phdr_text.p_align = 0x1000;
+
+ phdr_data.p_type = PT_LOAD;
+ phdr_data.p_offset = DATA_OFFSET;
+ phdr_data.p_vaddr = DATA_OFFSET;
+ phdr_data.p_paddr = DATA_OFFSET;
+ phdr_data.p_filesz = dlen;
+ phdr_data.p_memsz = dlen;
+ phdr_data.p_flags = PF_R;
+ phdr_data.p_align = 0x1000;
+
+ fwrite(&ehdr, 1, sizeof(Elf64_Ehdr), f);
+ fwrite(&phdr_text, sizeof(phdr_text), 1, f);
+ fwrite(&phdr_data, sizeof(phdr_data), 1, f);
+ char empty = 0;
+
+ for (int i = 0; i < 0x1000 - pretextlen; i++) {
+ fwrite(&empty, 1, 1, f);
+ }
+ fwrite(text, 1, len, f);
+ for (int i = 0; i < 0x1000 - len; i++) {
+ fwrite(&empty, 1, 1, f);
+ }
+ fwrite(data, 1, dlen, f);
+}
diff --git a/elf.h b/elf.h
@@ -0,0 +1 @@
+int elf(char *text, size_t len, char* data, size_t dlen, FILE *f);
diff --git a/main.c b/main.c
@@ -13,68 +13,8 @@
#include "x64.h"
#include "util.h"
#include "nooc.h"
+#include "elf.h"
-int
-elf(char *text, size_t len, char* data, size_t dlen, FILE *f)
-{
- Elf64_Ehdr ehdr = { 0 };
- Elf64_Phdr phdr_text = { 0 };
- Elf64_Phdr phdr_data = { 0 };
-
- ehdr.e_ident[0] = ELFMAG0;
- ehdr.e_ident[1] = ELFMAG1;
- ehdr.e_ident[2] = ELFMAG2;
- ehdr.e_ident[3] = ELFMAG3;
- ehdr.e_ident[4] = ELFCLASS64;
- ehdr.e_ident[5] = ELFDATA2LSB;
- ehdr.e_ident[6] = EV_CURRENT;
- ehdr.e_ident[7] = ELFOSABI_LINUX;
-
- ehdr.e_type = ET_EXEC;
- ehdr.e_machine = EM_X86_64;
- ehdr.e_version = EV_CURRENT;
-
- ehdr.e_entry = 0x1000;
- ehdr.e_phoff = sizeof(ehdr);
- ehdr.e_phentsize = sizeof(phdr_text);
- ehdr.e_phnum = 2;
-
- ehdr.e_ehsize = sizeof(ehdr);
-
- size_t pretextlen = sizeof(ehdr) + sizeof(phdr_text) + sizeof(phdr_data);
-
- phdr_text.p_type = PT_LOAD;
- phdr_text.p_offset = 0x1000;
- phdr_text.p_vaddr = 0x1000;
- phdr_text.p_paddr = 0x1000;
- phdr_text.p_filesz = len;
- phdr_text.p_memsz = len;
- phdr_text.p_flags = PF_R | PF_X;
- phdr_text.p_align = 0x1000;
-
- phdr_data.p_type = PT_LOAD;
- phdr_data.p_offset = DATA_OFFSET;
- phdr_data.p_vaddr = DATA_OFFSET;
- phdr_data.p_paddr = DATA_OFFSET;
- phdr_data.p_filesz = dlen;
- phdr_data.p_memsz = dlen;
- phdr_data.p_flags = PF_R;
- phdr_data.p_align = 0x1000;
-
- fwrite(&ehdr, 1, sizeof(Elf64_Ehdr), f);
- fwrite(&phdr_text, sizeof(phdr_text), 1, f);
- fwrite(&phdr_data, sizeof(phdr_data), 1, f);
- char empty = 0;
-
- for (int i = 0; i < 0x1000 - pretextlen; i++) {
- fwrite(&empty, 1, 1, f);
- }
- fwrite(text, 1, len, f);
- for (int i = 0; i < 0x1000 - len; i++) {
- fwrite(&empty, 1, 1, f);
- }
- fwrite(data, 1, dlen, f);
-}
struct decls decls;
struct exprs exprs;