github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/debug/buildinfo/buildinfo.go (about) 1 // Copyright 2021 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. 4 5 // Package buildinfo provides access to information embedded in a Go binary 6 // about how it was built. This includes the Go toolchain version, and the 7 // set of modules used (for binaries built in module mode). 8 // 9 // Build information is available for the currently running binary in 10 // runtime/debug.ReadBuildInfo. 11 package buildinfo 12 13 import ( 14 "bytes" 15 "debug/elf" 16 "debug/macho" 17 "debug/pe" 18 "debug/plan9obj" 19 "encoding/binary" 20 "errors" 21 "fmt" 22 "internal/xcoff" 23 "io" 24 "io/fs" 25 "os" 26 "runtime/debug" 27 ) 28 29 // Type alias for build info. We cannot move the types here, since 30 // runtime/debug would need to import this package, which would make it 31 // a much larger dependency. 32 type BuildInfo = debug.BuildInfo 33 34 var ( 35 // errUnrecognizedFormat is returned when a given executable file doesn't 36 // appear to be in a known format, or it breaks the rules of that format, 37 // or when there are I/O errors reading the file. 38 errUnrecognizedFormat = errors.New("unrecognized file format") 39 40 // errNotGoExe is returned when a given executable file is valid but does 41 // not contain Go build information. 42 errNotGoExe = errors.New("not a Go executable") 43 44 // The build info blob left by the linker is identified by 45 // a 16-byte header, consisting of buildInfoMagic (14 bytes), 46 // the binary's pointer size (1 byte), 47 // and whether the binary is big endian (1 byte). 48 buildInfoMagic = []byte("\xff Go buildinf:") 49 ) 50 51 // ReadFile returns build information embedded in a Go binary 52 // file at the given path. Most information is only available for binaries built 53 // with module support. 54 func ReadFile(name string) (info *BuildInfo, err error) { 55 defer func() { 56 if pathErr := (*fs.PathError)(nil); errors.As(err, &pathErr) { 57 err = fmt.Errorf("could not read Go build info: %w", err) 58 } else if err != nil { 59 err = fmt.Errorf("could not read Go build info from %s: %w", name, err) 60 } 61 }() 62 63 f, err := os.Open(name) 64 if err != nil { 65 return nil, err 66 } 67 defer f.Close() 68 return Read(f) 69 } 70 71 // Read returns build information embedded in a Go binary file 72 // accessed through the given ReaderAt. Most information is only available for 73 // binaries built with module support. 74 func Read(r io.ReaderAt) (*BuildInfo, error) { 75 vers, mod, err := readRawBuildInfo(r) 76 if err != nil { 77 return nil, err 78 } 79 bi, err := debug.ParseBuildInfo(mod) 80 if err != nil { 81 return nil, err 82 } 83 bi.GoVersion = vers 84 return bi, nil 85 } 86 87 type exe interface { 88 // ReadData reads and returns up to size bytes starting at virtual address addr. 89 ReadData(addr, size uint64) ([]byte, error) 90 91 // DataStart returns the virtual address of the segment or section that 92 // should contain build information. This is either a specially named section 93 // or the first writable non-zero data segment. 94 DataStart() uint64 95 } 96 97 // readRawBuildInfo extracts the Go toolchain version and module information 98 // strings from a Go binary. On success, vers should be non-empty. mod 99 // is empty if the binary was not built with modules enabled. 100 func readRawBuildInfo(r io.ReaderAt) (vers, mod string, err error) { 101 // Read the first bytes of the file to identify the format, then delegate to 102 // a format-specific function to load segment and section headers. 103 ident := make([]byte, 16) 104 if n, err := r.ReadAt(ident, 0); n < len(ident) || err != nil { 105 return "", "", errUnrecognizedFormat 106 } 107 108 var x exe 109 switch { 110 case bytes.HasPrefix(ident, []byte("\x7FELF")): 111 f, err := elf.NewFile(r) 112 if err != nil { 113 return "", "", errUnrecognizedFormat 114 } 115 x = &elfExe{f} 116 case bytes.HasPrefix(ident, []byte("MZ")): 117 f, err := pe.NewFile(r) 118 if err != nil { 119 return "", "", errUnrecognizedFormat 120 } 121 x = &peExe{f} 122 case bytes.HasPrefix(ident, []byte("\xFE\xED\xFA")) || bytes.HasPrefix(ident[1:], []byte("\xFA\xED\xFE")): 123 f, err := macho.NewFile(r) 124 if err != nil { 125 return "", "", errUnrecognizedFormat 126 } 127 x = &machoExe{f} 128 case bytes.HasPrefix(ident, []byte{0x01, 0xDF}) || bytes.HasPrefix(ident, []byte{0x01, 0xF7}): 129 f, err := xcoff.NewFile(r) 130 if err != nil { 131 return "", "", errUnrecognizedFormat 132 } 133 x = &xcoffExe{f} 134 case hasPlan9Magic(ident): 135 f, err := plan9obj.NewFile(r) 136 if err != nil { 137 return "", "", errUnrecognizedFormat 138 } 139 x = &plan9objExe{f} 140 default: 141 return "", "", errUnrecognizedFormat 142 } 143 144 // Read the first 64kB of dataAddr to find the build info blob. 145 // On some platforms, the blob will be in its own section, and DataStart 146 // returns the address of that section. On others, it's somewhere in the 147 // data segment; the linker puts it near the beginning. 148 // See cmd/link/internal/ld.Link.buildinfo. 149 dataAddr := x.DataStart() 150 data, err := x.ReadData(dataAddr, 64*1024) 151 if err != nil { 152 return "", "", err 153 } 154 const ( 155 buildInfoAlign = 16 156 buildInfoSize = 32 157 ) 158 for { 159 i := bytes.Index(data, buildInfoMagic) 160 if i < 0 || len(data)-i < buildInfoSize { 161 return "", "", errNotGoExe 162 } 163 if i%buildInfoAlign == 0 && len(data)-i >= buildInfoSize { 164 data = data[i:] 165 break 166 } 167 data = data[(i+buildInfoAlign-1)&^(buildInfoAlign-1):] 168 } 169 170 // Decode the blob. 171 // The first 14 bytes are buildInfoMagic. 172 // The next two bytes indicate pointer size in bytes (4 or 8) and endianness 173 // (0 for little, 1 for big). 174 // Two virtual addresses to Go strings follow that: runtime.buildVersion, 175 // and runtime.modinfo. 176 // On 32-bit platforms, the last 8 bytes are unused. 177 // If the endianness has the 2 bit set, then the pointers are zero 178 // and the 32-byte header is followed by varint-prefixed string data 179 // for the two string values we care about. 180 ptrSize := int(data[14]) 181 if data[15]&2 != 0 { 182 vers, data = decodeString(data[32:]) 183 mod, data = decodeString(data) 184 } else { 185 bigEndian := data[15] != 0 186 var bo binary.ByteOrder 187 if bigEndian { 188 bo = binary.BigEndian 189 } else { 190 bo = binary.LittleEndian 191 } 192 var readPtr func([]byte) uint64 193 if ptrSize == 4 { 194 readPtr = func(b []byte) uint64 { return uint64(bo.Uint32(b)) } 195 } else { 196 readPtr = bo.Uint64 197 } 198 vers = readString(x, ptrSize, readPtr, readPtr(data[16:])) 199 mod = readString(x, ptrSize, readPtr, readPtr(data[16+ptrSize:])) 200 } 201 if vers == "" { 202 return "", "", errNotGoExe 203 } 204 if len(mod) >= 33 && mod[len(mod)-17] == '\n' { 205 // Strip module framing: sentinel strings delimiting the module info. 206 // These are cmd/go/internal/modload.infoStart and infoEnd. 207 mod = mod[16 : len(mod)-16] 208 } else { 209 mod = "" 210 } 211 212 return vers, mod, nil 213 } 214 215 func hasPlan9Magic(magic []byte) bool { 216 if len(magic) >= 4 { 217 m := binary.BigEndian.Uint32(magic) 218 switch m { 219 case plan9obj.Magic386, plan9obj.MagicAMD64, plan9obj.MagicARM: 220 return true 221 } 222 } 223 return false 224 } 225 226 func decodeString(data []byte) (s string, rest []byte) { 227 u, n := binary.Uvarint(data) 228 if n <= 0 || u >= uint64(len(data)-n) { 229 return "", nil 230 } 231 return string(data[n : uint64(n)+u]), data[uint64(n)+u:] 232 } 233 234 // readString returns the string at address addr in the executable x. 235 func readString(x exe, ptrSize int, readPtr func([]byte) uint64, addr uint64) string { 236 hdr, err := x.ReadData(addr, uint64(2*ptrSize)) 237 if err != nil || len(hdr) < 2*ptrSize { 238 return "" 239 } 240 dataAddr := readPtr(hdr) 241 dataLen := readPtr(hdr[ptrSize:]) 242 data, err := x.ReadData(dataAddr, dataLen) 243 if err != nil || uint64(len(data)) < dataLen { 244 return "" 245 } 246 return string(data) 247 } 248 249 // elfExe is the ELF implementation of the exe interface. 250 type elfExe struct { 251 f *elf.File 252 } 253 254 func (x *elfExe) ReadData(addr, size uint64) ([]byte, error) { 255 for _, prog := range x.f.Progs { 256 if prog.Vaddr <= addr && addr <= prog.Vaddr+prog.Filesz-1 { 257 n := prog.Vaddr + prog.Filesz - addr 258 if n > size { 259 n = size 260 } 261 data := make([]byte, n) 262 _, err := prog.ReadAt(data, int64(addr-prog.Vaddr)) 263 if err != nil { 264 return nil, err 265 } 266 return data, nil 267 } 268 } 269 return nil, errUnrecognizedFormat 270 } 271 272 func (x *elfExe) DataStart() uint64 { 273 for _, s := range x.f.Sections { 274 if s.Name == ".go.buildinfo" { 275 return s.Addr 276 } 277 } 278 for _, p := range x.f.Progs { 279 if p.Type == elf.PT_LOAD && p.Flags&(elf.PF_X|elf.PF_W) == elf.PF_W { 280 return p.Vaddr 281 } 282 } 283 return 0 284 } 285 286 // peExe is the PE (Windows Portable Executable) implementation of the exe interface. 287 type peExe struct { 288 f *pe.File 289 } 290 291 func (x *peExe) imageBase() uint64 { 292 switch oh := x.f.OptionalHeader.(type) { 293 case *pe.OptionalHeader32: 294 return uint64(oh.ImageBase) 295 case *pe.OptionalHeader64: 296 return oh.ImageBase 297 } 298 return 0 299 } 300 301 func (x *peExe) ReadData(addr, size uint64) ([]byte, error) { 302 addr -= x.imageBase() 303 for _, sect := range x.f.Sections { 304 if uint64(sect.VirtualAddress) <= addr && addr <= uint64(sect.VirtualAddress+sect.Size-1) { 305 n := uint64(sect.VirtualAddress+sect.Size) - addr 306 if n > size { 307 n = size 308 } 309 data := make([]byte, n) 310 _, err := sect.ReadAt(data, int64(addr-uint64(sect.VirtualAddress))) 311 if err != nil { 312 return nil, errUnrecognizedFormat 313 } 314 return data, nil 315 } 316 } 317 return nil, errUnrecognizedFormat 318 } 319 320 func (x *peExe) DataStart() uint64 { 321 // Assume data is first writable section. 322 const ( 323 IMAGE_SCN_CNT_CODE = 0x00000020 324 IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040 325 IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080 326 IMAGE_SCN_MEM_EXECUTE = 0x20000000 327 IMAGE_SCN_MEM_READ = 0x40000000 328 IMAGE_SCN_MEM_WRITE = 0x80000000 329 IMAGE_SCN_MEM_DISCARDABLE = 0x2000000 330 IMAGE_SCN_LNK_NRELOC_OVFL = 0x1000000 331 IMAGE_SCN_ALIGN_32BYTES = 0x600000 332 ) 333 for _, sect := range x.f.Sections { 334 if sect.VirtualAddress != 0 && sect.Size != 0 && 335 sect.Characteristics&^IMAGE_SCN_ALIGN_32BYTES == IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ|IMAGE_SCN_MEM_WRITE { 336 return uint64(sect.VirtualAddress) + x.imageBase() 337 } 338 } 339 return 0 340 } 341 342 // machoExe is the Mach-O (Apple macOS/iOS) implementation of the exe interface. 343 type machoExe struct { 344 f *macho.File 345 } 346 347 func (x *machoExe) ReadData(addr, size uint64) ([]byte, error) { 348 for _, load := range x.f.Loads { 349 seg, ok := load.(*macho.Segment) 350 if !ok { 351 continue 352 } 353 if seg.Addr <= addr && addr <= seg.Addr+seg.Filesz-1 { 354 if seg.Name == "__PAGEZERO" { 355 continue 356 } 357 n := seg.Addr + seg.Filesz - addr 358 if n > size { 359 n = size 360 } 361 data := make([]byte, n) 362 _, err := seg.ReadAt(data, int64(addr-seg.Addr)) 363 if err != nil { 364 return nil, err 365 } 366 return data, nil 367 } 368 } 369 return nil, errUnrecognizedFormat 370 } 371 372 func (x *machoExe) DataStart() uint64 { 373 // Look for section named "__go_buildinfo". 374 for _, sec := range x.f.Sections { 375 if sec.Name == "__go_buildinfo" { 376 return sec.Addr 377 } 378 } 379 // Try the first non-empty writable segment. 380 const RW = 3 381 for _, load := range x.f.Loads { 382 seg, ok := load.(*macho.Segment) 383 if ok && seg.Addr != 0 && seg.Filesz != 0 && seg.Prot == RW && seg.Maxprot == RW { 384 return seg.Addr 385 } 386 } 387 return 0 388 } 389 390 // xcoffExe is the XCOFF (AIX eXtended COFF) implementation of the exe interface. 391 type xcoffExe struct { 392 f *xcoff.File 393 } 394 395 func (x *xcoffExe) ReadData(addr, size uint64) ([]byte, error) { 396 for _, sect := range x.f.Sections { 397 if sect.VirtualAddress <= addr && addr <= sect.VirtualAddress+sect.Size-1 { 398 n := sect.VirtualAddress + sect.Size - addr 399 if n > size { 400 n = size 401 } 402 data := make([]byte, n) 403 _, err := sect.ReadAt(data, int64(addr-sect.VirtualAddress)) 404 if err != nil { 405 return nil, err 406 } 407 return data, nil 408 } 409 } 410 return nil, errors.New("address not mapped") 411 } 412 413 func (x *xcoffExe) DataStart() uint64 { 414 if s := x.f.SectionByType(xcoff.STYP_DATA); s != nil { 415 return s.VirtualAddress 416 } 417 return 0 418 } 419 420 // plan9objExe is the Plan 9 a.out implementation of the exe interface. 421 type plan9objExe struct { 422 f *plan9obj.File 423 } 424 425 func (x *plan9objExe) DataStart() uint64 { 426 if s := x.f.Section("data"); s != nil { 427 return uint64(s.Offset) 428 } 429 return 0 430 } 431 432 func (x *plan9objExe) ReadData(addr, size uint64) ([]byte, error) { 433 for _, sect := range x.f.Sections { 434 if uint64(sect.Offset) <= addr && addr <= uint64(sect.Offset+sect.Size-1) { 435 n := uint64(sect.Offset+sect.Size) - addr 436 if n > size { 437 n = size 438 } 439 data := make([]byte, n) 440 _, err := sect.ReadAt(data, int64(addr-uint64(sect.Offset))) 441 if err != nil { 442 return nil, err 443 } 444 return data, nil 445 } 446 } 447 return nil, errors.New("address not mapped") 448 449 }