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