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 #ifndef FOLLY_EXPERIMENTAL_SYMBOLIZER_ELF_H_
19 # error This file must be included from Elf.h
23 namespace symbolizer {
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++) {
38 const ElfW(Shdr)* ElfFile::iterateSectionsWithType(uint32_t type, Fn fn)
40 return iterateSections(
41 [&](const ElfW(Shdr)& sh) {
42 return sh.sh_type == type && fn(sh);
47 const char* ElfFile::iterateStrings(const ElfW(Shdr)& stringTable, Fn fn)
49 validateStringTable(stringTable);
51 const char* start = file_ + stringTable.sh_offset;
52 const char* end = start + stringTable.sh_size;
54 const char* ptr = start;
55 while (ptr != end && !fn(ptr)) {
56 ptr += strlen(ptr) + 1;
59 return ptr != end ? ptr : nullptr;
63 const ElfW(Sym)* ElfFile::iterateSymbols(const ElfW(Shdr)& section, Fn fn)
65 FOLLY_SAFE_CHECK(section.sh_entsize == sizeof(ElfW(Sym)),
66 "invalid entry size in symbol table");
68 const ElfW(Sym)* sym = &at<ElfW(Sym)>(section.sh_offset);
69 const ElfW(Sym)* end = sym + (section.sh_size / section.sh_entsize);
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);
91 } // namespace symbolizer