Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 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" |
Martin Storsjo | 6ccdeef | 2019-01-07 20:55:33 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringMap.h" |
Zachary Turner | 63d2fab | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h" |
| 13 | #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h" |
| 14 | #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h" |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 15 | #include "llvm/Object/COFF.h" |
Rafael Espindola | 18903ff | 2016-03-01 19:15:06 +0000 | [diff] [blame] | 16 | #include "llvm/ObjectYAML/COFFYAML.h" |
Zachary Turner | 63d2fab | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 17 | #include "llvm/ObjectYAML/CodeViewYAMLTypes.h" |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | #include "llvm/Support/YAMLTraits.h" |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 20 | |
Rafael Espindola | da177ce | 2013-04-20 02:55:00 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 22 | |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | class COFFDumper { |
| 26 | const object::COFFObjectFile &Obj; |
| 27 | COFFYAML::Object YAMLObj; |
David Majnemer | 237544b | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 28 | template <typename T> |
| 29 | void dumpOptionalHeader(T OptionalHeader); |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 30 | void dumpHeader(); |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 31 | void dumpSections(unsigned numSections); |
| 32 | void dumpSymbols(unsigned numSymbols); |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 33 | |
| 34 | public: |
| 35 | COFFDumper(const object::COFFObjectFile &Obj); |
| 36 | COFFYAML::Object &getYAMLObj(); |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 41 | COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) { |
David Majnemer | 237544b | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 42 | const object::pe32_header *PE32Header = nullptr; |
| 43 | Obj.getPE32Header(PE32Header); |
| 44 | if (PE32Header) { |
| 45 | dumpOptionalHeader(PE32Header); |
| 46 | } else { |
| 47 | const object::pe32plus_header *PE32PlusHeader = nullptr; |
| 48 | Obj.getPE32PlusHeader(PE32PlusHeader); |
| 49 | if (PE32PlusHeader) { |
| 50 | dumpOptionalHeader(PE32PlusHeader); |
| 51 | } |
| 52 | } |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 53 | dumpHeader(); |
| 54 | dumpSections(Obj.getNumberOfSections()); |
| 55 | dumpSymbols(Obj.getNumberOfSymbols()); |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 56 | } |
| 57 | |
David Majnemer | 237544b | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 58 | template <typename T> void COFFDumper::dumpOptionalHeader(T OptionalHeader) { |
| 59 | YAMLObj.OptionalHeader = COFFYAML::PEHeader(); |
| 60 | YAMLObj.OptionalHeader->Header.AddressOfEntryPoint = |
| 61 | OptionalHeader->AddressOfEntryPoint; |
| 62 | YAMLObj.OptionalHeader->Header.AddressOfEntryPoint = |
| 63 | OptionalHeader->AddressOfEntryPoint; |
| 64 | YAMLObj.OptionalHeader->Header.ImageBase = OptionalHeader->ImageBase; |
| 65 | YAMLObj.OptionalHeader->Header.SectionAlignment = |
| 66 | OptionalHeader->SectionAlignment; |
| 67 | YAMLObj.OptionalHeader->Header.FileAlignment = OptionalHeader->FileAlignment; |
| 68 | YAMLObj.OptionalHeader->Header.MajorOperatingSystemVersion = |
| 69 | OptionalHeader->MajorOperatingSystemVersion; |
| 70 | YAMLObj.OptionalHeader->Header.MinorOperatingSystemVersion = |
| 71 | OptionalHeader->MinorOperatingSystemVersion; |
| 72 | YAMLObj.OptionalHeader->Header.MajorImageVersion = |
| 73 | OptionalHeader->MajorImageVersion; |
| 74 | YAMLObj.OptionalHeader->Header.MinorImageVersion = |
| 75 | OptionalHeader->MinorImageVersion; |
| 76 | YAMLObj.OptionalHeader->Header.MajorSubsystemVersion = |
| 77 | OptionalHeader->MajorSubsystemVersion; |
| 78 | YAMLObj.OptionalHeader->Header.MinorSubsystemVersion = |
| 79 | OptionalHeader->MinorSubsystemVersion; |
| 80 | YAMLObj.OptionalHeader->Header.Subsystem = OptionalHeader->Subsystem; |
| 81 | YAMLObj.OptionalHeader->Header.DLLCharacteristics = |
| 82 | OptionalHeader->DLLCharacteristics; |
| 83 | YAMLObj.OptionalHeader->Header.SizeOfStackReserve = |
| 84 | OptionalHeader->SizeOfStackReserve; |
| 85 | YAMLObj.OptionalHeader->Header.SizeOfStackCommit = |
| 86 | OptionalHeader->SizeOfStackCommit; |
| 87 | YAMLObj.OptionalHeader->Header.SizeOfHeapReserve = |
| 88 | OptionalHeader->SizeOfHeapReserve; |
| 89 | YAMLObj.OptionalHeader->Header.SizeOfHeapCommit = |
| 90 | OptionalHeader->SizeOfHeapCommit; |
| 91 | unsigned I = 0; |
| 92 | for (auto &DestDD : YAMLObj.OptionalHeader->DataDirectories) { |
| 93 | const object::data_directory *DD; |
| 94 | if (Obj.getDataDirectory(I++, DD)) |
| 95 | continue; |
| 96 | DestDD = COFF::DataDirectory(); |
| 97 | DestDD->RelativeVirtualAddress = DD->RelativeVirtualAddress; |
| 98 | DestDD->Size = DD->Size; |
| 99 | } |
| 100 | } |
| 101 | |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 102 | void COFFDumper::dumpHeader() { |
| 103 | YAMLObj.Header.Machine = Obj.getMachine(); |
| 104 | YAMLObj.Header.Characteristics = Obj.getCharacteristics(); |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Zachary Turner | 63d2fab | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 107 | static void |
| 108 | initializeFileAndStringTable(const llvm::object::COFFObjectFile &Obj, |
| 109 | codeview::StringsAndChecksumsRef &SC) { |
| 110 | |
| 111 | ExitOnError Err("Invalid .debug$S section!"); |
| 112 | // Iterate all .debug$S sections looking for the checksums and string table. |
| 113 | // Exit as soon as both sections are found. |
| 114 | for (const auto &S : Obj.sections()) { |
| 115 | if (SC.hasStrings() && SC.hasChecksums()) |
| 116 | break; |
| 117 | |
| 118 | StringRef SectionName; |
| 119 | S.getName(SectionName); |
| 120 | ArrayRef<uint8_t> sectionData; |
| 121 | if (SectionName != ".debug$S") |
| 122 | continue; |
| 123 | |
| 124 | const object::coff_section *COFFSection = Obj.getCOFFSection(S); |
| 125 | |
| 126 | Obj.getSectionContents(COFFSection, sectionData); |
| 127 | |
| 128 | BinaryStreamReader Reader(sectionData, support::little); |
| 129 | uint32_t Magic; |
| 130 | |
| 131 | Err(Reader.readInteger(Magic)); |
| 132 | assert(Magic == COFF::DEBUG_SECTION_MAGIC && "Invalid .debug$S section!"); |
| 133 | |
| 134 | codeview::DebugSubsectionArray Subsections; |
| 135 | Err(Reader.readArray(Subsections, Reader.bytesRemaining())); |
| 136 | |
| 137 | SC.initialize(Subsections); |
| 138 | } |
| 139 | } |
| 140 | |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 141 | void COFFDumper::dumpSections(unsigned NumSections) { |
David Majnemer | 237544b | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 142 | std::vector<COFFYAML::Section> &YAMLSections = YAMLObj.Sections; |
Zachary Turner | 63d2fab | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 143 | codeview::StringsAndChecksumsRef SC; |
| 144 | initializeFileAndStringTable(Obj, SC); |
| 145 | |
Martin Storsjo | 6ccdeef | 2019-01-07 20:55:33 +0000 | [diff] [blame] | 146 | StringMap<bool> SymbolUnique; |
| 147 | for (const auto &S : Obj.symbols()) { |
| 148 | object::COFFSymbolRef Symbol = Obj.getCOFFSymbol(S); |
| 149 | StringRef Name; |
| 150 | Obj.getSymbolName(Symbol, Name); |
| 151 | StringMap<bool>::iterator It; |
| 152 | bool Inserted; |
| 153 | std::tie(It, Inserted) = SymbolUnique.insert(std::make_pair(Name, true)); |
| 154 | if (!Inserted) |
| 155 | It->second = false; |
| 156 | } |
| 157 | |
David Majnemer | 237544b | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 158 | for (const auto &ObjSection : Obj.sections()) { |
| 159 | const object::coff_section *COFFSection = Obj.getCOFFSection(ObjSection); |
| 160 | COFFYAML::Section NewYAMLSection; |
| 161 | ObjSection.getName(NewYAMLSection.Name); |
| 162 | NewYAMLSection.Header.Characteristics = COFFSection->Characteristics; |
Martin Storsjo | 8dae0db | 2018-11-29 20:53:57 +0000 | [diff] [blame] | 163 | NewYAMLSection.Header.VirtualAddress = COFFSection->VirtualAddress; |
David Majnemer | 237544b | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 164 | NewYAMLSection.Header.VirtualSize = COFFSection->VirtualSize; |
Zachary Turner | 63d2fab | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 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; |
Peter Collingbourne | c0d9a0b | 2018-05-04 16:28:41 +0000 | [diff] [blame] | 175 | uint32_t Shift = (COFFSection->Characteristics >> 20) & 0xF; |
| 176 | NewYAMLSection.Alignment = (1U << Shift) >> 1; |
David Majnemer | cb980bd | 2016-03-18 21:51:14 +0000 | [diff] [blame] | 177 | assert(NewYAMLSection.Alignment <= 8192); |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 178 | |
Rafael Espindola | da177ce | 2013-04-20 02:55:00 +0000 | [diff] [blame] | 179 | ArrayRef<uint8_t> sectionData; |
David Majnemer | 237544b | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 180 | if (!ObjSection.isBSS()) |
| 181 | Obj.getSectionContents(COFFSection, sectionData); |
| 182 | NewYAMLSection.SectionData = yaml::BinaryRef(sectionData); |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 183 | |
Zachary Turner | 63d2fab | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 184 | if (NewYAMLSection.Name == ".debug$S") |
| 185 | NewYAMLSection.DebugS = CodeViewYAML::fromDebugS(sectionData, SC); |
| 186 | else if (NewYAMLSection.Name == ".debug$T") |
Alexandre Ganea | 2031cf6 | 2018-04-09 20:17:56 +0000 | [diff] [blame] | 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); |
Zachary Turner | 8b34868 | 2017-12-06 18:58:48 +0000 | [diff] [blame] | 192 | else if (NewYAMLSection.Name == ".debug$H") |
| 193 | NewYAMLSection.DebugH = CodeViewYAML::fromDebugH(sectionData); |
Zachary Turner | 63d2fab | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 194 | |
Rafael Espindola | e3a0e7f | 2013-06-06 13:06:17 +0000 | [diff] [blame] | 195 | std::vector<COFFYAML::Relocation> Relocations; |
David Majnemer | 237544b | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 196 | for (const auto &Reloc : ObjSection.relocations()) { |
Alexey Samsonov | 5b64579 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 197 | const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc); |
Rafael Espindola | e3a0e7f | 2013-06-06 13:06:17 +0000 | [diff] [blame] | 198 | COFFYAML::Relocation Rel; |
Alexey Samsonov | 5b64579 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 199 | object::symbol_iterator Sym = Reloc.getSymbol(); |
Kevin Enderby | 813e0cf | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 200 | Expected<StringRef> SymbolNameOrErr = Sym->getName(); |
| 201 | if (!SymbolNameOrErr) { |
| 202 | std::string Buf; |
| 203 | raw_string_ostream OS(Buf); |
Jonas Devlieghere | 686dfe3 | 2018-11-11 01:46:03 +0000 | [diff] [blame] | 204 | logAllUnhandledErrors(SymbolNameOrErr.takeError(), OS); |
Kevin Enderby | 813e0cf | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 205 | OS.flush(); |
| 206 | report_fatal_error(Buf); |
| 207 | } |
Martin Storsjo | 6ccdeef | 2019-01-07 20:55:33 +0000 | [diff] [blame] | 208 | if (SymbolUnique.lookup(*SymbolNameOrErr)) |
| 209 | Rel.SymbolName = *SymbolNameOrErr; |
| 210 | else |
| 211 | Rel.SymbolTableIndex = reloc->SymbolTableIndex; |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 212 | Rel.VirtualAddress = reloc->VirtualAddress; |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 213 | Rel.Type = reloc->Type; |
| 214 | Relocations.push_back(Rel); |
| 215 | } |
David Majnemer | 237544b | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 216 | NewYAMLSection.Relocations = Relocations; |
| 217 | YAMLSections.push_back(NewYAMLSection); |
Rafael Espindola | da177ce | 2013-04-20 02:55:00 +0000 | [diff] [blame] | 218 | } |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 219 | } |
| 220 | |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 221 | static void |
| 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; |
| 229 | |
| 230 | Sym->FunctionDefinition = YAMLFD; |
| 231 | } |
| 232 | |
| 233 | static void |
| 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; |
| 239 | |
| 240 | Sym->bfAndefSymbol = YAMLAAS; |
| 241 | } |
| 242 | |
| 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; |
| 248 | |
| 249 | Sym->WeakExternal = YAMLWE; |
| 250 | } |
| 251 | |
| 252 | static void |
| 253 | dumpSectionDefinition(COFFYAML::Symbol *Sym, |
David Majnemer | f1198da | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 254 | const object::coff_aux_section_definition *ObjSD, |
| 255 | bool IsBigObj) { |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 256 | COFF::AuxiliarySectionDefinition YAMLASD; |
David Majnemer | f1198da | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 257 | int32_t AuxNumber = ObjSD->getNumber(IsBigObj); |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 258 | YAMLASD.Length = ObjSD->Length; |
| 259 | YAMLASD.NumberOfRelocations = ObjSD->NumberOfRelocations; |
| 260 | YAMLASD.NumberOfLinenumbers = ObjSD->NumberOfLinenumbers; |
| 261 | YAMLASD.CheckSum = ObjSD->CheckSum; |
David Majnemer | f1198da | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 262 | YAMLASD.Number = AuxNumber; |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 263 | YAMLASD.Selection = ObjSD->Selection; |
| 264 | |
| 265 | Sym->SectionDefinition = YAMLASD; |
| 266 | } |
| 267 | |
| 268 | static void |
| 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; |
| 274 | |
| 275 | Sym->CLRToken = YAMLCLRToken; |
| 276 | } |
| 277 | |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 278 | void COFFDumper::dumpSymbols(unsigned NumSymbols) { |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 279 | std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols; |
Alexey Samsonov | 5b64579 | 2014-03-18 06:53:02 +0000 | [diff] [blame] | 280 | for (const auto &S : Obj.symbols()) { |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 281 | object::COFFSymbolRef Symbol = Obj.getCOFFSymbol(S); |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 282 | COFFYAML::Symbol Sym; |
| 283 | Obj.getSymbolName(Symbol, Sym.Name); |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 284 | Sym.SimpleType = COFF::SymbolBaseType(Symbol.getBaseType()); |
| 285 | Sym.ComplexType = COFF::SymbolComplexType(Symbol.getComplexType()); |
| 286 | Sym.Header.StorageClass = Symbol.getStorageClass(); |
| 287 | Sym.Header.Value = Symbol.getValue(); |
| 288 | Sym.Header.SectionNumber = Symbol.getSectionNumber(); |
| 289 | Sym.Header.NumberOfAuxSymbols = Symbol.getNumberOfAuxSymbols(); |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 290 | |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 291 | if (Symbol.getNumberOfAuxSymbols() > 0) { |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 292 | ArrayRef<uint8_t> AuxData = Obj.getSymbolAuxData(Symbol); |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 293 | if (Symbol.isFunctionDefinition()) { |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 294 | // This symbol represents a function definition. |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 295 | assert(Symbol.getNumberOfAuxSymbols() == 1 && |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 296 | "Expected a single aux symbol to describe this function!"); |
| 297 | |
| 298 | const object::coff_aux_function_definition *ObjFD = |
| 299 | reinterpret_cast<const object::coff_aux_function_definition *>( |
| 300 | AuxData.data()); |
| 301 | dumpFunctionDefinition(&Sym, ObjFD); |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 302 | } else if (Symbol.isFunctionLineInfo()) { |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 303 | // This symbol describes function line number information. |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 304 | assert(Symbol.getNumberOfAuxSymbols() == 1 && |
| 305 | "Expected a single aux symbol to describe this function!"); |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 306 | |
| 307 | const object::coff_aux_bf_and_ef_symbol *ObjBES = |
| 308 | reinterpret_cast<const object::coff_aux_bf_and_ef_symbol *>( |
| 309 | AuxData.data()); |
| 310 | dumpbfAndEfLineInfo(&Sym, ObjBES); |
David Majnemer | a271590 | 2014-10-31 05:07:00 +0000 | [diff] [blame] | 311 | } else if (Symbol.isAnyUndefined()) { |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 312 | // This symbol represents a weak external definition. |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 313 | assert(Symbol.getNumberOfAuxSymbols() == 1 && |
| 314 | "Expected a single aux symbol to describe this weak symbol!"); |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 315 | |
| 316 | const object::coff_aux_weak_external *ObjWE = |
| 317 | reinterpret_cast<const object::coff_aux_weak_external *>( |
| 318 | AuxData.data()); |
| 319 | dumpWeakExternal(&Sym, ObjWE); |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 320 | } else if (Symbol.isFileRecord()) { |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 321 | // This symbol represents a file record. |
| 322 | Sym.File = StringRef(reinterpret_cast<const char *>(AuxData.data()), |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 323 | Symbol.getNumberOfAuxSymbols() * |
| 324 | Obj.getSymbolTableEntrySize()) |
David Majnemer | d9a0abc | 2014-03-20 06:29:02 +0000 | [diff] [blame] | 325 | .rtrim(StringRef("\0", /*length=*/1)); |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 326 | } else if (Symbol.isSectionDefinition()) { |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 327 | // This symbol represents a section definition. |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 328 | assert(Symbol.getNumberOfAuxSymbols() == 1 && |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 329 | "Expected a single aux symbol to describe this section!"); |
| 330 | |
| 331 | const object::coff_aux_section_definition *ObjSD = |
| 332 | reinterpret_cast<const object::coff_aux_section_definition *>( |
| 333 | AuxData.data()); |
David Majnemer | f1198da | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 334 | dumpSectionDefinition(&Sym, ObjSD, Symbol.isBigObj()); |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 335 | } else if (Symbol.isCLRToken()) { |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 336 | // This symbol represents a CLR token definition. |
David Majnemer | b3a86a0 | 2014-09-10 12:51:52 +0000 | [diff] [blame] | 337 | assert(Symbol.getNumberOfAuxSymbols() == 1 && |
| 338 | "Expected a single aux symbol to describe this CLR Token!"); |
David Majnemer | 6d19153 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 339 | |
| 340 | const object::coff_aux_clr_token *ObjCLRToken = |
| 341 | reinterpret_cast<const object::coff_aux_clr_token *>( |
| 342 | AuxData.data()); |
| 343 | dumpCLRTokenDefinition(&Sym, ObjCLRToken); |
| 344 | } else { |
| 345 | llvm_unreachable("Unhandled auxiliary symbol!"); |
| 346 | } |
| 347 | } |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 348 | Symbols.push_back(Sym); |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 349 | } |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 352 | COFFYAML::Object &COFFDumper::getYAMLObj() { |
| 353 | return YAMLObj; |
| 354 | } |
| 355 | |
Rafael Espindola | 1ad4502 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 356 | std::error_code coff2yaml(raw_ostream &Out, const object::COFFObjectFile &Obj) { |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 357 | COFFDumper Dumper(Obj); |
Rafael Espindola | da177ce | 2013-04-20 02:55:00 +0000 | [diff] [blame] | 358 | |
Rafael Espindola | 2bbe378 | 2013-05-17 22:58:42 +0000 | [diff] [blame] | 359 | yaml::Output Yout(Out); |
| 360 | Yout << Dumper.getYAMLObj(); |
Rafael Espindola | da177ce | 2013-04-20 02:55:00 +0000 | [diff] [blame] | 361 | |
Rui Ueyama | eae4673 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 362 | return std::error_code(); |
Marshall Clow | c57b888 | 2012-06-19 18:02:35 +0000 | [diff] [blame] | 363 | } |