1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/StringMap.h"
11 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
12 #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
13 #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
14 #include "llvm/Object/COFF.h"
15 #include "llvm/ObjectYAML/COFFYAML.h"
16 #include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/YAMLTraits.h"
25 const object::COFFObjectFile
&Obj
;
26 COFFYAML::Object YAMLObj
;
28 void dumpOptionalHeader(T OptionalHeader
);
30 void dumpSections(unsigned numSections
);
31 void dumpSymbols(unsigned numSymbols
);
34 COFFDumper(const object::COFFObjectFile
&Obj
);
35 COFFYAML::Object
&getYAMLObj();
40 COFFDumper::COFFDumper(const object::COFFObjectFile
&Obj
) : Obj(Obj
) {
41 if (const object::pe32_header
*PE32Header
= Obj
.getPE32Header())
42 dumpOptionalHeader(PE32Header
);
43 else if (const object::pe32plus_header
*PE32PlusHeader
=
44 Obj
.getPE32PlusHeader())
45 dumpOptionalHeader(PE32PlusHeader
);
48 dumpSections(Obj
.getNumberOfSections());
49 dumpSymbols(Obj
.getNumberOfSymbols());
52 template <typename T
> void COFFDumper::dumpOptionalHeader(T OptionalHeader
) {
53 YAMLObj
.OptionalHeader
= COFFYAML::PEHeader();
54 YAMLObj
.OptionalHeader
->Header
.AddressOfEntryPoint
=
55 OptionalHeader
->AddressOfEntryPoint
;
56 YAMLObj
.OptionalHeader
->Header
.ImageBase
= OptionalHeader
->ImageBase
;
57 YAMLObj
.OptionalHeader
->Header
.SectionAlignment
=
58 OptionalHeader
->SectionAlignment
;
59 YAMLObj
.OptionalHeader
->Header
.FileAlignment
= OptionalHeader
->FileAlignment
;
60 YAMLObj
.OptionalHeader
->Header
.MajorOperatingSystemVersion
=
61 OptionalHeader
->MajorOperatingSystemVersion
;
62 YAMLObj
.OptionalHeader
->Header
.MinorOperatingSystemVersion
=
63 OptionalHeader
->MinorOperatingSystemVersion
;
64 YAMLObj
.OptionalHeader
->Header
.MajorImageVersion
=
65 OptionalHeader
->MajorImageVersion
;
66 YAMLObj
.OptionalHeader
->Header
.MinorImageVersion
=
67 OptionalHeader
->MinorImageVersion
;
68 YAMLObj
.OptionalHeader
->Header
.MajorSubsystemVersion
=
69 OptionalHeader
->MajorSubsystemVersion
;
70 YAMLObj
.OptionalHeader
->Header
.MinorSubsystemVersion
=
71 OptionalHeader
->MinorSubsystemVersion
;
72 YAMLObj
.OptionalHeader
->Header
.Subsystem
= OptionalHeader
->Subsystem
;
73 YAMLObj
.OptionalHeader
->Header
.DLLCharacteristics
=
74 OptionalHeader
->DLLCharacteristics
;
75 YAMLObj
.OptionalHeader
->Header
.SizeOfStackReserve
=
76 OptionalHeader
->SizeOfStackReserve
;
77 YAMLObj
.OptionalHeader
->Header
.SizeOfStackCommit
=
78 OptionalHeader
->SizeOfStackCommit
;
79 YAMLObj
.OptionalHeader
->Header
.SizeOfHeapReserve
=
80 OptionalHeader
->SizeOfHeapReserve
;
81 YAMLObj
.OptionalHeader
->Header
.SizeOfHeapCommit
=
82 OptionalHeader
->SizeOfHeapCommit
;
84 for (auto &DestDD
: YAMLObj
.OptionalHeader
->DataDirectories
) {
85 const object::data_directory
*DD
= Obj
.getDataDirectory(I
++);
88 DestDD
= COFF::DataDirectory();
89 DestDD
->RelativeVirtualAddress
= DD
->RelativeVirtualAddress
;
90 DestDD
->Size
= DD
->Size
;
94 void COFFDumper::dumpHeader() {
95 YAMLObj
.Header
.Machine
= Obj
.getMachine();
96 YAMLObj
.Header
.Characteristics
= Obj
.getCharacteristics();
100 initializeFileAndStringTable(const llvm::object::COFFObjectFile
&Obj
,
101 codeview::StringsAndChecksumsRef
&SC
) {
103 ExitOnError
Err("invalid .debug$S section");
104 // Iterate all .debug$S sections looking for the checksums and string table.
105 // Exit as soon as both sections are found.
106 for (const auto &S
: Obj
.sections()) {
107 if (SC
.hasStrings() && SC
.hasChecksums())
110 Expected
<StringRef
> SectionNameOrErr
= S
.getName();
111 if (!SectionNameOrErr
) {
112 consumeError(SectionNameOrErr
.takeError());
116 ArrayRef
<uint8_t> sectionData
;
117 if ((*SectionNameOrErr
) != ".debug$S")
120 const object::coff_section
*COFFSection
= Obj
.getCOFFSection(S
);
122 cantFail(Obj
.getSectionContents(COFFSection
, sectionData
));
124 BinaryStreamReader
Reader(sectionData
, support::little
);
127 Err(Reader
.readInteger(Magic
));
128 assert(Magic
== COFF::DEBUG_SECTION_MAGIC
&& "Invalid .debug$S section!");
130 codeview::DebugSubsectionArray Subsections
;
131 Err(Reader
.readArray(Subsections
, Reader
.bytesRemaining()));
133 SC
.initialize(Subsections
);
137 void COFFDumper::dumpSections(unsigned NumSections
) {
138 std::vector
<COFFYAML::Section
> &YAMLSections
= YAMLObj
.Sections
;
139 codeview::StringsAndChecksumsRef SC
;
140 initializeFileAndStringTable(Obj
, SC
);
142 ExitOnError
Err("invalid section table");
143 StringMap
<bool> SymbolUnique
;
144 for (const auto &S
: Obj
.symbols()) {
145 StringRef Name
= Err(Obj
.getSymbolName(Obj
.getCOFFSymbol(S
)));
146 StringMap
<bool>::iterator It
;
148 std::tie(It
, Inserted
) = SymbolUnique
.insert(std::make_pair(Name
, true));
153 for (const auto &ObjSection
: Obj
.sections()) {
154 const object::coff_section
*COFFSection
= Obj
.getCOFFSection(ObjSection
);
155 COFFYAML::Section NewYAMLSection
;
157 if (Expected
<StringRef
> NameOrErr
= ObjSection
.getName())
158 NewYAMLSection
.Name
= *NameOrErr
;
160 consumeError(NameOrErr
.takeError());
162 NewYAMLSection
.Header
.Characteristics
= COFFSection
->Characteristics
;
163 NewYAMLSection
.Header
.VirtualAddress
= COFFSection
->VirtualAddress
;
164 NewYAMLSection
.Header
.VirtualSize
= COFFSection
->VirtualSize
;
165 NewYAMLSection
.Header
.NumberOfLineNumbers
=
166 COFFSection
->NumberOfLinenumbers
;
167 NewYAMLSection
.Header
.NumberOfRelocations
=
168 COFFSection
->NumberOfRelocations
;
169 NewYAMLSection
.Header
.PointerToLineNumbers
=
170 COFFSection
->PointerToLinenumbers
;
171 NewYAMLSection
.Header
.PointerToRawData
= COFFSection
->PointerToRawData
;
172 NewYAMLSection
.Header
.PointerToRelocations
=
173 COFFSection
->PointerToRelocations
;
174 NewYAMLSection
.Header
.SizeOfRawData
= COFFSection
->SizeOfRawData
;
175 uint32_t Shift
= (COFFSection
->Characteristics
>> 20) & 0xF;
176 NewYAMLSection
.Alignment
= (1U << Shift
) >> 1;
177 assert(NewYAMLSection
.Alignment
<= 8192);
179 ArrayRef
<uint8_t> sectionData
;
180 if (!ObjSection
.isBSS())
181 cantFail(Obj
.getSectionContents(COFFSection
, sectionData
));
182 NewYAMLSection
.SectionData
= yaml::BinaryRef(sectionData
);
184 if (NewYAMLSection
.Name
== ".debug$S")
185 NewYAMLSection
.DebugS
= CodeViewYAML::fromDebugS(sectionData
, SC
);
186 else if (NewYAMLSection
.Name
== ".debug$T")
187 NewYAMLSection
.DebugT
= CodeViewYAML::fromDebugT(sectionData
,
188 NewYAMLSection
.Name
);
189 else if (NewYAMLSection
.Name
== ".debug$P")
190 NewYAMLSection
.DebugP
= CodeViewYAML::fromDebugT(sectionData
,
191 NewYAMLSection
.Name
);
192 else if (NewYAMLSection
.Name
== ".debug$H")
193 NewYAMLSection
.DebugH
= CodeViewYAML::fromDebugH(sectionData
);
195 std::vector
<COFFYAML::Relocation
> Relocations
;
196 for (const auto &Reloc
: ObjSection
.relocations()) {
197 const object::coff_relocation
*reloc
= Obj
.getCOFFRelocation(Reloc
);
198 COFFYAML::Relocation Rel
;
199 object::symbol_iterator Sym
= Reloc
.getSymbol();
200 Expected
<StringRef
> SymbolNameOrErr
= Sym
->getName();
201 if (!SymbolNameOrErr
) {
203 raw_string_ostream
OS(Buf
);
204 logAllUnhandledErrors(SymbolNameOrErr
.takeError(), OS
);
206 report_fatal_error(Buf
);
208 if (SymbolUnique
.lookup(*SymbolNameOrErr
))
209 Rel
.SymbolName
= *SymbolNameOrErr
;
211 Rel
.SymbolTableIndex
= reloc
->SymbolTableIndex
;
212 Rel
.VirtualAddress
= reloc
->VirtualAddress
;
213 Rel
.Type
= reloc
->Type
;
214 Relocations
.push_back(Rel
);
216 NewYAMLSection
.Relocations
= Relocations
;
217 YAMLSections
.push_back(NewYAMLSection
);
222 dumpFunctionDefinition(COFFYAML::Symbol
*Sym
,
223 const object::coff_aux_function_definition
*ObjFD
) {
224 COFF::AuxiliaryFunctionDefinition YAMLFD
;
225 YAMLFD
.TagIndex
= ObjFD
->TagIndex
;
226 YAMLFD
.TotalSize
= ObjFD
->TotalSize
;
227 YAMLFD
.PointerToLinenumber
= ObjFD
->PointerToLinenumber
;
228 YAMLFD
.PointerToNextFunction
= ObjFD
->PointerToNextFunction
;
230 Sym
->FunctionDefinition
= YAMLFD
;
234 dumpbfAndEfLineInfo(COFFYAML::Symbol
*Sym
,
235 const object::coff_aux_bf_and_ef_symbol
*ObjBES
) {
236 COFF::AuxiliarybfAndefSymbol YAMLAAS
;
237 YAMLAAS
.Linenumber
= ObjBES
->Linenumber
;
238 YAMLAAS
.PointerToNextFunction
= ObjBES
->PointerToNextFunction
;
240 Sym
->bfAndefSymbol
= YAMLAAS
;
243 static void dumpWeakExternal(COFFYAML::Symbol
*Sym
,
244 const object::coff_aux_weak_external
*ObjWE
) {
245 COFF::AuxiliaryWeakExternal YAMLWE
;
246 YAMLWE
.TagIndex
= ObjWE
->TagIndex
;
247 YAMLWE
.Characteristics
= ObjWE
->Characteristics
;
249 Sym
->WeakExternal
= YAMLWE
;
253 dumpSectionDefinition(COFFYAML::Symbol
*Sym
,
254 const object::coff_aux_section_definition
*ObjSD
,
256 COFF::AuxiliarySectionDefinition YAMLASD
;
257 int32_t AuxNumber
= ObjSD
->getNumber(IsBigObj
);
258 YAMLASD
.Length
= ObjSD
->Length
;
259 YAMLASD
.NumberOfRelocations
= ObjSD
->NumberOfRelocations
;
260 YAMLASD
.NumberOfLinenumbers
= ObjSD
->NumberOfLinenumbers
;
261 YAMLASD
.CheckSum
= ObjSD
->CheckSum
;
262 YAMLASD
.Number
= AuxNumber
;
263 YAMLASD
.Selection
= ObjSD
->Selection
;
265 Sym
->SectionDefinition
= YAMLASD
;
269 dumpCLRTokenDefinition(COFFYAML::Symbol
*Sym
,
270 const object::coff_aux_clr_token
*ObjCLRToken
) {
271 COFF::AuxiliaryCLRToken YAMLCLRToken
;
272 YAMLCLRToken
.AuxType
= ObjCLRToken
->AuxType
;
273 YAMLCLRToken
.SymbolTableIndex
= ObjCLRToken
->SymbolTableIndex
;
275 Sym
->CLRToken
= YAMLCLRToken
;
278 void COFFDumper::dumpSymbols(unsigned NumSymbols
) {
279 ExitOnError
Err("invalid symbol table");
281 std::vector
<COFFYAML::Symbol
> &Symbols
= YAMLObj
.Symbols
;
282 for (const auto &S
: Obj
.symbols()) {
283 object::COFFSymbolRef Symbol
= Obj
.getCOFFSymbol(S
);
284 COFFYAML::Symbol Sym
;
285 Sym
.Name
= Err(Obj
.getSymbolName(Symbol
));
286 Sym
.SimpleType
= COFF::SymbolBaseType(Symbol
.getBaseType());
287 Sym
.ComplexType
= COFF::SymbolComplexType(Symbol
.getComplexType());
288 Sym
.Header
.StorageClass
= Symbol
.getStorageClass();
289 Sym
.Header
.Value
= Symbol
.getValue();
290 Sym
.Header
.SectionNumber
= Symbol
.getSectionNumber();
291 Sym
.Header
.NumberOfAuxSymbols
= Symbol
.getNumberOfAuxSymbols();
293 if (Symbol
.getNumberOfAuxSymbols() > 0) {
294 ArrayRef
<uint8_t> AuxData
= Obj
.getSymbolAuxData(Symbol
);
295 if (Symbol
.isFunctionDefinition()) {
296 // This symbol represents a function definition.
297 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
298 "Expected a single aux symbol to describe this function!");
300 const object::coff_aux_function_definition
*ObjFD
=
301 reinterpret_cast<const object::coff_aux_function_definition
*>(
303 dumpFunctionDefinition(&Sym
, ObjFD
);
304 } else if (Symbol
.isFunctionLineInfo()) {
305 // This symbol describes function line number information.
306 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
307 "Expected a single aux symbol to describe this function!");
309 const object::coff_aux_bf_and_ef_symbol
*ObjBES
=
310 reinterpret_cast<const object::coff_aux_bf_and_ef_symbol
*>(
312 dumpbfAndEfLineInfo(&Sym
, ObjBES
);
313 } else if (Symbol
.isAnyUndefined()) {
314 // This symbol represents a weak external definition.
315 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
316 "Expected a single aux symbol to describe this weak symbol!");
318 const object::coff_aux_weak_external
*ObjWE
=
319 reinterpret_cast<const object::coff_aux_weak_external
*>(
321 dumpWeakExternal(&Sym
, ObjWE
);
322 } else if (Symbol
.isFileRecord()) {
323 // This symbol represents a file record.
324 Sym
.File
= StringRef(reinterpret_cast<const char *>(AuxData
.data()),
325 Symbol
.getNumberOfAuxSymbols() *
326 Obj
.getSymbolTableEntrySize())
327 .rtrim(StringRef("\0", /*length=*/1));
328 } else if (Symbol
.isSectionDefinition()) {
329 // This symbol represents a section definition.
330 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
331 "Expected a single aux symbol to describe this section!");
333 const object::coff_aux_section_definition
*ObjSD
=
334 reinterpret_cast<const object::coff_aux_section_definition
*>(
336 dumpSectionDefinition(&Sym
, ObjSD
, Symbol
.isBigObj());
337 } else if (Symbol
.isCLRToken()) {
338 // This symbol represents a CLR token definition.
339 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
340 "Expected a single aux symbol to describe this CLR Token!");
342 const object::coff_aux_clr_token
*ObjCLRToken
=
343 reinterpret_cast<const object::coff_aux_clr_token
*>(
345 dumpCLRTokenDefinition(&Sym
, ObjCLRToken
);
347 llvm_unreachable("Unhandled auxiliary symbol!");
350 Symbols
.push_back(Sym
);
354 COFFYAML::Object
&COFFDumper::getYAMLObj() {
358 std::error_code
coff2yaml(raw_ostream
&Out
, const object::COFFObjectFile
&Obj
) {
359 COFFDumper
Dumper(Obj
);
361 yaml::Output
Yout(Out
);
362 Yout
<< Dumper
.getYAMLObj();
364 return std::error_code();