1 //===-- XCOFFYAML.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 //===----------------------------------------------------------------------===//
9 // This file defines classes for handling the YAML representation of XCOFF.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ObjectYAML/XCOFFYAML.h"
14 #include "llvm/BinaryFormat/XCOFF.h"
20 Object::Object() { memset(&Header
, 0, sizeof(Header
)); }
22 AuxSymbolEnt::~AuxSymbolEnt() = default;
24 } // namespace XCOFFYAML
28 void ScalarBitSetTraits
<XCOFF::SectionTypeFlags
>::bitset(
29 IO
&IO
, XCOFF::SectionTypeFlags
&Value
) {
30 #define ECase(X) IO.bitSetCase(Value, #X, XCOFF::X)
47 void ScalarEnumerationTraits
<XCOFF::StorageClass
>::enumeration(
48 IO
&IO
, XCOFF::StorageClass
&Value
) {
49 #define ECase(X) IO.enumCase(Value, #X, XCOFF::X)
103 void ScalarEnumerationTraits
<XCOFF::StorageMappingClass
>::enumeration(
104 IO
&IO
, XCOFF::StorageMappingClass
&Value
) {
105 #define ECase(X) IO.enumCase(Value, #X, XCOFF::X)
130 void ScalarEnumerationTraits
<XCOFFYAML::AuxSymbolType
>::enumeration(
131 IO
&IO
, XCOFFYAML::AuxSymbolType
&Type
) {
132 #define ECase(X) IO.enumCase(Type, #X, XCOFFYAML::X)
143 void ScalarEnumerationTraits
<XCOFF::CFileStringType
>::enumeration(
144 IO
&IO
, XCOFF::CFileStringType
&Type
) {
145 #define ECase(X) IO.enumCase(Type, #X, XCOFF::X)
153 struct NSectionFlags
{
154 NSectionFlags(IO
&) : Flags(XCOFF::SectionTypeFlags(0)) {}
155 NSectionFlags(IO
&, uint32_t C
) : Flags(XCOFF::SectionTypeFlags(C
)) {}
157 uint32_t denormalize(IO
&) { return Flags
; }
159 XCOFF::SectionTypeFlags Flags
;
162 void MappingTraits
<XCOFFYAML::FileHeader
>::mapping(
163 IO
&IO
, XCOFFYAML::FileHeader
&FileHdr
) {
164 IO
.mapOptional("MagicNumber", FileHdr
.Magic
);
165 IO
.mapOptional("NumberOfSections", FileHdr
.NumberOfSections
);
166 IO
.mapOptional("CreationTime", FileHdr
.TimeStamp
);
167 IO
.mapOptional("OffsetToSymbolTable", FileHdr
.SymbolTableOffset
);
168 IO
.mapOptional("EntriesInSymbolTable", FileHdr
.NumberOfSymTableEntries
);
169 IO
.mapOptional("AuxiliaryHeaderSize", FileHdr
.AuxHeaderSize
);
170 IO
.mapOptional("Flags", FileHdr
.Flags
);
173 void MappingTraits
<XCOFFYAML::AuxiliaryHeader
>::mapping(
174 IO
&IO
, XCOFFYAML::AuxiliaryHeader
&AuxHdr
) {
175 IO
.mapOptional("Magic", AuxHdr
.Magic
);
176 IO
.mapOptional("Version", AuxHdr
.Version
);
177 IO
.mapOptional("TextStartAddr", AuxHdr
.TextStartAddr
);
178 IO
.mapOptional("DataStartAddr", AuxHdr
.DataStartAddr
);
179 IO
.mapOptional("TOCAnchorAddr", AuxHdr
.TOCAnchorAddr
);
180 IO
.mapOptional("TextSectionSize", AuxHdr
.TextSize
);
181 IO
.mapOptional("DataSectionSize", AuxHdr
.InitDataSize
);
182 IO
.mapOptional("BssSectionSize", AuxHdr
.BssDataSize
);
183 IO
.mapOptional("SecNumOfEntryPoint", AuxHdr
.SecNumOfEntryPoint
);
184 IO
.mapOptional("SecNumOfText", AuxHdr
.SecNumOfText
);
185 IO
.mapOptional("SecNumOfData", AuxHdr
.SecNumOfData
);
186 IO
.mapOptional("SecNumOfTOC", AuxHdr
.SecNumOfTOC
);
187 IO
.mapOptional("SecNumOfLoader", AuxHdr
.SecNumOfLoader
);
188 IO
.mapOptional("SecNumOfBSS", AuxHdr
.SecNumOfBSS
);
189 IO
.mapOptional("MaxAlignOfText", AuxHdr
.MaxAlignOfText
);
190 IO
.mapOptional("MaxAlignOfData", AuxHdr
.MaxAlignOfData
);
191 IO
.mapOptional("ModuleType", AuxHdr
.CpuFlag
);
192 IO
.mapOptional("TextPageSize", AuxHdr
.TextPageSize
);
193 IO
.mapOptional("DataPageSize", AuxHdr
.DataPageSize
);
194 IO
.mapOptional("StackPageSize", AuxHdr
.StackPageSize
);
195 IO
.mapOptional("FlagAndTDataAlignment", AuxHdr
.FlagAndTDataAlignment
);
196 IO
.mapOptional("EntryPointAddr", AuxHdr
.EntryPointAddr
);
197 IO
.mapOptional("MaxStackSize", AuxHdr
.MaxStackSize
);
198 IO
.mapOptional("MaxDataSize", AuxHdr
.MaxDataSize
);
199 IO
.mapOptional("SecNumOfTData", AuxHdr
.SecNumOfTData
);
200 IO
.mapOptional("SecNumOfTBSS", AuxHdr
.SecNumOfTBSS
);
201 IO
.mapOptional("Flag", AuxHdr
.Flag
);
204 void MappingTraits
<XCOFFYAML::Relocation
>::mapping(IO
&IO
,
205 XCOFFYAML::Relocation
&R
) {
206 IO
.mapOptional("Address", R
.VirtualAddress
);
207 IO
.mapOptional("Symbol", R
.SymbolIndex
);
208 IO
.mapOptional("Info", R
.Info
);
209 IO
.mapOptional("Type", R
.Type
);
212 void MappingTraits
<XCOFFYAML::Section
>::mapping(IO
&IO
,
213 XCOFFYAML::Section
&Sec
) {
214 MappingNormalization
<NSectionFlags
, uint32_t> NC(IO
, Sec
.Flags
);
215 IO
.mapOptional("Name", Sec
.SectionName
);
216 IO
.mapOptional("Address", Sec
.Address
);
217 IO
.mapOptional("Size", Sec
.Size
);
218 IO
.mapOptional("FileOffsetToData", Sec
.FileOffsetToData
);
219 IO
.mapOptional("FileOffsetToRelocations", Sec
.FileOffsetToRelocations
);
220 IO
.mapOptional("FileOffsetToLineNumbers", Sec
.FileOffsetToLineNumbers
);
221 IO
.mapOptional("NumberOfRelocations", Sec
.NumberOfRelocations
);
222 IO
.mapOptional("NumberOfLineNumbers", Sec
.NumberOfLineNumbers
);
223 IO
.mapOptional("Flags", NC
->Flags
);
224 IO
.mapOptional("SectionData", Sec
.SectionData
);
225 IO
.mapOptional("Relocations", Sec
.Relocations
);
228 static void auxSymMapping(IO
&IO
, XCOFFYAML::CsectAuxEnt
&AuxSym
, bool Is64
) {
229 IO
.mapOptional("ParameterHashIndex", AuxSym
.ParameterHashIndex
);
230 IO
.mapOptional("TypeChkSectNum", AuxSym
.TypeChkSectNum
);
231 IO
.mapOptional("SymbolAlignmentAndType", AuxSym
.SymbolAlignmentAndType
);
232 IO
.mapOptional("StorageMappingClass", AuxSym
.StorageMappingClass
);
234 IO
.mapOptional("SectionOrLengthLo", AuxSym
.SectionOrLengthLo
);
235 IO
.mapOptional("SectionOrLengthHi", AuxSym
.SectionOrLengthHi
);
237 IO
.mapOptional("SectionOrLength", AuxSym
.SectionOrLength
);
238 IO
.mapOptional("StabInfoIndex", AuxSym
.StabInfoIndex
);
239 IO
.mapOptional("StabSectNum", AuxSym
.StabSectNum
);
243 static void auxSymMapping(IO
&IO
, XCOFFYAML::FileAuxEnt
&AuxSym
) {
244 IO
.mapOptional("FileNameOrString", AuxSym
.FileNameOrString
);
245 IO
.mapOptional("FileStringType", AuxSym
.FileStringType
);
248 static void auxSymMapping(IO
&IO
, XCOFFYAML::BlockAuxEnt
&AuxSym
, bool Is64
) {
250 IO
.mapOptional("LineNum", AuxSym
.LineNum
);
252 IO
.mapOptional("LineNumHi", AuxSym
.LineNumHi
);
253 IO
.mapOptional("LineNumLo", AuxSym
.LineNumLo
);
257 static void auxSymMapping(IO
&IO
, XCOFFYAML::FunctionAuxEnt
&AuxSym
,
260 IO
.mapOptional("OffsetToExceptionTbl", AuxSym
.OffsetToExceptionTbl
);
261 IO
.mapOptional("SizeOfFunction", AuxSym
.SizeOfFunction
);
262 IO
.mapOptional("SymIdxOfNextBeyond", AuxSym
.SymIdxOfNextBeyond
);
263 IO
.mapOptional("PtrToLineNum", AuxSym
.PtrToLineNum
);
266 static void auxSymMapping(IO
&IO
, XCOFFYAML::ExcpetionAuxEnt
&AuxSym
) {
267 IO
.mapOptional("OffsetToExceptionTbl", AuxSym
.OffsetToExceptionTbl
);
268 IO
.mapOptional("SizeOfFunction", AuxSym
.SizeOfFunction
);
269 IO
.mapOptional("SymIdxOfNextBeyond", AuxSym
.SymIdxOfNextBeyond
);
272 static void auxSymMapping(IO
&IO
, XCOFFYAML::SectAuxEntForDWARF
&AuxSym
) {
273 IO
.mapOptional("LengthOfSectionPortion", AuxSym
.LengthOfSectionPortion
);
274 IO
.mapOptional("NumberOfRelocEnt", AuxSym
.NumberOfRelocEnt
);
277 static void auxSymMapping(IO
&IO
, XCOFFYAML::SectAuxEntForStat
&AuxSym
) {
278 IO
.mapOptional("SectionLength", AuxSym
.SectionLength
);
279 IO
.mapOptional("NumberOfRelocEnt", AuxSym
.NumberOfRelocEnt
);
280 IO
.mapOptional("NumberOfLineNum", AuxSym
.NumberOfLineNum
);
283 template <typename AuxEntT
>
284 static void ResetAuxSym(IO
&IO
,
285 std::unique_ptr
<XCOFFYAML::AuxSymbolEnt
> &AuxSym
) {
286 if (!IO
.outputting())
287 AuxSym
.reset(new AuxEntT
);
290 void MappingTraits
<std::unique_ptr
<XCOFFYAML::AuxSymbolEnt
>>::mapping(
291 IO
&IO
, std::unique_ptr
<XCOFFYAML::AuxSymbolEnt
> &AuxSym
) {
293 static_cast<XCOFFYAML::Object
*>(IO
.getContext())->Header
.Magic
==
294 (llvm::yaml::Hex16
)XCOFF::XCOFF64
;
296 XCOFFYAML::AuxSymbolType AuxType
;
298 AuxType
= AuxSym
.get()->Type
;
299 IO
.mapRequired("Type", AuxType
);
301 case XCOFFYAML::AUX_EXCEPT
:
303 IO
.setError("an auxiliary symbol of type AUX_EXCEPT cannot be defined in "
307 ResetAuxSym
<XCOFFYAML::ExcpetionAuxEnt
>(IO
, AuxSym
);
308 auxSymMapping(IO
, *cast
<XCOFFYAML::ExcpetionAuxEnt
>(AuxSym
.get()));
310 case XCOFFYAML::AUX_FCN
:
311 ResetAuxSym
<XCOFFYAML::FunctionAuxEnt
>(IO
, AuxSym
);
312 auxSymMapping(IO
, *cast
<XCOFFYAML::FunctionAuxEnt
>(AuxSym
.get()), Is64
);
314 case XCOFFYAML::AUX_SYM
:
315 ResetAuxSym
<XCOFFYAML::BlockAuxEnt
>(IO
, AuxSym
);
316 auxSymMapping(IO
, *cast
<XCOFFYAML::BlockAuxEnt
>(AuxSym
.get()), Is64
);
318 case XCOFFYAML::AUX_FILE
:
319 ResetAuxSym
<XCOFFYAML::FileAuxEnt
>(IO
, AuxSym
);
320 auxSymMapping(IO
, *cast
<XCOFFYAML::FileAuxEnt
>(AuxSym
.get()));
322 case XCOFFYAML::AUX_CSECT
:
323 ResetAuxSym
<XCOFFYAML::CsectAuxEnt
>(IO
, AuxSym
);
324 auxSymMapping(IO
, *cast
<XCOFFYAML::CsectAuxEnt
>(AuxSym
.get()), Is64
);
326 case XCOFFYAML::AUX_SECT
:
327 ResetAuxSym
<XCOFFYAML::SectAuxEntForDWARF
>(IO
, AuxSym
);
328 auxSymMapping(IO
, *cast
<XCOFFYAML::SectAuxEntForDWARF
>(AuxSym
.get()));
330 case XCOFFYAML::AUX_STAT
:
333 "an auxiliary symbol of type AUX_STAT cannot be defined in XCOFF64");
336 ResetAuxSym
<XCOFFYAML::SectAuxEntForStat
>(IO
, AuxSym
);
337 auxSymMapping(IO
, *cast
<XCOFFYAML::SectAuxEntForStat
>(AuxSym
.get()));
342 void MappingTraits
<XCOFFYAML::Symbol
>::mapping(IO
&IO
, XCOFFYAML::Symbol
&S
) {
343 IO
.mapOptional("Name", S
.SymbolName
);
344 IO
.mapOptional("Value", S
.Value
);
345 IO
.mapOptional("Section", S
.SectionName
);
346 IO
.mapOptional("SectionIndex", S
.SectionIndex
);
347 IO
.mapOptional("Type", S
.Type
);
348 IO
.mapOptional("StorageClass", S
.StorageClass
);
349 IO
.mapOptional("NumberOfAuxEntries", S
.NumberOfAuxEntries
);
350 IO
.mapOptional("AuxEntries", S
.AuxEntries
);
353 void MappingTraits
<XCOFFYAML::StringTable
>::mapping(IO
&IO
, XCOFFYAML::StringTable
&Str
) {
354 IO
.mapOptional("ContentSize", Str
.ContentSize
);
355 IO
.mapOptional("Length", Str
.Length
);
356 IO
.mapOptional("Strings", Str
.Strings
);
357 IO
.mapOptional("RawContent", Str
.RawContent
);
360 void MappingTraits
<XCOFFYAML::Object
>::mapping(IO
&IO
, XCOFFYAML::Object
&Obj
) {
362 IO
.mapTag("!XCOFF", true);
363 IO
.mapRequired("FileHeader", Obj
.Header
);
364 IO
.mapOptional("AuxiliaryHeader", Obj
.AuxHeader
);
365 IO
.mapOptional("Sections", Obj
.Sections
);
366 IO
.mapOptional("Symbols", Obj
.Symbols
);
367 IO
.mapOptional("StringTable", Obj
.StrTbl
);
368 IO
.setContext(nullptr);