/src/CMake/Source/cmELF.cxx
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 | | #include "cmELF.h" |
4 | | |
5 | | #include <algorithm> |
6 | | #include <cstddef> |
7 | | #include <cstdint> |
8 | | #include <map> |
9 | | #include <memory> |
10 | | #include <sstream> |
11 | | #include <utility> |
12 | | #include <vector> |
13 | | |
14 | | #include <cm/memory> |
15 | | #include <cmext/algorithm> |
16 | | |
17 | | #include <cm3p/kwiml/abi.h> |
18 | | |
19 | | #include "cmsys/FStream.hxx" |
20 | | |
21 | | #include "cmelf/elf32.h" |
22 | | #include "cmelf/elf64.h" |
23 | | #include "cmelf/elf_common.h" |
24 | | |
25 | | // Low-level byte swapping implementation. |
26 | | template <size_t s> |
27 | | struct cmELFByteSwapSize |
28 | | { |
29 | | }; |
30 | | static void cmELFByteSwap(char* data, cmELFByteSwapSize<2> /*unused*/) |
31 | 3.22k | { |
32 | 3.22k | char one_byte; |
33 | 3.22k | one_byte = data[0]; |
34 | 3.22k | data[0] = data[1]; |
35 | 3.22k | data[1] = one_byte; |
36 | 3.22k | } |
37 | | static void cmELFByteSwap(char* data, cmELFByteSwapSize<4> /*unused*/) |
38 | 43.2M | { |
39 | 43.2M | char one_byte; |
40 | 43.2M | one_byte = data[0]; |
41 | 43.2M | data[0] = data[3]; |
42 | 43.2M | data[3] = one_byte; |
43 | 43.2M | one_byte = data[1]; |
44 | 43.2M | data[1] = data[2]; |
45 | 43.2M | data[2] = one_byte; |
46 | 43.2M | } |
47 | | static void cmELFByteSwap(char* data, cmELFByteSwapSize<8> /*unused*/) |
48 | 2.52M | { |
49 | 2.52M | char one_byte; |
50 | 2.52M | one_byte = data[0]; |
51 | 2.52M | data[0] = data[7]; |
52 | 2.52M | data[7] = one_byte; |
53 | 2.52M | one_byte = data[1]; |
54 | 2.52M | data[1] = data[6]; |
55 | 2.52M | data[6] = one_byte; |
56 | 2.52M | one_byte = data[2]; |
57 | 2.52M | data[2] = data[5]; |
58 | 2.52M | data[5] = one_byte; |
59 | 2.52M | one_byte = data[3]; |
60 | 2.52M | data[3] = data[4]; |
61 | 2.52M | data[4] = one_byte; |
62 | 2.52M | } |
63 | | |
64 | | // Low-level byte swapping interface. |
65 | | template <typename T> |
66 | | void cmELFByteSwap(T& x) |
67 | 45.7M | { |
68 | 45.7M | cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>()); |
69 | 45.7M | } void cmELFByteSwap<unsigned short>(unsigned short&) Line | Count | Source | 67 | 3.22k | { | 68 | 3.22k | cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>()); | 69 | 3.22k | } |
void cmELFByteSwap<unsigned int>(unsigned int&) Line | Count | Source | 67 | 43.2M | { | 68 | 43.2M | cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>()); | 69 | 43.2M | } |
void cmELFByteSwap<int>(int&) Line | Count | Source | 67 | 6.87k | { | 68 | 6.87k | cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>()); | 69 | 6.87k | } |
void cmELFByteSwap<unsigned long>(unsigned long&) Line | Count | Source | 67 | 2.52M | { | 68 | 2.52M | cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>()); | 69 | 2.52M | } |
void cmELFByteSwap<long>(long&) Line | Count | Source | 67 | 1 | { | 68 | 1 | cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>()); | 69 | 1 | } |
|
70 | | |
71 | | class cmELFInternal |
72 | | { |
73 | | public: |
74 | | using StringEntry = cmELF::StringEntry; |
75 | | enum ByteOrderType |
76 | | { |
77 | | ByteOrderMSB, |
78 | | ByteOrderLSB |
79 | | }; |
80 | | |
81 | | // Construct and take ownership of the file stream object. |
82 | | cmELFInternal(cmELF* external, std::unique_ptr<std::istream> fin, |
83 | | ByteOrderType order) |
84 | 662 | : External(external) |
85 | 662 | , Stream(std::move(fin)) |
86 | 662 | , ByteOrder(order) |
87 | 662 | { |
88 | | // In most cases the processor-specific byte order will match that |
89 | | // of the target execution environment. If we choose wrong here |
90 | | // it is fixed when the header is read. |
91 | 662 | #if KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_LITTLE |
92 | 662 | this->NeedSwap = (this->ByteOrder == ByteOrderMSB); |
93 | | #elif KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_BIG |
94 | | this->NeedSwap = (this->ByteOrder == ByteOrderLSB); |
95 | | #else |
96 | | this->NeedSwap = false; // Final decision is at runtime anyway. |
97 | | #endif |
98 | | |
99 | | // We have not yet loaded the section info. |
100 | 662 | this->DynamicSectionIndex = -1; |
101 | 662 | } |
102 | | |
103 | | // Destruct and delete the file stream object. |
104 | 662 | virtual ~cmELFInternal() = default; |
105 | | |
106 | | // Forward to the per-class implementation. |
107 | | virtual std::size_t GetNumberOfSections() const = 0; |
108 | | virtual unsigned long GetDynamicEntryPosition(int j) = 0; |
109 | | virtual cmELF::DynamicEntryList GetDynamicEntries() = 0; |
110 | | virtual std::vector<char> EncodeDynamicEntries( |
111 | | cmELF::DynamicEntryList const&) = 0; |
112 | | virtual StringEntry const* GetDynamicSectionString(unsigned int tag) = 0; |
113 | | virtual bool IsMips() const = 0; |
114 | | virtual void PrintInfo(std::ostream& os) const = 0; |
115 | | |
116 | | /** Returns true if the ELF file has a dynamic section **/ |
117 | 3.35k | bool HasDynamicSection() const { return this->DynamicSectionIndex >= 0; } |
118 | | |
119 | | // Lookup the SONAME in the DYNAMIC section. |
120 | | StringEntry const* GetSOName() |
121 | 109 | { |
122 | 109 | return this->GetDynamicSectionString(DT_SONAME); |
123 | 109 | } |
124 | | |
125 | | // Lookup the RPATH in the DYNAMIC section. |
126 | | StringEntry const* GetRPath() |
127 | 198 | { |
128 | 198 | return this->GetDynamicSectionString(DT_RPATH); |
129 | 198 | } |
130 | | |
131 | | // Lookup the RUNPATH in the DYNAMIC section. |
132 | | StringEntry const* GetRunPath() |
133 | 110 | { |
134 | 110 | return this->GetDynamicSectionString(DT_RUNPATH); |
135 | 110 | } |
136 | | |
137 | | // Return the recorded ELF type. |
138 | 9.31k | cmELF::FileType GetFileType() const { return this->ELFType; } |
139 | | |
140 | | // Return the recorded machine. |
141 | 379 | std::uint16_t GetMachine() const { return this->Machine; } |
142 | | |
143 | | protected: |
144 | | // Data common to all ELF class implementations. |
145 | | |
146 | | // The external cmELF object. |
147 | | cmELF* External; |
148 | | |
149 | | // The stream from which to read. |
150 | | std::unique_ptr<std::istream> Stream; |
151 | | |
152 | | // The byte order of the ELF file. |
153 | | ByteOrderType ByteOrder; |
154 | | |
155 | | // The ELF file type. |
156 | | cmELF::FileType ELFType = cmELF::FileTypeInvalid; |
157 | | |
158 | | // The ELF architecture. |
159 | | std::uint16_t Machine; |
160 | | |
161 | | // Whether we need to byte-swap structures read from the stream. |
162 | | bool NeedSwap; |
163 | | |
164 | | // The section header index of the DYNAMIC section (-1 if none). |
165 | | int DynamicSectionIndex; |
166 | | |
167 | | // Helper methods for subclasses. |
168 | | void SetErrorMessage(char const* msg) |
169 | 433 | { |
170 | 433 | this->External->ErrorMessage = msg; |
171 | 433 | this->ELFType = cmELF::FileTypeInvalid; |
172 | 433 | } |
173 | | |
174 | | // Store string table entry states. |
175 | | std::map<unsigned int, StringEntry> DynamicSectionStrings; |
176 | | }; |
177 | | |
178 | | // Configure the implementation template for 32-bit ELF files. |
179 | | struct cmELFTypes32 |
180 | | { |
181 | | using ELF_Ehdr = Elf32_Ehdr; |
182 | | using ELF_Shdr = Elf32_Shdr; |
183 | | using ELF_Dyn = Elf32_Dyn; |
184 | | using ELF_Half = Elf32_Half; |
185 | | using tagtype = ::uint32_t; |
186 | 145 | static char const* GetName() { return "32-bit"; } |
187 | | }; |
188 | | |
189 | | // Configure the implementation template for 64-bit ELF files. |
190 | | struct cmELFTypes64 |
191 | | { |
192 | | using ELF_Ehdr = Elf64_Ehdr; |
193 | | using ELF_Shdr = Elf64_Shdr; |
194 | | using ELF_Dyn = Elf64_Dyn; |
195 | | using ELF_Half = Elf64_Half; |
196 | | using tagtype = ::uint64_t; |
197 | 84 | static char const* GetName() { return "64-bit"; } |
198 | | }; |
199 | | |
200 | | // Parser implementation template. |
201 | | template <class Types> |
202 | | class cmELFInternalImpl : public cmELFInternal |
203 | | { |
204 | | public: |
205 | | // Copy the ELF file format types from our configuration parameter. |
206 | | using ELF_Ehdr = typename Types::ELF_Ehdr; |
207 | | using ELF_Shdr = typename Types::ELF_Shdr; |
208 | | using ELF_Dyn = typename Types::ELF_Dyn; |
209 | | using ELF_Half = typename Types::ELF_Half; |
210 | | using tagtype = typename Types::tagtype; |
211 | | |
212 | | // Construct with a stream and byte swap indicator. |
213 | | cmELFInternalImpl(cmELF* external, std::unique_ptr<std::istream> fin, |
214 | | ByteOrderType order); |
215 | | |
216 | | // Return the number of sections as specified by the ELF header. |
217 | | std::size_t GetNumberOfSections() const override |
218 | 929 | { |
219 | 929 | return static_cast<unsigned int>(this->ELFHeader.e_shnum + |
220 | 929 | this->SectionHeaders[0].sh_size); |
221 | 929 | } cmELFInternalImpl<cmELFTypes32>::GetNumberOfSections() const Line | Count | Source | 218 | 498 | { | 219 | 498 | return static_cast<unsigned int>(this->ELFHeader.e_shnum + | 220 | 498 | this->SectionHeaders[0].sh_size); | 221 | 498 | } |
cmELFInternalImpl<cmELFTypes64>::GetNumberOfSections() const Line | Count | Source | 218 | 431 | { | 219 | 431 | return static_cast<unsigned int>(this->ELFHeader.e_shnum + | 220 | 431 | this->SectionHeaders[0].sh_size); | 221 | 431 | } |
|
222 | | |
223 | | // Get the file position of a dynamic section entry. |
224 | | unsigned long GetDynamicEntryPosition(int j) override; |
225 | | |
226 | | cmELF::DynamicEntryList GetDynamicEntries() override; |
227 | | std::vector<char> EncodeDynamicEntries( |
228 | | cmELF::DynamicEntryList const&) override; |
229 | | |
230 | | // Lookup a string from the dynamic section with the given tag. |
231 | | StringEntry const* GetDynamicSectionString(unsigned int tag) override; |
232 | | |
233 | 379 | bool IsMips() const override { return this->ELFHeader.e_machine == EM_MIPS; }cmELFInternalImpl<cmELFTypes32>::IsMips() const Line | Count | Source | 233 | 200 | bool IsMips() const override { return this->ELFHeader.e_machine == EM_MIPS; } |
cmELFInternalImpl<cmELFTypes64>::IsMips() const Line | Count | Source | 233 | 179 | bool IsMips() const override { return this->ELFHeader.e_machine == EM_MIPS; } |
|
234 | | |
235 | | // Print information about the ELF file. |
236 | | void PrintInfo(std::ostream& os) const override |
237 | 229 | { |
238 | 229 | os << "ELF " << Types::GetName(); |
239 | 229 | if (this->ByteOrder == ByteOrderMSB) { |
240 | 145 | os << " MSB"; |
241 | 145 | } else if (this->ByteOrder == ByteOrderLSB) { |
242 | 84 | os << " LSB"; |
243 | 84 | } |
244 | 229 | switch (this->ELFType) { |
245 | 0 | case cmELF::FileTypeInvalid: |
246 | 0 | os << " invalid file"; |
247 | 0 | break; |
248 | 16 | case cmELF::FileTypeRelocatableObject: |
249 | 16 | os << " relocatable object"; |
250 | 16 | break; |
251 | 71 | case cmELF::FileTypeExecutable: |
252 | 71 | os << " executable"; |
253 | 71 | break; |
254 | 38 | case cmELF::FileTypeSharedLibrary: |
255 | 38 | os << " shared library"; |
256 | 38 | break; |
257 | 53 | case cmELF::FileTypeCore: |
258 | 53 | os << " core file"; |
259 | 53 | break; |
260 | 35 | case cmELF::FileTypeSpecificOS: |
261 | 35 | os << " os-specific type"; |
262 | 35 | break; |
263 | 16 | case cmELF::FileTypeSpecificProc: |
264 | 16 | os << " processor-specific type"; |
265 | 16 | break; |
266 | 229 | } |
267 | 229 | os << "\n"; |
268 | 229 | } cmELFInternalImpl<cmELFTypes32>::PrintInfo(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const Line | Count | Source | 237 | 145 | { | 238 | 145 | os << "ELF " << Types::GetName(); | 239 | 145 | if (this->ByteOrder == ByteOrderMSB) { | 240 | 136 | os << " MSB"; | 241 | 136 | } else if (this->ByteOrder == ByteOrderLSB) { | 242 | 9 | os << " LSB"; | 243 | 9 | } | 244 | 145 | switch (this->ELFType) { | 245 | 0 | case cmELF::FileTypeInvalid: | 246 | 0 | os << " invalid file"; | 247 | 0 | break; | 248 | 9 | case cmELF::FileTypeRelocatableObject: | 249 | 9 | os << " relocatable object"; | 250 | 9 | break; | 251 | 32 | case cmELF::FileTypeExecutable: | 252 | 32 | os << " executable"; | 253 | 32 | break; | 254 | 18 | case cmELF::FileTypeSharedLibrary: | 255 | 18 | os << " shared library"; | 256 | 18 | break; | 257 | 49 | case cmELF::FileTypeCore: | 258 | 49 | os << " core file"; | 259 | 49 | break; | 260 | 30 | case cmELF::FileTypeSpecificOS: | 261 | 30 | os << " os-specific type"; | 262 | 30 | break; | 263 | 7 | case cmELF::FileTypeSpecificProc: | 264 | 7 | os << " processor-specific type"; | 265 | 7 | break; | 266 | 145 | } | 267 | 145 | os << "\n"; | 268 | 145 | } |
cmELFInternalImpl<cmELFTypes64>::PrintInfo(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const Line | Count | Source | 237 | 84 | { | 238 | 84 | os << "ELF " << Types::GetName(); | 239 | 84 | if (this->ByteOrder == ByteOrderMSB) { | 240 | 9 | os << " MSB"; | 241 | 75 | } else if (this->ByteOrder == ByteOrderLSB) { | 242 | 75 | os << " LSB"; | 243 | 75 | } | 244 | 84 | switch (this->ELFType) { | 245 | 0 | case cmELF::FileTypeInvalid: | 246 | 0 | os << " invalid file"; | 247 | 0 | break; | 248 | 7 | case cmELF::FileTypeRelocatableObject: | 249 | 7 | os << " relocatable object"; | 250 | 7 | break; | 251 | 39 | case cmELF::FileTypeExecutable: | 252 | 39 | os << " executable"; | 253 | 39 | break; | 254 | 20 | case cmELF::FileTypeSharedLibrary: | 255 | 20 | os << " shared library"; | 256 | 20 | break; | 257 | 4 | case cmELF::FileTypeCore: | 258 | 4 | os << " core file"; | 259 | 4 | break; | 260 | 5 | case cmELF::FileTypeSpecificOS: | 261 | 5 | os << " os-specific type"; | 262 | 5 | break; | 263 | 9 | case cmELF::FileTypeSpecificProc: | 264 | 9 | os << " processor-specific type"; | 265 | 9 | break; | 266 | 84 | } | 267 | 84 | os << "\n"; | 268 | 84 | } |
|
269 | | |
270 | | private: |
271 | | static_assert(sizeof(ELF_Dyn().d_un.d_val) == sizeof(ELF_Dyn().d_un.d_ptr), |
272 | | "ByteSwap(ELF_Dyn) assumes d_val and d_ptr are the same size"); |
273 | | |
274 | | void ByteSwap(ELF_Ehdr& elf_header) |
275 | 342 | { |
276 | 342 | cmELFByteSwap(elf_header.e_type); |
277 | 342 | cmELFByteSwap(elf_header.e_machine); |
278 | 342 | cmELFByteSwap(elf_header.e_version); |
279 | 342 | cmELFByteSwap(elf_header.e_entry); |
280 | 342 | cmELFByteSwap(elf_header.e_phoff); |
281 | 342 | cmELFByteSwap(elf_header.e_shoff); |
282 | 342 | cmELFByteSwap(elf_header.e_flags); |
283 | 342 | cmELFByteSwap(elf_header.e_ehsize); |
284 | 342 | cmELFByteSwap(elf_header.e_phentsize); |
285 | 342 | cmELFByteSwap(elf_header.e_phnum); |
286 | 342 | cmELFByteSwap(elf_header.e_shentsize); |
287 | 342 | cmELFByteSwap(elf_header.e_shnum); |
288 | 342 | cmELFByteSwap(elf_header.e_shstrndx); |
289 | 342 | } cmELFInternalImpl<cmELFTypes32>::ByteSwap(Elf32_Ehdr&) Line | Count | Source | 275 | 276 | { | 276 | 276 | cmELFByteSwap(elf_header.e_type); | 277 | 276 | cmELFByteSwap(elf_header.e_machine); | 278 | 276 | cmELFByteSwap(elf_header.e_version); | 279 | 276 | cmELFByteSwap(elf_header.e_entry); | 280 | 276 | cmELFByteSwap(elf_header.e_phoff); | 281 | 276 | cmELFByteSwap(elf_header.e_shoff); | 282 | 276 | cmELFByteSwap(elf_header.e_flags); | 283 | 276 | cmELFByteSwap(elf_header.e_ehsize); | 284 | 276 | cmELFByteSwap(elf_header.e_phentsize); | 285 | 276 | cmELFByteSwap(elf_header.e_phnum); | 286 | 276 | cmELFByteSwap(elf_header.e_shentsize); | 287 | 276 | cmELFByteSwap(elf_header.e_shnum); | 288 | 276 | cmELFByteSwap(elf_header.e_shstrndx); | 289 | 276 | } |
cmELFInternalImpl<cmELFTypes64>::ByteSwap(Elf64_Ehdr&) Line | Count | Source | 275 | 66 | { | 276 | 66 | cmELFByteSwap(elf_header.e_type); | 277 | 66 | cmELFByteSwap(elf_header.e_machine); | 278 | 66 | cmELFByteSwap(elf_header.e_version); | 279 | 66 | cmELFByteSwap(elf_header.e_entry); | 280 | 66 | cmELFByteSwap(elf_header.e_phoff); | 281 | 66 | cmELFByteSwap(elf_header.e_shoff); | 282 | 66 | cmELFByteSwap(elf_header.e_flags); | 283 | 66 | cmELFByteSwap(elf_header.e_ehsize); | 284 | 66 | cmELFByteSwap(elf_header.e_phentsize); | 285 | 66 | cmELFByteSwap(elf_header.e_phnum); | 286 | 66 | cmELFByteSwap(elf_header.e_shentsize); | 287 | 66 | cmELFByteSwap(elf_header.e_shnum); | 288 | 66 | cmELFByteSwap(elf_header.e_shstrndx); | 289 | 66 | } |
|
290 | | |
291 | | void ByteSwap(ELF_Shdr& sec_header) |
292 | 4.57M | { |
293 | 4.57M | cmELFByteSwap(sec_header.sh_name); |
294 | 4.57M | cmELFByteSwap(sec_header.sh_type); |
295 | 4.57M | cmELFByteSwap(sec_header.sh_flags); |
296 | 4.57M | cmELFByteSwap(sec_header.sh_addr); |
297 | 4.57M | cmELFByteSwap(sec_header.sh_offset); |
298 | 4.57M | cmELFByteSwap(sec_header.sh_size); |
299 | 4.57M | cmELFByteSwap(sec_header.sh_link); |
300 | 4.57M | cmELFByteSwap(sec_header.sh_info); |
301 | 4.57M | cmELFByteSwap(sec_header.sh_addralign); |
302 | 4.57M | cmELFByteSwap(sec_header.sh_entsize); |
303 | 4.57M | } cmELFInternalImpl<cmELFTypes32>::ByteSwap(Elf32_Shdr&) Line | Count | Source | 292 | 4.15M | { | 293 | 4.15M | cmELFByteSwap(sec_header.sh_name); | 294 | 4.15M | cmELFByteSwap(sec_header.sh_type); | 295 | 4.15M | cmELFByteSwap(sec_header.sh_flags); | 296 | 4.15M | cmELFByteSwap(sec_header.sh_addr); | 297 | 4.15M | cmELFByteSwap(sec_header.sh_offset); | 298 | 4.15M | cmELFByteSwap(sec_header.sh_size); | 299 | 4.15M | cmELFByteSwap(sec_header.sh_link); | 300 | 4.15M | cmELFByteSwap(sec_header.sh_info); | 301 | 4.15M | cmELFByteSwap(sec_header.sh_addralign); | 302 | 4.15M | cmELFByteSwap(sec_header.sh_entsize); | 303 | 4.15M | } |
cmELFInternalImpl<cmELFTypes64>::ByteSwap(Elf64_Shdr&) Line | Count | Source | 292 | 421k | { | 293 | 421k | cmELFByteSwap(sec_header.sh_name); | 294 | 421k | cmELFByteSwap(sec_header.sh_type); | 295 | 421k | cmELFByteSwap(sec_header.sh_flags); | 296 | 421k | cmELFByteSwap(sec_header.sh_addr); | 297 | 421k | cmELFByteSwap(sec_header.sh_offset); | 298 | 421k | cmELFByteSwap(sec_header.sh_size); | 299 | 421k | cmELFByteSwap(sec_header.sh_link); | 300 | 421k | cmELFByteSwap(sec_header.sh_info); | 301 | 421k | cmELFByteSwap(sec_header.sh_addralign); | 302 | 421k | cmELFByteSwap(sec_header.sh_entsize); | 303 | 421k | } |
|
304 | | |
305 | | void ByteSwap(ELF_Dyn& dyn) |
306 | 6.87k | { |
307 | 6.87k | cmELFByteSwap(dyn.d_tag); |
308 | 6.87k | cmELFByteSwap(dyn.d_un.d_val); |
309 | 6.87k | } cmELFInternalImpl<cmELFTypes32>::ByteSwap(Elf32_Dyn&) Line | Count | Source | 306 | 6.87k | { | 307 | 6.87k | cmELFByteSwap(dyn.d_tag); | 308 | 6.87k | cmELFByteSwap(dyn.d_un.d_val); | 309 | 6.87k | } |
cmELFInternalImpl<cmELFTypes64>::ByteSwap(Elf64_Dyn&) Line | Count | Source | 306 | 1 | { | 307 | 1 | cmELFByteSwap(dyn.d_tag); | 308 | 1 | cmELFByteSwap(dyn.d_un.d_val); | 309 | 1 | } |
|
310 | | |
311 | | bool FileTypeValid(ELF_Half et) |
312 | 812 | { |
313 | 812 | unsigned int eti = static_cast<unsigned int>(et); |
314 | 812 | if (eti == ET_NONE || eti == ET_REL || eti == ET_EXEC || eti == ET_DYN || |
315 | 472 | eti == ET_CORE) { |
316 | 438 | return true; |
317 | 438 | } |
318 | 374 | if (eti >= ET_LOOS && eti <= ET_HIOS) { |
319 | 77 | return true; |
320 | 77 | } |
321 | 297 | if (eti >= ET_LOPROC && eti <= ET_HIPROC) { |
322 | 87 | return true; |
323 | 87 | } |
324 | 210 | return false; |
325 | 297 | } cmELFInternalImpl<cmELFTypes32>::FileTypeValid(unsigned short) Line | Count | Source | 312 | 444 | { | 313 | 444 | unsigned int eti = static_cast<unsigned int>(et); | 314 | 444 | if (eti == ET_NONE || eti == ET_REL || eti == ET_EXEC || eti == ET_DYN || | 315 | 324 | eti == ET_CORE) { | 316 | 210 | return true; | 317 | 210 | } | 318 | 234 | if (eti >= ET_LOOS && eti <= ET_HIOS) { | 319 | 65 | return true; | 320 | 65 | } | 321 | 169 | if (eti >= ET_LOPROC && eti <= ET_HIPROC) { | 322 | 54 | return true; | 323 | 54 | } | 324 | 115 | return false; | 325 | 169 | } |
cmELFInternalImpl<cmELFTypes64>::FileTypeValid(unsigned short) Line | Count | Source | 312 | 368 | { | 313 | 368 | unsigned int eti = static_cast<unsigned int>(et); | 314 | 368 | if (eti == ET_NONE || eti == ET_REL || eti == ET_EXEC || eti == ET_DYN || | 315 | 228 | eti == ET_CORE) { | 316 | 228 | return true; | 317 | 228 | } | 318 | 140 | if (eti >= ET_LOOS && eti <= ET_HIOS) { | 319 | 12 | return true; | 320 | 12 | } | 321 | 128 | if (eti >= ET_LOPROC && eti <= ET_HIPROC) { | 322 | 33 | return true; | 323 | 33 | } | 324 | 95 | return false; | 325 | 128 | } |
|
326 | | |
327 | | bool Read(ELF_Ehdr& x) |
328 | 662 | { |
329 | | // Read the header from the file. |
330 | 662 | if (!this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x))) { |
331 | 4 | return false; |
332 | 4 | } |
333 | | |
334 | | // The byte order of ELF header fields may not match that of the |
335 | | // processor-specific data. The header fields are ordered to |
336 | | // match the target execution environment, so we may need to |
337 | | // memorize the order of all platforms based on the e_machine |
338 | | // value. As a heuristic, if the type is invalid but its |
339 | | // swapped value is okay then flip our swap mode. |
340 | 658 | ELF_Half et = x.e_type; |
341 | 658 | if (this->NeedSwap) { |
342 | 332 | cmELFByteSwap(et); |
343 | 332 | } |
344 | 658 | if (!this->FileTypeValid(et)) { |
345 | 154 | cmELFByteSwap(et); |
346 | 154 | if (this->FileTypeValid(et)) { |
347 | | // The previous byte order guess was wrong. Flip it. |
348 | 98 | this->NeedSwap = !this->NeedSwap; |
349 | 98 | } |
350 | 154 | } |
351 | | |
352 | | // Fix the byte order of the header. |
353 | 658 | if (this->NeedSwap) { |
354 | 342 | this->ByteSwap(x); |
355 | 342 | } |
356 | 658 | return true; |
357 | 662 | } cmELFInternalImpl<cmELFTypes32>::Read(Elf32_Ehdr&) Line | Count | Source | 328 | 364 | { | 329 | | // Read the header from the file. | 330 | 364 | if (!this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x))) { | 331 | 2 | return false; | 332 | 2 | } | 333 | | | 334 | | // The byte order of ELF header fields may not match that of the | 335 | | // processor-specific data. The header fields are ordered to | 336 | | // match the target execution environment, so we may need to | 337 | | // memorize the order of all platforms based on the e_machine | 338 | | // value. As a heuristic, if the type is invalid but its | 339 | | // swapped value is okay then flip our swap mode. | 340 | 362 | ELF_Half et = x.e_type; | 341 | 362 | if (this->NeedSwap) { | 342 | 301 | cmELFByteSwap(et); | 343 | 301 | } | 344 | 362 | if (!this->FileTypeValid(et)) { | 345 | 82 | cmELFByteSwap(et); | 346 | 82 | if (this->FileTypeValid(et)) { | 347 | | // The previous byte order guess was wrong. Flip it. | 348 | 49 | this->NeedSwap = !this->NeedSwap; | 349 | 49 | } | 350 | 82 | } | 351 | | | 352 | | // Fix the byte order of the header. | 353 | 362 | if (this->NeedSwap) { | 354 | 276 | this->ByteSwap(x); | 355 | 276 | } | 356 | 362 | return true; | 357 | 364 | } |
cmELFInternalImpl<cmELFTypes64>::Read(Elf64_Ehdr&) Line | Count | Source | 328 | 298 | { | 329 | | // Read the header from the file. | 330 | 298 | if (!this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x))) { | 331 | 2 | return false; | 332 | 2 | } | 333 | | | 334 | | // The byte order of ELF header fields may not match that of the | 335 | | // processor-specific data. The header fields are ordered to | 336 | | // match the target execution environment, so we may need to | 337 | | // memorize the order of all platforms based on the e_machine | 338 | | // value. As a heuristic, if the type is invalid but its | 339 | | // swapped value is okay then flip our swap mode. | 340 | 296 | ELF_Half et = x.e_type; | 341 | 296 | if (this->NeedSwap) { | 342 | 31 | cmELFByteSwap(et); | 343 | 31 | } | 344 | 296 | if (!this->FileTypeValid(et)) { | 345 | 72 | cmELFByteSwap(et); | 346 | 72 | if (this->FileTypeValid(et)) { | 347 | | // The previous byte order guess was wrong. Flip it. | 348 | 49 | this->NeedSwap = !this->NeedSwap; | 349 | 49 | } | 350 | 72 | } | 351 | | | 352 | | // Fix the byte order of the header. | 353 | 296 | if (this->NeedSwap) { | 354 | 66 | this->ByteSwap(x); | 355 | 66 | } | 356 | 296 | return true; | 357 | 298 | } |
|
358 | | bool Read(ELF_Shdr& x) |
359 | 6.32M | { |
360 | 6.32M | if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) && |
361 | 6.32M | this->NeedSwap) { |
362 | 4.57M | this->ByteSwap(x); |
363 | 4.57M | } |
364 | 6.32M | return !this->Stream->fail(); |
365 | 6.32M | } cmELFInternalImpl<cmELFTypes32>::Read(Elf32_Shdr&) Line | Count | Source | 359 | 4.85M | { | 360 | 4.85M | if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) && | 361 | 4.85M | this->NeedSwap) { | 362 | 4.15M | this->ByteSwap(x); | 363 | 4.15M | } | 364 | 4.85M | return !this->Stream->fail(); | 365 | 4.85M | } |
cmELFInternalImpl<cmELFTypes64>::Read(Elf64_Shdr&) Line | Count | Source | 359 | 1.46M | { | 360 | 1.46M | if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) && | 361 | 1.46M | this->NeedSwap) { | 362 | 421k | this->ByteSwap(x); | 363 | 421k | } | 364 | 1.46M | return !this->Stream->fail(); | 365 | 1.46M | } |
|
366 | | bool Read(ELF_Dyn& x) |
367 | 4.42k | { |
368 | 4.42k | if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) && |
369 | 4.34k | this->NeedSwap) { |
370 | 3.87k | this->ByteSwap(x); |
371 | 3.87k | } |
372 | 4.42k | return !this->Stream->fail(); |
373 | 4.42k | } cmELFInternalImpl<cmELFTypes32>::Read(Elf32_Dyn&) Line | Count | Source | 367 | 4.15k | { | 368 | 4.15k | if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) && | 369 | 4.11k | this->NeedSwap) { | 370 | 3.87k | this->ByteSwap(x); | 371 | 3.87k | } | 372 | 4.15k | return !this->Stream->fail(); | 373 | 4.15k | } |
cmELFInternalImpl<cmELFTypes64>::Read(Elf64_Dyn&) Line | Count | Source | 367 | 276 | { | 368 | 276 | if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) && | 369 | 235 | this->NeedSwap) { | 370 | 1 | this->ByteSwap(x); | 371 | 1 | } | 372 | 276 | return !this->Stream->fail(); | 373 | 276 | } |
|
374 | | |
375 | | bool LoadSectionHeader(std::size_t i) |
376 | 6.32M | { |
377 | | // Read the section header from the file. |
378 | 6.32M | this->Stream->seekg(this->ELFHeader.e_shoff + |
379 | 6.32M | this->ELFHeader.e_shentsize * i); |
380 | 6.32M | if (!this->Read(this->SectionHeaders[i])) { |
381 | 221 | this->SetErrorMessage("Failed to load section headers."); |
382 | 221 | return false; |
383 | 221 | } |
384 | | |
385 | | // Identify some important sections. |
386 | 6.32M | if (this->SectionHeaders[i].sh_type == SHT_DYNAMIC) { |
387 | 112k | this->DynamicSectionIndex = static_cast<int>(i); |
388 | 112k | } |
389 | 6.32M | return true; |
390 | 6.32M | } cmELFInternalImpl<cmELFTypes32>::LoadSectionHeader(unsigned long) Line | Count | Source | 376 | 4.85M | { | 377 | | // Read the section header from the file. | 378 | 4.85M | this->Stream->seekg(this->ELFHeader.e_shoff + | 379 | 4.85M | this->ELFHeader.e_shentsize * i); | 380 | 4.85M | if (!this->Read(this->SectionHeaders[i])) { | 381 | 128 | this->SetErrorMessage("Failed to load section headers."); | 382 | 128 | return false; | 383 | 128 | } | 384 | | | 385 | | // Identify some important sections. | 386 | 4.85M | if (this->SectionHeaders[i].sh_type == SHT_DYNAMIC) { | 387 | 111k | this->DynamicSectionIndex = static_cast<int>(i); | 388 | 111k | } | 389 | 4.85M | return true; | 390 | 4.85M | } |
cmELFInternalImpl<cmELFTypes64>::LoadSectionHeader(unsigned long) Line | Count | Source | 376 | 1.46M | { | 377 | | // Read the section header from the file. | 378 | 1.46M | this->Stream->seekg(this->ELFHeader.e_shoff + | 379 | 1.46M | this->ELFHeader.e_shentsize * i); | 380 | 1.46M | if (!this->Read(this->SectionHeaders[i])) { | 381 | 93 | this->SetErrorMessage("Failed to load section headers."); | 382 | 93 | return false; | 383 | 93 | } | 384 | | | 385 | | // Identify some important sections. | 386 | 1.46M | if (this->SectionHeaders[i].sh_type == SHT_DYNAMIC) { | 387 | 353 | this->DynamicSectionIndex = static_cast<int>(i); | 388 | 353 | } | 389 | 1.46M | return true; | 390 | 1.46M | } |
|
391 | | |
392 | | bool LoadDynamicSection(); |
393 | | |
394 | | // Store the main ELF header. |
395 | | ELF_Ehdr ELFHeader; |
396 | | |
397 | | // Store all the section headers. |
398 | | std::vector<ELF_Shdr> SectionHeaders; |
399 | | |
400 | | // Store all entries of the DYNAMIC section. |
401 | | std::vector<ELF_Dyn> DynamicSectionEntries; |
402 | | }; |
403 | | |
404 | | template <class Types> |
405 | | cmELFInternalImpl<Types>::cmELFInternalImpl(cmELF* external, |
406 | | std::unique_ptr<std::istream> fin, |
407 | | ByteOrderType order) |
408 | 662 | : cmELFInternal(external, std::move(fin), order) |
409 | 662 | { |
410 | | // Read the main header. |
411 | 662 | if (!this->Read(this->ELFHeader)) { |
412 | 4 | this->SetErrorMessage("Failed to read main ELF header."); |
413 | 4 | return; |
414 | 4 | } |
415 | | |
416 | | // Determine the ELF file type. |
417 | 658 | switch (this->ELFHeader.e_type) { |
418 | 2 | case ET_NONE: |
419 | 2 | this->SetErrorMessage("ELF file type is NONE."); |
420 | 2 | return; |
421 | 34 | case ET_REL: |
422 | 34 | this->ELFType = cmELF::FileTypeRelocatableObject; |
423 | 34 | break; |
424 | 225 | case ET_EXEC: |
425 | 225 | this->ELFType = cmELF::FileTypeExecutable; |
426 | 225 | break; |
427 | 79 | case ET_DYN: |
428 | 79 | this->ELFType = cmELF::FileTypeSharedLibrary; |
429 | 79 | break; |
430 | 98 | case ET_CORE: |
431 | 98 | this->ELFType = cmELF::FileTypeCore; |
432 | 98 | break; |
433 | 220 | default: { |
434 | 220 | unsigned int eti = static_cast<unsigned int>(this->ELFHeader.e_type); |
435 | 220 | if (eti >= ET_LOOS && eti <= ET_HIOS) { |
436 | 77 | this->ELFType = cmELF::FileTypeSpecificOS; |
437 | 77 | break; |
438 | 77 | } |
439 | 143 | if (eti >= ET_LOPROC && eti <= ET_HIPROC) { |
440 | 87 | this->ELFType = cmELF::FileTypeSpecificProc; |
441 | 87 | break; |
442 | 87 | } |
443 | 56 | std::ostringstream e; |
444 | 56 | e << "Unknown ELF file type " << eti; |
445 | 56 | this->SetErrorMessage(e.str().c_str()); |
446 | 56 | return; |
447 | 143 | } |
448 | 658 | } |
449 | | |
450 | 600 | this->Machine = this->ELFHeader.e_machine; |
451 | | |
452 | | // Load the section headers. |
453 | 600 | std::size_t const minSections = 1; |
454 | 600 | std::size_t numSections = this->ELFHeader.e_shnum; |
455 | 600 | this->SectionHeaders.resize(std::max(numSections, minSections)); |
456 | 600 | if (!this->LoadSectionHeader(0)) { |
457 | 50 | return; |
458 | 50 | } |
459 | 550 | numSections = this->GetNumberOfSections(); |
460 | 550 | this->SectionHeaders.resize(std::max(numSections, minSections)); |
461 | 6.32M | for (std::size_t i = 1; i < numSections; ++i) { |
462 | 6.32M | if (!this->LoadSectionHeader(i)) { |
463 | 171 | return; |
464 | 171 | } |
465 | 6.32M | } |
466 | 550 | } cmELFInternalImpl<cmELFTypes32>::cmELFInternalImpl(cmELF*, std::__1::unique_ptr<std::__1::basic_istream<char, std::__1::char_traits<char> >, std::__1::default_delete<std::__1::basic_istream<char, std::__1::char_traits<char> > > >, cmELFInternal::ByteOrderType) Line | Count | Source | 408 | 364 | : cmELFInternal(external, std::move(fin), order) | 409 | 364 | { | 410 | | // Read the main header. | 411 | 364 | if (!this->Read(this->ELFHeader)) { | 412 | 2 | this->SetErrorMessage("Failed to read main ELF header."); | 413 | 2 | return; | 414 | 2 | } | 415 | | | 416 | | // Determine the ELF file type. | 417 | 362 | switch (this->ELFHeader.e_type) { | 418 | 1 | case ET_NONE: | 419 | 1 | this->SetErrorMessage("ELF file type is NONE."); | 420 | 1 | return; | 421 | 16 | case ET_REL: | 422 | 16 | this->ELFType = cmELF::FileTypeRelocatableObject; | 423 | 16 | break; | 424 | 70 | case ET_EXEC: | 425 | 70 | this->ELFType = cmELF::FileTypeExecutable; | 426 | 70 | break; | 427 | 33 | case ET_DYN: | 428 | 33 | this->ELFType = cmELF::FileTypeSharedLibrary; | 429 | 33 | break; | 430 | 90 | case ET_CORE: | 431 | 90 | this->ELFType = cmELF::FileTypeCore; | 432 | 90 | break; | 433 | 152 | default: { | 434 | 152 | unsigned int eti = static_cast<unsigned int>(this->ELFHeader.e_type); | 435 | 152 | if (eti >= ET_LOOS && eti <= ET_HIOS) { | 436 | 65 | this->ELFType = cmELF::FileTypeSpecificOS; | 437 | 65 | break; | 438 | 65 | } | 439 | 87 | if (eti >= ET_LOPROC && eti <= ET_HIPROC) { | 440 | 54 | this->ELFType = cmELF::FileTypeSpecificProc; | 441 | 54 | break; | 442 | 54 | } | 443 | 33 | std::ostringstream e; | 444 | 33 | e << "Unknown ELF file type " << eti; | 445 | 33 | this->SetErrorMessage(e.str().c_str()); | 446 | 33 | return; | 447 | 87 | } | 448 | 362 | } | 449 | | | 450 | 328 | this->Machine = this->ELFHeader.e_machine; | 451 | | | 452 | | // Load the section headers. | 453 | 328 | std::size_t const minSections = 1; | 454 | 328 | std::size_t numSections = this->ELFHeader.e_shnum; | 455 | 328 | this->SectionHeaders.resize(std::max(numSections, minSections)); | 456 | 328 | if (!this->LoadSectionHeader(0)) { | 457 | 30 | return; | 458 | 30 | } | 459 | 298 | numSections = this->GetNumberOfSections(); | 460 | 298 | this->SectionHeaders.resize(std::max(numSections, minSections)); | 461 | 4.85M | for (std::size_t i = 1; i < numSections; ++i) { | 462 | 4.85M | if (!this->LoadSectionHeader(i)) { | 463 | 98 | return; | 464 | 98 | } | 465 | 4.85M | } | 466 | 298 | } |
cmELFInternalImpl<cmELFTypes64>::cmELFInternalImpl(cmELF*, std::__1::unique_ptr<std::__1::basic_istream<char, std::__1::char_traits<char> >, std::__1::default_delete<std::__1::basic_istream<char, std::__1::char_traits<char> > > >, cmELFInternal::ByteOrderType) Line | Count | Source | 408 | 298 | : cmELFInternal(external, std::move(fin), order) | 409 | 298 | { | 410 | | // Read the main header. | 411 | 298 | if (!this->Read(this->ELFHeader)) { | 412 | 2 | this->SetErrorMessage("Failed to read main ELF header."); | 413 | 2 | return; | 414 | 2 | } | 415 | | | 416 | | // Determine the ELF file type. | 417 | 296 | switch (this->ELFHeader.e_type) { | 418 | 1 | case ET_NONE: | 419 | 1 | this->SetErrorMessage("ELF file type is NONE."); | 420 | 1 | return; | 421 | 18 | case ET_REL: | 422 | 18 | this->ELFType = cmELF::FileTypeRelocatableObject; | 423 | 18 | break; | 424 | 155 | case ET_EXEC: | 425 | 155 | this->ELFType = cmELF::FileTypeExecutable; | 426 | 155 | break; | 427 | 46 | case ET_DYN: | 428 | 46 | this->ELFType = cmELF::FileTypeSharedLibrary; | 429 | 46 | break; | 430 | 8 | case ET_CORE: | 431 | 8 | this->ELFType = cmELF::FileTypeCore; | 432 | 8 | break; | 433 | 68 | default: { | 434 | 68 | unsigned int eti = static_cast<unsigned int>(this->ELFHeader.e_type); | 435 | 68 | if (eti >= ET_LOOS && eti <= ET_HIOS) { | 436 | 12 | this->ELFType = cmELF::FileTypeSpecificOS; | 437 | 12 | break; | 438 | 12 | } | 439 | 56 | if (eti >= ET_LOPROC && eti <= ET_HIPROC) { | 440 | 33 | this->ELFType = cmELF::FileTypeSpecificProc; | 441 | 33 | break; | 442 | 33 | } | 443 | 23 | std::ostringstream e; | 444 | 23 | e << "Unknown ELF file type " << eti; | 445 | 23 | this->SetErrorMessage(e.str().c_str()); | 446 | 23 | return; | 447 | 56 | } | 448 | 296 | } | 449 | | | 450 | 272 | this->Machine = this->ELFHeader.e_machine; | 451 | | | 452 | | // Load the section headers. | 453 | 272 | std::size_t const minSections = 1; | 454 | 272 | std::size_t numSections = this->ELFHeader.e_shnum; | 455 | 272 | this->SectionHeaders.resize(std::max(numSections, minSections)); | 456 | 272 | if (!this->LoadSectionHeader(0)) { | 457 | 20 | return; | 458 | 20 | } | 459 | 252 | numSections = this->GetNumberOfSections(); | 460 | 252 | this->SectionHeaders.resize(std::max(numSections, minSections)); | 461 | 1.46M | for (std::size_t i = 1; i < numSections; ++i) { | 462 | 1.46M | if (!this->LoadSectionHeader(i)) { | 463 | 73 | return; | 464 | 73 | } | 465 | 1.46M | } | 466 | 252 | } |
|
467 | | |
468 | | template <class Types> |
469 | | bool cmELFInternalImpl<Types>::LoadDynamicSection() |
470 | 2.97k | { |
471 | | // If there is no dynamic section we are done. |
472 | 2.97k | if (!this->HasDynamicSection()) { |
473 | 155 | return false; |
474 | 155 | } |
475 | | |
476 | | // If the section was already loaded we are done. |
477 | 2.81k | if (!this->DynamicSectionEntries.empty()) { |
478 | 2.44k | return true; |
479 | 2.44k | } |
480 | | |
481 | | // If there are no entries we are done. |
482 | 369 | ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex]; |
483 | 369 | if (sec.sh_entsize == 0) { |
484 | 17 | return false; |
485 | 17 | } |
486 | | |
487 | | // Allocate the dynamic section entries. |
488 | 352 | int n = static_cast<int>(sec.sh_size / sec.sh_entsize); |
489 | 352 | this->DynamicSectionEntries.resize(n); |
490 | | |
491 | | // Read each entry. |
492 | 4.69k | for (int j = 0; j < n; ++j) { |
493 | | // Seek to the beginning of the section entry. |
494 | 4.42k | this->Stream->seekg(sec.sh_offset + sec.sh_entsize * j); |
495 | 4.42k | ELF_Dyn& dyn = this->DynamicSectionEntries[j]; |
496 | | |
497 | | // Try reading the entry. |
498 | 4.42k | if (!this->Read(dyn)) { |
499 | 79 | this->SetErrorMessage("Error reading entry from DYNAMIC section."); |
500 | 79 | this->DynamicSectionIndex = -1; |
501 | 79 | return false; |
502 | 79 | } |
503 | 4.42k | } |
504 | 273 | return true; |
505 | 352 | } cmELFInternalImpl<cmELFTypes32>::LoadDynamicSection() Line | Count | Source | 470 | 2.64k | { | 471 | | // If there is no dynamic section we are done. | 472 | 2.64k | if (!this->HasDynamicSection()) { | 473 | 78 | return false; | 474 | 78 | } | 475 | | | 476 | | // If the section was already loaded we are done. | 477 | 2.56k | if (!this->DynamicSectionEntries.empty()) { | 478 | 2.39k | return true; | 479 | 2.39k | } | 480 | | | 481 | | // If there are no entries we are done. | 482 | 166 | ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex]; | 483 | 166 | if (sec.sh_entsize == 0) { | 484 | 8 | return false; | 485 | 8 | } | 486 | | | 487 | | // Allocate the dynamic section entries. | 488 | 158 | int n = static_cast<int>(sec.sh_size / sec.sh_entsize); | 489 | 158 | this->DynamicSectionEntries.resize(n); | 490 | | | 491 | | // Read each entry. | 492 | 4.27k | for (int j = 0; j < n; ++j) { | 493 | | // Seek to the beginning of the section entry. | 494 | 4.15k | this->Stream->seekg(sec.sh_offset + sec.sh_entsize * j); | 495 | 4.15k | ELF_Dyn& dyn = this->DynamicSectionEntries[j]; | 496 | | | 497 | | // Try reading the entry. | 498 | 4.15k | if (!this->Read(dyn)) { | 499 | 38 | this->SetErrorMessage("Error reading entry from DYNAMIC section."); | 500 | 38 | this->DynamicSectionIndex = -1; | 501 | 38 | return false; | 502 | 38 | } | 503 | 4.15k | } | 504 | 120 | return true; | 505 | 158 | } |
cmELFInternalImpl<cmELFTypes64>::LoadDynamicSection() Line | Count | Source | 470 | 333 | { | 471 | | // If there is no dynamic section we are done. | 472 | 333 | if (!this->HasDynamicSection()) { | 473 | 77 | return false; | 474 | 77 | } | 475 | | | 476 | | // If the section was already loaded we are done. | 477 | 256 | if (!this->DynamicSectionEntries.empty()) { | 478 | 53 | return true; | 479 | 53 | } | 480 | | | 481 | | // If there are no entries we are done. | 482 | 203 | ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex]; | 483 | 203 | if (sec.sh_entsize == 0) { | 484 | 9 | return false; | 485 | 9 | } | 486 | | | 487 | | // Allocate the dynamic section entries. | 488 | 194 | int n = static_cast<int>(sec.sh_size / sec.sh_entsize); | 489 | 194 | this->DynamicSectionEntries.resize(n); | 490 | | | 491 | | // Read each entry. | 492 | 429 | for (int j = 0; j < n; ++j) { | 493 | | // Seek to the beginning of the section entry. | 494 | 276 | this->Stream->seekg(sec.sh_offset + sec.sh_entsize * j); | 495 | 276 | ELF_Dyn& dyn = this->DynamicSectionEntries[j]; | 496 | | | 497 | | // Try reading the entry. | 498 | 276 | if (!this->Read(dyn)) { | 499 | 41 | this->SetErrorMessage("Error reading entry from DYNAMIC section."); | 500 | 41 | this->DynamicSectionIndex = -1; | 501 | 41 | return false; | 502 | 41 | } | 503 | 276 | } | 504 | 153 | return true; | 505 | 194 | } |
|
506 | | |
507 | | template <class Types> |
508 | | unsigned long cmELFInternalImpl<Types>::GetDynamicEntryPosition(int j) |
509 | 2.33k | { |
510 | 2.33k | if (!this->LoadDynamicSection()) { |
511 | 0 | return 0; |
512 | 0 | } |
513 | 2.33k | if (j < 0 || j >= static_cast<int>(this->DynamicSectionEntries.size())) { |
514 | 0 | return 0; |
515 | 0 | } |
516 | 2.33k | ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex]; |
517 | 2.33k | return sec.sh_offset + sec.sh_entsize * static_cast<unsigned long>(j); |
518 | 2.33k | } cmELFInternalImpl<cmELFTypes32>::GetDynamicEntryPosition(int) Line | Count | Source | 509 | 2.31k | { | 510 | 2.31k | if (!this->LoadDynamicSection()) { | 511 | 0 | return 0; | 512 | 0 | } | 513 | 2.31k | if (j < 0 || j >= static_cast<int>(this->DynamicSectionEntries.size())) { | 514 | 0 | return 0; | 515 | 0 | } | 516 | 2.31k | ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex]; | 517 | 2.31k | return sec.sh_offset + sec.sh_entsize * static_cast<unsigned long>(j); | 518 | 2.31k | } |
cmELFInternalImpl<cmELFTypes64>::GetDynamicEntryPosition(int) Line | Count | Source | 509 | 16 | { | 510 | 16 | if (!this->LoadDynamicSection()) { | 511 | 0 | return 0; | 512 | 0 | } | 513 | 16 | if (j < 0 || j >= static_cast<int>(this->DynamicSectionEntries.size())) { | 514 | 0 | return 0; | 515 | 0 | } | 516 | 16 | ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex]; | 517 | 16 | return sec.sh_offset + sec.sh_entsize * static_cast<unsigned long>(j); | 518 | 16 | } |
|
519 | | |
520 | | template <class Types> |
521 | | cmELF::DynamicEntryList cmELFInternalImpl<Types>::GetDynamicEntries() |
522 | 261 | { |
523 | 261 | cmELF::DynamicEntryList result; |
524 | | |
525 | | // Ensure entries have been read from file |
526 | 261 | if (!this->LoadDynamicSection()) { |
527 | 129 | return result; |
528 | 129 | } |
529 | | |
530 | | // Copy into public array |
531 | 132 | result.reserve(this->DynamicSectionEntries.size()); |
532 | 3.01k | for (ELF_Dyn& dyn : this->DynamicSectionEntries) { |
533 | 3.01k | result.emplace_back(dyn.d_tag, dyn.d_un.d_val); |
534 | 3.01k | } |
535 | | |
536 | 132 | return result; |
537 | 261 | } cmELFInternalImpl<cmELFTypes32>::GetDynamicEntries() Line | Count | Source | 522 | 177 | { | 523 | 177 | cmELF::DynamicEntryList result; | 524 | | | 525 | | // Ensure entries have been read from file | 526 | 177 | if (!this->LoadDynamicSection()) { | 527 | 86 | return result; | 528 | 86 | } | 529 | | | 530 | | // Copy into public array | 531 | 91 | result.reserve(this->DynamicSectionEntries.size()); | 532 | 2.99k | for (ELF_Dyn& dyn : this->DynamicSectionEntries) { | 533 | 2.99k | result.emplace_back(dyn.d_tag, dyn.d_un.d_val); | 534 | 2.99k | } | 535 | | | 536 | 91 | return result; | 537 | 177 | } |
cmELFInternalImpl<cmELFTypes64>::GetDynamicEntries() Line | Count | Source | 522 | 84 | { | 523 | 84 | cmELF::DynamicEntryList result; | 524 | | | 525 | | // Ensure entries have been read from file | 526 | 84 | if (!this->LoadDynamicSection()) { | 527 | 43 | return result; | 528 | 43 | } | 529 | | | 530 | | // Copy into public array | 531 | 41 | result.reserve(this->DynamicSectionEntries.size()); | 532 | 41 | for (ELF_Dyn& dyn : this->DynamicSectionEntries) { | 533 | 16 | result.emplace_back(dyn.d_tag, dyn.d_un.d_val); | 534 | 16 | } | 535 | | | 536 | 41 | return result; | 537 | 84 | } |
|
538 | | |
539 | | template <class Types> |
540 | | std::vector<char> cmELFInternalImpl<Types>::EncodeDynamicEntries( |
541 | | cmELF::DynamicEntryList const& entries) |
542 | 47 | { |
543 | 47 | std::vector<char> result; |
544 | 47 | result.reserve(sizeof(ELF_Dyn) * entries.size()); |
545 | | |
546 | 3.01k | for (auto const& entry : entries) { |
547 | | // Store the entry in an ELF_Dyn, byteswap it, then serialize to chars |
548 | 3.01k | ELF_Dyn dyn; |
549 | 3.01k | dyn.d_tag = static_cast<tagtype>(entry.first); |
550 | 3.01k | dyn.d_un.d_val = static_cast<tagtype>(entry.second); |
551 | | |
552 | 3.01k | if (this->NeedSwap) { |
553 | 2.99k | this->ByteSwap(dyn); |
554 | 2.99k | } |
555 | | |
556 | 3.01k | char* pdyn = reinterpret_cast<char*>(&dyn); |
557 | 3.01k | cm::append(result, pdyn, pdyn + sizeof(ELF_Dyn)); |
558 | 3.01k | } |
559 | | |
560 | 47 | return result; |
561 | 47 | } cmELFInternalImpl<cmELFTypes32>::EncodeDynamicEntries(std::__1::vector<std::__1::pair<long, unsigned long>, std::__1::allocator<std::__1::pair<long, unsigned long> > > const&) Line | Count | Source | 542 | 32 | { | 543 | 32 | std::vector<char> result; | 544 | 32 | result.reserve(sizeof(ELF_Dyn) * entries.size()); | 545 | | | 546 | 2.99k | for (auto const& entry : entries) { | 547 | | // Store the entry in an ELF_Dyn, byteswap it, then serialize to chars | 548 | 2.99k | ELF_Dyn dyn; | 549 | 2.99k | dyn.d_tag = static_cast<tagtype>(entry.first); | 550 | 2.99k | dyn.d_un.d_val = static_cast<tagtype>(entry.second); | 551 | | | 552 | 2.99k | if (this->NeedSwap) { | 553 | 2.99k | this->ByteSwap(dyn); | 554 | 2.99k | } | 555 | | | 556 | 2.99k | char* pdyn = reinterpret_cast<char*>(&dyn); | 557 | 2.99k | cm::append(result, pdyn, pdyn + sizeof(ELF_Dyn)); | 558 | 2.99k | } | 559 | | | 560 | 32 | return result; | 561 | 32 | } |
cmELFInternalImpl<cmELFTypes64>::EncodeDynamicEntries(std::__1::vector<std::__1::pair<long, unsigned long>, std::__1::allocator<std::__1::pair<long, unsigned long> > > const&) Line | Count | Source | 542 | 15 | { | 543 | 15 | std::vector<char> result; | 544 | 15 | result.reserve(sizeof(ELF_Dyn) * entries.size()); | 545 | | | 546 | 16 | for (auto const& entry : entries) { | 547 | | // Store the entry in an ELF_Dyn, byteswap it, then serialize to chars | 548 | 16 | ELF_Dyn dyn; | 549 | 16 | dyn.d_tag = static_cast<tagtype>(entry.first); | 550 | 16 | dyn.d_un.d_val = static_cast<tagtype>(entry.second); | 551 | | | 552 | 16 | if (this->NeedSwap) { | 553 | 0 | this->ByteSwap(dyn); | 554 | 0 | } | 555 | | | 556 | 16 | char* pdyn = reinterpret_cast<char*>(&dyn); | 557 | 16 | cm::append(result, pdyn, pdyn + sizeof(ELF_Dyn)); | 558 | 16 | } | 559 | | | 560 | 15 | return result; | 561 | 15 | } |
|
562 | | |
563 | | template <class Types> |
564 | | cmELF::StringEntry const* cmELFInternalImpl<Types>::GetDynamicSectionString( |
565 | | unsigned int tag) |
566 | 417 | { |
567 | | // Short-circuit if already checked. |
568 | 417 | auto dssi = this->DynamicSectionStrings.find(tag); |
569 | 417 | if (dssi != this->DynamicSectionStrings.end()) { |
570 | 40 | if (dssi->second.Position > 0) { |
571 | 9 | return &dssi->second; |
572 | 9 | } |
573 | 31 | return nullptr; |
574 | 40 | } |
575 | | |
576 | | // Create an entry for this tag. Assume it is missing until found. |
577 | 377 | StringEntry& se = this->DynamicSectionStrings[tag]; |
578 | 377 | se.Position = 0; |
579 | 377 | se.Size = 0; |
580 | 377 | se.IndexInSection = -1; |
581 | | |
582 | | // Try reading the dynamic section. |
583 | 377 | if (!this->LoadDynamicSection()) { |
584 | 122 | return nullptr; |
585 | 122 | } |
586 | | |
587 | | // Get the string table referenced by the DYNAMIC section. |
588 | 255 | ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex]; |
589 | 255 | if (sec.sh_link >= this->SectionHeaders.size()) { |
590 | 66 | this->SetErrorMessage("Section DYNAMIC has invalid string table index."); |
591 | 66 | return nullptr; |
592 | 66 | } |
593 | 189 | ELF_Shdr const& strtab = this->SectionHeaders[sec.sh_link]; |
594 | | |
595 | | // Look for the requested entry. |
596 | 189 | for (auto di = this->DynamicSectionEntries.begin(); |
597 | 5.82k | di != this->DynamicSectionEntries.end(); ++di) { |
598 | 5.65k | ELF_Dyn& dyn = *di; |
599 | 5.65k | if (static_cast<tagtype>(dyn.d_tag) == static_cast<tagtype>(tag)) { |
600 | | // We found the tag requested. |
601 | | // Make sure the position given is within the string section. |
602 | 24 | if (dyn.d_un.d_val >= strtab.sh_size) { |
603 | 2 | this->SetErrorMessage("Section DYNAMIC references string beyond " |
604 | 2 | "the end of its string section."); |
605 | 2 | return nullptr; |
606 | 2 | } |
607 | | |
608 | | // Seek to the position reported by the entry. |
609 | 22 | unsigned long first = static_cast<unsigned long>(dyn.d_un.d_val); |
610 | 22 | unsigned long last = first; |
611 | 22 | unsigned long end = static_cast<unsigned long>(strtab.sh_size); |
612 | 22 | this->Stream->seekg(strtab.sh_offset + first); |
613 | | |
614 | | // Read the string. It may be followed by more than one NULL |
615 | | // terminator. Count the total size of the region allocated to |
616 | | // the string. This assumes that the next string in the table |
617 | | // is non-empty, but the "chrpath" tool makes the same |
618 | | // assumption. |
619 | 22 | bool terminated = false; |
620 | 22 | char c; |
621 | 152 | while (last != end && this->Stream->get(c) && !(terminated && c)) { |
622 | 130 | ++last; |
623 | 130 | if (c) { |
624 | 58 | se.Value += c; |
625 | 72 | } else { |
626 | 72 | terminated = true; |
627 | 72 | } |
628 | 130 | } |
629 | | |
630 | | // Make sure the whole value was read. |
631 | 22 | if (!(*this->Stream)) { |
632 | 3 | if (tag == cmELF::TagRPath) { |
633 | 1 | this->SetErrorMessage( |
634 | 1 | "Dynamic section specifies unreadable DT_RPATH"); |
635 | 2 | } else if (tag == cmELF::TagRunPath) { |
636 | 1 | this->SetErrorMessage( |
637 | 1 | "Dynamic section specifies unreadable DT_RUNPATH"); |
638 | 1 | } else if (tag == cmELF::TagMipsRldMapRel) { |
639 | 0 | this->SetErrorMessage( |
640 | 0 | "Dynamic section specifies unreadable DT_MIPS_RLD_MAP_REL"); |
641 | 1 | } else { |
642 | 1 | this->SetErrorMessage("Dynamic section specifies unreadable value" |
643 | 1 | " for unexpected attribute"); |
644 | 1 | } |
645 | 3 | se.Value = ""; |
646 | 3 | return nullptr; |
647 | 3 | } |
648 | | |
649 | | // The value has been read successfully. Report it. |
650 | 19 | se.Position = static_cast<unsigned long>(strtab.sh_offset + first); |
651 | 19 | se.Size = last - first; |
652 | 19 | se.IndexInSection = |
653 | 19 | static_cast<int>(di - this->DynamicSectionEntries.begin()); |
654 | 19 | return &se; |
655 | 22 | } |
656 | 5.65k | } |
657 | 165 | return nullptr; |
658 | 189 | } cmELFInternalImpl<cmELFTypes32>::GetDynamicSectionString(unsigned int) Line | Count | Source | 566 | 164 | { | 567 | | // Short-circuit if already checked. | 568 | 164 | auto dssi = this->DynamicSectionStrings.find(tag); | 569 | 164 | if (dssi != this->DynamicSectionStrings.end()) { | 570 | 20 | if (dssi->second.Position > 0) { | 571 | 9 | return &dssi->second; | 572 | 9 | } | 573 | 11 | return nullptr; | 574 | 20 | } | 575 | | | 576 | | // Create an entry for this tag. Assume it is missing until found. | 577 | 144 | StringEntry& se = this->DynamicSectionStrings[tag]; | 578 | 144 | se.Position = 0; | 579 | 144 | se.Size = 0; | 580 | 144 | se.IndexInSection = -1; | 581 | | | 582 | | // Try reading the dynamic section. | 583 | 144 | if (!this->LoadDynamicSection()) { | 584 | 38 | return nullptr; | 585 | 38 | } | 586 | | | 587 | | // Get the string table referenced by the DYNAMIC section. | 588 | 106 | ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex]; | 589 | 106 | if (sec.sh_link >= this->SectionHeaders.size()) { | 590 | 12 | this->SetErrorMessage("Section DYNAMIC has invalid string table index."); | 591 | 12 | return nullptr; | 592 | 12 | } | 593 | 94 | ELF_Shdr const& strtab = this->SectionHeaders[sec.sh_link]; | 594 | | | 595 | | // Look for the requested entry. | 596 | 94 | for (auto di = this->DynamicSectionEntries.begin(); | 597 | 5.68k | di != this->DynamicSectionEntries.end(); ++di) { | 598 | 5.61k | ELF_Dyn& dyn = *di; | 599 | 5.61k | if (static_cast<tagtype>(dyn.d_tag) == static_cast<tagtype>(tag)) { | 600 | | // We found the tag requested. | 601 | | // Make sure the position given is within the string section. | 602 | 24 | if (dyn.d_un.d_val >= strtab.sh_size) { | 603 | 2 | this->SetErrorMessage("Section DYNAMIC references string beyond " | 604 | 2 | "the end of its string section."); | 605 | 2 | return nullptr; | 606 | 2 | } | 607 | | | 608 | | // Seek to the position reported by the entry. | 609 | 22 | unsigned long first = static_cast<unsigned long>(dyn.d_un.d_val); | 610 | 22 | unsigned long last = first; | 611 | 22 | unsigned long end = static_cast<unsigned long>(strtab.sh_size); | 612 | 22 | this->Stream->seekg(strtab.sh_offset + first); | 613 | | | 614 | | // Read the string. It may be followed by more than one NULL | 615 | | // terminator. Count the total size of the region allocated to | 616 | | // the string. This assumes that the next string in the table | 617 | | // is non-empty, but the "chrpath" tool makes the same | 618 | | // assumption. | 619 | 22 | bool terminated = false; | 620 | 22 | char c; | 621 | 152 | while (last != end && this->Stream->get(c) && !(terminated && c)) { | 622 | 130 | ++last; | 623 | 130 | if (c) { | 624 | 58 | se.Value += c; | 625 | 72 | } else { | 626 | 72 | terminated = true; | 627 | 72 | } | 628 | 130 | } | 629 | | | 630 | | // Make sure the whole value was read. | 631 | 22 | if (!(*this->Stream)) { | 632 | 3 | if (tag == cmELF::TagRPath) { | 633 | 1 | this->SetErrorMessage( | 634 | 1 | "Dynamic section specifies unreadable DT_RPATH"); | 635 | 2 | } else if (tag == cmELF::TagRunPath) { | 636 | 1 | this->SetErrorMessage( | 637 | 1 | "Dynamic section specifies unreadable DT_RUNPATH"); | 638 | 1 | } else if (tag == cmELF::TagMipsRldMapRel) { | 639 | 0 | this->SetErrorMessage( | 640 | 0 | "Dynamic section specifies unreadable DT_MIPS_RLD_MAP_REL"); | 641 | 1 | } else { | 642 | 1 | this->SetErrorMessage("Dynamic section specifies unreadable value" | 643 | 1 | " for unexpected attribute"); | 644 | 1 | } | 645 | 3 | se.Value = ""; | 646 | 3 | return nullptr; | 647 | 3 | } | 648 | | | 649 | | // The value has been read successfully. Report it. | 650 | 19 | se.Position = static_cast<unsigned long>(strtab.sh_offset + first); | 651 | 19 | se.Size = last - first; | 652 | 19 | se.IndexInSection = | 653 | 19 | static_cast<int>(di - this->DynamicSectionEntries.begin()); | 654 | 19 | return &se; | 655 | 22 | } | 656 | 5.61k | } | 657 | 70 | return nullptr; | 658 | 94 | } |
cmELFInternalImpl<cmELFTypes64>::GetDynamicSectionString(unsigned int) Line | Count | Source | 566 | 253 | { | 567 | | // Short-circuit if already checked. | 568 | 253 | auto dssi = this->DynamicSectionStrings.find(tag); | 569 | 253 | if (dssi != this->DynamicSectionStrings.end()) { | 570 | 20 | if (dssi->second.Position > 0) { | 571 | 0 | return &dssi->second; | 572 | 0 | } | 573 | 20 | return nullptr; | 574 | 20 | } | 575 | | | 576 | | // Create an entry for this tag. Assume it is missing until found. | 577 | 233 | StringEntry& se = this->DynamicSectionStrings[tag]; | 578 | 233 | se.Position = 0; | 579 | 233 | se.Size = 0; | 580 | 233 | se.IndexInSection = -1; | 581 | | | 582 | | // Try reading the dynamic section. | 583 | 233 | if (!this->LoadDynamicSection()) { | 584 | 84 | return nullptr; | 585 | 84 | } | 586 | | | 587 | | // Get the string table referenced by the DYNAMIC section. | 588 | 149 | ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex]; | 589 | 149 | if (sec.sh_link >= this->SectionHeaders.size()) { | 590 | 54 | this->SetErrorMessage("Section DYNAMIC has invalid string table index."); | 591 | 54 | return nullptr; | 592 | 54 | } | 593 | 95 | ELF_Shdr const& strtab = this->SectionHeaders[sec.sh_link]; | 594 | | | 595 | | // Look for the requested entry. | 596 | 95 | for (auto di = this->DynamicSectionEntries.begin(); | 597 | 134 | di != this->DynamicSectionEntries.end(); ++di) { | 598 | 39 | ELF_Dyn& dyn = *di; | 599 | 39 | if (static_cast<tagtype>(dyn.d_tag) == static_cast<tagtype>(tag)) { | 600 | | // We found the tag requested. | 601 | | // Make sure the position given is within the string section. | 602 | 0 | if (dyn.d_un.d_val >= strtab.sh_size) { | 603 | 0 | this->SetErrorMessage("Section DYNAMIC references string beyond " | 604 | 0 | "the end of its string section."); | 605 | 0 | return nullptr; | 606 | 0 | } | 607 | | | 608 | | // Seek to the position reported by the entry. | 609 | 0 | unsigned long first = static_cast<unsigned long>(dyn.d_un.d_val); | 610 | 0 | unsigned long last = first; | 611 | 0 | unsigned long end = static_cast<unsigned long>(strtab.sh_size); | 612 | 0 | this->Stream->seekg(strtab.sh_offset + first); | 613 | | | 614 | | // Read the string. It may be followed by more than one NULL | 615 | | // terminator. Count the total size of the region allocated to | 616 | | // the string. This assumes that the next string in the table | 617 | | // is non-empty, but the "chrpath" tool makes the same | 618 | | // assumption. | 619 | 0 | bool terminated = false; | 620 | 0 | char c; | 621 | 0 | while (last != end && this->Stream->get(c) && !(terminated && c)) { | 622 | 0 | ++last; | 623 | 0 | if (c) { | 624 | 0 | se.Value += c; | 625 | 0 | } else { | 626 | 0 | terminated = true; | 627 | 0 | } | 628 | 0 | } | 629 | | | 630 | | // Make sure the whole value was read. | 631 | 0 | if (!(*this->Stream)) { | 632 | 0 | if (tag == cmELF::TagRPath) { | 633 | 0 | this->SetErrorMessage( | 634 | 0 | "Dynamic section specifies unreadable DT_RPATH"); | 635 | 0 | } else if (tag == cmELF::TagRunPath) { | 636 | 0 | this->SetErrorMessage( | 637 | 0 | "Dynamic section specifies unreadable DT_RUNPATH"); | 638 | 0 | } else if (tag == cmELF::TagMipsRldMapRel) { | 639 | 0 | this->SetErrorMessage( | 640 | 0 | "Dynamic section specifies unreadable DT_MIPS_RLD_MAP_REL"); | 641 | 0 | } else { | 642 | 0 | this->SetErrorMessage("Dynamic section specifies unreadable value" | 643 | 0 | " for unexpected attribute"); | 644 | 0 | } | 645 | 0 | se.Value = ""; | 646 | 0 | return nullptr; | 647 | 0 | } | 648 | | | 649 | | // The value has been read successfully. Report it. | 650 | 0 | se.Position = static_cast<unsigned long>(strtab.sh_offset + first); | 651 | 0 | se.Size = last - first; | 652 | 0 | se.IndexInSection = | 653 | 0 | static_cast<int>(di - this->DynamicSectionEntries.begin()); | 654 | 0 | return &se; | 655 | 0 | } | 656 | 39 | } | 657 | 95 | return nullptr; | 658 | 95 | } |
|
659 | | |
660 | | //============================================================================ |
661 | | // External class implementation. |
662 | | |
663 | | long const cmELF::TagRPath = DT_RPATH; |
664 | | long const cmELF::TagRunPath = DT_RUNPATH; |
665 | | long const cmELF::TagMipsRldMapRel = DT_MIPS_RLD_MAP_REL; |
666 | | |
667 | | cmELF::cmELF(char const* fname) |
668 | 760 | { |
669 | | // Try to open the file. |
670 | 760 | auto fin = cm::make_unique<cmsys::ifstream>(fname, std::ios::binary); |
671 | | |
672 | | // Quit now if the file could not be opened. |
673 | 760 | if (!fin || !*fin) { |
674 | 0 | this->ErrorMessage = "Error opening input file."; |
675 | 0 | return; |
676 | 0 | } |
677 | | |
678 | | // Read the ELF identification block. |
679 | 760 | char ident[EI_NIDENT]; |
680 | 760 | if (!fin->read(ident, EI_NIDENT)) { |
681 | 0 | this->ErrorMessage = "Error reading ELF identification."; |
682 | 0 | return; |
683 | 0 | } |
684 | 760 | if (!fin->seekg(0)) { |
685 | 0 | this->ErrorMessage = "Error seeking to beginning of file."; |
686 | 0 | return; |
687 | 0 | } |
688 | | |
689 | | // Verify the ELF identification. |
690 | 760 | if (!(ident[EI_MAG0] == ELFMAG0 && ident[EI_MAG1] == ELFMAG1 && |
691 | 706 | ident[EI_MAG2] == ELFMAG2 && ident[EI_MAG3] == ELFMAG3)) { |
692 | 77 | this->ErrorMessage = "File does not have a valid ELF identification."; |
693 | 77 | return; |
694 | 77 | } |
695 | | |
696 | | // Check the byte order in which the rest of the file is encoded. |
697 | 683 | cmELFInternal::ByteOrderType order; |
698 | 683 | if (ident[EI_DATA] == ELFDATA2LSB) { |
699 | | // File is LSB. |
700 | 333 | order = cmELFInternal::ByteOrderLSB; |
701 | 350 | } else if (ident[EI_DATA] == ELFDATA2MSB) { |
702 | | // File is MSB. |
703 | 338 | order = cmELFInternal::ByteOrderMSB; |
704 | 338 | } else { |
705 | 12 | this->ErrorMessage = "ELF file is not LSB or MSB encoded."; |
706 | 12 | return; |
707 | 12 | } |
708 | | |
709 | | // Check the class of the file and construct the corresponding |
710 | | // parser implementation. |
711 | 671 | if (ident[EI_CLASS] == ELFCLASS32) { |
712 | | // 32-bit ELF |
713 | 364 | this->Internal = cm::make_unique<cmELFInternalImpl<cmELFTypes32>>( |
714 | 364 | this, std::move(fin), order); |
715 | 364 | } else if (ident[EI_CLASS] == ELFCLASS64) { |
716 | | // 64-bit ELF |
717 | 298 | this->Internal = cm::make_unique<cmELFInternalImpl<cmELFTypes64>>( |
718 | 298 | this, std::move(fin), order); |
719 | 298 | } else { |
720 | 9 | this->ErrorMessage = "ELF file class is not 32-bit or 64-bit."; |
721 | 9 | return; |
722 | 9 | } |
723 | 671 | } |
724 | | |
725 | 760 | cmELF::~cmELF() = default; |
726 | | |
727 | | bool cmELF::Valid() const |
728 | 7.31k | { |
729 | 7.31k | return this->Internal && this->Internal->GetFileType() != FileTypeInvalid; |
730 | 7.31k | } |
731 | | |
732 | | cmELF::FileType cmELF::GetFileType() const |
733 | 379 | { |
734 | 379 | if (this->Valid()) { |
735 | 379 | return this->Internal->GetFileType(); |
736 | 379 | } |
737 | 0 | return FileTypeInvalid; |
738 | 379 | } |
739 | | |
740 | | std::uint16_t cmELF::GetMachine() const |
741 | 379 | { |
742 | 379 | if (this->Valid()) { |
743 | 379 | return this->Internal->GetMachine(); |
744 | 379 | } |
745 | 0 | return 0; |
746 | 379 | } |
747 | | |
748 | | std::size_t cmELF::GetNumberOfSections() const |
749 | 379 | { |
750 | 379 | if (this->Valid()) { |
751 | 379 | return this->Internal->GetNumberOfSections(); |
752 | 379 | } |
753 | 0 | return 0; |
754 | 379 | } |
755 | | |
756 | | unsigned long cmELF::GetDynamicEntryPosition(int index) const |
757 | 2.33k | { |
758 | 2.33k | if (this->Valid()) { |
759 | 2.33k | return this->Internal->GetDynamicEntryPosition(index); |
760 | 2.33k | } |
761 | 0 | return 0; |
762 | 2.33k | } |
763 | | |
764 | | cmELF::DynamicEntryList cmELF::GetDynamicEntries() const |
765 | 379 | { |
766 | 379 | if (this->Valid()) { |
767 | 261 | return this->Internal->GetDynamicEntries(); |
768 | 261 | } |
769 | | |
770 | 118 | return cmELF::DynamicEntryList(); |
771 | 379 | } |
772 | | |
773 | | std::vector<char> cmELF::EncodeDynamicEntries( |
774 | | cmELF::DynamicEntryList const& dentries) const |
775 | 47 | { |
776 | 47 | if (this->Valid()) { |
777 | 47 | return this->Internal->EncodeDynamicEntries(dentries); |
778 | 47 | } |
779 | | |
780 | 0 | return std::vector<char>(); |
781 | 47 | } |
782 | | |
783 | | bool cmELF::HasDynamicSection() const |
784 | 379 | { |
785 | 379 | return this->Valid() && this->Internal->HasDynamicSection(); |
786 | 379 | } |
787 | | |
788 | | bool cmELF::GetSOName(std::string& soname) |
789 | 379 | { |
790 | 379 | if (StringEntry const* se = this->GetSOName()) { |
791 | 9 | soname = se->Value; |
792 | 9 | return true; |
793 | 9 | } |
794 | 370 | return false; |
795 | 379 | } |
796 | | |
797 | | cmELF::StringEntry const* cmELF::GetSOName() |
798 | 758 | { |
799 | 758 | if (this->Valid() && |
800 | 729 | this->Internal->GetFileType() == cmELF::FileTypeSharedLibrary) { |
801 | 109 | return this->Internal->GetSOName(); |
802 | 109 | } |
803 | 649 | return nullptr; |
804 | 758 | } |
805 | | |
806 | | cmELF::StringEntry const* cmELF::GetRPath() |
807 | 379 | { |
808 | 379 | if (this->Valid() && |
809 | 350 | (this->Internal->GetFileType() == cmELF::FileTypeExecutable || |
810 | 198 | this->Internal->GetFileType() == cmELF::FileTypeSharedLibrary)) { |
811 | 198 | return this->Internal->GetRPath(); |
812 | 198 | } |
813 | 181 | return nullptr; |
814 | 379 | } |
815 | | |
816 | | cmELF::StringEntry const* cmELF::GetRunPath() |
817 | 379 | { |
818 | 379 | if (this->Valid() && |
819 | 262 | (this->Internal->GetFileType() == cmELF::FileTypeExecutable || |
820 | 191 | this->Internal->GetFileType() == cmELF::FileTypeSharedLibrary)) { |
821 | 110 | return this->Internal->GetRunPath(); |
822 | 110 | } |
823 | 269 | return nullptr; |
824 | 379 | } |
825 | | |
826 | | bool cmELF::IsMIPS() const |
827 | 379 | { |
828 | 379 | if (this->Valid()) { |
829 | 379 | return this->Internal->IsMips(); |
830 | 379 | } |
831 | 0 | return false; |
832 | 379 | } |
833 | | |
834 | | void cmELF::PrintInfo(std::ostream& os) const |
835 | 379 | { |
836 | 379 | if (this->Valid()) { |
837 | 229 | this->Internal->PrintInfo(os); |
838 | 229 | } else { |
839 | 150 | os << "Not a valid ELF file.\n"; |
840 | 150 | } |
841 | 379 | } |