diff mbox

libgo patch committed: support SPARC64/ELF relocs

Message ID CAOyqgcUMbLJDvaCQ-EUYKzbpDoUmDW82pAZ0a4th6zG+R6eH-w@mail.gmail.com
State New
Headers show

Commit Message

Ian Lance Taylor Oct. 14, 2016, 5:16 p.m. UTC
This patch by James Clarke adds support for SPARC64/ELF relocs to the
debug/elf package in libgo.  This is a backport of
https://golang.org/cl/30870 from the master library.  Bootstrapped and
ran Go testsuite on x86_64-pc-linux-gnu.  Committed to mainline.

Ian
Index: libgo/go/debug/elf/testdata/go-relocation-test-gcc620-sparc64.obj
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: libgo/go/debug/elf/testdata/go-relocation-test-gcc620-sparc64.obj
===================================================================
--- libgo/go/debug/elf/testdata/go-relocation-test-gcc620-sparc64.obj	(revision 0)
+++ libgo/go/debug/elf/testdata/go-relocation-test-gcc620-sparc64.obj	(working copy)

Property changes on: libgo/go/debug/elf/testdata/go-relocation-test-gcc620-sparc64.obj
diff mbox

Patch

Index: gcc/go/gofrontend/MERGE
===================================================================
--- gcc/go/gofrontend/MERGE	(revision 241163)
+++ gcc/go/gofrontend/MERGE	(working copy)
@@ -1,4 +1,4 @@ 
-5f043fc2bf0f92a84a1f7da57acd79a61c9d2592
+911fceabd4c955b2f29f6b532f241a002ca7ad4f
 
 The first line of this file holds the git revision number of the last
 merge done from the gofrontend repository.
Index: libgo/go/debug/elf/file.go
===================================================================
--- libgo/go/debug/elf/file.go	(revision 240942)
+++ libgo/go/debug/elf/file.go	(working copy)
@@ -598,6 +598,8 @@  func (f *File) applyRelocations(dst []by
 		return f.applyRelocationsMIPS64(dst, rels)
 	case f.Class == ELFCLASS64 && f.Machine == EM_S390:
 		return f.applyRelocationss390x(dst, rels)
+	case f.Class == ELFCLASS64 && f.Machine == EM_SPARCV9:
+		return f.applyRelocationsSPARC64(dst, rels)
 	default:
 		return errors.New("applyRelocations: not implemented")
 	}
@@ -959,6 +961,51 @@  func (f *File) applyRelocationss390x(dst
 		}
 	}
 
+	return nil
+}
+
+func (f *File) applyRelocationsSPARC64(dst []byte, rels []byte) error {
+	// 24 is the size of Rela64.
+	if len(rels)%24 != 0 {
+		return errors.New("length of relocation section is not a multiple of 24")
+	}
+
+	symbols, _, err := f.getSymbols(SHT_SYMTAB)
+	if err != nil {
+		return err
+	}
+
+	b := bytes.NewReader(rels)
+	var rela Rela64
+
+	for b.Len() > 0 {
+		binary.Read(b, f.ByteOrder, &rela)
+		symNo := rela.Info >> 32
+		t := R_SPARC(rela.Info & 0xffff)
+
+		if symNo == 0 || symNo > uint64(len(symbols)) {
+			continue
+		}
+		sym := &symbols[symNo-1]
+		if SymType(sym.Info&0xf) != STT_SECTION {
+			// We don't handle non-section relocations for now.
+			continue
+		}
+
+		switch t {
+		case R_SPARC_64, R_SPARC_UA64:
+			if rela.Off+8 >= uint64(len(dst)) || rela.Addend < 0 {
+				continue
+			}
+			f.ByteOrder.PutUint64(dst[rela.Off:rela.Off+8], uint64(rela.Addend))
+		case R_SPARC_32, R_SPARC_UA32:
+			if rela.Off+4 >= uint64(len(dst)) || rela.Addend < 0 {
+				continue
+			}
+			f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], uint32(rela.Addend))
+		}
+	}
+
 	return nil
 }
 
Index: libgo/go/debug/elf/file_test.go
===================================================================
--- libgo/go/debug/elf/file_test.go	(revision 240942)
+++ libgo/go/debug/elf/file_test.go	(working copy)
@@ -492,6 +492,25 @@  var relocationTests = []relocationTest{
 		},
 	},
 	{
+		"testdata/go-relocation-test-gcc620-sparc64.obj",
+		[]relocationTestEntry{
+			{0, &dwarf.Entry{
+				Offset:   0xb,
+				Tag:      dwarf.TagCompileUnit,
+				Children: true,
+				Field: []dwarf.Field{
+					{Attr: dwarf.AttrProducer, Val: "GNU C11 6.2.0 20160914 -mcpu=v9 -g -fstack-protector-strong", Class: dwarf.ClassString},
+					{Attr: dwarf.AttrLanguage, Val: int64(12), Class: dwarf.ClassConstant},
+					{Attr: dwarf.AttrName, Val: "hello.c", Class: dwarf.ClassString},
+					{Attr: dwarf.AttrCompDir, Val: "/tmp", Class: dwarf.ClassString},
+					{Attr: dwarf.AttrLowpc, Val: uint64(0x0), Class: dwarf.ClassAddress},
+					{Attr: dwarf.AttrHighpc, Val: int64(0x2c), Class: dwarf.ClassConstant},
+					{Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr},
+				},
+			}},
+		},
+	},
+	{
 		"testdata/go-relocation-test-gcc493-mips64le.obj",
 		[]relocationTestEntry{
 			{0, &dwarf.Entry{