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" |
Frederic Riss | 841b173 | 2015-12-11 17:50:37 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/Optional.h" |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 14 | #include "llvm/Object/MachO.h" |
| 15 | #include "llvm/Support/Path.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | |
| 18 | namespace { |
| 19 | using namespace llvm; |
| 20 | using namespace llvm::dsymutil; |
| 21 | using namespace llvm::object; |
| 22 | |
| 23 | class MachODebugMapParser { |
| 24 | public: |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 25 | MachODebugMapParser(StringRef BinaryPath, ArrayRef<std::string> Archs, |
| 26 | StringRef PathPrefix = "", bool Verbose = false) |
| 27 | : BinaryPath(BinaryPath), Archs(Archs.begin(), Archs.end()), |
| 28 | PathPrefix(PathPrefix), MainBinaryHolder(Verbose), |
| 29 | CurrentObjectHolder(Verbose), CurrentDebugMapObject(nullptr) {} |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 30 | |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 31 | /// \brief Parses and returns the DebugMaps of the input binary. |
| 32 | /// The binary contains multiple maps in case it is a universal |
| 33 | /// binary. |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 34 | /// \returns an error in case the provided BinaryPath doesn't exist |
| 35 | /// or isn't of a supported type. |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 36 | ErrorOr<std::vector<std::unique_ptr<DebugMap>>> parse(); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 37 | |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 38 | /// Walk the symbol table and dump it. |
| 39 | bool dumpStab(); |
| 40 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 41 | private: |
| 42 | std::string BinaryPath; |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 43 | SmallVector<StringRef, 1> Archs; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 44 | std::string PathPrefix; |
| 45 | |
Frederic Riss | 4398850 | 2015-01-05 21:29:28 +0000 | [diff] [blame] | 46 | /// Owns the MemoryBuffer for the main binary. |
| 47 | BinaryHolder MainBinaryHolder; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 48 | /// Map of the binary symbol addresses. |
| 49 | StringMap<uint64_t> MainBinarySymbolAddresses; |
Frederic Riss | 896b2c5 | 2014-12-16 20:21:34 +0000 | [diff] [blame] | 50 | StringRef MainBinaryStrings; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 51 | /// The constructed DebugMap. |
| 52 | std::unique_ptr<DebugMap> Result; |
| 53 | |
Frederic Riss | 4398850 | 2015-01-05 21:29:28 +0000 | [diff] [blame] | 54 | /// Owns the MemoryBuffer for the currently handled object file. |
| 55 | BinaryHolder CurrentObjectHolder; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 56 | /// Map of the currently processed object file symbol addresses. |
Frederic Riss | 841b173 | 2015-12-11 17:50:37 +0000 | [diff] [blame] | 57 | StringMap<Optional<uint64_t>> CurrentObjectAddresses; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 58 | /// Element of the debug map corresponfing to the current object file. |
| 59 | DebugMapObject *CurrentDebugMapObject; |
| 60 | |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 61 | /// Holds function info while function scope processing. |
| 62 | const char *CurrentFunctionName; |
| 63 | uint64_t CurrentFunctionAddress; |
| 64 | |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 65 | std::unique_ptr<DebugMap> parseOneBinary(const MachOObjectFile &MainBinary, |
| 66 | StringRef BinaryPath); |
| 67 | |
Pavel Labath | 62d7204 | 2016-11-09 11:43:52 +0000 | [diff] [blame] | 68 | void |
| 69 | switchToNewDebugMapObject(StringRef Filename, |
| 70 | sys::TimePoint<std::chrono::seconds> Timestamp); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 71 | void resetParserState(); |
| 72 | uint64_t getMainBinarySymbolAddress(StringRef Name); |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 73 | void loadMainBinarySymbols(const MachOObjectFile &MainBinary); |
| 74 | void loadCurrentObjectFileSymbols(const object::MachOObjectFile &Obj); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 75 | void handleStabSymbolTableEntry(uint32_t StringIndex, uint8_t Type, |
| 76 | uint8_t SectionIndex, uint16_t Flags, |
| 77 | uint64_t Value); |
| 78 | |
| 79 | template <typename STEType> void handleStabDebugMapEntry(const STEType &STE) { |
| 80 | handleStabSymbolTableEntry(STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc, |
| 81 | STE.n_value); |
| 82 | } |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 83 | |
| 84 | /// Dump the symbol table output header. |
| 85 | void dumpSymTabHeader(raw_ostream &OS, StringRef Arch); |
| 86 | |
| 87 | /// Dump the contents of nlist entries. |
| 88 | void dumpSymTabEntry(raw_ostream &OS, uint64_t Index, uint32_t StringIndex, |
| 89 | uint8_t Type, uint8_t SectionIndex, uint16_t Flags, |
| 90 | uint64_t Value); |
| 91 | |
| 92 | template <typename STEType> |
| 93 | void dumpSymTabEntry(raw_ostream &OS, uint64_t Index, const STEType &STE) { |
| 94 | dumpSymTabEntry(OS, Index, STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc, |
| 95 | STE.n_value); |
| 96 | } |
| 97 | void dumpOneBinaryStab(const MachOObjectFile &MainBinary, |
| 98 | StringRef BinaryPath); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | static void Warning(const Twine &Msg) { errs() << "warning: " + Msg + "\n"; } |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 102 | } // anonymous namespace |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 103 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 104 | /// Reset the parser state coresponding to the current object |
| 105 | /// file. This is to be called after an object file is finished |
| 106 | /// processing. |
| 107 | void MachODebugMapParser::resetParserState() { |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 108 | CurrentObjectAddresses.clear(); |
| 109 | CurrentDebugMapObject = nullptr; |
| 110 | } |
| 111 | |
| 112 | /// Create a new DebugMapObject. This function resets the state of the |
| 113 | /// parser that was referring to the last object file and sets |
| 114 | /// everything up to add symbols to the new one. |
Pavel Labath | 62d7204 | 2016-11-09 11:43:52 +0000 | [diff] [blame] | 115 | void MachODebugMapParser::switchToNewDebugMapObject( |
| 116 | StringRef Filename, sys::TimePoint<std::chrono::seconds> Timestamp) { |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 117 | resetParserState(); |
| 118 | |
| 119 | SmallString<80> Path(PathPrefix); |
| 120 | sys::path::append(Path, Filename); |
| 121 | |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 122 | auto MachOOrError = |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 123 | CurrentObjectHolder.GetFilesAs<MachOObjectFile>(Path, Timestamp); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 124 | if (auto Error = MachOOrError.getError()) { |
| 125 | Warning(Twine("cannot open debug object \"") + Path.str() + "\": " + |
| 126 | Error.message() + "\n"); |
| 127 | return; |
| 128 | } |
| 129 | |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 130 | auto ErrOrAchObj = |
| 131 | CurrentObjectHolder.GetAs<MachOObjectFile>(Result->getTriple()); |
| 132 | if (auto Err = ErrOrAchObj.getError()) { |
| 133 | return Warning(Twine("cannot open debug object \"") + Path.str() + "\": " + |
| 134 | Err.message() + "\n"); |
| 135 | } |
| 136 | |
Frederic Riss | 9ccfddc | 2015-07-22 23:24:00 +0000 | [diff] [blame] | 137 | CurrentDebugMapObject = &Result->addDebugMapObject(Path, Timestamp); |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 138 | loadCurrentObjectFileSymbols(*ErrOrAchObj); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 141 | static std::string getArchName(const object::MachOObjectFile &Obj) { |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 142 | Triple T = Obj.getArchTriple(); |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 143 | return T.getArchName(); |
| 144 | } |
| 145 | |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 146 | std::unique_ptr<DebugMap> |
| 147 | MachODebugMapParser::parseOneBinary(const MachOObjectFile &MainBinary, |
| 148 | StringRef BinaryPath) { |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 149 | loadMainBinarySymbols(MainBinary); |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 150 | Result = make_unique<DebugMap>(MainBinary.getArchTriple(), BinaryPath); |
Frederic Riss | 896b2c5 | 2014-12-16 20:21:34 +0000 | [diff] [blame] | 151 | MainBinaryStrings = MainBinary.getStringTableData(); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 152 | for (const SymbolRef &Symbol : MainBinary.symbols()) { |
| 153 | const DataRefImpl &DRI = Symbol.getRawDataRefImpl(); |
| 154 | if (MainBinary.is64Bit()) |
| 155 | handleStabDebugMapEntry(MainBinary.getSymbol64TableEntry(DRI)); |
| 156 | else |
| 157 | handleStabDebugMapEntry(MainBinary.getSymbolTableEntry(DRI)); |
| 158 | } |
| 159 | |
| 160 | resetParserState(); |
| 161 | return std::move(Result); |
| 162 | } |
| 163 | |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 164 | // Table that maps Darwin's Mach-O stab constants to strings to allow printing. |
| 165 | // llvm-nm has very similar code, the strings used here are however slightly |
| 166 | // different and part of the interface of dsymutil (some project's build-systems |
| 167 | // parse the ouptut of dsymutil -s), thus they shouldn't be changed. |
| 168 | struct DarwinStabName { |
| 169 | uint8_t NType; |
| 170 | const char *Name; |
| 171 | }; |
| 172 | |
| 173 | static const struct DarwinStabName DarwinStabNames[] = { |
| 174 | {MachO::N_GSYM, "N_GSYM"}, {MachO::N_FNAME, "N_FNAME"}, |
| 175 | {MachO::N_FUN, "N_FUN"}, {MachO::N_STSYM, "N_STSYM"}, |
| 176 | {MachO::N_LCSYM, "N_LCSYM"}, {MachO::N_BNSYM, "N_BNSYM"}, |
| 177 | {MachO::N_PC, "N_PC"}, {MachO::N_AST, "N_AST"}, |
| 178 | {MachO::N_OPT, "N_OPT"}, {MachO::N_RSYM, "N_RSYM"}, |
| 179 | {MachO::N_SLINE, "N_SLINE"}, {MachO::N_ENSYM, "N_ENSYM"}, |
| 180 | {MachO::N_SSYM, "N_SSYM"}, {MachO::N_SO, "N_SO"}, |
| 181 | {MachO::N_OSO, "N_OSO"}, {MachO::N_LSYM, "N_LSYM"}, |
| 182 | {MachO::N_BINCL, "N_BINCL"}, {MachO::N_SOL, "N_SOL"}, |
| 183 | {MachO::N_PARAMS, "N_PARAM"}, {MachO::N_VERSION, "N_VERS"}, |
| 184 | {MachO::N_OLEVEL, "N_OLEV"}, {MachO::N_PSYM, "N_PSYM"}, |
| 185 | {MachO::N_EINCL, "N_EINCL"}, {MachO::N_ENTRY, "N_ENTRY"}, |
| 186 | {MachO::N_LBRAC, "N_LBRAC"}, {MachO::N_EXCL, "N_EXCL"}, |
| 187 | {MachO::N_RBRAC, "N_RBRAC"}, {MachO::N_BCOMM, "N_BCOMM"}, |
| 188 | {MachO::N_ECOMM, "N_ECOMM"}, {MachO::N_ECOML, "N_ECOML"}, |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 189 | {MachO::N_LENG, "N_LENG"}, {0, nullptr}}; |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 190 | |
| 191 | static const char *getDarwinStabString(uint8_t NType) { |
| 192 | for (unsigned i = 0; DarwinStabNames[i].Name; i++) { |
| 193 | if (DarwinStabNames[i].NType == NType) |
| 194 | return DarwinStabNames[i].Name; |
| 195 | } |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 196 | return nullptr; |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void MachODebugMapParser::dumpSymTabHeader(raw_ostream &OS, StringRef Arch) { |
| 200 | OS << "-----------------------------------" |
| 201 | "-----------------------------------\n"; |
| 202 | OS << "Symbol table for: '" << BinaryPath << "' (" << Arch.data() << ")\n"; |
| 203 | OS << "-----------------------------------" |
| 204 | "-----------------------------------\n"; |
| 205 | OS << "Index n_strx n_type n_sect n_desc n_value\n"; |
| 206 | OS << "======== -------- ------------------ ------ ------ ----------------\n"; |
| 207 | } |
| 208 | |
| 209 | void MachODebugMapParser::dumpSymTabEntry(raw_ostream &OS, uint64_t Index, |
| 210 | uint32_t StringIndex, uint8_t Type, |
| 211 | uint8_t SectionIndex, uint16_t Flags, |
| 212 | uint64_t Value) { |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 213 | // Index |
| 214 | OS << '[' << format_decimal(Index, 6) << "] " |
| 215 | // n_strx |
| 216 | << format_hex_no_prefix(StringIndex, 8) << ' ' |
| 217 | // n_type... |
| 218 | << format_hex_no_prefix(Type, 2) << " ("; |
| 219 | |
| 220 | if (Type & MachO::N_STAB) |
| 221 | OS << left_justify(getDarwinStabString(Type), 13); |
| 222 | else { |
| 223 | if (Type & MachO::N_PEXT) |
| 224 | OS << "PEXT "; |
| 225 | else |
| 226 | OS << " "; |
| 227 | switch (Type & MachO::N_TYPE) { |
| 228 | case MachO::N_UNDF: // 0x0 undefined, n_sect == NO_SECT |
| 229 | OS << "UNDF"; |
| 230 | break; |
| 231 | case MachO::N_ABS: // 0x2 absolute, n_sect == NO_SECT |
| 232 | OS << "ABS "; |
| 233 | break; |
| 234 | case MachO::N_SECT: // 0xe defined in section number n_sect |
| 235 | OS << "SECT"; |
| 236 | break; |
| 237 | case MachO::N_PBUD: // 0xc prebound undefined (defined in a dylib) |
| 238 | OS << "PBUD"; |
| 239 | break; |
| 240 | case MachO::N_INDR: // 0xa indirect |
| 241 | OS << "INDR"; |
| 242 | break; |
| 243 | default: |
| 244 | OS << format_hex_no_prefix(Type, 2) << " "; |
| 245 | break; |
| 246 | } |
| 247 | if (Type & MachO::N_EXT) |
| 248 | OS << " EXT"; |
| 249 | else |
| 250 | OS << " "; |
| 251 | } |
| 252 | |
| 253 | OS << ") " |
| 254 | // n_sect |
| 255 | << format_hex_no_prefix(SectionIndex, 2) << " " |
| 256 | // n_desc |
| 257 | << format_hex_no_prefix(Flags, 4) << " " |
| 258 | // n_value |
| 259 | << format_hex_no_prefix(Value, 16); |
| 260 | |
| 261 | const char *Name = &MainBinaryStrings.data()[StringIndex]; |
| 262 | if (Name && Name[0]) |
| 263 | OS << " '" << Name << "'"; |
| 264 | |
| 265 | OS << "\n"; |
| 266 | } |
| 267 | |
| 268 | void MachODebugMapParser::dumpOneBinaryStab(const MachOObjectFile &MainBinary, |
| 269 | StringRef BinaryPath) { |
| 270 | loadMainBinarySymbols(MainBinary); |
| 271 | MainBinaryStrings = MainBinary.getStringTableData(); |
| 272 | raw_ostream &OS(llvm::outs()); |
| 273 | |
Frederic Riss | e20f288 | 2015-08-31 00:49:34 +0000 | [diff] [blame] | 274 | dumpSymTabHeader(OS, getArchName(MainBinary)); |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 275 | uint64_t Idx = 0; |
| 276 | for (const SymbolRef &Symbol : MainBinary.symbols()) { |
| 277 | const DataRefImpl &DRI = Symbol.getRawDataRefImpl(); |
| 278 | if (MainBinary.is64Bit()) |
| 279 | dumpSymTabEntry(OS, Idx, MainBinary.getSymbol64TableEntry(DRI)); |
| 280 | else |
| 281 | dumpSymTabEntry(OS, Idx, MainBinary.getSymbolTableEntry(DRI)); |
| 282 | Idx++; |
| 283 | } |
| 284 | |
| 285 | OS << "\n\n"; |
| 286 | resetParserState(); |
| 287 | } |
| 288 | |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 289 | static bool shouldLinkArch(SmallVectorImpl<StringRef> &Archs, StringRef Arch) { |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 290 | if (Archs.empty() || is_contained(Archs, "all") || is_contained(Archs, "*")) |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 291 | return true; |
| 292 | |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 293 | if (Arch.startswith("arm") && Arch != "arm64" && is_contained(Archs, "arm")) |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 294 | return true; |
| 295 | |
Frederic Riss | 5af2c00 | 2016-05-09 06:01:12 +0000 | [diff] [blame] | 296 | SmallString<16> ArchName = Arch; |
| 297 | if (Arch.startswith("thumb")) |
| 298 | ArchName = ("arm" + Arch.substr(5)).str(); |
| 299 | |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 300 | return is_contained(Archs, ArchName); |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 303 | bool MachODebugMapParser::dumpStab() { |
| 304 | auto MainBinOrError = |
| 305 | MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath); |
| 306 | if (auto Error = MainBinOrError.getError()) { |
| 307 | llvm::errs() << "Cannot get '" << BinaryPath |
| 308 | << "' as MachO file: " << Error.message() << "\n"; |
| 309 | return false; |
| 310 | } |
| 311 | |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 312 | for (const auto *Binary : *MainBinOrError) |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 313 | if (shouldLinkArch(Archs, Binary->getArchTriple().getArchName())) |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 314 | dumpOneBinaryStab(*Binary, BinaryPath); |
| 315 | |
| 316 | return true; |
| 317 | } |
| 318 | |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 319 | /// This main parsing routine tries to open the main binary and if |
| 320 | /// successful iterates over the STAB entries. The real parsing is |
| 321 | /// done in handleStabSymbolTableEntry. |
| 322 | ErrorOr<std::vector<std::unique_ptr<DebugMap>>> MachODebugMapParser::parse() { |
| 323 | auto MainBinOrError = |
| 324 | MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath); |
| 325 | if (auto Error = MainBinOrError.getError()) |
| 326 | return Error; |
| 327 | |
| 328 | std::vector<std::unique_ptr<DebugMap>> Results; |
| 329 | for (const auto *Binary : *MainBinOrError) |
Tim Northover | 9e8eb41 | 2016-04-22 23:21:13 +0000 | [diff] [blame] | 330 | if (shouldLinkArch(Archs, Binary->getArchTriple().getArchName())) |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 331 | Results.push_back(parseOneBinary(*Binary, BinaryPath)); |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 332 | |
| 333 | return std::move(Results); |
| 334 | } |
| 335 | |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 336 | /// Interpret the STAB entries to fill the DebugMap. |
| 337 | void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex, |
| 338 | uint8_t Type, |
| 339 | uint8_t SectionIndex, |
| 340 | uint16_t Flags, |
| 341 | uint64_t Value) { |
| 342 | if (!(Type & MachO::N_STAB)) |
| 343 | return; |
| 344 | |
Frederic Riss | 896b2c5 | 2014-12-16 20:21:34 +0000 | [diff] [blame] | 345 | const char *Name = &MainBinaryStrings.data()[StringIndex]; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 346 | |
| 347 | // An N_OSO entry represents the start of a new object file description. |
Pavel Labath | 62d7204 | 2016-11-09 11:43:52 +0000 | [diff] [blame] | 348 | if (Type == MachO::N_OSO) |
| 349 | return switchToNewDebugMapObject(Name, sys::toTimePoint(Value)); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 350 | |
| 351 | // If the last N_OSO object file wasn't found, |
| 352 | // CurrentDebugMapObject will be null. Do not update anything |
| 353 | // until we find the next valid N_OSO entry. |
| 354 | if (!CurrentDebugMapObject) |
| 355 | return; |
| 356 | |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 357 | uint32_t Size = 0; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 358 | switch (Type) { |
| 359 | case MachO::N_GSYM: |
| 360 | // This is a global variable. We need to query the main binary |
| 361 | // symbol table to find its address as it might not be in the |
| 362 | // debug map (for common symbols). |
| 363 | Value = getMainBinarySymbolAddress(Name); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 364 | break; |
| 365 | case MachO::N_FUN: |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 366 | // Functions are scopes in STABS. They have an end marker that |
| 367 | // contains the function size. |
| 368 | if (Name[0] == '\0') { |
| 369 | Size = Value; |
| 370 | Value = CurrentFunctionAddress; |
| 371 | Name = CurrentFunctionName; |
| 372 | break; |
| 373 | } else { |
| 374 | CurrentFunctionName = Name; |
| 375 | CurrentFunctionAddress = Value; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 376 | return; |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 377 | } |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 378 | case MachO::N_STSYM: |
| 379 | break; |
| 380 | default: |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | auto ObjectSymIt = CurrentObjectAddresses.find(Name); |
| 385 | if (ObjectSymIt == CurrentObjectAddresses.end()) |
| 386 | return Warning("could not find object file symbol for symbol " + |
| 387 | Twine(Name)); |
Frederic Riss | d8c33dc | 2016-01-31 04:29:22 +0000 | [diff] [blame] | 388 | if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value, |
Frederic Riss | 912d0f1 | 2015-03-15 01:29:30 +0000 | [diff] [blame] | 389 | Size)) |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 390 | return Warning(Twine("failed to insert symbol '") + Name + |
| 391 | "' in the debug map."); |
| 392 | } |
| 393 | |
| 394 | /// Load the current object file symbols into CurrentObjectAddresses. |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 395 | void MachODebugMapParser::loadCurrentObjectFileSymbols( |
| 396 | const object::MachOObjectFile &Obj) { |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 397 | CurrentObjectAddresses.clear(); |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 398 | |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 399 | for (auto Sym : Obj.symbols()) { |
Rafael Espindola | be8b0ea | 2015-07-07 17:12:59 +0000 | [diff] [blame] | 400 | uint64_t Addr = Sym.getValue(); |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 401 | Expected<StringRef> Name = Sym.getName(); |
| 402 | if (!Name) { |
| 403 | // TODO: Actually report errors helpfully. |
| 404 | consumeError(Name.takeError()); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 405 | continue; |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 406 | } |
Frederic Riss | 6c8521a | 2016-01-31 04:29:34 +0000 | [diff] [blame] | 407 | // The value of some categories of symbols isn't meaningful. For |
| 408 | // example common symbols store their size in the value field, not |
| 409 | // their address. Absolute symbols have a fixed address that can |
| 410 | // conflict with standard symbols. These symbols (especially the |
| 411 | // common ones), might still be referenced by relocations. These |
| 412 | // relocations will use the symbol itself, and won't need an |
| 413 | // object file address. The object file address field is optional |
| 414 | // in the DebugMap, leave it unassigned for these symbols. |
| 415 | if (Sym.getFlags() & (SymbolRef::SF_Absolute | SymbolRef::SF_Common)) |
Frederic Riss | 841b173 | 2015-12-11 17:50:37 +0000 | [diff] [blame] | 416 | CurrentObjectAddresses[*Name] = None; |
| 417 | else |
| 418 | CurrentObjectAddresses[*Name] = Addr; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | /// Lookup a symbol address in the main binary symbol table. The |
| 423 | /// parser only needs to query common symbols, thus not every symbol's |
| 424 | /// address is available through this function. |
| 425 | uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) { |
| 426 | auto Sym = MainBinarySymbolAddresses.find(Name); |
| 427 | if (Sym == MainBinarySymbolAddresses.end()) |
Rafael Espindola | be8b0ea | 2015-07-07 17:12:59 +0000 | [diff] [blame] | 428 | return 0; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 429 | return Sym->second; |
| 430 | } |
| 431 | |
| 432 | /// Load the interesting main binary symbols' addresses into |
| 433 | /// MainBinarySymbolAddresses. |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 434 | void MachODebugMapParser::loadMainBinarySymbols( |
| 435 | const MachOObjectFile &MainBinary) { |
Frederic Riss | 4398850 | 2015-01-05 21:29:28 +0000 | [diff] [blame] | 436 | section_iterator Section = MainBinary.section_end(); |
Frederic Riss | eb85c8f | 2015-07-24 06:41:11 +0000 | [diff] [blame] | 437 | MainBinarySymbolAddresses.clear(); |
Frederic Riss | 4398850 | 2015-01-05 21:29:28 +0000 | [diff] [blame] | 438 | for (const auto &Sym : MainBinary.symbols()) { |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 439 | Expected<SymbolRef::Type> TypeOrErr = Sym.getType(); |
| 440 | if (!TypeOrErr) { |
| 441 | // TODO: Actually report errors helpfully. |
| 442 | consumeError(TypeOrErr.takeError()); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 443 | continue; |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 444 | } |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 445 | SymbolRef::Type Type = *TypeOrErr; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 446 | // Skip undefined and STAB entries. |
David Majnemer | f5c0689 | 2016-10-31 17:11:23 +0000 | [diff] [blame] | 447 | if ((Type == SymbolRef::ST_Debug) || (Type == SymbolRef::ST_Unknown)) |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 448 | continue; |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 449 | // The only symbols of interest are the global variables. These |
| 450 | // are the only ones that need to be queried because the address |
| 451 | // of common data won't be described in the debug map. All other |
| 452 | // addresses should be fetched for the debug map. |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 453 | if (!(Sym.getFlags() & SymbolRef::SF_Global)) |
| 454 | continue; |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 455 | Expected<section_iterator> SectionOrErr = Sym.getSection(); |
| 456 | if (!SectionOrErr) { |
| 457 | // TODO: Actually report errors helpfully. |
| 458 | consumeError(SectionOrErr.takeError()); |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 459 | continue; |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 460 | } |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 461 | Section = *SectionOrErr; |
| 462 | if (Section == MainBinary.section_end() || Section->isText()) |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 463 | continue; |
Rafael Espindola | be8b0ea | 2015-07-07 17:12:59 +0000 | [diff] [blame] | 464 | uint64_t Addr = Sym.getValue(); |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 465 | Expected<StringRef> NameOrErr = Sym.getName(); |
| 466 | if (!NameOrErr) { |
| 467 | // TODO: Actually report errors helpfully. |
| 468 | consumeError(NameOrErr.takeError()); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 469 | continue; |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 470 | } |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 471 | StringRef Name = *NameOrErr; |
| 472 | if (Name.size() == 0 || Name[0] == '\0') |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 473 | continue; |
| 474 | MainBinarySymbolAddresses[Name] = Addr; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | namespace llvm { |
| 479 | namespace dsymutil { |
Frederic Riss | 4dd3e0c | 2015-08-05 18:27:44 +0000 | [diff] [blame] | 480 | llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>> |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 481 | parseDebugMap(StringRef InputFile, ArrayRef<std::string> Archs, |
| 482 | StringRef PrependPath, bool Verbose, bool InputIsYAML) { |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 483 | if (!InputIsYAML) { |
Frederic Riss | ae0d436 | 2015-08-05 22:33:28 +0000 | [diff] [blame] | 484 | MachODebugMapParser Parser(InputFile, Archs, PrependPath, Verbose); |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 485 | return Parser.parse(); |
| 486 | } else { |
Frederic Riss | 4d0ba66 | 2015-06-05 20:27:04 +0000 | [diff] [blame] | 487 | return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose); |
Frederic Riss | 90e0bd9 | 2015-06-03 20:29:24 +0000 | [diff] [blame] | 488 | } |
Frederic Riss | 231f714 | 2014-12-12 17:31:24 +0000 | [diff] [blame] | 489 | } |
Frederic Riss | 5ba01d6 | 2015-08-31 00:29:09 +0000 | [diff] [blame] | 490 | |
| 491 | bool dumpStab(StringRef InputFile, ArrayRef<std::string> Archs, |
| 492 | StringRef PrependPath) { |
| 493 | MachODebugMapParser Parser(InputFile, Archs, PrependPath, false); |
| 494 | return Parser.dumpStab(); |
| 495 | } |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 496 | } // namespace dsymutil |
| 497 | } // namespace llvm |