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

     1  //go:build go1.13 && !go1.23
     2  // +build go1.13,!go1.23
     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  		offset := -1 * ptrsize
    29  		return uintptr(offset)
    30  		/*
    31  		 * For x86, Apple has reserved a slot in the TLS for Go. See issue 23617.
    32  		 * That slot is at offset 0x30 on amd64, and 0x18 on 386.
    33  		 * The slot will hold the G pointer.
    34  		 * These constants should match those in runtime/sys_darwin_{386,amd64}.s
    35  		 * and runtime/cgo/gcc_darwin_{386,amd64}.c.
    36  		 */
    37  	case Hdarwin:
    38  		switch arch.Name {
    39  		case sys.Arch386.Name:
    40  			return uintptr(0x18)
    41  		case sys.ArchAMD64.Name:
    42  			return uintptr(0x30)
    43  		case sys.ArchARM64.Name,
    44  			sys.ArchARM.Name:
    45  			return 0x0
    46  		default:
    47  			panic(fmt.Sprintf("unknown thread-local storage offset for darwin/%s", arch.Name))
    48  		}
    49  	default:
    50  		panic(fmt.Sprintf("undealed GetTLSOffset on os:%s", runtime.GOOS))
    51  	}
    52  }