-
Notifications
You must be signed in to change notification settings - Fork 1
/
elf.c
68 lines (57 loc) · 1.7 KB
/
elf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <elf.h>
#include <stdbool.h>
#include <stdio.h>
#include "nooc.h"
#include "elf.h"
void
elf(const size_t entry, const struct data *const text, const struct data *const data, FILE *const 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 = entry;
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 = TEXT_OFFSET;
phdr_text.p_paddr = TEXT_OFFSET;
phdr_text.p_filesz = text->len;
phdr_text.p_memsz = text->len;
phdr_text.p_flags = PF_R | PF_X;
phdr_text.p_align = 0x1000;
phdr_data.p_type = PT_LOAD;
phdr_data.p_offset = 0x2000;
phdr_data.p_vaddr = DATA_OFFSET;
phdr_data.p_paddr = DATA_OFFSET;
phdr_data.p_filesz = data->len;
phdr_data.p_memsz = data->len;
phdr_data.p_flags = PF_R | PF_W;
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->data, 1, text->len, f);
for (int i = 0; i < 0x1000 - text->len; i++) {
fwrite(&empty, 1, 1, f);
}
fwrite(data->data, 1, data->len, f);
}