1//===------ goff2yaml.cpp - obj2yaml conversion tool ----------------------===//
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/GOFFObjectFile.h"
11#include "llvm/ObjectYAML/ObjectYAML.h"
12#include "llvm/Support/ConvertEBCDIC.h"
13
14using namespace llvm;
15
16static std::string getFixedLengthEBCDICString(DataExtractor &Data,
17 DataExtractor::Cursor &C,
18 uint64_t Length,
19 StringRef TrimChars = {"\0", 1}) {
20 StringRef FixedLenStr = Data.getBytes(C, Length);
21 SmallString<16> Str;
22 ConverterEBCDIC::convertToUTF8(Source: FixedLenStr, Result&: Str);
23 return Str.str().trim(Chars: TrimChars).str();
24}
25
26class GOFFDumper {
27 const object::GOFFObjectFile &Obj;
28 GOFFYAML::Object YAMLObj;
29
30 Error dumpHeader(ArrayRef<uint8_t> Data);
31 Error dumpExternalSymbol(ArrayRef<uint8_t> Data);
32 Error dumpText(ArrayRef<uint8_t> Data);
33 Error dumpRelocationDirectory(ArrayRef<uint8_t> Data);
34 Error dumpDeferredLength(ArrayRef<uint8_t> Data);
35 Error dumpEnd(ArrayRef<uint8_t> Data);
36
37public:
38 GOFFDumper(const object::GOFFObjectFile &Obj);
39 Error dump();
40 GOFFYAML::Object &getYAMLObj();
41};
42
43GOFFDumper::GOFFDumper(const object::GOFFObjectFile &Obj) : Obj(Obj) {}
44
45Error GOFFDumper::dumpHeader(ArrayRef<uint8_t> Data) {
46 DataExtractor DE(Data, false);
47 DataExtractor::Cursor C(0);
48
49 // Flattened data contains: PTV header (bytes 0-2) + bytes 3-60 (prefix) +
50 // data from byte 60 onwards HDR data starts at byte 4 in the original record
51 C.seek(NewOffSet: 4); // Skip PTV header and record type
52 YAMLObj.Header.TargetEnvironment = DE.getU32(C);
53 YAMLObj.Header.TargetOperatingSystem = DE.getU32(C);
54 DE.skip(C, Length: 2);
55 YAMLObj.Header.CCSID = DE.getU16(C);
56 YAMLObj.Header.CharacterSetName = getFixedLengthEBCDICString(Data&: DE, C, Length: 16);
57 YAMLObj.Header.LanguageProductIdentifier =
58 getFixedLengthEBCDICString(Data&: DE, C, Length: 16);
59 YAMLObj.Header.ArchitectureLevel = DE.getU32(C);
60 uint16_t PropertiesLength = DE.getU16(C);
61 DE.skip(C, Length: 6);
62 if (PropertiesLength) {
63 YAMLObj.Header.InternalCCSID = DE.getU16(C);
64 PropertiesLength -= 2;
65 }
66 if (PropertiesLength) {
67 YAMLObj.Header.TargetSoftwareEnvironment = DE.getU8(C);
68 PropertiesLength -= 1;
69 }
70 if (!C)
71 return C.takeError();
72 return Error::success();
73}
74
75Error GOFFDumper::dumpExternalSymbol(ArrayRef<uint8_t> Data) {
76 // TODO: Implement dumping ESD records
77 return Error::success();
78}
79
80Error GOFFDumper::dumpText(ArrayRef<uint8_t> Data) {
81 // TODO: Implement dumping TXT records
82 return Error::success();
83}
84
85Error GOFFDumper::dumpRelocationDirectory(ArrayRef<uint8_t> Data) {
86 // TODO: Implement dumping RLD records
87 return Error::success();
88}
89
90Error GOFFDumper::dumpDeferredLength(ArrayRef<uint8_t> Records) {
91 // TODO: Implement if/when GOFF LEN records are emitted by current producers
92 // or covered by handcrafted-object tests.
93 return Error::success();
94}
95
96Error GOFFDumper::dumpEnd(ArrayRef<uint8_t> Records) {
97 // TODO: implement dumping END records
98 return Error::success();
99}
100
101Error GOFFDumper::dump() {
102 Error Err = Error::success();
103
104 // Use the pre-flattened data structure instead of iterating through records
105 const auto &FlattenedData = Obj.getFlattenedData();
106
107 for (const auto &[RecordType, Data] : FlattenedData) {
108 switch (RecordType) {
109 case GOFF::RT_HDR:
110 if (auto Err = dumpHeader(Data))
111 return Err;
112 break;
113 case GOFF::RT_ESD:
114 if (auto Err = dumpExternalSymbol(Data))
115 return Err;
116 break;
117 case GOFF::RT_TXT:
118 if (auto Err = dumpText(Data))
119 return Err;
120 break;
121 case GOFF::RT_RLD:
122 if (auto Err = dumpRelocationDirectory(Data))
123 return Err;
124 break;
125 case GOFF::RT_LEN:
126 if (auto Err = dumpDeferredLength(Records: Data))
127 return Err;
128 break;
129 case GOFF::RT_END:
130 if (auto Err = dumpEnd(Records: Data))
131 return Err;
132 break;
133 }
134 }
135 return Err;
136}
137
138GOFFYAML::Object &GOFFDumper::getYAMLObj() { return YAMLObj; }
139
140Error goff2yaml(raw_ostream &Out, const llvm::object::GOFFObjectFile &Obj) {
141 GOFFDumper Dumper(Obj);
142
143 if (auto Err = Dumper.dump())
144 return Err;
145
146 yaml::Output Yout(Out);
147 Yout << Dumper.getYAMLObj();
148
149 return Error::success();
150}
151