elf.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2022 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "debug/elf"
  17. "encoding/binary"
  18. "encoding/hex"
  19. "errors"
  20. "fmt"
  21. "io"
  22. "os"
  23. )
  24. const gnuBuildID = "GNU\x00"
  25. // elfIdentifier extracts the elf build ID from an elf file. If allowMissing is true it returns
  26. // an empty identifier if the file exists but the build ID note does not.
  27. func elfIdentifier(filename string, allowMissing bool) (string, error) {
  28. f, err := os.Open(filename)
  29. if err != nil {
  30. return "", fmt.Errorf("failed to open %s: %w", filename, err)
  31. }
  32. defer f.Close()
  33. return elfIdentifierFromReaderAt(f, filename, allowMissing)
  34. }
  35. // elfIdentifierFromReaderAt extracts the elf build ID from a ReaderAt. If allowMissing is true it
  36. // returns an empty identifier if the file exists but the build ID note does not.
  37. func elfIdentifierFromReaderAt(r io.ReaderAt, filename string, allowMissing bool) (string, error) {
  38. f, err := elf.NewFile(r)
  39. if err != nil {
  40. if allowMissing {
  41. if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
  42. return "", nil
  43. }
  44. if _, ok := err.(*elf.FormatError); ok {
  45. // The file was not an elf file.
  46. return "", nil
  47. }
  48. }
  49. return "", fmt.Errorf("failed to parse elf file %s: %w", filename, err)
  50. }
  51. defer f.Close()
  52. buildIDNote := f.Section(".note.gnu.build-id")
  53. if buildIDNote == nil {
  54. if allowMissing {
  55. return "", nil
  56. }
  57. return "", fmt.Errorf("failed to find .note.gnu.build-id in %s", filename)
  58. }
  59. buildIDs, err := readNote(buildIDNote.Open(), f.ByteOrder)
  60. if err != nil {
  61. return "", fmt.Errorf("failed to read .note.gnu.build-id: %w", err)
  62. }
  63. for name, desc := range buildIDs {
  64. if name == gnuBuildID {
  65. return hex.EncodeToString(desc), nil
  66. }
  67. }
  68. return "", nil
  69. }
  70. // readNote reads the contents of a note section, returning it as a map from name to descriptor.
  71. func readNote(note io.Reader, byteOrder binary.ByteOrder) (map[string][]byte, error) {
  72. var noteHeader struct {
  73. Namesz uint32
  74. Descsz uint32
  75. Type uint32
  76. }
  77. notes := make(map[string][]byte)
  78. for {
  79. err := binary.Read(note, byteOrder, &noteHeader)
  80. if err != nil {
  81. if err == io.EOF {
  82. return notes, nil
  83. }
  84. return nil, fmt.Errorf("failed to read note header: %w", err)
  85. }
  86. nameBuf := make([]byte, align4(noteHeader.Namesz))
  87. err = binary.Read(note, byteOrder, &nameBuf)
  88. if err != nil {
  89. return nil, fmt.Errorf("failed to read note name: %w", err)
  90. }
  91. name := string(nameBuf[:noteHeader.Namesz])
  92. descBuf := make([]byte, align4(noteHeader.Descsz))
  93. err = binary.Read(note, byteOrder, &descBuf)
  94. if err != nil {
  95. return nil, fmt.Errorf("failed to read note desc: %w", err)
  96. }
  97. notes[name] = descBuf[:noteHeader.Descsz]
  98. }
  99. }
  100. // align4 rounds the input up to the next multiple of 4.
  101. func align4(i uint32) uint32 {
  102. return (i + 3) &^ 3
  103. }