Fixing a -Wsign-compare warning; NFC.
[oota-llvm.git] / tools / yaml2obj / yaml2coff.cpp
1 //===- yaml2coff - Convert YAML to a COFF 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 /// \file
11 /// \brief The COFF component of yaml2obj.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "yaml2obj.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/Object/COFFYAML.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <vector>
27
28 using namespace llvm;
29
30 /// This parses a yaml stream that represents a COFF object file.
31 /// See docs/yaml2obj for the yaml scheema.
32 struct COFFParser {
33   COFFParser(COFFYAML::Object &Obj) : Obj(Obj) {
34     // A COFF string table always starts with a 4 byte size field. Offsets into
35     // it include this size, so allocate it now.
36     StringTable.append(4, char(0));
37   }
38
39   bool useBigObj() const {
40     return static_cast<int32_t>(Obj.Sections.size()) >
41            COFF::MaxNumberOfSections16;
42   }
43
44   unsigned getHeaderSize() const {
45     return useBigObj() ? COFF::Header32Size : COFF::Header16Size;
46   }
47
48   unsigned getSymbolSize() const {
49     return useBigObj() ? COFF::Symbol32Size : COFF::Symbol16Size;
50   }
51
52   bool parseSections() {
53     for (std::vector<COFFYAML::Section>::iterator i = Obj.Sections.begin(),
54            e = Obj.Sections.end(); i != e; ++i) {
55       COFFYAML::Section &Sec = *i;
56
57       // If the name is less than 8 bytes, store it in place, otherwise
58       // store it in the string table.
59       StringRef Name = Sec.Name;
60
61       if (Name.size() <= COFF::NameSize) {
62         std::copy(Name.begin(), Name.end(), Sec.Header.Name);
63       } else {
64         // Add string to the string table and format the index for output.
65         unsigned Index = getStringIndex(Name);
66         std::string str = utostr(Index);
67         if (str.size() > 7) {
68           errs() << "String table got too large";
69           return false;
70         }
71         Sec.Header.Name[0] = '/';
72         std::copy(str.begin(), str.end(), Sec.Header.Name + 1);
73       }
74
75       Sec.Header.Characteristics |= (Log2_32(Sec.Alignment) + 1) << 20;
76     }
77     return true;
78   }
79
80   bool parseSymbols() {
81     for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols.begin(),
82            e = Obj.Symbols.end(); i != e; ++i) {
83       COFFYAML::Symbol &Sym = *i;
84
85       // If the name is less than 8 bytes, store it in place, otherwise
86       // store it in the string table.
87       StringRef Name = Sym.Name;
88       if (Name.size() <= COFF::NameSize) {
89         std::copy(Name.begin(), Name.end(), Sym.Header.Name);
90       } else {
91         // Add string to the string table and format the index for output.
92         unsigned Index = getStringIndex(Name);
93         *reinterpret_cast<support::aligned_ulittle32_t*>(
94             Sym.Header.Name + 4) = Index;
95       }
96
97       Sym.Header.Type = Sym.SimpleType;
98       Sym.Header.Type |= Sym.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT;
99     }
100     return true;
101   }
102
103   bool parse() {
104     if (!parseSections())
105       return false;
106     if (!parseSymbols())
107       return false;
108     return true;
109   }
110
111   unsigned getStringIndex(StringRef Str) {
112     StringMap<unsigned>::iterator i = StringTableMap.find(Str);
113     if (i == StringTableMap.end()) {
114       unsigned Index = StringTable.size();
115       StringTable.append(Str.begin(), Str.end());
116       StringTable.push_back(0);
117       StringTableMap[Str] = Index;
118       return Index;
119     }
120     return i->second;
121   }
122
123   COFFYAML::Object &Obj;
124
125   StringMap<unsigned> StringTableMap;
126   std::string StringTable;
127 };
128
129 // Take a CP and assign addresses and sizes to everything. Returns false if the
130 // layout is not valid to do.
131 static bool layoutCOFF(COFFParser &CP) {
132   uint32_t SectionTableStart = 0;
133   uint32_t SectionTableSize  = 0;
134
135   // The section table starts immediately after the header, including the
136   // optional header.
137   SectionTableStart = CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader;
138   SectionTableSize = COFF::SectionSize * CP.Obj.Sections.size();
139
140   uint32_t CurrentSectionDataOffset = SectionTableStart + SectionTableSize;
141
142   // Assign each section data address consecutively.
143   for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
144                                                 e = CP.Obj.Sections.end();
145                                                 i != e; ++i) {
146     if (i->SectionData.binary_size() > 0) {
147       i->Header.SizeOfRawData = i->SectionData.binary_size();
148       i->Header.PointerToRawData = CurrentSectionDataOffset;
149       CurrentSectionDataOffset += i->Header.SizeOfRawData;
150       if (!i->Relocations.empty()) {
151         i->Header.PointerToRelocations = CurrentSectionDataOffset;
152         i->Header.NumberOfRelocations = i->Relocations.size();
153         CurrentSectionDataOffset += i->Header.NumberOfRelocations *
154           COFF::RelocationSize;
155       }
156       // TODO: Handle alignment.
157     } else {
158       i->Header.SizeOfRawData = 0;
159       i->Header.PointerToRawData = 0;
160     }
161   }
162
163   uint32_t SymbolTableStart = CurrentSectionDataOffset;
164
165   // Calculate number of symbols.
166   uint32_t NumberOfSymbols = 0;
167   for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
168                                                e = CP.Obj.Symbols.end();
169                                                i != e; ++i) {
170     uint32_t NumberOfAuxSymbols = 0;
171     if (i->FunctionDefinition)
172       NumberOfAuxSymbols += 1;
173     if (i->bfAndefSymbol)
174       NumberOfAuxSymbols += 1;
175     if (i->WeakExternal)
176       NumberOfAuxSymbols += 1;
177     if (!i->File.empty())
178       NumberOfAuxSymbols +=
179           (i->File.size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
180     if (i->SectionDefinition)
181       NumberOfAuxSymbols += 1;
182     if (i->CLRToken)
183       NumberOfAuxSymbols += 1;
184     i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols;
185     NumberOfSymbols += 1 + NumberOfAuxSymbols;
186   }
187
188   // Store all the allocated start addresses in the header.
189   CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size();
190   CP.Obj.Header.NumberOfSymbols = NumberOfSymbols;
191   CP.Obj.Header.PointerToSymbolTable = SymbolTableStart;
192
193   *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0])
194     = CP.StringTable.size();
195
196   return true;
197 }
198
199 template <typename value_type>
200 struct binary_le_impl {
201   value_type Value;
202   binary_le_impl(value_type V) : Value(V) {}
203 };
204
205 template <typename value_type>
206 raw_ostream &operator <<( raw_ostream &OS
207                         , const binary_le_impl<value_type> &BLE) {
208   char Buffer[sizeof(BLE.Value)];
209   support::endian::write<value_type, support::little, support::unaligned>(
210     Buffer, BLE.Value);
211   OS.write(Buffer, sizeof(BLE.Value));
212   return OS;
213 }
214
215 template <typename value_type>
216 binary_le_impl<value_type> binary_le(value_type V) {
217   return binary_le_impl<value_type>(V);
218 }
219
220 template <size_t NumBytes>
221 struct zeros_impl {
222   zeros_impl() {}
223 };
224
225 template <size_t NumBytes>
226 raw_ostream &operator<<(raw_ostream &OS, const zeros_impl<NumBytes> &) {
227   char Buffer[NumBytes];
228   memset(Buffer, 0, sizeof(Buffer));
229   OS.write(Buffer, sizeof(Buffer));
230   return OS;
231 }
232
233 template <typename T>
234 zeros_impl<sizeof(T)> zeros(const T &) {
235   return zeros_impl<sizeof(T)>();
236 }
237
238 struct num_zeros_impl {
239   size_t N;
240   num_zeros_impl(size_t N) : N(N) {}
241 };
242
243 raw_ostream &operator<<(raw_ostream &OS, const num_zeros_impl &NZI) {
244   for (size_t I = 0; I != NZI.N; ++I)
245     OS.write(0);
246   return OS;
247 }
248
249 num_zeros_impl num_zeros(size_t N) {
250   num_zeros_impl NZI(N);
251   return NZI;
252 }
253
254 bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
255   if (CP.useBigObj()) {
256     OS << binary_le(static_cast<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN))
257        << binary_le(static_cast<uint16_t>(0xffff))
258        << binary_le(static_cast<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion))
259        << binary_le(CP.Obj.Header.Machine)
260        << binary_le(CP.Obj.Header.TimeDateStamp);
261     OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
262     OS << zeros(uint32_t(0))
263        << zeros(uint32_t(0))
264        << zeros(uint32_t(0))
265        << zeros(uint32_t(0))
266        << binary_le(CP.Obj.Header.NumberOfSections)
267        << binary_le(CP.Obj.Header.PointerToSymbolTable)
268        << binary_le(CP.Obj.Header.NumberOfSymbols);
269   } else {
270     OS << binary_le(CP.Obj.Header.Machine)
271        << binary_le(static_cast<int16_t>(CP.Obj.Header.NumberOfSections))
272        << binary_le(CP.Obj.Header.TimeDateStamp)
273        << binary_le(CP.Obj.Header.PointerToSymbolTable)
274        << binary_le(CP.Obj.Header.NumberOfSymbols)
275        << binary_le(CP.Obj.Header.SizeOfOptionalHeader)
276        << binary_le(CP.Obj.Header.Characteristics);
277   }
278
279   // Output section table.
280   for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
281                                                 e = CP.Obj.Sections.end();
282                                                 i != e; ++i) {
283     OS.write(i->Header.Name, COFF::NameSize);
284     OS << binary_le(i->Header.VirtualSize)
285        << binary_le(i->Header.VirtualAddress)
286        << binary_le(i->Header.SizeOfRawData)
287        << binary_le(i->Header.PointerToRawData)
288        << binary_le(i->Header.PointerToRelocations)
289        << binary_le(i->Header.PointerToLineNumbers)
290        << binary_le(i->Header.NumberOfRelocations)
291        << binary_le(i->Header.NumberOfLineNumbers)
292        << binary_le(i->Header.Characteristics);
293   }
294
295   unsigned CurSymbol = 0;
296   StringMap<unsigned> SymbolTableIndexMap;
297   for (std::vector<COFFYAML::Symbol>::iterator I = CP.Obj.Symbols.begin(),
298                                                E = CP.Obj.Symbols.end();
299        I != E; ++I) {
300     SymbolTableIndexMap[I->Name] = CurSymbol;
301     CurSymbol += 1 + I->Header.NumberOfAuxSymbols;
302   }
303
304   // Output section data.
305   for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
306                                                 e = CP.Obj.Sections.end();
307                                                 i != e; ++i) {
308     i->SectionData.writeAsBinary(OS);
309     for (unsigned I2 = 0, E2 = i->Relocations.size(); I2 != E2; ++I2) {
310       const COFFYAML::Relocation &R = i->Relocations[I2];
311       uint32_t SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
312       OS << binary_le(R.VirtualAddress)
313          << binary_le(SymbolTableIndex)
314          << binary_le(R.Type);
315     }
316   }
317
318   // Output symbol table.
319
320   for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
321                                                      e = CP.Obj.Symbols.end();
322                                                      i != e; ++i) {
323     OS.write(i->Header.Name, COFF::NameSize);
324     OS << binary_le(i->Header.Value);
325     if (CP.useBigObj())
326        OS << binary_le(i->Header.SectionNumber);
327     else
328        OS << binary_le(static_cast<int16_t>(i->Header.SectionNumber));
329     OS << binary_le(i->Header.Type)
330        << binary_le(i->Header.StorageClass)
331        << binary_le(i->Header.NumberOfAuxSymbols);
332
333     if (i->FunctionDefinition)
334       OS << binary_le(i->FunctionDefinition->TagIndex)
335          << binary_le(i->FunctionDefinition->TotalSize)
336          << binary_le(i->FunctionDefinition->PointerToLinenumber)
337          << binary_le(i->FunctionDefinition->PointerToNextFunction)
338          << zeros(i->FunctionDefinition->unused)
339          << num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
340     if (i->bfAndefSymbol)
341       OS << zeros(i->bfAndefSymbol->unused1)
342          << binary_le(i->bfAndefSymbol->Linenumber)
343          << zeros(i->bfAndefSymbol->unused2)
344          << binary_le(i->bfAndefSymbol->PointerToNextFunction)
345          << zeros(i->bfAndefSymbol->unused3)
346          << num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
347     if (i->WeakExternal)
348       OS << binary_le(i->WeakExternal->TagIndex)
349          << binary_le(i->WeakExternal->Characteristics)
350          << zeros(i->WeakExternal->unused)
351          << num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
352     if (!i->File.empty()) {
353       unsigned SymbolSize = CP.getSymbolSize();
354       uint32_t NumberOfAuxRecords =
355           (i->File.size() + SymbolSize - 1) / SymbolSize;
356       uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
357       uint32_t NumZeros = NumberOfAuxBytes - i->File.size();
358       OS.write(i->File.data(), i->File.size());
359       OS << num_zeros(NumZeros);
360     }
361     if (i->SectionDefinition)
362       OS << binary_le(i->SectionDefinition->Length)
363          << binary_le(i->SectionDefinition->NumberOfRelocations)
364          << binary_le(i->SectionDefinition->NumberOfLinenumbers)
365          << binary_le(i->SectionDefinition->CheckSum)
366          << binary_le(static_cast<int16_t>(i->SectionDefinition->Number))
367          << binary_le(i->SectionDefinition->Selection)
368          << zeros(i->SectionDefinition->unused)
369          << binary_le(static_cast<int16_t>(i->SectionDefinition->Number >> 16))
370          << num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
371     if (i->CLRToken)
372       OS << binary_le(i->CLRToken->AuxType)
373          << zeros(i->CLRToken->unused1)
374          << binary_le(i->CLRToken->SymbolTableIndex)
375          << zeros(i->CLRToken->unused2)
376          << num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
377   }
378
379   // Output string table.
380   OS.write(&CP.StringTable[0], CP.StringTable.size());
381   return true;
382 }
383
384 int yaml2coff(yaml::Input &YIn, raw_ostream &Out) {
385   COFFYAML::Object Doc;
386   YIn >> Doc;
387   if (YIn.error()) {
388     errs() << "yaml2obj: Failed to parse YAML file!\n";
389     return 1;
390   }
391
392   COFFParser CP(Doc);
393   if (!CP.parse()) {
394     errs() << "yaml2obj: Failed to parse YAML file!\n";
395     return 1;
396   }
397
398   if (!layoutCOFF(CP)) {
399     errs() << "yaml2obj: Failed to layout COFF file!\n";
400     return 1;
401   }
402   if (!writeCOFF(CP, Out)) {
403     errs() << "yaml2obj: Failed to write COFF file!\n";
404     return 1;
405   }
406   return 0;
407 }