blob: 2f4a9876a24f2fa0fefa3c72c06b821714da64b5 [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
54 void switchToNewDebugMapObject(StringRef Filename);
55 void resetParserState();
56 uint64_t getMainBinarySymbolAddress(StringRef Name);
57 void loadMainBinarySymbols();
58 void loadCurrentObjectFileSymbols();
59 void handleStabSymbolTableEntry(uint32_t StringIndex, uint8_t Type,
60 uint8_t SectionIndex, uint16_t Flags,
61 uint64_t Value);
62
63 template <typename STEType> void handleStabDebugMapEntry(const STEType &STE) {
64 handleStabSymbolTableEntry(STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc,
65 STE.n_value);
66 }
67};
68
69static void Warning(const Twine &Msg) { errs() << "warning: " + Msg + "\n"; }
70}
71
Frederic Riss231f7142014-12-12 17:31:24 +000072/// Reset the parser state coresponding to the current object
73/// file. This is to be called after an object file is finished
74/// processing.
75void MachODebugMapParser::resetParserState() {
Frederic Riss231f7142014-12-12 17:31:24 +000076 CurrentObjectAddresses.clear();
77 CurrentDebugMapObject = nullptr;
78}
79
80/// Create a new DebugMapObject. This function resets the state of the
81/// parser that was referring to the last object file and sets
82/// everything up to add symbols to the new one.
83void MachODebugMapParser::switchToNewDebugMapObject(StringRef Filename) {
84 resetParserState();
85
86 SmallString<80> Path(PathPrefix);
87 sys::path::append(Path, Filename);
88
Frederic Riss43988502015-01-05 21:29:28 +000089 auto MachOOrError = CurrentObjectHolder.GetFileAs<MachOObjectFile>(Path);
Frederic Riss231f7142014-12-12 17:31:24 +000090 if (auto Error = MachOOrError.getError()) {
91 Warning(Twine("cannot open debug object \"") + Path.str() + "\": " +
92 Error.message() + "\n");
93 return;
94 }
95
Frederic Riss231f7142014-12-12 17:31:24 +000096 loadCurrentObjectFileSymbols();
97 CurrentDebugMapObject = &Result->addDebugMapObject(Path);
98}
99
Frederic Risse4a6fef2015-01-19 23:33:14 +0000100static Triple getTriple(const object::MachOObjectFile &Obj) {
101 Triple TheTriple("unknown-unknown-unknown");
102 TheTriple.setArch(Triple::ArchType(Obj.getArch()));
103 TheTriple.setObjectFormat(Triple::MachO);
104 return TheTriple;
105}
106
Frederic Riss231f7142014-12-12 17:31:24 +0000107/// This main parsing routine tries to open the main binary and if
108/// successful iterates over the STAB entries. The real parsing is
109/// done in handleStabSymbolTableEntry.
110ErrorOr<std::unique_ptr<DebugMap>> MachODebugMapParser::parse() {
Frederic Riss43988502015-01-05 21:29:28 +0000111 auto MainBinOrError = MainBinaryHolder.GetFileAs<MachOObjectFile>(BinaryPath);
112 if (auto Error = MainBinOrError.getError())
Frederic Riss231f7142014-12-12 17:31:24 +0000113 return Error;
114
Frederic Riss43988502015-01-05 21:29:28 +0000115 const MachOObjectFile &MainBinary = *MainBinOrError;
Frederic Riss231f7142014-12-12 17:31:24 +0000116 loadMainBinarySymbols();
Frederic Risse4a6fef2015-01-19 23:33:14 +0000117 Result = make_unique<DebugMap>(getTriple(MainBinary));
Frederic Riss896b2c52014-12-16 20:21:34 +0000118 MainBinaryStrings = MainBinary.getStringTableData();
Frederic Riss231f7142014-12-12 17:31:24 +0000119 for (const SymbolRef &Symbol : MainBinary.symbols()) {
120 const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
121 if (MainBinary.is64Bit())
122 handleStabDebugMapEntry(MainBinary.getSymbol64TableEntry(DRI));
123 else
124 handleStabDebugMapEntry(MainBinary.getSymbolTableEntry(DRI));
125 }
126
127 resetParserState();
128 return std::move(Result);
129}
130
131/// Interpret the STAB entries to fill the DebugMap.
132void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex,
133 uint8_t Type,
134 uint8_t SectionIndex,
135 uint16_t Flags,
136 uint64_t Value) {
137 if (!(Type & MachO::N_STAB))
138 return;
139
Frederic Riss896b2c52014-12-16 20:21:34 +0000140 const char *Name = &MainBinaryStrings.data()[StringIndex];
Frederic Riss231f7142014-12-12 17:31:24 +0000141
142 // An N_OSO entry represents the start of a new object file description.
143 if (Type == MachO::N_OSO)
144 return switchToNewDebugMapObject(Name);
145
146 // If the last N_OSO object file wasn't found,
147 // CurrentDebugMapObject will be null. Do not update anything
148 // until we find the next valid N_OSO entry.
149 if (!CurrentDebugMapObject)
150 return;
151
152 switch (Type) {
153 case MachO::N_GSYM:
154 // This is a global variable. We need to query the main binary
155 // symbol table to find its address as it might not be in the
156 // debug map (for common symbols).
157 Value = getMainBinarySymbolAddress(Name);
158 if (Value == UnknownAddressOrSize)
159 return;
160 break;
161 case MachO::N_FUN:
162 // Functions are scopes in STABS. They have an end marker that we
163 // need to ignore.
164 if (Name[0] == '\0')
165 return;
166 break;
167 case MachO::N_STSYM:
168 break;
169 default:
170 return;
171 }
172
173 auto ObjectSymIt = CurrentObjectAddresses.find(Name);
174 if (ObjectSymIt == CurrentObjectAddresses.end())
175 return Warning("could not find object file symbol for symbol " +
176 Twine(Name));
177 if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value))
178 return Warning(Twine("failed to insert symbol '") + Name +
179 "' in the debug map.");
180}
181
182/// Load the current object file symbols into CurrentObjectAddresses.
183void MachODebugMapParser::loadCurrentObjectFileSymbols() {
184 CurrentObjectAddresses.clear();
Frederic Riss231f7142014-12-12 17:31:24 +0000185
Frederic Riss43988502015-01-05 21:29:28 +0000186 for (auto Sym : CurrentObjectHolder.Get().symbols()) {
Frederic Riss231f7142014-12-12 17:31:24 +0000187 StringRef Name;
188 uint64_t Addr;
189 if (Sym.getAddress(Addr) || Addr == UnknownAddressOrSize ||
190 Sym.getName(Name))
191 continue;
192 CurrentObjectAddresses[Name] = Addr;
193 }
194}
195
196/// Lookup a symbol address in the main binary symbol table. The
197/// parser only needs to query common symbols, thus not every symbol's
198/// address is available through this function.
199uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
200 auto Sym = MainBinarySymbolAddresses.find(Name);
201 if (Sym == MainBinarySymbolAddresses.end())
202 return UnknownAddressOrSize;
203 return Sym->second;
204}
205
206/// Load the interesting main binary symbols' addresses into
207/// MainBinarySymbolAddresses.
208void MachODebugMapParser::loadMainBinarySymbols() {
Frederic Riss43988502015-01-05 21:29:28 +0000209 const MachOObjectFile &MainBinary = MainBinaryHolder.GetAs<MachOObjectFile>();
210 section_iterator Section = MainBinary.section_end();
211 for (const auto &Sym : MainBinary.symbols()) {
Frederic Riss231f7142014-12-12 17:31:24 +0000212 SymbolRef::Type Type;
213 // Skip undefined and STAB entries.
214 if (Sym.getType(Type) || (Type & SymbolRef::ST_Debug) ||
215 (Type & SymbolRef::ST_Unknown))
216 continue;
217 StringRef Name;
218 uint64_t Addr;
219 // The only symbols of interest are the global variables. These
220 // are the only ones that need to be queried because the address
221 // of common data won't be described in the debug map. All other
222 // addresses should be fetched for the debug map.
223 if (Sym.getAddress(Addr) || Addr == UnknownAddressOrSize ||
224 !(Sym.getFlags() & SymbolRef::SF_Global) || Sym.getSection(Section) ||
225 Section->isText() || Sym.getName(Name) || Name.size() == 0 ||
226 Name[0] == '\0')
227 continue;
228 MainBinarySymbolAddresses[Name] = Addr;
229 }
230}
231
232namespace llvm {
233namespace dsymutil {
Frederic Riss9ac9a282015-02-28 00:29:05 +0000234llvm::ErrorOr<std::unique_ptr<DebugMap>>
235parseDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose) {
Frederic Riss43988502015-01-05 21:29:28 +0000236 MachODebugMapParser Parser(InputFile, PrependPath, Verbose);
Frederic Riss231f7142014-12-12 17:31:24 +0000237 return Parser.parse();
238}
239}
240}