cmELF.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmELF_h
  4. #define cmELF_h
  5. #include <cmConfigure.h>
  6. #include <iosfwd>
  7. #include <string>
  8. #if !defined(CMAKE_USE_ELF_PARSER)
  9. #error "This file may be included only if CMAKE_USE_ELF_PARSER is enabled."
  10. #endif
  11. class cmELFInternal;
  12. /** \class cmELF
  13. * \brief Executable and Link Format (ELF) parser.
  14. */
  15. class cmELF
  16. {
  17. public:
  18. /** Construct with the name of the ELF input file to parse. */
  19. cmELF(const char* fname);
  20. /** Destruct. */
  21. ~cmELF();
  22. /** Get the error message if any. */
  23. std::string const& GetErrorMessage() const { return this->ErrorMessage; }
  24. /** Boolean conversion. True if the ELF file is valid. */
  25. operator bool() const { return this->Valid(); }
  26. /** Enumeration of ELF file types. */
  27. enum FileType
  28. {
  29. FileTypeInvalid,
  30. FileTypeRelocatableObject,
  31. FileTypeExecutable,
  32. FileTypeSharedLibrary,
  33. FileTypeCore,
  34. FileTypeSpecificOS,
  35. FileTypeSpecificProc
  36. };
  37. /** Represent string table entries. */
  38. struct StringEntry
  39. {
  40. // The string value itself.
  41. std::string Value;
  42. // The position in the file at which the string appears.
  43. unsigned long Position;
  44. // The size of the string table entry. This includes the space
  45. // allocated for one or more null terminators.
  46. unsigned long Size;
  47. // The index of the section entry referencing the string.
  48. int IndexInSection;
  49. };
  50. /** Get the type of the file opened. */
  51. FileType GetFileType() const;
  52. /** Get the number of ELF sections present. */
  53. unsigned int GetNumberOfSections() const;
  54. /** Get the number of DYNAMIC section entries before the first
  55. DT_NULL. Returns zero on error. */
  56. unsigned int GetDynamicEntryCount() const;
  57. /** Get the position of a DYNAMIC section header entry. Returns
  58. zero on error. */
  59. unsigned long GetDynamicEntryPosition(int index) const;
  60. /** Read bytes from the file. */
  61. bool ReadBytes(unsigned long pos, unsigned long size, char* buf) const;
  62. /** Get the SONAME field if any. */
  63. bool GetSOName(std::string& soname);
  64. StringEntry const* GetSOName();
  65. /** Get the RPATH field if any. */
  66. StringEntry const* GetRPath();
  67. /** Get the RUNPATH field if any. */
  68. StringEntry const* GetRunPath();
  69. /** Print human-readable information about the ELF file. */
  70. void PrintInfo(std::ostream& os) const;
  71. private:
  72. friend class cmELFInternal;
  73. bool Valid() const;
  74. cmELFInternal* Internal;
  75. std::string ErrorMessage;
  76. };
  77. #endif