github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/link/internal/ld/xcoff.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ld
     6  
     7  import (
     8  	"github.com/shogo82148/std/cmd/link/internal/loader"
     9  )
    10  
    11  const (
    12  	// Total amount of space to reserve at the start of the file
    13  	// for File Header, Auxiliary Header, and Section Headers.
    14  	// May waste some.
    15  	XCOFFHDRRESERVE = FILHSZ_64 + AOUTHSZ_EXEC64 + SCNHSZ_64*23
    16  
    17  	// base on dump -o, then rounded from 32B to 64B to
    18  	// match worst case elf text section alignment on ppc64.
    19  	XCOFFSECTALIGN int64 = 64
    20  
    21  	// XCOFF binaries should normally have all its sections position-independent.
    22  	// However, this is not yet possible for .text because of some R_ADDR relocations
    23  	// inside RODATA symbols.
    24  	// .data and .bss are position-independent so their address start inside an unreachable
    25  	// segment during execution to force segfault if something is wrong.
    26  	XCOFFTEXTBASE = 0x100000000
    27  	XCOFFDATABASE = 0x200000000
    28  )
    29  
    30  // File Header
    31  type XcoffFileHdr64 struct {
    32  	Fmagic   uint16
    33  	Fnscns   uint16
    34  	Ftimedat int32
    35  	Fsymptr  uint64
    36  	Fopthdr  uint16
    37  	Fflags   uint16
    38  	Fnsyms   int32
    39  }
    40  
    41  const (
    42  	U64_TOCMAGIC = 0767
    43  )
    44  
    45  // Flags that describe the type of the object file.
    46  const (
    47  	F_RELFLG    = 0x0001
    48  	F_EXEC      = 0x0002
    49  	F_LNNO      = 0x0004
    50  	F_FDPR_PROF = 0x0010
    51  	F_FDPR_OPTI = 0x0020
    52  	F_DSA       = 0x0040
    53  	F_VARPG     = 0x0100
    54  	F_DYNLOAD   = 0x1000
    55  	F_SHROBJ    = 0x2000
    56  	F_LOADONLY  = 0x4000
    57  )
    58  
    59  // Auxiliary Header
    60  type XcoffAoutHdr64 struct {
    61  	Omagic      int16
    62  	Ovstamp     int16
    63  	Odebugger   uint32
    64  	Otextstart  uint64
    65  	Odatastart  uint64
    66  	Otoc        uint64
    67  	Osnentry    int16
    68  	Osntext     int16
    69  	Osndata     int16
    70  	Osntoc      int16
    71  	Osnloader   int16
    72  	Osnbss      int16
    73  	Oalgntext   int16
    74  	Oalgndata   int16
    75  	Omodtype    [2]byte
    76  	Ocpuflag    uint8
    77  	Ocputype    uint8
    78  	Otextpsize  uint8
    79  	Odatapsize  uint8
    80  	Ostackpsize uint8
    81  	Oflags      uint8
    82  	Otsize      uint64
    83  	Odsize      uint64
    84  	Obsize      uint64
    85  	Oentry      uint64
    86  	Omaxstack   uint64
    87  	Omaxdata    uint64
    88  	Osntdata    int16
    89  	Osntbss     int16
    90  	Ox64flags   uint16
    91  	Oresv3a     int16
    92  	Oresv3      [2]int32
    93  }
    94  
    95  // Section Header
    96  type XcoffScnHdr64 struct {
    97  	Sname    [8]byte
    98  	Spaddr   uint64
    99  	Svaddr   uint64
   100  	Ssize    uint64
   101  	Sscnptr  uint64
   102  	Srelptr  uint64
   103  	Slnnoptr uint64
   104  	Snreloc  uint32
   105  	Snlnno   uint32
   106  	Sflags   uint32
   107  }
   108  
   109  // Flags defining the section type.
   110  const (
   111  	STYP_DWARF  = 0x0010
   112  	STYP_TEXT   = 0x0020
   113  	STYP_DATA   = 0x0040
   114  	STYP_BSS    = 0x0080
   115  	STYP_EXCEPT = 0x0100
   116  	STYP_INFO   = 0x0200
   117  	STYP_TDATA  = 0x0400
   118  	STYP_TBSS   = 0x0800
   119  	STYP_LOADER = 0x1000
   120  	STYP_DEBUG  = 0x2000
   121  	STYP_TYPCHK = 0x4000
   122  	STYP_OVRFLO = 0x8000
   123  )
   124  const (
   125  	SSUBTYP_DWINFO  = 0x10000
   126  	SSUBTYP_DWLINE  = 0x20000
   127  	SSUBTYP_DWPBNMS = 0x30000
   128  	SSUBTYP_DWPBTYP = 0x40000
   129  	SSUBTYP_DWARNGE = 0x50000
   130  	SSUBTYP_DWABREV = 0x60000
   131  	SSUBTYP_DWSTR   = 0x70000
   132  	SSUBTYP_DWRNGES = 0x80000
   133  	SSUBTYP_DWLOC   = 0x90000
   134  	SSUBTYP_DWFRAME = 0xA0000
   135  	SSUBTYP_DWMAC   = 0xB0000
   136  )
   137  
   138  // Headers size
   139  const (
   140  	FILHSZ_32      = 20
   141  	FILHSZ_64      = 24
   142  	AOUTHSZ_EXEC32 = 72
   143  	AOUTHSZ_EXEC64 = 120
   144  	SCNHSZ_32      = 40
   145  	SCNHSZ_64      = 72
   146  	LDHDRSZ_32     = 32
   147  	LDHDRSZ_64     = 56
   148  	LDSYMSZ_64     = 24
   149  	RELSZ_64       = 14
   150  )
   151  
   152  // Symbol Table Entry
   153  type XcoffSymEnt64 struct {
   154  	Nvalue  uint64
   155  	Noffset uint32
   156  	Nscnum  int16
   157  	Ntype   uint16
   158  	Nsclass uint8
   159  	Nnumaux int8
   160  }
   161  
   162  const SYMESZ = 18
   163  
   164  const (
   165  	// Nscnum
   166  	N_DEBUG = -2
   167  	N_ABS   = -1
   168  	N_UNDEF = 0
   169  
   170  	//Ntype
   171  	SYM_V_INTERNAL  = 0x1000
   172  	SYM_V_HIDDEN    = 0x2000
   173  	SYM_V_PROTECTED = 0x3000
   174  	SYM_V_EXPORTED  = 0x4000
   175  	SYM_TYPE_FUNC   = 0x0020
   176  )
   177  
   178  // Storage Class.
   179  const (
   180  	C_NULL    = 0
   181  	C_EXT     = 2
   182  	C_STAT    = 3
   183  	C_BLOCK   = 100
   184  	C_FCN     = 101
   185  	C_FILE    = 103
   186  	C_HIDEXT  = 107
   187  	C_BINCL   = 108
   188  	C_EINCL   = 109
   189  	C_WEAKEXT = 111
   190  	C_DWARF   = 112
   191  	C_GSYM    = 128
   192  	C_LSYM    = 129
   193  	C_PSYM    = 130
   194  	C_RSYM    = 131
   195  	C_RPSYM   = 132
   196  	C_STSYM   = 133
   197  	C_BCOMM   = 135
   198  	C_ECOML   = 136
   199  	C_ECOMM   = 137
   200  	C_DECL    = 140
   201  	C_ENTRY   = 141
   202  	C_FUN     = 142
   203  	C_BSTAT   = 143
   204  	C_ESTAT   = 144
   205  	C_GTLS    = 145
   206  	C_STTLS   = 146
   207  )
   208  
   209  // File Auxiliary Entry
   210  type XcoffAuxFile64 struct {
   211  	Xzeroes  uint32
   212  	Xoffset  uint32
   213  	X_pad1   [6]byte
   214  	Xftype   uint8
   215  	X_pad2   [2]byte
   216  	Xauxtype uint8
   217  }
   218  
   219  // Function Auxiliary Entry
   220  type XcoffAuxFcn64 struct {
   221  	Xlnnoptr uint64
   222  	Xfsize   uint32
   223  	Xendndx  uint32
   224  	Xpad     uint8
   225  	Xauxtype uint8
   226  }
   227  
   228  // csect Auxiliary Entry.
   229  type XcoffAuxCSect64 struct {
   230  	Xscnlenlo uint32
   231  	Xparmhash uint32
   232  	Xsnhash   uint16
   233  	Xsmtyp    uint8
   234  	Xsmclas   uint8
   235  	Xscnlenhi uint32
   236  	Xpad      uint8
   237  	Xauxtype  uint8
   238  }
   239  
   240  // DWARF Auxiliary Entry
   241  type XcoffAuxDWARF64 struct {
   242  	Xscnlen  uint64
   243  	X_pad    [9]byte
   244  	Xauxtype uint8
   245  }
   246  
   247  // Xftype field
   248  const (
   249  	XFT_FN = 0
   250  	XFT_CT = 1
   251  	XFT_CV = 2
   252  	XFT_CD = 128
   253  )
   254  
   255  // Symbol type field.
   256  const (
   257  	XTY_ER  = 0
   258  	XTY_SD  = 1
   259  	XTY_LD  = 2
   260  	XTY_CM  = 3
   261  	XTY_WK  = 0x8
   262  	XTY_EXP = 0x10
   263  	XTY_ENT = 0x20
   264  	XTY_IMP = 0x40
   265  )
   266  
   267  // Storage-mapping class.
   268  const (
   269  	XMC_PR     = 0
   270  	XMC_RO     = 1
   271  	XMC_DB     = 2
   272  	XMC_TC     = 3
   273  	XMC_UA     = 4
   274  	XMC_RW     = 5
   275  	XMC_GL     = 6
   276  	XMC_XO     = 7
   277  	XMC_SV     = 8
   278  	XMC_BS     = 9
   279  	XMC_DS     = 10
   280  	XMC_UC     = 11
   281  	XMC_TC0    = 15
   282  	XMC_TD     = 16
   283  	XMC_SV64   = 17
   284  	XMC_SV3264 = 18
   285  	XMC_TL     = 20
   286  	XMC_UL     = 21
   287  	XMC_TE     = 22
   288  )
   289  
   290  // Loader Header
   291  type XcoffLdHdr64 struct {
   292  	Lversion int32
   293  	Lnsyms   int32
   294  	Lnreloc  int32
   295  	Listlen  uint32
   296  	Lnimpid  int32
   297  	Lstlen   uint32
   298  	Limpoff  uint64
   299  	Lstoff   uint64
   300  	Lsymoff  uint64
   301  	Lrldoff  uint64
   302  }
   303  
   304  // Loader Symbol
   305  type XcoffLdSym64 struct {
   306  	Lvalue  uint64
   307  	Loffset uint32
   308  	Lscnum  int16
   309  	Lsmtype int8
   310  	Lsmclas int8
   311  	Lifile  int32
   312  	Lparm   uint32
   313  }
   314  
   315  type XcoffLdImportFile64 struct {
   316  	Limpidpath string
   317  	Limpidbase string
   318  	Limpidmem  string
   319  }
   320  
   321  type XcoffLdRel64 struct {
   322  	Lvaddr  uint64
   323  	Lrtype  uint16
   324  	Lrsecnm int16
   325  	Lsymndx int32
   326  }
   327  
   328  const (
   329  	XCOFF_R_POS = 0x00
   330  	XCOFF_R_NEG = 0x01
   331  	XCOFF_R_REL = 0x02
   332  	XCOFF_R_TOC = 0x03
   333  	XCOFF_R_TRL = 0x12
   334  
   335  	XCOFF_R_TRLA = 0x13
   336  	XCOFF_R_GL   = 0x05
   337  	XCOFF_R_TCL  = 0x06
   338  	XCOFF_R_RL   = 0x0C
   339  	XCOFF_R_RLA  = 0x0D
   340  	XCOFF_R_REF  = 0x0F
   341  	XCOFF_R_BA   = 0x08
   342  	XCOFF_R_RBA  = 0x18
   343  	XCOFF_R_BR   = 0x0A
   344  	XCOFF_R_RBR  = 0x1A
   345  
   346  	XCOFF_R_TLS    = 0x20
   347  	XCOFF_R_TLS_IE = 0x21
   348  	XCOFF_R_TLS_LD = 0x22
   349  	XCOFF_R_TLS_LE = 0x23
   350  	XCOFF_R_TLSM   = 0x24
   351  	XCOFF_R_TLSML  = 0x25
   352  
   353  	XCOFF_R_TOCU = 0x30
   354  	XCOFF_R_TOCL = 0x31
   355  )
   356  
   357  type XcoffLdStr64 struct {
   358  	size uint16
   359  	name string
   360  }
   361  
   362  // Xcoffinit initialised some internal value and setups
   363  // already known header information.
   364  func Xcoffinit(ctxt *Link)
   365  
   366  // Xcoffadddynrel adds a dynamic relocation in a XCOFF file.
   367  // This relocation will be made by the loader.
   368  func Xcoffadddynrel(target *Target, ldr *loader.Loader, syms *ArchSyms, s loader.Sym, r loader.Reloc, rIdx int) bool
   369  
   370  // Create loader section and returns its size.
   371  func Loaderblk(ctxt *Link, off uint64)