compiler: rationalize external symbol names
[official-gcc.git] / libgo / go / runtime / pprof / elf.go
bloba8b5ea68175f92f32d6f47dc61c46a35ec22e2d3
1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package pprof
7 import (
8 "encoding/binary"
9 "errors"
10 "fmt"
11 "os"
14 var (
15 errBadELF = errors.New("malformed ELF binary")
16 errNoBuildID = errors.New("no NT_GNU_BUILD_ID found in ELF binary")
19 // elfBuildID returns the GNU build ID of the named ELF binary,
20 // without introducing a dependency on debug/elf and its dependencies.
21 func elfBuildID(file string) (string, error) {
22 buf := make([]byte, 256)
23 f, err := os.Open(file)
24 if err != nil {
25 return "", err
27 defer f.Close()
29 if _, err := f.ReadAt(buf[:64], 0); err != nil {
30 return "", err
33 // ELF file begins with \x7F E L F.
34 if buf[0] != 0x7F || buf[1] != 'E' || buf[2] != 'L' || buf[3] != 'F' {
35 return "", errBadELF
38 var byteOrder binary.ByteOrder
39 switch buf[5] {
40 default:
41 return "", errBadELF
42 case 1: // little-endian
43 byteOrder = binary.LittleEndian
44 case 2: // big-endian
45 byteOrder = binary.BigEndian
48 var shnum int
49 var shoff, shentsize int64
50 switch buf[4] {
51 default:
52 return "", errBadELF
53 case 1: // 32-bit file header
54 shoff = int64(byteOrder.Uint32(buf[32:]))
55 shentsize = int64(byteOrder.Uint16(buf[46:]))
56 if shentsize != 40 {
57 return "", errBadELF
59 shnum = int(byteOrder.Uint16(buf[48:]))
60 case 2: // 64-bit file header
61 shoff = int64(byteOrder.Uint64(buf[40:]))
62 shentsize = int64(byteOrder.Uint16(buf[58:]))
63 if shentsize != 64 {
64 return "", errBadELF
66 shnum = int(byteOrder.Uint16(buf[60:]))
69 for i := 0; i < shnum; i++ {
70 if _, err := f.ReadAt(buf[:shentsize], shoff+int64(i)*shentsize); err != nil {
71 return "", err
73 if typ := byteOrder.Uint32(buf[4:]); typ != 7 { // SHT_NOTE
74 continue
76 var off, size int64
77 if shentsize == 40 {
78 // 32-bit section header
79 off = int64(byteOrder.Uint32(buf[16:]))
80 size = int64(byteOrder.Uint32(buf[20:]))
81 } else {
82 // 64-bit section header
83 off = int64(byteOrder.Uint64(buf[24:]))
84 size = int64(byteOrder.Uint64(buf[32:]))
86 size += off
87 for off < size {
88 if _, err := f.ReadAt(buf[:16], off); err != nil { // room for header + name GNU\x00
89 return "", err
91 nameSize := int(byteOrder.Uint32(buf[0:]))
92 descSize := int(byteOrder.Uint32(buf[4:]))
93 noteType := int(byteOrder.Uint32(buf[8:]))
94 descOff := off + int64(12+(nameSize+3)&^3)
95 off = descOff + int64((descSize+3)&^3)
96 if nameSize != 4 || noteType != 3 || buf[12] != 'G' || buf[13] != 'N' || buf[14] != 'U' || buf[15] != '\x00' { // want name GNU\x00 type 3 (NT_GNU_BUILD_ID)
97 continue
99 if descSize > len(buf) {
100 return "", errBadELF
102 if _, err := f.ReadAt(buf[:descSize], descOff); err != nil {
103 return "", err
105 return fmt.Sprintf("%x", buf[:descSize]), nil
108 return "", errNoBuildID