| 1 | //===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===// |
| 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 | #include "DebugMap.h" |
| 10 | #include "BinaryHolder.h" |
| 11 | #include "llvm/ADT/SmallString.h" |
| 12 | #include "llvm/ADT/StringMap.h" |
| 13 | #include "llvm/ADT/StringRef.h" |
| 14 | #include "llvm/BinaryFormat/MachO.h" |
| 15 | #include "llvm/Object/ObjectFile.h" |
| 16 | #include "llvm/Support/Chrono.h" |
| 17 | #include "llvm/Support/Error.h" |
| 18 | #include "llvm/Support/Format.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #include "llvm/Support/Path.h" |
| 21 | #include "llvm/Support/WithColor.h" |
| 22 | #include "llvm/Support/YAMLTraits.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include "llvm/TargetParser/Triple.h" |
| 25 | #include <algorithm> |
| 26 | #include <cinttypes> |
| 27 | #include <cstdint> |
| 28 | #include <memory> |
| 29 | #include <optional> |
| 30 | #include <string> |
| 31 | #include <utility> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace llvm { |
| 35 | |
| 36 | namespace dsymutil { |
| 37 | |
| 38 | using namespace llvm::object; |
| 39 | |
| 40 | DebugMapObject::DebugMapObject(StringRef ObjectFilename, |
| 41 | sys::TimePoint<std::chrono::seconds> Timestamp, |
| 42 | uint8_t Type) |
| 43 | : Filename(std::string(ObjectFilename)), Timestamp(Timestamp), Type(Type) {} |
| 44 | |
| 45 | bool DebugMapObject::addSymbol(StringRef Name, |
| 46 | std::optional<uint64_t> ObjectAddress, |
| 47 | uint64_t LinkedAddress, uint32_t Size) { |
| 48 | if (Symbols.count(Key: Name)) { |
| 49 | // Symbol was previously added. |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | auto InsertResult = Symbols.insert( |
| 54 | KV: std::make_pair(x&: Name, y: SymbolMapping(ObjectAddress, LinkedAddress, Size))); |
| 55 | |
| 56 | if (ObjectAddress && InsertResult.second) |
| 57 | AddressToMapping[*ObjectAddress] = &*InsertResult.first; |
| 58 | return InsertResult.second; |
| 59 | } |
| 60 | |
| 61 | void DebugMapObject::setRelocationMap(dsymutil::RelocationMap &RM) { |
| 62 | RelocMap.emplace(args&: RM); |
| 63 | } |
| 64 | |
| 65 | void DebugMapObject::setInstallName(StringRef IN) { InstallName.emplace(args&: IN); } |
| 66 | |
| 67 | void DebugMapObject::print(raw_ostream &OS) const { |
| 68 | OS << getObjectFilename() << ":\n" ; |
| 69 | // Sort the symbols in alphabetical order, like llvm-nm (and to get |
| 70 | // deterministic output for testing). |
| 71 | using Entry = std::pair<StringRef, SymbolMapping>; |
| 72 | std::vector<Entry> Entries; |
| 73 | Entries.reserve(n: Symbols.getNumItems()); |
| 74 | for (const auto &Sym : Symbols) |
| 75 | Entries.push_back(x: std::make_pair(x: Sym.getKey(), y: Sym.getValue())); |
| 76 | llvm::sort(C&: Entries, Comp: llvm::less_first()); |
| 77 | for (const auto &Sym : Entries) { |
| 78 | if (Sym.second.ObjectAddress) |
| 79 | OS << format(Fmt: "\t%016" PRIx64, Vals: uint64_t(*Sym.second.ObjectAddress)); |
| 80 | else |
| 81 | OS << "\t????????????????" ; |
| 82 | OS << format(Fmt: " => %016" PRIx64 "+0x%x\t%s\n" , |
| 83 | Vals: uint64_t(Sym.second.BinaryAddress), Vals: uint32_t(Sym.second.Size), |
| 84 | Vals: Sym.first.data()); |
| 85 | } |
| 86 | OS << '\n'; |
| 87 | } |
| 88 | |
| 89 | #ifndef NDEBUG |
| 90 | void DebugMapObject::dump() const { print(errs()); } |
| 91 | #endif |
| 92 | |
| 93 | DebugMapObject & |
| 94 | DebugMap::addDebugMapObject(StringRef ObjectFilePath, |
| 95 | sys::TimePoint<std::chrono::seconds> Timestamp, |
| 96 | uint8_t Type) { |
| 97 | Objects.emplace_back(args: new DebugMapObject(ObjectFilePath, Timestamp, Type)); |
| 98 | return *Objects.back(); |
| 99 | } |
| 100 | |
| 101 | const DebugMapObject::DebugMapEntry * |
| 102 | DebugMapObject::lookupSymbol(StringRef SymbolName) const { |
| 103 | StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(Key: SymbolName); |
| 104 | if (Sym == Symbols.end()) |
| 105 | return nullptr; |
| 106 | return &*Sym; |
| 107 | } |
| 108 | |
| 109 | const DebugMapObject::DebugMapEntry * |
| 110 | DebugMapObject::lookupObjectAddress(uint64_t Address) const { |
| 111 | auto Mapping = AddressToMapping.find(Val: Address); |
| 112 | if (Mapping == AddressToMapping.end()) |
| 113 | return nullptr; |
| 114 | return Mapping->getSecond(); |
| 115 | } |
| 116 | |
| 117 | void DebugMap::print(raw_ostream &OS) const { |
| 118 | yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0); |
| 119 | yout << const_cast<DebugMap &>(*this); |
| 120 | } |
| 121 | |
| 122 | #ifndef NDEBUG |
| 123 | void DebugMap::dump() const { print(errs()); } |
| 124 | #endif |
| 125 | |
| 126 | namespace { |
| 127 | |
| 128 | struct YAMLContext { |
| 129 | YAMLContext(BinaryHolder &BinHolder, StringRef PrependPath) |
| 130 | : BinHolder(BinHolder), PrependPath(PrependPath) {} |
| 131 | BinaryHolder &BinHolder; |
| 132 | StringRef PrependPath; |
| 133 | Triple BinaryTriple; |
| 134 | }; |
| 135 | |
| 136 | } // end anonymous namespace |
| 137 | |
| 138 | ErrorOr<std::vector<std::unique_ptr<DebugMap>>> |
| 139 | DebugMap::parseYAMLDebugMap(BinaryHolder &BinHolder, StringRef InputFile, |
| 140 | StringRef PrependPath, bool Verbose) { |
| 141 | auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(Filename: InputFile); |
| 142 | if (auto Err = ErrOrFile.getError()) |
| 143 | return Err; |
| 144 | |
| 145 | YAMLContext Ctxt(BinHolder, PrependPath); |
| 146 | |
| 147 | std::unique_ptr<DebugMap> Res; |
| 148 | yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt); |
| 149 | yin >> Res; |
| 150 | |
| 151 | if (auto EC = yin.error()) |
| 152 | return EC; |
| 153 | std::vector<std::unique_ptr<DebugMap>> Result; |
| 154 | Result.push_back(x: std::move(Res)); |
| 155 | return std::move(Result); |
| 156 | } |
| 157 | |
| 158 | } // end namespace dsymutil |
| 159 | |
| 160 | namespace yaml { |
| 161 | |
| 162 | // Normalize/Denormalize between YAML and a DebugMapObject. |
| 163 | struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO { |
| 164 | YamlDMO(IO &io) {} |
| 165 | YamlDMO(IO &io, dsymutil::DebugMapObject &Obj); |
| 166 | dsymutil::DebugMapObject denormalize(IO &IO); |
| 167 | |
| 168 | std::string Filename; |
| 169 | int64_t Timestamp = 0; |
| 170 | uint8_t Type = MachO::N_OSO; |
| 171 | std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries; |
| 172 | }; |
| 173 | |
| 174 | void MappingTraits<std::pair<std::string, SymbolMapping>>::mapping( |
| 175 | IO &io, std::pair<std::string, SymbolMapping> &s) { |
| 176 | io.mapRequired(Key: "sym" , Val&: s.first); |
| 177 | io.mapOptional(Key: "objAddr" , Val&: s.second.ObjectAddress); |
| 178 | io.mapRequired(Key: "binAddr" , Val&: s.second.BinaryAddress); |
| 179 | io.mapOptional(Key: "size" , Val&: s.second.Size); |
| 180 | } |
| 181 | |
| 182 | void MappingTraits<dsymutil::DebugMapObject>::mapping( |
| 183 | IO &io, dsymutil::DebugMapObject &DMO) { |
| 184 | MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO); |
| 185 | io.mapRequired(Key: "filename" , Val&: Norm->Filename); |
| 186 | io.mapOptional(Key: "timestamp" , Val&: Norm->Timestamp); |
| 187 | io.mapOptional(Key: "type" , Val&: Norm->Type); |
| 188 | io.mapRequired(Key: "symbols" , Val&: Norm->Entries); |
| 189 | } |
| 190 | |
| 191 | void ScalarTraits<Triple>::output(const Triple &val, void *, raw_ostream &out) { |
| 192 | out << val.str(); |
| 193 | } |
| 194 | |
| 195 | StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) { |
| 196 | value = Triple(scalar); |
| 197 | return StringRef(); |
| 198 | } |
| 199 | |
| 200 | size_t |
| 201 | SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size( |
| 202 | IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) { |
| 203 | return seq.size(); |
| 204 | } |
| 205 | |
| 206 | dsymutil::DebugMapObject & |
| 207 | SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element( |
| 208 | IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq, |
| 209 | size_t index) { |
| 210 | if (index >= seq.size()) { |
| 211 | seq.resize(new_size: index + 1); |
| 212 | seq[index].reset(p: new dsymutil::DebugMapObject); |
| 213 | } |
| 214 | return *seq[index]; |
| 215 | } |
| 216 | |
| 217 | void MappingTraits<dsymutil::DebugMap>::mapping(IO &io, |
| 218 | dsymutil::DebugMap &DM) { |
| 219 | io.mapRequired(Key: "triple" , Val&: DM.BinaryTriple); |
| 220 | io.mapOptional(Key: "binary-path" , Val&: DM.BinaryPath); |
| 221 | if (void *Ctxt = io.getContext()) |
| 222 | reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple; |
| 223 | io.mapOptional(Key: "objects" , Val&: DM.Objects); |
| 224 | } |
| 225 | |
| 226 | void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping( |
| 227 | IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) { |
| 228 | if (!DM) |
| 229 | DM.reset(p: new DebugMap()); |
| 230 | io.mapRequired(Key: "triple" , Val&: DM->BinaryTriple); |
| 231 | io.mapOptional(Key: "binary-path" , Val&: DM->BinaryPath); |
| 232 | if (void *Ctxt = io.getContext()) |
| 233 | reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple; |
| 234 | io.mapOptional(Key: "objects" , Val&: DM->Objects); |
| 235 | } |
| 236 | |
| 237 | MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO( |
| 238 | IO &io, dsymutil::DebugMapObject &Obj) { |
| 239 | Filename = Obj.Filename; |
| 240 | Timestamp = sys::toTimeT(TP: Obj.getTimestamp()); |
| 241 | Type = Obj.getType(); |
| 242 | Entries.reserve(n: Obj.Symbols.size()); |
| 243 | for (auto &Entry : Obj.Symbols) |
| 244 | Entries.push_back( |
| 245 | x: std::make_pair(x: std::string(Entry.getKey()), y&: Entry.getValue())); |
| 246 | llvm::sort(C&: Entries, Comp: llvm::less_first()); |
| 247 | } |
| 248 | |
| 249 | dsymutil::DebugMapObject |
| 250 | MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) { |
| 251 | const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext()); |
| 252 | SmallString<80> Path(Ctxt.PrependPath); |
| 253 | StringMap<uint64_t> SymbolAddresses; |
| 254 | |
| 255 | sys::path::append(path&: Path, a: Filename); |
| 256 | |
| 257 | auto ObjectEntry = Ctxt.BinHolder.getObjectEntry(Filename: Path); |
| 258 | if (!ObjectEntry) { |
| 259 | auto Err = ObjectEntry.takeError(); |
| 260 | WithColor::warning() << "Unable to open " << Path << " " |
| 261 | << toString(E: std::move(Err)) << '\n'; |
| 262 | } else { |
| 263 | auto Object = ObjectEntry->getObject(T: Ctxt.BinaryTriple); |
| 264 | if (!Object) { |
| 265 | auto Err = Object.takeError(); |
| 266 | WithColor::warning() << "Unable to open " << Path << " " |
| 267 | << toString(E: std::move(Err)) << '\n'; |
| 268 | } else { |
| 269 | for (const auto &Sym : Object->symbols()) { |
| 270 | Expected<uint64_t> AddressOrErr = Sym.getValue(); |
| 271 | if (!AddressOrErr) { |
| 272 | // TODO: Actually report errors helpfully. |
| 273 | consumeError(Err: AddressOrErr.takeError()); |
| 274 | continue; |
| 275 | } |
| 276 | Expected<StringRef> Name = Sym.getName(); |
| 277 | Expected<uint32_t> FlagsOrErr = Sym.getFlags(); |
| 278 | if (!Name || !FlagsOrErr || |
| 279 | (*FlagsOrErr & (SymbolRef::SF_Absolute | SymbolRef::SF_Common))) { |
| 280 | // TODO: Actually report errors helpfully. |
| 281 | if (!FlagsOrErr) |
| 282 | consumeError(Err: FlagsOrErr.takeError()); |
| 283 | if (!Name) |
| 284 | consumeError(Err: Name.takeError()); |
| 285 | continue; |
| 286 | } |
| 287 | SymbolAddresses[*Name] = *AddressOrErr; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | if (Path.ends_with(Suffix: ".dylib" )) { |
| 293 | // FIXME: find a more resilient way |
| 294 | Type = MachO::N_LIB; |
| 295 | } |
| 296 | dsymutil::DebugMapObject Res(Path, sys::toTimePoint(T: Timestamp), Type); |
| 297 | |
| 298 | for (auto &Entry : Entries) { |
| 299 | auto &Mapping = Entry.second; |
| 300 | std::optional<uint64_t> ObjAddress; |
| 301 | if (Mapping.ObjectAddress) |
| 302 | ObjAddress = *Mapping.ObjectAddress; |
| 303 | auto AddressIt = SymbolAddresses.find(Key: Entry.first); |
| 304 | if (AddressIt != SymbolAddresses.end()) |
| 305 | ObjAddress = AddressIt->getValue(); |
| 306 | Res.addSymbol(Name: Entry.first, ObjectAddress: ObjAddress, LinkedAddress: Mapping.BinaryAddress, Size: Mapping.Size); |
| 307 | } |
| 308 | return Res; |
| 309 | } |
| 310 | |
| 311 | } // end namespace yaml |
| 312 | } // end namespace llvm |
| 313 | |