summaryrefslogtreecommitdiff
path: root/src/cmd/dist/buildruntime.c
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2014-04-15 13:45:39 -0400
committerRuss Cox <rsc@golang.org>2014-04-15 13:45:39 -0400
commit68ae8a20c01f21456b99dca0ad960db740a4153c (patch)
treea4b9352eff281fe453b92ed8016300a522ed939f /src/cmd/dist/buildruntime.c
parent6a424d127f2cd87fc070c7647cc3972496dd5644 (diff)
downloadgo-68ae8a20c01f21456b99dca0ad960db740a4153c.tar.gz
liblink: introduce TLS register on 386 and amd64
When I did the original 386 ports on Linux and OS X, I chose to define GS-relative expressions like 4(GS) as relative to the actual thread-local storage base, which was usually GS but might not be (it might be FS, or it might be a different constant offset from GS or FS). The original scope was limited but since then the rewrites have gotten out of control. Sometimes GS is rewritten, sometimes FS. Some ports do other rewrites to enable shared libraries and other linking. At no point in the code is it clear whether you are looking at the real GS/FS or some synthesized thing that will be rewritten. The code manipulating all these is duplicated in many places. The first step to fixing issue 7719 is to make the code intelligible again. This CL adds an explicit TLS pseudo-register to the 386 and amd64. As a register, TLS refers to the thread-local storage base, and it can only be loaded into another register: MOVQ TLS, AX An offset from the thread-local storage base is written off(reg)(TLS*1). Semantically it is off(reg), but the (TLS*1) annotation marks this as indexing from the loaded TLS base. This emits a relocation so that if the linker needs to adjust the offset, it can. For example: MOVQ TLS, AX MOVQ 8(AX)(TLS*1), CX // load m into CX On systems that support direct access to the TLS memory, this pair of instructions can be reduced to a direct TLS memory reference: MOVQ 8(TLS), CX // load m into CX The 2-instruction and 1-instruction forms correspond roughly to ELF TLS initial exec mode and ELF TLS local exec mode, respectively. Liblink applies this rewrite on systems that support the 1-instruction form. The decision is made using only the operating system (and probably the -shared flag, eventually), not the link mode. If some link modes on a particular operating system require the 2-instruction form, then all builds for that operating system will use the 2-instruction form, so that the link mode decision can be delayed to link time. Obviously it is late to be making changes like this, but I despair of correcting issue 7719 and issue 7164 without it. To make sure I am not changing existing behavior, I built a "hello world" program for every GOOS/GOARCH combination we have and then worked to make sure that the rewrite generates exactly the same binaries, byte for byte. There are a handful of TODOs in the code marking kludges to get the byte-for-byte property, but at least now I can explain exactly how each binary is handled. The targets I tested this way are: darwin-386 darwin-amd64 dragonfly-386 dragonfly-amd64 freebsd-386 freebsd-amd64 freebsd-arm linux-386 linux-amd64 linux-arm nacl-386 nacl-amd64p32 netbsd-386 netbsd-amd64 openbsd-386 openbsd-amd64 plan9-386 plan9-amd64 solaris-amd64 windows-386 windows-amd64 There were four exceptions to the byte-for-byte goal: windows-386 and windows-amd64 have a time stamp at bytes 137 and 138 of the header. darwin-386 and plan9-386 have five or six modified bytes in the middle of the Go symbol table, caused by editing comments in runtime/sys_{darwin,plan9}_386.s. Fixes issue 7164. LGTM=iant R=iant, aram, minux.ma, dave CC=golang-codereviews https://codereview.appspot.com/87920043
Diffstat (limited to 'src/cmd/dist/buildruntime.c')
-rw-r--r--src/cmd/dist/buildruntime.c99
1 files changed, 11 insertions, 88 deletions
diff --git a/src/cmd/dist/buildruntime.c b/src/cmd/dist/buildruntime.c
index e2d46cdac..ba5993b2f 100644
--- a/src/cmd/dist/buildruntime.c
+++ b/src/cmd/dist/buildruntime.c
@@ -127,99 +127,22 @@ static struct {
char *goos;
char *hdr;
} zasmhdr[] = {
- {"386", "windows",
- "#define get_tls(r) MOVL 0x14(FS), r\n"
- "#define g(r) 0(r)\n"
- "#define m(r) 4(r)\n"
- },
- {"386", "plan9",
- "// Plan 9 does not have per-process segment descriptors with\n"
- "// which to do thread-local storage. Instead, we will use a\n"
- "// fixed offset from the per-process TOS struct address for\n"
- "// the local storage. Since the process ID is contained in the\n"
- "// TOS struct, we specify an offset for that here as well.\n"
- "#define get_tls(r) MOVL _tos(SB), r \n"
- "#define g(r) -8(r)\n"
- "#define m(r) -4(r)\n"
- "#define procid(r) 48(r)\n"
- },
- {"386", "linux",
- "// On Linux systems, what we call 0(GS) and 4(GS) for g and m\n"
- "// turn into %gs:-8 and %gs:-4 (using gcc syntax to denote\n"
- "// what the machine sees as opposed to 8l input).\n"
- "// 8l rewrites 0(GS) and 4(GS) into these.\n"
- "//\n"
- "// On Linux Xen, it is not allowed to use %gs:-8 and %gs:-4\n"
- "// directly. Instead, we have to store %gs:0 into a temporary\n"
- "// register and then use -8(%reg) and -4(%reg). This kind\n"
- "// of addressing is correct even when not running Xen.\n"
- "//\n"
- "// 8l can rewrite MOVL 0(GS), CX into the appropriate pair\n"
- "// of mov instructions, using CX as the intermediate register\n"
- "// (safe because CX is about to be written to anyway).\n"
- "// But 8l cannot handle other instructions, like storing into 0(GS),\n"
- "// which is where these macros come into play.\n"
- "// get_tls sets up the temporary and then g and r use it.\n"
- "//\n"
- "// Another wrinkle is that get_tls needs to read from %gs:0,\n"
- "// but in 8l input it's called 8(GS), because 8l is going to\n"
- "// subtract 8 from all the offsets, as described above.\n"
- "//\n"
- "// The final wrinkle is that when generating an ELF .o file for\n"
- "// external linking mode, we need to be able to relocate the\n"
- "// -8(r) and -4(r) instructions. Tag them with an extra (GS*1)\n"
- "// that is ignored by the linker except for that identification.\n"
- "#define get_tls(r) MOVL 8(GS), r\n"
- "#define g(r) -8(r)(GS*1)\n"
- "#define m(r) -4(r)(GS*1)\n"
- },
- {"386", "nacl",
- // Same as Linux above.
- "#define get_tls(r) MOVL 8(GS), r\n"
- "#define g(r) -8(r)(GS*1)\n"
- "#define m(r) -4(r)(GS*1)\n"
- },
{"386", "",
- "#define get_tls(r)\n"
- "#define g(r) 0(GS)\n"
- "#define m(r) 4(GS)\n"
- },
-
- {"amd64p32", "nacl",
- "#define get_tls(r)\n"
- "#define g(r) 0(GS)\n"
- "#define m(r) 4(GS)\n"
- },
- {"amd64", "windows",
- "#define get_tls(r) MOVQ 0x28(GS), r\n"
- "#define g(r) 0(r)\n"
- "#define m(r) 8(r)\n"
- },
- {"amd64", "plan9",
- "#define get_tls(r)\n"
- "#define g(r) 0(GS)\n"
- "#define m(r) 8(GS)\n"
- "#define procid(r) 16(GS)\n"
+ "#define get_tls(r) MOVL TLS, r\n"
+ "#define g(r) 0(r)(TLS*1)\n"
+ "#define m(r) 4(r)(TLS*1)\n"
},
- {"amd64", "solaris",
- "#define get_tls(r) MOVQ 0(FS), r\n"
- "#define g(r) -16(r)(FS*1)\n"
- "#define m(r) -8(r)(FS*1)\n"
- },
- // The TLS accessors here are defined here to use initial exec model.
- // If the linker is not outputting a shared library, it will reduce
- // the TLS accessors to the local exec model, effectively removing
- // get_tls().
- {"amd64", "linux",
- "#define get_tls(r) MOVQ runtime·tlsgm(SB), r\n"
- "#define g(r) 0(r)(GS*1)\n"
- "#define m(r) 8(r)(GS*1)\n"
+ {"amd64p32", "",
+ "#define get_tls(r) MOVL TLS, r\n"
+ "#define g(r) 0(r)(TLS*1)\n"
+ "#define m(r) 4(r)(TLS*1)\n"
},
{"amd64", "",
- "#define get_tls(r)\n"
- "#define g(r) 0(GS)\n"
- "#define m(r) 8(GS)\n"
+ "#define get_tls(r) MOVQ TLS, r\n"
+ "#define g(r) 0(r)(TLS*1)\n"
+ "#define m(r) 8(r)(TLS*1)\n"
},
+
{"arm", "",
"#define LR R14\n"
},