1 //===-- llvm/Support/COFF.h -------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains an definitions used in Windows COFF Files. 11 // 12 // Structures and enums defined within this file where created using 13 // information from Microsoft's publicly available PE/COFF format document: 14 // 15 // Microsoft Portable Executable and Common Object File Format Specification 16 // Revision 8.1 - February 15, 2008 17 // 18 // As of 5/2/2010, hosted by Microsoft at: 19 // http://www.microsoft.com/whdc/system/platform/firmware/pecoff.mspx 20 // 21 //===----------------------------------------------------------------------===// 22 23 #ifndef LLVM_SUPPORT_WIN_COFF_H 24 #define LLVM_SUPPORT_WIN_COFF_H 25 26 #include "llvm/Support/DataTypes.h" 27 #include <cstring> 28 29 namespace llvm { 30 namespace COFF { 31 32 // Sizes in bytes of various things in the COFF format. 33 enum { 34 HeaderSize = 20, 35 NameSize = 8, 36 SymbolSize = 18, 37 SectionSize = 40, 38 RelocationSize = 10 39 }; 40 41 struct header { 42 uint16_t Machine; 43 uint16_t NumberOfSections; 44 uint32_t TimeDateStamp; 45 uint32_t PointerToSymbolTable; 46 uint32_t NumberOfSymbols; 47 uint16_t SizeOfOptionalHeader; 48 uint16_t Characteristics; 49 }; 50 51 enum MachineTypes { 52 IMAGE_FILE_MACHINE_I386 = 0x14C, 53 IMAGE_FILE_MACHINE_AMD64 = 0x8664 54 }; 55 56 struct symbol { 57 char Name[NameSize]; 58 uint32_t Value; 59 uint16_t Type; 60 uint8_t StorageClass; 61 uint16_t SectionNumber; 62 uint8_t NumberOfAuxSymbols; 63 }; 64 65 enum SymbolFlags { 66 SF_TypeMask = 0x0000FFFF, 67 SF_TypeShift = 0, 68 69 SF_ClassMask = 0x00FF0000, 70 SF_ClassShift = 16, 71 72 SF_WeakExternal = 0x01000000 73 }; 74 75 enum SymbolSectionNumber { 76 IMAGE_SYM_DEBUG = -2, 77 IMAGE_SYM_ABSOLUTE = -1, 78 IMAGE_SYM_UNDEFINED = 0 79 }; 80 81 /// Storage class tells where and what the symbol represents 82 enum SymbolStorageClass { 83 IMAGE_SYM_CLASS_END_OF_FUNCTION = -1, ///< Physical end of function 84 IMAGE_SYM_CLASS_NULL = 0, ///< No symbol 85 IMAGE_SYM_CLASS_AUTOMATIC = 1, ///< Stack variable 86 IMAGE_SYM_CLASS_EXTERNAL = 2, ///< External symbol 87 IMAGE_SYM_CLASS_STATIC = 3, ///< Static 88 IMAGE_SYM_CLASS_REGISTER = 4, ///< Register variable 89 IMAGE_SYM_CLASS_EXTERNAL_DEF = 5, ///< External definition 90 IMAGE_SYM_CLASS_LABEL = 6, ///< Label 91 IMAGE_SYM_CLASS_UNDEFINED_LABEL = 7, ///< Undefined label 92 IMAGE_SYM_CLASS_MEMBER_OF_STRUCT = 8, ///< Member of structure 93 IMAGE_SYM_CLASS_ARGUMENT = 9, ///< Function argument 94 IMAGE_SYM_CLASS_STRUCT_TAG = 10, ///< Structure tag 95 IMAGE_SYM_CLASS_MEMBER_OF_UNION = 11, ///< Member of union 96 IMAGE_SYM_CLASS_UNION_TAG = 12, ///< Union tag 97 IMAGE_SYM_CLASS_TYPE_DEFINITION = 13, ///< Type definition 98 IMAGE_SYM_CLASS_UNDEFINED_STATIC = 14, ///< Undefined static 99 IMAGE_SYM_CLASS_ENUM_TAG = 15, ///< Enumeration tag 100 IMAGE_SYM_CLASS_MEMBER_OF_ENUM = 16, ///< Member of enumeration 101 IMAGE_SYM_CLASS_REGISTER_PARAM = 17, ///< Register parameter 102 IMAGE_SYM_CLASS_BIT_FIELD = 18, ///< Bit field 103 /// ".bb" or ".eb" - beginning or end of block 104 IMAGE_SYM_CLASS_BLOCK = 100, 105 /// ".bf" or ".ef" - beginning or end of function 106 IMAGE_SYM_CLASS_FUNCTION = 101, 107 IMAGE_SYM_CLASS_END_OF_STRUCT = 102, ///< End of structure 108 IMAGE_SYM_CLASS_FILE = 103, ///< File name 109 /// Line number, reformatted as symbol 110 IMAGE_SYM_CLASS_SECTION = 104, 111 IMAGE_SYM_CLASS_WEAK_EXTERNAL = 105, ///< Duplicate tag 112 /// External symbol in dmert public lib 113 IMAGE_SYM_CLASS_CLR_TOKEN = 107 114 }; 115 116 enum SymbolBaseType { 117 IMAGE_SYM_TYPE_NULL = 0, ///< No type information or unknown base type. 118 IMAGE_SYM_TYPE_VOID = 1, ///< Used with void pointers and functions. 119 IMAGE_SYM_TYPE_CHAR = 2, ///< A character (signed byte). 120 IMAGE_SYM_TYPE_SHORT = 3, ///< A 2-byte signed integer. 121 IMAGE_SYM_TYPE_INT = 4, ///< A natural integer type on the target. 122 IMAGE_SYM_TYPE_LONG = 5, ///< A 4-byte signed integer. 123 IMAGE_SYM_TYPE_FLOAT = 6, ///< A 4-byte floating-point number. 124 IMAGE_SYM_TYPE_DOUBLE = 7, ///< An 8-byte floating-point number. 125 IMAGE_SYM_TYPE_STRUCT = 8, ///< A structure. 126 IMAGE_SYM_TYPE_UNION = 9, ///< An union. 127 IMAGE_SYM_TYPE_ENUM = 10, ///< An enumerated type. 128 IMAGE_SYM_TYPE_MOE = 11, ///< A member of enumeration (a specific value). 129 IMAGE_SYM_TYPE_BYTE = 12, ///< A byte; unsigned 1-byte integer. 130 IMAGE_SYM_TYPE_WORD = 13, ///< A word; unsigned 2-byte integer. 131 IMAGE_SYM_TYPE_UINT = 14, ///< An unsigned integer of natural size. 132 IMAGE_SYM_TYPE_DWORD = 15 ///< An unsigned 4-byte integer. 133 }; 134 135 enum SymbolComplexType { 136 IMAGE_SYM_DTYPE_NULL = 0, ///< No complex type; simple scalar variable. 137 IMAGE_SYM_DTYPE_POINTER = 1, ///< A pointer to base type. 138 IMAGE_SYM_DTYPE_FUNCTION = 2, ///< A function that returns a base type. 139 IMAGE_SYM_DTYPE_ARRAY = 3, ///< An array of base type. 140 141 /// Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT)) 142 SCT_COMPLEX_TYPE_SHIFT = 4 143 }; 144 145 struct section { 146 char Name[NameSize]; 147 uint32_t VirtualSize; 148 uint32_t VirtualAddress; 149 uint32_t SizeOfRawData; 150 uint32_t PointerToRawData; 151 uint32_t PointerToRelocations; 152 uint32_t PointerToLineNumbers; 153 uint16_t NumberOfRelocations; 154 uint16_t NumberOfLineNumbers; 155 uint32_t Characteristics; 156 }; 157 158 enum SectionCharacteristics { 159 IMAGE_SCN_TYPE_NO_PAD = 0x00000008, 160 IMAGE_SCN_CNT_CODE = 0x00000020, 161 IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040, 162 IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080, 163 IMAGE_SCN_LNK_OTHER = 0x00000100, 164 IMAGE_SCN_LNK_INFO = 0x00000200, 165 IMAGE_SCN_LNK_REMOVE = 0x00000800, 166 IMAGE_SCN_LNK_COMDAT = 0x00001000, 167 IMAGE_SCN_GPREL = 0x00008000, 168 IMAGE_SCN_MEM_PURGEABLE = 0x00020000, 169 IMAGE_SCN_MEM_16BIT = 0x00020000, 170 IMAGE_SCN_MEM_LOCKED = 0x00040000, 171 IMAGE_SCN_MEM_PRELOAD = 0x00080000, 172 IMAGE_SCN_ALIGN_1BYTES = 0x00100000, 173 IMAGE_SCN_ALIGN_2BYTES = 0x00200000, 174 IMAGE_SCN_ALIGN_4BYTES = 0x00300000, 175 IMAGE_SCN_ALIGN_8BYTES = 0x00400000, 176 IMAGE_SCN_ALIGN_16BYTES = 0x00500000, 177 IMAGE_SCN_ALIGN_32BYTES = 0x00600000, 178 IMAGE_SCN_ALIGN_64BYTES = 0x00700000, 179 IMAGE_SCN_ALIGN_128BYTES = 0x00800000, 180 IMAGE_SCN_ALIGN_256BYTES = 0x00900000, 181 IMAGE_SCN_ALIGN_512BYTES = 0x00A00000, 182 IMAGE_SCN_ALIGN_1024BYTES = 0x00B00000, 183 IMAGE_SCN_ALIGN_2048BYTES = 0x00C00000, 184 IMAGE_SCN_ALIGN_4096BYTES = 0x00D00000, 185 IMAGE_SCN_ALIGN_8192BYTES = 0x00E00000, 186 IMAGE_SCN_LNK_NRELOC_OVFL = 0x01000000, 187 IMAGE_SCN_MEM_DISCARDABLE = 0x02000000, 188 IMAGE_SCN_MEM_NOT_CACHED = 0x04000000, 189 IMAGE_SCN_MEM_NOT_PAGED = 0x08000000, 190 IMAGE_SCN_MEM_SHARED = 0x10000000, 191 IMAGE_SCN_MEM_EXECUTE = 0x20000000, 192 IMAGE_SCN_MEM_READ = 0x40000000, 193 IMAGE_SCN_MEM_WRITE = 0x80000000 194 }; 195 196 struct relocation { 197 uint32_t VirtualAddress; 198 uint32_t SymbolTableIndex; 199 uint16_t Type; 200 }; 201 202 enum RelocationTypeX86 { 203 IMAGE_REL_I386_ABSOLUTE = 0x0000, 204 IMAGE_REL_I386_DIR16 = 0x0001, 205 IMAGE_REL_I386_REL16 = 0x0002, 206 IMAGE_REL_I386_DIR32 = 0x0006, 207 IMAGE_REL_I386_DIR32NB = 0x0007, 208 IMAGE_REL_I386_SEG12 = 0x0009, 209 IMAGE_REL_I386_SECTION = 0x000A, 210 IMAGE_REL_I386_SECREL = 0x000B, 211 IMAGE_REL_I386_TOKEN = 0x000C, 212 IMAGE_REL_I386_SECREL7 = 0x000D, 213 IMAGE_REL_I386_REL32 = 0x0014, 214 215 IMAGE_REL_AMD64_ABSOLUTE = 0x0000, 216 IMAGE_REL_AMD64_ADDR64 = 0x0001, 217 IMAGE_REL_AMD64_ADDR32 = 0x0002, 218 IMAGE_REL_AMD64_ADDR32NB = 0x0003, 219 IMAGE_REL_AMD64_REL32 = 0x0004, 220 IMAGE_REL_AMD64_REL32_1 = 0x0005, 221 IMAGE_REL_AMD64_REL32_2 = 0x0006, 222 IMAGE_REL_AMD64_REL32_3 = 0x0007, 223 IMAGE_REL_AMD64_REL32_4 = 0x0008, 224 IMAGE_REL_AMD64_REL32_5 = 0x0009, 225 IMAGE_REL_AMD64_SECTION = 0x000A, 226 IMAGE_REL_AMD64_SECREL = 0x000B, 227 IMAGE_REL_AMD64_SECREL7 = 0x000C, 228 IMAGE_REL_AMD64_TOKEN = 0x000D, 229 IMAGE_REL_AMD64_SREL32 = 0x000E, 230 IMAGE_REL_AMD64_PAIR = 0x000F, 231 IMAGE_REL_AMD64_SSPAN32 = 0x0010 232 }; 233 234 enum COMDATType { 235 IMAGE_COMDAT_SELECT_NODUPLICATES = 1, 236 IMAGE_COMDAT_SELECT_ANY, 237 IMAGE_COMDAT_SELECT_SAME_SIZE, 238 IMAGE_COMDAT_SELECT_EXACT_MATCH, 239 IMAGE_COMDAT_SELECT_ASSOCIATIVE, 240 IMAGE_COMDAT_SELECT_LARGEST 241 }; 242 243 // Auxiliary Symbol Formats 244 struct AuxiliaryFunctionDefinition { 245 uint32_t TagIndex; 246 uint32_t TotalSize; 247 uint32_t PointerToLinenumber; 248 uint32_t PointerToNextFunction; 249 uint8_t unused[2]; 250 }; 251 252 struct AuxiliarybfAndefSymbol { 253 uint8_t unused1[4]; 254 uint16_t Linenumber; 255 uint8_t unused2[6]; 256 uint32_t PointerToNextFunction; 257 uint8_t unused3[2]; 258 }; 259 260 struct AuxiliaryWeakExternal { 261 uint32_t TagIndex; 262 uint32_t Characteristics; 263 uint8_t unused[10]; 264 }; 265 266 /// These are not documented in the spec, but are located in WinNT.h. 267 enum WeakExternalCharacteristics { 268 IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY = 1, 269 IMAGE_WEAK_EXTERN_SEARCH_LIBRARY = 2, 270 IMAGE_WEAK_EXTERN_SEARCH_ALIAS = 3 271 }; 272 273 struct AuxiliaryFile { 274 uint8_t FileName[18]; 275 }; 276 277 struct AuxiliarySectionDefinition { 278 uint32_t Length; 279 uint16_t NumberOfRelocations; 280 uint16_t NumberOfLinenumbers; 281 uint32_t CheckSum; 282 uint16_t Number; 283 uint8_t Selection; 284 uint8_t unused[3]; 285 }; 286 287 union Auxiliary { 288 AuxiliaryFunctionDefinition FunctionDefinition; 289 AuxiliarybfAndefSymbol bfAndefSymbol; 290 AuxiliaryWeakExternal WeakExternal; 291 AuxiliaryFile File; 292 AuxiliarySectionDefinition SectionDefinition; 293 }; 294 295 } // End namespace llvm. 296 } // End namespace COFF. 297 298 #endif 299