1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
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 #include "obj2yaml.h"
11 #include "llvm/Object/COFF.h"
12 #include "llvm/ObjectYAML/COFFYAML.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/YAMLTraits.h"
15
16 using namespace llvm;
17
18 namespace {
19
20 class COFFDumper {
21 const object::COFFObjectFile &Obj;
22 COFFYAML::Object YAMLObj;
23 template <typename T>
24 void dumpOptionalHeader(T OptionalHeader);
25 void dumpHeader();
26 void dumpSections(unsigned numSections);
27 void dumpSymbols(unsigned numSymbols);
28
29 public:
30 COFFDumper(const object::COFFObjectFile &Obj);
31 COFFYAML::Object &getYAMLObj();
32 };
33
34 }
35
COFFDumper(const object::COFFObjectFile & Obj)36 COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
37 const object::pe32_header *PE32Header = nullptr;
38 Obj.getPE32Header(PE32Header);
39 if (PE32Header) {
40 dumpOptionalHeader(PE32Header);
41 } else {
42 const object::pe32plus_header *PE32PlusHeader = nullptr;
43 Obj.getPE32PlusHeader(PE32PlusHeader);
44 if (PE32PlusHeader) {
45 dumpOptionalHeader(PE32PlusHeader);
46 }
47 }
48 dumpHeader();
49 dumpSections(Obj.getNumberOfSections());
50 dumpSymbols(Obj.getNumberOfSymbols());
51 }
52
dumpOptionalHeader(T OptionalHeader)53 template <typename T> void COFFDumper::dumpOptionalHeader(T OptionalHeader) {
54 YAMLObj.OptionalHeader = COFFYAML::PEHeader();
55 YAMLObj.OptionalHeader->Header.AddressOfEntryPoint =
56 OptionalHeader->AddressOfEntryPoint;
57 YAMLObj.OptionalHeader->Header.AddressOfEntryPoint =
58 OptionalHeader->AddressOfEntryPoint;
59 YAMLObj.OptionalHeader->Header.ImageBase = OptionalHeader->ImageBase;
60 YAMLObj.OptionalHeader->Header.SectionAlignment =
61 OptionalHeader->SectionAlignment;
62 YAMLObj.OptionalHeader->Header.FileAlignment = OptionalHeader->FileAlignment;
63 YAMLObj.OptionalHeader->Header.MajorOperatingSystemVersion =
64 OptionalHeader->MajorOperatingSystemVersion;
65 YAMLObj.OptionalHeader->Header.MinorOperatingSystemVersion =
66 OptionalHeader->MinorOperatingSystemVersion;
67 YAMLObj.OptionalHeader->Header.MajorImageVersion =
68 OptionalHeader->MajorImageVersion;
69 YAMLObj.OptionalHeader->Header.MinorImageVersion =
70 OptionalHeader->MinorImageVersion;
71 YAMLObj.OptionalHeader->Header.MajorSubsystemVersion =
72 OptionalHeader->MajorSubsystemVersion;
73 YAMLObj.OptionalHeader->Header.MinorSubsystemVersion =
74 OptionalHeader->MinorSubsystemVersion;
75 YAMLObj.OptionalHeader->Header.Subsystem = OptionalHeader->Subsystem;
76 YAMLObj.OptionalHeader->Header.DLLCharacteristics =
77 OptionalHeader->DLLCharacteristics;
78 YAMLObj.OptionalHeader->Header.SizeOfStackReserve =
79 OptionalHeader->SizeOfStackReserve;
80 YAMLObj.OptionalHeader->Header.SizeOfStackCommit =
81 OptionalHeader->SizeOfStackCommit;
82 YAMLObj.OptionalHeader->Header.SizeOfHeapReserve =
83 OptionalHeader->SizeOfHeapReserve;
84 YAMLObj.OptionalHeader->Header.SizeOfHeapCommit =
85 OptionalHeader->SizeOfHeapCommit;
86 unsigned I = 0;
87 for (auto &DestDD : YAMLObj.OptionalHeader->DataDirectories) {
88 const object::data_directory *DD;
89 if (Obj.getDataDirectory(I++, DD))
90 continue;
91 DestDD = COFF::DataDirectory();
92 DestDD->RelativeVirtualAddress = DD->RelativeVirtualAddress;
93 DestDD->Size = DD->Size;
94 }
95 }
96
dumpHeader()97 void COFFDumper::dumpHeader() {
98 YAMLObj.Header.Machine = Obj.getMachine();
99 YAMLObj.Header.Characteristics = Obj.getCharacteristics();
100 }
101
dumpSections(unsigned NumSections)102 void COFFDumper::dumpSections(unsigned NumSections) {
103 std::vector<COFFYAML::Section> &YAMLSections = YAMLObj.Sections;
104 for (const auto &ObjSection : Obj.sections()) {
105 const object::coff_section *COFFSection = Obj.getCOFFSection(ObjSection);
106 COFFYAML::Section NewYAMLSection;
107 ObjSection.getName(NewYAMLSection.Name);
108 NewYAMLSection.Header.Characteristics = COFFSection->Characteristics;
109 NewYAMLSection.Header.VirtualAddress = ObjSection.getAddress();
110 NewYAMLSection.Header.VirtualSize = COFFSection->VirtualSize;
111 NewYAMLSection.Alignment = ObjSection.getAlignment();
112 assert(NewYAMLSection.Alignment <= 8192);
113
114 ArrayRef<uint8_t> sectionData;
115 if (!ObjSection.isBSS())
116 Obj.getSectionContents(COFFSection, sectionData);
117 NewYAMLSection.SectionData = yaml::BinaryRef(sectionData);
118
119 std::vector<COFFYAML::Relocation> Relocations;
120 for (const auto &Reloc : ObjSection.relocations()) {
121 const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc);
122 COFFYAML::Relocation Rel;
123 object::symbol_iterator Sym = Reloc.getSymbol();
124 Expected<StringRef> SymbolNameOrErr = Sym->getName();
125 if (!SymbolNameOrErr) {
126 std::string Buf;
127 raw_string_ostream OS(Buf);
128 logAllUnhandledErrors(SymbolNameOrErr.takeError(), OS, "");
129 OS.flush();
130 report_fatal_error(Buf);
131 }
132 Rel.SymbolName = *SymbolNameOrErr;
133 Rel.VirtualAddress = reloc->VirtualAddress;
134 Rel.Type = reloc->Type;
135 Relocations.push_back(Rel);
136 }
137 NewYAMLSection.Relocations = Relocations;
138 YAMLSections.push_back(NewYAMLSection);
139 }
140 }
141
142 static void
dumpFunctionDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_function_definition * ObjFD)143 dumpFunctionDefinition(COFFYAML::Symbol *Sym,
144 const object::coff_aux_function_definition *ObjFD) {
145 COFF::AuxiliaryFunctionDefinition YAMLFD;
146 YAMLFD.TagIndex = ObjFD->TagIndex;
147 YAMLFD.TotalSize = ObjFD->TotalSize;
148 YAMLFD.PointerToLinenumber = ObjFD->PointerToLinenumber;
149 YAMLFD.PointerToNextFunction = ObjFD->PointerToNextFunction;
150
151 Sym->FunctionDefinition = YAMLFD;
152 }
153
154 static void
dumpbfAndEfLineInfo(COFFYAML::Symbol * Sym,const object::coff_aux_bf_and_ef_symbol * ObjBES)155 dumpbfAndEfLineInfo(COFFYAML::Symbol *Sym,
156 const object::coff_aux_bf_and_ef_symbol *ObjBES) {
157 COFF::AuxiliarybfAndefSymbol YAMLAAS;
158 YAMLAAS.Linenumber = ObjBES->Linenumber;
159 YAMLAAS.PointerToNextFunction = ObjBES->PointerToNextFunction;
160
161 Sym->bfAndefSymbol = YAMLAAS;
162 }
163
dumpWeakExternal(COFFYAML::Symbol * Sym,const object::coff_aux_weak_external * ObjWE)164 static void dumpWeakExternal(COFFYAML::Symbol *Sym,
165 const object::coff_aux_weak_external *ObjWE) {
166 COFF::AuxiliaryWeakExternal YAMLWE;
167 YAMLWE.TagIndex = ObjWE->TagIndex;
168 YAMLWE.Characteristics = ObjWE->Characteristics;
169
170 Sym->WeakExternal = YAMLWE;
171 }
172
173 static void
dumpSectionDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_section_definition * ObjSD,bool IsBigObj)174 dumpSectionDefinition(COFFYAML::Symbol *Sym,
175 const object::coff_aux_section_definition *ObjSD,
176 bool IsBigObj) {
177 COFF::AuxiliarySectionDefinition YAMLASD;
178 int32_t AuxNumber = ObjSD->getNumber(IsBigObj);
179 YAMLASD.Length = ObjSD->Length;
180 YAMLASD.NumberOfRelocations = ObjSD->NumberOfRelocations;
181 YAMLASD.NumberOfLinenumbers = ObjSD->NumberOfLinenumbers;
182 YAMLASD.CheckSum = ObjSD->CheckSum;
183 YAMLASD.Number = AuxNumber;
184 YAMLASD.Selection = ObjSD->Selection;
185
186 Sym->SectionDefinition = YAMLASD;
187 }
188
189 static void
dumpCLRTokenDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_clr_token * ObjCLRToken)190 dumpCLRTokenDefinition(COFFYAML::Symbol *Sym,
191 const object::coff_aux_clr_token *ObjCLRToken) {
192 COFF::AuxiliaryCLRToken YAMLCLRToken;
193 YAMLCLRToken.AuxType = ObjCLRToken->AuxType;
194 YAMLCLRToken.SymbolTableIndex = ObjCLRToken->SymbolTableIndex;
195
196 Sym->CLRToken = YAMLCLRToken;
197 }
198
dumpSymbols(unsigned NumSymbols)199 void COFFDumper::dumpSymbols(unsigned NumSymbols) {
200 std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
201 for (const auto &S : Obj.symbols()) {
202 object::COFFSymbolRef Symbol = Obj.getCOFFSymbol(S);
203 COFFYAML::Symbol Sym;
204 Obj.getSymbolName(Symbol, Sym.Name);
205 Sym.SimpleType = COFF::SymbolBaseType(Symbol.getBaseType());
206 Sym.ComplexType = COFF::SymbolComplexType(Symbol.getComplexType());
207 Sym.Header.StorageClass = Symbol.getStorageClass();
208 Sym.Header.Value = Symbol.getValue();
209 Sym.Header.SectionNumber = Symbol.getSectionNumber();
210 Sym.Header.NumberOfAuxSymbols = Symbol.getNumberOfAuxSymbols();
211
212 if (Symbol.getNumberOfAuxSymbols() > 0) {
213 ArrayRef<uint8_t> AuxData = Obj.getSymbolAuxData(Symbol);
214 if (Symbol.isFunctionDefinition()) {
215 // This symbol represents a function definition.
216 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
217 "Expected a single aux symbol to describe this function!");
218
219 const object::coff_aux_function_definition *ObjFD =
220 reinterpret_cast<const object::coff_aux_function_definition *>(
221 AuxData.data());
222 dumpFunctionDefinition(&Sym, ObjFD);
223 } else if (Symbol.isFunctionLineInfo()) {
224 // This symbol describes function line number information.
225 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
226 "Expected a single aux symbol to describe this function!");
227
228 const object::coff_aux_bf_and_ef_symbol *ObjBES =
229 reinterpret_cast<const object::coff_aux_bf_and_ef_symbol *>(
230 AuxData.data());
231 dumpbfAndEfLineInfo(&Sym, ObjBES);
232 } else if (Symbol.isAnyUndefined()) {
233 // This symbol represents a weak external definition.
234 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
235 "Expected a single aux symbol to describe this weak symbol!");
236
237 const object::coff_aux_weak_external *ObjWE =
238 reinterpret_cast<const object::coff_aux_weak_external *>(
239 AuxData.data());
240 dumpWeakExternal(&Sym, ObjWE);
241 } else if (Symbol.isFileRecord()) {
242 // This symbol represents a file record.
243 Sym.File = StringRef(reinterpret_cast<const char *>(AuxData.data()),
244 Symbol.getNumberOfAuxSymbols() *
245 Obj.getSymbolTableEntrySize())
246 .rtrim(StringRef("\0", /*length=*/1));
247 } else if (Symbol.isSectionDefinition()) {
248 // This symbol represents a section definition.
249 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
250 "Expected a single aux symbol to describe this section!");
251
252 const object::coff_aux_section_definition *ObjSD =
253 reinterpret_cast<const object::coff_aux_section_definition *>(
254 AuxData.data());
255 dumpSectionDefinition(&Sym, ObjSD, Symbol.isBigObj());
256 } else if (Symbol.isCLRToken()) {
257 // This symbol represents a CLR token definition.
258 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
259 "Expected a single aux symbol to describe this CLR Token!");
260
261 const object::coff_aux_clr_token *ObjCLRToken =
262 reinterpret_cast<const object::coff_aux_clr_token *>(
263 AuxData.data());
264 dumpCLRTokenDefinition(&Sym, ObjCLRToken);
265 } else {
266 llvm_unreachable("Unhandled auxiliary symbol!");
267 }
268 }
269 Symbols.push_back(Sym);
270 }
271 }
272
getYAMLObj()273 COFFYAML::Object &COFFDumper::getYAMLObj() {
274 return YAMLObj;
275 }
276
coff2yaml(raw_ostream & Out,const object::COFFObjectFile & Obj)277 std::error_code coff2yaml(raw_ostream &Out, const object::COFFObjectFile &Obj) {
278 COFFDumper Dumper(Obj);
279
280 yaml::Output Yout(Out);
281 Yout << Dumper.getYAMLObj();
282
283 return std::error_code();
284 }
285