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