1//===-- XCOFFYAML.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// This file defines classes for handling the YAML representation of XCOFF.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ObjectYAML/XCOFFYAML.h"
14#include "llvm/BinaryFormat/XCOFF.h"
15#include <string.h>
16
17namespace llvm {
18namespace XCOFFYAML {
19
20Object::Object() { memset(s: &Header, c: 0, n: sizeof(Header)); }
21
22AuxSymbolEnt::~AuxSymbolEnt() = default;
23
24} // namespace XCOFFYAML
25
26namespace yaml {
27
28void ScalarBitSetTraits<XCOFF::SectionTypeFlags>::bitset(
29 IO &IO, XCOFF::SectionTypeFlags &Value) {
30#define ECase(X) IO.bitSetCase(Value, #X, XCOFF::X)
31 ECase(STYP_PAD);
32 ECase(STYP_DWARF);
33 ECase(STYP_TEXT);
34 ECase(STYP_DATA);
35 ECase(STYP_BSS);
36 ECase(STYP_EXCEPT);
37 ECase(STYP_INFO);
38 ECase(STYP_TDATA);
39 ECase(STYP_TBSS);
40 ECase(STYP_LOADER);
41 ECase(STYP_DEBUG);
42 ECase(STYP_TYPCHK);
43 ECase(STYP_OVRFLO);
44#undef ECase
45}
46
47void ScalarEnumerationTraits<XCOFF::DwarfSectionSubtypeFlags>::enumeration(
48 IO &IO, XCOFF::DwarfSectionSubtypeFlags &Value) {
49#define ECase(X) IO.enumCase(Value, #X, XCOFF::X)
50 ECase(SSUBTYP_DWINFO);
51 ECase(SSUBTYP_DWLINE);
52 ECase(SSUBTYP_DWPBNMS);
53 ECase(SSUBTYP_DWPBTYP);
54 ECase(SSUBTYP_DWARNGE);
55 ECase(SSUBTYP_DWABREV);
56 ECase(SSUBTYP_DWSTR);
57 ECase(SSUBTYP_DWRNGES);
58 ECase(SSUBTYP_DWLOC);
59 ECase(SSUBTYP_DWFRAME);
60 ECase(SSUBTYP_DWMAC);
61#undef ECase
62 IO.enumFallback<Hex32>(Val&: Value);
63}
64
65void ScalarEnumerationTraits<XCOFF::StorageClass>::enumeration(
66 IO &IO, XCOFF::StorageClass &Value) {
67#define ECase(X) IO.enumCase(Value, #X, XCOFF::X)
68 ECase(C_NULL);
69 ECase(C_AUTO);
70 ECase(C_EXT);
71 ECase(C_STAT);
72 ECase(C_REG);
73 ECase(C_EXTDEF);
74 ECase(C_LABEL);
75 ECase(C_ULABEL);
76 ECase(C_MOS);
77 ECase(C_ARG);
78 ECase(C_STRTAG);
79 ECase(C_MOU);
80 ECase(C_UNTAG);
81 ECase(C_TPDEF);
82 ECase(C_USTATIC);
83 ECase(C_ENTAG);
84 ECase(C_MOE);
85 ECase(C_REGPARM);
86 ECase(C_FIELD);
87 ECase(C_BLOCK);
88 ECase(C_FCN);
89 ECase(C_EOS);
90 ECase(C_FILE);
91 ECase(C_LINE);
92 ECase(C_ALIAS);
93 ECase(C_HIDDEN);
94 ECase(C_HIDEXT);
95 ECase(C_BINCL);
96 ECase(C_EINCL);
97 ECase(C_INFO);
98 ECase(C_WEAKEXT);
99 ECase(C_DWARF);
100 ECase(C_GSYM);
101 ECase(C_LSYM);
102 ECase(C_PSYM);
103 ECase(C_RSYM);
104 ECase(C_RPSYM);
105 ECase(C_STSYM);
106 ECase(C_TCSYM);
107 ECase(C_BCOMM);
108 ECase(C_ECOML);
109 ECase(C_ECOMM);
110 ECase(C_DECL);
111 ECase(C_ENTRY);
112 ECase(C_FUN);
113 ECase(C_BSTAT);
114 ECase(C_ESTAT);
115 ECase(C_GTLS);
116 ECase(C_STTLS);
117 ECase(C_EFCN);
118#undef ECase
119}
120
121void ScalarEnumerationTraits<XCOFF::StorageMappingClass>::enumeration(
122 IO &IO, XCOFF::StorageMappingClass &Value) {
123#define ECase(X) IO.enumCase(Value, #X, XCOFF::X)
124 ECase(XMC_PR);
125 ECase(XMC_RO);
126 ECase(XMC_DB);
127 ECase(XMC_GL);
128 ECase(XMC_XO);
129 ECase(XMC_SV);
130 ECase(XMC_SV64);
131 ECase(XMC_SV3264);
132 ECase(XMC_TI);
133 ECase(XMC_TB);
134 ECase(XMC_RW);
135 ECase(XMC_TC0);
136 ECase(XMC_TC);
137 ECase(XMC_TD);
138 ECase(XMC_DS);
139 ECase(XMC_UA);
140 ECase(XMC_BS);
141 ECase(XMC_UC);
142 ECase(XMC_TL);
143 ECase(XMC_UL);
144 ECase(XMC_TE);
145#undef ECase
146}
147
148void ScalarEnumerationTraits<XCOFF::SymbolType>::enumeration(
149 IO &IO, XCOFF::SymbolType &Value) {
150#define ECase(X) IO.enumCase(Value, #X, XCOFF::X)
151 ECase(XTY_ER);
152 ECase(XTY_SD);
153 ECase(XTY_LD);
154 ECase(XTY_CM);
155#undef ECase
156 IO.enumFallback<Hex8>(Val&: Value);
157}
158
159void ScalarEnumerationTraits<XCOFFYAML::AuxSymbolType>::enumeration(
160 IO &IO, XCOFFYAML::AuxSymbolType &Type) {
161#define ECase(X) IO.enumCase(Type, #X, XCOFFYAML::X)
162 ECase(AUX_EXCEPT);
163 ECase(AUX_FCN);
164 ECase(AUX_SYM);
165 ECase(AUX_FILE);
166 ECase(AUX_CSECT);
167 ECase(AUX_SECT);
168 ECase(AUX_STAT);
169#undef ECase
170}
171
172void ScalarEnumerationTraits<XCOFF::CFileStringType>::enumeration(
173 IO &IO, XCOFF::CFileStringType &Type) {
174#define ECase(X) IO.enumCase(Type, #X, XCOFF::X)
175 ECase(XFT_FN);
176 ECase(XFT_CT);
177 ECase(XFT_CV);
178 ECase(XFT_CD);
179#undef ECase
180}
181
182struct NSectionFlags {
183 NSectionFlags(IO &) : Flags(XCOFF::SectionTypeFlags(0)) {}
184 NSectionFlags(IO &, uint32_t C) : Flags(XCOFF::SectionTypeFlags(C)) {}
185
186 uint32_t denormalize(IO &) { return Flags; }
187
188 XCOFF::SectionTypeFlags Flags;
189};
190
191void MappingTraits<XCOFFYAML::FileHeader>::mapping(
192 IO &IO, XCOFFYAML::FileHeader &FileHdr) {
193 IO.mapOptional(Key: "MagicNumber", Val&: FileHdr.Magic);
194 IO.mapOptional(Key: "NumberOfSections", Val&: FileHdr.NumberOfSections);
195 IO.mapOptional(Key: "CreationTime", Val&: FileHdr.TimeStamp);
196 IO.mapOptional(Key: "OffsetToSymbolTable", Val&: FileHdr.SymbolTableOffset);
197 IO.mapOptional(Key: "EntriesInSymbolTable", Val&: FileHdr.NumberOfSymTableEntries);
198 IO.mapOptional(Key: "AuxiliaryHeaderSize", Val&: FileHdr.AuxHeaderSize);
199 IO.mapOptional(Key: "Flags", Val&: FileHdr.Flags);
200}
201
202void MappingTraits<XCOFFYAML::AuxiliaryHeader>::mapping(
203 IO &IO, XCOFFYAML::AuxiliaryHeader &AuxHdr) {
204 IO.mapOptional(Key: "Magic", Val&: AuxHdr.Magic);
205 IO.mapOptional(Key: "Version", Val&: AuxHdr.Version);
206 IO.mapOptional(Key: "TextStartAddr", Val&: AuxHdr.TextStartAddr);
207 IO.mapOptional(Key: "DataStartAddr", Val&: AuxHdr.DataStartAddr);
208 IO.mapOptional(Key: "TOCAnchorAddr", Val&: AuxHdr.TOCAnchorAddr);
209 IO.mapOptional(Key: "TextSectionSize", Val&: AuxHdr.TextSize);
210 IO.mapOptional(Key: "DataSectionSize", Val&: AuxHdr.InitDataSize);
211 IO.mapOptional(Key: "BssSectionSize", Val&: AuxHdr.BssDataSize);
212 IO.mapOptional(Key: "SecNumOfEntryPoint", Val&: AuxHdr.SecNumOfEntryPoint);
213 IO.mapOptional(Key: "SecNumOfText", Val&: AuxHdr.SecNumOfText);
214 IO.mapOptional(Key: "SecNumOfData", Val&: AuxHdr.SecNumOfData);
215 IO.mapOptional(Key: "SecNumOfTOC", Val&: AuxHdr.SecNumOfTOC);
216 IO.mapOptional(Key: "SecNumOfLoader", Val&: AuxHdr.SecNumOfLoader);
217 IO.mapOptional(Key: "SecNumOfBSS", Val&: AuxHdr.SecNumOfBSS);
218 IO.mapOptional(Key: "MaxAlignOfText", Val&: AuxHdr.MaxAlignOfText);
219 IO.mapOptional(Key: "MaxAlignOfData", Val&: AuxHdr.MaxAlignOfData);
220 IO.mapOptional(Key: "ModuleType", Val&: AuxHdr.CpuFlag);
221 IO.mapOptional(Key: "TextPageSize", Val&: AuxHdr.TextPageSize);
222 IO.mapOptional(Key: "DataPageSize", Val&: AuxHdr.DataPageSize);
223 IO.mapOptional(Key: "StackPageSize", Val&: AuxHdr.StackPageSize);
224 IO.mapOptional(Key: "FlagAndTDataAlignment", Val&: AuxHdr.FlagAndTDataAlignment);
225 IO.mapOptional(Key: "EntryPointAddr", Val&: AuxHdr.EntryPointAddr);
226 IO.mapOptional(Key: "MaxStackSize", Val&: AuxHdr.MaxStackSize);
227 IO.mapOptional(Key: "MaxDataSize", Val&: AuxHdr.MaxDataSize);
228 IO.mapOptional(Key: "SecNumOfTData", Val&: AuxHdr.SecNumOfTData);
229 IO.mapOptional(Key: "SecNumOfTBSS", Val&: AuxHdr.SecNumOfTBSS);
230 IO.mapOptional(Key: "Flag", Val&: AuxHdr.Flag);
231}
232
233void MappingTraits<XCOFFYAML::Relocation>::mapping(IO &IO,
234 XCOFFYAML::Relocation &R) {
235 IO.mapOptional(Key: "Address", Val&: R.VirtualAddress);
236 IO.mapOptional(Key: "Symbol", Val&: R.SymbolIndex);
237 IO.mapOptional(Key: "Info", Val&: R.Info);
238 IO.mapOptional(Key: "Type", Val&: R.Type);
239}
240
241void MappingTraits<XCOFFYAML::Section>::mapping(IO &IO,
242 XCOFFYAML::Section &Sec) {
243 MappingNormalization<NSectionFlags, uint32_t> NC(IO, Sec.Flags);
244 IO.mapOptional(Key: "Name", Val&: Sec.SectionName);
245 IO.mapOptional(Key: "Address", Val&: Sec.Address);
246 IO.mapOptional(Key: "Size", Val&: Sec.Size);
247 IO.mapOptional(Key: "FileOffsetToData", Val&: Sec.FileOffsetToData);
248 IO.mapOptional(Key: "FileOffsetToRelocations", Val&: Sec.FileOffsetToRelocations);
249 IO.mapOptional(Key: "FileOffsetToLineNumbers", Val&: Sec.FileOffsetToLineNumbers);
250 IO.mapOptional(Key: "NumberOfRelocations", Val&: Sec.NumberOfRelocations);
251 IO.mapOptional(Key: "NumberOfLineNumbers", Val&: Sec.NumberOfLineNumbers);
252 IO.mapOptional(Key: "Flags", Val&: NC->Flags);
253 IO.mapOptional(Key: "DWARFSectionSubtype", Val&: Sec.SectionSubtype);
254 IO.mapOptional(Key: "SectionData", Val&: Sec.SectionData);
255 IO.mapOptional(Key: "Relocations", Val&: Sec.Relocations);
256}
257
258static void auxSymMapping(IO &IO, XCOFFYAML::CsectAuxEnt &AuxSym, bool Is64) {
259 IO.mapOptional(Key: "ParameterHashIndex", Val&: AuxSym.ParameterHashIndex);
260 IO.mapOptional(Key: "TypeChkSectNum", Val&: AuxSym.TypeChkSectNum);
261 IO.mapOptional(Key: "SymbolAlignmentAndType", Val&: AuxSym.SymbolAlignmentAndType);
262 IO.mapOptional(Key: "SymbolType", Val&: AuxSym.SymbolType);
263 IO.mapOptional(Key: "SymbolAlignment", Val&: AuxSym.SymbolAlignment);
264 IO.mapOptional(Key: "StorageMappingClass", Val&: AuxSym.StorageMappingClass);
265 if (Is64) {
266 IO.mapOptional(Key: "SectionOrLengthLo", Val&: AuxSym.SectionOrLengthLo);
267 IO.mapOptional(Key: "SectionOrLengthHi", Val&: AuxSym.SectionOrLengthHi);
268 } else {
269 IO.mapOptional(Key: "SectionOrLength", Val&: AuxSym.SectionOrLength);
270 IO.mapOptional(Key: "StabInfoIndex", Val&: AuxSym.StabInfoIndex);
271 IO.mapOptional(Key: "StabSectNum", Val&: AuxSym.StabSectNum);
272 }
273}
274
275static void auxSymMapping(IO &IO, XCOFFYAML::FileAuxEnt &AuxSym) {
276 IO.mapOptional(Key: "FileNameOrString", Val&: AuxSym.FileNameOrString);
277 IO.mapOptional(Key: "FileStringType", Val&: AuxSym.FileStringType);
278}
279
280static void auxSymMapping(IO &IO, XCOFFYAML::BlockAuxEnt &AuxSym, bool Is64) {
281 if (Is64) {
282 IO.mapOptional(Key: "LineNum", Val&: AuxSym.LineNum);
283 } else {
284 IO.mapOptional(Key: "LineNumHi", Val&: AuxSym.LineNumHi);
285 IO.mapOptional(Key: "LineNumLo", Val&: AuxSym.LineNumLo);
286 }
287}
288
289static void auxSymMapping(IO &IO, XCOFFYAML::FunctionAuxEnt &AuxSym,
290 bool Is64) {
291 if (!Is64)
292 IO.mapOptional(Key: "OffsetToExceptionTbl", Val&: AuxSym.OffsetToExceptionTbl);
293 IO.mapOptional(Key: "SizeOfFunction", Val&: AuxSym.SizeOfFunction);
294 IO.mapOptional(Key: "SymIdxOfNextBeyond", Val&: AuxSym.SymIdxOfNextBeyond);
295 IO.mapOptional(Key: "PtrToLineNum", Val&: AuxSym.PtrToLineNum);
296}
297
298static void auxSymMapping(IO &IO, XCOFFYAML::ExcpetionAuxEnt &AuxSym) {
299 IO.mapOptional(Key: "OffsetToExceptionTbl", Val&: AuxSym.OffsetToExceptionTbl);
300 IO.mapOptional(Key: "SizeOfFunction", Val&: AuxSym.SizeOfFunction);
301 IO.mapOptional(Key: "SymIdxOfNextBeyond", Val&: AuxSym.SymIdxOfNextBeyond);
302}
303
304static void auxSymMapping(IO &IO, XCOFFYAML::SectAuxEntForDWARF &AuxSym) {
305 IO.mapOptional(Key: "LengthOfSectionPortion", Val&: AuxSym.LengthOfSectionPortion);
306 IO.mapOptional(Key: "NumberOfRelocEnt", Val&: AuxSym.NumberOfRelocEnt);
307}
308
309static void auxSymMapping(IO &IO, XCOFFYAML::SectAuxEntForStat &AuxSym) {
310 IO.mapOptional(Key: "SectionLength", Val&: AuxSym.SectionLength);
311 IO.mapOptional(Key: "NumberOfRelocEnt", Val&: AuxSym.NumberOfRelocEnt);
312 IO.mapOptional(Key: "NumberOfLineNum", Val&: AuxSym.NumberOfLineNum);
313}
314
315template <typename AuxEntT>
316static void ResetAuxSym(IO &IO,
317 std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym) {
318 if (!IO.outputting())
319 AuxSym.reset(p: new AuxEntT);
320}
321
322void MappingTraits<std::unique_ptr<XCOFFYAML::AuxSymbolEnt>>::mapping(
323 IO &IO, std::unique_ptr<XCOFFYAML::AuxSymbolEnt> &AuxSym) {
324 const bool Is64 =
325 static_cast<XCOFFYAML::Object *>(IO.getContext())->Header.Magic ==
326 (llvm::yaml::Hex16)XCOFF::XCOFF64;
327
328 XCOFFYAML::AuxSymbolType AuxType;
329 if (IO.outputting())
330 AuxType = AuxSym->Type;
331 IO.mapRequired(Key: "Type", Val&: AuxType);
332 switch (AuxType) {
333 case XCOFFYAML::AUX_EXCEPT:
334 if (!Is64) {
335 IO.setError("an auxiliary symbol of type AUX_EXCEPT cannot be defined in "
336 "XCOFF32");
337 return;
338 }
339 ResetAuxSym<XCOFFYAML::ExcpetionAuxEnt>(IO, AuxSym);
340 auxSymMapping(IO, AuxSym&: *cast<XCOFFYAML::ExcpetionAuxEnt>(Val: AuxSym.get()));
341 break;
342 case XCOFFYAML::AUX_FCN:
343 ResetAuxSym<XCOFFYAML::FunctionAuxEnt>(IO, AuxSym);
344 auxSymMapping(IO, AuxSym&: *cast<XCOFFYAML::FunctionAuxEnt>(Val: AuxSym.get()), Is64);
345 break;
346 case XCOFFYAML::AUX_SYM:
347 ResetAuxSym<XCOFFYAML::BlockAuxEnt>(IO, AuxSym);
348 auxSymMapping(IO, AuxSym&: *cast<XCOFFYAML::BlockAuxEnt>(Val: AuxSym.get()), Is64);
349 break;
350 case XCOFFYAML::AUX_FILE:
351 ResetAuxSym<XCOFFYAML::FileAuxEnt>(IO, AuxSym);
352 auxSymMapping(IO, AuxSym&: *cast<XCOFFYAML::FileAuxEnt>(Val: AuxSym.get()));
353 break;
354 case XCOFFYAML::AUX_CSECT:
355 ResetAuxSym<XCOFFYAML::CsectAuxEnt>(IO, AuxSym);
356 auxSymMapping(IO, AuxSym&: *cast<XCOFFYAML::CsectAuxEnt>(Val: AuxSym.get()), Is64);
357 break;
358 case XCOFFYAML::AUX_SECT:
359 ResetAuxSym<XCOFFYAML::SectAuxEntForDWARF>(IO, AuxSym);
360 auxSymMapping(IO, AuxSym&: *cast<XCOFFYAML::SectAuxEntForDWARF>(Val: AuxSym.get()));
361 break;
362 case XCOFFYAML::AUX_STAT:
363 if (Is64) {
364 IO.setError(
365 "an auxiliary symbol of type AUX_STAT cannot be defined in XCOFF64");
366 return;
367 }
368 ResetAuxSym<XCOFFYAML::SectAuxEntForStat>(IO, AuxSym);
369 auxSymMapping(IO, AuxSym&: *cast<XCOFFYAML::SectAuxEntForStat>(Val: AuxSym.get()));
370 break;
371 }
372}
373
374void MappingTraits<XCOFFYAML::Symbol>::mapping(IO &IO, XCOFFYAML::Symbol &S) {
375 IO.mapOptional(Key: "Name", Val&: S.SymbolName);
376 IO.mapOptional(Key: "Value", Val&: S.Value);
377 IO.mapOptional(Key: "Section", Val&: S.SectionName);
378 IO.mapOptional(Key: "SectionIndex", Val&: S.SectionIndex);
379 IO.mapOptional(Key: "Type", Val&: S.Type);
380 IO.mapOptional(Key: "StorageClass", Val&: S.StorageClass);
381 IO.mapOptional(Key: "NumberOfAuxEntries", Val&: S.NumberOfAuxEntries);
382 IO.mapOptional(Key: "AuxEntries", Val&: S.AuxEntries);
383}
384
385void MappingTraits<XCOFFYAML::StringTable>::mapping(
386 IO &IO, XCOFFYAML::StringTable &Str) {
387 IO.mapOptional(Key: "ContentSize", Val&: Str.ContentSize);
388 IO.mapOptional(Key: "Length", Val&: Str.Length);
389 IO.mapOptional(Key: "Strings", Val&: Str.Strings);
390 IO.mapOptional(Key: "RawContent", Val&: Str.RawContent);
391}
392
393void MappingTraits<XCOFFYAML::Object>::mapping(IO &IO, XCOFFYAML::Object &Obj) {
394 IO.setContext(&Obj);
395 IO.mapTag(Tag: "!XCOFF", Default: true);
396 IO.mapRequired(Key: "FileHeader", Val&: Obj.Header);
397 IO.mapOptional(Key: "AuxiliaryHeader", Val&: Obj.AuxHeader);
398 IO.mapOptional(Key: "Sections", Val&: Obj.Sections);
399 IO.mapOptional(Key: "Symbols", Val&: Obj.Symbols);
400 IO.mapOptional(Key: "StringTable", Val&: Obj.StrTbl);
401 IO.setContext(nullptr);
402}
403
404} // namespace yaml
405} // namespace llvm
406