2 * Copyright 2017-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <folly/experimental/symbolizer/Elf.h>
19 #include <folly/portability/SysMman.h>
21 #include <sys/types.h>
26 #include <glog/logging.h>
28 #include <folly/Conv.h>
29 #include <folly/Exception.h>
30 #include <folly/ScopeGuard.h>
33 #define STT_GNU_IFUNC 10
37 namespace symbolizer {
39 ElfFile::ElfFile() noexcept
41 file_(static_cast<char*>(MAP_FAILED)),
45 ElfFile::ElfFile(const char* name, bool readOnly)
47 file_(static_cast<char*>(MAP_FAILED)),
53 void ElfFile::open(const char* name, bool readOnly) {
55 int r = openNoThrow(name, readOnly, &msg);
56 if (r == kSystemError) {
57 throwSystemError(msg);
59 CHECK_EQ(r, kSuccess) << msg;
63 int ElfFile::openNoThrow(
66 const char** msg) noexcept {
67 FOLLY_SAFE_CHECK(fd_ == -1, "File already open");
68 strncat(filepath_, name, kFilepathMaxLen - 1);
69 fd_ = ::open(name, readOnly ? O_RDONLY : O_RDWR);
76 // Always close fd and unmap in case of failure along the way to avoid
77 // check failure above if we leave fd != -1 and the object is recycled
78 // like it is inside SignalSafeElfCache
79 ScopeGuard guard = makeGuard([&] { reset(); });
81 int r = fstat(fd_, &st);
94 file_ = static_cast<char*>(mmap(nullptr, length_, prot, MAP_SHARED, fd_, 0));
95 if (file_ == MAP_FAILED) {
103 return kInvalidElfFile;
109 int ElfFile::openAndFollow(
112 const char** msg) noexcept {
113 auto result = openNoThrow(name, readOnly, msg);
114 if (!readOnly || result != kSuccess) {
118 /* NOTE .gnu_debuglink specifies only the name of the debugging info file
119 * (with no directory components). GDB checks 3 different directories, but
120 * ElfFile only supports the first version:
122 * - dirname(name) + /.debug/
123 * - X/dirname(name)/ - where X is set in gdb's `debug-file-directory`.
125 auto dirend = strrchr(name, '/');
126 // include ending '/' if any.
127 auto dirlen = dirend != nullptr ? dirend + 1 - name : 0;
129 auto debuginfo = getSectionByName(".gnu_debuglink");
134 // The section starts with the filename, with any leading directory
135 // components removed, followed by a zero byte.
136 auto debugFileName = getSectionBody(*debuginfo);
137 auto debugFileLen = strlen(debugFileName.begin());
138 if (dirlen + debugFileLen >= PATH_MAX) {
142 char linkname[PATH_MAX];
143 memcpy(linkname, name, dirlen);
144 memcpy(linkname + dirlen, debugFileName.begin(), debugFileLen + 1);
146 result = openNoThrow(linkname, readOnly, msg);
147 if (result == kSuccess) {
150 return openNoThrow(name, readOnly, msg);
153 ElfFile::~ElfFile() {
157 ElfFile::ElfFile(ElfFile&& other) noexcept
160 length_(other.length_),
161 baseAddress_(other.baseAddress_) {
162 // copy other.filepath_, leaving filepath_ zero-terminated, always.
163 strncat(filepath_, other.filepath_, kFilepathMaxLen - 1);
164 other.filepath_[0] = 0;
166 other.file_ = static_cast<char*>(MAP_FAILED);
168 other.baseAddress_ = 0;
171 ElfFile& ElfFile::operator=(ElfFile&& other) {
172 assert(this != &other);
175 // copy other.filepath_, leaving filepath_ zero-terminated, always.
176 strncat(filepath_, other.filepath_, kFilepathMaxLen - 1);
179 length_ = other.length_;
180 baseAddress_ = other.baseAddress_;
182 other.filepath_[0] = 0;
184 other.file_ = static_cast<char*>(MAP_FAILED);
186 other.baseAddress_ = 0;
191 void ElfFile::reset() {
194 if (file_ != MAP_FAILED) {
195 munmap(file_, length_);
196 file_ = static_cast<char*>(MAP_FAILED);
205 bool ElfFile::init(const char** msg) {
208 *msg = "not an ELF file (too short)";
213 // Validate ELF magic numbers
214 if (file_[EI_MAG0] != ELFMAG0 || file_[EI_MAG1] != ELFMAG1 ||
215 file_[EI_MAG2] != ELFMAG2 || file_[EI_MAG3] != ELFMAG3) {
217 *msg = "invalid ELF magic";
222 auto& elfHeader = this->elfHeader();
224 #define EXPECTED_CLASS P1(ELFCLASS, __ELF_NATIVE_CLASS)
225 #define P1(a, b) P2(a, b)
226 #define P2(a, b) a##b
227 // Validate ELF class (32/64 bits)
228 if (elfHeader.e_ident[EI_CLASS] != EXPECTED_CLASS) {
230 *msg = "invalid ELF class";
236 #undef EXPECTED_CLASS
238 // Validate ELF data encoding (LSB/MSB)
239 static constexpr auto kExpectedEncoding =
240 kIsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
241 if (elfHeader.e_ident[EI_DATA] != kExpectedEncoding) {
243 *msg = "invalid ELF encoding";
248 // Validate ELF version (1)
249 if (elfHeader.e_ident[EI_VERSION] != EV_CURRENT ||
250 elfHeader.e_version != EV_CURRENT) {
252 *msg = "invalid ELF version";
257 // We only support executable and shared object files
258 if (elfHeader.e_type != ET_EXEC && elfHeader.e_type != ET_DYN) {
260 *msg = "invalid ELF file type";
265 if (elfHeader.e_phnum == 0) {
267 *msg = "no program header!";
272 if (elfHeader.e_phentsize != sizeof(ElfPhdr)) {
274 *msg = "invalid program header entry size";
279 if (elfHeader.e_shentsize != sizeof(ElfShdr)) {
281 *msg = "invalid section header entry size";
285 // Program headers are sorted by load address, so the first PT_LOAD
286 // header gives us the base address.
287 const ElfPhdr* programHeader =
288 iterateProgramHeaders([](auto& h) { return h.p_type == PT_LOAD; });
290 if (!programHeader) {
292 *msg = "could not find base address";
296 baseAddress_ = programHeader->p_vaddr;
301 const ElfShdr* ElfFile::getSectionByIndex(size_t idx) const {
302 FOLLY_SAFE_CHECK(idx < elfHeader().e_shnum, "invalid section index");
303 return &at<ElfShdr>(elfHeader().e_shoff + idx * sizeof(ElfShdr));
306 folly::StringPiece ElfFile::getSectionBody(const ElfShdr& section) const {
307 return folly::StringPiece(file_ + section.sh_offset, section.sh_size);
310 void ElfFile::validateStringTable(const ElfShdr& stringTable) const {
312 stringTable.sh_type == SHT_STRTAB, "invalid type for string table");
314 const char* start = file_ + stringTable.sh_offset;
315 // First and last bytes must be 0
317 stringTable.sh_size == 0 ||
318 (start[0] == '\0' && start[stringTable.sh_size - 1] == '\0'),
319 "invalid string table");
322 const char* ElfFile::getString(const ElfShdr& stringTable, size_t offset)
324 validateStringTable(stringTable);
326 offset < stringTable.sh_size, "invalid offset in string table");
328 return file_ + stringTable.sh_offset + offset;
331 const char* ElfFile::getSectionName(const ElfShdr& section) const {
332 if (elfHeader().e_shstrndx == SHN_UNDEF) {
333 return nullptr; // no section name string table
336 const ElfShdr& sectionNames = *getSectionByIndex(elfHeader().e_shstrndx);
337 return getString(sectionNames, section.sh_name);
340 const ElfShdr* ElfFile::getSectionByName(const char* name) const {
341 if (elfHeader().e_shstrndx == SHN_UNDEF) {
342 return nullptr; // no section name string table
345 // Find offset in the section name string table of the requested name
346 const ElfShdr& sectionNames = *getSectionByIndex(elfHeader().e_shstrndx);
347 const char* foundName = iterateStrings(
348 sectionNames, [&](const char* s) { return !strcmp(name, s); });
349 if (foundName == nullptr) {
353 size_t offset = foundName - (file_ + sectionNames.sh_offset);
355 // Find section with the appropriate sh_name offset
356 const ElfShdr* foundSection = iterateSections([&](const ElfShdr& sh) {
357 if (sh.sh_name == offset) {
365 ElfFile::Symbol ElfFile::getDefinitionByAddress(uintptr_t address) const {
366 Symbol foundSymbol{nullptr, nullptr};
368 auto findSection = [&](const ElfShdr& section) {
369 auto findSymbols = [&](const ElfSym& sym) {
370 if (sym.st_shndx == SHN_UNDEF) {
371 return false; // not a definition
373 if (address >= sym.st_value && address < sym.st_value + sym.st_size) {
374 foundSymbol.first = §ion;
375 foundSymbol.second = &sym;
382 return iterateSymbolsWithTypes(
383 section, {STT_OBJECT, STT_FUNC, STT_GNU_IFUNC}, findSymbols);
386 // Try the .dynsym section first if it exists, it's smaller.
387 (iterateSectionsWithType(SHT_DYNSYM, findSection) ||
388 iterateSectionsWithType(SHT_SYMTAB, findSection));
393 ElfFile::Symbol ElfFile::getSymbolByName(const char* name) const {
394 Symbol foundSymbol{nullptr, nullptr};
396 auto findSection = [&](const ElfShdr& section) -> bool {
397 // This section has no string table associated w/ its symbols; hence we
398 // can't get names for them
399 if (section.sh_link == SHN_UNDEF) {
403 auto findSymbols = [&](const ElfSym& sym) -> bool {
404 if (sym.st_shndx == SHN_UNDEF) {
405 return false; // not a definition
407 if (sym.st_name == 0) {
408 return false; // no name for this symbol
410 const char* sym_name =
411 getString(*getSectionByIndex(section.sh_link), sym.st_name);
412 if (strcmp(sym_name, name) == 0) {
413 foundSymbol.first = §ion;
414 foundSymbol.second = &sym;
421 return iterateSymbolsWithTypes(
422 section, {STT_OBJECT, STT_FUNC, STT_GNU_IFUNC}, findSymbols);
425 // Try the .dynsym section first if it exists, it's smaller.
426 iterateSectionsWithType(SHT_DYNSYM, findSection) ||
427 iterateSectionsWithType(SHT_SYMTAB, findSection);
432 const ElfShdr* ElfFile::getSectionContainingAddress(ElfAddr addr) const {
433 return iterateSections([&](const ElfShdr& sh) -> bool {
434 return (addr >= sh.sh_addr) && (addr < (sh.sh_addr + sh.sh_size));
438 const char* ElfFile::getSymbolName(Symbol symbol) const {
439 if (!symbol.first || !symbol.second) {
443 if (symbol.second->st_name == 0) {
444 return nullptr; // symbol has no name
447 if (symbol.first->sh_link == SHN_UNDEF) {
448 return nullptr; // symbol table has no strings
452 *getSectionByIndex(symbol.first->sh_link), symbol.second->st_name);
455 } // namespace symbolizer