1//===------ xcoff2yaml.cpp - XCOFF YAMLIO implementation --------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "obj2yaml.h"
10#include "llvm/Object/XCOFFObjectFile.h"
11#include "llvm/ObjectYAML/XCOFFYAML.h"
12#include "llvm/Support/YAMLTraits.h"
13
14using namespace llvm;
15using namespace llvm::object;
16namespace {
17
18class XCOFFDumper {
19 const object::XCOFFObjectFile &Obj;
20 XCOFFYAML::Object YAMLObj;
21 void dumpHeader();
22 Error dumpSections();
23 Error dumpSymbols();
24 template <typename Shdr, typename Reloc>
25 Error dumpSections(ArrayRef<Shdr> Sections);
26
27 // Dump auxiliary symbols.
28 Error dumpFileAuxSym(XCOFFYAML::Symbol &Sym,
29 const XCOFFSymbolRef &SymbolEntRef);
30 Error dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
31 const XCOFFSymbolRef &SymbolEntRef);
32 Error dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
33 const XCOFFSymbolRef &SymbolEntRef);
34 Error dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
35 const XCOFFSymbolRef &SymbolEntRef);
36 Error dumpAuxSyms(XCOFFYAML::Symbol &Sym, const XCOFFSymbolRef &SymbolEntRef);
37 void dumpFuncAuxSym(XCOFFYAML::Symbol &Sym, const uintptr_t AuxAddress);
38 void dumpExpAuxSym(XCOFFYAML::Symbol &Sym, const uintptr_t AuxAddress);
39 void dumpCsectAuxSym(XCOFFYAML::Symbol &Sym,
40 const object::XCOFFCsectAuxRef &AuxEntPtr);
41
42public:
43 XCOFFDumper(const object::XCOFFObjectFile &obj) : Obj(obj) {}
44 Error dump();
45 XCOFFYAML::Object &getYAMLObj() { return YAMLObj; }
46
47 template <typename T> const T *getAuxEntPtr(uintptr_t AuxAddress) {
48 Obj.checkSymbolEntryPointer(SymbolEntPtr: AuxAddress);
49 return reinterpret_cast<const T *>(AuxAddress);
50 }
51};
52} // namespace
53
54Error XCOFFDumper::dump() {
55 dumpHeader();
56 if (Error E = dumpSections())
57 return E;
58 return dumpSymbols();
59}
60
61void XCOFFDumper::dumpHeader() {
62 YAMLObj.Header.Magic = Obj.getMagic();
63 YAMLObj.Header.NumberOfSections = Obj.getNumberOfSections();
64 YAMLObj.Header.TimeStamp = Obj.getTimeStamp();
65 YAMLObj.Header.SymbolTableOffset = Obj.is64Bit()
66 ? Obj.getSymbolTableOffset64()
67 : Obj.getSymbolTableOffset32();
68 YAMLObj.Header.NumberOfSymTableEntries =
69 Obj.is64Bit() ? Obj.getNumberOfSymbolTableEntries64()
70 : Obj.getRawNumberOfSymbolTableEntries32();
71 YAMLObj.Header.AuxHeaderSize = Obj.getOptionalHeaderSize();
72 YAMLObj.Header.Flags = Obj.getFlags();
73}
74
75Error XCOFFDumper::dumpSections() {
76 if (Obj.is64Bit())
77 return dumpSections<XCOFFSectionHeader64, XCOFFRelocation64>(
78 Sections: Obj.sections64());
79 return dumpSections<XCOFFSectionHeader32, XCOFFRelocation32>(
80 Sections: Obj.sections32());
81}
82
83template <typename Shdr, typename Reloc>
84Error XCOFFDumper::dumpSections(ArrayRef<Shdr> Sections) {
85 std::vector<XCOFFYAML::Section> &YamlSections = YAMLObj.Sections;
86 for (const Shdr &S : Sections) {
87 XCOFFYAML::Section YamlSec;
88 YamlSec.SectionName = S.getName();
89 YamlSec.Address = S.PhysicalAddress;
90 YamlSec.Size = S.SectionSize;
91 YamlSec.NumberOfRelocations = S.NumberOfRelocations;
92 YamlSec.NumberOfLineNumbers = S.NumberOfLineNumbers;
93 YamlSec.FileOffsetToData = S.FileOffsetToRawData;
94 YamlSec.FileOffsetToRelocations = S.FileOffsetToRelocationInfo;
95 YamlSec.FileOffsetToLineNumbers = S.FileOffsetToLineNumberInfo;
96 YamlSec.Flags = S.Flags;
97 if (YamlSec.Flags & XCOFF::STYP_DWARF) {
98 unsigned Mask = Obj.is64Bit()
99 ? XCOFFSectionHeader64::SectionFlagsTypeMask
100 : XCOFFSectionHeader32::SectionFlagsTypeMask;
101 YamlSec.SectionSubtype =
102 static_cast<XCOFF::DwarfSectionSubtypeFlags>(S.Flags & ~Mask);
103 }
104
105 // Dump section data.
106 if (S.FileOffsetToRawData) {
107 DataRefImpl SectionDRI;
108 SectionDRI.p = reinterpret_cast<uintptr_t>(&S);
109 Expected<ArrayRef<uint8_t>> SecDataRefOrErr =
110 Obj.getSectionContents(Sec: SectionDRI);
111 if (!SecDataRefOrErr)
112 return SecDataRefOrErr.takeError();
113 YamlSec.SectionData = SecDataRefOrErr.get();
114 }
115
116 // Dump relocations.
117 if (S.NumberOfRelocations) {
118 auto RelRefOrErr = Obj.relocations<Shdr, Reloc>(S);
119 if (!RelRefOrErr)
120 return RelRefOrErr.takeError();
121 for (const Reloc &R : RelRefOrErr.get()) {
122 XCOFFYAML::Relocation YamlRel;
123 YamlRel.Type = R.Type;
124 YamlRel.Info = R.Info;
125 YamlRel.SymbolIndex = R.SymbolIndex;
126 YamlRel.VirtualAddress = R.VirtualAddress;
127 YamlSec.Relocations.push_back(x: YamlRel);
128 }
129 }
130 YamlSections.push_back(x: YamlSec);
131 }
132 return Error::success();
133}
134
135Error XCOFFDumper::dumpFileAuxSym(XCOFFYAML::Symbol &Sym,
136 const XCOFFSymbolRef &SymbolEntRef) {
137 for (uint8_t I = 1; I <= Sym.NumberOfAuxEntries; ++I) {
138 uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
139 CurrentAddress: SymbolEntRef.getEntryAddress(), Distance: I);
140 const XCOFFFileAuxEnt *FileAuxEntPtr =
141 getAuxEntPtr<XCOFFFileAuxEnt>(AuxAddress);
142 auto FileNameOrError = Obj.getCFileName(CFileEntPtr: FileAuxEntPtr);
143 if (!FileNameOrError)
144 return FileNameOrError.takeError();
145
146 XCOFFYAML::FileAuxEnt FileAuxSym;
147 FileAuxSym.FileNameOrString = FileNameOrError.get();
148 FileAuxSym.FileStringType = FileAuxEntPtr->Type;
149 Sym.AuxEntries.push_back(
150 x: std::make_unique<XCOFFYAML::FileAuxEnt>(args&: FileAuxSym));
151 }
152 return Error::success();
153}
154
155Error XCOFFDumper::dumpStatAuxSym(XCOFFYAML::Symbol &Sym,
156 const XCOFFSymbolRef &SymbolEntRef) {
157 if (Sym.NumberOfAuxEntries != 1) {
158 uint32_t SymbolIndex = Obj.getSymbolIndex(SymEntPtr: SymbolEntRef.getEntryAddress());
159 return createError(Err: "failed to parse symbol \"" + Sym.SymbolName +
160 "\" with index of " + Twine(SymbolIndex) +
161 ": expected 1 aux symbol for C_STAT, while got " +
162 Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
163 }
164
165 const XCOFFSectAuxEntForStat *AuxEntPtr =
166 getAuxEntPtr<XCOFFSectAuxEntForStat>(
167 AuxAddress: XCOFFObjectFile::getAdvancedSymbolEntryAddress(
168 CurrentAddress: SymbolEntRef.getEntryAddress(), Distance: 1));
169 XCOFFYAML::SectAuxEntForStat StatAuxSym;
170 StatAuxSym.SectionLength = AuxEntPtr->SectionLength;
171 StatAuxSym.NumberOfLineNum = AuxEntPtr->NumberOfLineNum;
172 StatAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
173 Sym.AuxEntries.push_back(
174 x: std::make_unique<XCOFFYAML::SectAuxEntForStat>(args&: StatAuxSym));
175 return Error::success();
176}
177
178void XCOFFDumper::dumpFuncAuxSym(XCOFFYAML::Symbol &Sym,
179 const uintptr_t AuxAddress) {
180 XCOFFYAML::FunctionAuxEnt FunAuxSym;
181
182 if (Obj.is64Bit()) {
183 const XCOFFFunctionAuxEnt64 *AuxEntPtr =
184 getAuxEntPtr<XCOFFFunctionAuxEnt64>(AuxAddress);
185 FunAuxSym.PtrToLineNum = AuxEntPtr->PtrToLineNum;
186 FunAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
187 FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
188 } else {
189 const XCOFFFunctionAuxEnt32 *AuxEntPtr =
190 getAuxEntPtr<XCOFFFunctionAuxEnt32>(AuxAddress);
191 FunAuxSym.OffsetToExceptionTbl = AuxEntPtr->OffsetToExceptionTbl;
192 FunAuxSym.PtrToLineNum = AuxEntPtr->PtrToLineNum;
193 FunAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
194 FunAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
195 }
196
197 Sym.AuxEntries.push_back(
198 x: std::make_unique<XCOFFYAML::FunctionAuxEnt>(args&: FunAuxSym));
199}
200
201void XCOFFDumper::dumpExpAuxSym(XCOFFYAML::Symbol &Sym,
202 const uintptr_t AuxAddress) {
203 const XCOFFExceptionAuxEnt *AuxEntPtr =
204 getAuxEntPtr<XCOFFExceptionAuxEnt>(AuxAddress);
205 XCOFFYAML::ExcpetionAuxEnt ExceptAuxSym;
206 ExceptAuxSym.OffsetToExceptionTbl = AuxEntPtr->OffsetToExceptionTbl;
207 ExceptAuxSym.SizeOfFunction = AuxEntPtr->SizeOfFunction;
208 ExceptAuxSym.SymIdxOfNextBeyond = AuxEntPtr->SymIdxOfNextBeyond;
209 Sym.AuxEntries.push_back(
210 x: std::make_unique<XCOFFYAML::ExcpetionAuxEnt>(args&: ExceptAuxSym));
211}
212
213void XCOFFDumper::dumpCsectAuxSym(XCOFFYAML::Symbol &Sym,
214 const object::XCOFFCsectAuxRef &AuxEntPtr) {
215 XCOFFYAML::CsectAuxEnt CsectAuxSym;
216 CsectAuxSym.ParameterHashIndex = AuxEntPtr.getParameterHashIndex();
217 CsectAuxSym.TypeChkSectNum = AuxEntPtr.getTypeChkSectNum();
218 CsectAuxSym.SymbolAlignment = AuxEntPtr.getAlignmentLog2();
219 CsectAuxSym.SymbolType =
220 static_cast<XCOFF::SymbolType>(AuxEntPtr.getSymbolType());
221 CsectAuxSym.StorageMappingClass = AuxEntPtr.getStorageMappingClass();
222
223 if (Obj.is64Bit()) {
224 CsectAuxSym.SectionOrLengthLo =
225 static_cast<uint32_t>(AuxEntPtr.getSectionOrLength64());
226 CsectAuxSym.SectionOrLengthHi =
227 static_cast<uint32_t>(AuxEntPtr.getSectionOrLength64() >> 32);
228 } else {
229 CsectAuxSym.SectionOrLength = AuxEntPtr.getSectionOrLength32();
230 CsectAuxSym.StabInfoIndex = AuxEntPtr.getStabInfoIndex32();
231 CsectAuxSym.StabSectNum = AuxEntPtr.getStabSectNum32();
232 }
233
234 Sym.AuxEntries.push_back(
235 x: std::make_unique<XCOFFYAML::CsectAuxEnt>(args&: CsectAuxSym));
236}
237
238Error XCOFFDumper::dumpAuxSyms(XCOFFYAML::Symbol &Sym,
239 const XCOFFSymbolRef &SymbolEntRef) {
240 auto ErrOrCsectAuxRef = SymbolEntRef.getXCOFFCsectAuxRef();
241 if (!ErrOrCsectAuxRef)
242 return ErrOrCsectAuxRef.takeError();
243 XCOFFCsectAuxRef CsectAuxRef = ErrOrCsectAuxRef.get();
244
245 for (uint8_t I = 1; I <= Sym.NumberOfAuxEntries; ++I) {
246
247 if (I == Sym.NumberOfAuxEntries && !Obj.is64Bit()) {
248 dumpCsectAuxSym(Sym, AuxEntPtr: CsectAuxRef);
249 return Error::success();
250 }
251
252 uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
253 CurrentAddress: SymbolEntRef.getEntryAddress(), Distance: I);
254
255 if (Obj.is64Bit()) {
256 XCOFF::SymbolAuxType Type = *Obj.getSymbolAuxType(AuxEntryAddress: AuxAddress);
257 if (Type == XCOFF::SymbolAuxType::AUX_CSECT)
258 dumpCsectAuxSym(Sym, AuxEntPtr: CsectAuxRef);
259 else if (Type == XCOFF::SymbolAuxType::AUX_FCN)
260 dumpFuncAuxSym(Sym, AuxAddress);
261 else if (Type == XCOFF::SymbolAuxType::AUX_EXCEPT)
262 dumpExpAuxSym(Sym, AuxAddress);
263 else {
264 uint32_t SymbolIndex =
265 Obj.getSymbolIndex(SymEntPtr: SymbolEntRef.getEntryAddress());
266 return createError(Err: "failed to parse symbol \"" + Sym.SymbolName +
267 "\" with index of " + Twine(SymbolIndex) +
268 ": invalid auxiliary symbol type: " +
269 Twine(static_cast<uint32_t>(Type)));
270 }
271
272 } else
273 dumpFuncAuxSym(Sym, AuxAddress);
274 }
275
276 return Error::success();
277}
278
279Error XCOFFDumper::dumpBlockAuxSym(XCOFFYAML::Symbol &Sym,
280 const XCOFFSymbolRef &SymbolEntRef) {
281 if (Sym.NumberOfAuxEntries != 1) {
282 uint32_t SymbolIndex = Obj.getSymbolIndex(SymEntPtr: SymbolEntRef.getEntryAddress());
283 return createError(
284 Err: "failed to parse symbol \"" + Sym.SymbolName + "\" with index of " +
285 Twine(SymbolIndex) +
286 ": expected 1 aux symbol for C_BLOCK or C_FCN, while got " +
287 Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
288 }
289
290 uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
291 CurrentAddress: SymbolEntRef.getEntryAddress(), Distance: 1);
292 XCOFFYAML::BlockAuxEnt BlockAuxSym;
293
294 if (Obj.is64Bit()) {
295 const XCOFFBlockAuxEnt64 *AuxEntPtr =
296 getAuxEntPtr<XCOFFBlockAuxEnt64>(AuxAddress);
297 BlockAuxSym.LineNum = AuxEntPtr->LineNum;
298 } else {
299 const XCOFFBlockAuxEnt32 *AuxEntPtr =
300 getAuxEntPtr<XCOFFBlockAuxEnt32>(AuxAddress);
301 BlockAuxSym.LineNumLo = AuxEntPtr->LineNumLo;
302 BlockAuxSym.LineNumHi = AuxEntPtr->LineNumHi;
303 }
304
305 Sym.AuxEntries.push_back(
306 x: std::make_unique<XCOFFYAML::BlockAuxEnt>(args&: BlockAuxSym));
307 return Error::success();
308}
309
310Error XCOFFDumper::dumpDwarfAuxSym(XCOFFYAML::Symbol &Sym,
311 const XCOFFSymbolRef &SymbolEntRef) {
312 if (Sym.NumberOfAuxEntries != 1) {
313 uint32_t SymbolIndex = Obj.getSymbolIndex(SymEntPtr: SymbolEntRef.getEntryAddress());
314 return createError(Err: "failed to parse symbol \"" + Sym.SymbolName +
315 "\" with index of " + Twine(SymbolIndex) +
316 ": expected 1 aux symbol for C_DWARF, while got " +
317 Twine(static_cast<uint32_t>(*Sym.NumberOfAuxEntries)));
318 }
319
320 uintptr_t AuxAddress = XCOFFObjectFile::getAdvancedSymbolEntryAddress(
321 CurrentAddress: SymbolEntRef.getEntryAddress(), Distance: 1);
322 XCOFFYAML::SectAuxEntForDWARF DwarfAuxSym;
323
324 if (Obj.is64Bit()) {
325 const XCOFFSectAuxEntForDWARF64 *AuxEntPtr =
326 getAuxEntPtr<XCOFFSectAuxEntForDWARF64>(AuxAddress);
327 DwarfAuxSym.LengthOfSectionPortion = AuxEntPtr->LengthOfSectionPortion;
328 DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
329 } else {
330 const XCOFFSectAuxEntForDWARF32 *AuxEntPtr =
331 getAuxEntPtr<XCOFFSectAuxEntForDWARF32>(AuxAddress);
332 DwarfAuxSym.LengthOfSectionPortion = AuxEntPtr->LengthOfSectionPortion;
333 DwarfAuxSym.NumberOfRelocEnt = AuxEntPtr->NumberOfRelocEnt;
334 }
335
336 Sym.AuxEntries.push_back(
337 x: std::make_unique<XCOFFYAML::SectAuxEntForDWARF>(args&: DwarfAuxSym));
338 return Error::success();
339}
340
341Error XCOFFDumper::dumpSymbols() {
342 std::vector<XCOFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
343
344 for (const SymbolRef &S : Obj.symbols()) {
345 DataRefImpl SymbolDRI = S.getRawDataRefImpl();
346 const XCOFFSymbolRef SymbolEntRef = Obj.toSymbolRef(Ref: SymbolDRI);
347 XCOFFYAML::Symbol Sym;
348
349 Expected<StringRef> SymNameRefOrErr = Obj.getSymbolName(Symb: SymbolDRI);
350 if (!SymNameRefOrErr) {
351 return SymNameRefOrErr.takeError();
352 }
353 Sym.SymbolName = SymNameRefOrErr.get();
354
355 Sym.Value = SymbolEntRef.getValue();
356
357 Expected<StringRef> SectionNameRefOrErr =
358 Obj.getSymbolSectionName(Ref: SymbolEntRef);
359 if (!SectionNameRefOrErr)
360 return SectionNameRefOrErr.takeError();
361
362 Sym.SectionName = SectionNameRefOrErr.get();
363
364 Sym.Type = SymbolEntRef.getSymbolType();
365 Sym.StorageClass = SymbolEntRef.getStorageClass();
366 Sym.NumberOfAuxEntries = SymbolEntRef.getNumberOfAuxEntries();
367
368 if (Sym.NumberOfAuxEntries) {
369 switch (Sym.StorageClass) {
370 case XCOFF::C_FILE:
371 if (Error E = dumpFileAuxSym(Sym, SymbolEntRef))
372 return E;
373 break;
374 case XCOFF::C_STAT:
375 if (Error E = dumpStatAuxSym(Sym, SymbolEntRef))
376 return E;
377 break;
378 case XCOFF::C_EXT:
379 case XCOFF::C_WEAKEXT:
380 case XCOFF::C_HIDEXT:
381 if (Error E = dumpAuxSyms(Sym, SymbolEntRef))
382 return E;
383 break;
384 case XCOFF::C_BLOCK:
385 case XCOFF::C_FCN:
386 if (Error E = dumpBlockAuxSym(Sym, SymbolEntRef))
387 return E;
388 break;
389 case XCOFF::C_DWARF:
390 if (Error E = dumpDwarfAuxSym(Sym, SymbolEntRef))
391 return E;
392 break;
393 default:
394 break;
395 }
396 }
397
398 Symbols.push_back(x: std::move(Sym));
399 }
400
401 return Error::success();
402}
403
404Error xcoff2yaml(raw_ostream &Out, const object::XCOFFObjectFile &Obj) {
405 XCOFFDumper Dumper(Obj);
406
407 if (Error E = Dumper.dump())
408 return E;
409
410 yaml::Output Yout(Out);
411 Yout << Dumper.getYAMLObj();
412
413 return Error::success();
414}
415