blob: 76fc7612e68b5749ca397a2e2c08bb2f2831bdf4 [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 Riss43988502015-01-05 21:29:28 +000024 MachODebugMapParser(StringRef BinaryPath, StringRef PathPrefix = "",
25 bool Verbose = false)
Alexey Samsonovd927bd82014-12-18 00:45:32 +000026 : BinaryPath(BinaryPath), PathPrefix(PathPrefix),
Alexey Samsonovdb50b3d2015-01-07 21:13:30 +000027 MainBinaryHolder(Verbose), CurrentObjectHolder(Verbose),
28 CurrentDebugMapObject(nullptr) {}
Frederic Riss231f7142014-12-12 17:31:24 +000029
30 /// \brief Parses and returns the DebugMap of the input binary.
31 /// \returns an error in case the provided BinaryPath doesn't exist
32 /// or isn't of a supported type.
33 ErrorOr<std::unique_ptr<DebugMap>> parse();
34
35private:
36 std::string BinaryPath;
37 std::string PathPrefix;
38
Frederic Riss43988502015-01-05 21:29:28 +000039 /// Owns the MemoryBuffer for the main binary.
40 BinaryHolder MainBinaryHolder;
Frederic Riss231f7142014-12-12 17:31:24 +000041 /// Map of the binary symbol addresses.
42 StringMap<uint64_t> MainBinarySymbolAddresses;
Frederic Riss896b2c52014-12-16 20:21:34 +000043 StringRef MainBinaryStrings;
Frederic Riss231f7142014-12-12 17:31:24 +000044 /// The constructed DebugMap.
45 std::unique_ptr<DebugMap> Result;
46
Frederic Riss43988502015-01-05 21:29:28 +000047 /// Owns the MemoryBuffer for the currently handled object file.
48 BinaryHolder CurrentObjectHolder;
Frederic Riss231f7142014-12-12 17:31:24 +000049 /// Map of the currently processed object file symbol addresses.
50 StringMap<uint64_t> CurrentObjectAddresses;
51 /// Element of the debug map corresponfing to the current object file.
52 DebugMapObject *CurrentDebugMapObject;
53
Frederic Riss912d0f12015-03-15 01:29:30 +000054 /// Holds function info while function scope processing.
55 const char *CurrentFunctionName;
56 uint64_t CurrentFunctionAddress;
57
Frederic Riss231f7142014-12-12 17:31:24 +000058 void switchToNewDebugMapObject(StringRef Filename);
59 void resetParserState();
60 uint64_t getMainBinarySymbolAddress(StringRef Name);
61 void loadMainBinarySymbols();
62 void loadCurrentObjectFileSymbols();
63 void handleStabSymbolTableEntry(uint32_t StringIndex, uint8_t Type,
64 uint8_t SectionIndex, uint16_t Flags,
65 uint64_t Value);
66
67 template <typename STEType> void handleStabDebugMapEntry(const STEType &STE) {
68 handleStabSymbolTableEntry(STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc,
69 STE.n_value);
70 }
71};
72
73static void Warning(const Twine &Msg) { errs() << "warning: " + Msg + "\n"; }
74}
75
Frederic Riss231f7142014-12-12 17:31:24 +000076/// Reset the parser state coresponding to the current object
77/// file. This is to be called after an object file is finished
78/// processing.
79void MachODebugMapParser::resetParserState() {
Frederic Riss231f7142014-12-12 17:31:24 +000080 CurrentObjectAddresses.clear();
81 CurrentDebugMapObject = nullptr;
82}
83
84/// Create a new DebugMapObject. This function resets the state of the
85/// parser that was referring to the last object file and sets
86/// everything up to add symbols to the new one.
87void MachODebugMapParser::switchToNewDebugMapObject(StringRef Filename) {
88 resetParserState();
89
90 SmallString<80> Path(PathPrefix);
91 sys::path::append(Path, Filename);
92
Frederic Riss43988502015-01-05 21:29:28 +000093 auto MachOOrError = CurrentObjectHolder.GetFileAs<MachOObjectFile>(Path);
Frederic Riss231f7142014-12-12 17:31:24 +000094 if (auto Error = MachOOrError.getError()) {
95 Warning(Twine("cannot open debug object \"") + Path.str() + "\": " +
96 Error.message() + "\n");
97 return;
98 }
99
Frederic Riss231f7142014-12-12 17:31:24 +0000100 loadCurrentObjectFileSymbols();
101 CurrentDebugMapObject = &Result->addDebugMapObject(Path);
102}
103
Frederic Risse4a6fef2015-01-19 23:33:14 +0000104static Triple getTriple(const object::MachOObjectFile &Obj) {
105 Triple TheTriple("unknown-unknown-unknown");
106 TheTriple.setArch(Triple::ArchType(Obj.getArch()));
107 TheTriple.setObjectFormat(Triple::MachO);
108 return TheTriple;
109}
110
Frederic Riss231f7142014-12-12 17:31:24 +0000111/// This main parsing routine tries to open the main binary and if
112/// successful iterates over the STAB entries. The real parsing is
113/// done in handleStabSymbolTableEntry.
114ErrorOr<std::unique_ptr<DebugMap>> MachODebugMapParser::parse() {
Frederic Riss43988502015-01-05 21:29:28 +0000115 auto MainBinOrError = MainBinaryHolder.GetFileAs<MachOObjectFile>(BinaryPath);
116 if (auto Error = MainBinOrError.getError())
Frederic Riss231f7142014-12-12 17:31:24 +0000117 return Error;
118
Frederic Riss43988502015-01-05 21:29:28 +0000119 const MachOObjectFile &MainBinary = *MainBinOrError;
Frederic Riss231f7142014-12-12 17:31:24 +0000120 loadMainBinarySymbols();
Frederic Risse4a6fef2015-01-19 23:33:14 +0000121 Result = make_unique<DebugMap>(getTriple(MainBinary));
Frederic Riss896b2c52014-12-16 20:21:34 +0000122 MainBinaryStrings = MainBinary.getStringTableData();
Frederic Riss231f7142014-12-12 17:31:24 +0000123 for (const SymbolRef &Symbol : MainBinary.symbols()) {
124 const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
125 if (MainBinary.is64Bit())
126 handleStabDebugMapEntry(MainBinary.getSymbol64TableEntry(DRI));
127 else
128 handleStabDebugMapEntry(MainBinary.getSymbolTableEntry(DRI));
129 }
130
131 resetParserState();
132 return std::move(Result);
133}
134
135/// Interpret the STAB entries to fill the DebugMap.
136void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex,
137 uint8_t Type,
138 uint8_t SectionIndex,
139 uint16_t Flags,
140 uint64_t Value) {
141 if (!(Type & MachO::N_STAB))
142 return;
143
Frederic Riss896b2c52014-12-16 20:21:34 +0000144 const char *Name = &MainBinaryStrings.data()[StringIndex];
Frederic Riss231f7142014-12-12 17:31:24 +0000145
146 // An N_OSO entry represents the start of a new object file description.
147 if (Type == MachO::N_OSO)
148 return switchToNewDebugMapObject(Name);
149
150 // If the last N_OSO object file wasn't found,
151 // CurrentDebugMapObject will be null. Do not update anything
152 // until we find the next valid N_OSO entry.
153 if (!CurrentDebugMapObject)
154 return;
155
Frederic Riss912d0f12015-03-15 01:29:30 +0000156 uint32_t Size = 0;
Frederic Riss231f7142014-12-12 17:31:24 +0000157 switch (Type) {
158 case MachO::N_GSYM:
159 // This is a global variable. We need to query the main binary
160 // symbol table to find its address as it might not be in the
161 // debug map (for common symbols).
162 Value = getMainBinarySymbolAddress(Name);
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000163 if (Value == UnknownAddress)
Frederic Riss231f7142014-12-12 17:31:24 +0000164 return;
165 break;
166 case MachO::N_FUN:
Frederic Riss912d0f12015-03-15 01:29:30 +0000167 // Functions are scopes in STABS. They have an end marker that
168 // contains the function size.
169 if (Name[0] == '\0') {
170 Size = Value;
171 Value = CurrentFunctionAddress;
172 Name = CurrentFunctionName;
173 break;
174 } else {
175 CurrentFunctionName = Name;
176 CurrentFunctionAddress = Value;
Frederic Riss231f7142014-12-12 17:31:24 +0000177 return;
Frederic Riss912d0f12015-03-15 01:29:30 +0000178 }
Frederic Riss231f7142014-12-12 17:31:24 +0000179 case MachO::N_STSYM:
180 break;
181 default:
182 return;
183 }
184
185 auto ObjectSymIt = CurrentObjectAddresses.find(Name);
186 if (ObjectSymIt == CurrentObjectAddresses.end())
187 return Warning("could not find object file symbol for symbol " +
188 Twine(Name));
Frederic Riss912d0f12015-03-15 01:29:30 +0000189 if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value,
190 Size))
Frederic Riss231f7142014-12-12 17:31:24 +0000191 return Warning(Twine("failed to insert symbol '") + Name +
192 "' in the debug map.");
193}
194
195/// Load the current object file symbols into CurrentObjectAddresses.
196void MachODebugMapParser::loadCurrentObjectFileSymbols() {
197 CurrentObjectAddresses.clear();
Frederic Riss231f7142014-12-12 17:31:24 +0000198
Frederic Riss43988502015-01-05 21:29:28 +0000199 for (auto Sym : CurrentObjectHolder.Get().symbols()) {
Rafael Espindola7e7be922015-07-07 15:05:09 +0000200 uint64_t Addr;
201 if (Sym.getFlags() & SymbolRef::SF_Common) {
202 Addr = Sym.getCommonSize();
203 } else {
204 Addr = Sym.getValue();
205 if (Addr == UnknownAddress)
206 continue;
207 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000208 ErrorOr<StringRef> Name = Sym.getName();
209 if (!Name)
210 continue;
211 CurrentObjectAddresses[*Name] = Addr;
Frederic Riss231f7142014-12-12 17:31:24 +0000212 }
213}
214
215/// Lookup a symbol address in the main binary symbol table. The
216/// parser only needs to query common symbols, thus not every symbol's
217/// address is available through this function.
218uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
219 auto Sym = MainBinarySymbolAddresses.find(Name);
220 if (Sym == MainBinarySymbolAddresses.end())
Rafael Espindolad7a32ea2015-06-24 10:20:30 +0000221 return UnknownAddress;
Frederic Riss231f7142014-12-12 17:31:24 +0000222 return Sym->second;
223}
224
225/// Load the interesting main binary symbols' addresses into
226/// MainBinarySymbolAddresses.
227void MachODebugMapParser::loadMainBinarySymbols() {
Frederic Riss43988502015-01-05 21:29:28 +0000228 const MachOObjectFile &MainBinary = MainBinaryHolder.GetAs<MachOObjectFile>();
229 section_iterator Section = MainBinary.section_end();
230 for (const auto &Sym : MainBinary.symbols()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000231 SymbolRef::Type Type = Sym.getType();
Frederic Riss231f7142014-12-12 17:31:24 +0000232 // Skip undefined and STAB entries.
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000233 if ((Type & SymbolRef::ST_Debug) || (Type & SymbolRef::ST_Unknown))
Frederic Riss231f7142014-12-12 17:31:24 +0000234 continue;
Rafael Espindolae2df87f2015-07-03 18:02:36 +0000235 uint64_t Addr = Sym.getValue();
Frederic Riss231f7142014-12-12 17:31:24 +0000236 // The only symbols of interest are the global variables. These
237 // are the only ones that need to be queried because the address
238 // of common data won't be described in the debug map. All other
239 // addresses should be fetched for the debug map.
Rafael Espindolae2df87f2015-07-03 18:02:36 +0000240 if (Addr == UnknownAddress || !(Sym.getFlags() & SymbolRef::SF_Global) ||
241 Sym.getSection(Section) || Section->isText())
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000242 continue;
243 ErrorOr<StringRef> NameOrErr = Sym.getName();
244 if (!NameOrErr)
245 continue;
246 StringRef Name = *NameOrErr;
247 if (Name.size() == 0 || Name[0] == '\0')
Frederic Riss231f7142014-12-12 17:31:24 +0000248 continue;
249 MainBinarySymbolAddresses[Name] = Addr;
250 }
251}
252
253namespace llvm {
254namespace dsymutil {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000255llvm::ErrorOr<std::unique_ptr<DebugMap>> parseDebugMap(StringRef InputFile,
256 StringRef PrependPath,
257 bool Verbose,
258 bool InputIsYAML) {
Frederic Riss90e0bd92015-06-03 20:29:24 +0000259 if (!InputIsYAML) {
260 MachODebugMapParser Parser(InputFile, PrependPath, Verbose);
261 return Parser.parse();
262 } else {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000263 return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose);
Frederic Riss90e0bd92015-06-03 20:29:24 +0000264 }
Frederic Riss231f7142014-12-12 17:31:24 +0000265}
266}
267}