Skip to content

Commit 48a1dcb

Browse files
ianlancetaylorgopherbot
authored andcommitted
debug/elf: support zstd compression
Test cases added to debug/dwarf because that is where it matters in practice. The new test binary line-gcc-zstd.elf built with gcc -g -no-pie -Wl,--compress-debug-sections=zstd line[12].c using gcc (Debian 12.2.0-10) 12.2.0 with a development version of the GNU binutils. Fixes #55107 Change-Id: I48507c96902e1f83a174e5647b5cc403d965b52b Reviewed-on: https://go-review.googlesource.com/c/go/+/473256 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Than McIntosh <thanm@google.com>
1 parent 73ee0fc commit 48a1dcb

File tree

9 files changed

+71
-4
lines changed

9 files changed

+71
-4
lines changed

api/next/55107.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pkg debug/elf, const COMPRESS_ZSTD = 2 #55107
2+
pkg debug/elf, const COMPRESS_ZSTD CompressionType #55107

src/cmd/dist/buildtool.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ var bootstrapDirs = []string{
7878
"internal/types/errors",
7979
"internal/unsafeheader",
8080
"internal/xcoff",
81+
"internal/zstd",
8182
"math/big",
8283
"math/bits",
8384
"sort",

src/debug/dwarf/entry_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ func TestReaderSeek(t *testing.T) {
6969
{0x40117e, nil},
7070
}
7171
testRanges(t, "testdata/line-clang-dwarf5.elf", want)
72+
73+
want = []wantRange{
74+
{0x401126, [][2]uint64{{0x401126, 0x40116a}}},
75+
{0x40116a, [][2]uint64{{0x40116a, 0x401180}}},
76+
}
77+
testRanges(t, "testdata/line-gcc-zstd.elf", want)
7278
}
7379

7480
func TestRangesSection(t *testing.T) {
@@ -156,6 +162,15 @@ func TestReaderRanges(t *testing.T) {
156162
{"f2", [][2]uint64{{0x401180, 0x401197}}},
157163
},
158164
},
165+
{
166+
"testdata/line-gcc-zstd.elf",
167+
subprograms{
168+
{"f2", nil},
169+
{"main", [][2]uint64{{0x40114b, 0x40116a}}},
170+
{"f1", [][2]uint64{{0x401126, 0x40114b}}},
171+
{"f2", [][2]uint64{{0x40116a, 0x401180}}},
172+
},
173+
},
159174
}
160175

161176
for _, test := range tests {

src/debug/dwarf/line_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,44 @@ func TestLineELFGCC(t *testing.T) {
4848
testLineTable(t, want, files, elfData(t, "testdata/line-gcc.elf"))
4949
}
5050

51+
func TestLineELFGCCZstd(t *testing.T) {
52+
// Generated by:
53+
// # gcc --version | head -n1
54+
// gcc (Debian 12.2.0-10) 12.2.0
55+
// # gcc -g -no-pie -Wl,--compress-debug-sections=zstd line*.c
56+
57+
zfile1H := &LineFile{Name: "/home/iant/go/src/debug/dwarf/testdata/line1.h"}
58+
zfile1C := &LineFile{Name: "/home/iant/go/src/debug/dwarf/testdata/line1.c"}
59+
zfile2C := &LineFile{Name: "/home/iant/go/src/debug/dwarf/testdata/line2.c"}
60+
61+
// Line table based on readelf --debug-dump=rawline,decodedline
62+
want := []LineEntry{
63+
{Address: 0x401126, File: zfile1H, Line: 2, Column: 1, IsStmt: true},
64+
{Address: 0x40112a, File: zfile1H, Line: 5, Column: 8, IsStmt: true},
65+
{Address: 0x401131, File: zfile1H, Line: 5, Column: 2, IsStmt: true},
66+
{Address: 0x401133, File: zfile1H, Line: 6, Column: 10, IsStmt: true, Discriminator: 3},
67+
{Address: 0x40113d, File: zfile1H, Line: 5, Column: 22, IsStmt: true, Discriminator: 3},
68+
{Address: 0x401141, File: zfile1H, Line: 5, Column: 15, IsStmt: true, Discriminator: 1},
69+
{Address: 0x401147, File: zfile1H, Line: 7, Column: 1, IsStmt: true},
70+
{Address: 0x40114b, File: zfile1C, Line: 6, Column: 1, IsStmt: true},
71+
{Address: 0x40114f, File: zfile1C, Line: 7, Column: 2, IsStmt: true},
72+
{Address: 0x401159, File: zfile1C, Line: 8, Column: 2, IsStmt: true},
73+
{Address: 0x401168, File: zfile1C, Line: 9, Column: 1, IsStmt: true},
74+
{Address: 0x40116a, EndSequence: true},
75+
76+
{Address: 0x40116a, File: zfile2C, Line: 4, Column: 1, IsStmt: true},
77+
{Address: 0x40116e, File: zfile2C, Line: 5, Column: 2, IsStmt: true},
78+
{Address: 0x40117d, File: zfile2C, Line: 6, Column: 1, IsStmt: true},
79+
{Address: 0x401180, EndSequence: true},
80+
}
81+
files := [][]*LineFile{
82+
{zfile1C, zfile1H, zfile1C},
83+
{zfile2C, zfile2C},
84+
}
85+
86+
testLineTable(t, want, files, elfData(t, "testdata/line-gcc-zstd.elf"))
87+
}
88+
5189
func TestLineGCCWindows(t *testing.T) {
5290
// Generated by:
5391
// > gcc --version
@@ -277,7 +315,7 @@ func testLineTable(t *testing.T, want []LineEntry, files [][]*LineFile, d *Data)
277315
}
278316

279317
// Compare line tables.
280-
if !compareLines(got, want) {
318+
if !compareLines(t, got, want) {
281319
t.Log("Line tables do not match. Got:")
282320
dumpLines(t, got)
283321
t.Log("Want:")
@@ -312,8 +350,10 @@ func dumpFiles(t *testing.T, files []*LineFile) {
312350
}
313351
}
314352

315-
func compareLines(a, b []LineEntry) bool {
353+
func compareLines(t *testing.T, a, b []LineEntry) bool {
354+
t.Helper()
316355
if len(a) != len(b) {
356+
t.Errorf("len(a) == %d, len(b) == %d", len(a), len(b))
317357
return false
318358
}
319359

@@ -326,11 +366,13 @@ func compareLines(a, b []LineEntry) bool {
326366
continue
327367
}
328368
if al.File.Name != bl.File.Name {
369+
t.Errorf("%d: name %v != name %v", i, al.File.Name, bl.File.Name)
329370
return false
330371
}
331372
al.File = nil
332373
bl.File = nil
333374
if al != bl {
375+
t.Errorf("%d: %#v != %#v", i, al, bl)
334376
return false
335377
}
336378
}
16.8 KB
Binary file not shown.

src/debug/elf/elf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ type CompressionType int
728728

729729
const (
730730
COMPRESS_ZLIB CompressionType = 1 /* ZLIB compression. */
731+
COMPRESS_ZSTD CompressionType = 2 /* ZSTD compression. */
731732
COMPRESS_LOOS CompressionType = 0x60000000 /* First OS-specific. */
732733
COMPRESS_HIOS CompressionType = 0x6fffffff /* Last OS-specific. */
733734
COMPRESS_LOPROC CompressionType = 0x70000000 /* First processor-specific type. */
@@ -736,6 +737,7 @@ const (
736737

737738
var compressionStrings = []intName{
738739
{1, "COMPRESS_ZLIB"},
740+
{2, "COMPRESS_ZSTD"},
739741
{0x60000000, "COMPRESS_LOOS"},
740742
{0x6fffffff, "COMPRESS_HIOS"},
741743
{0x70000000, "COMPRESS_LOPROC"},

src/debug/elf/elf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var nameTests = []nameTest{
3838
{R_SPARC_GOT22, "R_SPARC_GOT22"},
3939
{ET_LOOS + 5, "ET_LOOS+5"},
4040
{ProgFlag(0x50), "0x50"},
41-
{COMPRESS_ZLIB + 1, "COMPRESS_ZLIB+1"},
41+
{COMPRESS_ZLIB + 2, "COMPRESS_ZSTD+1"},
4242
}
4343

4444
func TestNames(t *testing.T) {

src/debug/elf/file.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"errors"
2424
"fmt"
2525
"internal/saferio"
26+
"internal/zstd"
2627
"io"
2728
"os"
2829
"strings"
@@ -164,6 +165,10 @@ func (s *Section) Open() io.ReadSeeker {
164165
switch s.compressionType {
165166
case COMPRESS_ZLIB:
166167
zrd = zlib.NewReader
168+
case COMPRESS_ZSTD:
169+
zrd = func(r io.Reader) (io.ReadCloser, error) {
170+
return io.NopCloser(zstd.NewReader(r)), nil
171+
}
167172
}
168173

169174
if zrd == nil {

src/go/build/deps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ var depsRules = `
251251
< index/suffixarray;
252252
253253
# executable parsing
254-
FMT, encoding/binary, compress/zlib, internal/saferio
254+
FMT, encoding/binary, compress/zlib, internal/saferio, internal/zstd
255255
< runtime/debug
256256
< debug/dwarf
257257
< debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff

0 commit comments

Comments
 (0)