2013-06-06 03:56:47 +08:00
|
|
|
//===- yaml2coff - Convert YAML to a COFF object file ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief The COFF component of yaml2obj.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "yaml2obj.h"
|
2014-03-19 12:47:47 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2013-06-06 03:56:47 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2014-11-14 16:15:42 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/Object/COFFYAML.h"
|
2013-06-06 03:56:47 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// This parses a yaml stream that represents a COFF object file.
|
|
|
|
/// See docs/yaml2obj for the yaml scheema.
|
|
|
|
struct COFFParser {
|
2014-11-14 16:15:42 +08:00
|
|
|
COFFParser(COFFYAML::Object &Obj)
|
|
|
|
: Obj(Obj), SectionTableStart(0), SectionTableSize(0) {
|
2013-06-06 03:56:47 +08:00
|
|
|
// A COFF string table always starts with a 4 byte size field. Offsets into
|
|
|
|
// it include this size, so allocate it now.
|
2013-10-13 05:29:16 +08:00
|
|
|
StringTable.append(4, char(0));
|
2013-06-06 03:56:47 +08:00
|
|
|
}
|
|
|
|
|
2014-09-16 11:52:46 +08:00
|
|
|
bool useBigObj() const {
|
2014-10-22 21:09:43 +08:00
|
|
|
return static_cast<int32_t>(Obj.Sections.size()) >
|
|
|
|
COFF::MaxNumberOfSections16;
|
2014-09-16 11:52:46 +08:00
|
|
|
}
|
|
|
|
|
2014-11-14 16:15:42 +08:00
|
|
|
bool isPE() const { return Obj.OptionalHeader.hasValue(); }
|
|
|
|
bool is64Bit() const {
|
|
|
|
return Obj.Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getFileAlignment() const {
|
|
|
|
return Obj.OptionalHeader->Header.FileAlignment;
|
|
|
|
}
|
|
|
|
|
2014-09-16 11:52:46 +08:00
|
|
|
unsigned getHeaderSize() const {
|
|
|
|
return useBigObj() ? COFF::Header32Size : COFF::Header16Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getSymbolSize() const {
|
|
|
|
return useBigObj() ? COFF::Symbol32Size : COFF::Symbol16Size;
|
|
|
|
}
|
|
|
|
|
2013-06-06 03:56:47 +08:00
|
|
|
bool parseSections() {
|
|
|
|
for (std::vector<COFFYAML::Section>::iterator i = Obj.Sections.begin(),
|
|
|
|
e = Obj.Sections.end(); i != e; ++i) {
|
|
|
|
COFFYAML::Section &Sec = *i;
|
|
|
|
|
|
|
|
// If the name is less than 8 bytes, store it in place, otherwise
|
|
|
|
// store it in the string table.
|
|
|
|
StringRef Name = Sec.Name;
|
|
|
|
|
|
|
|
if (Name.size() <= COFF::NameSize) {
|
|
|
|
std::copy(Name.begin(), Name.end(), Sec.Header.Name);
|
|
|
|
} else {
|
|
|
|
// Add string to the string table and format the index for output.
|
|
|
|
unsigned Index = getStringIndex(Name);
|
|
|
|
std::string str = utostr(Index);
|
|
|
|
if (str.size() > 7) {
|
|
|
|
errs() << "String table got too large";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Sec.Header.Name[0] = '/';
|
|
|
|
std::copy(str.begin(), str.end(), Sec.Header.Name + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sec.Header.Characteristics |= (Log2_32(Sec.Alignment) + 1) << 20;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseSymbols() {
|
|
|
|
for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols.begin(),
|
|
|
|
e = Obj.Symbols.end(); i != e; ++i) {
|
|
|
|
COFFYAML::Symbol &Sym = *i;
|
|
|
|
|
|
|
|
// If the name is less than 8 bytes, store it in place, otherwise
|
|
|
|
// store it in the string table.
|
|
|
|
StringRef Name = Sym.Name;
|
|
|
|
if (Name.size() <= COFF::NameSize) {
|
|
|
|
std::copy(Name.begin(), Name.end(), Sym.Header.Name);
|
|
|
|
} else {
|
|
|
|
// Add string to the string table and format the index for output.
|
|
|
|
unsigned Index = getStringIndex(Name);
|
|
|
|
*reinterpret_cast<support::aligned_ulittle32_t*>(
|
|
|
|
Sym.Header.Name + 4) = Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sym.Header.Type = Sym.SimpleType;
|
|
|
|
Sym.Header.Type |= Sym.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parse() {
|
|
|
|
if (!parseSections())
|
|
|
|
return false;
|
|
|
|
if (!parseSymbols())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getStringIndex(StringRef Str) {
|
|
|
|
StringMap<unsigned>::iterator i = StringTableMap.find(Str);
|
|
|
|
if (i == StringTableMap.end()) {
|
|
|
|
unsigned Index = StringTable.size();
|
|
|
|
StringTable.append(Str.begin(), Str.end());
|
|
|
|
StringTable.push_back(0);
|
|
|
|
StringTableMap[Str] = Index;
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
COFFYAML::Object &Obj;
|
|
|
|
|
|
|
|
StringMap<unsigned> StringTableMap;
|
|
|
|
std::string StringTable;
|
2014-11-14 16:15:42 +08:00
|
|
|
uint32_t SectionTableStart;
|
|
|
|
uint32_t SectionTableSize;
|
2013-06-06 03:56:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Take a CP and assign addresses and sizes to everything. Returns false if the
|
|
|
|
// layout is not valid to do.
|
2014-11-14 16:15:42 +08:00
|
|
|
static bool layoutOptionalHeader(COFFParser &CP) {
|
|
|
|
if (!CP.isPE())
|
|
|
|
return true;
|
2014-11-15 03:35:59 +08:00
|
|
|
unsigned PEHeaderSize = CP.is64Bit() ? sizeof(object::pe32plus_header)
|
|
|
|
: sizeof(object::pe32_header);
|
2014-11-14 16:15:42 +08:00
|
|
|
CP.Obj.Header.SizeOfOptionalHeader =
|
2014-11-15 03:35:59 +08:00
|
|
|
PEHeaderSize +
|
|
|
|
sizeof(object::data_directory) * (COFF::NUM_DATA_DIRECTORIES + 1);
|
2014-11-14 16:15:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-06-06 03:56:47 +08:00
|
|
|
|
2014-11-15 10:03:59 +08:00
|
|
|
namespace {
|
|
|
|
enum { DOSStubSize = 128 };
|
|
|
|
}
|
|
|
|
|
2014-11-14 16:15:42 +08:00
|
|
|
// Take a CP and assign addresses and sizes to everything. Returns false if the
|
|
|
|
// layout is not valid to do.
|
|
|
|
static bool layoutCOFF(COFFParser &CP) {
|
2013-06-06 03:56:47 +08:00
|
|
|
// The section table starts immediately after the header, including the
|
|
|
|
// optional header.
|
2014-11-14 16:15:42 +08:00
|
|
|
CP.SectionTableStart =
|
|
|
|
CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader;
|
2014-11-15 10:03:59 +08:00
|
|
|
if (CP.isPE())
|
|
|
|
CP.SectionTableStart += DOSStubSize + sizeof(COFF::PEMagic);
|
2014-11-14 16:15:42 +08:00
|
|
|
CP.SectionTableSize = COFF::SectionSize * CP.Obj.Sections.size();
|
2013-06-06 03:56:47 +08:00
|
|
|
|
2014-11-14 16:15:42 +08:00
|
|
|
uint32_t CurrentSectionDataOffset =
|
|
|
|
CP.SectionTableStart + CP.SectionTableSize;
|
2013-06-06 03:56:47 +08:00
|
|
|
|
|
|
|
// Assign each section data address consecutively.
|
2014-11-14 16:15:42 +08:00
|
|
|
for (COFFYAML::Section &S : CP.Obj.Sections) {
|
|
|
|
if (S.SectionData.binary_size() > 0) {
|
|
|
|
CurrentSectionDataOffset = RoundUpToAlignment(
|
|
|
|
CurrentSectionDataOffset, CP.isPE() ? CP.getFileAlignment() : 4);
|
|
|
|
S.Header.SizeOfRawData = S.SectionData.binary_size();
|
|
|
|
if (CP.isPE())
|
|
|
|
S.Header.SizeOfRawData =
|
|
|
|
RoundUpToAlignment(S.Header.SizeOfRawData, CP.getFileAlignment());
|
|
|
|
S.Header.PointerToRawData = CurrentSectionDataOffset;
|
|
|
|
CurrentSectionDataOffset += S.Header.SizeOfRawData;
|
|
|
|
if (!S.Relocations.empty()) {
|
|
|
|
S.Header.PointerToRelocations = CurrentSectionDataOffset;
|
|
|
|
S.Header.NumberOfRelocations = S.Relocations.size();
|
|
|
|
CurrentSectionDataOffset +=
|
|
|
|
S.Header.NumberOfRelocations * COFF::RelocationSize;
|
2013-06-06 03:56:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
2014-11-14 16:15:42 +08:00
|
|
|
S.Header.SizeOfRawData = 0;
|
|
|
|
S.Header.PointerToRawData = 0;
|
2013-06-06 03:56:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t SymbolTableStart = CurrentSectionDataOffset;
|
|
|
|
|
|
|
|
// Calculate number of symbols.
|
|
|
|
uint32_t NumberOfSymbols = 0;
|
|
|
|
for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
|
|
|
|
e = CP.Obj.Symbols.end();
|
|
|
|
i != e; ++i) {
|
2014-03-19 12:47:47 +08:00
|
|
|
uint32_t NumberOfAuxSymbols = 0;
|
|
|
|
if (i->FunctionDefinition)
|
|
|
|
NumberOfAuxSymbols += 1;
|
|
|
|
if (i->bfAndefSymbol)
|
|
|
|
NumberOfAuxSymbols += 1;
|
|
|
|
if (i->WeakExternal)
|
|
|
|
NumberOfAuxSymbols += 1;
|
|
|
|
if (!i->File.empty())
|
|
|
|
NumberOfAuxSymbols +=
|
2014-09-16 11:52:46 +08:00
|
|
|
(i->File.size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
|
2014-03-19 12:47:47 +08:00
|
|
|
if (i->SectionDefinition)
|
|
|
|
NumberOfAuxSymbols += 1;
|
|
|
|
if (i->CLRToken)
|
|
|
|
NumberOfAuxSymbols += 1;
|
|
|
|
i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols;
|
|
|
|
NumberOfSymbols += 1 + NumberOfAuxSymbols;
|
2013-06-06 03:56:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store all the allocated start addresses in the header.
|
|
|
|
CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size();
|
|
|
|
CP.Obj.Header.NumberOfSymbols = NumberOfSymbols;
|
2014-11-14 16:15:42 +08:00
|
|
|
if (NumberOfSymbols > 0 || CP.StringTable.size() > 4)
|
|
|
|
CP.Obj.Header.PointerToSymbolTable = SymbolTableStart;
|
|
|
|
else
|
|
|
|
CP.Obj.Header.PointerToSymbolTable = 0;
|
2013-06-06 03:56:47 +08:00
|
|
|
|
|
|
|
*reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0])
|
|
|
|
= CP.StringTable.size();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename value_type>
|
|
|
|
struct binary_le_impl {
|
|
|
|
value_type Value;
|
|
|
|
binary_le_impl(value_type V) : Value(V) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename value_type>
|
|
|
|
raw_ostream &operator <<( raw_ostream &OS
|
|
|
|
, const binary_le_impl<value_type> &BLE) {
|
|
|
|
char Buffer[sizeof(BLE.Value)];
|
|
|
|
support::endian::write<value_type, support::little, support::unaligned>(
|
|
|
|
Buffer, BLE.Value);
|
|
|
|
OS.write(Buffer, sizeof(BLE.Value));
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename value_type>
|
|
|
|
binary_le_impl<value_type> binary_le(value_type V) {
|
|
|
|
return binary_le_impl<value_type>(V);
|
|
|
|
}
|
|
|
|
|
2015-04-12 02:57:14 +08:00
|
|
|
template <size_t NumBytes> struct zeros_impl {};
|
2014-03-19 12:47:47 +08:00
|
|
|
|
|
|
|
template <size_t NumBytes>
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const zeros_impl<NumBytes> &) {
|
|
|
|
char Buffer[NumBytes];
|
|
|
|
memset(Buffer, 0, sizeof(Buffer));
|
|
|
|
OS.write(Buffer, sizeof(Buffer));
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
zeros_impl<sizeof(T)> zeros(const T &) {
|
|
|
|
return zeros_impl<sizeof(T)>();
|
|
|
|
}
|
|
|
|
|
2014-09-16 11:52:46 +08:00
|
|
|
struct num_zeros_impl {
|
|
|
|
size_t N;
|
|
|
|
num_zeros_impl(size_t N) : N(N) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const num_zeros_impl &NZI) {
|
|
|
|
for (size_t I = 0; I != NZI.N; ++I)
|
|
|
|
OS.write(0);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2015-03-10 00:23:46 +08:00
|
|
|
static num_zeros_impl num_zeros(size_t N) {
|
2014-09-16 11:52:46 +08:00
|
|
|
num_zeros_impl NZI(N);
|
|
|
|
return NZI;
|
|
|
|
}
|
|
|
|
|
2014-11-14 16:15:42 +08:00
|
|
|
template <typename T>
|
2014-11-15 03:35:59 +08:00
|
|
|
static uint32_t initializeOptionalHeader(COFFParser &CP, uint16_t Magic, T Header) {
|
2014-11-14 16:15:42 +08:00
|
|
|
memset(Header, 0, sizeof(*Header));
|
|
|
|
Header->Magic = Magic;
|
|
|
|
Header->SectionAlignment = CP.Obj.OptionalHeader->Header.SectionAlignment;
|
2014-11-15 03:35:59 +08:00
|
|
|
Header->FileAlignment = CP.Obj.OptionalHeader->Header.FileAlignment;
|
2014-11-14 16:15:42 +08:00
|
|
|
uint32_t SizeOfCode = 0, SizeOfInitializedData = 0,
|
|
|
|
SizeOfUninitializedData = 0;
|
|
|
|
uint32_t SizeOfHeaders = RoundUpToAlignment(
|
2014-11-15 03:35:59 +08:00
|
|
|
CP.SectionTableStart + CP.SectionTableSize, Header->FileAlignment);
|
|
|
|
uint32_t SizeOfImage =
|
|
|
|
RoundUpToAlignment(SizeOfHeaders, Header->SectionAlignment);
|
|
|
|
uint32_t BaseOfData = 0;
|
2014-11-14 16:15:42 +08:00
|
|
|
for (const COFFYAML::Section &S : CP.Obj.Sections) {
|
|
|
|
if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_CODE)
|
|
|
|
SizeOfCode += S.Header.SizeOfRawData;
|
|
|
|
if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
|
|
|
|
SizeOfInitializedData += S.Header.SizeOfRawData;
|
|
|
|
if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
|
|
|
|
SizeOfUninitializedData += S.Header.SizeOfRawData;
|
|
|
|
if (S.Name.equals(".text"))
|
2014-11-15 03:35:59 +08:00
|
|
|
Header->BaseOfCode = S.Header.VirtualAddress; // RVA
|
|
|
|
else if (S.Name.equals(".data"))
|
|
|
|
BaseOfData = S.Header.VirtualAddress; // RVA
|
2014-11-14 16:15:42 +08:00
|
|
|
if (S.Header.VirtualAddress)
|
|
|
|
SizeOfImage +=
|
|
|
|
RoundUpToAlignment(S.Header.VirtualSize, Header->SectionAlignment);
|
|
|
|
}
|
|
|
|
Header->SizeOfCode = SizeOfCode;
|
|
|
|
Header->SizeOfInitializedData = SizeOfInitializedData;
|
|
|
|
Header->SizeOfUninitializedData = SizeOfUninitializedData;
|
|
|
|
Header->AddressOfEntryPoint =
|
|
|
|
CP.Obj.OptionalHeader->Header.AddressOfEntryPoint; // RVA
|
|
|
|
Header->ImageBase = CP.Obj.OptionalHeader->Header.ImageBase;
|
|
|
|
Header->MajorOperatingSystemVersion =
|
|
|
|
CP.Obj.OptionalHeader->Header.MajorOperatingSystemVersion;
|
|
|
|
Header->MinorOperatingSystemVersion =
|
|
|
|
CP.Obj.OptionalHeader->Header.MinorOperatingSystemVersion;
|
|
|
|
Header->MajorImageVersion =
|
|
|
|
CP.Obj.OptionalHeader->Header.MajorImageVersion;
|
|
|
|
Header->MinorImageVersion =
|
|
|
|
CP.Obj.OptionalHeader->Header.MinorImageVersion;
|
|
|
|
Header->MajorSubsystemVersion =
|
|
|
|
CP.Obj.OptionalHeader->Header.MajorSubsystemVersion;
|
|
|
|
Header->MinorSubsystemVersion =
|
|
|
|
CP.Obj.OptionalHeader->Header.MinorSubsystemVersion;
|
|
|
|
Header->SizeOfImage = SizeOfImage;
|
|
|
|
Header->SizeOfHeaders = SizeOfHeaders;
|
|
|
|
Header->Subsystem = CP.Obj.OptionalHeader->Header.Subsystem;
|
|
|
|
Header->DLLCharacteristics = CP.Obj.OptionalHeader->Header.DLLCharacteristics;
|
|
|
|
Header->SizeOfStackReserve = CP.Obj.OptionalHeader->Header.SizeOfStackReserve;
|
|
|
|
Header->SizeOfStackCommit = CP.Obj.OptionalHeader->Header.SizeOfStackCommit;
|
|
|
|
Header->SizeOfHeapReserve = CP.Obj.OptionalHeader->Header.SizeOfHeapReserve;
|
|
|
|
Header->SizeOfHeapCommit = CP.Obj.OptionalHeader->Header.SizeOfHeapCommit;
|
|
|
|
Header->NumberOfRvaAndSize = COFF::NUM_DATA_DIRECTORIES + 1;
|
2014-11-15 03:35:59 +08:00
|
|
|
return BaseOfData;
|
2014-11-14 16:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
|
|
|
|
if (CP.isPE()) {
|
|
|
|
// PE files start with a DOS stub.
|
|
|
|
object::dos_header DH;
|
|
|
|
memset(&DH, 0, sizeof(DH));
|
|
|
|
|
|
|
|
// DOS EXEs start with "MZ" magic.
|
|
|
|
DH.Magic[0] = 'M';
|
|
|
|
DH.Magic[1] = 'Z';
|
|
|
|
// Initializing the AddressOfRelocationTable is strictly optional but
|
|
|
|
// mollifies certain tools which expect it to have a value greater than
|
|
|
|
// 0x40.
|
|
|
|
DH.AddressOfRelocationTable = sizeof(DH);
|
|
|
|
// This is the address of the PE signature.
|
2014-11-15 10:03:59 +08:00
|
|
|
DH.AddressOfNewExeHeader = DOSStubSize;
|
2014-11-14 16:15:42 +08:00
|
|
|
|
|
|
|
// Write out our DOS stub.
|
|
|
|
OS.write(reinterpret_cast<char *>(&DH), sizeof(DH));
|
|
|
|
// Write padding until we reach the position of where our PE signature
|
|
|
|
// should live.
|
2014-11-15 10:03:59 +08:00
|
|
|
OS << num_zeros(DOSStubSize - sizeof(DH));
|
2014-11-14 16:15:42 +08:00
|
|
|
// Write out the PE signature.
|
|
|
|
OS.write(COFF::PEMagic, sizeof(COFF::PEMagic));
|
|
|
|
}
|
2014-09-16 11:52:46 +08:00
|
|
|
if (CP.useBigObj()) {
|
|
|
|
OS << binary_le(static_cast<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN))
|
|
|
|
<< binary_le(static_cast<uint16_t>(0xffff))
|
|
|
|
<< binary_le(static_cast<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion))
|
|
|
|
<< binary_le(CP.Obj.Header.Machine)
|
|
|
|
<< binary_le(CP.Obj.Header.TimeDateStamp);
|
|
|
|
OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
|
|
|
|
OS << zeros(uint32_t(0))
|
|
|
|
<< zeros(uint32_t(0))
|
|
|
|
<< zeros(uint32_t(0))
|
|
|
|
<< zeros(uint32_t(0))
|
|
|
|
<< binary_le(CP.Obj.Header.NumberOfSections)
|
|
|
|
<< binary_le(CP.Obj.Header.PointerToSymbolTable)
|
|
|
|
<< binary_le(CP.Obj.Header.NumberOfSymbols);
|
|
|
|
} else {
|
|
|
|
OS << binary_le(CP.Obj.Header.Machine)
|
|
|
|
<< binary_le(static_cast<int16_t>(CP.Obj.Header.NumberOfSections))
|
|
|
|
<< binary_le(CP.Obj.Header.TimeDateStamp)
|
|
|
|
<< binary_le(CP.Obj.Header.PointerToSymbolTable)
|
|
|
|
<< binary_le(CP.Obj.Header.NumberOfSymbols)
|
|
|
|
<< binary_le(CP.Obj.Header.SizeOfOptionalHeader)
|
|
|
|
<< binary_le(CP.Obj.Header.Characteristics);
|
|
|
|
}
|
2014-11-14 16:15:42 +08:00
|
|
|
if (CP.isPE()) {
|
|
|
|
if (CP.is64Bit()) {
|
|
|
|
object::pe32plus_header PEH;
|
|
|
|
initializeOptionalHeader(CP, COFF::PE32Header::PE32_PLUS, &PEH);
|
|
|
|
OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH));
|
|
|
|
} else {
|
|
|
|
object::pe32_header PEH;
|
2014-11-15 03:35:59 +08:00
|
|
|
uint32_t BaseOfData = initializeOptionalHeader(CP, COFF::PE32Header::PE32, &PEH);
|
|
|
|
PEH.BaseOfData = BaseOfData;
|
2014-11-14 16:15:42 +08:00
|
|
|
OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH));
|
|
|
|
}
|
|
|
|
for (const Optional<COFF::DataDirectory> &DD :
|
|
|
|
CP.Obj.OptionalHeader->DataDirectories) {
|
|
|
|
if (!DD.hasValue()) {
|
|
|
|
OS << zeros(uint32_t(0));
|
|
|
|
OS << zeros(uint32_t(0));
|
|
|
|
} else {
|
|
|
|
OS << binary_le(DD->RelativeVirtualAddress);
|
|
|
|
OS << binary_le(DD->Size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OS << zeros(uint32_t(0));
|
|
|
|
OS << zeros(uint32_t(0));
|
|
|
|
}
|
2013-06-06 03:56:47 +08:00
|
|
|
|
2014-11-15 10:03:59 +08:00
|
|
|
assert(OS.tell() == CP.SectionTableStart);
|
2013-06-06 03:56:47 +08:00
|
|
|
// Output section table.
|
|
|
|
for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
|
|
|
|
e = CP.Obj.Sections.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
OS.write(i->Header.Name, COFF::NameSize);
|
|
|
|
OS << binary_le(i->Header.VirtualSize)
|
|
|
|
<< binary_le(i->Header.VirtualAddress)
|
|
|
|
<< binary_le(i->Header.SizeOfRawData)
|
|
|
|
<< binary_le(i->Header.PointerToRawData)
|
|
|
|
<< binary_le(i->Header.PointerToRelocations)
|
|
|
|
<< binary_le(i->Header.PointerToLineNumbers)
|
|
|
|
<< binary_le(i->Header.NumberOfRelocations)
|
|
|
|
<< binary_le(i->Header.NumberOfLineNumbers)
|
|
|
|
<< binary_le(i->Header.Characteristics);
|
|
|
|
}
|
2014-11-15 10:03:59 +08:00
|
|
|
assert(OS.tell() == CP.SectionTableStart + CP.SectionTableSize);
|
2013-06-06 03:56:47 +08:00
|
|
|
|
2013-06-06 21:06:17 +08:00
|
|
|
unsigned CurSymbol = 0;
|
|
|
|
StringMap<unsigned> SymbolTableIndexMap;
|
|
|
|
for (std::vector<COFFYAML::Symbol>::iterator I = CP.Obj.Symbols.begin(),
|
|
|
|
E = CP.Obj.Symbols.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
SymbolTableIndexMap[I->Name] = CurSymbol;
|
|
|
|
CurSymbol += 1 + I->Header.NumberOfAuxSymbols;
|
|
|
|
}
|
|
|
|
|
2013-06-06 03:56:47 +08:00
|
|
|
// Output section data.
|
2014-11-14 16:15:42 +08:00
|
|
|
for (const COFFYAML::Section &S : CP.Obj.Sections) {
|
|
|
|
if (!S.Header.SizeOfRawData)
|
|
|
|
continue;
|
2014-11-15 10:03:59 +08:00
|
|
|
assert(S.Header.PointerToRawData >= OS.tell());
|
2014-11-14 16:15:42 +08:00
|
|
|
OS << num_zeros(S.Header.PointerToRawData - OS.tell());
|
|
|
|
S.SectionData.writeAsBinary(OS);
|
2014-11-15 10:03:59 +08:00
|
|
|
assert(S.Header.SizeOfRawData >= S.SectionData.binary_size());
|
2014-11-14 16:15:42 +08:00
|
|
|
OS << num_zeros(S.Header.SizeOfRawData - S.SectionData.binary_size());
|
|
|
|
for (const COFFYAML::Relocation &R : S.Relocations) {
|
2013-06-06 21:06:17 +08:00
|
|
|
uint32_t SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
|
2013-06-06 03:56:47 +08:00
|
|
|
OS << binary_le(R.VirtualAddress)
|
2013-06-06 21:06:17 +08:00
|
|
|
<< binary_le(SymbolTableIndex)
|
2013-06-06 03:56:47 +08:00
|
|
|
<< binary_le(R.Type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output symbol table.
|
|
|
|
|
|
|
|
for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
|
|
|
|
e = CP.Obj.Symbols.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
OS.write(i->Header.Name, COFF::NameSize);
|
2014-09-16 11:52:46 +08:00
|
|
|
OS << binary_le(i->Header.Value);
|
|
|
|
if (CP.useBigObj())
|
|
|
|
OS << binary_le(i->Header.SectionNumber);
|
|
|
|
else
|
|
|
|
OS << binary_le(static_cast<int16_t>(i->Header.SectionNumber));
|
|
|
|
OS << binary_le(i->Header.Type)
|
2013-06-06 03:56:47 +08:00
|
|
|
<< binary_le(i->Header.StorageClass)
|
|
|
|
<< binary_le(i->Header.NumberOfAuxSymbols);
|
2014-03-19 12:47:47 +08:00
|
|
|
|
|
|
|
if (i->FunctionDefinition)
|
|
|
|
OS << binary_le(i->FunctionDefinition->TagIndex)
|
|
|
|
<< binary_le(i->FunctionDefinition->TotalSize)
|
|
|
|
<< binary_le(i->FunctionDefinition->PointerToLinenumber)
|
|
|
|
<< binary_le(i->FunctionDefinition->PointerToNextFunction)
|
2014-09-16 11:52:46 +08:00
|
|
|
<< zeros(i->FunctionDefinition->unused)
|
|
|
|
<< num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
|
2014-03-19 12:47:47 +08:00
|
|
|
if (i->bfAndefSymbol)
|
|
|
|
OS << zeros(i->bfAndefSymbol->unused1)
|
|
|
|
<< binary_le(i->bfAndefSymbol->Linenumber)
|
|
|
|
<< zeros(i->bfAndefSymbol->unused2)
|
|
|
|
<< binary_le(i->bfAndefSymbol->PointerToNextFunction)
|
2014-09-16 11:52:46 +08:00
|
|
|
<< zeros(i->bfAndefSymbol->unused3)
|
|
|
|
<< num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
|
2014-03-19 12:47:47 +08:00
|
|
|
if (i->WeakExternal)
|
|
|
|
OS << binary_le(i->WeakExternal->TagIndex)
|
|
|
|
<< binary_le(i->WeakExternal->Characteristics)
|
2014-09-16 11:52:46 +08:00
|
|
|
<< zeros(i->WeakExternal->unused)
|
|
|
|
<< num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
|
2014-03-19 12:47:47 +08:00
|
|
|
if (!i->File.empty()) {
|
2014-09-16 11:52:46 +08:00
|
|
|
unsigned SymbolSize = CP.getSymbolSize();
|
2014-03-19 12:47:47 +08:00
|
|
|
uint32_t NumberOfAuxRecords =
|
2014-09-16 11:52:46 +08:00
|
|
|
(i->File.size() + SymbolSize - 1) / SymbolSize;
|
|
|
|
uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
|
2014-03-19 12:47:47 +08:00
|
|
|
uint32_t NumZeros = NumberOfAuxBytes - i->File.size();
|
|
|
|
OS.write(i->File.data(), i->File.size());
|
2014-09-16 11:52:46 +08:00
|
|
|
OS << num_zeros(NumZeros);
|
2014-03-19 12:47:47 +08:00
|
|
|
}
|
|
|
|
if (i->SectionDefinition)
|
|
|
|
OS << binary_le(i->SectionDefinition->Length)
|
|
|
|
<< binary_le(i->SectionDefinition->NumberOfRelocations)
|
|
|
|
<< binary_le(i->SectionDefinition->NumberOfLinenumbers)
|
|
|
|
<< binary_le(i->SectionDefinition->CheckSum)
|
2014-09-16 03:42:42 +08:00
|
|
|
<< binary_le(static_cast<int16_t>(i->SectionDefinition->Number))
|
2014-03-19 12:47:47 +08:00
|
|
|
<< binary_le(i->SectionDefinition->Selection)
|
2014-09-16 03:42:42 +08:00
|
|
|
<< zeros(i->SectionDefinition->unused)
|
2014-09-16 11:52:46 +08:00
|
|
|
<< binary_le(static_cast<int16_t>(i->SectionDefinition->Number >> 16))
|
|
|
|
<< num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
|
2014-03-19 12:47:47 +08:00
|
|
|
if (i->CLRToken)
|
|
|
|
OS << binary_le(i->CLRToken->AuxType)
|
|
|
|
<< zeros(i->CLRToken->unused1)
|
|
|
|
<< binary_le(i->CLRToken->SymbolTableIndex)
|
2014-09-16 11:52:46 +08:00
|
|
|
<< zeros(i->CLRToken->unused2)
|
|
|
|
<< num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
|
2013-06-06 03:56:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output string table.
|
2014-11-14 16:15:42 +08:00
|
|
|
if (CP.Obj.Header.PointerToSymbolTable)
|
|
|
|
OS.write(&CP.StringTable[0], CP.StringTable.size());
|
2013-06-06 03:56:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-31 12:51:07 +08:00
|
|
|
int yaml2coff(yaml::Input &YIn, raw_ostream &Out) {
|
2013-06-06 03:56:47 +08:00
|
|
|
COFFYAML::Object Doc;
|
|
|
|
YIn >> Doc;
|
|
|
|
if (YIn.error()) {
|
|
|
|
errs() << "yaml2obj: Failed to parse YAML file!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
COFFParser CP(Doc);
|
|
|
|
if (!CP.parse()) {
|
|
|
|
errs() << "yaml2obj: Failed to parse YAML file!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-11-14 16:15:42 +08:00
|
|
|
if (!layoutOptionalHeader(CP)) {
|
|
|
|
errs() << "yaml2obj: Failed to layout optional header for COFF file!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-06 03:56:47 +08:00
|
|
|
if (!layoutCOFF(CP)) {
|
|
|
|
errs() << "yaml2obj: Failed to layout COFF file!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!writeCOFF(CP, Out)) {
|
|
|
|
errs() << "yaml2obj: Failed to write COFF file!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|