package pprof
import (
)
var (
errBadELF = errors.New("malformed ELF binary")
errNoBuildID = errors.New("no NT_GNU_BUILD_ID found in ELF binary")
)
func ( string) (string, error) {
:= make([]byte, 256)
, := os.Open()
if != nil {
return "",
}
defer .Close()
if , := .ReadAt([:64], 0); != nil {
return "",
}
if [0] != 0x7F || [1] != 'E' || [2] != 'L' || [3] != 'F' {
return "", errBadELF
}
var binary.ByteOrder
switch [5] {
default:
return "", errBadELF
case 1:
= binary.LittleEndian
case 2:
= binary.BigEndian
}
var int
var , int64
switch [4] {
default:
return "", errBadELF
case 1:
= int64(.Uint32([32:]))
= int64(.Uint16([46:]))
if != 40 {
return "", errBadELF
}
= int(.Uint16([48:]))
case 2:
= int64(.Uint64([40:]))
= int64(.Uint16([58:]))
if != 64 {
return "", errBadELF
}
= int(.Uint16([60:]))
}
for := 0; < ; ++ {
if , := .ReadAt([:], +int64()*); != nil {
return "",
}
if := .Uint32([4:]); != 7 {
continue
}
var , int64
if == 40 {
= int64(.Uint32([16:]))
= int64(.Uint32([20:]))
} else {
= int64(.Uint64([24:]))
= int64(.Uint64([32:]))
}
+=
for < {
if , := .ReadAt([:16], ); != nil {
return "",
}
:= int(.Uint32([0:]))
:= int(.Uint32([4:]))
:= int(.Uint32([8:]))
:= + int64(12+(+3)&^3)
= + int64((+3)&^3)
if != 4 || != 3 || [12] != 'G' || [13] != 'N' || [14] != 'U' || [15] != '\x00' {
continue
}
if > len() {
return "", errBadELF
}
if , := .ReadAt([:], ); != nil {
return "",
}
return fmt.Sprintf("%x", [:]), nil
}
}
return "", errNoBuildID
}