cmELF.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmELF_h
  11. #define cmELF_h
  12. #if !defined(CMAKE_USE_ELF_PARSER)
  13. # error "This file may be included only if CMAKE_USE_ELF_PARSER is enabled."
  14. #endif
  15. class cmELFInternal;
  16. /** \class cmELF
  17. * \brief Executable and Link Format (ELF) parser.
  18. */
  19. class cmELF
  20. {
  21. public:
  22. /** Construct with the name of the ELF input file to parse. */
  23. cmELF(const char* fname);
  24. /** Destruct. */
  25. ~cmELF();
  26. /** Get the error message if any. */
  27. std::string const& GetErrorMessage() const
  28. {
  29. return this->ErrorMessage;
  30. }
  31. /** Boolean conversion. True if the ELF file is valid. */
  32. operator bool() const { return this->Valid(); }
  33. /** Enumeration of ELF file types. */
  34. enum FileType
  35. {
  36. FileTypeInvalid,
  37. FileTypeRelocatableObject,
  38. FileTypeExecutable,
  39. FileTypeSharedLibrary,
  40. FileTypeCore,
  41. FileTypeSpecificOS,
  42. FileTypeSpecificProc
  43. };
  44. /** Represent string table entries. */
  45. struct StringEntry
  46. {
  47. // The string value itself.
  48. std::string Value;
  49. // The position in the file at which the string appears.
  50. unsigned long Position;
  51. // The size of the string table entry. This includes the space
  52. // allocated for one or more null terminators.
  53. unsigned long Size;
  54. // The index of the section entry referencing the string.
  55. int IndexInSection;
  56. };
  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 number of DYNAMIC section entries before the first
  62. DT_NULL. Returns zero on error. */
  63. unsigned int GetDynamicEntryCount() const;
  64. /** Get the position of a DYNAMIC section header entry. Returns
  65. zero on error. */
  66. unsigned long GetDynamicEntryPosition(int index) const;
  67. /** Read bytes from the file. */
  68. bool ReadBytes(unsigned long pos, unsigned long size, char* buf) const;
  69. /** Get the SONAME field if any. */
  70. bool GetSOName(std::string& soname);
  71. StringEntry const* GetSOName();
  72. /** Get the RPATH field if any. */
  73. StringEntry const* GetRPath();
  74. /** Get the RUNPATH field if any. */
  75. StringEntry const* GetRunPath();
  76. /** Print human-readable information about the ELF file. */
  77. void PrintInfo(std::ostream& os) const;
  78. private:
  79. friend class cmELFInternal;
  80. bool Valid() const;
  81. cmELFInternal* Internal;
  82. std::string ErrorMessage;
  83. };
  84. #endif