Fix SignalHandlerTest with ASAN
[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 #ifndef FOLLY_EXPERIMENTAL_SYMBOLIZER_ELF_H_
18 #error This file must be included from Elf.h
19 #endif
20
21 namespace folly {
22 namespace symbolizer {
23
24 template <class Fn>
25 const ElfPhdr* ElfFile::iterateProgramHeaders(Fn fn) const {
26   const ElfPhdr* ptr = &at<ElfPhdr>(elfHeader().e_phoff);
27   for (size_t i = 0; i < elfHeader().e_phnum; i++, ptr++) {
28     if (fn(*ptr)) {
29       return ptr;
30     }
31   }
32
33   return nullptr;
34 }
35
36 template <class Fn>
37 const ElfShdr* ElfFile::iterateSections(Fn fn) const {
38   const ElfShdr* ptr = &at<ElfShdr>(elfHeader().e_shoff);
39   for (size_t i = 0; i < elfHeader().e_shnum; i++, ptr++) {
40     if (fn(*ptr)) {
41       return ptr;
42     }
43   }
44
45   return nullptr;
46 }
47
48 template <class Fn>
49 const ElfShdr* ElfFile::iterateSectionsWithType(uint32_t type, Fn fn) const {
50   return iterateSections(
51       [&](const ElfShdr& sh) { return sh.sh_type == type && fn(sh); });
52 }
53
54 template <class Fn>
55 const ElfShdr* ElfFile::iterateSectionsWithTypes(
56     std::initializer_list<uint32_t> types,
57     Fn fn) const {
58   return iterateSections([&](const ElfShdr& sh) {
59     auto const it = std::find(types.begin(), types.end(), sh.sh_type);
60     return it != types.end() && fn(sh);
61   });
62 }
63
64 template <class Fn>
65 const char* ElfFile::iterateStrings(const ElfShdr& stringTable, Fn fn) const {
66   validateStringTable(stringTable);
67
68   const char* start = file_ + stringTable.sh_offset;
69   const char* end = start + stringTable.sh_size;
70
71   const char* ptr = start;
72   while (ptr != end && !fn(ptr)) {
73     ptr += strlen(ptr) + 1;
74   }
75
76   return ptr != end ? ptr : nullptr;
77 }
78
79 template <class Fn>
80 const ElfSym* ElfFile::iterateSymbols(const ElfShdr& section, Fn fn) const {
81   FOLLY_SAFE_CHECK(
82       section.sh_entsize == sizeof(ElfSym),
83       "invalid entry size in symbol table");
84
85   const ElfSym* sym = &at<ElfSym>(section.sh_offset);
86   const ElfSym* end = sym + (section.sh_size / section.sh_entsize);
87
88   while (sym < end) {
89     if (fn(*sym)) {
90       return sym;
91     }
92
93     ++sym;
94   }
95
96   return nullptr;
97 }
98
99 template <class Fn>
100 const ElfSym* ElfFile::iterateSymbolsWithType(
101     const ElfShdr& section,
102     uint32_t type,
103     Fn fn) const {
104   // N.B. st_info has the same representation on 32- and 64-bit platforms
105   return iterateSymbols(section, [&](const ElfSym& sym) -> bool {
106     return ELF32_ST_TYPE(sym.st_info) == type && fn(sym);
107   });
108 }
109
110 template <class Fn>
111 const ElfSym* ElfFile::iterateSymbolsWithTypes(
112     const ElfShdr& section,
113     std::initializer_list<uint32_t> types,
114     Fn fn) const {
115   // N.B. st_info has the same representation on 32- and 64-bit platforms
116   return iterateSymbols(section, [&](const ElfSym& sym) -> bool {
117     auto const elfType = ELF32_ST_TYPE(sym.st_info);
118     auto const it = std::find(types.begin(), types.end(), elfType);
119     return it != types.end() && fn(sym);
120   });
121 }
122
123 } // namespace symbolizer
124 } // namespace folly