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