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