cmELF.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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" // IWYU pragma: keep
  6. #include <iosfwd>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #if !defined(CMAKE_USE_ELF_PARSER)
  11. #error "This file may be included only if CMAKE_USE_ELF_PARSER is enabled."
  12. #endif
  13. class cmELFInternal;
  14. /** \class cmELF
  15. * \brief Executable and Link Format (ELF) parser.
  16. */
  17. class cmELF
  18. {
  19. public:
  20. /** Construct with the name of the ELF input file to parse. */
  21. cmELF(const char* fname);
  22. /** Destruct. */
  23. ~cmELF();
  24. /** Get the error message if any. */
  25. std::string const& GetErrorMessage() const { return this->ErrorMessage; }
  26. /** Boolean conversion. True if the ELF file is valid. */
  27. operator bool() const { return this->Valid(); }
  28. /** Enumeration of ELF file types. */
  29. enum FileType
  30. {
  31. FileTypeInvalid,
  32. FileTypeRelocatableObject,
  33. FileTypeExecutable,
  34. FileTypeSharedLibrary,
  35. FileTypeCore,
  36. FileTypeSpecificOS,
  37. FileTypeSpecificProc
  38. };
  39. /** Represent string table entries. */
  40. struct StringEntry
  41. {
  42. // The string value itself.
  43. std::string Value;
  44. // The position in the file at which the string appears.
  45. unsigned long Position;
  46. // The size of the string table entry. This includes the space
  47. // allocated for one or more null terminators.
  48. unsigned long Size;
  49. // The index of the section entry referencing the string.
  50. int IndexInSection;
  51. };
  52. /** Represent entire dynamic section header */
  53. typedef std::vector<std::pair<long, unsigned long>> DynamicEntryList;
  54. /** Get the type of the file opened. */
  55. FileType GetFileType() const;
  56. /** Get the number of ELF sections present. */
  57. unsigned int GetNumberOfSections() const;
  58. /** Get the position of a DYNAMIC section header entry. Returns
  59. zero on error. */
  60. unsigned long GetDynamicEntryPosition(int index) const;
  61. /** Get a copy of all the DYNAMIC section header entries.
  62. Returns an empty vector on error */
  63. DynamicEntryList GetDynamicEntries() const;
  64. /** Encodes a DYNAMIC section header entry list into a char vector according
  65. to the type of ELF file this is */
  66. std::vector<char> EncodeDynamicEntries(
  67. const DynamicEntryList& entries) const;
  68. /** Get the SONAME field if any. */
  69. bool GetSOName(std::string& soname);
  70. StringEntry const* GetSOName();
  71. /** Get the RPATH field if any. */
  72. StringEntry const* GetRPath();
  73. /** Get the RUNPATH field if any. */
  74. StringEntry const* GetRunPath();
  75. /** Print human-readable information about the ELF file. */
  76. void PrintInfo(std::ostream& os) const;
  77. /** Interesting dynamic tags.
  78. If the tag is 0, it does not exist in the host ELF implementation */
  79. static const long TagRPath, TagRunPath, TagMipsRldMapRel;
  80. private:
  81. friend class cmELFInternal;
  82. bool Valid() const;
  83. cmELFInternal* Internal;
  84. std::string ErrorMessage;
  85. };
  86. #endif