[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / tools / obj2yaml / xcoff2yaml.cpp
blobdd9d8e875096544b31e22a620e6f83abd99368ef
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 //===----------------------------------------------------------------------===//
9 #include "obj2yaml.h"
10 #include "llvm/Object/XCOFFObjectFile.h"
11 #include "llvm/ObjectYAML/XCOFFYAML.h"
12 #include "llvm/Support/YAMLTraits.h"
14 using namespace llvm;
15 using namespace llvm::object;
16 namespace {
18 class XCOFFDumper {
19 const object::XCOFFObjectFile &Obj;
20 XCOFFYAML::Object YAMLObj;
21 void dumpHeader();
22 std::error_code dumpSymbols();
24 public:
25 XCOFFDumper(const object::XCOFFObjectFile &obj) : Obj(obj) {}
26 std::error_code dump();
27 XCOFFYAML::Object &getYAMLObj() { return YAMLObj; }
29 } // namespace
31 std::error_code XCOFFDumper::dump() {
32 dumpHeader();
33 return dumpSymbols();
36 void XCOFFDumper::dumpHeader() {
38 YAMLObj.Header.Magic = Obj.getMagic();
39 YAMLObj.Header.NumberOfSections = Obj.getNumberOfSections();
40 YAMLObj.Header.TimeStamp = Obj.getTimeStamp();
42 // TODO FIXME only dump 32 bit header for now.
43 if (Obj.is64Bit())
44 report_fatal_error("64-bit XCOFF files not supported yet.");
45 YAMLObj.Header.SymbolTableOffset = Obj.getSymbolTableOffset32();
47 YAMLObj.Header.NumberOfSymTableEntries =
48 Obj.getRawNumberOfSymbolTableEntries32();
49 YAMLObj.Header.AuxHeaderSize = Obj.getOptionalHeaderSize();
50 YAMLObj.Header.Flags = Obj.getFlags();
53 std::error_code XCOFFDumper::dumpSymbols() {
54 std::vector<XCOFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
56 for (const SymbolRef &S : Obj.symbols()) {
57 DataRefImpl SymbolDRI = S.getRawDataRefImpl();
58 const XCOFFSymbolEntry *SymbolEntPtr = Obj.toSymbolEntry(SymbolDRI);
59 XCOFFYAML::Symbol Sym;
61 Expected<StringRef> SymNameRefOrErr = Obj.getSymbolName(SymbolDRI);
62 if (!SymNameRefOrErr) {
63 return errorToErrorCode(SymNameRefOrErr.takeError());
65 Sym.SymbolName = SymNameRefOrErr.get();
67 Sym.Value = SymbolEntPtr->Value;
69 Expected<StringRef> SectionNameRefOrErr =
70 Obj.getSymbolSectionName(SymbolEntPtr);
71 if (!SectionNameRefOrErr)
72 return errorToErrorCode(SectionNameRefOrErr.takeError());
74 Sym.SectionName = SectionNameRefOrErr.get();
76 Sym.Type = SymbolEntPtr->SymbolType;
77 Sym.StorageClass = SymbolEntPtr->StorageClass;
78 Sym.NumberOfAuxEntries = SymbolEntPtr->NumberOfAuxEntries;
79 Symbols.push_back(Sym);
82 return std::error_code();
85 std::error_code xcoff2yaml(raw_ostream &Out,
86 const object::XCOFFObjectFile &Obj) {
87 XCOFFDumper Dumper(Obj);
89 if (std::error_code EC = Dumper.dump())
90 return EC;
92 yaml::Output Yout(Out);
93 Yout << Dumper.getYAMLObj();
95 return std::error_code();