SymbolSize.cpp 3.5 KB

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