| 6 | #define BASE_DEBUG_ELF_READER_H_ |
| 7 |
| 8 | #include <elf.h> |
| 9 |
| 10 | #include "base/base_export.h" |
| 11 | #include "base/containers/span.h" |
| 12 | #include "base/hash/sha1.h" |
| 13 | #include "base/optional.h" |
| 14 | #include "base/strings/string_piece.h" |
| 15 |
| 16 | // Functions for querying metadata from ELF binaries. |
| 17 | // All functions require that the file be fully memory mapped. |
| 18 |
| 19 | #if __SIZEOF_POINTER__ == 4 |
| 20 | using Phdr = Elf32_Phdr; |
| 21 | #else |
| 22 | using Phdr = Elf64_Phdr; |
| 23 | #endif |
| 24 |
| 25 | namespace base { |
| 26 | namespace debug { |
| 27 |
| 28 | // Hex-encodes the build ID from the ELF binary located at |elf_base|. |
| 29 | // Returns the length of the build ID in bytes, or zero if the build ID couldn't |
| 30 | // be read. |
| 31 | // When |uppercase| is |true|, the output string is written using uppercase hex |
| 32 | // characters. Otherwise, the output is lowercased. |
| 33 | constexpr size_t kMaxBuildIdStringLength = kSHA1Length * 2; |
| 34 | using ElfBuildIdBuffer = char[kMaxBuildIdStringLength + 1]; |
| 35 | size_t BASE_EXPORT ReadElfBuildId(const void* elf_base, |
| 36 | bool uppercase, |
| 37 | ElfBuildIdBuffer build_id); |
| 38 |
| 39 | // Returns the library name from the ELF file mapped at |elf_base|. |
| 40 | // Returns an empty result if the name could not be read. |
| 41 | Optional<StringPiece> BASE_EXPORT ReadElfLibraryName(const void* elf_base); |
| 42 |
| 43 | // Returns a span of ELF program headers for the ELF file mapped at |
| 44 | // |elf_base|, or an empty span if the header couldn't be read. |
| 45 | span<const Phdr> BASE_EXPORT GetElfProgramHeaders(const void* elf_base); |
| 46 |
| 47 | } // namespace debug |
| 48 | } // namespace base |
| 49 |
| 50 | #endif // BASE_DEBUG_ELF_READER_H_ |
| 51 |