cmELF.h 3.09 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
Brad King's avatar
Brad King committed
3 4 5
#ifndef cmELF_h
#define cmELF_h

6
#include "cmConfigure.h" // IWYU pragma: keep
7 8

#include <iosfwd>
9
#include <memory>
10
#include <string>
11 12
#include <utility>
#include <vector>
13

Brad King's avatar
Brad King committed
14
#if !defined(CMAKE_USE_ELF_PARSER)
15
#  error "This file may be included only if CMAKE_USE_ELF_PARSER is enabled."
Brad King's avatar
Brad King committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#endif

class cmELFInternal;

/** \class cmELF
 * \brief Executable and Link Format (ELF) parser.
 */
class cmELF
{
public:
  /** Construct with the name of the ELF input file to parse.  */
  cmELF(const char* fname);

  /** Destruct.   */
  ~cmELF();

32 33 34
  cmELF(const cmELF&) = delete;
  cmELF& operator=(const cmELF&) = delete;

Brad King's avatar
Brad King committed
35
  /** Get the error message if any.  */
36
  std::string const& GetErrorMessage() const { return this->ErrorMessage; }
Brad King's avatar
Brad King committed
37 38

  /** Boolean conversion.  True if the ELF file is valid.  */
wahikihiki's avatar
wahikihiki committed
39
  explicit operator bool() const { return this->Valid(); }
Brad King's avatar
Brad King committed
40 41 42 43 44 45 46 47

  /** Enumeration of ELF file types.  */
  enum FileType
  {
    FileTypeInvalid,
    FileTypeRelocatableObject,
    FileTypeExecutable,
    FileTypeSharedLibrary,
48 49 50
    FileTypeCore,
    FileTypeSpecificOS,
    FileTypeSpecificProc
Brad King's avatar
Brad King committed
51 52
  };

53 54 55 56 57 58 59 60
  /** Represent string table entries.  */
  struct StringEntry
  {
    // The string value itself.
    std::string Value;

    // The position in the file at which the string appears.
    unsigned long Position;
61 62 63 64

    // The size of the string table entry.  This includes the space
    // allocated for one or more null terminators.
    unsigned long Size;
65 66 67

    // The index of the section entry referencing the string.
    int IndexInSection;
68 69
  };

70
  /** Represent entire dynamic section header */
71
  typedef std::vector<std::pair<long, unsigned long>> DynamicEntryList;
72

Brad King's avatar
Brad King committed
73 74 75 76 77 78
  /** Get the type of the file opened.  */
  FileType GetFileType() const;

  /** Get the number of ELF sections present.  */
  unsigned int GetNumberOfSections() const;

79 80 81 82
  /** Get the position of a DYNAMIC section header entry.  Returns
      zero on error.  */
  unsigned long GetDynamicEntryPosition(int index) const;

83 84 85 86 87 88 89 90 91
  /** Get a copy of all the DYNAMIC section header entries.
      Returns an empty vector on error */
  DynamicEntryList GetDynamicEntries() const;

  /** Encodes a DYNAMIC section header entry list into a char vector according
      to the type of ELF file this is */
  std::vector<char> EncodeDynamicEntries(
    const DynamicEntryList& entries) const;

Brad King's avatar
Brad King committed
92 93
  /** Get the SONAME field if any.  */
  bool GetSOName(std::string& soname);
94 95 96 97 98 99 100
  StringEntry const* GetSOName();

  /** Get the RPATH field if any.  */
  StringEntry const* GetRPath();

  /** Get the RUNPATH field if any.  */
  StringEntry const* GetRunPath();
Brad King's avatar
Brad King committed
101 102 103 104

  /** Print human-readable information about the ELF file.  */
  void PrintInfo(std::ostream& os) const;

105 106
  /** Interesting dynamic tags.
      If the tag is 0, it does not exist in the host ELF implementation */
107
  static const long TagRPath, TagRunPath, TagMipsRldMapRel;
108

Brad King's avatar
Brad King committed
109 110 111
private:
  friend class cmELFInternal;
  bool Valid() const;
112
  std::unique_ptr<cmELFInternal> Internal;
Brad King's avatar
Brad King committed
113 114 115 116
  std::string ErrorMessage;
};

#endif