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