cmELF.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <iosfwd>
  6. #include <memory>
  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. cmELF(const cmELF&) = delete;
  25. cmELF& operator=(const cmELF&) = delete;
  26. /** Get the error message if any. */
  27. std::string const& GetErrorMessage() const { return this->ErrorMessage; }
  28. /** Boolean conversion. True if the ELF file is valid. */
  29. explicit operator bool() const { return this->Valid(); }
  30. /** Enumeration of ELF file types. */
  31. enum FileType
  32. {
  33. FileTypeInvalid,
  34. FileTypeRelocatableObject,
  35. FileTypeExecutable,
  36. FileTypeSharedLibrary,
  37. FileTypeCore,
  38. FileTypeSpecificOS,
  39. FileTypeSpecificProc
  40. };
  41. /** Represent string table entries. */
  42. struct StringEntry
  43. {
  44. // The string value itself.
  45. std::string Value;
  46. // The position in the file at which the string appears.
  47. unsigned long Position;
  48. // The size of the string table entry. This includes the space
  49. // allocated for one or more null terminators.
  50. unsigned long Size;
  51. // The index of the section entry referencing the string.
  52. int IndexInSection;
  53. };
  54. /** Represent entire dynamic section header */
  55. using DynamicEntryList = std::vector<std::pair<long, unsigned long>>;
  56. /** Get the type of the file opened. */
  57. FileType GetFileType() const;
  58. /** Get the number of ELF sections present. */
  59. unsigned int GetNumberOfSections() const;
  60. /** Get the position of a DYNAMIC section header entry. Returns
  61. zero on error. */
  62. unsigned long GetDynamicEntryPosition(int index) const;
  63. /** Get a copy of all the DYNAMIC section header entries.
  64. Returns an empty vector on error */
  65. DynamicEntryList GetDynamicEntries() const;
  66. /** Encodes a DYNAMIC section header entry list into a char vector according
  67. to the type of ELF file this is */
  68. std::vector<char> EncodeDynamicEntries(
  69. const DynamicEntryList& entries) const;
  70. /** Get the SONAME field if any. */
  71. bool GetSOName(std::string& soname);
  72. StringEntry const* GetSOName();
  73. /** Get the RPATH field if any. */
  74. StringEntry const* GetRPath();
  75. /** Get the RUNPATH field if any. */
  76. StringEntry const* GetRunPath();
  77. /** Print human-readable information about the ELF file. */
  78. void PrintInfo(std::ostream& os) const;
  79. /** Interesting dynamic tags.
  80. If the tag is 0, it does not exist in the host ELF implementation */
  81. static const long TagRPath, TagRunPath, TagMipsRldMapRel;
  82. private:
  83. friend class cmELFInternal;
  84. bool Valid() const;
  85. std::unique_ptr<cmELFInternal> Internal;
  86. std::string ErrorMessage;
  87. };