github.com/pkujhd/goloader@v0.0.0-20240411034752-1a28096bd7bd/objabi/tls/tls.1.11.go (about) 1 //go:build go1.11 && !go1.13 2 // +build go1.11,!go1.13 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 * For x86, Apple has reserved a slot in the TLS for Go. See issue 23617. 46 * That slot is at offset 0x30 on amd64, and 0x18 on 386. 47 * The slot will hold the G pointer. 48 * These constants should match those in runtime/sys_darwin_{386,amd64}.s 49 * and runtime/cgo/gcc_darwin_{386,amd64}.c. 50 */ 51 case Hdarwin: 52 switch arch.Name { 53 case sys.Arch386.Name: 54 return uintptr(0x18) 55 case sys.ArchAMD64.Name: 56 return uintptr(0x30) 57 case sys.ArchARM64.Name, 58 sys.ArchARM.Name: 59 return 0x0 60 default: 61 panic(fmt.Sprintf("unknown thread-local storage offset for darwin/%s", arch.Name)) 62 } 63 default: 64 panic(fmt.Sprintf("undealed GetTLSOffset on os:%s", runtime.GOOS)) 65 } 66 }