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
;
86 if (Obj
.getDataDirectory(I
++, DD
))
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 StringMap
<bool> SymbolUnique
;
143 for (const auto &S
: Obj
.symbols()) {
144 object::COFFSymbolRef Symbol
= Obj
.getCOFFSymbol(S
);
146 Obj
.getSymbolName(Symbol
, Name
);
147 StringMap
<bool>::iterator It
;
149 std::tie(It
, Inserted
) = SymbolUnique
.insert(std::make_pair(Name
, true));
154 for (const auto &ObjSection
: Obj
.sections()) {
155 const object::coff_section
*COFFSection
= Obj
.getCOFFSection(ObjSection
);
156 COFFYAML::Section NewYAMLSection
;
158 if (Expected
<StringRef
> NameOrErr
= ObjSection
.getName())
159 NewYAMLSection
.Name
= *NameOrErr
;
161 consumeError(NameOrErr
.takeError());
163 NewYAMLSection
.Header
.Characteristics
= COFFSection
->Characteristics
;
164 NewYAMLSection
.Header
.VirtualAddress
= COFFSection
->VirtualAddress
;
165 NewYAMLSection
.Header
.VirtualSize
= COFFSection
->VirtualSize
;
166 NewYAMLSection
.Header
.NumberOfLineNumbers
=
167 COFFSection
->NumberOfLinenumbers
;
168 NewYAMLSection
.Header
.NumberOfRelocations
=
169 COFFSection
->NumberOfRelocations
;
170 NewYAMLSection
.Header
.PointerToLineNumbers
=
171 COFFSection
->PointerToLinenumbers
;
172 NewYAMLSection
.Header
.PointerToRawData
= COFFSection
->PointerToRawData
;
173 NewYAMLSection
.Header
.PointerToRelocations
=
174 COFFSection
->PointerToRelocations
;
175 NewYAMLSection
.Header
.SizeOfRawData
= COFFSection
->SizeOfRawData
;
176 uint32_t Shift
= (COFFSection
->Characteristics
>> 20) & 0xF;
177 NewYAMLSection
.Alignment
= (1U << Shift
) >> 1;
178 assert(NewYAMLSection
.Alignment
<= 8192);
180 ArrayRef
<uint8_t> sectionData
;
181 if (!ObjSection
.isBSS())
182 cantFail(Obj
.getSectionContents(COFFSection
, sectionData
));
183 NewYAMLSection
.SectionData
= yaml::BinaryRef(sectionData
);
185 if (NewYAMLSection
.Name
== ".debug$S")
186 NewYAMLSection
.DebugS
= CodeViewYAML::fromDebugS(sectionData
, SC
);
187 else if (NewYAMLSection
.Name
== ".debug$T")
188 NewYAMLSection
.DebugT
= CodeViewYAML::fromDebugT(sectionData
,
189 NewYAMLSection
.Name
);
190 else if (NewYAMLSection
.Name
== ".debug$P")
191 NewYAMLSection
.DebugP
= CodeViewYAML::fromDebugT(sectionData
,
192 NewYAMLSection
.Name
);
193 else if (NewYAMLSection
.Name
== ".debug$H")
194 NewYAMLSection
.DebugH
= CodeViewYAML::fromDebugH(sectionData
);
196 std::vector
<COFFYAML::Relocation
> Relocations
;
197 for (const auto &Reloc
: ObjSection
.relocations()) {
198 const object::coff_relocation
*reloc
= Obj
.getCOFFRelocation(Reloc
);
199 COFFYAML::Relocation Rel
;
200 object::symbol_iterator Sym
= Reloc
.getSymbol();
201 Expected
<StringRef
> SymbolNameOrErr
= Sym
->getName();
202 if (!SymbolNameOrErr
) {
204 raw_string_ostream
OS(Buf
);
205 logAllUnhandledErrors(SymbolNameOrErr
.takeError(), OS
);
207 report_fatal_error(Buf
);
209 if (SymbolUnique
.lookup(*SymbolNameOrErr
))
210 Rel
.SymbolName
= *SymbolNameOrErr
;
212 Rel
.SymbolTableIndex
= reloc
->SymbolTableIndex
;
213 Rel
.VirtualAddress
= reloc
->VirtualAddress
;
214 Rel
.Type
= reloc
->Type
;
215 Relocations
.push_back(Rel
);
217 NewYAMLSection
.Relocations
= Relocations
;
218 YAMLSections
.push_back(NewYAMLSection
);
223 dumpFunctionDefinition(COFFYAML::Symbol
*Sym
,
224 const object::coff_aux_function_definition
*ObjFD
) {
225 COFF::AuxiliaryFunctionDefinition YAMLFD
;
226 YAMLFD
.TagIndex
= ObjFD
->TagIndex
;
227 YAMLFD
.TotalSize
= ObjFD
->TotalSize
;
228 YAMLFD
.PointerToLinenumber
= ObjFD
->PointerToLinenumber
;
229 YAMLFD
.PointerToNextFunction
= ObjFD
->PointerToNextFunction
;
231 Sym
->FunctionDefinition
= YAMLFD
;
235 dumpbfAndEfLineInfo(COFFYAML::Symbol
*Sym
,
236 const object::coff_aux_bf_and_ef_symbol
*ObjBES
) {
237 COFF::AuxiliarybfAndefSymbol YAMLAAS
;
238 YAMLAAS
.Linenumber
= ObjBES
->Linenumber
;
239 YAMLAAS
.PointerToNextFunction
= ObjBES
->PointerToNextFunction
;
241 Sym
->bfAndefSymbol
= YAMLAAS
;
244 static void dumpWeakExternal(COFFYAML::Symbol
*Sym
,
245 const object::coff_aux_weak_external
*ObjWE
) {
246 COFF::AuxiliaryWeakExternal YAMLWE
;
247 YAMLWE
.TagIndex
= ObjWE
->TagIndex
;
248 YAMLWE
.Characteristics
= ObjWE
->Characteristics
;
250 Sym
->WeakExternal
= YAMLWE
;
254 dumpSectionDefinition(COFFYAML::Symbol
*Sym
,
255 const object::coff_aux_section_definition
*ObjSD
,
257 COFF::AuxiliarySectionDefinition YAMLASD
;
258 int32_t AuxNumber
= ObjSD
->getNumber(IsBigObj
);
259 YAMLASD
.Length
= ObjSD
->Length
;
260 YAMLASD
.NumberOfRelocations
= ObjSD
->NumberOfRelocations
;
261 YAMLASD
.NumberOfLinenumbers
= ObjSD
->NumberOfLinenumbers
;
262 YAMLASD
.CheckSum
= ObjSD
->CheckSum
;
263 YAMLASD
.Number
= AuxNumber
;
264 YAMLASD
.Selection
= ObjSD
->Selection
;
266 Sym
->SectionDefinition
= YAMLASD
;
270 dumpCLRTokenDefinition(COFFYAML::Symbol
*Sym
,
271 const object::coff_aux_clr_token
*ObjCLRToken
) {
272 COFF::AuxiliaryCLRToken YAMLCLRToken
;
273 YAMLCLRToken
.AuxType
= ObjCLRToken
->AuxType
;
274 YAMLCLRToken
.SymbolTableIndex
= ObjCLRToken
->SymbolTableIndex
;
276 Sym
->CLRToken
= YAMLCLRToken
;
279 void COFFDumper::dumpSymbols(unsigned NumSymbols
) {
280 std::vector
<COFFYAML::Symbol
> &Symbols
= YAMLObj
.Symbols
;
281 for (const auto &S
: Obj
.symbols()) {
282 object::COFFSymbolRef Symbol
= Obj
.getCOFFSymbol(S
);
283 COFFYAML::Symbol Sym
;
284 Obj
.getSymbolName(Symbol
, Sym
.Name
);
285 Sym
.SimpleType
= COFF::SymbolBaseType(Symbol
.getBaseType());
286 Sym
.ComplexType
= COFF::SymbolComplexType(Symbol
.getComplexType());
287 Sym
.Header
.StorageClass
= Symbol
.getStorageClass();
288 Sym
.Header
.Value
= Symbol
.getValue();
289 Sym
.Header
.SectionNumber
= Symbol
.getSectionNumber();
290 Sym
.Header
.NumberOfAuxSymbols
= Symbol
.getNumberOfAuxSymbols();
292 if (Symbol
.getNumberOfAuxSymbols() > 0) {
293 ArrayRef
<uint8_t> AuxData
= Obj
.getSymbolAuxData(Symbol
);
294 if (Symbol
.isFunctionDefinition()) {
295 // This symbol represents a function definition.
296 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
297 "Expected a single aux symbol to describe this function!");
299 const object::coff_aux_function_definition
*ObjFD
=
300 reinterpret_cast<const object::coff_aux_function_definition
*>(
302 dumpFunctionDefinition(&Sym
, ObjFD
);
303 } else if (Symbol
.isFunctionLineInfo()) {
304 // This symbol describes function line number information.
305 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
306 "Expected a single aux symbol to describe this function!");
308 const object::coff_aux_bf_and_ef_symbol
*ObjBES
=
309 reinterpret_cast<const object::coff_aux_bf_and_ef_symbol
*>(
311 dumpbfAndEfLineInfo(&Sym
, ObjBES
);
312 } else if (Symbol
.isAnyUndefined()) {
313 // This symbol represents a weak external definition.
314 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
315 "Expected a single aux symbol to describe this weak symbol!");
317 const object::coff_aux_weak_external
*ObjWE
=
318 reinterpret_cast<const object::coff_aux_weak_external
*>(
320 dumpWeakExternal(&Sym
, ObjWE
);
321 } else if (Symbol
.isFileRecord()) {
322 // This symbol represents a file record.
323 Sym
.File
= StringRef(reinterpret_cast<const char *>(AuxData
.data()),
324 Symbol
.getNumberOfAuxSymbols() *
325 Obj
.getSymbolTableEntrySize())
326 .rtrim(StringRef("\0", /*length=*/1));
327 } else if (Symbol
.isSectionDefinition()) {
328 // This symbol represents a section definition.
329 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
330 "Expected a single aux symbol to describe this section!");
332 const object::coff_aux_section_definition
*ObjSD
=
333 reinterpret_cast<const object::coff_aux_section_definition
*>(
335 dumpSectionDefinition(&Sym
, ObjSD
, Symbol
.isBigObj());
336 } else if (Symbol
.isCLRToken()) {
337 // This symbol represents a CLR token definition.
338 assert(Symbol
.getNumberOfAuxSymbols() == 1 &&
339 "Expected a single aux symbol to describe this CLR Token!");
341 const object::coff_aux_clr_token
*ObjCLRToken
=
342 reinterpret_cast<const object::coff_aux_clr_token
*>(
344 dumpCLRTokenDefinition(&Sym
, ObjCLRToken
);
346 llvm_unreachable("Unhandled auxiliary symbol!");
349 Symbols
.push_back(Sym
);
353 COFFYAML::Object
&COFFDumper::getYAMLObj() {
357 std::error_code
coff2yaml(raw_ostream
&Out
, const object::COFFObjectFile
&Obj
) {
358 COFFDumper
Dumper(Obj
);
360 yaml::Output
Yout(Out
);
361 Yout
<< Dumper
.getYAMLObj();
363 return std::error_code();