https://github.com/akkartik/mu/blob/master/subx/028translate.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 :(before "End Main")
20 if (is_equal(argv[1], "translate")) {
21 START_TRACING_UNTIL_END_OF_SCOPE;
22 reset();
23
24 program p;
25 string output_filename;
26 for (int i = 2; i < argc; ++i) {
27 if (is_equal(argv[i], "-o")) {
28 ++i;
29 if (i >= argc) {
30 print_translate_usage();
31 cerr << "'-o' must be followed by a filename to write results to\n";
32 exit(1);
33 }
34 output_filename = argv[i];
35 }
36 else {
37 ifstream fin(argv[i]);
38 if (!fin) {
39 cerr << "could not open " << argv[i] << '\n';
40 return 1;
41 }
42 parse(fin, p);
43 if (trace_contains_errors()) return 1;
44 }
45 }
46 if (p.segments.empty()) {
47 print_translate_usage();
48 cerr << "nothing to do; must provide at least one file to read\n";
49 exit(1);
50 }
51 if (output_filename.empty()) {
52 print_translate_usage();
53 cerr << "must provide a filename to write to using '-o'\n";
54 exit(1);
55 }
56 transform(p);
57 if (trace_contains_errors()) return 1;
58 save_elf(p, output_filename);
59 if (trace_contains_errors()) {
60 unlink(output_filename.c_str());
61 return 1;
62 }
63
64 return 0;
65 }
66
67 :(code)
68 void print_translate_usage() {
69 cerr << "Usage: subx translate file1 file2 ... -o output\n";
70 }
71
72
73 void save_elf(const program& p, const string& filename) {
74 ofstream out(filename.c_str(), ios::binary);
75 write_elf_header(out, p);
76 for (size_t i = 0; i < p.segments.size(); ++i)
77 write_segment(p.segments.at(i), out);
78 out.close();
79 }
80
81 void write_elf_header(ostream& out, const program& p) {
82 char c = '\0';
83
84
85
86
87
88 O(0x7f); O(0x45); O(0x4c); O(0x46);
89 O(0x1);
90 O(0x1);
91 O(0x1); O(0x0);
92 for (size_t i = 0; i < 8; ++i) { O(0x0); }
93
94 O(0x02); O(0x00);
95
96 O(0x03); O(0x00);
97
98 O(0x01); O(0x00); O(0x00); O(0x00);
99
100 uint32_t e_entry = p.segments.at(0).start;
101
102 emit(e_entry);
103
104 uint32_t e_phoff = 0x34;
105 emit(e_phoff);
106
107 uint32_t dummy32 = 0;
108 emit(dummy32);
109
110 emit(dummy32);
111
112 uint16_t e_ehsize = 0x34;
113 emit(e_ehsize);
114
115 uint16_t e_phentsize = 0x20;
116 emit(e_phentsize);
117
118 uint16_t e_phnum = SIZE(p.segments);
119 emit(e_phnum);
120
121 uint16_t dummy16 = 0x0;
122 emit(dummy16);
123
124 emit(dummy16);
125
126 emit(dummy16);
127
128 uint32_t p_offset = 0x34 + SIZE(p.segments)*0x20;
129 for (int i = 0; i < SIZE(p.segments); ++i) {
130
131
132 uint32_t p_type = 0x1;
133 emit(p_type);
134
135 emit(p_offset);
136
137 uint32_t p_start = p.segments.at(i).start;
138 emit(p_start);
139
140 emit(p_start);
141
142 uint32_t size = num_words(p.segments.at(i));
143 assert(p_offset + size < SEGMENT_ALIGNMENT);
144 emit(size);
145
146 emit(size);
147
148 uint32_t p_flags = (i == 0) ? 0x5 : 0x6;
149 emit(p_flags);
150
151
152
153
154
155
156
157
158
159
160
161
162 uint32_t p_align = 0x1000;
163 emit(p_align);
164 if (p_offset % p_align != p_start % p_align) {
165 raise << "segment starting at 0x" << HEXWORD << p_start << " is improperly aligned; alignment for p_offset " << p_offset << " should be " << (p_offset % p_align) << " but is " << (p_start % p_align) << '\n' << end();
166 return;
167 }
168
169
170 p_offset += size;
171 }
172
173
174 }
175
176 void write_segment(const segment& s, ostream& out) {
177 for (int i = 0; i < SIZE(s.lines); ++i) {
178 const vector<word>& w = s.lines.at(i).words;
179 for (int j = 0; j < SIZE(w); ++j) {
180 uint8_t x = hex_byte(w.at(j).data);
181 out.write(reinterpret_cast<const char*>(&x), 1);
182 }
183 }
184 }
185
186 uint32_t num_words(const segment& s) {
187 uint32_t sum = 0;
188 for (int i = 0; i < SIZE(s.lines); ++i)
189 sum += SIZE(s.lines.at(i).words);
190 return sum;
191 }
192
193 :(before "End Includes")
194 using std::ios;