1
2
3
4
5 :(before "End Main")
6 assert(argc > 1);
7 if (is_equal(argv[1], "run")) {
8 START_TRACING_UNTIL_END_OF_SCOPE;
9 assert(argc > 2);
10 reset();
11 cerr << std::hex;
12 initialize_mem();
13 Mem_offset = CODE_START;
14 load_elf(argv[2], argc, argv);
15 while (EIP < End_of_program)
16 run_one_instruction();
17 trace(90, "load") << "executed past end of the world: " << EIP << " vs " << End_of_program << end();
18 return 0;
19 }
20
21 :(code)
22 void load_elf(const string& filename, int argc, char* argv[]) {
23 int fd = open(filename.c_str(), O_RDONLY);
24 if (fd < 0) raise << filename.c_str() << ": open" << perr() << '\n' << die();
25 off_t size = lseek(fd, 0, SEEK_END);
26 lseek(fd, 0, SEEK_SET);
27 uint8_t* elf_contents = static_cast<uint8_t*>(malloc(size));
28 if (elf_contents == NULL) raise << "malloc(" << size << ')' << perr() << '\n' << die();
29 ssize_t read_size = read(fd, elf_contents, size);
30 if (size != read_size) raise << "read → " << size << " (!= " << read_size << ')' << perr() << '\n' << die();
31 load_elf_contents(elf_contents, size, argc, argv);
32 free(elf_contents);
33 }
34
35 void load_elf_contents(uint8_t* elf_contents, size_t size, int argc, char* argv[]) {
36 uint8_t magic[5] = {0};
37 memcpy(magic, elf_contents, 4);
38 if (memcmp(magic, "\177ELF", 4) != 0)
39 raise << "Invalid ELF file; starts with \"" << magic << '"' << die();
40 if (elf_contents[4] != 1)
41 raise << "Only 32-bit ELF files (4-byte words; virtual addresses up to 4GB) supported.\n" << die();
42 if (elf_contents[5] != 1)
43 raise << "Only little-endian ELF files supported.\n" << die();
44
45 uint32_t e_machine_type = u32_in(&elf_contents[16]);
46 if (e_machine_type != 0x00030002)
47 raise << "ELF type/machine 0x" << HEXWORD << e_machine_type << " isn't i386 executable\n" << die();
48
49 uint32_t e_entry = u32_in(&elf_contents[24]);
50 uint32_t e_phoff = u32_in(&elf_contents[28]);
51
52
53 uint32_t e_ehsize = u16_in(&elf_contents[40]);
54 if (e_ehsize < 52) raise << "Invalid binary; ELF header too small\n" << die();
55 uint32_t e_phentsize = u16_in(&elf_contents[42]);
56 uint32_t e_phnum = u16_in(&elf_contents[44]);
57 trace(90, "load") << e_phnum << " entries in the program header, each " << e_phentsize << " bytes long" << end();
58
59
60
61
62 for (size_t i = 0; i < e_phnum; ++i)
63 load_segment_from_program_header(elf_contents, size, e_phoff + i*e_phentsize, e_ehsize);
64
65
66 Reg[ESP].u = AFTER_STACK;
67 Reg[EBP].u = 0;
68 EIP = e_entry;
69
70
71
72
73 uint32_t argv_data = ARGV_DATA_SEGMENT;
74 for (int i = argc-1; i >= 2; --i) {
75 push(argv_data);
76 for (size_t j = 0; j <= strlen(argv[i]); ++j) {
77 write_mem_u8(argv_data, argv[i][j]);
78 argv_data += sizeof(char);
79 assert(argv_data < ARGV_DATA_SEGMENT + SEGMENT_SIZE);
80 }
81 }
82 push(argc-2);
83 }
84
85 void push(uint32_t val) {
86 Reg[ESP].u -= 4;
87 trace(90, "run") << "decrementing ESP to 0x" << HEXWORD << Reg[ESP].u << end();
88 trace(90, "run") << "pushing value 0x" << HEXWORD << val << end();
89 write_mem_u32(Reg[ESP].u, val);
90 }
91
92 void load_segment_from_program_header(uint8_t* elf_contents, size_t size, uint32_t offset, uint32_t e_ehsize) {
93 uint32_t p_type = u32_in(&elf_contents[offset]);
94 trace(90, "load") << "program header at offset " << offset << ": type " << p_type << end();
95 if (p_type != 1) {
96 trace(90, "load") << "ignoring segment at offset " << offset << " of non PT_LOAD type " << p_type << " (see http://refspecs.linuxbase.org/elf/elf.pdf)" << end();
97 return;
98 }
99 uint32_t p_offset = u32_in(&elf_contents[offset + 4]);
100 uint32_t p_vaddr = u32_in(&elf_contents[offset + 8]);
101 if (e_ehsize > p_vaddr) raise << "Invalid binary; program header overlaps ELF header\n" << die();
102
103 uint32_t p_filesz = u32_in(&elf_contents[offset + 16]);
104 uint32_t p_memsz = u32_in(&elf_contents[offset + 20]);
105 if (p_filesz != p_memsz)
106 raise << "Can't handle segments where p_filesz != p_memsz (see http://refspecs.linuxbase.org/elf/elf.pdf)\n" << die();
107
108 if (p_offset + p_filesz > size)
109 raise << "Invalid binary; segment at offset " << offset << " is too large: wants to end at " << p_offset+p_filesz << " but the file ends at " << size << '\n' << die();
110 if (Mem.size() < p_vaddr + p_memsz)
111 Mem.resize(p_vaddr + p_memsz);
112 if (size > p_memsz) size = p_memsz;
113 trace(90, "load") << "blitting file offsets (" << p_offset << ", " << (p_offset+p_filesz) << ") to addresses (" << p_vaddr << ", " << (p_vaddr+p_memsz) << ')' << end();
114 for (size_t i = 0; i < p_filesz; ++i)
115 write_mem_u8(p_vaddr+i, elf_contents[p_offset+i]);
116 if (End_of_program < p_vaddr+p_memsz)
117 End_of_program = p_vaddr+p_memsz;
118 }
119
120 :(before "End Includes")
121
122
123
124
125
126 const int CODE_START = 0x08048000;
127 const int SEGMENT_SIZE = 0x1000;
128 const int AFTER_STACK = 0x0804c000;
129 const int ARGV_DATA_SEGMENT = 0x0804e000;
130 :(code)
131 void initialize_mem() {
132 Mem.resize(AFTER_STACK - CODE_START);
133 }
134
135 inline uint32_t u32_in(uint8_t* p) {
136 return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
137 }
138
139 inline uint16_t u16_in(uint8_t* p) {
140 return p[0] | p[1] << 8;
141 }
142
143 :(before "End Types")
144 struct perr {};
145 :(code)
146 ostream& operator<<(ostream& os, perr ) {
147 if (errno)
148 os << ": " << strerror(errno);
149 return os;
150 }
151
152 :(before "End Types")
153 struct die {};
154 :(code)
155 ostream& operator<<(ostream& , die ) {
156 if (Trace_stream) Trace_stream->newline();
157 exit(1);
158 }
159
160 :(before "End Includes")
161
162
163
164
165
166