CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / cmELF.h
blobce8bd7fb922987bbce1bd6d5501779dfcaab9c9b
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
5 #include "cmConfigure.h" // IWYU pragma: keep
7 #include <cstdint>
8 #include <iosfwd>
9 #include <memory>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 class cmELFInternal;
16 /** \class cmELF
17 * \brief Executable and Link Format (ELF) parser.
19 class cmELF
21 public:
22 /** Construct with the name of the ELF input file to parse. */
23 cmELF(const char* fname);
25 /** Destruct. */
26 ~cmELF();
28 cmELF(const cmELF&) = delete;
29 cmELF& operator=(const cmELF&) = delete;
31 /** Get the error message if any. */
32 std::string const& GetErrorMessage() const { return this->ErrorMessage; }
34 /** Boolean conversion. True if the ELF file is valid. */
35 explicit operator bool() const { return this->Valid(); }
37 /** Enumeration of ELF file types. */
38 enum FileType
40 FileTypeInvalid,
41 FileTypeRelocatableObject,
42 FileTypeExecutable,
43 FileTypeSharedLibrary,
44 FileTypeCore,
45 FileTypeSpecificOS,
46 FileTypeSpecificProc
49 /** Represent string table entries. */
50 struct StringEntry
52 // The string value itself.
53 std::string Value;
55 // The position in the file at which the string appears.
56 unsigned long Position;
58 // The size of the string table entry. This includes the space
59 // allocated for one or more null terminators.
60 unsigned long Size;
62 // The index of the section entry referencing the string.
63 int IndexInSection;
66 /** Represent entire dynamic section header */
67 using DynamicEntryList = std::vector<std::pair<long, unsigned long>>;
69 /** Get the type of the file opened. */
70 FileType GetFileType() const;
72 /** Get the machine of the file opened. */
73 std::uint16_t GetMachine() const;
75 /** Get the number of ELF sections present. */
76 unsigned int GetNumberOfSections() const;
78 /** Get the position of a DYNAMIC section header entry. Returns
79 zero on error. */
80 unsigned long GetDynamicEntryPosition(int index) const;
82 /** Get a copy of all the DYNAMIC section header entries.
83 Returns an empty vector on error */
84 DynamicEntryList GetDynamicEntries() const;
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;
91 /** Get the SONAME field if any. */
92 bool GetSOName(std::string& soname);
93 StringEntry const* GetSOName();
95 /** Get the RPATH field if any. */
96 StringEntry const* GetRPath();
98 /** Get the RUNPATH field if any. */
99 StringEntry const* GetRunPath();
101 /** Returns true if the ELF file targets a MIPS CPU. */
102 bool IsMIPS() const;
104 /** Print human-readable information about the ELF file. */
105 void PrintInfo(std::ostream& os) const;
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;
111 private:
112 friend class cmELFInternal;
113 bool Valid() const;
114 std::unique_ptr<cmELFInternal> Internal;
115 std::string ErrorMessage;