From e93870c12bbd5843b93447646c631eb8d6d99a9a Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Tue, 24 Mar 2015 20:55:43 +0000 Subject: [PATCH] re PR go/65417 (debug/elf: lacks support for PPC relocations) PR go/65417 debug/elf: support reading debug info from 32-bit PPC objects This is a backport of http://golang.org/7590 from the master Go library. From-SVN: r221644 --- libgo/go/debug/elf/file.go | 43 ++++++++++++++++++ libgo/go/debug/elf/file_test.go | 6 +++ .../testdata/go-relocation-test-gcc5-ppc.obj | Bin 0 -> 2356 bytes 3 files changed, 49 insertions(+) create mode 100644 libgo/go/debug/elf/testdata/go-relocation-test-gcc5-ppc.obj diff --git a/libgo/go/debug/elf/file.go b/libgo/go/debug/elf/file.go index 5a418d81021..653b8d42400 100644 --- a/libgo/go/debug/elf/file.go +++ b/libgo/go/debug/elf/file.go @@ -532,6 +532,9 @@ func (f *File) applyRelocations(dst []byte, rels []byte) error { if f.Class == ELFCLASS64 && f.Machine == EM_AARCH64 { return f.applyRelocationsARM64(dst, rels) } + if f.Class == ELFCLASS32 && f.Machine == EM_PPC { + return f.applyRelocationsPPC(dst, rels) + } if f.Class == ELFCLASS64 && f.Machine == EM_PPC64 { return f.applyRelocationsPPC64(dst, rels) } @@ -677,6 +680,46 @@ func (f *File) applyRelocationsARM64(dst []byte, rels []byte) error { return nil } +func (f *File) applyRelocationsPPC(dst []byte, rels []byte) error { + // 12 is the size of Rela32. + if len(rels)%12 != 0 { + return errors.New("length of relocation section is not a multiple of 12") + } + + symbols, _, err := f.getSymbols(SHT_SYMTAB) + if err != nil { + return err + } + + b := bytes.NewReader(rels) + var rela Rela32 + + for b.Len() > 0 { + binary.Read(b, f.ByteOrder, &rela) + symNo := rela.Info >> 8 + t := R_PPC(rela.Info & 0xff) + + if symNo == 0 || symNo > uint32(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_PPC_ADDR32: + if rela.Off+4 >= uint32(len(dst)) || rela.Addend < 0 { + continue + } + f.ByteOrder.PutUint32(dst[rela.Off:rela.Off+4], uint32(rela.Addend)) + } + } + + return nil +} + func (f *File) applyRelocationsPPC64(dst []byte, rels []byte) error { // 24 is the size of Rela64. if len(rels)%24 != 0 { diff --git a/libgo/go/debug/elf/file_test.go b/libgo/go/debug/elf/file_test.go index ce10da71d2b..56e2604d9d3 100644 --- a/libgo/go/debug/elf/file_test.go +++ b/libgo/go/debug/elf/file_test.go @@ -260,6 +260,12 @@ var relocationTests = []relocationTest{ {0, &dwarf.Entry{Offset: 0xb, Tag: dwarf.TagCompileUnit, Children: true, Field: []dwarf.Field{{Attr: dwarf.AttrProducer, Val: "GNU C 4.2.4 (Ubuntu 4.2.4-1ubuntu4)"}, {Attr: dwarf.AttrLanguage, Val: int64(1)}, {Attr: dwarf.AttrName, Val: "go-relocation-test-gcc424.c"}, {Attr: dwarf.AttrCompDir, Val: "/tmp"}, {Attr: dwarf.AttrLowpc, Val: uint64(0x0)}, {Attr: dwarf.AttrHighpc, Val: uint64(0x6)}, {Attr: dwarf.AttrStmtList, Val: int64(0)}}}}, }, }, + { + "testdata/go-relocation-test-gcc5-ppc.obj", + []relocationTestEntry{ + {0, &dwarf.Entry{Offset: 0xb, Tag: dwarf.TagCompileUnit, Children: true, Field: []dwarf.Field{dwarf.Field{Attr: dwarf.AttrProducer, Val: "GNU C11 5.0.0 20150116 (experimental) -Asystem=linux -Asystem=unix -Asystem=posix -g"}, dwarf.Field{Attr: dwarf.AttrLanguage, Val: int64(12)}, dwarf.Field{Attr: dwarf.AttrName, Val: "go-relocation-test-gcc5-ppc.c"}, dwarf.Field{Attr: dwarf.AttrCompDir, Val: "/tmp"}, dwarf.Field{Attr: dwarf.AttrLowpc, Val: uint64(0x0)}, dwarf.Field{Attr: dwarf.AttrHighpc, Val: int64(0x44)}, dwarf.Field{Attr: dwarf.AttrStmtList, Val: int64(0)}}}}, + }, + }, { "testdata/go-relocation-test-gcc482-ppc64le.obj", []relocationTestEntry{ diff --git a/libgo/go/debug/elf/testdata/go-relocation-test-gcc5-ppc.obj b/libgo/go/debug/elf/testdata/go-relocation-test-gcc5-ppc.obj new file mode 100644 index 0000000000000000000000000000000000000000..f4165af0982d3b87761a4a540900e687b39e980f GIT binary patch literal 2356 zcma)6&2Jl35TCc+O&07lUmy}CqDvLPLG{)~1hqwNlg7{nPz6XF_^4p*Z0!Ym*V=t9 z4hp1FMFj^$Di8;R)B{zIT)6gcK>P>587^=@LP83^dArZvSg2*>c{B5ykN4*7JnudK z@(aQcG$cU}LC*o!@5=X#TFdk>&3*OQ!7sbEac5u9%9p>=^6t6fV1JM7{aaKz;}D$; zh%TbWyGS{;N6xh(nY(u3&h_68{`tszYjEirIc>kwiHT{-39iC_YmLnc~8w*PYE;%~^A+Zq<3ZT3f5u zYR@`lf6(*epzDXy>#RDJjbta0e)mi#2>XMP*$;!9)r%4~TU2UAmDuk@O-}|Ed(6Cl@(zec|i#ivk|Yq7>hnHx_t%qGd8z@b?~)y6DWhtXykC zh`;`vA_$%(9t*nnhg0d6s=Y+1To{w6Mi(|WPdjBy*6O{-ipWiNy3%Vv$~cv6tzmvV zSNa1Bn0p-%+c z8hKUl#F&kd7!l$h99b&9IA%|c+0$e8?3g_R`)G#M=hE}9=ooh1XF}9N==EiiwZylTAv;Nr4#xNF~Gw=TIAUp|0IWhjDgF)6wEGuBVU6s$i68MSC)-y7IohRMf zaezDr_lv6`hI7<=h2(nqdC=!Oo5evW`V{NMOlP6vPG$N!gXrUJEcjlk555E4$1BkF zS)tNTF!9ZXq|;hgoS|>l^LLq4AmZ>A4w0_MZ(Pp`s~q=3E>g#_)UyKB#7rs|-X|Sl06YVJ0Gz95 M@$9aGHL0Y30Z^?JZ2$lO literal 0 HcmV?d00001 -- 2.30.2