2 * Copyright 2017 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.
17 #ifndef FOLLY_EXPERIMENTAL_SYMBOLIZER_ELF_H_
18 #error This file must be included from Elf.h
22 namespace symbolizer {
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++) {
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++) {
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); });
55 const char* ElfFile::iterateStrings(const ElfShdr& stringTable, Fn fn) const {
56 validateStringTable(stringTable);
58 const char* start = file_ + stringTable.sh_offset;
59 const char* end = start + stringTable.sh_size;
61 const char* ptr = start;
62 while (ptr != end && !fn(ptr)) {
63 ptr += strlen(ptr) + 1;
66 return ptr != end ? ptr : nullptr;
70 const ElfSym* ElfFile::iterateSymbols(const ElfShdr& section, Fn fn) const {
72 section.sh_entsize == sizeof(ElfSym),
73 "invalid entry size in symbol table");
75 const ElfSym* sym = &at<ElfSym>(section.sh_offset);
76 const ElfSym* end = sym + (section.sh_size / section.sh_entsize);
90 const ElfSym* ElfFile::iterateSymbolsWithType(
91 const ElfShdr& section,
94 // N.B. st_info has the same representation on 32- and 64-bit platforms
95 return iterateSymbols(section, [&](const ElfSym& sym) -> bool {
96 return ELF32_ST_TYPE(sym.st_info) == type && fn(sym);
100 } // namespace symbolizer