Adds the next bit of support for llvm-objdump’s -private-headers for executable Mach...
[oota-llvm.git] / include / llvm / Object / MachO.h
1 //===- MachO.h - MachO object file implementation ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the MachOObjectFile class, which implement the ObjectFile
11 // interface for MachO files.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_OBJECT_MACHO_H
16 #define LLVM_OBJECT_MACHO_H
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/MachO.h"
23
24 namespace llvm {
25 namespace object {
26
27 /// DiceRef - This is a value type class that represents a single
28 /// data in code entry in the table in a Mach-O object file.
29 class DiceRef {
30   DataRefImpl DicePimpl;
31   const ObjectFile *OwningObject;
32
33 public:
34   DiceRef() : OwningObject(nullptr) { }
35
36   DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
37
38   bool operator==(const DiceRef &Other) const;
39   bool operator<(const DiceRef &Other) const;
40
41   void moveNext();
42
43   std::error_code getOffset(uint32_t &Result) const;
44   std::error_code getLength(uint16_t &Result) const;
45   std::error_code getKind(uint16_t &Result) const;
46
47   DataRefImpl getRawDataRefImpl() const;
48   const ObjectFile *getObjectFile() const;
49 };
50 typedef content_iterator<DiceRef> dice_iterator;
51
52 /// ExportEntry encapsulates the current-state-of-the-walk used when doing a
53 /// non-recursive walk of the trie data structure.  This allows you to iterate
54 /// across all exported symbols using:
55 ///      for (const llvm::object::ExportEntry &AnExport : Obj->exports()) {
56 ///      }
57 class ExportEntry {
58 public:
59   ExportEntry(ArrayRef<uint8_t> Trie);
60
61   StringRef name() const;
62   uint64_t flags() const;
63   uint64_t address() const;
64   uint64_t other() const;
65   StringRef otherName() const;
66   uint32_t nodeOffset() const;
67
68   bool operator==(const ExportEntry &) const;
69
70   void moveNext();
71
72 private:
73   friend class MachOObjectFile;
74   void moveToFirst();
75   void moveToEnd();
76   uint64_t readULEB128(const uint8_t *&p);
77   void pushDownUntilBottom();
78   void pushNode(uint64_t Offset);
79
80   // Represents a node in the mach-o exports trie.
81   struct NodeState {
82     NodeState(const uint8_t *Ptr);
83     const uint8_t *Start;
84     const uint8_t *Current;
85     uint64_t Flags;
86     uint64_t Address;
87     uint64_t Other;
88     const char *ImportName;
89     unsigned ChildCount;
90     unsigned NextChildIndex;
91     unsigned ParentStringLength;
92     bool IsExportNode;
93   };
94
95   ArrayRef<uint8_t> Trie;
96   SmallString<256> CumulativeString;
97   SmallVector<NodeState, 16> Stack;
98   bool Malformed;
99   bool Done;
100 };
101 typedef content_iterator<ExportEntry> export_iterator;
102
103 class MachOObjectFile : public ObjectFile {
104 public:
105   struct LoadCommandInfo {
106     const char *Ptr;      // Where in memory the load command is.
107     MachO::load_command C; // The command itself.
108   };
109
110   MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
111                   std::error_code &EC);
112
113   void moveSymbolNext(DataRefImpl &Symb) const override;
114   std::error_code getSymbolName(DataRefImpl Symb,
115                                 StringRef &Res) const override;
116
117   // MachO specific.
118   std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
119
120   std::error_code getSymbolAddress(DataRefImpl Symb,
121                                    uint64_t &Res) const override;
122   std::error_code getSymbolAlignment(DataRefImpl Symb,
123                                      uint32_t &Res) const override;
124   std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
125   std::error_code getSymbolType(DataRefImpl Symb,
126                                 SymbolRef::Type &Res) const override;
127   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
128   std::error_code getSymbolSection(DataRefImpl Symb,
129                                    section_iterator &Res) const override;
130
131   void moveSectionNext(DataRefImpl &Sec) const override;
132   std::error_code getSectionName(DataRefImpl Sec,
133                                  StringRef &Res) const override;
134   std::error_code getSectionAddress(DataRefImpl Sec,
135                                     uint64_t &Res) const override;
136   std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
137   std::error_code getSectionContents(DataRefImpl Sec,
138                                      StringRef &Res) const override;
139   std::error_code getSectionAlignment(DataRefImpl Sec,
140                                       uint64_t &Res) const override;
141   std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
142   std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
143   std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
144   std::error_code isSectionRequiredForExecution(DataRefImpl Sec,
145                                                 bool &Res) const override;
146   std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
147   std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
148   std::error_code isSectionReadOnlyData(DataRefImpl Sec,
149                                         bool &Res) const override;
150   std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
151                                         bool &Result) const override;
152   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
153   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
154
155   void moveRelocationNext(DataRefImpl &Rel) const override;
156   std::error_code getRelocationAddress(DataRefImpl Rel,
157                                        uint64_t &Res) const override;
158   std::error_code getRelocationOffset(DataRefImpl Rel,
159                                       uint64_t &Res) const override;
160   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
161   std::error_code getRelocationType(DataRefImpl Rel,
162                                     uint64_t &Res) const override;
163   std::error_code
164   getRelocationTypeName(DataRefImpl Rel,
165                         SmallVectorImpl<char> &Result) const override;
166   std::error_code
167   getRelocationValueString(DataRefImpl Rel,
168                            SmallVectorImpl<char> &Result) const override;
169   std::error_code getRelocationHidden(DataRefImpl Rel,
170                                       bool &Result) const override;
171
172   // MachO specific.
173   std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
174
175   // TODO: Would be useful to have an iterator based version
176   // of the load command interface too.
177
178   basic_symbol_iterator symbol_begin_impl() const override;
179   basic_symbol_iterator symbol_end_impl() const override;
180
181   // MachO specific.
182   basic_symbol_iterator getSymbolByIndex(unsigned Index) const;
183
184   section_iterator section_begin() const override;
185   section_iterator section_end() const override;
186
187   uint8_t getBytesInAddress() const override;
188
189   StringRef getFileFormatName() const override;
190   unsigned getArch() const override;
191   Triple getArch(const char **McpuDefault, Triple *ThumbTriple) const;
192
193   relocation_iterator section_rel_begin(unsigned Index) const;
194   relocation_iterator section_rel_end(unsigned Index) const;
195
196   dice_iterator begin_dices() const;
197   dice_iterator end_dices() const;
198   
199   /// For use iterating over all exported symbols.
200   iterator_range<export_iterator> exports() const;
201   
202   /// For use examining a trie not in a MachOObjectFile.
203   static iterator_range<export_iterator> exports(ArrayRef<uint8_t> Trie);
204
205   // In a MachO file, sections have a segment name. This is used in the .o
206   // files. They have a single segment, but this field specifies which segment
207   // a section should be put in in the final object.
208   StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
209
210   // Names are stored as 16 bytes. These returns the raw 16 bytes without
211   // interpreting them as a C string.
212   ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
213   ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
214
215   // MachO specific Info about relocations.
216   bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
217   unsigned getPlainRelocationSymbolNum(
218                                     const MachO::any_relocation_info &RE) const;
219   bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
220   bool getScatteredRelocationScattered(
221                                     const MachO::any_relocation_info &RE) const;
222   uint32_t getScatteredRelocationValue(
223                                     const MachO::any_relocation_info &RE) const;
224   unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
225   unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
226   unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
227   unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
228   SectionRef getRelocationSection(const MachO::any_relocation_info &RE) const;
229
230   // Walk load commands.
231   LoadCommandInfo getFirstLoadCommandInfo() const;
232   LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
233
234   // MachO specific structures.
235   MachO::section getSection(DataRefImpl DRI) const;
236   MachO::section_64 getSection64(DataRefImpl DRI) const;
237   MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
238   MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
239   MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
240   MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
241
242   MachO::linkedit_data_command
243   getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
244   MachO::segment_command
245   getSegmentLoadCommand(const LoadCommandInfo &L) const;
246   MachO::segment_command_64
247   getSegment64LoadCommand(const LoadCommandInfo &L) const;
248   MachO::linker_options_command
249   getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
250   MachO::version_min_command
251   getVersionMinLoadCommand(const LoadCommandInfo &L) const;
252   MachO::dylib_command
253   getDylibIDLoadCommand(const LoadCommandInfo &L) const;
254   MachO::dyld_info_command
255   getDyldInfoLoadCommand(const LoadCommandInfo &L) const;
256   MachO::dylinker_command
257   getDylinkerCommand(const LoadCommandInfo &L) const;
258   MachO::uuid_command
259   getUuidCommand(const LoadCommandInfo &L) const;
260   MachO::source_version_command
261   getSourceVersionCommand(const LoadCommandInfo &L) const;
262   MachO::entry_point_command
263   getEntryPointCommand(const LoadCommandInfo &L) const;
264
265   MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
266   MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
267   MachO::mach_header getHeader() const;
268   MachO::mach_header_64 getHeader64() const;
269   uint32_t
270   getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
271                               unsigned Index) const;
272   MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
273                                                     unsigned Index) const;
274   MachO::symtab_command getSymtabLoadCommand() const;
275   MachO::dysymtab_command getDysymtabLoadCommand() const;
276   MachO::linkedit_data_command getDataInCodeLoadCommand() const;
277   ArrayRef<uint8_t> getDyldInfoRebaseOpcodes() const;
278   ArrayRef<uint8_t> getDyldInfoBindOpcodes() const;
279   ArrayRef<uint8_t> getDyldInfoWeakBindOpcodes() const;
280   ArrayRef<uint8_t> getDyldInfoLazyBindOpcodes() const;
281   ArrayRef<uint8_t> getDyldInfoExportsTrie() const;
282
283   StringRef getStringTableData() const;
284   bool is64Bit() const;
285   void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
286
287   static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
288                                          StringRef &Suffix);
289
290   static Triple::ArchType getArch(uint32_t CPUType);
291   static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
292                         const char **McpuDefault = nullptr);
293   static Triple getThumbArch(uint32_t CPUType, uint32_t CPUSubType,
294                              const char **McpuDefault = nullptr);
295   static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
296                         const char **McpuDefault, Triple *ThumbTriple);
297   static bool isValidArch(StringRef ArchFlag);
298   static Triple getHostArch();
299
300   bool isRelocatableObject() const override;
301
302   static bool classof(const Binary *v) {
303     return v->isMachO();
304   }
305
306 private:
307   typedef SmallVector<const char*, 1> SectionList;
308   SectionList Sections;
309   typedef SmallVector<const char*, 1> LibraryList;
310   LibraryList Libraries;
311   typedef SmallVector<StringRef, 1> LibraryShortName;
312   mutable LibraryShortName LibrariesShortNames;
313   const char *SymtabLoadCmd;
314   const char *DysymtabLoadCmd;
315   const char *DataInCodeLoadCmd;
316   const char *DyldInfoLoadCmd;
317 };
318
319 /// DiceRef
320 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
321   : DicePimpl(DiceP) , OwningObject(Owner) {}
322
323 inline bool DiceRef::operator==(const DiceRef &Other) const {
324   return DicePimpl == Other.DicePimpl;
325 }
326
327 inline bool DiceRef::operator<(const DiceRef &Other) const {
328   return DicePimpl < Other.DicePimpl;
329 }
330
331 inline void DiceRef::moveNext() {
332   const MachO::data_in_code_entry *P =
333     reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
334   DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
335 }
336
337 // Since a Mach-O data in code reference, a DiceRef, can only be created when
338 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
339 // the methods that get the values of the fields of the reference.
340
341 inline std::error_code DiceRef::getOffset(uint32_t &Result) const {
342   const MachOObjectFile *MachOOF =
343     static_cast<const MachOObjectFile *>(OwningObject);
344   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
345   Result = Dice.offset;
346   return object_error::success;
347 }
348
349 inline std::error_code DiceRef::getLength(uint16_t &Result) const {
350   const MachOObjectFile *MachOOF =
351     static_cast<const MachOObjectFile *>(OwningObject);
352   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
353   Result = Dice.length;
354   return object_error::success;
355 }
356
357 inline std::error_code DiceRef::getKind(uint16_t &Result) const {
358   const MachOObjectFile *MachOOF =
359     static_cast<const MachOObjectFile *>(OwningObject);
360   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
361   Result = Dice.kind;
362   return object_error::success;
363 }
364
365 inline DataRefImpl DiceRef::getRawDataRefImpl() const {
366   return DicePimpl;
367 }
368
369 inline const ObjectFile *DiceRef::getObjectFile() const {
370   return OwningObject;
371 }
372
373 }
374 }
375
376 #endif
377