summaryrefslogtreecommitdiffstats
path: root/lld/MachO/MapFile.cpp
blob: a4a0065c2816560f11031b3a8190bc3ea6b67a1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//===- MapFile.cpp --------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the -map option. It shows lists in order and
// hierarchically the outputFile, arch, input files, output sections and
// symbol:
//
// # Path: test
// # Arch: x86_84
// # Object files:
// [  0] linker synthesized
// [  1] a.o
// # Sections:
// # Address  Size      Segment  Section
// 0x1000005C0  0x0000004C  __TEXT  __text
// # Symbols:
// # Address  File  Name
// 0x1000005C0  [  1] _main
//
//===----------------------------------------------------------------------===//

#include "MapFile.h"
#include "Config.h"
#include "InputFiles.h"
#include "InputSection.h"
#include "OutputSection.h"
#include "OutputSegment.h"
#include "Symbols.h"
#include "SyntheticSections.h"
#include "Target.h"
#include "llvm/Support/Parallel.h"
#include "llvm/Support/TimeProfiler.h"

using namespace llvm;
using namespace llvm::sys;
using namespace lld;
using namespace lld::macho;

using Symbols = std::vector<Defined *>;
// Returns a pair where the left element is a container of all live Symbols and
// the right element is a container of all dead symbols.
static std::pair<Symbols, Symbols> getSymbols() {
  Symbols liveSymbols, deadSymbols;
  for (InputFile *file : inputFiles)
    if (isa<ObjFile>(file))
      for (Symbol *sym : file->symbols)
        if (auto *d = dyn_cast_or_null<Defined>(sym))
          if (d->isec && d->getFile() == file) {
            if (d->isLive()) {
              assert(!shouldOmitFromOutput(d->isec));
              liveSymbols.push_back(d);
            } else {
              deadSymbols.push_back(d);
            }
          }
  parallelSort(liveSymbols.begin(), liveSymbols.end(),
               [](Defined *a, Defined *b) {
                 return a->getVA() != b->getVA() ? a->getVA() < b->getVA()
                                                 : a->getName() < b->getName();
               });
  parallelSort(
      deadSymbols.begin(), deadSymbols.end(),
      [](Defined *a, Defined *b) { return a->getName() < b->getName(); });
  return {std::move(liveSymbols), std::move(deadSymbols)};
}

// Construct a map from symbols to their stringified representations.
// Demangling symbols (which is what toString() does) is slow, so
// we do that in batch using parallel-for.
static DenseMap<Symbol *, std::string>
getSymbolStrings(ArrayRef<Defined *> syms) {
  std::vector<std::string> str(syms.size());
  parallelForEachN(0, syms.size(), [&](size_t i) {
    raw_string_ostream os(str[i]);
    Defined *sym = syms[i];

    switch (sym->isec->kind()) {
    case InputSection::CStringLiteralKind: {
      // Output "literal string: <string literal>"
      const auto *isec = cast<CStringInputSection>(sym->isec);
      const StringPiece &piece = isec->getStringPiece(sym->value);
      assert(
          sym->value == piece.inSecOff &&
          "We expect symbols to always point to the start of a StringPiece.");
      StringRef str = isec->getStringRef(&piece - &(*isec->pieces.begin()));
      assert(str.back() == '\000');
      (os << "literal string: ")
          // Remove null sequence at the end
          .write_escaped(str.substr(0, str.size() - 1));
      break;
    }
    case InputSection::ConcatKind:
    case InputSection::WordLiteralKind:
      os << toString(*sym);
    }
  });

  DenseMap<Symbol *, std::string> ret;
  for (size_t i = 0, e = syms.size(); i < e; ++i)
    ret[syms[i]] = std::move(str[i]);
  return ret;
}

void macho::writeMapFile() {
  if (config->mapFile.empty())
    return;

  TimeTraceScope timeScope("Write map file");

  // Open a map file for writing.
  std::error_code ec;
  raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
  if (ec) {
    error("cannot open " + config->mapFile + ": " + ec.message());
    return;
  }

  // Dump output path.
  os << format("# Path: %s\n", config->outputFile.str().c_str());

  // Dump output architecture.
  os << format("# Arch: %s\n",
               getArchitectureName(config->arch()).str().c_str());

  // Dump table of object files.
  os << "# Object files:\n";
  os << format("[%3u] %s\n", 0, (const char *)"linker synthesized");
  uint32_t fileIndex = 1;
  DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal;
  for (InputFile *file : inputFiles) {
    if (isa<ObjFile>(file)) {
      os << format("[%3u] %s\n", fileIndex, file->getName().str().c_str());
      readerToFileOrdinal[file] = fileIndex++;
    }
  }

  // Dump table of sections
  os << "# Sections:\n";
  os << "# Address\tSize    \tSegment\tSection\n";
  for (OutputSegment *seg : outputSegments)
    for (OutputSection *osec : seg->getSections()) {
      if (osec->isHidden())
        continue;

      os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(),
                   seg->name.str().c_str(), osec->name.str().c_str());
    }

  // Dump table of symbols
  Symbols liveSymbols, deadSymbols;
  std::tie(liveSymbols, deadSymbols) = getSymbols();

  DenseMap<Symbol *, std::string> liveSymbolStrings =
      getSymbolStrings(liveSymbols);
  os << "# Symbols:\n";
  os << "# Address\t    File  Name\n";
  for (Symbol *sym : liveSymbols) {
    assert(sym->isLive());
    os << format("0x%08llX\t[%3u] %s\n", sym->getVA(),
                 readerToFileOrdinal[sym->getFile()],
                 liveSymbolStrings[sym].c_str());
  }

  if (config->deadStrip) {
    DenseMap<Symbol *, std::string> deadSymbolStrings =
        getSymbolStrings(deadSymbols);
    os << "# Dead Stripped Symbols:\n";
    os << "# Address\t    File  Name\n";
    for (Symbol *sym : deadSymbols) {
      assert(!sym->isLive());
      os << format("<<dead>>\t[%3u] %s\n", readerToFileOrdinal[sym->getFile()],
                   deadSymbolStrings[sym].c_str());
    }
  }
}