MapFile.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //===- MapFile.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. //
  9. // This file implements the /lldmap option. It shows lists in order and
  10. // hierarchically the output sections, input sections, input files and
  11. // symbol:
  12. //
  13. // Address Size Align Out File Symbol
  14. // 00201000 00000015 4 .text
  15. // 00201000 0000000e 4 test.o:(.text)
  16. // 0020100e 00000000 0 local
  17. // 00201005 00000000 0 f(int)
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "MapFile.h"
  21. #include "SymbolTable.h"
  22. #include "Symbols.h"
  23. #include "Writer.h"
  24. #include "lld/Common/ErrorHandler.h"
  25. #include "lld/Common/Threads.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. using namespace llvm;
  28. using namespace llvm::object;
  29. namespace lld {
  30. namespace coff {
  31. using SymbolMapTy =
  32. DenseMap<const SectionChunk *, SmallVector<DefinedRegular *, 4>>;
  33. static constexpr char indent8[] = " "; // 8 spaces
  34. static constexpr char indent16[] = " "; // 16 spaces
  35. // Print out the first three columns of a line.
  36. static void writeHeader(raw_ostream &os, uint64_t addr, uint64_t size,
  37. uint64_t align) {
  38. os << format("%08llx %08llx %5lld ", addr, size, align);
  39. }
  40. // Returns a list of all symbols that we want to print out.
  41. static std::vector<DefinedRegular *> getSymbols() {
  42. std::vector<DefinedRegular *> v;
  43. for (ObjFile *file : ObjFile::instances)
  44. for (Symbol *b : file->getSymbols())
  45. if (auto *sym = dyn_cast_or_null<DefinedRegular>(b))
  46. if (sym && !sym->getCOFFSymbol().isSectionDefinition())
  47. v.push_back(sym);
  48. return v;
  49. }
  50. // Returns a map from sections to their symbols.
  51. static SymbolMapTy getSectionSyms(ArrayRef<DefinedRegular *> syms) {
  52. SymbolMapTy ret;
  53. for (DefinedRegular *s : syms)
  54. ret[s->getChunk()].push_back(s);
  55. // Sort symbols by address.
  56. for (auto &it : ret) {
  57. SmallVectorImpl<DefinedRegular *> &v = it.second;
  58. std::sort(v.begin(), v.end(), [](DefinedRegular *a, DefinedRegular *b) {
  59. return a->getRVA() < b->getRVA();
  60. });
  61. }
  62. return ret;
  63. }
  64. // Construct a map from symbols to their stringified representations.
  65. static DenseMap<DefinedRegular *, std::string>
  66. getSymbolStrings(ArrayRef<DefinedRegular *> syms) {
  67. std::vector<std::string> str(syms.size());
  68. parallelForEachN((size_t)0, syms.size(), [&](size_t i) {
  69. raw_string_ostream os(str[i]);
  70. writeHeader(os, syms[i]->getRVA(), 0, 0);
  71. os << indent16 << toString(*syms[i]);
  72. });
  73. DenseMap<DefinedRegular *, std::string> ret;
  74. for (size_t i = 0, e = syms.size(); i < e; ++i)
  75. ret[syms[i]] = std::move(str[i]);
  76. return ret;
  77. }
  78. void writeMapFile(ArrayRef<OutputSection *> outputSections) {
  79. if (config->mapFile.empty())
  80. return;
  81. std::error_code ec;
  82. raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
  83. if (ec)
  84. fatal("cannot open " + config->mapFile + ": " + ec.message());
  85. // Collect symbol info that we want to print out.
  86. std::vector<DefinedRegular *> syms = getSymbols();
  87. SymbolMapTy sectionSyms = getSectionSyms(syms);
  88. DenseMap<DefinedRegular *, std::string> symStr = getSymbolStrings(syms);
  89. // Print out the header line.
  90. os << "Address Size Align Out In Symbol\n";
  91. // Print out file contents.
  92. for (OutputSection *sec : outputSections) {
  93. writeHeader(os, sec->getRVA(), sec->getVirtualSize(), /*align=*/pageSize);
  94. os << sec->name << '\n';
  95. for (Chunk *c : sec->chunks) {
  96. auto *sc = dyn_cast<SectionChunk>(c);
  97. if (!sc)
  98. continue;
  99. writeHeader(os, sc->getRVA(), sc->getSize(), sc->getAlignment());
  100. os << indent8 << sc->file->getName() << ":(" << sc->getSectionName()
  101. << ")\n";
  102. for (DefinedRegular *sym : sectionSyms[sc])
  103. os << symStr[sym] << '\n';
  104. }
  105. }
  106. }
  107. } // namespace coff
  108. } // namespace lld