Prefer constexpr to preprocessor conditionals when checking endianness
[folly.git] / folly / experimental / symbolizer / Elf.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <folly/experimental/symbolizer/Elf.h>
19
20 #include <sys/mman.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24
25 #include <string>
26
27 #include <glog/logging.h>
28
29 #include <folly/Conv.h>
30 #include <folly/Exception.h>
31 #include <folly/ScopeGuard.h>
32
33 namespace folly {
34 namespace symbolizer {
35
36 ElfFile::ElfFile() noexcept
37   : fd_(-1),
38     file_(static_cast<char*>(MAP_FAILED)),
39     length_(0),
40     baseAddress_(0) {
41 }
42
43 ElfFile::ElfFile(const char* name, bool readOnly)
44   : fd_(-1),
45     file_(static_cast<char*>(MAP_FAILED)),
46     length_(0),
47     baseAddress_(0) {
48   open(name, readOnly);
49 }
50
51 void ElfFile::open(const char* name, bool readOnly) {
52   const char* msg = "";
53   int r = openNoThrow(name, readOnly, &msg);
54   if (r == kSystemError) {
55     throwSystemError(msg);
56   } else {
57     CHECK_EQ(r, kSuccess) << msg;
58   }
59 }
60
61 int ElfFile::openNoThrow(const char* name, bool readOnly, const char** msg)
62   noexcept {
63   FOLLY_SAFE_CHECK(fd_ == -1, "File already open");
64   fd_ = ::open(name, readOnly ? O_RDONLY : O_RDWR);
65   if (fd_ == -1) {
66     if (msg) *msg = "open";
67     return kSystemError;
68   }
69   // Always close fd and unmap in case of failure along the way to avoid
70   // check failure above if we leave fd != -1 and the object is recycled
71   // like it is inside SignalSafeElfCache
72   ScopeGuard guard = makeGuard([&]{ reset(); });
73   struct stat st;
74   int r = fstat(fd_, &st);
75   if (r == -1) {
76     if (msg) *msg = "fstat";
77     return kSystemError;
78   }
79
80   length_ = st.st_size;
81   int prot = PROT_READ;
82   if (!readOnly) {
83     prot |= PROT_WRITE;
84   }
85   file_ = static_cast<char*>(mmap(nullptr, length_, prot, MAP_SHARED, fd_, 0));
86   if (file_ == MAP_FAILED) {
87     if (msg) *msg = "mmap";
88     return kSystemError;
89   }
90   if (!init(msg)) {
91     errno = EINVAL;
92     return kInvalidElfFile;
93   }
94   guard.dismiss();
95   return kSuccess;
96 }
97
98 ElfFile::~ElfFile() {
99   reset();
100 }
101
102 ElfFile::ElfFile(ElfFile&& other) noexcept
103   : fd_(other.fd_),
104     file_(other.file_),
105     length_(other.length_),
106     baseAddress_(other.baseAddress_) {
107   other.fd_ = -1;
108   other.file_ = static_cast<char*>(MAP_FAILED);
109   other.length_ = 0;
110   other.baseAddress_ = 0;
111 }
112
113 ElfFile& ElfFile::operator=(ElfFile&& other) {
114   assert(this != &other);
115   reset();
116
117   fd_ = other.fd_;
118   file_ = other.file_;
119   length_ = other.length_;
120   baseAddress_ = other.baseAddress_;
121
122   other.fd_ = -1;
123   other.file_ = static_cast<char*>(MAP_FAILED);
124   other.length_ = 0;
125   other.baseAddress_ = 0;
126
127   return *this;
128 }
129
130 void ElfFile::reset() {
131   if (file_ != MAP_FAILED) {
132     munmap(file_, length_);
133     file_ = static_cast<char*>(MAP_FAILED);
134   }
135
136   if (fd_ != -1) {
137     close(fd_);
138     fd_ = -1;
139   }
140 }
141
142 bool ElfFile::init(const char** msg) {
143   auto& elfHeader = this->elfHeader();
144
145   // Validate ELF magic numbers
146   if (!(elfHeader.e_ident[EI_MAG0] == ELFMAG0 &&
147         elfHeader.e_ident[EI_MAG1] == ELFMAG1 &&
148         elfHeader.e_ident[EI_MAG2] == ELFMAG2 &&
149         elfHeader.e_ident[EI_MAG3] == ELFMAG3)) {
150     if (msg) *msg = "invalid ELF magic";
151     return false;
152   }
153
154   // Validate ELF class (32/64 bits)
155 #define EXPECTED_CLASS P1(ELFCLASS, __ELF_NATIVE_CLASS)
156 #define P1(a, b) P2(a, b)
157 #define P2(a, b) a ## b
158   if (elfHeader.e_ident[EI_CLASS] != EXPECTED_CLASS) {
159     if (msg) *msg = "invalid ELF class";
160     return false;
161   }
162 #undef P1
163 #undef P2
164 #undef EXPECTED_CLASS
165
166   // Validate ELF data encoding (LSB/MSB)
167   static constexpr auto kExpectedEncoding =
168       kIsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
169   if (elfHeader.e_ident[EI_DATA] != kExpectedEncoding) {
170     if (msg) *msg = "invalid ELF encoding";
171     return false;
172   }
173
174   // Validate ELF version (1)
175   if (elfHeader.e_ident[EI_VERSION] != EV_CURRENT ||
176       elfHeader.e_version != EV_CURRENT) {
177     if (msg) *msg = "invalid ELF version";
178     return false;
179   }
180
181   // We only support executable and shared object files
182   if (elfHeader.e_type != ET_EXEC && elfHeader.e_type != ET_DYN) {
183     if (msg) *msg = "invalid ELF file type";
184     return false;
185   }
186
187   if (elfHeader.e_phnum == 0) {
188     if (msg) *msg = "no program header!";
189     return false;
190   }
191
192   if (elfHeader.e_phentsize != sizeof(ElfW(Phdr))) {
193     if (msg) *msg = "invalid program header entry size";
194     return false;
195   }
196
197   if (elfHeader.e_shentsize != sizeof(ElfW(Shdr))) {
198     if (msg) *msg = "invalid section header entry size";
199   }
200
201   const ElfW(Phdr)* programHeader = &at<ElfW(Phdr)>(elfHeader.e_phoff);
202   bool foundBase = false;
203   for (size_t i = 0; i < elfHeader.e_phnum; programHeader++, i++) {
204     // Program headers are sorted by load address, so the first PT_LOAD
205     // header gives us the base address.
206     if (programHeader->p_type == PT_LOAD) {
207       baseAddress_ = programHeader->p_vaddr;
208       foundBase = true;
209       break;
210     }
211   }
212
213   if (!foundBase) {
214     if (msg) *msg =  "could not find base address";
215     return false;
216   }
217
218   return true;
219 }
220
221 const ElfW(Shdr)* ElfFile::getSectionByIndex(size_t idx) const {
222   FOLLY_SAFE_CHECK(idx < elfHeader().e_shnum, "invalid section index");
223   return &at<ElfW(Shdr)>(elfHeader().e_shoff + idx * sizeof(ElfW(Shdr)));
224 }
225
226 folly::StringPiece ElfFile::getSectionBody(const ElfW(Shdr)& section) const {
227   return folly::StringPiece(file_ + section.sh_offset, section.sh_size);
228 }
229
230 void ElfFile::validateStringTable(const ElfW(Shdr)& stringTable) const {
231   FOLLY_SAFE_CHECK(stringTable.sh_type == SHT_STRTAB,
232                    "invalid type for string table");
233
234   const char* start = file_ + stringTable.sh_offset;
235   // First and last bytes must be 0
236   FOLLY_SAFE_CHECK(stringTable.sh_size == 0 ||
237                    (start[0] == '\0' && start[stringTable.sh_size - 1] == '\0'),
238                    "invalid string table");
239 }
240
241 const char* ElfFile::getString(const ElfW(Shdr)& stringTable, size_t offset)
242   const {
243   validateStringTable(stringTable);
244   FOLLY_SAFE_CHECK(offset < stringTable.sh_size,
245                    "invalid offset in string table");
246
247   return file_ + stringTable.sh_offset + offset;
248 }
249
250 const char* ElfFile::getSectionName(const ElfW(Shdr)& section) const {
251   if (elfHeader().e_shstrndx == SHN_UNDEF) {
252     return nullptr;  // no section name string table
253   }
254
255   const ElfW(Shdr)& sectionNames = *getSectionByIndex(elfHeader().e_shstrndx);
256   return getString(sectionNames, section.sh_name);
257 }
258
259 const ElfW(Shdr)* ElfFile::getSectionByName(const char* name) const {
260   if (elfHeader().e_shstrndx == SHN_UNDEF) {
261     return nullptr;  // no section name string table
262   }
263
264   // Find offset in the section name string table of the requested name
265   const ElfW(Shdr)& sectionNames = *getSectionByIndex(elfHeader().e_shstrndx);
266   const char* foundName = iterateStrings(
267       sectionNames,
268       [&] (const char* s) { return !strcmp(name, s); });
269   if (foundName == nullptr) {
270     return nullptr;
271   }
272
273   size_t offset = foundName - (file_ + sectionNames.sh_offset);
274
275   // Find section with the appropriate sh_name offset
276   const ElfW(Shdr)* foundSection = iterateSections(
277     [&](const ElfW(Shdr)& sh) {
278       if (sh.sh_name == offset) {
279         return true;
280       }
281       return false;
282     });
283   return foundSection;
284 }
285
286 ElfFile::Symbol ElfFile::getDefinitionByAddress(uintptr_t address) const {
287   Symbol foundSymbol {nullptr, nullptr};
288
289   auto findSection = [&](const ElfW(Shdr)& section) {
290     auto findSymbols = [&](const ElfW(Sym)& sym) {
291       if (sym.st_shndx == SHN_UNDEF) {
292         return false;  // not a definition
293       }
294       if (address >= sym.st_value && address < sym.st_value + sym.st_size) {
295         foundSymbol.first = &section;
296         foundSymbol.second = &sym;
297         return true;
298       }
299
300       return false;
301     };
302
303     return iterateSymbolsWithType(section, STT_OBJECT, findSymbols) ||
304       iterateSymbolsWithType(section, STT_FUNC, findSymbols);
305   };
306
307   // Try the .dynsym section first if it exists, it's smaller.
308   (iterateSectionsWithType(SHT_DYNSYM, findSection) ||
309    iterateSectionsWithType(SHT_SYMTAB, findSection));
310
311   return foundSymbol;
312 }
313
314 ElfFile::Symbol ElfFile::getSymbolByName(const char* name) const {
315   Symbol foundSymbol{nullptr, nullptr};
316
317   auto findSection = [&](const ElfW(Shdr)& section) -> bool {
318     // This section has no string table associated w/ its symbols; hence we
319     // can't get names for them
320     if (section.sh_link == SHN_UNDEF) {
321       return false;
322     }
323
324     auto findSymbols = [&](const ElfW(Sym)& sym) -> bool {
325       if (sym.st_shndx == SHN_UNDEF) {
326         return false;  // not a definition
327       }
328       if (sym.st_name == 0) {
329         return false;  // no name for this symbol
330       }
331       const char* sym_name = getString(
332         *getSectionByIndex(section.sh_link), sym.st_name);
333       if (strcmp(sym_name, name) == 0) {
334         foundSymbol.first = &section;
335         foundSymbol.second = &sym;
336         return true;
337       }
338
339       return false;
340     };
341
342     return iterateSymbolsWithType(section, STT_OBJECT, findSymbols) ||
343       iterateSymbolsWithType(section, STT_FUNC, findSymbols);
344   };
345
346   // Try the .dynsym section first if it exists, it's smaller.
347   iterateSectionsWithType(SHT_DYNSYM, findSection) ||
348     iterateSectionsWithType(SHT_SYMTAB, findSection);
349
350   return foundSymbol;
351 }
352
353 const ElfW(Shdr)* ElfFile::getSectionContainingAddress(ElfW(Addr) addr) const {
354   return iterateSections([&](const ElfW(Shdr)& sh) -> bool {
355     return (addr >= sh.sh_addr) && (addr < (sh.sh_addr + sh.sh_size));
356   });
357 }
358
359 const char* ElfFile::getSymbolName(Symbol symbol) const {
360   if (!symbol.first || !symbol.second) {
361     return nullptr;
362   }
363
364   if (symbol.second->st_name == 0) {
365     return nullptr;  // symbol has no name
366   }
367
368   if (symbol.first->sh_link == SHN_UNDEF) {
369     return nullptr;  // symbol table has no strings
370   }
371
372   return getString(*getSectionByIndex(symbol.first->sh_link),
373                    symbol.second->st_name);
374 }
375
376 }  // namespace symbolizer
377 }  // namespace folly