1 //===------ xcoff2yaml.cpp - XCOFF YAMLIO implementation --------*- 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/Object/XCOFFObjectFile.h"
11 #include "llvm/ObjectYAML/XCOFFYAML.h"
12 #include "llvm/Support/YAMLTraits.h"
15 using namespace llvm::object
;
19 const object::XCOFFObjectFile
&Obj
;
20 XCOFFYAML::Object YAMLObj
;
22 std::error_code
dumpSymbols();
25 XCOFFDumper(const object::XCOFFObjectFile
&obj
) : Obj(obj
) {}
26 std::error_code
dump();
27 XCOFFYAML::Object
&getYAMLObj() { return YAMLObj
; }
31 std::error_code
XCOFFDumper::dump() {
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.
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())
92 yaml::Output
Yout(Out
);
93 Yout
<< Dumper
.getYAMLObj();
95 return std::error_code();