annotate libgo/misc/cgo/test/buildid_linux.go @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Copyright 2014 The Go Authors. All rights reserved.
kono
parents:
diff changeset
2 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
3 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 package cgotest
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 // Test that we have no more than one build ID. In the past we used
kono
parents:
diff changeset
8 // to generate a separate build ID for each package using cgo, and the
kono
parents:
diff changeset
9 // linker concatenated them all. We don't want that--we only want
kono
parents:
diff changeset
10 // one.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 import (
kono
parents:
diff changeset
13 "bytes"
kono
parents:
diff changeset
14 "debug/elf"
kono
parents:
diff changeset
15 "os"
kono
parents:
diff changeset
16 "testing"
kono
parents:
diff changeset
17 )
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 func testBuildID(t *testing.T) {
kono
parents:
diff changeset
20 f, err := elf.Open("/proc/self/exe")
kono
parents:
diff changeset
21 if err != nil {
kono
parents:
diff changeset
22 if os.IsNotExist(err) {
kono
parents:
diff changeset
23 t.Skip("no /proc/self/exe")
kono
parents:
diff changeset
24 }
kono
parents:
diff changeset
25 t.Fatal("opening /proc/self/exe: ", err)
kono
parents:
diff changeset
26 }
kono
parents:
diff changeset
27 defer f.Close()
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 c := 0
kono
parents:
diff changeset
30 for i, s := range f.Sections {
kono
parents:
diff changeset
31 if s.Type != elf.SHT_NOTE {
kono
parents:
diff changeset
32 continue
kono
parents:
diff changeset
33 }
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 d, err := s.Data()
kono
parents:
diff changeset
36 if err != nil {
kono
parents:
diff changeset
37 t.Logf("reading data of note section %d: %v", i, err)
kono
parents:
diff changeset
38 continue
kono
parents:
diff changeset
39 }
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 for len(d) > 0 {
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 // ELF standards differ as to the sizes in
kono
parents:
diff changeset
44 // note sections. Both the GNU linker and
kono
parents:
diff changeset
45 // gold always generate 32-bit sizes, so that
kono
parents:
diff changeset
46 // is what we assume here.
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 if len(d) < 12 {
kono
parents:
diff changeset
49 t.Logf("note section %d too short (%d < 12)", i, len(d))
kono
parents:
diff changeset
50 continue
kono
parents:
diff changeset
51 }
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 namesz := f.ByteOrder.Uint32(d)
kono
parents:
diff changeset
54 descsz := f.ByteOrder.Uint32(d[4:])
kono
parents:
diff changeset
55 typ := f.ByteOrder.Uint32(d[8:])
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 an := (namesz + 3) &^ 3
kono
parents:
diff changeset
58 ad := (descsz + 3) &^ 3
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 if int(12+an+ad) > len(d) {
kono
parents:
diff changeset
61 t.Logf("note section %d too short for header (%d < 12 + align(%d,4) + align(%d,4))", i, len(d), namesz, descsz)
kono
parents:
diff changeset
62 continue
kono
parents:
diff changeset
63 }
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 // 3 == NT_GNU_BUILD_ID
kono
parents:
diff changeset
66 if typ == 3 && namesz == 4 && bytes.Equal(d[12:16], []byte("GNU\000")) {
kono
parents:
diff changeset
67 c++
kono
parents:
diff changeset
68 }
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 d = d[12+an+ad:]
kono
parents:
diff changeset
71 }
kono
parents:
diff changeset
72 }
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 if c > 1 {
kono
parents:
diff changeset
75 t.Errorf("found %d build ID notes", c)
kono
parents:
diff changeset
76 }
kono
parents:
diff changeset
77 }