diff --git a/src/debug/elf/file.go b/src/debug/elf/file.go index 1e863ef78e5dd4..3e89273b66dfbb 100644 --- a/src/debug/elf/file.go +++ b/src/debug/elf/file.go @@ -630,17 +630,14 @@ func (f *File) applyRelocations(dst []byte, rels []byte) error { // relocSymbolTargetOK decides whether we should try to apply a // relocation to a DWARF data section, given a pointer to the symbol -// targeted by the relocation. Most relocations in DWARF data tend to -// be section-relative, but some target non-section symbols (for -// example, low_PC attrs on subprogram or compilation unit DIEs that -// target function symbols), and we need to include these as well. +// targeted by the relocation. +// Note: most relocations in DWARF data tend to be section-relative, +// but some target non-section symbols (for example, low_PC attrs on +// subprogram or compilation unit DIEs that target function symbols). // Return value is a pair (X,Y) where X is a boolean indicating // whether the relocation is needed, and Y is the symbol value in the // case of a non-section relocation that needs to be applied. func relocSymbolTargetOK(sym *Symbol) (bool, uint64) { - if ST_TYPE(sym.Info) == STT_SECTION { - return true, 0 - } if sym.Section != SHN_UNDEF && sym.Section < SHN_LORESERVE { return true, sym.Value } diff --git a/src/debug/elf/file_test.go b/src/debug/elf/file_test.go index b13d13ebf09d5e..4b0bfc1d4a7a54 100644 --- a/src/debug/elf/file_test.go +++ b/src/debug/elf/file_test.go @@ -293,6 +293,7 @@ func decompress(gz string) (io.ReaderAt, error) { type relocationTestEntry struct { entryNumber int entry *dwarf.Entry + pcRanges [][2]uint64 } type relocationTest struct { @@ -317,7 +318,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: uint64(0x6), Class: dwarf.ClassAddress}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -336,7 +337,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: uint64(0x5), Class: dwarf.ClassAddress}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -355,7 +356,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: uint64(0x6), Class: dwarf.ClassAddress}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -374,7 +375,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: int64(0x24), Class: dwarf.ClassConstant}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -393,7 +394,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: int64(0x28), Class: dwarf.ClassConstant}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -412,7 +413,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrLowpc, Val: uint64(0x0), Class: dwarf.ClassAddress}, {Attr: dwarf.AttrHighpc, Val: int64(48), Class: dwarf.ClassConstant}, }, - }}, + }, nil}, }, }, { @@ -431,7 +432,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: int64(0x44), Class: dwarf.ClassConstant}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -450,7 +451,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: uint64(0x24), Class: dwarf.ClassAddress}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -469,7 +470,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: int64(100), Class: dwarf.ClassConstant}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -488,7 +489,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: int64(58), Class: dwarf.ClassConstant}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -507,7 +508,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: int64(0x2c), Class: dwarf.ClassConstant}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -526,7 +527,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: int64(0x58), Class: dwarf.ClassConstant}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -545,7 +546,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: uint64(0x5c), Class: dwarf.ClassAddress}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -564,7 +565,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: int64(100), Class: dwarf.ClassConstant}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -583,7 +584,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrHighpc, Val: uint64(0x2c), Class: dwarf.ClassAddress}, {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, }, - }}, + }, nil}, }, }, { @@ -600,7 +601,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, {Attr: dwarf.AttrCompDir, Val: "/tmp", Class: dwarf.ClassString}, }, - }}, + }, nil}, }, }, { @@ -617,7 +618,7 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrType, Val: dwarf.Offset(0xb7f), Class: dwarf.ClassReference}, {Attr: dwarf.AttrDataMemberLoc, Val: []byte{0x23, 0x0}, Class: dwarf.ClassExprLoc}, }, - }}, + }, nil}, {204, &dwarf.Entry{ Offset: 0xc70, Tag: dwarf.TagMember, @@ -629,6 +630,50 @@ var relocationTests = []relocationTest{ {Attr: dwarf.AttrType, Val: dwarf.Offset(0xb7f), Class: dwarf.ClassReference}, {Attr: dwarf.AttrDataMemberLoc, Val: []byte{0x23, 0x10}, Class: dwarf.ClassExprLoc}, }, + }, nil}, + }, + }, + { + "testdata/go-relocation-test-gcc930-ranges-no-rela-x86-64", + []relocationTestEntry{ + {0, &dwarf.Entry{ + Offset: 0xb, + Tag: dwarf.TagCompileUnit, + Children: true, + Field: []dwarf.Field{ + {Attr: dwarf.AttrProducer, Val: "GNU C17 9.3.0 -mtune=generic -march=x86-64 -g -fasynchronous-unwind-tables", Class: dwarf.ClassString}, + {Attr: dwarf.AttrLanguage, Val: int64(12), Class: dwarf.ClassConstant}, + {Attr: dwarf.AttrName, Val: "multiple-code-sections.c", Class: dwarf.ClassString}, + {Attr: dwarf.AttrCompDir, Val: "/tmp", Class: dwarf.ClassString}, + {Attr: dwarf.AttrRanges, Val: int64(0), Class: dwarf.ClassRangeListPtr}, + {Attr: dwarf.AttrLowpc, Val: uint64(0), Class: dwarf.ClassAddress}, + {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, + }, + }, [][2]uint64{ + {0x1125, 0x1137}, + {0xc0ffee, 0xc0fff9}, + }}, + }, + }, + { + "testdata/go-relocation-test-gcc930-ranges-with-rela-x86-64", + []relocationTestEntry{ + {0, &dwarf.Entry{ + Offset: 0xb, + Tag: dwarf.TagCompileUnit, + Children: true, + Field: []dwarf.Field{ + {Attr: dwarf.AttrProducer, Val: "GNU C17 9.3.0 -mtune=generic -march=x86-64 -g -fasynchronous-unwind-tables", Class: dwarf.ClassString}, + {Attr: dwarf.AttrLanguage, Val: int64(12), Class: dwarf.ClassConstant}, + {Attr: dwarf.AttrName, Val: "multiple-code-sections.c", Class: dwarf.ClassString}, + {Attr: dwarf.AttrCompDir, Val: "/tmp", Class: dwarf.ClassString}, + {Attr: dwarf.AttrRanges, Val: int64(0), Class: dwarf.ClassRangeListPtr}, + {Attr: dwarf.AttrLowpc, Val: uint64(0), Class: dwarf.ClassAddress}, + {Attr: dwarf.AttrStmtList, Val: int64(0), Class: dwarf.ClassLinePtr}, + }, + }, [][2]uint64{ + {0x1125, 0x1137}, + {0xc0ffee, 0xc0fff9}, }}, }, }, @@ -664,6 +709,17 @@ func TestDWARFRelocations(t *testing.T) { t.Errorf("#%d/%d: mismatch: got:%#v want:%#v", i, testEntry.entryNumber, entry, testEntry.entry) continue } + if testEntry.pcRanges != nil { + pcRanges, err := dwarf.Ranges(entry) + if err != nil { + t.Error(err) + continue + } + if !reflect.DeepEqual(testEntry.pcRanges, pcRanges) { + t.Errorf("#%d/%d: PC range mismatch: got:%#v want:%#v", i, testEntry.entryNumber, pcRanges, testEntry.pcRanges) + continue + } + } } } } diff --git a/src/debug/elf/testdata/go-relocation-test-gcc930-ranges-no-rela-x86-64 b/src/debug/elf/testdata/go-relocation-test-gcc930-ranges-no-rela-x86-64 new file mode 100644 index 00000000000000..d8f03804e0585e Binary files /dev/null and b/src/debug/elf/testdata/go-relocation-test-gcc930-ranges-no-rela-x86-64 differ diff --git a/src/debug/elf/testdata/go-relocation-test-gcc930-ranges-with-rela-x86-64 b/src/debug/elf/testdata/go-relocation-test-gcc930-ranges-with-rela-x86-64 new file mode 100644 index 00000000000000..3b0f4d3482fd13 Binary files /dev/null and b/src/debug/elf/testdata/go-relocation-test-gcc930-ranges-with-rela-x86-64 differ diff --git a/src/debug/elf/testdata/multiple-code-sections.c b/src/debug/elf/testdata/multiple-code-sections.c new file mode 100644 index 00000000000000..97339f9138991c --- /dev/null +++ b/src/debug/elf/testdata/multiple-code-sections.c @@ -0,0 +1,11 @@ +// Build with: +// gcc -g multiple-code-sections.c multiple-code-sections.ld -Wl,--emit-relocs -Wl,--discard-none -o go-relocation-test-gcc930-ranges-with-rela-x86-64 +// gcc -g multiple-code-sections.c multiple-code-sections.ld -o go-relocation-test-gcc930-ranges-no-rela-x86-64 +__attribute__((section(".coffee_section"))) +int coffee(void) { + return 0; +} + +int main(int argc, char *argv[]) { + return 0; +} diff --git a/src/debug/elf/testdata/multiple-code-sections.ld b/src/debug/elf/testdata/multiple-code-sections.ld new file mode 100644 index 00000000000000..670456e3331989 --- /dev/null +++ b/src/debug/elf/testdata/multiple-code-sections.ld @@ -0,0 +1,5 @@ +SECTIONS +{ + . = 0xC0FFEE; + .coffee_section : {} +}