github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/link/sym/reloc.go (about)

     1  // Copyright 2017 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 sym
     6  
     7  import (
     8  	"debug/elf"
     9  	"debug/macho"
    10  
    11  	"github.com/go-asm/go/cmd/objabi"
    12  	"github.com/go-asm/go/cmd/sys"
    13  )
    14  
    15  // RelocVariant is a linker-internal variation on a relocation.
    16  type RelocVariant uint8
    17  
    18  const (
    19  	RV_NONE RelocVariant = iota
    20  	RV_POWER_LO
    21  	RV_POWER_HI
    22  	RV_POWER_HA
    23  	RV_POWER_DS
    24  
    25  	// RV_390_DBL is a s390x-specific relocation variant that indicates that
    26  	// the value to be placed into the relocatable field should first be
    27  	// divided by 2.
    28  	RV_390_DBL
    29  
    30  	RV_CHECK_OVERFLOW RelocVariant = 1 << 7
    31  	RV_TYPE_MASK      RelocVariant = RV_CHECK_OVERFLOW - 1
    32  )
    33  
    34  func RelocName(arch *sys.Arch, r objabi.RelocType) string {
    35  	switch {
    36  	case r >= objabi.MachoRelocOffset: // Mach-O
    37  		nr := (r - objabi.MachoRelocOffset) >> 1
    38  		switch arch.Family {
    39  		case sys.AMD64:
    40  			return macho.RelocTypeX86_64(nr).String()
    41  		case sys.ARM64:
    42  			return macho.RelocTypeARM64(nr).String()
    43  		default:
    44  			panic("unreachable")
    45  		}
    46  	case r >= objabi.ElfRelocOffset: // ELF
    47  		nr := r - objabi.ElfRelocOffset
    48  		switch arch.Family {
    49  		case sys.AMD64:
    50  			return elf.R_X86_64(nr).String()
    51  		case sys.ARM:
    52  			return elf.R_ARM(nr).String()
    53  		case sys.ARM64:
    54  			return elf.R_AARCH64(nr).String()
    55  		case sys.I386:
    56  			return elf.R_386(nr).String()
    57  		case sys.Loong64:
    58  			return elf.R_LARCH(nr).String()
    59  		case sys.MIPS, sys.MIPS64:
    60  			return elf.R_MIPS(nr).String()
    61  		case sys.PPC64:
    62  			return elf.R_PPC64(nr).String()
    63  		case sys.S390X:
    64  			return elf.R_390(nr).String()
    65  		case sys.RISCV64:
    66  			return elf.R_RISCV(nr).String()
    67  		default:
    68  			panic("unreachable")
    69  		}
    70  	}
    71  
    72  	return r.String()
    73  }