2 * Copyright 2016 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.
18 #include <folly/experimental/symbolizer/Elf.h>
21 #include <sys/types.h>
27 #include <glog/logging.h>
29 #include <folly/Conv.h>
30 #include <folly/Exception.h>
31 #include <folly/ScopeGuard.h>
34 namespace symbolizer {
36 ElfFile::ElfFile() noexcept
38 file_(static_cast<char*>(MAP_FAILED)),
43 ElfFile::ElfFile(const char* name, bool readOnly)
45 file_(static_cast<char*>(MAP_FAILED)),
51 void ElfFile::open(const char* name, bool readOnly) {
53 int r = openNoThrow(name, readOnly, &msg);
54 if (r == kSystemError) {
55 throwSystemError(msg);
57 CHECK_EQ(r, kSuccess) << msg;
61 int ElfFile::openNoThrow(const char* name, bool readOnly, const char** msg)
63 FOLLY_SAFE_CHECK(fd_ == -1, "File already open");
64 fd_ = ::open(name, readOnly ? O_RDONLY : O_RDWR);
66 if (msg) *msg = "open";
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(); });
74 int r = fstat(fd_, &st);
76 if (msg) *msg = "fstat";
85 file_ = static_cast<char*>(mmap(nullptr, length_, prot, MAP_SHARED, fd_, 0));
86 if (file_ == MAP_FAILED) {
87 if (msg) *msg = "mmap";
92 return kInvalidElfFile;
102 ElfFile::ElfFile(ElfFile&& other) noexcept
105 length_(other.length_),
106 baseAddress_(other.baseAddress_) {
108 other.file_ = static_cast<char*>(MAP_FAILED);
110 other.baseAddress_ = 0;
113 ElfFile& ElfFile::operator=(ElfFile&& other) {
114 assert(this != &other);
119 length_ = other.length_;
120 baseAddress_ = other.baseAddress_;
123 other.file_ = static_cast<char*>(MAP_FAILED);
125 other.baseAddress_ = 0;
130 void ElfFile::reset() {
131 if (file_ != MAP_FAILED) {
132 munmap(file_, length_);
133 file_ = static_cast<char*>(MAP_FAILED);
142 bool ElfFile::init(const char** msg) {
143 auto& elfHeader = this->elfHeader();
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";
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";
164 #undef EXPECTED_CLASS
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";
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";
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";
187 if (elfHeader.e_phnum == 0) {
188 if (msg) *msg = "no program header!";
192 if (elfHeader.e_phentsize != sizeof(ElfW(Phdr))) {
193 if (msg) *msg = "invalid program header entry size";
197 if (elfHeader.e_shentsize != sizeof(ElfW(Shdr))) {
198 if (msg) *msg = "invalid section header entry size";
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;
214 if (msg) *msg = "could not find base address";
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)));
226 folly::StringPiece ElfFile::getSectionBody(const ElfW(Shdr)& section) const {
227 return folly::StringPiece(file_ + section.sh_offset, section.sh_size);
230 void ElfFile::validateStringTable(const ElfW(Shdr)& stringTable) const {
231 FOLLY_SAFE_CHECK(stringTable.sh_type == SHT_STRTAB,
232 "invalid type for string table");
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");
241 const char* ElfFile::getString(const ElfW(Shdr)& stringTable, size_t offset)
243 validateStringTable(stringTable);
244 FOLLY_SAFE_CHECK(offset < stringTable.sh_size,
245 "invalid offset in string table");
247 return file_ + stringTable.sh_offset + offset;
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
255 const ElfW(Shdr)& sectionNames = *getSectionByIndex(elfHeader().e_shstrndx);
256 return getString(sectionNames, section.sh_name);
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
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(
268 [&] (const char* s) { return !strcmp(name, s); });
269 if (foundName == nullptr) {
273 size_t offset = foundName - (file_ + sectionNames.sh_offset);
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) {
286 ElfFile::Symbol ElfFile::getDefinitionByAddress(uintptr_t address) const {
287 Symbol foundSymbol {nullptr, nullptr};
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
294 if (address >= sym.st_value && address < sym.st_value + sym.st_size) {
295 foundSymbol.first = §ion;
296 foundSymbol.second = &sym;
303 return iterateSymbolsWithType(section, STT_OBJECT, findSymbols) ||
304 iterateSymbolsWithType(section, STT_FUNC, findSymbols);
307 // Try the .dynsym section first if it exists, it's smaller.
308 (iterateSectionsWithType(SHT_DYNSYM, findSection) ||
309 iterateSectionsWithType(SHT_SYMTAB, findSection));
314 ElfFile::Symbol ElfFile::getSymbolByName(const char* name) const {
315 Symbol foundSymbol{nullptr, nullptr};
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) {
324 auto findSymbols = [&](const ElfW(Sym)& sym) -> bool {
325 if (sym.st_shndx == SHN_UNDEF) {
326 return false; // not a definition
328 if (sym.st_name == 0) {
329 return false; // no name for this symbol
331 const char* sym_name = getString(
332 *getSectionByIndex(section.sh_link), sym.st_name);
333 if (strcmp(sym_name, name) == 0) {
334 foundSymbol.first = §ion;
335 foundSymbol.second = &sym;
342 return iterateSymbolsWithType(section, STT_OBJECT, findSymbols) ||
343 iterateSymbolsWithType(section, STT_FUNC, findSymbols);
346 // Try the .dynsym section first if it exists, it's smaller.
347 iterateSectionsWithType(SHT_DYNSYM, findSection) ||
348 iterateSectionsWithType(SHT_SYMTAB, findSection);
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));
359 const char* ElfFile::getSymbolName(Symbol symbol) const {
360 if (!symbol.first || !symbol.second) {
364 if (symbol.second->st_name == 0) {
365 return nullptr; // symbol has no name
368 if (symbol.first->sh_link == SHN_UNDEF) {
369 return nullptr; // symbol table has no strings
372 return getString(*getSectionByIndex(symbol.first->sh_link),
373 symbol.second->st_name);
376 } // namespace symbolizer