SymbolSize.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //===- SymbolSize.cpp -----------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Object/SymbolSize.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/Object/COFF.h"
  11. #include "llvm/Object/ELFObjectFile.h"
  12. #include "llvm/Object/MachO.h"
  13. #include "llvm/Object/Wasm.h"
  14. using namespace llvm;
  15. using namespace object;
  16. // Orders increasingly by (SectionID, Address).
  17. int llvm::object::compareAddress(const SymEntry *A, const SymEntry *B) {
  18. if (A->SectionID != B->SectionID)
  19. return A->SectionID < B->SectionID ? -1 : 1;
  20. if (A->Address != B->Address)
  21. return A->Address < B->Address ? -1 : 1;
  22. return 0;
  23. }
  24. static unsigned getSectionID(const ObjectFile &O, SectionRef Sec) {
  25. if (auto *M = dyn_cast<MachOObjectFile>(&O))
  26. return M->getSectionID(Sec);
  27. if (isa<WasmObjectFile>(&O))
  28. return Sec.getIndex();
  29. return cast<COFFObjectFile>(O).getSectionID(Sec);
  30. }
  31. static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym) {
  32. if (auto *M = dyn_cast<MachOObjectFile>(&O))
  33. return M->getSymbolSectionID(Sym);
  34. if (const auto *M = dyn_cast<WasmObjectFile>(&O))
  35. return M->getSymbolSectionId(Sym);
  36. return cast<COFFObjectFile>(O).getSymbolSectionID(Sym);
  37. }
  38. std::vector<std::pair<SymbolRef, uint64_t>>
  39. llvm::object::computeSymbolSizes(const ObjectFile &O) {
  40. std::vector<std::pair<SymbolRef, uint64_t>> Ret;
  41. if (const auto *E = dyn_cast<ELFObjectFileBase>(&O)) {
  42. auto Syms = E->symbols();
  43. if (Syms.empty())
  44. Syms = E->getDynamicSymbolIterators();
  45. for (ELFSymbolRef Sym : Syms)
  46. Ret.push_back({Sym, Sym.getSize()});
  47. return Ret;
  48. }
  49. // Collect sorted symbol addresses. Include dummy addresses for the end
  50. // of each section.
  51. std::vector<SymEntry> Addresses;
  52. unsigned SymNum = 0;
  53. for (symbol_iterator I = O.symbol_begin(), E = O.symbol_end(); I != E; ++I) {
  54. SymbolRef Sym = *I;
  55. Expected<uint64_t> ValueOrErr = Sym.getValue();
  56. if (!ValueOrErr)
  57. // TODO: Actually report errors helpfully.
  58. report_fatal_error(ValueOrErr.takeError());
  59. Addresses.push_back({I, *ValueOrErr, SymNum, getSymbolSectionID(O, Sym)});
  60. ++SymNum;
  61. }
  62. for (SectionRef Sec : O.sections()) {
  63. uint64_t Address = Sec.getAddress();
  64. uint64_t Size = Sec.getSize();
  65. Addresses.push_back(
  66. {O.symbol_end(), Address + Size, 0, getSectionID(O, Sec)});
  67. }
  68. if (Addresses.empty())
  69. return Ret;
  70. array_pod_sort(Addresses.begin(), Addresses.end(), compareAddress);
  71. // Compute the size as the gap to the next symbol
  72. for (unsigned I = 0, N = Addresses.size() - 1; I < N; ++I) {
  73. auto &P = Addresses[I];
  74. if (P.I == O.symbol_end())
  75. continue;
  76. // If multiple symbol have the same address, give both the same size.
  77. unsigned NextI = I + 1;
  78. while (NextI < N && Addresses[NextI].Address == P.Address)
  79. ++NextI;
  80. uint64_t Size = Addresses[NextI].Address - P.Address;
  81. P.Address = Size;
  82. }
  83. // Assign the sorted symbols in the original order.
  84. Ret.resize(SymNum);
  85. for (SymEntry &P : Addresses) {
  86. if (P.I == O.symbol_end())
  87. continue;
  88. Ret[P.Number] = {*P.I, P.Address};
  89. }
  90. return Ret;
  91. }