2017
[folly.git] / folly / experimental / symbolizer / Elf-inl.h
1 /*
2  * Copyright 2017 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 #ifndef FOLLY_EXPERIMENTAL_SYMBOLIZER_ELF_H_
19 # error This file must be included from Elf.h
20 #endif
21
22 namespace folly {
23 namespace symbolizer {
24
25 template <class Fn>
26 const ElfW(Shdr)* ElfFile::iterateSections(Fn fn) const {
27   const ElfW(Shdr)* ptr = &at<ElfW(Shdr)>(elfHeader().e_shoff);
28   for (size_t i = 0; i < elfHeader().e_shnum; i++, ptr++) {
29     if (fn(*ptr)) {
30       return ptr;
31     }
32   }
33
34   return nullptr;
35 }
36
37 template <class Fn>
38 const ElfW(Shdr)* ElfFile::iterateSectionsWithType(uint32_t type, Fn fn)
39   const {
40   return iterateSections(
41       [&](const ElfW(Shdr)& sh) {
42         return sh.sh_type == type && fn(sh);
43       });
44 }
45
46 template <class Fn>
47 const char* ElfFile::iterateStrings(const ElfW(Shdr)& stringTable, Fn fn)
48   const {
49   validateStringTable(stringTable);
50
51   const char* start = file_ + stringTable.sh_offset;
52   const char* end = start + stringTable.sh_size;
53
54   const char* ptr = start;
55   while (ptr != end && !fn(ptr)) {
56     ptr += strlen(ptr) + 1;
57   }
58
59   return ptr != end ? ptr : nullptr;
60 }
61
62 template <class Fn>
63 const ElfW(Sym)* ElfFile::iterateSymbols(const ElfW(Shdr)& section, Fn fn)
64   const {
65   FOLLY_SAFE_CHECK(section.sh_entsize == sizeof(ElfW(Sym)),
66                    "invalid entry size in symbol table");
67
68   const ElfW(Sym)* sym = &at<ElfW(Sym)>(section.sh_offset);
69   const ElfW(Sym)* end = sym + (section.sh_size / section.sh_entsize);
70
71   while (sym < end) {
72     if (fn(*sym)) {
73       return sym;
74     }
75
76     ++sym;
77   }
78
79   return nullptr;
80 }
81
82 template <class Fn>
83 const ElfW(Sym)* ElfFile::iterateSymbolsWithType(const ElfW(Shdr)& section,
84                                                  uint32_t type, Fn fn) const {
85   // N.B. st_info has the same representation on 32- and 64-bit platforms
86   return iterateSymbols(section, [&](const ElfW(Sym)& sym) -> bool {
87     return ELF32_ST_TYPE(sym.st_info) == type && fn(sym);
88   });
89 }
90
91 }  // namespace symbolizer
92 }  // namespace folly