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. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <cstdint>
  6. #include <iosfwd>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  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. cmELF(const cmELF&) = delete;
  23. cmELF& operator=(const cmELF&) = delete;
  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. explicit 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. using DynamicEntryList = std::vector<std::pair<long, unsigned long>>;
  54. /** Get the type of the file opened. */
  55. FileType GetFileType() const;
  56. /** Get the machine of the file opened. */
  57. std::uint16_t GetMachine() 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. /** Returns true if the ELF file targets a MIPS CPU. */
  78. bool IsMIPS() const;
  79. /** Print human-readable information about the ELF file. */
  80. void PrintInfo(std::ostream& os) const;
  81. /** Interesting dynamic tags.
  82. If the tag is 0, it does not exist in the host ELF implementation */
  83. static const long TagRPath, TagRunPath, TagMipsRldMapRel;
  84. private:
  85. friend class cmELFInternal;
  86. bool Valid() const;
  87. std::unique_ptr<cmELFInternal> Internal;
  88. std::string ErrorMessage;
  89. };