elf_reader.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_DEBUG_ELF_READER_H_
  5. #define BASE_DEBUG_ELF_READER_H_
  6. #include <elf.h>
  7. #include "base/base_export.h"
  8. #include "base/containers/span.h"
  9. #include "base/hash/sha1.h"
  10. #include "base/optional.h"
  11. #include "base/strings/string_piece.h"
  12. // Functions for querying metadata from ELF binaries. All functions are signal
  13. // safe and require that the file be fully memory mapped.
  14. #if __SIZEOF_POINTER__ == 4
  15. using Phdr = Elf32_Phdr;
  16. #else
  17. using Phdr = Elf64_Phdr;
  18. #endif
  19. namespace base {
  20. namespace debug {
  21. // Hex-encodes the build ID from the ELF binary located at |elf_mapped_base|.
  22. // Returns the length of the build ID in bytes, or zero if the build ID couldn't
  23. // be read.
  24. // When |uppercase| is |true|, the output string is written using uppercase hex
  25. // characters. Otherwise, the output is lowercased.
  26. constexpr size_t kMaxBuildIdStringLength = kSHA1Length * 2;
  27. using ElfBuildIdBuffer = char[kMaxBuildIdStringLength + 1];
  28. size_t BASE_EXPORT ReadElfBuildId(const void* elf_mapped_base,
  29. bool uppercase,
  30. ElfBuildIdBuffer build_id);
  31. // Returns the library name from the ELF file mapped at |elf_mapped_base|.
  32. // Returns an empty result if the name could not be read.
  33. Optional<StringPiece> BASE_EXPORT
  34. ReadElfLibraryName(const void* elf_mapped_base);
  35. // Returns a span of ELF program headers for the ELF file mapped at
  36. // |elf_mapped_base|, or an empty span if the header couldn't be read.
  37. span<const Phdr> BASE_EXPORT GetElfProgramHeaders(const void* elf_mapped_base);
  38. // Returns the offset to add to virtual addresses in the image to compute the
  39. // mapped virtual address. This value must be added to the p_vaddr field in the
  40. // Phdrs to obtain the mapped virtual address.
  41. size_t BASE_EXPORT GetRelocationOffset(const void* elf_mapped_base);
  42. } // namespace debug
  43. } // namespace base
  44. #endif // BASE_DEBUG_ELF_READER_H_