resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmELF.h
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
5 #include "cmConfigure.h" // IWYU pragma: keep
6
7 #include <cstdint>
8 #include <iosfwd>
9 #include <memory>
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 class cmELFInternal;
15
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
25   /** Destruct.   */
26   ~cmELF();
27
28   cmELF(const cmELF&) = delete;
29   cmELF& operator=(const cmELF&) = delete;
30
31   /** Get the error message if any.  */
32   std::string const& GetErrorMessage() const { return this->ErrorMessage; }
33
34   /** Boolean conversion.  True if the ELF file is valid.  */
35   explicit operator bool() const { return this->Valid(); }
36
37   /** Enumeration of ELF file types.  */
38   enum FileType
39   {
40     FileTypeInvalid,
41     FileTypeRelocatableObject,
42     FileTypeExecutable,
43     FileTypeSharedLibrary,
44     FileTypeCore,
45     FileTypeSpecificOS,
46     FileTypeSpecificProc
47   };
48
49   /** Represent string table entries.  */
50   struct StringEntry
51   {
52     // The string value itself.
53     std::string Value;
54
55     // The position in the file at which the string appears.
56     unsigned long Position;
57
58     // The size of the string table entry.  This includes the space
59     // allocated for one or more null terminators.
60     unsigned long Size;
61
62     // The index of the section entry referencing the string.
63     int IndexInSection;
64   };
65
66   /** Represent entire dynamic section header */
67   using DynamicEntryList = std::vector<std::pair<long, unsigned long>>;
68
69   /** Get the type of the file opened.  */
70   FileType GetFileType() const;
71
72   /** Get the machine of the file opened.  */
73   std::uint16_t GetMachine() const;
74
75   /** Get the number of ELF sections present.  */
76   unsigned int GetNumberOfSections() const;
77
78   /** Get the position of a DYNAMIC section header entry.  Returns
79       zero on error.  */
80   unsigned long GetDynamicEntryPosition(int index) const;
81
82   /** Get a copy of all the DYNAMIC section header entries.
83       Returns an empty vector on error */
84   DynamicEntryList GetDynamicEntries() const;
85
86   /** Encodes a DYNAMIC section header entry list into a char vector according
87       to the type of ELF file this is */
88   std::vector<char> EncodeDynamicEntries(
89     const DynamicEntryList& entries) const;
90
91   /** Get the SONAME field if any.  */
92   bool GetSOName(std::string& soname);
93   StringEntry const* GetSOName();
94
95   /** Get the RPATH field if any.  */
96   StringEntry const* GetRPath();
97
98   /** Get the RUNPATH field if any.  */
99   StringEntry const* GetRunPath();
100
101   /** Returns true if the ELF file targets a MIPS CPU.  */
102   bool IsMIPS() const;
103
104   /** Print human-readable information about the ELF file.  */
105   void PrintInfo(std::ostream& os) const;
106
107   /** Interesting dynamic tags.
108       If the tag is 0, it does not exist in the host ELF implementation */
109   static const long TagRPath, TagRunPath, TagMipsRldMapRel;
110
111 private:
112   friend class cmELFInternal;
113   bool Valid() const;
114   std::unique_ptr<cmELFInternal> Internal;
115   std::string ErrorMessage;
116 };