Initial dsymutil tool commit.
[oota-llvm.git] / tools / dsymutil / MachODebugMapParser.h
1 //===- tools/dsymutil/MachODebugMapParser.h - 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 /// \file
11 ///
12 /// This file contains the class declaration for the code that parses STABS
13 /// debug maps that are embedded in the binaries symbol tables.
14 ///
15 //===----------------------------------------------------------------------===//
16 #ifndef DSYMUTIL_MACHODEBUGMAPPARSER_H
17 #define DSYMUTIL_MACHODEBUGMAPPARSER_H
18
19 #include "DebugMap.h"
20
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Object/MachO.h"
23 #include "llvm/Object/Error.h"
24
25 namespace llvm {
26
27 class MachODebugMapParser {
28 public:
29  MachODebugMapParser(StringRef BinaryPath)
30    : BinaryPath(BinaryPath) {}
31
32   /// \brief Add a prefix to every object file path before trying to
33   /// open it.
34   void setPreprendPath(StringRef Prefix) { PathPrefix = Prefix; }
35
36   /// \brief Parses and returns the DebugMap of the input binary.
37   /// \returns an error in case the provided BinaryPath doesn't exist
38   /// or isn't of a supported type.
39   ErrorOr<std::unique_ptr<DebugMap>> parse();
40
41 private:
42   std::string BinaryPath;
43   std::string PathPrefix;
44
45   /// OwningBinary constructed from the BinaryPath.
46   object::OwningBinary<object::MachOObjectFile> MainOwningBinary;
47   /// Map of the binary symbol addresses.
48   StringMap<uint64_t> MainBinarySymbolAddresses;
49   /// The constructed DebugMap.
50   std::unique_ptr<DebugMap> Result;
51
52   /// Handle to the currently processed object file.
53   object::OwningBinary<object::MachOObjectFile> CurrentObjectFile;
54   /// Map of the currently processed object file symbol addresses.
55   StringMap<uint64_t> CurrentObjectAddresses;
56   /// Element of the debug map corresponfing to the current object file.
57   DebugMapObject *CurrentDebugMapObject;
58
59   void switchToNewDebugMapObject(StringRef Filename);
60   void resetParserState();
61   uint64_t getMainBinarySymbolAddress(StringRef Name);
62   void loadMainBinarySymbols();
63   void loadCurrentObjectFileSymbols();
64   void handleStabSymbolTableEntry(uint32_t StringIndex, uint8_t Type,
65                                   uint8_t SectionIndex, uint16_t Flags,
66                                   uint64_t Value);
67
68   template <typename STEType> void handleStabDebugMapEntry(const STEType &STE) {
69     handleStabSymbolTableEntry(STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc,
70                                STE.n_value);
71   }
72 };
73
74 }
75
76 #endif // DSYMUTIL_MACHODEBUGMAPPARSER_H