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 const object::pe32_header
*PE32Header
= nullptr;
42 Obj
.getPE32Header(PE32Header
);
44 dumpOptionalHeader(PE32Header
);
46 const object::pe32plus_header
*PE32PlusHeader
= nullptr;
47 Obj
.getPE32PlusHeader(PE32PlusHeader
);
49 dumpOptionalHeader(PE32PlusHeader
);
53 dumpSections(Obj
.getNumberOfSections());
54 dumpSymbols(Obj
.getNumberOfSymbols());
57 template <typename T
> void COFFDumper::dumpOptionalHeader(T OptionalHeader
) {
58 YAMLObj
.OptionalHeader
= COFFYAML::PEHeader();
59 YAMLObj
.OptionalHeader
->Header
.AddressOfEntryPoint
=
60 OptionalHeader
->AddressOfEntryPoint
;
61 YAMLObj
.OptionalHeader
->Header
.ImageBase
= OptionalHeader
->ImageBase
;
62 YAMLObj
.OptionalHeader
->Header
.SectionAlignment
=
63 OptionalHeader
->SectionAlignment
;
64 YAMLObj
.OptionalHeader
->Header
.FileAlignment
= OptionalHeader
->FileAlignment
;
65 YAMLObj
.OptionalHeader
->Header
.MajorOperatingSystemVersion
=
66 OptionalHeader
->MajorOperatingSystemVersion
;
67 YAMLObj
.OptionalHeader
->Header
.MinorOperatingSystemVersion
=
68 OptionalHeader
->MinorOperatingSystemVersion
;
69 YAMLObj
.OptionalHeader
->Header
.MajorImageVersion
=
70 OptionalHeader
->MajorImageVersion
;
71 YAMLObj
.OptionalHeader
->Header
.MinorImageVersion
=
72 OptionalHeader
->MinorImageVersion
;
73 YAMLObj
.OptionalHeader
->Header
.MajorSubsystemVersion
=
74 OptionalHeader
->MajorSubsystemVersion
;
75 YAMLObj
.OptionalHeader
->Header
.MinorSubsystemVersion
=
76 OptionalHeader
->MinorSubsystemVersion
;
77 YAMLObj
.OptionalHeader
->Header
.Subsystem
= OptionalHeader
->Subsystem
;
78 YAMLObj
.OptionalHeader
->Header
.DLLCharacteristics
=
79 OptionalHeader
->DLLCharacteristics
;
80 YAMLObj
.OptionalHeader
->Header
.SizeOfStackReserve
=
81 OptionalHeader
->SizeOfStackReserve
;
82 YAMLObj
.OptionalHeader
->Header
.SizeOfStackCommit
=
83 OptionalHeader
->SizeOfStackCommit
;
84 YAMLObj
.OptionalHeader
->Header
.SizeOfHeapReserve
=
85 OptionalHeader
->SizeOfHeapReserve
;
86 YAMLObj
.OptionalHeader
->Header
.SizeOfHeapCommit
=
87 OptionalHeader
->SizeOfHeapCommit
;
89 for (auto &DestDD
: YAMLObj
.OptionalHeader
->DataDirectories
) {
90 const object::data_directory
*DD
;
91 if (Obj
.getDataDirectory(I
++, DD
))
93 DestDD
= COFF::DataDirectory();
94 DestDD
->RelativeVirtualAddress
= DD
->RelativeVirtualAddress
;
95 DestDD
->Size
= DD
->Size
;
99 void COFFDumper::dumpHeader() {
100 YAMLObj
.Header
.Machine
= Obj
.getMachine();
101 YAMLObj
.Header
.Characteristics
= Obj
.getCharacteristics();
105 initializeFileAndStringTable(const llvm::object::COFFObjectFile
&Obj
,
106 codeview::StringsAndChecksumsRef
&SC
) {
108 ExitOnError
Err("Invalid .debug$S section!");
109 // Iterate all .debug$S sections looking for the checksums and string table.
110 // Exit as soon as both sections are found.
111 for (const auto &S
: Obj
.sections()) {
112 if (SC
.hasStrings() && SC
.hasChecksums())
115 StringRef SectionName
;
116 S
.getName(SectionName
);
117 ArrayRef
<uint8_t> sectionData
;
118 if (SectionName
!= ".debug$S")
121 const object::coff_section
*COFFSection
= Obj
.getCOFFSection(S
);
123 cantFail(Obj
.getSectionContents(COFFSection
, sectionData
));
125 BinaryStreamReader
Reader(sectionData
, support::little
);
128 Err(Reader
.readInteger(Magic
));
129 assert(Magic
== COFF::DEBUG_SECTION_MAGIC
&& "Invalid .debug$S section!");
131 codeview::DebugSubsectionArray Subsections
;
132 Err(Reader
.readArray(Subsections
, Reader
.bytesRemaining()));
134 SC
.initialize(Subsections
);
138 void COFFDumper::dumpSections(unsigned NumSections
) {
139 std::vector
<COFFYAML::Section
> &YAMLSections
= YAMLObj
.Sections
;
140 codeview::StringsAndChecksumsRef SC
;
141 initializeFileAndStringTable(Obj
, SC
);
143 StringMap
<bool> SymbolUnique
;
144 for (const auto &S
: Obj
.symbols()) {
145 object::COFFSymbolRef Symbol
= Obj
.getCOFFSymbol(S
);
147 Obj
.getSymbolName(Symbol
, Name
);
148 StringMap
<bool>::iterator It
;
150 std::tie(It
, Inserted
) = SymbolUnique
.insert(std::make_pair(Name
, true));
155 for (const auto &ObjSection
: Obj
.sections()) {
156 const object::coff_section
*COFFSection
= Obj
.getCOFFSection(ObjSection
);
157 COFFYAML::Section NewYAMLSection
;
158 ObjSection
.getName(NewYAMLSection
.Name
);
159 NewYAMLSection
.Header
.Characteristics
= COFFSection
->Characteristics
;
160 NewYAMLSection
.Header
.VirtualAddress
= COFFSection
->VirtualAddress
;
161 NewYAMLSection
.Header
.VirtualSize
= COFFSection
->VirtualSize
;
162 NewYAMLSection
.Header
.NumberOfLineNumbers
=
163 COFFSection
->NumberOfLinenumbers
;
164 NewYAMLSection
.Header
.NumberOfRelocations
=
165 COFFSection
->NumberOfRelocations
;
166 NewYAMLSection
.Header
.PointerToLineNumbers
=
167 COFFSection
->PointerToLinenumbers
;
168 NewYAMLSection
.Header
.PointerToRawData
= COFFSection
->PointerToRawData
;
169 NewYAMLSection
.Header
.PointerToRelocations
=
170 COFFSection
->PointerToRelocations
;
171 NewYAMLSection
.Header
.SizeOfRawData
= COFFSection
->SizeOfRawData
;
172 uint32_t Shift
= (COFFSection
->Characteristics
>> 20) & 0xF;
173 NewYAMLSection
.Alignment
= (1U << Shift
) >> 1;
174 assert(NewYAMLSection
.Alignment
<= 8192);
176 ArrayRef
<uint8_t> sectionData
;
177 if (!ObjSection
.isBSS())
178 cantFail(Obj
.getSectionContents(COFFSection
, sectionData
));
179 NewYAMLSection
.SectionData
= yaml::BinaryRef(sectionData
);
181 if (NewYAMLSection
.Name
== ".debug$S")
182 NewYAMLSection
.DebugS
= CodeViewYAML::fromDebugS(sectionData
, SC
);
183 else if (NewYAMLSection
.Name
== ".debug$T")
184 NewYAMLSection
.DebugT
= CodeViewYAML::fromDebugT(sectionData
,
185 NewYAMLSection
.Name
);
186 else if (NewYAMLSection
.Name
== ".debug$P")
187 NewYAMLSection
.DebugP
= CodeViewYAML::fromDebugT(sectionData
,
188 NewYAMLSection
.Name
);
189 else if (NewYAMLSection
.Name
== ".debug$H")
190 NewYAMLSection
.DebugH
= CodeViewYAML::fromDebugH(sectionData
);
192 std::vector
<COFFYAML::Relocation
> Relocations
;
193 for (const auto &Reloc
: ObjSection
.relocations()) {
194 const object::coff_relocation
*reloc
= Obj
.getCOFFRelocation(Reloc
);
195 COFFYAML::Relocation Rel
;
196 object::symbol_iterator Sym
= Reloc
.getSymbol();
197 Expected
<StringRef
> SymbolNameOrErr
= Sym
->getName();
198 if (!SymbolNameOrErr
) {
200 raw_string_ostream
OS(Buf
);
201 logAllUnhandledErrors(SymbolNameOrErr
.takeError(), OS
);
203 report_fatal_error(Buf
);
205 if (SymbolUnique
.lookup(*SymbolNameOrErr
))
206 Rel
.SymbolName
= *SymbolNameOrErr
;
208 Rel
.SymbolTableIndex
= reloc
->SymbolTableIndex
;
209 Rel
.VirtualAddress
= reloc
->VirtualAddress
;
210 Rel
.Type
= reloc
->Type
;
211 Relocations
.push_back(Rel
);
213 NewYAMLSection
.Relocations
= Relocations
;
214 YAMLSections
.push_back(NewYAMLSection
);
219 dumpFunctionDefinition(COFFYAML::Symbol
*Sym
,
220 const object::coff_aux_function_definition
*ObjFD
) {
221 COFF::AuxiliaryFunctionDefinition YAMLFD
;
222 YAMLFD
.TagIndex
= ObjFD
->TagIndex
;
223 YAMLFD
.TotalSize
= ObjFD
->TotalSize
;
224 YAMLFD
.PointerToLinenumber
= ObjFD
->PointerToLinenumber
;
225 YAMLFD
.PointerToNextFunction
= ObjFD
->PointerToNextFunction
;
227 Sym
->FunctionDefinition
= YAMLFD
;
231 dumpbfAndEfLineInfo(COFFYAML::Symbol
*Sym
,
232 const object::coff_aux_bf_and_ef_symbol
*ObjBES
) {
233 COFF::AuxiliarybfAndefSymbol YAMLAAS
;
234 YAMLAAS
.Linenumber
= ObjBES
->Linenumber
;
235 YAMLAAS
.PointerToNextFunction
= ObjBES
->PointerToNextFunction
;
237 Sym
->bfAndefSymbol
= YAMLAAS
;
240 static void dumpWeakExternal(COFFYAML::Symbol
*Sym
,
241 const object::coff_aux_weak_external
*ObjWE
) {
242 COFF::AuxiliaryWeakExternal YAMLWE
;
243 YAMLWE
.TagIndex
= ObjWE
->TagIndex
;
244 YAMLWE
.Characteristics
= ObjWE
->Characteristics
;
246 Sym
->WeakExternal
= YAMLWE
;
250 dumpSectionDefinition(COFFYAML::Symbol
*Sym
,
251 const object::coff_aux_section_definition
*ObjSD
,
253 COFF::AuxiliarySectionDefinition YAMLASD
;
254 int32_t AuxNumber
= ObjSD
->getNumber(IsBigObj
);
255 YAMLASD
.Length
= ObjSD
->Length
;
256 YAMLASD
.NumberOfRelocations
= ObjSD
->NumberOfRelocations
;
257 YAMLASD
.NumberOfLinenumbers
= ObjSD
->NumberOfLinenumbers
;
258 YAMLASD
.CheckSum
= ObjSD
->CheckSum
;
259 YAMLASD
.Number
= AuxNumber
;
260 YAMLASD
.Selection
= ObjSD
->Selection
;
262 Sym
->SectionDefinition
= YAMLASD
;
266 dumpCLRTokenDefinition(COFFYAML::Symbol
*Sym
,
267 const object::coff_aux_clr_token
*ObjCLRToken
) {
268 COFF::AuxiliaryCLRToken YAMLCLRToken
;
269 YAMLCLRToken
.AuxType
= ObjCLRToken
->AuxType
;
270 YAMLCLRToken
.SymbolTableIndex
= ObjCLRToken
->SymbolTableIndex
;
272 Sym
->CLRToken
= YAMLCLRToken
;
275 void COFFDumper::dumpSymbols(unsigned NumSymbols
) {
276 std::vector
<COFFYAML::Symbol
> &Symbols
= YAMLObj
.Symbols
;
277 for (const auto &S
: Obj
.symbols()) {
278 object::COFFSymbolRef Symbol
= Obj
.getCOFFSymbol(S
);
279 COFFYAML::Symbol Sym
;
280 Obj
.getSymbolName(Symbol
, Sym
.Name
);
281 Sym
.SimpleType
= COFF::SymbolBaseType(Symbol
.getBaseType());
282 Sym
.ComplexType
= COFF::SymbolComplexType(Symbol
.getComplexType());
283 Sym
.Header
.StorageClass
= Symbol
.getStorageClass();
284 Sym
.Header
.Value
= Symbol
.getValue();
285 Sym
.Header
.SectionNumber
= Symbol
.getSectionNumber();
286 Sym
.Header
.NumberOfAuxSymbols
= Symbol
.getNumberOfAuxSymbols();
288 if (Symbol
.getNumberOfAuxSymbols() > 0) {
289 ArrayRef
<uint8_t> AuxData
= Obj
.getSymbolAuxData(Symbol
);
290 if (Symbol
.isFunctionDefinition()) {
291 // This symbol represents a function definition.
292 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
293 "Expected a single aux symbol to describe this function!");
295 const object::coff_aux_function_definition
*ObjFD
=
296 reinterpret_cast<const object::coff_aux_function_definition
*>(
298 dumpFunctionDefinition(&Sym
, ObjFD
);
299 } else if (Symbol
.isFunctionLineInfo()) {
300 // This symbol describes function line number information.
301 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
302 "Expected a single aux symbol to describe this function!");
304 const object::coff_aux_bf_and_ef_symbol
*ObjBES
=
305 reinterpret_cast<const object::coff_aux_bf_and_ef_symbol
*>(
307 dumpbfAndEfLineInfo(&Sym
, ObjBES
);
308 } else if (Symbol
.isAnyUndefined()) {
309 // This symbol represents a weak external definition.
310 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
311 "Expected a single aux symbol to describe this weak symbol!");
313 const object::coff_aux_weak_external
*ObjWE
=
314 reinterpret_cast<const object::coff_aux_weak_external
*>(
316 dumpWeakExternal(&Sym
, ObjWE
);
317 } else if (Symbol
.isFileRecord()) {
318 // This symbol represents a file record.
319 Sym
.File
= StringRef(reinterpret_cast<const char *>(AuxData
.data()),
320 Symbol
.getNumberOfAuxSymbols() *
321 Obj
.getSymbolTableEntrySize())
322 .rtrim(StringRef("\0", /*length=*/1));
323 } else if (Symbol
.isSectionDefinition()) {
324 // This symbol represents a section definition.
325 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
326 "Expected a single aux symbol to describe this section!");
328 const object::coff_aux_section_definition
*ObjSD
=
329 reinterpret_cast<const object::coff_aux_section_definition
*>(
331 dumpSectionDefinition(&Sym
, ObjSD
, Symbol
.isBigObj());
332 } else if (Symbol
.isCLRToken()) {
333 // This symbol represents a CLR token definition.
334 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
335 "Expected a single aux symbol to describe this CLR Token!");
337 const object::coff_aux_clr_token
*ObjCLRToken
=
338 reinterpret_cast<const object::coff_aux_clr_token
*>(
340 dumpCLRTokenDefinition(&Sym
, ObjCLRToken
);
342 llvm_unreachable("Unhandled auxiliary symbol!");
345 Symbols
.push_back(Sym
);
349 COFFYAML::Object
&COFFDumper::getYAMLObj() {
353 std::error_code
coff2yaml(raw_ostream
&Out
, const object::COFFObjectFile
&Obj
) {
354 COFFDumper
Dumper(Obj
);
356 yaml::Output
Yout(Out
);
357 Yout
<< Dumper
.getYAMLObj();
359 return std::error_code();