comparison tools/obj2yaml/coff2yaml.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children 3a76565eade5
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 9
10 #include "obj2yaml.h" 10 #include "obj2yaml.h"
11 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
12 #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
13 #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
11 #include "llvm/Object/COFF.h" 14 #include "llvm/Object/COFF.h"
12 #include "llvm/ObjectYAML/COFFYAML.h" 15 #include "llvm/ObjectYAML/COFFYAML.h"
16 #include "llvm/ObjectYAML/CodeViewYAMLSymbols.h"
17 #include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
13 #include "llvm/Support/ErrorHandling.h" 18 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/YAMLTraits.h" 19 #include "llvm/Support/YAMLTraits.h"
15 20
16 using namespace llvm; 21 using namespace llvm;
17 22
97 void COFFDumper::dumpHeader() { 102 void COFFDumper::dumpHeader() {
98 YAMLObj.Header.Machine = Obj.getMachine(); 103 YAMLObj.Header.Machine = Obj.getMachine();
99 YAMLObj.Header.Characteristics = Obj.getCharacteristics(); 104 YAMLObj.Header.Characteristics = Obj.getCharacteristics();
100 } 105 }
101 106
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
102 void COFFDumper::dumpSections(unsigned NumSections) { 141 void COFFDumper::dumpSections(unsigned NumSections) {
103 std::vector<COFFYAML::Section> &YAMLSections = YAMLObj.Sections; 142 std::vector<COFFYAML::Section> &YAMLSections = YAMLObj.Sections;
143 codeview::StringsAndChecksumsRef SC;
144 initializeFileAndStringTable(Obj, SC);
145
104 for (const auto &ObjSection : Obj.sections()) { 146 for (const auto &ObjSection : Obj.sections()) {
105 const object::coff_section *COFFSection = Obj.getCOFFSection(ObjSection); 147 const object::coff_section *COFFSection = Obj.getCOFFSection(ObjSection);
106 COFFYAML::Section NewYAMLSection; 148 COFFYAML::Section NewYAMLSection;
107 ObjSection.getName(NewYAMLSection.Name); 149 ObjSection.getName(NewYAMLSection.Name);
108 NewYAMLSection.Header.Characteristics = COFFSection->Characteristics; 150 NewYAMLSection.Header.Characteristics = COFFSection->Characteristics;
109 NewYAMLSection.Header.VirtualAddress = ObjSection.getAddress(); 151 NewYAMLSection.Header.VirtualAddress = ObjSection.getAddress();
110 NewYAMLSection.Header.VirtualSize = COFFSection->VirtualSize; 152 NewYAMLSection.Header.VirtualSize = COFFSection->VirtualSize;
153 NewYAMLSection.Header.NumberOfLineNumbers =
154 COFFSection->NumberOfLinenumbers;
155 NewYAMLSection.Header.NumberOfRelocations =
156 COFFSection->NumberOfRelocations;
157 NewYAMLSection.Header.PointerToLineNumbers =
158 COFFSection->PointerToLinenumbers;
159 NewYAMLSection.Header.PointerToRawData = COFFSection->PointerToRawData;
160 NewYAMLSection.Header.PointerToRelocations =
161 COFFSection->PointerToRelocations;
162 NewYAMLSection.Header.SizeOfRawData = COFFSection->SizeOfRawData;
111 NewYAMLSection.Alignment = ObjSection.getAlignment(); 163 NewYAMLSection.Alignment = ObjSection.getAlignment();
112 assert(NewYAMLSection.Alignment <= 8192); 164 assert(NewYAMLSection.Alignment <= 8192);
113 165
114 ArrayRef<uint8_t> sectionData; 166 ArrayRef<uint8_t> sectionData;
115 if (!ObjSection.isBSS()) 167 if (!ObjSection.isBSS())
116 Obj.getSectionContents(COFFSection, sectionData); 168 Obj.getSectionContents(COFFSection, sectionData);
117 NewYAMLSection.SectionData = yaml::BinaryRef(sectionData); 169 NewYAMLSection.SectionData = yaml::BinaryRef(sectionData);
170
171 if (NewYAMLSection.Name == ".debug$S")
172 NewYAMLSection.DebugS = CodeViewYAML::fromDebugS(sectionData, SC);
173 else if (NewYAMLSection.Name == ".debug$T")
174 NewYAMLSection.DebugT = CodeViewYAML::fromDebugT(sectionData);
118 175
119 std::vector<COFFYAML::Relocation> Relocations; 176 std::vector<COFFYAML::Relocation> Relocations;
120 for (const auto &Reloc : ObjSection.relocations()) { 177 for (const auto &Reloc : ObjSection.relocations()) {
121 const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc); 178 const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc);
122 COFFYAML::Relocation Rel; 179 COFFYAML::Relocation Rel;