Move exception tracer library to folly/experimental
[folly.git] / folly / experimental / symbolizer / Elf-inl.h
1 /*
2  * Copyright 2012 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 facebook {
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
63 }  // namespace symbolizer
64 }  // namespace facebook
65