github.com/primecitizens/pcz/std@v0.2.1/encoding/binfmt/pe/xcoff.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2009 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 package pe 9 10 const ( 11 // Total amount of space to reserve at the start of the file 12 // for File Header, Auxiliary Header, and Section Headers. 13 // May waste some. 14 XCOFFHDRRESERVE = FILHSZ_64 + AOUTHSZ_EXEC64 + SCNHSZ_64*23 15 16 // base on dump -o, then rounded from 32B to 64B to 17 // match worst case elf text section alignment on ppc64. 18 XCOFFSECTALIGN int64 = 64 19 20 // XCOFF binaries should normally have all its sections position-independent. 21 // However, this is not yet possible for .text because of some R_ADDR relocations 22 // inside RODATA symbols. 23 // .data and .bss are position-independent so their address start inside a unreachable 24 // segment during execution to force segfault if something is wrong. 25 XCOFFTEXTBASE = 0x100000000 // Start of text address 26 XCOFFDATABASE = 0x200000000 // Start of data address 27 ) 28 29 // File Header 30 type XcoffFileHdr64 struct { 31 Fmagic uint16 // Target machine 32 Fnscns uint16 // Number of sections 33 Ftimedat int32 // Time and date of file creation 34 Fsymptr uint64 // Byte offset to symbol table start 35 Fopthdr uint16 // Number of bytes in optional header 36 Fflags uint16 // Flags 37 Fnsyms int32 // Number of entries in symbol table 38 } 39 40 const ( 41 U64_TOCMAGIC = 0767 // AIX 64-bit XCOFF 42 ) 43 44 // Flags that describe the type of the object file. 45 const ( 46 F_RELFLG = 0x0001 47 F_EXEC = 0x0002 48 F_LNNO = 0x0004 49 F_FDPR_PROF = 0x0010 50 F_FDPR_OPTI = 0x0020 51 F_DSA = 0x0040 52 F_VARPG = 0x0100 53 F_DYNLOAD = 0x1000 54 F_SHROBJ = 0x2000 55 F_LOADONLY = 0x4000 56 ) 57 58 // Auxiliary Header 59 type XcoffAoutHdr64 struct { 60 Omagic int16 // Flags - Ignored If Vstamp Is 1 61 Ovstamp int16 // Version 62 Odebugger uint32 // Reserved For Debugger 63 Otextstart uint64 // Virtual Address Of Text 64 Odatastart uint64 // Virtual Address Of Data 65 Otoc uint64 // Toc Address 66 Osnentry int16 // Section Number For Entry Point 67 Osntext int16 // Section Number For Text 68 Osndata int16 // Section Number For Data 69 Osntoc int16 // Section Number For Toc 70 Osnloader int16 // Section Number For Loader 71 Osnbss int16 // Section Number For Bss 72 Oalgntext int16 // Max Text Alignment 73 Oalgndata int16 // Max Data Alignment 74 Omodtype [2]byte // Module Type Field 75 Ocpuflag uint8 // Bit Flags - Cputypes Of Objects 76 Ocputype uint8 // Reserved for CPU type 77 Otextpsize uint8 // Requested text page size 78 Odatapsize uint8 // Requested data page size 79 Ostackpsize uint8 // Requested stack page size 80 Oflags uint8 // Flags And TLS Alignment 81 Otsize uint64 // Text Size In Bytes 82 Odsize uint64 // Data Size In Bytes 83 Obsize uint64 // Bss Size In Bytes 84 Oentry uint64 // Entry Point Address 85 Omaxstack uint64 // Max Stack Size Allowed 86 Omaxdata uint64 // Max Data Size Allowed 87 Osntdata int16 // Section Number For Tdata Section 88 Osntbss int16 // Section Number For Tbss Section 89 Ox64flags uint16 // Additional Flags For 64-Bit Objects 90 Oresv3a int16 // Reserved 91 Oresv3 [2]int32 // Reserved 92 } 93 94 // Section Header 95 type XcoffScnHdr64 struct { 96 Sname [8]byte // Section Name 97 Spaddr uint64 // Physical Address 98 Svaddr uint64 // Virtual Address 99 Ssize uint64 // Section Size 100 Sscnptr uint64 // File Offset To Raw Data 101 Srelptr uint64 // File Offset To Relocation 102 Slnnoptr uint64 // File Offset To Line Numbers 103 Snreloc uint32 // Number Of Relocation Entries 104 Snlnno uint32 // Number Of Line Number Entries 105 Sflags uint32 // flags 106 } 107 108 // Flags defining the section type. 109 const ( 110 STYP_DWARF = 0x0010 111 STYP_TEXT = 0x0020 112 STYP_DATA = 0x0040 113 STYP_BSS = 0x0080 114 STYP_EXCEPT = 0x0100 115 STYP_INFO = 0x0200 116 STYP_TDATA = 0x0400 117 STYP_TBSS = 0x0800 118 STYP_LOADER = 0x1000 119 STYP_DEBUG = 0x2000 120 STYP_TYPCHK = 0x4000 121 STYP_OVRFLO = 0x8000 122 ) 123 const ( 124 SSUBTYP_DWINFO = 0x10000 // DWARF info section 125 SSUBTYP_DWLINE = 0x20000 // DWARF line-number section 126 SSUBTYP_DWPBNMS = 0x30000 // DWARF public names section 127 SSUBTYP_DWPBTYP = 0x40000 // DWARF public types section 128 SSUBTYP_DWARNGE = 0x50000 // DWARF aranges section 129 SSUBTYP_DWABREV = 0x60000 // DWARF abbreviation section 130 SSUBTYP_DWSTR = 0x70000 // DWARF strings section 131 SSUBTYP_DWRNGES = 0x80000 // DWARF ranges section 132 SSUBTYP_DWLOC = 0x90000 // DWARF location lists section 133 SSUBTYP_DWFRAME = 0xA0000 // DWARF frames section 134 SSUBTYP_DWMAC = 0xB0000 // DWARF macros section 135 ) 136 137 // Headers size 138 const ( 139 FILHSZ_32 = 20 140 FILHSZ_64 = 24 141 AOUTHSZ_EXEC32 = 72 142 AOUTHSZ_EXEC64 = 120 143 SCNHSZ_32 = 40 144 SCNHSZ_64 = 72 145 LDHDRSZ_32 = 32 146 LDHDRSZ_64 = 56 147 LDSYMSZ_64 = 24 148 RELSZ_64 = 14 149 ) 150 151 // Type representing all XCOFF symbols. 152 type xcoffSym interface { 153 } 154 155 // Symbol Table Entry 156 type XcoffSymEnt64 struct { 157 Nvalue uint64 // Symbol value 158 Noffset uint32 // Offset of the name in string table or .debug section 159 Nscnum int16 // Section number of symbol 160 Ntype uint16 // Basic and derived type specification 161 Nsclass uint8 // Storage class of symbol 162 Nnumaux int8 // Number of auxiliary entries 163 } 164 165 const SYMESZ = 18 166 167 const ( 168 // Nscnum 169 N_DEBUG = -2 170 N_ABS = -1 171 N_UNDEF = 0 172 173 //Ntype 174 SYM_V_INTERNAL = 0x1000 175 SYM_V_HIDDEN = 0x2000 176 SYM_V_PROTECTED = 0x3000 177 SYM_V_EXPORTED = 0x4000 178 SYM_TYPE_FUNC = 0x0020 // is function 179 ) 180 181 // Storage Class. 182 const ( 183 C_NULL = 0 // Symbol table entry marked for deletion 184 C_EXT = 2 // External symbol 185 C_STAT = 3 // Static symbol 186 C_BLOCK = 100 // Beginning or end of inner block 187 C_FCN = 101 // Beginning or end of function 188 C_FILE = 103 // Source file name and compiler information 189 C_HIDEXT = 107 // Unnamed external symbol 190 C_BINCL = 108 // Beginning of include file 191 C_EINCL = 109 // End of include file 192 C_WEAKEXT = 111 // Weak external symbol 193 C_DWARF = 112 // DWARF symbol 194 C_GSYM = 128 // Global variable 195 C_LSYM = 129 // Automatic variable allocated on stack 196 C_PSYM = 130 // Argument to subroutine allocated on stack 197 C_RSYM = 131 // Register variable 198 C_RPSYM = 132 // Argument to function or procedure stored in register 199 C_STSYM = 133 // Statically allocated symbol 200 C_BCOMM = 135 // Beginning of common block 201 C_ECOML = 136 // Local member of common block 202 C_ECOMM = 137 // End of common block 203 C_DECL = 140 // Declaration of object 204 C_ENTRY = 141 // Alternate entry 205 C_FUN = 142 // Function or procedure 206 C_BSTAT = 143 // Beginning of static block 207 C_ESTAT = 144 // End of static block 208 C_GTLS = 145 // Global thread-local variable 209 C_STTLS = 146 // Static thread-local variable 210 ) 211 212 // File Auxiliary Entry 213 type XcoffAuxFile64 struct { 214 Xzeroes uint32 // The name is always in the string table 215 Xoffset uint32 // Offset in the string table 216 X_pad1 [6]byte 217 Xftype uint8 // Source file string type 218 X_pad2 [2]byte 219 Xauxtype uint8 // Type of auxiliary entry 220 } 221 222 // Function Auxiliary Entry 223 type XcoffAuxFcn64 struct { 224 Xlnnoptr uint64 // File pointer to line number 225 Xfsize uint32 // Size of function in bytes 226 Xendndx uint32 // Symbol table index of next entry 227 Xpad uint8 // Unused 228 Xauxtype uint8 // Type of auxiliary entry 229 } 230 231 // csect Auxiliary Entry. 232 type XcoffAuxCSect64 struct { 233 Xscnlenlo uint32 // Lower 4 bytes of length or symbol table index 234 Xparmhash uint32 // Offset of parameter type-check string 235 Xsnhash uint16 // .typchk section number 236 Xsmtyp uint8 // Symbol alignment and type 237 Xsmclas uint8 // Storage-mapping class 238 Xscnlenhi uint32 // Upper 4 bytes of length or symbol table index 239 Xpad uint8 // Unused 240 Xauxtype uint8 // Type of auxiliary entry 241 } 242 243 // DWARF Auxiliary Entry 244 type XcoffAuxDWARF64 struct { 245 Xscnlen uint64 // Length of this symbol section 246 X_pad [9]byte 247 Xauxtype uint8 // Type of auxiliary entry 248 } 249 250 // Auxiliary type 251 const ( 252 _AUX_EXCEPT = 255 253 _AUX_FCN = 254 254 _AUX_SYM = 253 255 _AUX_FILE = 252 256 _AUX_CSECT = 251 257 _AUX_SECT = 250 258 ) 259 260 // Xftype field 261 const ( 262 XFT_FN = 0 // Source File Name 263 XFT_CT = 1 // Compile Time Stamp 264 XFT_CV = 2 // Compiler Version Number 265 XFT_CD = 128 // Compiler Defined Information/ 266 267 ) 268 269 // Symbol type field. 270 const ( 271 XTY_ER = 0 // External reference 272 XTY_SD = 1 // Section definition 273 XTY_LD = 2 // Label definition 274 XTY_CM = 3 // Common csect definition 275 XTY_WK = 0x8 // Weak symbol 276 XTY_EXP = 0x10 // Exported symbol 277 XTY_ENT = 0x20 // Entry point symbol 278 XTY_IMP = 0x40 // Imported symbol 279 ) 280 281 // Storage-mapping class. 282 const ( 283 XMC_PR = 0 // Program code 284 XMC_RO = 1 // Read-only constant 285 XMC_DB = 2 // Debug dictionary table 286 XMC_TC = 3 // TOC entry 287 XMC_UA = 4 // Unclassified 288 XMC_RW = 5 // Read/Write data 289 XMC_GL = 6 // Global linkage 290 XMC_XO = 7 // Extended operation 291 XMC_SV = 8 // 32-bit supervisor call descriptor 292 XMC_BS = 9 // BSS class 293 XMC_DS = 10 // Function descriptor 294 XMC_UC = 11 // Unnamed FORTRAN common 295 XMC_TC0 = 15 // TOC anchor 296 XMC_TD = 16 // Scalar data entry in the TOC 297 XMC_SV64 = 17 // 64-bit supervisor call descriptor 298 XMC_SV3264 = 18 // Supervisor call descriptor for both 32-bit and 64-bit 299 XMC_TL = 20 // Read/Write thread-local data 300 XMC_UL = 21 // Read/Write thread-local data (.tbss) 301 XMC_TE = 22 // TOC entry 302 ) 303 304 // Loader Header 305 type XcoffLdHdr64 struct { 306 Lversion int32 // Loader section version number 307 Lnsyms int32 // Number of symbol table entries 308 Lnreloc int32 // Number of relocation table entries 309 Listlen uint32 // Length of import file ID string table 310 Lnimpid int32 // Number of import file IDs 311 Lstlen uint32 // Length of string table 312 Limpoff uint64 // Offset to start of import file IDs 313 Lstoff uint64 // Offset to start of string table 314 Lsymoff uint64 // Offset to start of symbol table 315 Lrldoff uint64 // Offset to start of relocation entries 316 } 317 318 // Loader Symbol 319 type XcoffLdSym64 struct { 320 Lvalue uint64 // Address field 321 Loffset uint32 // Byte offset into string table of symbol name 322 Lscnum int16 // Section number containing symbol 323 Lsmtype int8 // Symbol type, export, import flags 324 Lsmclas int8 // Symbol storage class 325 Lifile int32 // Import file ID; ordinal of import file IDs 326 Lparm uint32 // Parameter type-check field 327 } 328 329 type XcoffLdRel64 struct { 330 Lvaddr uint64 // Address Field 331 Lrtype uint16 // Relocation Size and Type 332 Lrsecnm int16 // Section Number being relocated 333 Lsymndx int32 // Loader-Section symbol table index 334 } 335 336 const ( 337 XCOFF_R_POS = 0x00 // A(sym) Positive Relocation 338 XCOFF_R_NEG = 0x01 // -A(sym) Negative Relocation 339 XCOFF_R_REL = 0x02 // A(sym-*) Relative to self 340 XCOFF_R_TOC = 0x03 // A(sym-TOC) Relative to TOC 341 XCOFF_R_TRL = 0x12 // A(sym-TOC) TOC Relative indirect load. 342 343 XCOFF_R_TRLA = 0x13 // A(sym-TOC) TOC Rel load address. modifiable inst 344 XCOFF_R_GL = 0x05 // A(external TOC of sym) Global Linkage 345 XCOFF_R_TCL = 0x06 // A(local TOC of sym) Local object TOC address 346 XCOFF_R_RL = 0x0C // A(sym) Pos indirect load. modifiable instruction 347 XCOFF_R_RLA = 0x0D // A(sym) Pos Load Address. modifiable instruction 348 XCOFF_R_REF = 0x0F // AL0(sym) Non relocating ref. No garbage collect 349 XCOFF_R_BA = 0x08 // A(sym) Branch absolute. Cannot modify instruction 350 XCOFF_R_RBA = 0x18 // A(sym) Branch absolute. modifiable instruction 351 XCOFF_R_BR = 0x0A // A(sym-*) Branch rel to self. non modifiable 352 XCOFF_R_RBR = 0x1A // A(sym-*) Branch rel to self. modifiable instr 353 354 XCOFF_R_TLS = 0x20 // General-dynamic reference to TLS symbol 355 XCOFF_R_TLS_IE = 0x21 // Initial-exec reference to TLS symbol 356 XCOFF_R_TLS_LD = 0x22 // Local-dynamic reference to TLS symbol 357 XCOFF_R_TLS_LE = 0x23 // Local-exec reference to TLS symbol 358 XCOFF_R_TLSM = 0x24 // Module reference to TLS symbol 359 XCOFF_R_TLSML = 0x25 // Module reference to local (own) module 360 361 XCOFF_R_TOCU = 0x30 // Relative to TOC - high order bits 362 XCOFF_R_TOCL = 0x31 // Relative to TOC - low order bits 363 )