github.com/bir3/gocompiler@v0.3.205/src/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 "github.com/bir3/gocompiler/src/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 if ptrSize == 8 { 196 readPtr = bo.Uint64 197 } else { 198 return "", "", errNotGoExe 199 } 200 vers = readString(x, ptrSize, readPtr, readPtr(data[16:])) 201 mod = readString(x, ptrSize, readPtr, readPtr(data[16+ptrSize:])) 202 } 203 if vers == "" { 204 return "", "", errNotGoExe 205 } 206 if len(mod) >= 33 && mod[len(mod)-17] == '\n' { 207 // Strip module framing: sentinel strings delimiting the module info. 208 // These are cmd/go/internal/modload.infoStart and infoEnd. 209 mod = mod[16 : len(mod)-16] 210 } else { 211 mod = "" 212 } 213 214 return vers, mod, nil 215 } 216 217 func hasPlan9Magic(magic []byte) bool { 218 if len(magic) >= 4 { 219 m := binary.BigEndian.Uint32(magic) 220 switch m { 221 case plan9obj.Magic386, plan9obj.MagicAMD64, plan9obj.MagicARM: 222 return true 223 } 224 } 225 return false 226 } 227 228 func decodeString(data []byte) (s string, rest []byte) { 229 u, n := binary.Uvarint(data) 230 if n <= 0 || u >= uint64(len(data)-n) { 231 return "", nil 232 } 233 return string(data[n : uint64(n)+u]), data[uint64(n)+u:] 234 } 235 236 // readString returns the string at address addr in the executable x. 237 func readString(x exe, ptrSize int, readPtr func([]byte) uint64, addr uint64) string { 238 hdr, err := x.ReadData(addr, uint64(2*ptrSize)) 239 if err != nil || len(hdr) < 2*ptrSize { 240 return "" 241 } 242 dataAddr := readPtr(hdr) 243 dataLen := readPtr(hdr[ptrSize:]) 244 data, err := x.ReadData(dataAddr, dataLen) 245 if err != nil || uint64(len(data)) < dataLen { 246 return "" 247 } 248 return string(data) 249 } 250 251 // elfExe is the ELF implementation of the exe interface. 252 type elfExe struct { 253 f *elf.File 254 } 255 256 func (x *elfExe) ReadData(addr, size uint64) ([]byte, error) { 257 for _, prog := range x.f.Progs { 258 if prog.Vaddr <= addr && addr <= prog.Vaddr+prog.Filesz-1 { 259 n := prog.Vaddr + prog.Filesz - addr 260 if n > size { 261 n = size 262 } 263 data := make([]byte, n) 264 _, err := prog.ReadAt(data, int64(addr-prog.Vaddr)) 265 if err != nil { 266 return nil, err 267 } 268 return data, nil 269 } 270 } 271 return nil, errUnrecognizedFormat 272 } 273 274 func (x *elfExe) DataStart() uint64 { 275 for _, s := range x.f.Sections { 276 if s.Name == ".go.buildinfo" { 277 return s.Addr 278 } 279 } 280 for _, p := range x.f.Progs { 281 if p.Type == elf.PT_LOAD && p.Flags&(elf.PF_X|elf.PF_W) == elf.PF_W { 282 return p.Vaddr 283 } 284 } 285 return 0 286 } 287 288 // peExe is the PE (Windows Portable Executable) implementation of the exe interface. 289 type peExe struct { 290 f *pe.File 291 } 292 293 func (x *peExe) imageBase() uint64 { 294 switch oh := x.f.OptionalHeader.(type) { 295 case *pe.OptionalHeader32: 296 return uint64(oh.ImageBase) 297 case *pe.OptionalHeader64: 298 return oh.ImageBase 299 } 300 return 0 301 } 302 303 func (x *peExe) ReadData(addr, size uint64) ([]byte, error) { 304 addr -= x.imageBase() 305 for _, sect := range x.f.Sections { 306 if uint64(sect.VirtualAddress) <= addr && addr <= uint64(sect.VirtualAddress+sect.Size-1) { 307 n := uint64(sect.VirtualAddress+sect.Size) - addr 308 if n > size { 309 n = size 310 } 311 data := make([]byte, n) 312 _, err := sect.ReadAt(data, int64(addr-uint64(sect.VirtualAddress))) 313 if err != nil { 314 return nil, errUnrecognizedFormat 315 } 316 return data, nil 317 } 318 } 319 return nil, errUnrecognizedFormat 320 } 321 322 func (x *peExe) DataStart() uint64 { 323 // Assume data is first writable section. 324 const ( 325 IMAGE_SCN_CNT_CODE = 0x00000020 326 IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040 327 IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080 328 IMAGE_SCN_MEM_EXECUTE = 0x20000000 329 IMAGE_SCN_MEM_READ = 0x40000000 330 IMAGE_SCN_MEM_WRITE = 0x80000000 331 IMAGE_SCN_MEM_DISCARDABLE = 0x2000000 332 IMAGE_SCN_LNK_NRELOC_OVFL = 0x1000000 333 IMAGE_SCN_ALIGN_32BYTES = 0x600000 334 ) 335 for _, sect := range x.f.Sections { 336 if sect.VirtualAddress != 0 && sect.Size != 0 && 337 sect.Characteristics&^IMAGE_SCN_ALIGN_32BYTES == IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ|IMAGE_SCN_MEM_WRITE { 338 return uint64(sect.VirtualAddress) + x.imageBase() 339 } 340 } 341 return 0 342 } 343 344 // machoExe is the Mach-O (Apple macOS/iOS) implementation of the exe interface. 345 type machoExe struct { 346 f *macho.File 347 } 348 349 func (x *machoExe) ReadData(addr, size uint64) ([]byte, error) { 350 for _, load := range x.f.Loads { 351 seg, ok := load.(*macho.Segment) 352 if !ok { 353 continue 354 } 355 if seg.Addr <= addr && addr <= seg.Addr+seg.Filesz-1 { 356 if seg.Name == "__PAGEZERO" { 357 continue 358 } 359 n := seg.Addr + seg.Filesz - addr 360 if n > size { 361 n = size 362 } 363 data := make([]byte, n) 364 _, err := seg.ReadAt(data, int64(addr-seg.Addr)) 365 if err != nil { 366 return nil, err 367 } 368 return data, nil 369 } 370 } 371 return nil, errUnrecognizedFormat 372 } 373 374 func (x *machoExe) DataStart() uint64 { 375 // Look for section named "__go_buildinfo". 376 for _, sec := range x.f.Sections { 377 if sec.Name == "__go_buildinfo" { 378 return sec.Addr 379 } 380 } 381 // Try the first non-empty writable segment. 382 const RW = 3 383 for _, load := range x.f.Loads { 384 seg, ok := load.(*macho.Segment) 385 if ok && seg.Addr != 0 && seg.Filesz != 0 && seg.Prot == RW && seg.Maxprot == RW { 386 return seg.Addr 387 } 388 } 389 return 0 390 } 391 392 // xcoffExe is the XCOFF (AIX eXtended COFF) implementation of the exe interface. 393 type xcoffExe struct { 394 f *xcoff.File 395 } 396 397 func (x *xcoffExe) ReadData(addr, size uint64) ([]byte, error) { 398 for _, sect := range x.f.Sections { 399 if sect.VirtualAddress <= addr && addr <= sect.VirtualAddress+sect.Size-1 { 400 n := sect.VirtualAddress + sect.Size - addr 401 if n > size { 402 n = size 403 } 404 data := make([]byte, n) 405 _, err := sect.ReadAt(data, int64(addr-sect.VirtualAddress)) 406 if err != nil { 407 return nil, err 408 } 409 return data, nil 410 } 411 } 412 return nil, errors.New("address not mapped") 413 } 414 415 func (x *xcoffExe) DataStart() uint64 { 416 if s := x.f.SectionByType(xcoff.STYP_DATA); s != nil { 417 return s.VirtualAddress 418 } 419 return 0 420 } 421 422 // plan9objExe is the Plan 9 a.out implementation of the exe interface. 423 type plan9objExe struct { 424 f *plan9obj.File 425 } 426 427 func (x *plan9objExe) DataStart() uint64 { 428 if s := x.f.Section("data"); s != nil { 429 return uint64(s.Offset) 430 } 431 return 0 432 } 433 434 func (x *plan9objExe) ReadData(addr, size uint64) ([]byte, error) { 435 for _, sect := range x.f.Sections { 436 if uint64(sect.Offset) <= addr && addr <= uint64(sect.Offset+sect.Size-1) { 437 n := uint64(sect.Offset+sect.Size) - addr 438 if n > size { 439 n = size 440 } 441 data := make([]byte, n) 442 _, err := sect.ReadAt(data, int64(addr-uint64(sect.Offset))) 443 if err != nil { 444 return nil, err 445 } 446 return data, nil 447 } 448 } 449 return nil, errors.New("address not mapped") 450 451 }