blob: 8d17d527dfcc370315ccd8e0cc972740d51a8669 [file] [log] [blame]
Frederic Riss231f7142014-12-12 17:31:24 +00001//===- 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 Riss43988502015-01-05 21:29:28 +000010#include "BinaryHolder.h"
Frederic Riss231f7142014-12-12 17:31:24 +000011#include "DebugMap.h"
12#include "dsymutil.h"
13#include "llvm/Object/MachO.h"
14#include "llvm/Support/Path.h"
15#include "llvm/Support/raw_ostream.h"
16
17namespace {
18using namespace llvm;
19using namespace llvm::dsymutil;
20using namespace llvm::object;
21
22class MachODebugMapParser {
23public:
Frederic Rissae0d4362015-08-05 22:33:28 +000024 MachODebugMapParser(StringRef BinaryPath, ArrayRef<std::string> Archs,
25 StringRef PathPrefix = "", bool Verbose = false)
26 : BinaryPath(BinaryPath), Archs(Archs.begin(), Archs.end()),
27 PathPrefix(PathPrefix), MainBinaryHolder(Verbose),
28 CurrentObjectHolder(Verbose), CurrentDebugMapObject(nullptr) {}
Frederic Riss231f7142014-12-12 17:31:24 +000029
Frederic Riss4dd3e0c2015-08-05 18:27:44 +000030 /// \brief Parses and returns the DebugMaps of the input binary.
31 /// The binary contains multiple maps in case it is a universal
32 /// binary.
Frederic Riss231f7142014-12-12 17:31:24 +000033 /// \returns an error in case the provided BinaryPath doesn't exist
34 /// or isn't of a supported type.
Frederic Riss4dd3e0c2015-08-05 18:27:44 +000035 ErrorOr<std::vector<std::unique_ptr<DebugMap>>> parse();
Frederic Riss231f7142014-12-12 17:31:24 +000036
37private:
38 std::string BinaryPath;
Frederic Rissae0d4362015-08-05 22:33:28 +000039 SmallVector<StringRef, 1> Archs;
Frederic Riss231f7142014-12-12 17:31:24 +000040 std::string PathPrefix;
41
Frederic Riss43988502015-01-05 21:29:28 +000042 /// Owns the MemoryBuffer for the main binary.
43 BinaryHolder MainBinaryHolder;
Frederic Riss231f7142014-12-12 17:31:24 +000044 /// Map of the binary symbol addresses.
45 StringMap<uint64_t> MainBinarySymbolAddresses;
Frederic Riss896b2c52014-12-16 20:21:34 +000046 StringRef MainBinaryStrings;
Frederic Riss231f7142014-12-12 17:31:24 +000047 /// The constructed DebugMap.
48 std::unique_ptr<DebugMap> Result;
49
Frederic Riss43988502015-01-05 21:29:28 +000050 /// Owns the MemoryBuffer for the currently handled object file.
51 BinaryHolder CurrentObjectHolder;
Frederic Riss231f7142014-12-12 17:31:24 +000052 /// Map of the currently processed object file symbol addresses.
53 StringMap<uint64_t> CurrentObjectAddresses;
54 /// Element of the debug map corresponfing to the current object file.
55 DebugMapObject *CurrentDebugMapObject;
56
Frederic Riss912d0f12015-03-15 01:29:30 +000057 /// Holds function info while function scope processing.
58 const char *CurrentFunctionName;
59 uint64_t CurrentFunctionAddress;
60
Frederic Riss4dd3e0c2015-08-05 18:27:44 +000061 std::unique_ptr<DebugMap> parseOneBinary(const MachOObjectFile &MainBinary,
62 StringRef BinaryPath);
63
Frederic Riss9ccfddc2015-07-22 23:24:00 +000064 void switchToNewDebugMapObject(StringRef Filename, sys::TimeValue Timestamp);
Frederic Riss231f7142014-12-12 17:31:24 +000065 void resetParserState();
66 uint64_t getMainBinarySymbolAddress(StringRef Name);
Frederic Risseb85c8f2015-07-24 06:41:11 +000067 void loadMainBinarySymbols(const MachOObjectFile &MainBinary);
68 void loadCurrentObjectFileSymbols(const object::MachOObjectFile &Obj);
Frederic Riss231f7142014-12-12 17:31:24 +000069 void handleStabSymbolTableEntry(uint32_t StringIndex, uint8_t Type,
70 uint8_t SectionIndex, uint16_t Flags,
71 uint64_t Value);
72
73 template <typename STEType> void handleStabDebugMapEntry(const STEType &STE) {
74 handleStabSymbolTableEntry(STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc,
75 STE.n_value);
76 }
77};
78
79static void Warning(const Twine &Msg) { errs() << "warning: " + Msg + "\n"; }
80}
81
Frederic Riss231f7142014-12-12 17:31:24 +000082/// Reset the parser state coresponding to the current object
83/// file. This is to be called after an object file is finished
84/// processing.
85void MachODebugMapParser::resetParserState() {
Frederic Riss231f7142014-12-12 17:31:24 +000086 CurrentObjectAddresses.clear();
87 CurrentDebugMapObject = nullptr;
88}
89
90/// Create a new DebugMapObject. This function resets the state of the
91/// parser that was referring to the last object file and sets
92/// everything up to add symbols to the new one.
Frederic Riss9ccfddc2015-07-22 23:24:00 +000093void MachODebugMapParser::switchToNewDebugMapObject(StringRef Filename,
94 sys::TimeValue Timestamp) {
Frederic Riss231f7142014-12-12 17:31:24 +000095 resetParserState();
96
97 SmallString<80> Path(PathPrefix);
98 sys::path::append(Path, Filename);
99
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000100 auto MachOOrError =
Frederic Risseb85c8f2015-07-24 06:41:11 +0000101 CurrentObjectHolder.GetFilesAs<MachOObjectFile>(Path, Timestamp);
Frederic Riss231f7142014-12-12 17:31:24 +0000102 if (auto Error = MachOOrError.getError()) {
103 Warning(Twine("cannot open debug object \"") + Path.str() + "\": " +
104 Error.message() + "\n");
105 return;
106 }
107
Frederic Risseb85c8f2015-07-24 06:41:11 +0000108 auto ErrOrAchObj =
109 CurrentObjectHolder.GetAs<MachOObjectFile>(Result->getTriple());
110 if (auto Err = ErrOrAchObj.getError()) {
111 return Warning(Twine("cannot open debug object \"") + Path.str() + "\": " +
112 Err.message() + "\n");
113 }
114
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000115 CurrentDebugMapObject = &Result->addDebugMapObject(Path, Timestamp);
Frederic Risseb85c8f2015-07-24 06:41:11 +0000116 loadCurrentObjectFileSymbols(*ErrOrAchObj);
Frederic Riss231f7142014-12-12 17:31:24 +0000117}
118
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000119std::unique_ptr<DebugMap>
120MachODebugMapParser::parseOneBinary(const MachOObjectFile &MainBinary,
121 StringRef BinaryPath) {
Frederic Risseb85c8f2015-07-24 06:41:11 +0000122 loadMainBinarySymbols(MainBinary);
Frederic Riss2c69d362015-08-26 05:09:59 +0000123 Result =
124 make_unique<DebugMap>(BinaryHolder::getTriple(MainBinary), BinaryPath);
Frederic Riss896b2c52014-12-16 20:21:34 +0000125 MainBinaryStrings = MainBinary.getStringTableData();
Frederic Riss231f7142014-12-12 17:31:24 +0000126 for (const SymbolRef &Symbol : MainBinary.symbols()) {
127 const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
128 if (MainBinary.is64Bit())
129 handleStabDebugMapEntry(MainBinary.getSymbol64TableEntry(DRI));
130 else
131 handleStabDebugMapEntry(MainBinary.getSymbolTableEntry(DRI));
132 }
133
134 resetParserState();
135 return std::move(Result);
136}
137
Frederic Rissae0d4362015-08-05 22:33:28 +0000138static bool shouldLinkArch(SmallVectorImpl<StringRef> &Archs, StringRef Arch) {
139 if (Archs.empty() ||
140 std::find(Archs.begin(), Archs.end(), "all") != Archs.end() ||
141 std::find(Archs.begin(), Archs.end(), "*") != Archs.end())
142 return true;
143
144 if (Arch.startswith("arm") && Arch != "arm64" &&
145 std::find(Archs.begin(), Archs.end(), "arm") != Archs.end())
146 return true;
147
148 return std::find(Archs.begin(), Archs.end(), Arch) != Archs.end();
149}
150
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000151/// This main parsing routine tries to open the main binary and if
152/// successful iterates over the STAB entries. The real parsing is
153/// done in handleStabSymbolTableEntry.
154ErrorOr<std::vector<std::unique_ptr<DebugMap>>> MachODebugMapParser::parse() {
155 auto MainBinOrError =
156 MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath);
157 if (auto Error = MainBinOrError.getError())
158 return Error;
159
160 std::vector<std::unique_ptr<DebugMap>> Results;
Frederic Rissae0d4362015-08-05 22:33:28 +0000161 Triple T;
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000162 for (const auto *Binary : *MainBinOrError)
Frederic Rissae0d4362015-08-05 22:33:28 +0000163 if (shouldLinkArch(Archs, Binary->getArch(nullptr, &T).getArchName()))
164 Results.push_back(parseOneBinary(*Binary, BinaryPath));
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000165
166 return std::move(Results);
167}
168
Frederic Riss231f7142014-12-12 17:31:24 +0000169/// Interpret the STAB entries to fill the DebugMap.
170void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex,
171 uint8_t Type,
172 uint8_t SectionIndex,
173 uint16_t Flags,
174 uint64_t Value) {
175 if (!(Type & MachO::N_STAB))
176 return;
177
Frederic Riss896b2c52014-12-16 20:21:34 +0000178 const char *Name = &MainBinaryStrings.data()[StringIndex];
Frederic Riss231f7142014-12-12 17:31:24 +0000179
180 // An N_OSO entry represents the start of a new object file description.
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000181 if (Type == MachO::N_OSO) {
182 sys::TimeValue Timestamp;
183 Timestamp.fromEpochTime(Value);
184 return switchToNewDebugMapObject(Name, Timestamp);
185 }
Frederic Riss231f7142014-12-12 17:31:24 +0000186
187 // If the last N_OSO object file wasn't found,
188 // CurrentDebugMapObject will be null. Do not update anything
189 // until we find the next valid N_OSO entry.
190 if (!CurrentDebugMapObject)
191 return;
192
Frederic Riss912d0f12015-03-15 01:29:30 +0000193 uint32_t Size = 0;
Frederic Riss231f7142014-12-12 17:31:24 +0000194 switch (Type) {
195 case MachO::N_GSYM:
196 // This is a global variable. We need to query the main binary
197 // symbol table to find its address as it might not be in the
198 // debug map (for common symbols).
199 Value = getMainBinarySymbolAddress(Name);
Frederic Riss231f7142014-12-12 17:31:24 +0000200 break;
201 case MachO::N_FUN:
Frederic Riss912d0f12015-03-15 01:29:30 +0000202 // Functions are scopes in STABS. They have an end marker that
203 // contains the function size.
204 if (Name[0] == '\0') {
205 Size = Value;
206 Value = CurrentFunctionAddress;
207 Name = CurrentFunctionName;
208 break;
209 } else {
210 CurrentFunctionName = Name;
211 CurrentFunctionAddress = Value;
Frederic Riss231f7142014-12-12 17:31:24 +0000212 return;
Frederic Riss912d0f12015-03-15 01:29:30 +0000213 }
Frederic Riss231f7142014-12-12 17:31:24 +0000214 case MachO::N_STSYM:
215 break;
216 default:
217 return;
218 }
219
220 auto ObjectSymIt = CurrentObjectAddresses.find(Name);
221 if (ObjectSymIt == CurrentObjectAddresses.end())
222 return Warning("could not find object file symbol for symbol " +
223 Twine(Name));
Frederic Riss912d0f12015-03-15 01:29:30 +0000224 if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value,
225 Size))
Frederic Riss231f7142014-12-12 17:31:24 +0000226 return Warning(Twine("failed to insert symbol '") + Name +
227 "' in the debug map.");
228}
229
230/// Load the current object file symbols into CurrentObjectAddresses.
Frederic Risseb85c8f2015-07-24 06:41:11 +0000231void MachODebugMapParser::loadCurrentObjectFileSymbols(
232 const object::MachOObjectFile &Obj) {
Frederic Riss231f7142014-12-12 17:31:24 +0000233 CurrentObjectAddresses.clear();
Frederic Riss231f7142014-12-12 17:31:24 +0000234
Frederic Risseb85c8f2015-07-24 06:41:11 +0000235 for (auto Sym : Obj.symbols()) {
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000236 uint64_t Addr = Sym.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000237 ErrorOr<StringRef> Name = Sym.getName();
238 if (!Name)
239 continue;
240 CurrentObjectAddresses[*Name] = Addr;
Frederic Riss231f7142014-12-12 17:31:24 +0000241 }
242}
243
244/// Lookup a symbol address in the main binary symbol table. The
245/// parser only needs to query common symbols, thus not every symbol's
246/// address is available through this function.
247uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
248 auto Sym = MainBinarySymbolAddresses.find(Name);
249 if (Sym == MainBinarySymbolAddresses.end())
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000250 return 0;
Frederic Riss231f7142014-12-12 17:31:24 +0000251 return Sym->second;
252}
253
254/// Load the interesting main binary symbols' addresses into
255/// MainBinarySymbolAddresses.
Frederic Risseb85c8f2015-07-24 06:41:11 +0000256void MachODebugMapParser::loadMainBinarySymbols(
257 const MachOObjectFile &MainBinary) {
Frederic Riss43988502015-01-05 21:29:28 +0000258 section_iterator Section = MainBinary.section_end();
Frederic Risseb85c8f2015-07-24 06:41:11 +0000259 MainBinarySymbolAddresses.clear();
Frederic Riss43988502015-01-05 21:29:28 +0000260 for (const auto &Sym : MainBinary.symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000261 SymbolRef::Type Type = Sym.getType();
Frederic Riss231f7142014-12-12 17:31:24 +0000262 // Skip undefined and STAB entries.
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000263 if ((Type & SymbolRef::ST_Debug) || (Type & SymbolRef::ST_Unknown))
Frederic Riss231f7142014-12-12 17:31:24 +0000264 continue;
Frederic Riss231f7142014-12-12 17:31:24 +0000265 // The only symbols of interest are the global variables. These
266 // are the only ones that need to be queried because the address
267 // of common data won't be described in the debug map. All other
268 // addresses should be fetched for the debug map.
Rafael Espindola8bab8892015-08-07 23:27:14 +0000269 if (!(Sym.getFlags() & SymbolRef::SF_Global))
270 continue;
271 ErrorOr<section_iterator> SectionOrErr = Sym.getSection();
272 if (!SectionOrErr)
273 continue;
274 Section = *SectionOrErr;
275 if (Section == MainBinary.section_end() || Section->isText())
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000276 continue;
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000277 uint64_t Addr = Sym.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000278 ErrorOr<StringRef> NameOrErr = Sym.getName();
279 if (!NameOrErr)
280 continue;
281 StringRef Name = *NameOrErr;
282 if (Name.size() == 0 || Name[0] == '\0')
Frederic Riss231f7142014-12-12 17:31:24 +0000283 continue;
284 MainBinarySymbolAddresses[Name] = Addr;
285 }
286}
287
288namespace llvm {
289namespace dsymutil {
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000290llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
Frederic Rissae0d4362015-08-05 22:33:28 +0000291parseDebugMap(StringRef InputFile, ArrayRef<std::string> Archs,
292 StringRef PrependPath, bool Verbose, bool InputIsYAML) {
Frederic Riss90e0bd92015-06-03 20:29:24 +0000293 if (!InputIsYAML) {
Frederic Rissae0d4362015-08-05 22:33:28 +0000294 MachODebugMapParser Parser(InputFile, Archs, PrependPath, Verbose);
Frederic Riss90e0bd92015-06-03 20:29:24 +0000295 return Parser.parse();
296 } else {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000297 return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose);
Frederic Riss90e0bd92015-06-03 20:29:24 +0000298 }
Frederic Riss231f7142014-12-12 17:31:24 +0000299}
300}
301}