github.com/pkujhd/goloader@v0.0.0-20240411034752-1a28096bd7bd/objabi/tls/tls.1.8.go (about)

     1  //go:build go1.8 && !go1.11
     2  // +build go1.8,!go1.11
     3  
     4  package tls
     5  
     6  import (
     7  	"cmd/objfile/sys"
     8  	"fmt"
     9  	"runtime"
    10  )
    11  
    12  //see:src/cmd/link/internal/ld/sym.go
    13  func GetTLSOffset(arch *sys.Arch, ptrsize int) uintptr {
    14  	switch GetHeadType() {
    15  	case Hwindows:
    16  		return 0x0
    17  	/*
    18  	 * ELF uses TLS offset negative from FS.
    19  	 * Translate 0(FS) and 8(FS) into -16(FS) and -8(FS).
    20  	 * Known to low-level assembly in package runtime and runtime/cgo.
    21  	 */
    22  	case Hlinux,
    23  		Hfreebsd,
    24  		Hnetbsd,
    25  		Hopenbsd,
    26  		Hdragonfly,
    27  		Hsolaris:
    28  		if runtime.GOOS == "android" {
    29  			switch arch.Name {
    30  			case sys.ArchAMD64.Name:
    31  				// Android/amd64 constant - offset from 0(FS) to our TLS slot.
    32  				// Explained in src/runtime/cgo/gcc_android_*.c
    33  				return 0x1d0
    34  			case sys.Arch386.Name:
    35  				// Android/386 constant - offset from 0(GS) to our TLS slot.
    36  				return 0xf8
    37  			default:
    38  				offset := -1 * ptrsize
    39  				return uintptr(offset)
    40  			}
    41  		}
    42  		offset := -1 * ptrsize
    43  		return uintptr(offset)
    44  	/*
    45  	 * OS X system constants - offset from 0(GS) to our TLS.
    46  	 * Explained in src/runtime/cgo/gcc_darwin_*.c.
    47  	 */
    48  	case Hdarwin:
    49  		switch arch.Name {
    50  		case sys.Arch386.Name:
    51  			return uintptr(0x468)
    52  		case sys.ArchAMD64.Name:
    53  			return uintptr(0x8a0)
    54  		case sys.ArchARM64.Name,
    55  			sys.ArchARM.Name:
    56  			return 0x0
    57  		default:
    58  			panic(fmt.Sprintf("unknown thread-local storage offset for darwin/%s", arch.Name))
    59  		}
    60  	default:
    61  		panic(fmt.Sprintf("undealed GetTLSOffset on os:%s", runtime.GOOS))
    62  	}
    63  }