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