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

source code of llvm/include/llvm/BinaryFormat/XCOFF.h