Skip to content

Commit c7e8556

Browse files
committed
cmd/link: trampoline support for external linking on ARM
all.bash passes with -debugtramp=2 (except the unavoidable disassembly test as we change instructions). And successfully build k8s.io/kubernetes/cmd/hyperkube in both internal linking and external linking mode. Fixes #17028. Change-Id: Ic8fac6a394488155c5eba9215662db1c1086e24b Reviewed-on: https://go-review.googlesource.com/31143 Reviewed-by: David Chase <[email protected]>
1 parent 007c907 commit c7e8556

File tree

3 files changed

+126
-26
lines changed

3 files changed

+126
-26
lines changed

src/cmd/link/internal/arm/asm.go

+111-10
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,17 @@ func signext24(x int64) int32 {
416416
return (int32(x) << 8) >> 8
417417
}
418418

419+
// encode an immediate in ARM's imm12 format. copied from ../../../internal/obj/arm/asm5.go
420+
func immrot(v uint32) uint32 {
421+
for i := 0; i < 16; i++ {
422+
if v&^0xff == 0 {
423+
return uint32(i<<8) | v | 1<<25
424+
}
425+
v = v<<2 | v>>30
426+
}
427+
return 0
428+
}
429+
419430
// Convert the direct jump relocation r to refer to a trampoline if the target is too far
420431
func trampoline(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol) {
421432
switch r.Type {
@@ -424,12 +435,18 @@ func trampoline(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol) {
424435
// low 24-bit encodes the target address
425436
t := (ld.Symaddr(r.Sym) + int64(signext24(r.Add&0xffffff)*4) - (s.Value + int64(r.Off))) / 4
426437
if t > 0x7fffff || t < -0x800000 || (*ld.FlagDebugTramp > 1 && s.File != r.Sym.File) {
427-
// direct call too far, need to insert trampoline
438+
// direct call too far, need to insert trampoline.
439+
// look up existing trampolines first. if we found one within the range
440+
// of direct call, we can reuse it. otherwise create a new one.
428441
offset := (signext24(r.Add&0xffffff) + 2) * 4
429442
var tramp *ld.Symbol
430443
for i := 0; ; i++ {
431444
name := r.Sym.Name + fmt.Sprintf("%+d-tramp%d", offset, i)
432445
tramp = ctxt.Syms.Lookup(name, int(r.Sym.Version))
446+
if tramp.Type == obj.SDYNIMPORT {
447+
// don't reuse trampoline defined in other module
448+
continue
449+
}
433450
if tramp.Value == 0 {
434451
// either the trampoline does not exist -- we need to create one,
435452
// or found one the address which is not assigned -- this will be
@@ -447,15 +464,16 @@ func trampoline(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol) {
447464
if tramp.Type == 0 {
448465
// trampoline does not exist, create one
449466
ctxt.AddTramp(tramp)
450-
tramp.Size = 12 // 3 instructions
451-
tramp.P = make([]byte, tramp.Size)
452-
t = ld.Symaddr(r.Sym) + int64(offset)
453-
o1 := uint32(0xe5900000 | 11<<12 | 15<<16) // MOVW (R15), R11 // R15 is actual pc + 8
454-
o2 := uint32(0xe12fff10 | 11) // JMP (R11)
455-
o3 := uint32(t) // WORD $target
456-
ld.SysArch.ByteOrder.PutUint32(tramp.P, o1)
457-
ld.SysArch.ByteOrder.PutUint32(tramp.P[4:], o2)
458-
ld.SysArch.ByteOrder.PutUint32(tramp.P[8:], o3)
467+
if ctxt.DynlinkingGo() {
468+
if immrot(uint32(offset)) == 0 {
469+
ld.Errorf(s, "odd offset in dynlink direct call: %v+%d", r.Sym, offset)
470+
}
471+
gentrampdyn(tramp, r.Sym, int64(offset))
472+
} else if ld.Buildmode == ld.BuildmodeCArchive || ld.Buildmode == ld.BuildmodeCShared || ld.Buildmode == ld.BuildmodePIE {
473+
gentramppic(tramp, r.Sym, int64(offset))
474+
} else {
475+
gentramp(tramp, r.Sym, int64(offset))
476+
}
459477
}
460478
// modify reloc to point to tramp, which will be resolved later
461479
r.Sym = tramp
@@ -467,6 +485,89 @@ func trampoline(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol) {
467485
}
468486
}
469487

488+
// generate a trampoline to target+offset
489+
func gentramp(tramp, target *ld.Symbol, offset int64) {
490+
tramp.Size = 12 // 3 instructions
491+
tramp.P = make([]byte, tramp.Size)
492+
t := ld.Symaddr(target) + int64(offset)
493+
o1 := uint32(0xe5900000 | 11<<12 | 15<<16) // MOVW (R15), R11 // R15 is actual pc + 8
494+
o2 := uint32(0xe12fff10 | 11) // JMP (R11)
495+
o3 := uint32(t) // WORD $target
496+
ld.SysArch.ByteOrder.PutUint32(tramp.P, o1)
497+
ld.SysArch.ByteOrder.PutUint32(tramp.P[4:], o2)
498+
ld.SysArch.ByteOrder.PutUint32(tramp.P[8:], o3)
499+
500+
if ld.Linkmode == ld.LinkExternal {
501+
r := ld.Addrel(tramp)
502+
r.Off = 8
503+
r.Type = obj.R_ADDR
504+
r.Siz = 4
505+
r.Sym = target
506+
r.Add = offset
507+
}
508+
}
509+
510+
// generate a trampoline to target+offset in position independent code
511+
func gentramppic(tramp, target *ld.Symbol, offset int64) {
512+
tramp.Size = 16 // 4 instructions
513+
tramp.P = make([]byte, tramp.Size)
514+
o1 := uint32(0xe5900000 | 11<<12 | 15<<16 | 4) // MOVW 4(R15), R11 // R15 is actual pc + 8
515+
o2 := uint32(0xe0800000 | 11<<12 | 15<<16 | 11) // ADD R15, R11, R11
516+
o3 := uint32(0xe12fff10 | 11) // JMP (R11)
517+
o4 := uint32(0) // WORD $(target-pc) // filled in with relocation
518+
ld.SysArch.ByteOrder.PutUint32(tramp.P, o1)
519+
ld.SysArch.ByteOrder.PutUint32(tramp.P[4:], o2)
520+
ld.SysArch.ByteOrder.PutUint32(tramp.P[8:], o3)
521+
ld.SysArch.ByteOrder.PutUint32(tramp.P[12:], o4)
522+
523+
r := ld.Addrel(tramp)
524+
r.Off = 12
525+
r.Type = obj.R_PCREL
526+
r.Siz = 4
527+
r.Sym = target
528+
r.Add = offset + 4
529+
}
530+
531+
// generate a trampoline to target+offset in dynlink mode (using GOT)
532+
func gentrampdyn(tramp, target *ld.Symbol, offset int64) {
533+
tramp.Size = 20 // 5 instructions
534+
o1 := uint32(0xe5900000 | 11<<12 | 15<<16 | 8) // MOVW 8(R15), R11 // R15 is actual pc + 8
535+
o2 := uint32(0xe0800000 | 11<<12 | 15<<16 | 11) // ADD R15, R11, R11
536+
o3 := uint32(0xe5900000 | 11<<12 | 11<<16) // MOVW (R11), R11
537+
o4 := uint32(0xe12fff10 | 11) // JMP (R11)
538+
o5 := uint32(0) // WORD $target@GOT // filled in with relocation
539+
o6 := uint32(0)
540+
if offset != 0 {
541+
// insert an instruction to add offset
542+
tramp.Size = 24 // 6 instructions
543+
o6 = o5
544+
o5 = o4
545+
o4 = uint32(0xe2800000 | 11<<12 | 11<<16 | immrot(uint32(offset))) // ADD $offset, R11, R11
546+
o1 = uint32(0xe5900000 | 11<<12 | 15<<16 | 12) // MOVW 12(R15), R11
547+
}
548+
tramp.P = make([]byte, tramp.Size)
549+
ld.SysArch.ByteOrder.PutUint32(tramp.P, o1)
550+
ld.SysArch.ByteOrder.PutUint32(tramp.P[4:], o2)
551+
ld.SysArch.ByteOrder.PutUint32(tramp.P[8:], o3)
552+
ld.SysArch.ByteOrder.PutUint32(tramp.P[12:], o4)
553+
ld.SysArch.ByteOrder.PutUint32(tramp.P[16:], o5)
554+
if offset != 0 {
555+
ld.SysArch.ByteOrder.PutUint32(tramp.P[20:], o6)
556+
}
557+
558+
r := ld.Addrel(tramp)
559+
r.Off = 16
560+
r.Type = obj.R_GOTPCREL
561+
r.Siz = 4
562+
r.Sym = target
563+
r.Add = 8
564+
if offset != 0 {
565+
// increase reloc offset by 4 as we inserted an ADD instruction
566+
r.Off = 20
567+
r.Add = 12
568+
}
569+
}
570+
470571
func archreloc(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, val *int64) int {
471572
if ld.Linkmode == ld.LinkExternal {
472573
switch r.Type {

src/cmd/link/internal/ld/data.go

-3
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,6 @@ func trampoline(ctxt *Link, s *Symbol) {
330330
if Thearch.Trampoline == nil {
331331
return // no need or no support of trampolines on this arch
332332
}
333-
if Linkmode == LinkExternal {
334-
return // currently only support internal linking
335-
}
336333

337334
for ri := range s.R {
338335
r := &s.R[ri]

src/cmd/link/linkbig_test.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,41 @@ import (
1212
"bytes"
1313
"cmd/internal/obj"
1414
"fmt"
15+
"internal/testenv"
1516
"io/ioutil"
1617
"os"
1718
"os/exec"
1819
"testing"
1920
)
2021

2122
func TestLargeText(t *testing.T) {
23+
if testing.Short() || (obj.GOARCH != "ppc64le" && obj.GOARCH != "ppc64" && obj.GOARCH != "arm") {
24+
t.Skip("Skipping large text section test in short mode or on %s", obj.GOARCH)
25+
}
26+
testenv.MustHaveGoBuild(t)
2227

2328
var w bytes.Buffer
24-
25-
if testing.Short() || (obj.GOARCH != "ppc64le" && obj.GOARCH != "ppc64") {
26-
t.Skip("Skipping large text section test in short mode or if not ppc64x")
27-
}
2829
const FN = 4
2930
tmpdir, err := ioutil.TempDir("", "bigtext")
3031

3132
defer os.RemoveAll(tmpdir)
3233

3334
// Generate the scenario where the total amount of text exceeds the
34-
// limit for the bl instruction, on RISC architectures like ppc64le,
35+
// limit for the jmp/call instruction, on RISC architectures like ppc64le,
3536
// which is 2^26. When that happens the call requires special trampolines or
3637
// long branches inserted by the linker where supported.
37-
3838
// Multiple .s files are generated instead of one.
39-
39+
instOnArch := map[string]string{
40+
"ppc64": "\tMOVD\tR0,R3\n",
41+
"ppc64le": "\tMOVD\tR0,R3\n",
42+
"arm": "\tMOVW\tR0,R1\n",
43+
}
44+
inst := instOnArch[obj.GOARCH]
4045
for j := 0; j < FN; j++ {
4146
testname := fmt.Sprintf("bigfn%d", j)
4247
fmt.Fprintf(&w, "TEXT ·%s(SB),$0\n", testname)
4348
for i := 0; i < 2200000; i++ {
44-
fmt.Fprintf(&w, "\tMOVD\tR0,R3\n")
49+
fmt.Fprintf(&w, inst)
4550
}
4651
fmt.Fprintf(&w, "\tRET\n")
4752
err := ioutil.WriteFile(tmpdir+"/"+testname+".s", w.Bytes(), 0666)
@@ -64,7 +69,6 @@ func TestLargeText(t *testing.T) {
6469
// There are lots of dummy code generated in the .s files just to generate a lot
6570
// of text. Link them in but guard their call so their code is not executed but
6671
// the main part of the program can be run.
67-
6872
fmt.Fprintf(&w, "\tif os.Getenv(\"LINKTESTARG\") != \"\" {\n")
6973
for i := 0; i < FN; i++ {
7074
fmt.Fprintf(&w, "\t\tbigfn%d()\n", i)
@@ -78,9 +82,8 @@ func TestLargeText(t *testing.T) {
7882
}
7983

8084
// Build and run with internal linking.
81-
8285
os.Chdir(tmpdir)
83-
cmd := exec.Command("go", "build", "-o", "bigtext")
86+
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "bigtext")
8487
out, err := cmd.CombinedOutput()
8588
if err != nil {
8689
t.Fatalf("Build failed for big text program with internal linking: %v, output: %s", err, out)
@@ -92,9 +95,8 @@ func TestLargeText(t *testing.T) {
9295
}
9396

9497
// Build and run with external linking
95-
9698
os.Chdir(tmpdir)
97-
cmd = exec.Command("go", "build", "-o", "bigtext", "-ldflags", "'-linkmode=external'")
99+
cmd = exec.Command(testenv.GoToolPath(t), "build", "-o", "bigtext", "-ldflags", "'-linkmode=external'")
98100
out, err = cmd.CombinedOutput()
99101
if err != nil {
100102
t.Fatalf("Build failed for big text program with external linking: %v, output: %s", err, out)

0 commit comments

Comments
 (0)