blob: e5cc87b3f3181edc09cfa21db71f62bec364bb5b [file] [log] [blame]
Frederic Riss231f7142014-12-12 17:31:24 +00001//===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "DebugMap.h"
Frederic Riss4f5874a2015-06-05 21:12:07 +000010#include "BinaryHolder.h"
Frederic Riss231f7142014-12-12 17:31:24 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000012#include "llvm/ADT/iterator_range.h"
Frederic Riss231f7142014-12-12 17:31:24 +000013#include "llvm/Support/DataTypes.h"
14#include "llvm/Support/Format.h"
15#include "llvm/Support/raw_ostream.h"
16#include <algorithm>
17
18namespace llvm {
19namespace dsymutil {
20
21using namespace llvm::object;
22
23DebugMapObject::DebugMapObject(StringRef ObjectFilename)
24 : Filename(ObjectFilename) {}
25
26bool DebugMapObject::addSymbol(StringRef Name, uint64_t ObjectAddress,
Frederic Riss912d0f12015-03-15 01:29:30 +000027 uint64_t LinkedAddress, uint32_t Size) {
Frederic Riss231f7142014-12-12 17:31:24 +000028 auto InsertResult = Symbols.insert(
Frederic Riss912d0f12015-03-15 01:29:30 +000029 std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size)));
Frederic Riss1595c5d2015-02-13 23:18:16 +000030
31 if (InsertResult.second)
32 AddressToMapping[ObjectAddress] = &*InsertResult.first;
Frederic Riss231f7142014-12-12 17:31:24 +000033 return InsertResult.second;
34}
35
36void DebugMapObject::print(raw_ostream &OS) const {
37 OS << getObjectFilename() << ":\n";
38 // Sort the symbols in alphabetical order, like llvm-nm (and to get
39 // deterministic output for testing).
40 typedef std::pair<StringRef, SymbolMapping> Entry;
41 std::vector<Entry> Entries;
42 Entries.reserve(Symbols.getNumItems());
43 for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
44 Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
45 std::sort(
46 Entries.begin(), Entries.end(),
47 [](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; });
48 for (const auto &Sym : Entries) {
Frederic Riss912d0f12015-03-15 01:29:30 +000049 OS << format("\t%016" PRIx64 " => %016" PRIx64 "+0x%x\t%s\n",
Frederic Riss08462f72015-06-01 21:12:45 +000050 uint64_t(Sym.second.ObjectAddress),
Frederic Rissf37964c2015-06-05 20:27:07 +000051 uint64_t(Sym.second.BinaryAddress), uint32_t(Sym.second.Size),
52 Sym.first.data());
Frederic Riss231f7142014-12-12 17:31:24 +000053 }
54 OS << '\n';
55}
56
57#ifndef NDEBUG
58void DebugMapObject::dump() const { print(errs()); }
59#endif
60
61DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath) {
62 Objects.emplace_back(new DebugMapObject(ObjectFilePath));
63 return *Objects.back();
64}
65
Frederic Riss1595c5d2015-02-13 23:18:16 +000066const DebugMapObject::DebugMapEntry *
Frederic Riss231f7142014-12-12 17:31:24 +000067DebugMapObject::lookupSymbol(StringRef SymbolName) const {
68 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
69 if (Sym == Symbols.end())
70 return nullptr;
Frederic Riss1595c5d2015-02-13 23:18:16 +000071 return &*Sym;
72}
73
74const DebugMapObject::DebugMapEntry *
75DebugMapObject::lookupObjectAddress(uint64_t Address) const {
76 auto Mapping = AddressToMapping.find(Address);
77 if (Mapping == AddressToMapping.end())
78 return nullptr;
79 return Mapping->getSecond();
Frederic Riss231f7142014-12-12 17:31:24 +000080}
81
82void DebugMap::print(raw_ostream &OS) const {
Frederic Riss08462f72015-06-01 21:12:45 +000083 yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
Frederic Rissf37964c2015-06-05 20:27:07 +000084 yout << const_cast<DebugMap &>(*this);
Frederic Riss231f7142014-12-12 17:31:24 +000085}
86
87#ifndef NDEBUG
88void DebugMap::dump() const { print(errs()); }
89#endif
Frederic Riss4d0ba662015-06-05 20:27:04 +000090
Frederic Riss4f5874a2015-06-05 21:12:07 +000091namespace {
92struct YAMLContext {
93 StringRef PrependPath;
Frederic Rissa81e8812015-06-05 21:21:57 +000094 Triple BinaryTriple;
Frederic Riss4f5874a2015-06-05 21:12:07 +000095};
96}
97
Frederic Riss4d0ba662015-06-05 20:27:04 +000098ErrorOr<std::unique_ptr<DebugMap>>
99DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
100 bool Verbose) {
101 auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
102 if (auto Err = ErrOrFile.getError())
103 return Err;
104
Frederic Riss4f5874a2015-06-05 21:12:07 +0000105 YAMLContext Ctxt;
106
107 Ctxt.PrependPath = PrependPath;
108
Frederic Riss4d0ba662015-06-05 20:27:04 +0000109 std::unique_ptr<DebugMap> Res;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000110 yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000111 yin >> Res;
112
113 if (auto EC = yin.error())
114 return EC;
115
116 return std::move(Res);
117}
118}
119
120namespace yaml {
121
122// Normalize/Denormalize between YAML and a DebugMapObject.
123struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
124 YamlDMO(IO &io) {}
125 YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
126 dsymutil::DebugMapObject denormalize(IO &IO);
127
128 std::string Filename;
129 std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
130};
131
132void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
133 mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
134 io.mapRequired("sym", s.first);
135 io.mapRequired("objAddr", s.second.ObjectAddress);
136 io.mapRequired("binAddr", s.second.BinaryAddress);
137 io.mapOptional("size", s.second.Size);
138}
139
140void MappingTraits<dsymutil::DebugMapObject>::mapping(
141 IO &io, dsymutil::DebugMapObject &DMO) {
142 MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
143 io.mapRequired("filename", Norm->Filename);
144 io.mapRequired("symbols", Norm->Entries);
145}
146
147void ScalarTraits<Triple>::output(const Triple &val, void *,
148 llvm::raw_ostream &out) {
149 out << val.str();
150}
151
152StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
153 value = Triple(scalar);
154 return StringRef();
155}
156
157size_t
158SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
159 IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
160 return seq.size();
161}
162
163dsymutil::DebugMapObject &
164SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
165 IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
166 size_t index) {
167 if (index >= seq.size()) {
168 seq.resize(index + 1);
169 seq[index].reset(new dsymutil::DebugMapObject);
170 }
171 return *seq[index];
172}
173
174void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
175 dsymutil::DebugMap &DM) {
176 io.mapRequired("triple", DM.BinaryTriple);
177 io.mapOptional("objects", DM.Objects);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000178 if (void *Ctxt = io.getContext())
Frederic Rissa81e8812015-06-05 21:21:57 +0000179 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
Frederic Riss4d0ba662015-06-05 20:27:04 +0000180}
181
182void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
183 IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
184 if (!DM)
185 DM.reset(new DebugMap());
186 io.mapRequired("triple", DM->BinaryTriple);
187 io.mapOptional("objects", DM->Objects);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000188 if (void *Ctxt = io.getContext())
Frederic Rissa81e8812015-06-05 21:21:57 +0000189 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
Frederic Riss4d0ba662015-06-05 20:27:04 +0000190}
191
192MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
193 IO &io, dsymutil::DebugMapObject &Obj) {
194 Filename = Obj.Filename;
195 Entries.reserve(Obj.Symbols.size());
196 for (auto &Entry : Obj.Symbols)
197 Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
198}
199
200dsymutil::DebugMapObject
201MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
Frederic Riss4f5874a2015-06-05 21:12:07 +0000202 BinaryHolder BinHolder(/* Verbose =*/false);
203 const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
204 SmallString<80> Path(Ctxt.PrependPath);
205 StringMap<uint64_t> SymbolAddresses;
206
Frederic Riss4d0ba662015-06-05 20:27:04 +0000207 sys::path::append(Path, Filename);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000208 auto ErrOrObjectFile = BinHolder.GetObjectFile(Path);
209 if (auto EC = ErrOrObjectFile.getError()) {
210 llvm::errs() << "warning: Unable to open " << Path << " " << EC.message()
211 << '\n';
212 } else {
213 // Rewrite the object file symbol addresses in the debug map. The
214 // YAML input is mainly used to test llvm-dsymutil without
215 // requiring binaries checked-in. If we generate the object files
216 // during the test, we can't hardcode the symbols addresses, so
217 // look them up here and rewrite them.
218 for (const auto &Sym : ErrOrObjectFile->symbols()) {
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000219 uint64_t Address = Sym.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000220 ErrorOr<StringRef> Name = Sym.getName();
221 if (!Name)
222 continue;
223 SymbolAddresses[*Name] = Address;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000224 }
225 }
226
Frederic Riss4d0ba662015-06-05 20:27:04 +0000227 dsymutil::DebugMapObject Res(Path);
228 for (auto &Entry : Entries) {
229 auto &Mapping = Entry.second;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000230 uint64_t ObjAddress = Mapping.ObjectAddress;
231 auto AddressIt = SymbolAddresses.find(Entry.first);
232 if (AddressIt != SymbolAddresses.end())
233 ObjAddress = AddressIt->getValue();
234 Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000235 }
236 return Res;
237}
Frederic Riss231f7142014-12-12 17:31:24 +0000238}
239}