From 4e2aa9bff0eb76f98bb3b238c7f56bea6330f70d Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 4 Jan 2013 17:03:57 -0500 Subject: cmd/ld: use native-endian symbol values in symbol table The Plan 9 symbol table format defines big-endian symbol values for portability, but we want to be able to generate an ELF object file and let the host linker link it, as part of the solution to issue 4069. The symbol table itself, since it is loaded into memory at run time, must be filled in by the final host linker, using relocation directives to set the symbol values. On a little-endian machine, the linker will only fill in little-endian values during relocation, so we are forced to use little-endian symbol values. To preserve most of the original portability of the symbol table format, we make the table itself say whether it uses big- or little-endian values. If the table begins with the magic sequence fe ff ff ff 00 00 then the actual table begins after those six bytes and contains little-endian symbol values. Otherwise, the table is in the original format and contains big-endian symbol values. The magic sequence looks like an "end of table" entry (the fifth byte is zero), so legacy readers will see a little-endian table as an empty table. All the gc architectures are little-endian today, so the practical effect of this CL is to make all the generated tables little-endian, but if a big-endian system comes along, ld will not generate the magic sequence, and the various readers will fall back to the original big-endian interpretation. R=ken2 CC=golang-dev https://golang.org/cl/7066043 --- src/pkg/debug/gosym/pclntab_test.go | 2 +- src/pkg/debug/gosym/symtab.go | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src/pkg/debug') diff --git a/src/pkg/debug/gosym/pclntab_test.go b/src/pkg/debug/gosym/pclntab_test.go index ade704335d..5f2242eba0 100644 --- a/src/pkg/debug/gosym/pclntab_test.go +++ b/src/pkg/debug/gosym/pclntab_test.go @@ -129,7 +129,7 @@ func TestLineFromAline(t *testing.T) { if !ok { t.Errorf("file %s starts on line %d", path, line) } else if line != ll+1 { - t.Errorf("expected next line of file %s to be %d, got %d", path, ll+1, line) + t.Fatalf("expected next line of file %s to be %d, got %d", path, ll+1, line) } lastline[path] = line } diff --git a/src/pkg/debug/gosym/symtab.go b/src/pkg/debug/gosym/symtab.go index 52d7d55a33..cc01e0b9d6 100644 --- a/src/pkg/debug/gosym/symtab.go +++ b/src/pkg/debug/gosym/symtab.go @@ -13,6 +13,7 @@ package gosym // and the Go format is the runtime source, specifically ../../runtime/symtab.c. import ( + "bytes" "encoding/binary" "fmt" "strconv" @@ -104,11 +105,18 @@ type sym struct { name []byte } +var littleEndianSymtab = []byte{0xFE, 0xFF, 0xFF, 0xFF, 0x00, 0x00} + func walksymtab(data []byte, fn func(sym) error) error { + var order binary.ByteOrder = binary.BigEndian + if bytes.HasPrefix(data, littleEndianSymtab) { + data = data[6:] + order = binary.LittleEndian + } var s sym p := data for len(p) >= 6 { - s.value = binary.BigEndian.Uint32(p[0:4]) + s.value = order.Uint32(p[0:4]) typ := p[4] if typ&0x80 == 0 { return &DecodingError{len(data) - len(p) + 4, "bad symbol type", typ} @@ -139,7 +147,7 @@ func walksymtab(data []byte, fn func(sym) error) error { } s.name = p[0:i] i += nnul - s.gotype = binary.BigEndian.Uint32(p[i : i+4]) + s.gotype = order.Uint32(p[i : i+4]) p = p[i+4:] fn(s) } -- cgit v1.3