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