1 //===- yaml2coff - Convert YAML to a COFF object file ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief The COFF component of yaml2obj.
13 //===----------------------------------------------------------------------===//
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"
30 /// This parses a yaml stream that represents a COFF object file.
31 /// See docs/yaml2obj for the yaml scheema.
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));
39 bool useBigObj() const {
40 return static_cast<int32_t>(Obj.Sections.size()) >
41 COFF::MaxNumberOfSections16;
44 unsigned getHeaderSize() const {
45 return useBigObj() ? COFF::Header32Size : COFF::Header16Size;
48 unsigned getSymbolSize() const {
49 return useBigObj() ? COFF::Symbol32Size : COFF::Symbol16Size;
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;
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;
61 if (Name.size() <= COFF::NameSize) {
62 std::copy(Name.begin(), Name.end(), Sec.Header.Name);
64 // Add string to the string table and format the index for output.
65 unsigned Index = getStringIndex(Name);
66 std::string str = utostr(Index);
68 errs() << "String table got too large";
71 Sec.Header.Name[0] = '/';
72 std::copy(str.begin(), str.end(), Sec.Header.Name + 1);
75 Sec.Header.Characteristics |= (Log2_32(Sec.Alignment) + 1) << 20;
81 for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols.begin(),
82 e = Obj.Symbols.end(); i != e; ++i) {
83 COFFYAML::Symbol &Sym = *i;
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);
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;
97 Sym.Header.Type = Sym.SimpleType;
98 Sym.Header.Type |= Sym.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT;
104 if (!parseSections())
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;
123 COFFYAML::Object &Obj;
125 StringMap<unsigned> StringTableMap;
126 std::string StringTable;
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;
135 // The section table starts immediately after the header, including the
137 SectionTableStart = CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader;
138 SectionTableSize = COFF::SectionSize * CP.Obj.Sections.size();
140 uint32_t CurrentSectionDataOffset = SectionTableStart + SectionTableSize;
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();
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;
156 // TODO: Handle alignment.
158 i->Header.SizeOfRawData = 0;
159 i->Header.PointerToRawData = 0;
163 uint32_t SymbolTableStart = CurrentSectionDataOffset;
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();
170 uint32_t NumberOfAuxSymbols = 0;
171 if (i->FunctionDefinition)
172 NumberOfAuxSymbols += 1;
173 if (i->bfAndefSymbol)
174 NumberOfAuxSymbols += 1;
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;
183 NumberOfAuxSymbols += 1;
184 i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols;
185 NumberOfSymbols += 1 + NumberOfAuxSymbols;
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;
193 *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0])
194 = CP.StringTable.size();
199 template <typename value_type>
200 struct binary_le_impl {
202 binary_le_impl(value_type V) : Value(V) {}
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>(
211 OS.write(Buffer, sizeof(BLE.Value));
215 template <typename value_type>
216 binary_le_impl<value_type> binary_le(value_type V) {
217 return binary_le_impl<value_type>(V);
220 template <size_t NumBytes>
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));
233 template <typename T>
234 zeros_impl<sizeof(T)> zeros(const T &) {
235 return zeros_impl<sizeof(T)>();
238 struct num_zeros_impl {
240 num_zeros_impl(size_t N) : N(N) {}
243 raw_ostream &operator<<(raw_ostream &OS, const num_zeros_impl &NZI) {
244 for (size_t I = 0; I != NZI.N; ++I)
249 num_zeros_impl num_zeros(size_t N) {
250 num_zeros_impl NZI(N);
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);
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);
279 // Output section table.
280 for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
281 e = CP.Obj.Sections.end();
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);
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();
300 SymbolTableIndexMap[I->Name] = CurSymbol;
301 CurSymbol += 1 + I->Header.NumberOfAuxSymbols;
304 // Output section data.
305 for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
306 e = CP.Obj.Sections.end();
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);
318 // Output symbol table.
320 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
321 e = CP.Obj.Symbols.end();
323 OS.write(i->Header.Name, COFF::NameSize);
324 OS << binary_le(i->Header.Value);
326 OS << binary_le(i->Header.SectionNumber);
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);
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);
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);
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);
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);
379 // Output string table.
380 OS.write(&CP.StringTable[0], CP.StringTable.size());
384 int yaml2coff(yaml::Input &YIn, raw_ostream &Out) {
385 COFFYAML::Object Doc;
388 errs() << "yaml2obj: Failed to parse YAML file!\n";
394 errs() << "yaml2obj: Failed to parse YAML file!\n";
398 if (!layoutCOFF(CP)) {
399 errs() << "yaml2obj: Failed to layout COFF file!\n";
402 if (!writeCOFF(CP, Out)) {
403 errs() << "yaml2obj: Failed to write COFF file!\n";