Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 1 | //===- 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 Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 10 | #include "BinaryHolder.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/iterator_range.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 13 | #include "llvm/Support/DataTypes.h" |
| 14 | #include "llvm/Support/Format.h" |
| 15 | #include "llvm/Support/raw_ostream.h" |
| 16 | #include <algorithm> |
| 17 | |
| 18 | namespace llvm { |
| 19 | namespace dsymutil { |
| 20 | |
| 21 | using namespace llvm::object; |
| 22 | |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 23 | DebugMapObject::DebugMapObject(StringRef ObjectFilename, |
| 24 | sys::TimeValue Timestamp) |
| 25 | : Filename(ObjectFilename), Timestamp(Timestamp) {} |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 26 | |
Frederic Riss | d8c33dc | 2016-01-31 04:29:22 +0000 | [diff] [blame^] | 27 | bool DebugMapObject::addSymbol(StringRef Name, Optional<uint64_t> ObjectAddress, |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 28 | uint64_t LinkedAddress, uint32_t Size) { |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 29 | auto InsertResult = Symbols.insert( |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 30 | std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size))); |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 31 | |
Frederic Riss | d8c33dc | 2016-01-31 04:29:22 +0000 | [diff] [blame^] | 32 | if (ObjectAddress && InsertResult.second) |
| 33 | AddressToMapping[*ObjectAddress] = &*InsertResult.first; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 34 | return InsertResult.second; |
| 35 | } |
| 36 | |
| 37 | void DebugMapObject::print(raw_ostream &OS) const { |
| 38 | OS << getObjectFilename() << ":\n"; |
| 39 | // Sort the symbols in alphabetical order, like llvm-nm (and to get |
| 40 | // deterministic output for testing). |
| 41 | typedef std::pair<StringRef, SymbolMapping> Entry; |
| 42 | std::vector<Entry> Entries; |
| 43 | Entries.reserve(Symbols.getNumItems()); |
| 44 | for (const auto &Sym : make_range(Symbols.begin(), Symbols.end())) |
| 45 | Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue())); |
| 46 | std::sort( |
| 47 | Entries.begin(), Entries.end(), |
| 48 | [](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; }); |
| 49 | for (const auto &Sym : Entries) { |
Frederic Riss | d8c33dc | 2016-01-31 04:29:22 +0000 | [diff] [blame^] | 50 | if (Sym.second.ObjectAddress) |
| 51 | OS << format("\t%016" PRIx64, uint64_t(*Sym.second.ObjectAddress)); |
| 52 | else |
| 53 | OS << "\t????????????????"; |
| 54 | OS << format(" => %016" PRIx64 "+0x%x\t%s\n", |
Frederic Riss | f37964c | 2015-06-05 20:27:07 +0000 | [diff] [blame] | 55 | uint64_t(Sym.second.BinaryAddress), uint32_t(Sym.second.Size), |
| 56 | Sym.first.data()); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 57 | } |
| 58 | OS << '\n'; |
| 59 | } |
| 60 | |
| 61 | #ifndef NDEBUG |
| 62 | void DebugMapObject::dump() const { print(errs()); } |
| 63 | #endif |
| 64 | |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 65 | DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath, |
| 66 | sys::TimeValue Timestamp) { |
| 67 | Objects.emplace_back(new DebugMapObject(ObjectFilePath, Timestamp)); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 68 | return *Objects.back(); |
| 69 | } |
| 70 | |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 71 | const DebugMapObject::DebugMapEntry * |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 72 | DebugMapObject::lookupSymbol(StringRef SymbolName) const { |
| 73 | StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName); |
| 74 | if (Sym == Symbols.end()) |
| 75 | return nullptr; |
Frederic Riss | 1595c5d | 2015-02-13 23:18:16 +0000 | [diff] [blame] | 76 | return &*Sym; |
| 77 | } |
| 78 | |
| 79 | const DebugMapObject::DebugMapEntry * |
| 80 | DebugMapObject::lookupObjectAddress(uint64_t Address) const { |
| 81 | auto Mapping = AddressToMapping.find(Address); |
| 82 | if (Mapping == AddressToMapping.end()) |
| 83 | return nullptr; |
| 84 | return Mapping->getSecond(); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void DebugMap::print(raw_ostream &OS) const { |
Frederic Riss | 08462f7 | 2015-06-01 21:12:45 +0000 | [diff] [blame] | 88 | yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0); |
Frederic Riss | f37964c | 2015-06-05 20:27:07 +0000 | [diff] [blame] | 89 | yout << const_cast<DebugMap &>(*this); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | #ifndef NDEBUG |
| 93 | void DebugMap::dump() const { print(errs()); } |
| 94 | #endif |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 95 | |
Frederic Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 96 | namespace { |
| 97 | struct YAMLContext { |
| 98 | StringRef PrependPath; |
Frederic Riss | a81e881 | 2015-06-05 21:21:57 +0000 | [diff] [blame] | 99 | Triple BinaryTriple; |
Frederic Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 100 | }; |
| 101 | } |
| 102 | |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 103 | ErrorOr<std::vector<std::unique_ptr<DebugMap>>> |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 104 | DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, |
| 105 | bool Verbose) { |
| 106 | auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile); |
| 107 | if (auto Err = ErrOrFile.getError()) |
| 108 | return Err; |
| 109 | |
Frederic Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 110 | YAMLContext Ctxt; |
| 111 | |
| 112 | Ctxt.PrependPath = PrependPath; |
| 113 | |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 114 | std::unique_ptr<DebugMap> Res; |
Frederic Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 115 | yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt); |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 116 | yin >> Res; |
| 117 | |
| 118 | if (auto EC = yin.error()) |
| 119 | return EC; |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 120 | std::vector<std::unique_ptr<DebugMap>> Result; |
| 121 | Result.push_back(std::move(Res)); |
| 122 | return std::move(Result); |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | namespace yaml { |
| 127 | |
| 128 | // Normalize/Denormalize between YAML and a DebugMapObject. |
| 129 | struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO { |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 130 | YamlDMO(IO &io) { Timestamp = 0; } |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 131 | YamlDMO(IO &io, dsymutil::DebugMapObject &Obj); |
| 132 | dsymutil::DebugMapObject denormalize(IO &IO); |
| 133 | |
| 134 | std::string Filename; |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 135 | sys::TimeValue::SecondsType Timestamp; |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 136 | std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries; |
| 137 | }; |
| 138 | |
| 139 | void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>:: |
| 140 | mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) { |
| 141 | io.mapRequired("sym", s.first); |
Frederic Riss | d8c33dc | 2016-01-31 04:29:22 +0000 | [diff] [blame^] | 142 | io.mapOptional("objAddr", s.second.ObjectAddress); |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 143 | io.mapRequired("binAddr", s.second.BinaryAddress); |
| 144 | io.mapOptional("size", s.second.Size); |
| 145 | } |
| 146 | |
| 147 | void MappingTraits<dsymutil::DebugMapObject>::mapping( |
| 148 | IO &io, dsymutil::DebugMapObject &DMO) { |
| 149 | MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO); |
| 150 | io.mapRequired("filename", Norm->Filename); |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 151 | io.mapOptional("timestamp", Norm->Timestamp); |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 152 | io.mapRequired("symbols", Norm->Entries); |
| 153 | } |
| 154 | |
| 155 | void ScalarTraits<Triple>::output(const Triple &val, void *, |
| 156 | llvm::raw_ostream &out) { |
| 157 | out << val.str(); |
| 158 | } |
| 159 | |
| 160 | StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) { |
| 161 | value = Triple(scalar); |
| 162 | return StringRef(); |
| 163 | } |
| 164 | |
| 165 | size_t |
| 166 | SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size( |
| 167 | IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) { |
| 168 | return seq.size(); |
| 169 | } |
| 170 | |
| 171 | dsymutil::DebugMapObject & |
| 172 | SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element( |
| 173 | IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq, |
| 174 | size_t index) { |
| 175 | if (index >= seq.size()) { |
| 176 | seq.resize(index + 1); |
| 177 | seq[index].reset(new dsymutil::DebugMapObject); |
| 178 | } |
| 179 | return *seq[index]; |
| 180 | } |
| 181 | |
| 182 | void MappingTraits<dsymutil::DebugMap>::mapping(IO &io, |
| 183 | dsymutil::DebugMap &DM) { |
| 184 | io.mapRequired("triple", DM.BinaryTriple); |
Frederic Riss | 2c69d36 | 2015-08-26 05:09:59 +0000 | [diff] [blame] | 185 | io.mapOptional("binary-path", DM.BinaryPath); |
Frederic Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 186 | if (void *Ctxt = io.getContext()) |
Frederic Riss | a81e881 | 2015-06-05 21:21:57 +0000 | [diff] [blame] | 187 | reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple; |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 188 | io.mapOptional("objects", DM.Objects); |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping( |
| 192 | IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) { |
| 193 | if (!DM) |
| 194 | DM.reset(new DebugMap()); |
| 195 | io.mapRequired("triple", DM->BinaryTriple); |
Frederic Riss | 2c69d36 | 2015-08-26 05:09:59 +0000 | [diff] [blame] | 196 | io.mapOptional("binary-path", DM->BinaryPath); |
Frederic Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 197 | if (void *Ctxt = io.getContext()) |
Frederic Riss | a81e881 | 2015-06-05 21:21:57 +0000 | [diff] [blame] | 198 | reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple; |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 199 | io.mapOptional("objects", DM->Objects); |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO( |
| 203 | IO &io, dsymutil::DebugMapObject &Obj) { |
| 204 | Filename = Obj.Filename; |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 205 | Timestamp = Obj.getTimestamp().toEpochTime(); |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 206 | Entries.reserve(Obj.Symbols.size()); |
| 207 | for (auto &Entry : Obj.Symbols) |
| 208 | Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue())); |
| 209 | } |
| 210 | |
| 211 | dsymutil::DebugMapObject |
| 212 | MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) { |
Frederic Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 213 | BinaryHolder BinHolder(/* Verbose =*/false); |
| 214 | const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext()); |
| 215 | SmallString<80> Path(Ctxt.PrependPath); |
| 216 | StringMap<uint64_t> SymbolAddresses; |
| 217 | |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 218 | sys::path::append(Path, Filename); |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 219 | auto ErrOrObjectFiles = BinHolder.GetObjectFiles(Path); |
| 220 | if (auto EC = ErrOrObjectFiles.getError()) { |
Frederic Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 221 | llvm::errs() << "warning: Unable to open " << Path << " " << EC.message() |
| 222 | << '\n'; |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 223 | } else if (auto ErrOrObjectFile = BinHolder.Get(Ctxt.BinaryTriple)) { |
Frederic Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 224 | // Rewrite the object file symbol addresses in the debug map. The |
| 225 | // YAML input is mainly used to test llvm-dsymutil without |
| 226 | // requiring binaries checked-in. If we generate the object files |
| 227 | // during the test, we can't hardcode the symbols addresses, so |
| 228 | // look them up here and rewrite them. |
| 229 | for (const auto &Sym : ErrOrObjectFile->symbols()) { |
Rafael Espindola | be8b0ea | 2015-07-07 17:12:59 +0000 | [diff] [blame] | 230 | uint64_t Address = Sym.getValue(); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 231 | ErrorOr<StringRef> Name = Sym.getName(); |
| 232 | if (!Name) |
| 233 | continue; |
| 234 | SymbolAddresses[*Name] = Address; |
Frederic Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 238 | sys::TimeValue TV; |
| 239 | TV.fromEpochTime(Timestamp); |
| 240 | dsymutil::DebugMapObject Res(Path, TV); |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 241 | for (auto &Entry : Entries) { |
| 242 | auto &Mapping = Entry.second; |
Frederic Riss | d8c33dc | 2016-01-31 04:29:22 +0000 | [diff] [blame^] | 243 | Optional<uint64_t> ObjAddress; |
| 244 | if (Mapping.ObjectAddress) |
| 245 | ObjAddress = *Mapping.ObjectAddress; |
Frederic Riss | 4f5874a | 2015-06-05 21:12:07 +0000 | [diff] [blame] | 246 | auto AddressIt = SymbolAddresses.find(Entry.first); |
| 247 | if (AddressIt != SymbolAddresses.end()) |
| 248 | ObjAddress = AddressIt->getValue(); |
| 249 | Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size); |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 250 | } |
| 251 | return Res; |
| 252 | } |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 253 | } |
| 254 | } |