Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 1 | //===- tools/dsymutil/MachODebugMapParser.cpp - Parse STABS debug maps ----===// |
| 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 | |
Frederic Riss | 4398850 | 2015-01-05 21:29:28 +0000 | [diff] [blame] | 10 | #include "BinaryHolder.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 11 | #include "DebugMap.h" |
| 12 | #include "dsymutil.h" |
| 13 | #include "llvm/Object/MachO.h" |
| 14 | #include "llvm/Support/Path.h" |
| 15 | #include "llvm/Support/raw_ostream.h" |
| 16 | |
| 17 | namespace { |
| 18 | using namespace llvm; |
| 19 | using namespace llvm::dsymutil; |
| 20 | using namespace llvm::object; |
| 21 | |
| 22 | class MachODebugMapParser { |
| 23 | public: |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 24 | MachODebugMapParser(StringRef BinaryPath, ArrayRef<std::string> Archs, |
| 25 | StringRef PathPrefix = "", bool Verbose = false) |
| 26 | : BinaryPath(BinaryPath), Archs(Archs.begin(), Archs.end()), |
| 27 | PathPrefix(PathPrefix), MainBinaryHolder(Verbose), |
| 28 | CurrentObjectHolder(Verbose), CurrentDebugMapObject(nullptr) {} |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 29 | |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 30 | /// \brief Parses and returns the DebugMaps of the input binary. |
| 31 | /// The binary contains multiple maps in case it is a universal |
| 32 | /// binary. |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 33 | /// \returns an error in case the provided BinaryPath doesn't exist |
| 34 | /// or isn't of a supported type. |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 35 | ErrorOr<std::vector<std::unique_ptr<DebugMap>>> parse(); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 36 | |
| 37 | private: |
| 38 | std::string BinaryPath; |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 39 | SmallVector<StringRef, 1> Archs; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 40 | std::string PathPrefix; |
| 41 | |
Frederic Riss | 4398850 | 2015-01-05 21:29:28 +0000 | [diff] [blame] | 42 | /// Owns the MemoryBuffer for the main binary. |
| 43 | BinaryHolder MainBinaryHolder; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 44 | /// Map of the binary symbol addresses. |
| 45 | StringMap<uint64_t> MainBinarySymbolAddresses; |
Frederic Riss | 896b2c5 | 2014-12-16 20:21:34 +0000 | [diff] [blame] | 46 | StringRef MainBinaryStrings; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 47 | /// The constructed DebugMap. |
| 48 | std::unique_ptr<DebugMap> Result; |
| 49 | |
Frederic Riss | 4398850 | 2015-01-05 21:29:28 +0000 | [diff] [blame] | 50 | /// Owns the MemoryBuffer for the currently handled object file. |
| 51 | BinaryHolder CurrentObjectHolder; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 52 | /// Map of the currently processed object file symbol addresses. |
| 53 | StringMap<uint64_t> CurrentObjectAddresses; |
| 54 | /// Element of the debug map corresponfing to the current object file. |
| 55 | DebugMapObject *CurrentDebugMapObject; |
| 56 | |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 57 | /// Holds function info while function scope processing. |
| 58 | const char *CurrentFunctionName; |
| 59 | uint64_t CurrentFunctionAddress; |
| 60 | |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 61 | std::unique_ptr<DebugMap> parseOneBinary(const MachOObjectFile &MainBinary, |
| 62 | StringRef BinaryPath); |
| 63 | |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 64 | void switchToNewDebugMapObject(StringRef Filename, sys::TimeValue Timestamp); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 65 | void resetParserState(); |
| 66 | uint64_t getMainBinarySymbolAddress(StringRef Name); |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 67 | void loadMainBinarySymbols(const MachOObjectFile &MainBinary); |
| 68 | void loadCurrentObjectFileSymbols(const object::MachOObjectFile &Obj); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 69 | void handleStabSymbolTableEntry(uint32_t StringIndex, uint8_t Type, |
| 70 | uint8_t SectionIndex, uint16_t Flags, |
| 71 | uint64_t Value); |
| 72 | |
| 73 | template <typename STEType> void handleStabDebugMapEntry(const STEType &STE) { |
| 74 | handleStabSymbolTableEntry(STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc, |
| 75 | STE.n_value); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | static void Warning(const Twine &Msg) { errs() << "warning: " + Msg + "\n"; } |
| 80 | } |
| 81 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 82 | /// Reset the parser state coresponding to the current object |
| 83 | /// file. This is to be called after an object file is finished |
| 84 | /// processing. |
| 85 | void MachODebugMapParser::resetParserState() { |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 86 | CurrentObjectAddresses.clear(); |
| 87 | CurrentDebugMapObject = nullptr; |
| 88 | } |
| 89 | |
| 90 | /// Create a new DebugMapObject. This function resets the state of the |
| 91 | /// parser that was referring to the last object file and sets |
| 92 | /// everything up to add symbols to the new one. |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 93 | void MachODebugMapParser::switchToNewDebugMapObject(StringRef Filename, |
| 94 | sys::TimeValue Timestamp) { |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 95 | resetParserState(); |
| 96 | |
| 97 | SmallString<80> Path(PathPrefix); |
| 98 | sys::path::append(Path, Filename); |
| 99 | |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 100 | auto MachOOrError = |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 101 | CurrentObjectHolder.GetFilesAs<MachOObjectFile>(Path, Timestamp); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 102 | if (auto Error = MachOOrError.getError()) { |
| 103 | Warning(Twine("cannot open debug object \"") + Path.str() + "\": " + |
| 104 | Error.message() + "\n"); |
| 105 | return; |
| 106 | } |
| 107 | |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 108 | auto ErrOrAchObj = |
| 109 | CurrentObjectHolder.GetAs<MachOObjectFile>(Result->getTriple()); |
| 110 | if (auto Err = ErrOrAchObj.getError()) { |
| 111 | return Warning(Twine("cannot open debug object \"") + Path.str() + "\": " + |
| 112 | Err.message() + "\n"); |
| 113 | } |
| 114 | |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 115 | CurrentDebugMapObject = &Result->addDebugMapObject(Path, Timestamp); |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 116 | loadCurrentObjectFileSymbols(*ErrOrAchObj); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 119 | std::unique_ptr<DebugMap> |
| 120 | MachODebugMapParser::parseOneBinary(const MachOObjectFile &MainBinary, |
| 121 | StringRef BinaryPath) { |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 122 | loadMainBinarySymbols(MainBinary); |
Frederic Riss | 65f0abf | 2015-07-24 06:41:04 +0000 | [diff] [blame] | 123 | Result = make_unique<DebugMap>(BinaryHolder::getTriple(MainBinary)); |
Frederic Riss | 896b2c5 | 2014-12-16 20:21:34 +0000 | [diff] [blame] | 124 | MainBinaryStrings = MainBinary.getStringTableData(); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 125 | for (const SymbolRef &Symbol : MainBinary.symbols()) { |
| 126 | const DataRefImpl &DRI = Symbol.getRawDataRefImpl(); |
| 127 | if (MainBinary.is64Bit()) |
| 128 | handleStabDebugMapEntry(MainBinary.getSymbol64TableEntry(DRI)); |
| 129 | else |
| 130 | handleStabDebugMapEntry(MainBinary.getSymbolTableEntry(DRI)); |
| 131 | } |
| 132 | |
| 133 | resetParserState(); |
| 134 | return std::move(Result); |
| 135 | } |
| 136 | |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 137 | static bool shouldLinkArch(SmallVectorImpl<StringRef> &Archs, StringRef Arch) { |
| 138 | if (Archs.empty() || |
| 139 | std::find(Archs.begin(), Archs.end(), "all") != Archs.end() || |
| 140 | std::find(Archs.begin(), Archs.end(), "*") != Archs.end()) |
| 141 | return true; |
| 142 | |
| 143 | if (Arch.startswith("arm") && Arch != "arm64" && |
| 144 | std::find(Archs.begin(), Archs.end(), "arm") != Archs.end()) |
| 145 | return true; |
| 146 | |
| 147 | return std::find(Archs.begin(), Archs.end(), Arch) != Archs.end(); |
| 148 | } |
| 149 | |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 150 | /// This main parsing routine tries to open the main binary and if |
| 151 | /// successful iterates over the STAB entries. The real parsing is |
| 152 | /// done in handleStabSymbolTableEntry. |
| 153 | ErrorOr<std::vector<std::unique_ptr<DebugMap>>> MachODebugMapParser::parse() { |
| 154 | auto MainBinOrError = |
| 155 | MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath); |
| 156 | if (auto Error = MainBinOrError.getError()) |
| 157 | return Error; |
| 158 | |
| 159 | std::vector<std::unique_ptr<DebugMap>> Results; |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 160 | Triple T; |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 161 | for (const auto *Binary : *MainBinOrError) |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 162 | if (shouldLinkArch(Archs, Binary->getArch(nullptr, &T).getArchName())) |
| 163 | Results.push_back(parseOneBinary(*Binary, BinaryPath)); |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 164 | |
| 165 | return std::move(Results); |
| 166 | } |
| 167 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 168 | /// Interpret the STAB entries to fill the DebugMap. |
| 169 | void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex, |
| 170 | uint8_t Type, |
| 171 | uint8_t SectionIndex, |
| 172 | uint16_t Flags, |
| 173 | uint64_t Value) { |
| 174 | if (!(Type & MachO::N_STAB)) |
| 175 | return; |
| 176 | |
Frederic Riss | 896b2c5 | 2014-12-16 20:21:34 +0000 | [diff] [blame] | 177 | const char *Name = &MainBinaryStrings.data()[StringIndex]; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 178 | |
| 179 | // An N_OSO entry represents the start of a new object file description. |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 180 | if (Type == MachO::N_OSO) { |
| 181 | sys::TimeValue Timestamp; |
| 182 | Timestamp.fromEpochTime(Value); |
| 183 | return switchToNewDebugMapObject(Name, Timestamp); |
| 184 | } |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 185 | |
| 186 | // If the last N_OSO object file wasn't found, |
| 187 | // CurrentDebugMapObject will be null. Do not update anything |
| 188 | // until we find the next valid N_OSO entry. |
| 189 | if (!CurrentDebugMapObject) |
| 190 | return; |
| 191 | |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 192 | uint32_t Size = 0; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 193 | switch (Type) { |
| 194 | case MachO::N_GSYM: |
| 195 | // This is a global variable. We need to query the main binary |
| 196 | // symbol table to find its address as it might not be in the |
| 197 | // debug map (for common symbols). |
| 198 | Value = getMainBinarySymbolAddress(Name); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 199 | break; |
| 200 | case MachO::N_FUN: |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 201 | // Functions are scopes in STABS. They have an end marker that |
| 202 | // contains the function size. |
| 203 | if (Name[0] == '\0') { |
| 204 | Size = Value; |
| 205 | Value = CurrentFunctionAddress; |
| 206 | Name = CurrentFunctionName; |
| 207 | break; |
| 208 | } else { |
| 209 | CurrentFunctionName = Name; |
| 210 | CurrentFunctionAddress = Value; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 211 | return; |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 212 | } |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 213 | case MachO::N_STSYM: |
| 214 | break; |
| 215 | default: |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | auto ObjectSymIt = CurrentObjectAddresses.find(Name); |
| 220 | if (ObjectSymIt == CurrentObjectAddresses.end()) |
| 221 | return Warning("could not find object file symbol for symbol " + |
| 222 | Twine(Name)); |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 223 | if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value, |
| 224 | Size)) |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 225 | return Warning(Twine("failed to insert symbol '") + Name + |
| 226 | "' in the debug map."); |
| 227 | } |
| 228 | |
| 229 | /// Load the current object file symbols into CurrentObjectAddresses. |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 230 | void MachODebugMapParser::loadCurrentObjectFileSymbols( |
| 231 | const object::MachOObjectFile &Obj) { |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 232 | CurrentObjectAddresses.clear(); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 233 | |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 234 | for (auto Sym : Obj.symbols()) { |
Rafael Espindola | be8b0ea | 2015-07-07 17:12:59 +0000 | [diff] [blame] | 235 | uint64_t Addr = Sym.getValue(); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 236 | ErrorOr<StringRef> Name = Sym.getName(); |
| 237 | if (!Name) |
| 238 | continue; |
| 239 | CurrentObjectAddresses[*Name] = Addr; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | /// Lookup a symbol address in the main binary symbol table. The |
| 244 | /// parser only needs to query common symbols, thus not every symbol's |
| 245 | /// address is available through this function. |
| 246 | uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) { |
| 247 | auto Sym = MainBinarySymbolAddresses.find(Name); |
| 248 | if (Sym == MainBinarySymbolAddresses.end()) |
Rafael Espindola | be8b0ea | 2015-07-07 17:12:59 +0000 | [diff] [blame] | 249 | return 0; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 250 | return Sym->second; |
| 251 | } |
| 252 | |
| 253 | /// Load the interesting main binary symbols' addresses into |
| 254 | /// MainBinarySymbolAddresses. |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 255 | void MachODebugMapParser::loadMainBinarySymbols( |
| 256 | const MachOObjectFile &MainBinary) { |
Frederic Riss | 4398850 | 2015-01-05 21:29:28 +0000 | [diff] [blame] | 257 | section_iterator Section = MainBinary.section_end(); |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 258 | MainBinarySymbolAddresses.clear(); |
Frederic Riss | 4398850 | 2015-01-05 21:29:28 +0000 | [diff] [blame] | 259 | for (const auto &Sym : MainBinary.symbols()) { |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 260 | SymbolRef::Type Type = Sym.getType(); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 261 | // Skip undefined and STAB entries. |
Rafael Espindola | 2fa80cc | 2015-06-26 12:18:49 +0000 | [diff] [blame] | 262 | if ((Type & SymbolRef::ST_Debug) || (Type & SymbolRef::ST_Unknown)) |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 263 | continue; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 264 | // The only symbols of interest are the global variables. These |
| 265 | // are the only ones that need to be queried because the address |
| 266 | // of common data won't be described in the debug map. All other |
| 267 | // addresses should be fetched for the debug map. |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 268 | if (!(Sym.getFlags() & SymbolRef::SF_Global)) |
| 269 | continue; |
| 270 | ErrorOr<section_iterator> SectionOrErr = Sym.getSection(); |
| 271 | if (!SectionOrErr) |
| 272 | continue; |
| 273 | Section = *SectionOrErr; |
| 274 | if (Section == MainBinary.section_end() || Section->isText()) |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 275 | continue; |
Rafael Espindola | be8b0ea | 2015-07-07 17:12:59 +0000 | [diff] [blame] | 276 | uint64_t Addr = Sym.getValue(); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 277 | ErrorOr<StringRef> NameOrErr = Sym.getName(); |
| 278 | if (!NameOrErr) |
| 279 | continue; |
| 280 | StringRef Name = *NameOrErr; |
| 281 | if (Name.size() == 0 || Name[0] == '\0') |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 282 | continue; |
| 283 | MainBinarySymbolAddresses[Name] = Addr; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | namespace llvm { |
| 288 | namespace dsymutil { |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 289 | llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>> |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 290 | parseDebugMap(StringRef InputFile, ArrayRef<std::string> Archs, |
| 291 | StringRef PrependPath, bool Verbose, bool InputIsYAML) { |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 292 | if (!InputIsYAML) { |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 293 | MachODebugMapParser Parser(InputFile, Archs, PrependPath, Verbose); |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 294 | return Parser.parse(); |
| 295 | } else { |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 296 | return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose); |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 297 | } |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |