Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:21

0001 //===- COFFYAML.h - COFF YAMLIO implementation ------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares classes for handling the YAML representation of COFF.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_OBJECTYAML_COFFYAML_H
0014 #define LLVM_OBJECTYAML_COFFYAML_H
0015 
0016 #include "llvm/ADT/StringRef.h"
0017 #include "llvm/BinaryFormat/COFF.h"
0018 #include "llvm/Object/COFF.h"
0019 #include "llvm/ObjectYAML/CodeViewYAMLDebugSections.h"
0020 #include "llvm/ObjectYAML/CodeViewYAMLTypeHashing.h"
0021 #include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
0022 #include "llvm/ObjectYAML/YAML.h"
0023 #include <cstdint>
0024 #include <optional>
0025 #include <vector>
0026 
0027 namespace llvm {
0028 
0029 namespace COFF {
0030 
0031 inline Characteristics operator|(Characteristics a, Characteristics b) {
0032   uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
0033   return static_cast<Characteristics>(Ret);
0034 }
0035 
0036 inline SectionCharacteristics operator|(SectionCharacteristics a,
0037                                         SectionCharacteristics b) {
0038   uint32_t Ret = static_cast<uint32_t>(a) | static_cast<uint32_t>(b);
0039   return static_cast<SectionCharacteristics>(Ret);
0040 }
0041 
0042 inline DLLCharacteristics operator|(DLLCharacteristics a,
0043                                     DLLCharacteristics b) {
0044   uint16_t Ret = static_cast<uint16_t>(a) | static_cast<uint16_t>(b);
0045   return static_cast<DLLCharacteristics>(Ret);
0046 }
0047 
0048 } // end namespace COFF
0049 
0050 // The structure of the yaml files is not an exact 1:1 match to COFF. In order
0051 // to use yaml::IO, we use these structures which are closer to the source.
0052 namespace COFFYAML {
0053 
0054 LLVM_YAML_STRONG_TYPEDEF(uint8_t, COMDATType)
0055 LLVM_YAML_STRONG_TYPEDEF(uint32_t, WeakExternalCharacteristics)
0056 LLVM_YAML_STRONG_TYPEDEF(uint8_t, AuxSymbolType)
0057 
0058 struct Relocation {
0059   uint32_t VirtualAddress;
0060   uint16_t Type;
0061 
0062   // Normally a Relocation can refer to the symbol via its name.
0063   // It can also use a direct symbol table index instead (with no name
0064   // specified), allowing disambiguating between multiple symbols with the
0065   // same name or crafting intentionally broken files for testing.
0066   StringRef SymbolName;
0067   std::optional<uint32_t> SymbolTableIndex;
0068 };
0069 
0070 struct SectionDataEntry {
0071   std::optional<uint32_t> UInt32;
0072   yaml::BinaryRef Binary;
0073   std::optional<object::coff_load_configuration32> LoadConfig32;
0074   std::optional<object::coff_load_configuration64> LoadConfig64;
0075 
0076   size_t size() const;
0077   void writeAsBinary(raw_ostream &OS) const;
0078 };
0079 
0080 struct Section {
0081   COFF::section Header;
0082   unsigned Alignment = 0;
0083   yaml::BinaryRef SectionData;
0084   std::vector<CodeViewYAML::YAMLDebugSubsection> DebugS;
0085   std::vector<CodeViewYAML::LeafRecord> DebugT;
0086   std::vector<CodeViewYAML::LeafRecord> DebugP;
0087   std::optional<CodeViewYAML::DebugHSection> DebugH;
0088   std::vector<SectionDataEntry> StructuredData;
0089   std::vector<Relocation> Relocations;
0090   StringRef Name;
0091 
0092   Section();
0093 };
0094 
0095 struct Symbol {
0096   COFF::symbol Header;
0097   COFF::SymbolBaseType SimpleType = COFF::IMAGE_SYM_TYPE_NULL;
0098   COFF::SymbolComplexType ComplexType = COFF::IMAGE_SYM_DTYPE_NULL;
0099   std::optional<COFF::AuxiliaryFunctionDefinition> FunctionDefinition;
0100   std::optional<COFF::AuxiliarybfAndefSymbol> bfAndefSymbol;
0101   std::optional<COFF::AuxiliaryWeakExternal> WeakExternal;
0102   StringRef File;
0103   std::optional<COFF::AuxiliarySectionDefinition> SectionDefinition;
0104   std::optional<COFF::AuxiliaryCLRToken> CLRToken;
0105   StringRef Name;
0106 
0107   Symbol();
0108 };
0109 
0110 struct PEHeader {
0111   COFF::PE32Header Header;
0112   std::optional<COFF::DataDirectory>
0113       DataDirectories[COFF::NUM_DATA_DIRECTORIES];
0114 };
0115 
0116 struct Object {
0117   std::optional<PEHeader> OptionalHeader;
0118   COFF::header Header;
0119   std::vector<Section> Sections;
0120   std::vector<Symbol> Symbols;
0121 
0122   Object();
0123 };
0124 
0125 } // end namespace COFFYAML
0126 
0127 } // end namespace llvm
0128 
0129 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Section)
0130 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Symbol)
0131 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::Relocation)
0132 LLVM_YAML_IS_SEQUENCE_VECTOR(COFFYAML::SectionDataEntry)
0133 
0134 namespace llvm {
0135 namespace yaml {
0136 
0137 template <>
0138 struct ScalarEnumerationTraits<COFFYAML::WeakExternalCharacteristics> {
0139   static void enumeration(IO &IO, COFFYAML::WeakExternalCharacteristics &Value);
0140 };
0141 
0142 template <>
0143 struct ScalarEnumerationTraits<COFFYAML::AuxSymbolType> {
0144   static void enumeration(IO &IO, COFFYAML::AuxSymbolType &Value);
0145 };
0146 
0147 template <>
0148 struct ScalarEnumerationTraits<COFFYAML::COMDATType> {
0149   static void enumeration(IO &IO, COFFYAML::COMDATType &Value);
0150 };
0151 
0152 template <>
0153 struct ScalarEnumerationTraits<COFF::MachineTypes> {
0154   static void enumeration(IO &IO, COFF::MachineTypes &Value);
0155 };
0156 
0157 template <>
0158 struct ScalarEnumerationTraits<COFF::SymbolBaseType> {
0159   static void enumeration(IO &IO, COFF::SymbolBaseType &Value);
0160 };
0161 
0162 template <>
0163 struct ScalarEnumerationTraits<COFF::SymbolStorageClass> {
0164   static void enumeration(IO &IO, COFF::SymbolStorageClass &Value);
0165 };
0166 
0167 template <>
0168 struct ScalarEnumerationTraits<COFF::SymbolComplexType> {
0169   static void enumeration(IO &IO, COFF::SymbolComplexType &Value);
0170 };
0171 
0172 template <>
0173 struct ScalarEnumerationTraits<COFF::RelocationTypeI386> {
0174   static void enumeration(IO &IO, COFF::RelocationTypeI386 &Value);
0175 };
0176 
0177 template <>
0178 struct ScalarEnumerationTraits<COFF::RelocationTypeAMD64> {
0179   static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value);
0180 };
0181 
0182 template <> struct ScalarEnumerationTraits<COFF::RelocationTypesMips> {
0183   static void enumeration(IO &IO, COFF::RelocationTypesMips &Value);
0184 };
0185 
0186 template <>
0187 struct ScalarEnumerationTraits<COFF::RelocationTypesARM> {
0188   static void enumeration(IO &IO, COFF::RelocationTypesARM &Value);
0189 };
0190 
0191 template <>
0192 struct ScalarEnumerationTraits<COFF::RelocationTypesARM64> {
0193   static void enumeration(IO &IO, COFF::RelocationTypesARM64 &Value);
0194 };
0195 
0196 template <>
0197 struct ScalarEnumerationTraits<COFF::WindowsSubsystem> {
0198   static void enumeration(IO &IO, COFF::WindowsSubsystem &Value);
0199 };
0200 
0201 template <>
0202 struct ScalarBitSetTraits<COFF::Characteristics> {
0203   static void bitset(IO &IO, COFF::Characteristics &Value);
0204 };
0205 
0206 template <>
0207 struct ScalarBitSetTraits<COFF::SectionCharacteristics> {
0208   static void bitset(IO &IO, COFF::SectionCharacteristics &Value);
0209 };
0210 
0211 template <>
0212 struct ScalarBitSetTraits<COFF::DLLCharacteristics> {
0213   static void bitset(IO &IO, COFF::DLLCharacteristics &Value);
0214 };
0215 
0216 template <>
0217 struct MappingTraits<COFFYAML::Relocation> {
0218   static void mapping(IO &IO, COFFYAML::Relocation &Rel);
0219 };
0220 
0221 template <>
0222 struct MappingTraits<COFFYAML::PEHeader> {
0223   static void mapping(IO &IO, COFFYAML::PEHeader &PH);
0224 };
0225 
0226 template <>
0227 struct MappingTraits<COFF::DataDirectory> {
0228   static void mapping(IO &IO, COFF::DataDirectory &DD);
0229 };
0230 
0231 template <>
0232 struct MappingTraits<COFF::header> {
0233   static void mapping(IO &IO, COFF::header &H);
0234 };
0235 
0236 template <> struct MappingTraits<COFF::AuxiliaryFunctionDefinition> {
0237   static void mapping(IO &IO, COFF::AuxiliaryFunctionDefinition &AFD);
0238 };
0239 
0240 template <> struct MappingTraits<COFF::AuxiliarybfAndefSymbol> {
0241   static void mapping(IO &IO, COFF::AuxiliarybfAndefSymbol &AAS);
0242 };
0243 
0244 template <> struct MappingTraits<COFF::AuxiliaryWeakExternal> {
0245   static void mapping(IO &IO, COFF::AuxiliaryWeakExternal &AWE);
0246 };
0247 
0248 template <> struct MappingTraits<COFF::AuxiliarySectionDefinition> {
0249   static void mapping(IO &IO, COFF::AuxiliarySectionDefinition &ASD);
0250 };
0251 
0252 template <> struct MappingTraits<COFF::AuxiliaryCLRToken> {
0253   static void mapping(IO &IO, COFF::AuxiliaryCLRToken &ACT);
0254 };
0255 
0256 template <> struct MappingTraits<object::coff_load_configuration32> {
0257   static void mapping(IO &IO, object::coff_load_configuration32 &ACT);
0258 };
0259 
0260 template <> struct MappingTraits<object::coff_load_configuration64> {
0261   static void mapping(IO &IO, object::coff_load_configuration64 &ACT);
0262 };
0263 
0264 template <> struct MappingTraits<object::coff_load_config_code_integrity> {
0265   static void mapping(IO &IO, object::coff_load_config_code_integrity &ACT);
0266 };
0267 
0268 template <>
0269 struct MappingTraits<COFFYAML::Symbol> {
0270   static void mapping(IO &IO, COFFYAML::Symbol &S);
0271 };
0272 
0273 template <> struct MappingTraits<COFFYAML::SectionDataEntry> {
0274   static void mapping(IO &IO, COFFYAML::SectionDataEntry &Sec);
0275 };
0276 
0277 template <>
0278 struct MappingTraits<COFFYAML::Section> {
0279   static void mapping(IO &IO, COFFYAML::Section &Sec);
0280 };
0281 
0282 template <>
0283 struct MappingTraits<COFFYAML::Object> {
0284   static void mapping(IO &IO, COFFYAML::Object &Obj);
0285 };
0286 
0287 } // end namespace yaml
0288 } // end namespace llvm
0289 
0290 #endif // LLVM_OBJECTYAML_COFFYAML_H