cmELF.h 3.1 KB

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