aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/debug
diff options
context:
space:
mode:
authorAdam Langley <agl@golang.org>2009-11-02 12:02:16 -0800
committerAdam Langley <agl@golang.org>2009-11-02 12:02:16 -0800
commit72ec930fa70c20ce69b21bf32a7916c04c2e9c2f (patch)
tree60fe8c0d3ae25d25dfdf05ba1073c880f3df739a /src/pkg/debug
parent6358cac7d85544d77ca27597bc4a46c61d0d964e (diff)
downloadgo-72ec930fa70c20ce69b21bf32a7916c04c2e9c2f.tar.xz
Fix cgo for GCC 4.4
Firstly, with -Werror, GCC switched to printing warnings starting with "error:". Widening the string matches solves this as the messages are otherwise unchanged. Secondly, GCC 4.4 outputs DWARF sections with with NUL bytes in all the offsets and requires the relocation section for .debug_info to be processed in order to result in valid DWARF data. Thus we add minimal handling for relocation sections, which is sufficient for our needs. BUG=1 Fixes #1. R=rsc, iant CC=go-dev http://go/go-review/1017003
Diffstat (limited to 'src/pkg/debug')
-rw-r--r--src/pkg/debug/elf/file.go130
-rw-r--r--src/pkg/debug/elf/file_test.go51
-rw-r--r--src/pkg/debug/elf/testdata/go-relocation-test-gcc424-x86-64.obin0 -> 3088 bytes
-rw-r--r--src/pkg/debug/elf/testdata/go-relocation-test-gcc441-x86-64.obin0 -> 2936 bytes
-rw-r--r--src/pkg/debug/elf/testdata/go-relocation-test-gcc441-x86.obin0 -> 1884 bytes
5 files changed, 181 insertions, 0 deletions
diff --git a/src/pkg/debug/elf/file.go b/src/pkg/debug/elf/file.go
index 7b1d784548..0945eb506f 100644
--- a/src/pkg/debug/elf/file.go
+++ b/src/pkg/debug/elf/file.go
@@ -6,6 +6,7 @@
package elf
import (
+ "bytes";
"debug/dwarf";
"encoding/binary";
"fmt";
@@ -109,6 +110,13 @@ func (p *Prog) Open() io.ReadSeeker {
return io.NewSectionReader(p.sr, 0, 1<<63 - 1);
}
+// A Symbol represents an entry in an ELF symbol table section.
+type Symbol struct {
+ Name uint32;
+ Info, Other byte;
+ Section uint32;
+ Value, Size uint64;
+}
/*
* ELF reader
@@ -305,6 +313,60 @@ func NewFile(r io.ReaderAt) (*File, os.Error) {
return f, nil;
}
+func (f *File) getSymbols() ([]Symbol, os.Error) {
+ switch f.Class {
+ case ELFCLASS64:
+ return f.getSymbols64();
+ }
+
+ return nil, os.ErrorString("not implemented");
+}
+
+// GetSymbols returns a slice of Symbols from parsing the symbol table.
+func (f *File) getSymbols64() ([]Symbol, os.Error) {
+ var symtabSection *Section;
+ for _, section := range f.Sections {
+ if section.Type == SHT_SYMTAB {
+ symtabSection = section;
+ break;
+ }
+ }
+
+ if symtabSection == nil {
+ return nil, os.ErrorString("no symbol section");
+ }
+
+ data, err := symtabSection.Data();
+ if err != nil {
+ return nil, os.ErrorString("cannot load symbol section");
+ }
+ symtab := bytes.NewBuffer(data);
+ if symtab.Len() % Sym64Size != 0 {
+ return nil, os.ErrorString("length of symbol section is not a multiple of Sym64Size");
+ }
+
+ // The first entry is all zeros.
+ var skip [Sym64Size]byte;
+ symtab.Read(skip[0:len(skip)]);
+
+ symbols := make([]Symbol, symtab.Len() / Sym64Size);
+
+ i := 0;
+ var sym Sym64;
+ for symtab.Len() > 0 {
+ binary.Read(symtab, f.ByteOrder, &sym);
+ symbols[i].Name = sym.Name;
+ symbols[i].Info = sym.Info;
+ symbols[i].Other = sym.Other;
+ symbols[i].Section = uint32(sym.Shndx);
+ symbols[i].Value = sym.Value;
+ symbols[i].Size = sym.Size;
+ i++;
+ }
+
+ return symbols, nil;
+}
+
// getString extracts a string from an ELF string table.
func getString(section []byte, start int) (string, bool) {
if start < 0 || start >= len(section) {
@@ -330,6 +392,60 @@ func (f *File) Section(name string) *Section {
return nil;
}
+// applyRelocations applies relocations to dst. rels is a relocations section
+// in RELA format.
+func (f *File) applyRelocations(dst []byte, rels []byte) os.Error {
+ if f.Class == ELFCLASS64 && f.Machine == EM_X86_64 {
+ return f.applyRelocationsAMD64(dst, rels);
+ }
+
+ return os.ErrorString("not implemented");
+}
+
+func (f *File) applyRelocationsAMD64(dst []byte, rels []byte) os.Error {
+ if len(rels) % Sym64Size != 0 {
+ return os.ErrorString("length of relocation section is not a multiple of Sym64Size");
+ }
+
+ symbols, err := f.getSymbols();
+ if err != nil {
+ return err;
+ }
+
+ b := bytes.NewBuffer(rels);
+ var rela Rela64;
+
+ for b.Len() > 0 {
+ binary.Read(b, f.ByteOrder, &rela);
+ symNo := rela.Info >> 32;
+ t := R_X86_64(rela.Info & 0xffff);
+
+ if symNo >= uint64(len(symbols)) {
+ continue;
+ }
+ sym := &symbols[symNo];
+ if SymType(sym.Info & 0xf) != STT_SECTION {
+ // We don't handle non-section relocations for now.
+ continue;
+ }
+
+ switch t {
+ case R_X86_64_64:
+ 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_X86_64_32:
+ 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;
+}
+
func (f *File) DWARF() (*dwarf.Data, os.Error) {
// There are many other DWARF sections, but these
// are the required ones, and the debug/dwarf package
@@ -349,6 +465,20 @@ func (f *File) DWARF() (*dwarf.Data, os.Error) {
dat[i] = b;
}
+ // If there's a relocation table for .debug_info, we have to process it
+ // now otherwise the data in .debug_info is invalid for x86-64 objects.
+ rela := f.Section(".rela.debug_info");
+ if rela != nil && rela.Type == SHT_RELA && f.Machine == EM_X86_64 {
+ data, err := rela.Data();
+ if err != nil {
+ return nil, err;
+ }
+ err = f.applyRelocations(dat[1], data);
+ if err != nil {
+ return nil, err;
+ }
+ }
+
abbrev, info, str := dat[0], dat[1], dat[2];
return dwarf.New(abbrev, nil, nil, info, nil, nil, nil, str);
}
diff --git a/src/pkg/debug/elf/file_test.go b/src/pkg/debug/elf/file_test.go
index 9b756aea12..04c924d124 100644
--- a/src/pkg/debug/elf/file_test.go
+++ b/src/pkg/debug/elf/file_test.go
@@ -5,6 +5,7 @@
package elf
import (
+ "debug/dwarf";
"encoding/binary";
"reflect";
"testing";
@@ -127,3 +128,53 @@ func TestOpen(t *testing.T) {
}
}
}
+
+type relocationTest struct {
+ file string;
+ firstEntry *dwarf.Entry;
+}
+
+var relocationTests = []relocationTest{
+ relocationTest{
+ "testdata/go-relocation-test-gcc441-x86-64.o",
+ &dwarf.Entry{Offset: 0xb, Tag: dwarf.TagCompileUnit, Children: true, Field: []dwarf.Field{dwarf.Field{Attr: dwarf.AttrProducer, Val: "GNU C 4.4.1"}, dwarf.Field{Attr: dwarf.AttrLanguage, Val: int64(1)}, dwarf.Field{Attr: dwarf.AttrName, Val: "go-relocation-test.c"}, dwarf.Field{Attr: dwarf.AttrCompDir, Val: "/tmp"}, dwarf.Field{Attr: dwarf.AttrLowpc, Val: uint64(0x0)}, dwarf.Field{Attr: dwarf.AttrHighpc, Val: uint64(0x6)}, dwarf.Field{Attr: dwarf.AttrStmtList, Val: int64(0)}}},
+ },
+ relocationTest{
+ "testdata/go-relocation-test-gcc441-x86.o",
+ &dwarf.Entry{Offset: 0xb, Tag: dwarf.TagCompileUnit, Children: true, Field: []dwarf.Field{dwarf.Field{Attr: dwarf.AttrProducer, Val: "GNU C 4.4.1"}, dwarf.Field{Attr: dwarf.AttrLanguage, Val: int64(1)}, dwarf.Field{Attr: dwarf.AttrName, Val: "t.c"}, dwarf.Field{Attr: dwarf.AttrCompDir, Val: "/tmp"}, dwarf.Field{Attr: dwarf.AttrLowpc, Val: uint64(0x0)}, dwarf.Field{Attr: dwarf.AttrHighpc, Val: uint64(0x5)}, dwarf.Field{Attr: dwarf.AttrStmtList, Val: int64(0)}}},
+ },
+ relocationTest{
+ "testdata/go-relocation-test-gcc424-x86-64.o",
+ &dwarf.Entry{Offset: 0xb, Tag: dwarf.TagCompileUnit, Children: true, Field: []dwarf.Field{dwarf.Field{Attr: dwarf.AttrProducer, Val: "GNU C 4.2.4 (Ubuntu 4.2.4-1ubuntu4)"}, dwarf.Field{Attr: dwarf.AttrLanguage, Val: int64(1)}, dwarf.Field{Attr: dwarf.AttrName, Val: "go-relocation-test-gcc424.c"}, dwarf.Field{Attr: dwarf.AttrCompDir, Val: "/tmp"}, dwarf.Field{Attr: dwarf.AttrLowpc, Val: uint64(0x0)}, dwarf.Field{Attr: dwarf.AttrHighpc, Val: uint64(0x6)}, dwarf.Field{Attr: dwarf.AttrStmtList, Val: int64(0)}}},
+ },
+}
+
+func TestDWARFRelocations(t *testing.T) {
+ for i, test := range relocationTests {
+ f, err := Open(test.file);
+ if err != nil {
+ t.Error(err);
+ continue;
+ }
+ dwarf, err := f.DWARF();
+ if err != nil {
+ t.Error(err);
+ continue;
+ }
+ reader := dwarf.Reader();
+ // Checking only the first entry is sufficient since it has
+ // many different strings. If the relocation had failed, all
+ // the string offsets would be zero and all the strings would
+ // end up being the same.
+ firstEntry, err := reader.Next();
+ if err != nil {
+ t.Error(err);
+ continue;
+ }
+
+ if !reflect.DeepEqual(test.firstEntry, firstEntry) {
+ t.Errorf("#%d: mismatch: got:%#v want:%#v", i, firstEntry, test.firstEntry);
+ continue;
+ }
+ }
+}
diff --git a/src/pkg/debug/elf/testdata/go-relocation-test-gcc424-x86-64.o b/src/pkg/debug/elf/testdata/go-relocation-test-gcc424-x86-64.o
new file mode 100644
index 0000000000..a7c6d6e562
--- /dev/null
+++ b/src/pkg/debug/elf/testdata/go-relocation-test-gcc424-x86-64.o
Binary files differ
diff --git a/src/pkg/debug/elf/testdata/go-relocation-test-gcc441-x86-64.o b/src/pkg/debug/elf/testdata/go-relocation-test-gcc441-x86-64.o
new file mode 100644
index 0000000000..2d37ab6e6e
--- /dev/null
+++ b/src/pkg/debug/elf/testdata/go-relocation-test-gcc441-x86-64.o
Binary files differ
diff --git a/src/pkg/debug/elf/testdata/go-relocation-test-gcc441-x86.o b/src/pkg/debug/elf/testdata/go-relocation-test-gcc441-x86.o
new file mode 100644
index 0000000000..0d59fe303b
--- /dev/null
+++ b/src/pkg/debug/elf/testdata/go-relocation-test-gcc441-x86.o
Binary files differ