blob: 8ff7e22da228f9c01800a9a642544bcd686e6af2 [file] [log] [blame]
Frederic Riss31e081e2014-12-12 17:31:24 +00001//===- tools/dsymutil/MachODebugMapParser.cpp - Parse STABS debug maps ----===//
2//
Jonas Devlieghere928fea22018-06-27 16:13:40 +00003// The LLVM Compiler Infrastructure
Frederic Riss31e081e2014-12-12 17:31:24 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Frederic Riss5a0743e2015-01-05 21:29:28 +000010#include "BinaryHolder.h"
Frederic Riss31e081e2014-12-12 17:31:24 +000011#include "DebugMap.h"
Jonas Devlieghere21cda302018-03-14 09:34:54 +000012#include "MachOUtils.h"
Frederic Rissba161512015-12-11 17:50:37 +000013#include "llvm/ADT/Optional.h"
Frederic Riss31e081e2014-12-12 17:31:24 +000014#include "llvm/Object/MachO.h"
15#include "llvm/Support/Path.h"
Jonas Devlieghere5baab4c2018-04-14 21:36:42 +000016#include "llvm/Support/WithColor.h"
Frederic Riss31e081e2014-12-12 17:31:24 +000017#include "llvm/Support/raw_ostream.h"
18
19namespace {
20using namespace llvm;
21using namespace llvm::dsymutil;
22using namespace llvm::object;
23
24class MachODebugMapParser {
25public:
Frederic Riss1b709aa2015-08-05 22:33:28 +000026 MachODebugMapParser(StringRef BinaryPath, ArrayRef<std::string> Archs,
Jonas Devlieghere67eb8fd2018-04-02 10:40:43 +000027 StringRef PathPrefix = "",
28 bool PaperTrailWarnings = false, bool Verbose = false)
Frederic Riss1b709aa2015-08-05 22:33:28 +000029 : BinaryPath(BinaryPath), Archs(Archs.begin(), Archs.end()),
Jonas Devlieghere67eb8fd2018-04-02 10:40:43 +000030 PathPrefix(PathPrefix), PaperTrailWarnings(PaperTrailWarnings),
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +000031 BinHolder(Verbose), CurrentDebugMapObject(nullptr) {}
Frederic Riss31e081e2014-12-12 17:31:24 +000032
Jonas Devlieghere92a76c52018-02-22 11:43:43 +000033 /// Parses and returns the DebugMaps of the input binary. The binary contains
34 /// multiple maps in case it is a universal binary.
Frederic Riss31e081e2014-12-12 17:31:24 +000035 /// \returns an error in case the provided BinaryPath doesn't exist
36 /// or isn't of a supported type.
Frederic Riss7a425782015-08-05 18:27:44 +000037 ErrorOr<std::vector<std::unique_ptr<DebugMap>>> parse();
Frederic Riss31e081e2014-12-12 17:31:24 +000038
Frederic Rissa8a34c52015-08-31 00:29:09 +000039 /// Walk the symbol table and dump it.
40 bool dumpStab();
41
Frederic Riss31e081e2014-12-12 17:31:24 +000042private:
43 std::string BinaryPath;
Frederic Riss1b709aa2015-08-05 22:33:28 +000044 SmallVector<StringRef, 1> Archs;
Frederic Riss31e081e2014-12-12 17:31:24 +000045 std::string PathPrefix;
Jonas Devlieghere67eb8fd2018-04-02 10:40:43 +000046 bool PaperTrailWarnings;
Frederic Riss31e081e2014-12-12 17:31:24 +000047
Frederic Riss5a0743e2015-01-05 21:29:28 +000048 /// Owns the MemoryBuffer for the main binary.
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +000049 BinaryHolder BinHolder;
Frederic Riss31e081e2014-12-12 17:31:24 +000050 /// Map of the binary symbol addresses.
51 StringMap<uint64_t> MainBinarySymbolAddresses;
Frederic Rissaabd6c12014-12-16 20:21:34 +000052 StringRef MainBinaryStrings;
Frederic Riss31e081e2014-12-12 17:31:24 +000053 /// The constructed DebugMap.
54 std::unique_ptr<DebugMap> Result;
55
Frederic Riss31e081e2014-12-12 17:31:24 +000056 /// Map of the currently processed object file symbol addresses.
Frederic Rissba161512015-12-11 17:50:37 +000057 StringMap<Optional<uint64_t>> CurrentObjectAddresses;
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +000058 /// Element of the debug map corresponding to the current object file.
Frederic Riss31e081e2014-12-12 17:31:24 +000059 DebugMapObject *CurrentDebugMapObject;
60
Frederic Risse7a35642015-03-15 01:29:30 +000061 /// Holds function info while function scope processing.
62 const char *CurrentFunctionName;
63 uint64_t CurrentFunctionAddress;
64
Frederic Riss7a425782015-08-05 18:27:44 +000065 std::unique_ptr<DebugMap> parseOneBinary(const MachOObjectFile &MainBinary,
66 StringRef BinaryPath);
67
Pavel Labath73ce0c02016-11-09 11:43:52 +000068 void
69 switchToNewDebugMapObject(StringRef Filename,
70 sys::TimePoint<std::chrono::seconds> Timestamp);
Frederic Riss31e081e2014-12-12 17:31:24 +000071 void resetParserState();
72 uint64_t getMainBinarySymbolAddress(StringRef Name);
Jonas Devlieghere42371bc2017-09-26 08:17:28 +000073 std::vector<StringRef> getMainBinarySymbolNames(uint64_t Value);
Frederic Rissfa7f7bd2015-07-24 06:41:11 +000074 void loadMainBinarySymbols(const MachOObjectFile &MainBinary);
75 void loadCurrentObjectFileSymbols(const object::MachOObjectFile &Obj);
Frederic Riss31e081e2014-12-12 17:31:24 +000076 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 Rissa8a34c52015-08-31 00:29:09 +000084
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 Devlieghere21cda302018-03-14 09:34:54 +0000100
101 void Warning(const Twine &Msg, StringRef File = StringRef()) {
Jonas Devlieghere5baab4c2018-04-14 21:36:42 +0000102 WithColor::warning() << "("
103 << MachOUtils::getArchName(
104 Result->getTriple().getArchName())
105 << ") " << File << " " << Msg << "\n";
Jonas Devlieghere67eb8fd2018-04-02 10:40:43 +0000106
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 Devlieghere21cda302018-03-14 09:34:54 +0000113 }
Frederic Riss31e081e2014-12-12 17:31:24 +0000114};
115
Hans Wennborg4d651e42015-10-06 23:24:35 +0000116} // anonymous namespace
Frederic Riss31e081e2014-12-12 17:31:24 +0000117
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000118/// Reset the parser state corresponding to the current object
Frederic Riss31e081e2014-12-12 17:31:24 +0000119/// file. This is to be called after an object file is finished
120/// processing.
121void MachODebugMapParser::resetParserState() {
Frederic Riss31e081e2014-12-12 17:31:24 +0000122 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 Labath73ce0c02016-11-09 11:43:52 +0000129void MachODebugMapParser::switchToNewDebugMapObject(
130 StringRef Filename, sys::TimePoint<std::chrono::seconds> Timestamp) {
Frederic Riss31e081e2014-12-12 17:31:24 +0000131 resetParserState();
132
133 SmallString<80> Path(PathPrefix);
134 sys::path::append(Path, Filename);
135
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +0000136 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 Riss31e081e2014-12-12 17:31:24 +0000141 return;
142 }
143
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +0000144 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 Devliegherecc46e392018-03-13 15:47:38 +0000149 return;
Frederic Rissfa7f7bd2015-07-24 06:41:11 +0000150 }
151
Francis Ricci6f200df2017-10-06 14:49:20 +0000152 CurrentDebugMapObject =
153 &Result->addDebugMapObject(Path, Timestamp, MachO::N_OSO);
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +0000154 loadCurrentObjectFileSymbols(*Object);
Frederic Riss31e081e2014-12-12 17:31:24 +0000155}
156
Frederic Rissa8a34c52015-08-31 00:29:09 +0000157static std::string getArchName(const object::MachOObjectFile &Obj) {
Tim Northoverd52a2442016-04-22 23:21:13 +0000158 Triple T = Obj.getArchTriple();
Frederic Rissa8a34c52015-08-31 00:29:09 +0000159 return T.getArchName();
160}
161
Frederic Riss7a425782015-08-05 18:27:44 +0000162std::unique_ptr<DebugMap>
163MachODebugMapParser::parseOneBinary(const MachOObjectFile &MainBinary,
164 StringRef BinaryPath) {
Frederic Rissfa7f7bd2015-07-24 06:41:11 +0000165 loadMainBinarySymbols(MainBinary);
Jonas Devlieghere20767d12019-01-07 23:27:25 +0000166 ArrayRef<uint8_t> UUID = MainBinary.getUuid();
167 Result = make_unique<DebugMap>(MainBinary.getArchTriple(), BinaryPath, UUID);
Frederic Rissaabd6c12014-12-16 20:21:34 +0000168 MainBinaryStrings = MainBinary.getStringTableData();
Frederic Riss31e081e2014-12-12 17:31:24 +0000169 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 Rissa8a34c52015-08-31 00:29:09 +0000181// 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.
185struct DarwinStabName {
186 uint8_t NType;
187 const char *Name;
188};
189
190static 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 Wennborg4d651e42015-10-06 23:24:35 +0000206 {MachO::N_LENG, "N_LENG"}, {0, nullptr}};
Frederic Rissa8a34c52015-08-31 00:29:09 +0000207
208static 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 Wennborg4d651e42015-10-06 23:24:35 +0000213 return nullptr;
Frederic Rissa8a34c52015-08-31 00:29:09 +0000214}
215
216void 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
226void 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 Rissa8a34c52015-08-31 00:29:09 +0000230 // Index
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000231 OS << '[' << format_decimal(Index, 6)
232 << "] "
Frederic Rissa8a34c52015-08-31 00:29:09 +0000233 // n_strx
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000234 << format_hex_no_prefix(StringIndex, 8)
235 << ' '
Frederic Rissa8a34c52015-08-31 00:29:09 +0000236 // 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 Devlieghere3ad0c5a2018-02-22 11:32:51 +0000274 << format_hex_no_prefix(SectionIndex, 2)
275 << " "
Frederic Rissa8a34c52015-08-31 00:29:09 +0000276 // n_desc
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +0000277 << format_hex_no_prefix(Flags, 4)
278 << " "
Frederic Rissa8a34c52015-08-31 00:29:09 +0000279 // 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
289void MachODebugMapParser::dumpOneBinaryStab(const MachOObjectFile &MainBinary,
290 StringRef BinaryPath) {
291 loadMainBinarySymbols(MainBinary);
292 MainBinaryStrings = MainBinary.getStringTableData();
293 raw_ostream &OS(llvm::outs());
294
Frederic Riss74e7aca2015-08-31 00:49:34 +0000295 dumpSymTabHeader(OS, getArchName(MainBinary));
Frederic Rissa8a34c52015-08-31 00:29:09 +0000296 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 Riss1b709aa2015-08-05 22:33:28 +0000310static bool shouldLinkArch(SmallVectorImpl<StringRef> &Archs, StringRef Arch) {
David Majnemer975248e2016-08-11 22:21:41 +0000311 if (Archs.empty() || is_contained(Archs, "all") || is_contained(Archs, "*"))
Frederic Riss1b709aa2015-08-05 22:33:28 +0000312 return true;
313
David Majnemer975248e2016-08-11 22:21:41 +0000314 if (Arch.startswith("arm") && Arch != "arm64" && is_contained(Archs, "arm"))
Frederic Riss1b709aa2015-08-05 22:33:28 +0000315 return true;
316
Frederic Rissc0fdf882016-05-09 06:01:12 +0000317 SmallString<16> ArchName = Arch;
318 if (Arch.startswith("thumb"))
319 ArchName = ("arm" + Arch.substr(5)).str();
320
David Majnemer975248e2016-08-11 22:21:41 +0000321 return is_contained(Archs, ArchName);
Frederic Riss1b709aa2015-08-05 22:33:28 +0000322}
323
Frederic Rissa8a34c52015-08-31 00:29:09 +0000324bool MachODebugMapParser::dumpStab() {
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +0000325 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 Rissa8a34c52015-08-31 00:29:09 +0000330 return false;
331 }
332
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +0000333 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 Rissa8a34c52015-08-31 00:29:09 +0000345
346 return true;
347}
348
Frederic Riss7a425782015-08-05 18:27:44 +0000349/// 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.
352ErrorOr<std::vector<std::unique_ptr<DebugMap>>> MachODebugMapParser::parse() {
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +0000353 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 Riss7a425782015-08-05 18:27:44 +0000362
363 std::vector<std::unique_ptr<DebugMap>> Results;
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +0000364 for (const auto *Object : *Objects)
365 if (shouldLinkArch(Archs, Object->getArchTriple().getArchName()))
366 Results.push_back(parseOneBinary(*Object, BinaryPath));
Frederic Riss7a425782015-08-05 18:27:44 +0000367
368 return std::move(Results);
369}
370
Frederic Riss31e081e2014-12-12 17:31:24 +0000371/// Interpret the STAB entries to fill the DebugMap.
372void 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 Rissaabd6c12014-12-16 20:21:34 +0000380 const char *Name = &MainBinaryStrings.data()[StringIndex];
Frederic Riss31e081e2014-12-12 17:31:24 +0000381
382 // An N_OSO entry represents the start of a new object file description.
Pavel Labath73ce0c02016-11-09 11:43:52 +0000383 if (Type == MachO::N_OSO)
384 return switchToNewDebugMapObject(Name, sys::toTimePoint(Value));
Frederic Riss31e081e2014-12-12 17:31:24 +0000385
Francis Ricci6f200df2017-10-06 14:49:20 +0000386 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 Devlieghere3ad0c5a2018-02-22 11:32:51 +0000393 // 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 Riss31e081e2014-12-12 17:31:24 +0000395 if (!CurrentDebugMapObject)
396 return;
397
Frederic Risse7a35642015-03-15 01:29:30 +0000398 uint32_t Size = 0;
Frederic Riss31e081e2014-12-12 17:31:24 +0000399 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 Riss31e081e2014-12-12 17:31:24 +0000405 break;
406 case MachO::N_FUN:
Frederic Risse7a35642015-03-15 01:29:30 +0000407 // 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 Riss31e081e2014-12-12 17:31:24 +0000417 return;
Frederic Risse7a35642015-03-15 01:29:30 +0000418 }
Frederic Riss31e081e2014-12-12 17:31:24 +0000419 case MachO::N_STSYM:
420 break;
421 default:
422 return;
423 }
424
425 auto ObjectSymIt = CurrentObjectAddresses.find(Name);
Jonas Devlieghere42371bc2017-09-26 08:17:28 +0000426
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 Devlieghere21cda302018-03-14 09:34:54 +0000437 if (ObjectSymIt == CurrentObjectAddresses.end()) {
438 Warning("could not find object file symbol for symbol " + Twine(Name));
439 return;
440 }
Jonas Devlieghere42371bc2017-09-26 08:17:28 +0000441
Frederic Rissc5413fc2016-01-31 04:29:22 +0000442 if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value,
Jonas Devlieghere21cda302018-03-14 09:34:54 +0000443 Size)) {
444 Warning(Twine("failed to insert symbol '") + Name + "' in the debug map.");
445 return;
446 }
Frederic Riss31e081e2014-12-12 17:31:24 +0000447}
448
449/// Load the current object file symbols into CurrentObjectAddresses.
Frederic Rissfa7f7bd2015-07-24 06:41:11 +0000450void MachODebugMapParser::loadCurrentObjectFileSymbols(
451 const object::MachOObjectFile &Obj) {
Frederic Riss31e081e2014-12-12 17:31:24 +0000452 CurrentObjectAddresses.clear();
Frederic Riss31e081e2014-12-12 17:31:24 +0000453
Frederic Rissfa7f7bd2015-07-24 06:41:11 +0000454 for (auto Sym : Obj.symbols()) {
Rafael Espindola7b7c81c2015-07-07 17:12:59 +0000455 uint64_t Addr = Sym.getValue();
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000456 Expected<StringRef> Name = Sym.getName();
457 if (!Name) {
458 // TODO: Actually report errors helpfully.
459 consumeError(Name.takeError());
Rafael Espindola8a806412015-07-02 20:55:21 +0000460 continue;
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000461 }
Frederic Riss99a07122016-01-31 04:29:34 +0000462 // 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 Rissba161512015-12-11 17:50:37 +0000471 CurrentObjectAddresses[*Name] = None;
472 else
473 CurrentObjectAddresses[*Name] = Addr;
Frederic Riss31e081e2014-12-12 17:31:24 +0000474 }
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.
480uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
481 auto Sym = MainBinarySymbolAddresses.find(Name);
482 if (Sym == MainBinarySymbolAddresses.end())
Rafael Espindola7b7c81c2015-07-07 17:12:59 +0000483 return 0;
Frederic Riss31e081e2014-12-12 17:31:24 +0000484 return Sym->second;
485}
486
Jonas Devlieghere42371bc2017-09-26 08:17:28 +0000487/// Get all symbol names in the main binary for the given value.
488std::vector<StringRef>
489MachODebugMapParser::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 Riss31e081e2014-12-12 17:31:24 +0000498/// Load the interesting main binary symbols' addresses into
499/// MainBinarySymbolAddresses.
Frederic Rissfa7f7bd2015-07-24 06:41:11 +0000500void MachODebugMapParser::loadMainBinarySymbols(
501 const MachOObjectFile &MainBinary) {
Frederic Riss5a0743e2015-01-05 21:29:28 +0000502 section_iterator Section = MainBinary.section_end();
Frederic Rissfa7f7bd2015-07-24 06:41:11 +0000503 MainBinarySymbolAddresses.clear();
Frederic Riss5a0743e2015-01-05 21:29:28 +0000504 for (const auto &Sym : MainBinary.symbols()) {
Kevin Enderbya486dca2016-05-02 20:28:12 +0000505 Expected<SymbolRef::Type> TypeOrErr = Sym.getType();
506 if (!TypeOrErr) {
507 // TODO: Actually report errors helpfully.
508 consumeError(TypeOrErr.takeError());
Kevin Enderby46e35ed2016-03-23 20:27:00 +0000509 continue;
Kevin Enderbya486dca2016-05-02 20:28:12 +0000510 }
Kevin Enderby46e35ed2016-03-23 20:27:00 +0000511 SymbolRef::Type Type = *TypeOrErr;
Frederic Riss31e081e2014-12-12 17:31:24 +0000512 // Skip undefined and STAB entries.
David Majnemer8d997482016-10-31 17:11:23 +0000513 if ((Type == SymbolRef::ST_Debug) || (Type == SymbolRef::ST_Unknown))
Frederic Riss31e081e2014-12-12 17:31:24 +0000514 continue;
Jonas Devliegherec7a92ba2018-11-30 18:56:10 +0000515 // 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 Ricci87b0f4a2017-10-09 17:27:47 +0000522 uint8_t SymType =
523 MainBinary.getSymbolTableEntry(Sym.getRawDataRefImpl()).n_type;
Jonas Devliegherec7a92ba2018-11-30 18:56:10 +0000524 bool Extern = SymType & (MachO::N_EXT | MachO::N_PEXT);
Kevin Enderbya486dca2016-05-02 20:28:12 +0000525 Expected<section_iterator> SectionOrErr = Sym.getSection();
526 if (!SectionOrErr) {
527 // TODO: Actually report errors helpfully.
528 consumeError(SectionOrErr.takeError());
Rafael Espindolae84d8c12015-08-07 23:27:14 +0000529 continue;
Kevin Enderbya486dca2016-05-02 20:28:12 +0000530 }
Rafael Espindolae84d8c12015-08-07 23:27:14 +0000531 Section = *SectionOrErr;
532 if (Section == MainBinary.section_end() || Section->isText())
Rafael Espindola8a806412015-07-02 20:55:21 +0000533 continue;
Rafael Espindola7b7c81c2015-07-07 17:12:59 +0000534 uint64_t Addr = Sym.getValue();
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000535 Expected<StringRef> NameOrErr = Sym.getName();
536 if (!NameOrErr) {
537 // TODO: Actually report errors helpfully.
538 consumeError(NameOrErr.takeError());
Rafael Espindola8a806412015-07-02 20:55:21 +0000539 continue;
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000540 }
Rafael Espindola8a806412015-07-02 20:55:21 +0000541 StringRef Name = *NameOrErr;
542 if (Name.size() == 0 || Name[0] == '\0')
Frederic Riss31e081e2014-12-12 17:31:24 +0000543 continue;
Jonas Devliegherec7a92ba2018-11-30 18:56:10 +0000544 // Override only if the new key is global.
545 if (Extern)
546 MainBinarySymbolAddresses[Name] = Addr;
547 else
548 MainBinarySymbolAddresses.try_emplace(Name, Addr);
Frederic Riss31e081e2014-12-12 17:31:24 +0000549 }
550}
551
552namespace llvm {
553namespace dsymutil {
Frederic Riss7a425782015-08-05 18:27:44 +0000554llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
Frederic Riss1b709aa2015-08-05 22:33:28 +0000555parseDebugMap(StringRef InputFile, ArrayRef<std::string> Archs,
Jonas Devlieghere67eb8fd2018-04-02 10:40:43 +0000556 StringRef PrependPath, bool PaperTrailWarnings, bool Verbose,
557 bool InputIsYAML) {
558 if (InputIsYAML)
Frederic Riss527bb612015-06-05 20:27:04 +0000559 return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose);
Jonas Devlieghere67eb8fd2018-04-02 10:40:43 +0000560
561 MachODebugMapParser Parser(InputFile, Archs, PrependPath, PaperTrailWarnings,
562 Verbose);
563 return Parser.parse();
Frederic Riss31e081e2014-12-12 17:31:24 +0000564}
Frederic Rissa8a34c52015-08-31 00:29:09 +0000565
566bool dumpStab(StringRef InputFile, ArrayRef<std::string> Archs,
567 StringRef PrependPath) {
568 MachODebugMapParser Parser(InputFile, Archs, PrependPath, false);
569 return Parser.dumpStab();
570}
Hans Wennborg4d651e42015-10-06 23:24:35 +0000571} // namespace dsymutil
572} // namespace llvm