XCOFF.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/BinaryFormat/XCOFF.h - The XCOFF file format -------*- C++/-*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines manifest constants for the XCOFF object file format.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_BINARYFORMAT_XCOFF_H
  18. #define LLVM_BINARYFORMAT_XCOFF_H
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. namespace llvm {
  22. class StringRef;
  23. template <unsigned> class SmallString;
  24. namespace XCOFF {
  25. // Constants used in the XCOFF definition.
  26. constexpr size_t FileNamePadSize = 6;
  27. constexpr size_t NameSize = 8;
  28. constexpr size_t SymbolTableEntrySize = 18;
  29. constexpr size_t RelocationSerializationSize32 = 10;
  30. constexpr uint16_t RelocOverflow = 65535;
  31. constexpr uint8_t AllocRegNo = 31;
  32. enum ReservedSectionNum : int16_t { N_DEBUG = -2, N_ABS = -1, N_UNDEF = 0 };
  33. // x_smclas field of x_csect from system header: /usr/include/syms.h
  34. /// Storage Mapping Class definitions.
  35. enum StorageMappingClass : uint8_t {
  36. // READ ONLY CLASSES
  37. XMC_PR = 0, ///< Program Code
  38. XMC_RO = 1, ///< Read Only Constant
  39. XMC_DB = 2, ///< Debug Dictionary Table
  40. XMC_GL = 6, ///< Global Linkage (Interfile Interface Code)
  41. XMC_XO = 7, ///< Extended Operation (Pseudo Machine Instruction)
  42. XMC_SV = 8, ///< Supervisor Call (32-bit process only)
  43. XMC_SV64 = 17, ///< Supervisor Call for 64-bit process
  44. XMC_SV3264 = 18, ///< Supervisor Call for both 32- and 64-bit processes
  45. XMC_TI = 12, ///< Traceback Index csect
  46. XMC_TB = 13, ///< Traceback Table csect
  47. // READ WRITE CLASSES
  48. XMC_RW = 5, ///< Read Write Data
  49. XMC_TC0 = 15, ///< TOC Anchor for TOC Addressability
  50. XMC_TC = 3, ///< General TOC item
  51. XMC_TD = 16, ///< Scalar data item in the TOC
  52. XMC_DS = 10, ///< Descriptor csect
  53. XMC_UA = 4, ///< Unclassified - Treated as Read Write
  54. XMC_BS = 9, ///< BSS class (uninitialized static internal)
  55. XMC_UC = 11, ///< Un-named Fortran Common
  56. XMC_TL = 20, ///< Initialized thread-local variable
  57. XMC_UL = 21, ///< Uninitialized thread-local variable
  58. XMC_TE = 22 ///< Symbol mapped at the end of TOC
  59. };
  60. // Flags for defining the section type. Masks for use with the (signed, 32-bit)
  61. // s_flags field of the section header structure, selecting for values in the
  62. // lower 16 bits. Defined in the system header `scnhdr.h`.
  63. enum SectionTypeFlags : int32_t {
  64. STYP_PAD = 0x0008,
  65. STYP_DWARF = 0x0010,
  66. STYP_TEXT = 0x0020,
  67. STYP_DATA = 0x0040,
  68. STYP_BSS = 0x0080,
  69. STYP_EXCEPT = 0x0100,
  70. STYP_INFO = 0x0200,
  71. STYP_TDATA = 0x0400,
  72. STYP_TBSS = 0x0800,
  73. STYP_LOADER = 0x1000,
  74. STYP_DEBUG = 0x2000,
  75. STYP_TYPCHK = 0x4000,
  76. STYP_OVRFLO = 0x8000
  77. };
  78. /// Values for defining the section subtype of sections of type STYP_DWARF as
  79. /// they would appear in the (signed, 32-bit) s_flags field of the section
  80. /// header structure, contributing to the 16 most significant bits. Defined in
  81. /// the system header `scnhdr.h`.
  82. enum DwarfSectionSubtypeFlags : int32_t {
  83. SSUBTYP_DWINFO = 0x1'0000, ///< DWARF info section
  84. SSUBTYP_DWLINE = 0x2'0000, ///< DWARF line section
  85. SSUBTYP_DWPBNMS = 0x3'0000, ///< DWARF pubnames section
  86. SSUBTYP_DWPBTYP = 0x4'0000, ///< DWARF pubtypes section
  87. SSUBTYP_DWARNGE = 0x5'0000, ///< DWARF aranges section
  88. SSUBTYP_DWABREV = 0x6'0000, ///< DWARF abbrev section
  89. SSUBTYP_DWSTR = 0x7'0000, ///< DWARF str section
  90. SSUBTYP_DWRNGES = 0x8'0000, ///< DWARF ranges section
  91. SSUBTYP_DWLOC = 0x9'0000, ///< DWARF loc section
  92. SSUBTYP_DWFRAME = 0xA'0000, ///< DWARF frame section
  93. SSUBTYP_DWMAC = 0xB'0000 ///< DWARF macinfo section
  94. };
  95. // STORAGE CLASSES, n_sclass field of syment.
  96. // The values come from `storclass.h` and `dbxstclass.h`.
  97. enum StorageClass : uint8_t {
  98. // Storage classes used for symbolic debugging symbols.
  99. C_FILE = 103, // File name
  100. C_BINCL = 108, // Beginning of include file
  101. C_EINCL = 109, // Ending of include file
  102. C_GSYM = 128, // Global variable
  103. C_STSYM = 133, // Statically allocated symbol
  104. C_BCOMM = 135, // Beginning of common block
  105. C_ECOMM = 137, // End of common block
  106. C_ENTRY = 141, // Alternate entry
  107. C_BSTAT = 143, // Beginning of static block
  108. C_ESTAT = 144, // End of static block
  109. C_GTLS = 145, // Global thread-local variable
  110. C_STTLS = 146, // Static thread-local variable
  111. // Storage classes used for DWARF symbols.
  112. C_DWARF = 112, // DWARF section symbol
  113. // Storage classes used for absolute symbols.
  114. C_LSYM = 129, // Automatic variable allocated on stack
  115. C_PSYM = 130, // Argument to subroutine allocated on stack
  116. C_RSYM = 131, // Register variable
  117. C_RPSYM = 132, // Argument to function or procedure stored in register
  118. C_ECOML = 136, // Local member of common block
  119. C_FUN = 142, // Function or procedure
  120. // Storage classes used for undefined external symbols or
  121. // symbols of general sections.
  122. C_EXT = 2, // External symbol
  123. C_WEAKEXT = 111, // Weak external symbol
  124. // Storage classes used for symbols of general sections.
  125. C_NULL = 0,
  126. C_STAT = 3, // Static
  127. C_BLOCK = 100, // ".bb" or ".eb"
  128. C_FCN = 101, // ".bf" or ".ef"
  129. C_HIDEXT = 107, // Un-named external symbol
  130. C_INFO = 110, // Comment string in .info section
  131. C_DECL = 140, // Declaration of object (type)
  132. // Storage classes - Obsolete/Undocumented.
  133. C_AUTO = 1, // Automatic variable
  134. C_REG = 4, // Register variable
  135. C_EXTDEF = 5, // External definition
  136. C_LABEL = 6, // Label
  137. C_ULABEL = 7, // Undefined label
  138. C_MOS = 8, // Member of structure
  139. C_ARG = 9, // Function argument
  140. C_STRTAG = 10, // Structure tag
  141. C_MOU = 11, // Member of union
  142. C_UNTAG = 12, // Union tag
  143. C_TPDEF = 13, // Type definition
  144. C_USTATIC = 14, // Undefined static
  145. C_ENTAG = 15, // Enumeration tag
  146. C_MOE = 16, // Member of enumeration
  147. C_REGPARM = 17, // Register parameter
  148. C_FIELD = 18, // Bit field
  149. C_EOS = 102, // End of structure
  150. C_LINE = 104,
  151. C_ALIAS = 105, // Duplicate tag
  152. C_HIDDEN = 106, // Special storage class for external
  153. C_EFCN = 255, // Physical end of function
  154. // Storage classes - reserved
  155. C_TCSYM = 134 // Reserved
  156. };
  157. // Flags for defining the symbol type. Values to be encoded into the lower 3
  158. // bits of the (unsigned, 8-bit) x_smtyp field of csect auxiliary symbol table
  159. // entries. Defined in the system header `syms.h`.
  160. enum SymbolType : uint8_t {
  161. XTY_ER = 0, ///< External reference.
  162. XTY_SD = 1, ///< Csect definition for initialized storage.
  163. XTY_LD = 2, ///< Label definition.
  164. ///< Defines an entry point to an initialized csect.
  165. XTY_CM = 3 ///< Common csect definition. For uninitialized storage.
  166. };
  167. /// Values for visibility as they would appear when encoded in the high 4 bits
  168. /// of the 16-bit unsigned n_type field of symbol table entries. Valid for
  169. /// 32-bit XCOFF only when the vstamp in the auxiliary header is greater than 1.
  170. enum VisibilityType : uint16_t {
  171. SYM_V_UNSPECIFIED = 0x0000,
  172. SYM_V_INTERNAL = 0x1000,
  173. SYM_V_HIDDEN = 0x2000,
  174. SYM_V_PROTECTED = 0x3000,
  175. SYM_V_EXPORTED = 0x4000
  176. };
  177. // Relocation types, defined in `/usr/include/reloc.h`.
  178. enum RelocationType : uint8_t {
  179. R_POS = 0x00, ///< Positive relocation. Provides the address of the referenced
  180. ///< symbol.
  181. R_RL = 0x0c, ///< Positive indirect load relocation. Modifiable instruction.
  182. R_RLA = 0x0d, ///< Positive load address relocation. Modifiable instruction.
  183. R_NEG = 0x01, ///< Negative relocation. Provides the negative of the address
  184. ///< of the referenced symbol.
  185. R_REL = 0x02, ///< Relative to self relocation. Provides a displacement value
  186. ///< between the address of the referenced symbol and the
  187. ///< address being relocated.
  188. R_TOC = 0x03, ///< Relative to the TOC relocation. Provides a displacement
  189. ///< that is the difference between the address of the
  190. ///< referenced symbol and the TOC anchor csect.
  191. R_TRL = 0x12, ///< TOC relative indirect load relocation. Similar to R_TOC,
  192. ///< but not modifiable instruction.
  193. R_TRLA =
  194. 0x13, ///< Relative to the TOC or to the thread-local storage base
  195. ///< relocation. Compilers are not permitted to generate this
  196. ///< relocation type. It is the result of a reversible
  197. ///< transformation by the linker of an R_TOC relation that turned a
  198. ///< load instruction into an add-immediate instruction.
  199. R_GL = 0x05, ///< Global linkage-external TOC address relocation. Provides the
  200. ///< address of the external TOC associated with a defined
  201. ///< external symbol.
  202. R_TCL = 0x06, ///< Local object TOC address relocation. Provides the address
  203. ///< of the local TOC entry of a defined external symbol.
  204. R_REF = 0x0f, ///< A non-relocating relocation. Used to prevent the binder
  205. ///< from garbage collecting a csect (such as code used for
  206. ///< dynamic initialization of non-local statics) for which
  207. ///< another csect has an implicit dependency.
  208. R_BA = 0x08, ///< Branch absolute relocation. Provides the address of the
  209. ///< referenced symbol. References a non-modifiable instruction.
  210. R_BR = 0x0a, ///< Branch relative to self relocation. Provides the
  211. ///< displacement that is the difference between the address of
  212. ///< the referenced symbol and the address of the referenced
  213. ///< branch instruction. References a non-modifiable instruction.
  214. R_RBA = 0x18, ///< Branch absolute relocation. Similar to R_BA but
  215. ///< references a modifiable instruction.
  216. R_RBR = 0x1a, ///< Branch relative to self relocation. Similar to the R_BR
  217. ///< relocation type, but references a modifiable instruction.
  218. R_TLS = 0x20, ///< General-dynamic reference to TLS symbol.
  219. R_TLS_IE = 0x21, ///< Initial-exec reference to TLS symbol.
  220. R_TLS_LD = 0x22, ///< Local-dynamic reference to TLS symbol.
  221. R_TLS_LE = 0x23, ///< Local-exec reference to TLS symbol.
  222. R_TLSM = 0x24, ///< Module reference to TLS. Provides a handle for the module
  223. ///< containing the referenced symbol.
  224. R_TLSML = 0x25, ///< Module reference to the local TLS storage.
  225. R_TOCU = 0x30, ///< Relative to TOC upper. Specifies the high-order 16 bits of
  226. ///< a large code model TOC-relative relocation.
  227. R_TOCL = 0x31 ///< Relative to TOC lower. Specifies the low-order 16 bits of a
  228. ///< large code model TOC-relative relocation.
  229. };
  230. struct FileHeader32 {
  231. uint16_t Magic;
  232. uint16_t NumberOfSections;
  233. int32_t TimeStamp;
  234. uint32_t SymbolTableFileOffset;
  235. int32_t NumberOfSymbolTableEntries;
  236. uint16_t AuxiliaryHeaderSize;
  237. uint16_t Flags;
  238. };
  239. struct SectionHeader32 {
  240. char Name[XCOFF::NameSize];
  241. uint32_t PhysicalAddress;
  242. uint32_t VirtualAddress;
  243. uint32_t Size;
  244. uint32_t FileOffsetToData;
  245. uint32_t FileOffsetToRelocations;
  246. uint32_t FileOffsetToLineNumbers;
  247. uint16_t NumberOfRelocations;
  248. uint16_t NumberOfLineNumbers;
  249. int32_t Flags;
  250. };
  251. enum CFileStringType : uint8_t {
  252. XFT_FN = 0, ///< Specifies the source-file name.
  253. XFT_CT = 1, ///< Specifies the compiler time stamp.
  254. XFT_CV = 2, ///< Specifies the compiler version number.
  255. XFT_CD = 128 ///< Specifies compiler-defined information.
  256. };
  257. enum CFileLangId : uint8_t {
  258. TB_C = 0, ///< C language.
  259. TB_CPLUSPLUS = 9 ///< C++ language.
  260. };
  261. enum CFileCpuId : uint8_t {
  262. TCPU_PPC64 = 2, ///< PowerPC common architecture 64-bit mode.
  263. TCPU_COM = 3, ///< POWER and PowerPC architecture common.
  264. TCPU_970 = 19 ///< PPC970 - PowerPC 64-bit architecture.
  265. };
  266. StringRef getMappingClassString(XCOFF::StorageMappingClass SMC);
  267. StringRef getRelocationTypeString(XCOFF::RelocationType Type);
  268. SmallString<32> parseParmsType(uint32_t Value, unsigned ParmsNum);
  269. struct TracebackTable {
  270. enum LanguageID : uint8_t {
  271. C,
  272. Fortran,
  273. Pascal,
  274. Ada,
  275. PL1,
  276. Basic,
  277. Lisp,
  278. Cobol,
  279. Modula2,
  280. CPlusPlus,
  281. Rpg,
  282. PL8,
  283. PLIX = PL8,
  284. Assembly,
  285. Java,
  286. ObjectiveC
  287. };
  288. // Byte 1
  289. static constexpr uint32_t VersionMask = 0xFF00'0000;
  290. static constexpr uint8_t VersionShift = 24;
  291. // Byte 2
  292. static constexpr uint32_t LanguageIdMask = 0x00FF'0000;
  293. static constexpr uint8_t LanguageIdShift = 16;
  294. // Byte 3
  295. static constexpr uint32_t IsGlobaLinkageMask = 0x0000'8000;
  296. static constexpr uint32_t IsOutOfLineEpilogOrPrologueMask = 0x0000'4000;
  297. static constexpr uint32_t HasTraceBackTableOffsetMask = 0x0000'2000;
  298. static constexpr uint32_t IsInternalProcedureMask = 0x0000'1000;
  299. static constexpr uint32_t HasControlledStorageMask = 0x0000'0800;
  300. static constexpr uint32_t IsTOClessMask = 0x0000'0400;
  301. static constexpr uint32_t IsFloatingPointPresentMask = 0x0000'0200;
  302. static constexpr uint32_t IsFloatingPointOperationLogOrAbortEnabledMask =
  303. 0x0000'0100;
  304. // Byte 4
  305. static constexpr uint32_t IsInterruptHandlerMask = 0x0000'0080;
  306. static constexpr uint32_t IsFunctionNamePresentMask = 0x0000'0040;
  307. static constexpr uint32_t IsAllocaUsedMask = 0x0000'0020;
  308. static constexpr uint32_t OnConditionDirectiveMask = 0x0000'001C;
  309. static constexpr uint32_t IsCRSavedMask = 0x0000'0002;
  310. static constexpr uint32_t IsLRSavedMask = 0x0000'0001;
  311. static constexpr uint8_t OnConditionDirectiveShift = 2;
  312. // Byte 5
  313. static constexpr uint32_t IsBackChainStoredMask = 0x8000'0000;
  314. static constexpr uint32_t IsFixupMask = 0x4000'0000;
  315. static constexpr uint32_t FPRSavedMask = 0x3F00'0000;
  316. static constexpr uint32_t FPRSavedShift = 24;
  317. // Byte 6
  318. static constexpr uint32_t HasVectorInfoMask = 0x0080'0000;
  319. static constexpr uint32_t HasExtensionTableMask = 0x0040'0000;
  320. static constexpr uint32_t GPRSavedMask = 0x003F'0000;
  321. static constexpr uint32_t GPRSavedShift = 16;
  322. // Byte 7
  323. static constexpr uint32_t NumberOfFixedParmsMask = 0x0000'FF00;
  324. static constexpr uint8_t NumberOfFixedParmsShift = 8;
  325. // Byte 8
  326. static constexpr uint32_t NumberOfFloatingPointParmsMask = 0x0000'00FE;
  327. static constexpr uint32_t HasParmsOnStackMask = 0x0000'0001;
  328. static constexpr uint8_t NumberOfFloatingPointParmsShift = 1;
  329. // Masks to select leftmost bits for decoding parameter type information.
  330. // Bit to use when vector info is not presented.
  331. static constexpr uint32_t ParmTypeIsFloatingBit = 0x8000'0000;
  332. static constexpr uint32_t ParmTypeFloatingIsDoubleBit = 0x4000'0000;
  333. // Bits to use when vector info is presented.
  334. static constexpr uint32_t ParmTypeIsFixedBits = 0x0000'0000;
  335. static constexpr uint32_t ParmTypeIsVectorBits = 0x4000'0000;
  336. static constexpr uint32_t ParmTypeIsFloatingBits = 0x8000'0000;
  337. static constexpr uint32_t ParmTypeIsDoubleBits = 0xC000'0000;
  338. static constexpr uint32_t ParmTypeMask = 0xC000'0000;
  339. // Vector extension
  340. static constexpr uint16_t NumberOfVRSavedMask = 0xFC00;
  341. static constexpr uint16_t IsVRSavedOnStackMask = 0x0200;
  342. static constexpr uint16_t HasVarArgsMask = 0x0100;
  343. static constexpr uint8_t NumberOfVRSavedShift = 10;
  344. static constexpr uint16_t NumberOfVectorParmsMask = 0x00FE;
  345. static constexpr uint16_t HasVMXInstructionMask = 0x0001;
  346. static constexpr uint8_t NumberOfVectorParmsShift = 1;
  347. static constexpr uint32_t ParmTypeIsVectorCharBit = 0x0000'0000;
  348. static constexpr uint32_t ParmTypeIsVectorShortBit = 0x4000'0000;
  349. static constexpr uint32_t ParmTypeIsVectorIntBit = 0x8000'0000;
  350. static constexpr uint32_t ParmTypeIsVectorFloatBit = 0xC000'0000;
  351. };
  352. // Extended Traceback table flags.
  353. enum ExtendedTBTableFlag : uint8_t {
  354. TB_OS1 = 0x80, ///< Reserved for OS use.
  355. TB_RESERVED = 0x40, ///< Reserved for compiler.
  356. TB_SSP_CANARY = 0x20, ///< stack smasher canary present on stack.
  357. TB_OS2 = 0x10, ///< Reserved for OS use.
  358. TB_EH_INFO = 0x08, ///< Exception handling info present.
  359. TB_LONGTBTABLE2 = 0x01 ///< Additional tbtable extension exists.
  360. };
  361. StringRef getNameForTracebackTableLanguageId(TracebackTable::LanguageID LangId);
  362. SmallString<32> getExtendedTBTableFlagString(uint8_t Flag);
  363. } // end namespace XCOFF
  364. } // end namespace llvm
  365. #endif
  366. #ifdef __GNUC__
  367. #pragma GCC diagnostic pop
  368. #endif