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