blob: 0c285c2fd8c62584b3d7e5efe9a68a476461f653 [file] [log] [blame]
Michael J. Spencera915f242012-08-02 19:16:56 +00001//===- yaml2obj - Convert YAML to a binary object file --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program takes a YAML description of an object file and outputs the
11// binary equivalent.
12//
13// This is used for writing tests that require binary files.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringSwitch.h"
Rafael Espindola2bbe3782013-05-17 22:58:42 +000021#include "llvm/Object/COFFYaml.h"
Michael J. Spencera915f242012-08-02 19:16:56 +000022#include "llvm/Support/Casting.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/Endian.h"
25#include "llvm/Support/ManagedStatic.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencera915f242012-08-02 19:16:56 +000028#include "llvm/Support/Signals.h"
29#include "llvm/Support/SourceMgr.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000030#include "llvm/Support/raw_ostream.h"
31#include "llvm/Support/system_error.h"
Michael J. Spencera915f242012-08-02 19:16:56 +000032#include <vector>
33
34using namespace llvm;
35
36static cl::opt<std::string>
37 Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
38
Michael J. Spencera915f242012-08-02 19:16:56 +000039/// This parses a yaml stream that represents a COFF object file.
40/// See docs/yaml2obj for the yaml scheema.
41struct COFFParser {
Rafael Espindola8ec018c2013-04-02 23:56:40 +000042 COFFParser(COFFYAML::Object &Obj) : Obj(Obj) {
Michael J. Spencera915f242012-08-02 19:16:56 +000043 // A COFF string table always starts with a 4 byte size field. Offsets into
44 // it include this size, so allocate it now.
45 StringTable.append(4, 0);
46 }
47
Rafael Espindola8ec018c2013-04-02 23:56:40 +000048 bool parseSections() {
49 for (std::vector<COFFYAML::Section>::iterator i = Obj.Sections.begin(),
50 e = Obj.Sections.end(); i != e; ++i) {
Rafael Espindolac0f15f62013-04-23 19:26:43 +000051 COFFYAML::Section &Sec = *i;
Rafael Espindola8ec018c2013-04-02 23:56:40 +000052
53 // If the name is less than 8 bytes, store it in place, otherwise
54 // store it in the string table.
Rafael Espindolac0f15f62013-04-23 19:26:43 +000055 StringRef Name = Sec.Name;
56
Rafael Espindola8ec018c2013-04-02 23:56:40 +000057 if (Name.size() <= COFF::NameSize) {
58 std::copy(Name.begin(), Name.end(), Sec.Header.Name);
59 } else {
60 // Add string to the string table and format the index for output.
61 unsigned Index = getStringIndex(Name);
62 std::string str = utostr(Index);
63 if (str.size() > 7) {
64 errs() << "String table got too large";
Michael J. Spencera915f242012-08-02 19:16:56 +000065 return false;
66 }
Rafael Espindola8ec018c2013-04-02 23:56:40 +000067 Sec.Header.Name[0] = '/';
68 std::copy(str.begin(), str.end(), Sec.Header.Name + 1);
69 }
Rafael Espindola7098ae22013-05-06 20:11:21 +000070
71 Sec.Header.Characteristics |= (Log2_32(Sec.Alignment) + 1) << 20;
Michael J. Spencera915f242012-08-02 19:16:56 +000072 }
73 return true;
74 }
75
Rafael Espindola8ec018c2013-04-02 23:56:40 +000076 bool parseSymbols() {
77 for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols.begin(),
78 e = Obj.Symbols.end(); i != e; ++i) {
Rafael Espindolac0f15f62013-04-23 19:26:43 +000079 COFFYAML::Symbol &Sym = *i;
Michael J. Spencera915f242012-08-02 19:16:56 +000080
Rafael Espindola8ec018c2013-04-02 23:56:40 +000081 // If the name is less than 8 bytes, store it in place, otherwise
82 // store it in the string table.
Rafael Espindolac0f15f62013-04-23 19:26:43 +000083 StringRef Name = Sym.Name;
Rafael Espindola8ec018c2013-04-02 23:56:40 +000084 if (Name.size() <= COFF::NameSize) {
85 std::copy(Name.begin(), Name.end(), Sym.Header.Name);
86 } else {
87 // Add string to the string table and format the index for output.
88 unsigned Index = getStringIndex(Name);
89 *reinterpret_cast<support::aligned_ulittle32_t*>(
90 Sym.Header.Name + 4) = Index;
91 }
92
Rafael Espindolac0f15f62013-04-23 19:26:43 +000093 Sym.Header.Type = Sym.SimpleType;
94 Sym.Header.Type |= Sym.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT;
Michael J. Spencera915f242012-08-02 19:16:56 +000095 }
96 return true;
97 }
98
99 bool parse() {
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000100 if (!parseSections())
Michael J. Spencera915f242012-08-02 19:16:56 +0000101 return false;
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000102 if (!parseSymbols())
103 return false;
104 return true;
Michael J. Spencera915f242012-08-02 19:16:56 +0000105 }
106
107 unsigned getStringIndex(StringRef Str) {
108 StringMap<unsigned>::iterator i = StringTableMap.find(Str);
109 if (i == StringTableMap.end()) {
110 unsigned Index = StringTable.size();
111 StringTable.append(Str.begin(), Str.end());
112 StringTable.push_back(0);
113 StringTableMap[Str] = Index;
114 return Index;
115 }
116 return i->second;
117 }
118
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000119 COFFYAML::Object &Obj;
Michael J. Spencera915f242012-08-02 19:16:56 +0000120
Michael J. Spencera915f242012-08-02 19:16:56 +0000121 StringMap<unsigned> StringTableMap;
122 std::string StringTable;
123};
124
125// Take a CP and assign addresses and sizes to everything. Returns false if the
126// layout is not valid to do.
127static bool layoutCOFF(COFFParser &CP) {
128 uint32_t SectionTableStart = 0;
129 uint32_t SectionTableSize = 0;
130
131 // The section table starts immediately after the header, including the
132 // optional header.
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000133 SectionTableStart = sizeof(COFF::header) + CP.Obj.Header.SizeOfOptionalHeader;
134 SectionTableSize = sizeof(COFF::section) * CP.Obj.Sections.size();
Michael J. Spencera915f242012-08-02 19:16:56 +0000135
136 uint32_t CurrentSectionDataOffset = SectionTableStart + SectionTableSize;
137
138 // Assign each section data address consecutively.
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000139 for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
140 e = CP.Obj.Sections.end();
141 i != e; ++i) {
Rafael Espindola05bc4a62013-05-31 20:26:44 +0000142 StringRef SecData = i->SectionData.getHex();
143 if (!SecData.empty()) {
144 i->Header.SizeOfRawData = SecData.size()/2;
Michael J. Spencera915f242012-08-02 19:16:56 +0000145 i->Header.PointerToRawData = CurrentSectionDataOffset;
146 CurrentSectionDataOffset += i->Header.SizeOfRawData;
Rafael Espindola120cf572013-04-23 15:53:02 +0000147 if (!i->Relocations.empty()) {
148 i->Header.PointerToRelocations = CurrentSectionDataOffset;
149 i->Header.NumberOfRelocations = i->Relocations.size();
150 CurrentSectionDataOffset += i->Header.NumberOfRelocations *
151 COFF::RelocationSize;
152 }
Michael J. Spencera915f242012-08-02 19:16:56 +0000153 // TODO: Handle alignment.
154 } else {
155 i->Header.SizeOfRawData = 0;
156 i->Header.PointerToRawData = 0;
157 }
158 }
159
160 uint32_t SymbolTableStart = CurrentSectionDataOffset;
161
162 // Calculate number of symbols.
163 uint32_t NumberOfSymbols = 0;
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000164 for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
165 e = CP.Obj.Symbols.end();
166 i != e; ++i) {
Rafael Espindola05bc4a62013-05-31 20:26:44 +0000167 unsigned AuxBytes = i->AuxiliaryData.getHex().size() / 2;
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000168 if (AuxBytes % COFF::SymbolSize != 0) {
Rafael Espindolaa0840c42013-04-23 19:39:34 +0000169 errs() << "AuxiliaryData size not a multiple of symbol size!\n";
Michael J. Spencera915f242012-08-02 19:16:56 +0000170 return false;
171 }
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000172 i->Header.NumberOfAuxSymbols = AuxBytes / COFF::SymbolSize;
Michael J. Spencera915f242012-08-02 19:16:56 +0000173 NumberOfSymbols += 1 + i->Header.NumberOfAuxSymbols;
174 }
175
176 // Store all the allocated start addresses in the header.
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000177 CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size();
178 CP.Obj.Header.NumberOfSymbols = NumberOfSymbols;
179 CP.Obj.Header.PointerToSymbolTable = SymbolTableStart;
Michael J. Spencera915f242012-08-02 19:16:56 +0000180
181 *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0])
182 = CP.StringTable.size();
183
184 return true;
185}
186
187template <typename value_type>
188struct binary_le_impl {
189 value_type Value;
190 binary_le_impl(value_type V) : Value(V) {}
191};
192
193template <typename value_type>
194raw_ostream &operator <<( raw_ostream &OS
195 , const binary_le_impl<value_type> &BLE) {
196 char Buffer[sizeof(BLE.Value)];
Michael J. Spencerc8b18df2013-01-02 20:14:11 +0000197 support::endian::write<value_type, support::little, support::unaligned>(
198 Buffer, BLE.Value);
Michael J. Spencera915f242012-08-02 19:16:56 +0000199 OS.write(Buffer, sizeof(BLE.Value));
200 return OS;
201}
202
203template <typename value_type>
204binary_le_impl<value_type> binary_le(value_type V) {
205 return binary_le_impl<value_type>(V);
206}
207
Rafael Espindola3d3cc322013-04-25 03:07:42 +0000208static bool writeHexData(StringRef Data, raw_ostream &OS) {
209 unsigned Size = Data.size();
210 if (Size % 2)
211 return false;
212
213 for (unsigned I = 0; I != Size; I += 2) {
214 uint8_t Byte;
215 if (Data.substr(I, 2).getAsInteger(16, Byte))
216 return false;
217 OS.write(Byte);
218 }
219
220 return true;
221}
222
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000223bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
224 OS << binary_le(CP.Obj.Header.Machine)
225 << binary_le(CP.Obj.Header.NumberOfSections)
226 << binary_le(CP.Obj.Header.TimeDateStamp)
227 << binary_le(CP.Obj.Header.PointerToSymbolTable)
228 << binary_le(CP.Obj.Header.NumberOfSymbols)
229 << binary_le(CP.Obj.Header.SizeOfOptionalHeader)
230 << binary_le(CP.Obj.Header.Characteristics);
Michael J. Spencera915f242012-08-02 19:16:56 +0000231
232 // Output section table.
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000233 for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
234 e = CP.Obj.Sections.end();
235 i != e; ++i) {
Michael J. Spencera915f242012-08-02 19:16:56 +0000236 OS.write(i->Header.Name, COFF::NameSize);
237 OS << binary_le(i->Header.VirtualSize)
238 << binary_le(i->Header.VirtualAddress)
239 << binary_le(i->Header.SizeOfRawData)
240 << binary_le(i->Header.PointerToRawData)
241 << binary_le(i->Header.PointerToRelocations)
242 << binary_le(i->Header.PointerToLineNumbers)
243 << binary_le(i->Header.NumberOfRelocations)
244 << binary_le(i->Header.NumberOfLineNumbers)
245 << binary_le(i->Header.Characteristics);
246 }
247
248 // Output section data.
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000249 for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
250 e = CP.Obj.Sections.end();
251 i != e; ++i) {
Rafael Espindola05bc4a62013-05-31 20:26:44 +0000252 StringRef SecData = i->SectionData.getHex();
253 if (!SecData.empty()) {
254 if (!writeHexData(SecData, OS)) {
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000255 errs() << "SectionData must be a collection of pairs of hex bytes";
256 return false;
257 }
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000258 }
Rafael Espindola120cf572013-04-23 15:53:02 +0000259 for (unsigned I2 = 0, E2 = i->Relocations.size(); I2 != E2; ++I2) {
260 const COFF::relocation &R = i->Relocations[I2];
261 OS << binary_le(R.VirtualAddress)
262 << binary_le(R.SymbolTableIndex)
263 << binary_le(R.Type);
264 }
Michael J. Spencera915f242012-08-02 19:16:56 +0000265 }
266
267 // Output symbol table.
268
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000269 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
270 e = CP.Obj.Symbols.end();
271 i != e; ++i) {
Michael J. Spencera915f242012-08-02 19:16:56 +0000272 OS.write(i->Header.Name, COFF::NameSize);
273 OS << binary_le(i->Header.Value)
274 << binary_le(i->Header.SectionNumber)
275 << binary_le(i->Header.Type)
276 << binary_le(i->Header.StorageClass)
277 << binary_le(i->Header.NumberOfAuxSymbols);
Rafael Espindola05bc4a62013-05-31 20:26:44 +0000278 StringRef Data = i->AuxiliaryData.getHex();
279 if (!Data.empty()) {
280 if (!writeHexData(Data, OS)) {
Rafael Espindolaa0840c42013-04-23 19:39:34 +0000281 errs() << "AuxiliaryData must be a collection of pairs of hex bytes";
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000282 return false;
283 }
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000284 }
Michael J. Spencera915f242012-08-02 19:16:56 +0000285 }
286
287 // Output string table.
288 OS.write(&CP.StringTable[0], CP.StringTable.size());
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000289 return true;
Michael J. Spencera915f242012-08-02 19:16:56 +0000290}
291
292int main(int argc, char **argv) {
293 cl::ParseCommandLineOptions(argc, argv);
294 sys::PrintStackTraceOnErrorSignal();
295 PrettyStackTraceProgram X(argc, argv);
296 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
297
298 OwningPtr<MemoryBuffer> Buf;
299 if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
300 return 1;
301
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000302 yaml::Input YIn(Buf->getBuffer());
303 COFFYAML::Object Doc;
304 YIn >> Doc;
305 if (YIn.error()) {
306 errs() << "yaml2obj: Failed to parse YAML file!\n";
307 return 1;
308 }
309
310 COFFParser CP(Doc);
Michael J. Spencera915f242012-08-02 19:16:56 +0000311 if (!CP.parse()) {
312 errs() << "yaml2obj: Failed to parse YAML file!\n";
313 return 1;
314 }
Rafael Espindola8ec018c2013-04-02 23:56:40 +0000315
Michael J. Spencera915f242012-08-02 19:16:56 +0000316 if (!layoutCOFF(CP)) {
317 errs() << "yaml2obj: Failed to layout COFF file!\n";
318 return 1;
319 }
Rafael Espindolac0f15f62013-04-23 19:26:43 +0000320 if (!writeCOFF(CP, outs())) {
321 errs() << "yaml2obj: Failed to write COFF file!\n";
322 return 1;
323 }
Michael J. Spencera915f242012-08-02 19:16:56 +0000324}