Skip to content

Commit

Permalink
cmd/internal/obj: some platform independent bits of proper toolchain …
Browse files Browse the repository at this point in the history
…support for thread local storage

Also simplifies some silliness around making the .tbss section wrt internal
vs external linking.

Issue golang#11270

Change-Id: Ia4fa135cb22d916728ead95bdbc0ebc1ae06f05c
  • Loading branch information
mwhudson committed Aug 27, 2015
1 parent e4bdff2 commit 6396988
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/cmd/internal/obj/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func savedata(ctxt *Link, s *LSym, p *Prog, pn string) {
if ctxt.Enforce_data_order != 0 && off < int32(len(s.P)) {
ctxt.Diag("data out of order (already have %d)\n%v", len(s.P), p)
}
if s.Type == SBSS || s.Type == STLSBSS {
ctxt.Diag("cannot supply data for BSS var")
}
Symgrow(ctxt, s, int64(off+siz))

switch int(p.To.Type) {
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/internal/obj/objfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ func Writeobjdirect(ctxt *Link, b *Biobuf) {
s.Type = SRODATA
} else if flag&NOPTR != 0 {
s.Type = SNOPTRBSS
} else if flag&TLSBSS != 0 {
s.Type = STLSBSS
}
edata = s
continue
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/internal/obj/textflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ const (

// When passed to ggloblsym, causes Local to be set to true on the LSym it creates.
LOCAL = 128

// Allocate a word of thread local storage and store the offset from the
// thread local base to this storage in this variable.
TLSBSS = 256
)
27 changes: 13 additions & 14 deletions src/cmd/link/internal/ld/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -1401,26 +1401,25 @@ func dodata() {
Diag("data or bss segment too large")
}

if Iself && Linkmode == LinkExternal && s != nil && s.Type == obj.STLSBSS && HEADTYPE != obj.Hopenbsd {
sect := addsection(&Segdata, ".tbss", 06)
sect.Align = int32(Thearch.Ptrsize)
sect.Vaddr = 0
datsize = 0
if s != nil && s.Type == obj.STLSBSS {
if Iself && (Linkmode == LinkExternal || Debug['d'] == 0) && HEADTYPE != obj.Hopenbsd {
sect = addsection(&Segdata, ".tbss", 06)
sect.Align = int32(Thearch.Ptrsize)
sect.Vaddr = 0
datsize = 0
} else {
sect = nil
}

for ; s != nil && s.Type == obj.STLSBSS; s = s.Next {
datsize = aligndatsize(datsize, s)
s.Sect = sect
s.Value = int64(uint64(datsize) - sect.Vaddr)
s.Value = datsize
growdatsize(&datsize, s)
}

sect.Length = uint64(datsize)
} else {
// Might be internal linking but still using cgo.
// In that case, the only possible STLSBSS symbol is runtime.tlsg.
// Give it offset 0, because it's the only thing here.
if s != nil && s.Type == obj.STLSBSS && s.Name == "runtime.tlsg" {
s.Value = 0
s = s.Next
if sect != nil {
sect.Length = uint64(datsize)
}
}

Expand Down
15 changes: 3 additions & 12 deletions src/cmd/link/internal/ld/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2287,11 +2287,12 @@ func Asmbelf(symo int64) {
// Do not emit PT_TLS for OpenBSD since ld.so(1) does
// not currently support it. This is handled
// appropriately in runtime/cgo.
if Ctxt.Tlsoffset != 0 && HEADTYPE != obj.Hopenbsd {
tlssize := elfshname(".tbss").size
if tlssize != 0 && HEADTYPE != obj.Hopenbsd {
ph := newElfPhdr()
ph.type_ = PT_TLS
ph.flags = PF_R
ph.memsz = uint64(-Ctxt.Tlsoffset)
ph.memsz = tlssize
ph.align = uint64(Thearch.Regsize)
}
}
Expand Down Expand Up @@ -2350,16 +2351,6 @@ elfobj:
sh.flags = 0
}

// generate .tbss section for dynamic internal linking (except for OpenBSD)
// external linking generates .tbss in data.c
if Linkmode == LinkInternal && Debug['d'] == 0 && HEADTYPE != obj.Hopenbsd {
sh := elfshname(".tbss")
sh.type_ = SHT_NOBITS
sh.addralign = uint64(Thearch.Regsize)
sh.size = uint64(-Ctxt.Tlsoffset)
sh.flags = SHF_ALLOC | SHF_TLS | SHF_WRITE
}

if Debug['s'] == 0 {
sh := elfshname(".symtab")
sh.type_ = SHT_SYMTAB
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/textflag.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
#define WRAPPER 32
// This function uses its incoming context register.
#define NEEDCTXT 64
// Allocate a word of thread local storage and store the offset from the
// thread local base to this storage in this variable.
#define TLSBSS 256

0 comments on commit 6396988

Please sign in to comment.