XCOFF.h 18 KB

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