From 7d06d2c03272d5ce8d61468dcd7a792e73528222 Mon Sep 17 00:00:00 2001 From: aarzilli Date: Wed, 28 Nov 2018 18:26:01 +0100 Subject: [PATCH] pkg/proc: align memory size of tls arena to pointer sized boundary The size of the TLS memory arena needs to be aligned to pointer sized boundaries on 86x64 architectures, otherwise some programs using cgo will not have the correct offset for the g struct. No tests because reproducing this problem depends on behavior of the GNU ld linker caused by unclear influences. Fixes #1428. --- pkg/proc/bininfo.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/proc/bininfo.go b/pkg/proc/bininfo.go index d6c9e316..9ac30e6c 100644 --- a/pkg/proc/bininfo.go +++ b/pkg/proc/bininfo.go @@ -753,9 +753,12 @@ func (bi *BinaryInfo) setGStructOffsetElf(exe *elf.File, wg *sync.WaitGroup) { break } } + memsz := tls.Memsz + + memsz = (memsz + uint64(bi.Arch.PtrSize()) - 1) & ^uint64(bi.Arch.PtrSize()-1) // align to pointer-sized-boundary // The TLS register points to the end of the TLS block, which is // tls.Memsz long. runtime.tlsg is an offset from the beginning of that block. - bi.gStructOffset = ^(tls.Memsz) + 1 + tlsg.Value // -tls.Memsz + tlsg.Value + bi.gStructOffset = ^(memsz) + 1 + tlsg.Value // -tls.Memsz + tlsg.Value } // PE //////////////////////////////////////////////////////////////// -- GitLab