Elf-inl.h 3.9 KB

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