Re-sort #include lines again, prior to moving headers around.
[oota-llvm.git] / tools / llvm-readobj / ARMEHABIPrinter.h
1 //===--- ARMEHABIPrinter.h - ARM EHABI Unwind Information Printer ----------===//
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 #ifndef LLVM_READOBJ_ARMEHABI_PRINTER_H
11 #define LLVM_READOBJ_ARMEHABI_PRINTER_H
12
13 #include "StreamWriter.h"
14 #include "llvm/Object/ELF.h"
15 #include "llvm/Object/ELFTypes.h"
16 #include "llvm/Support/ARMEHABI.h"
17 #include "llvm/Support/Endian.h"
18 #include "llvm/Support/type_traits.h"
19
20 namespace llvm {
21 namespace ARM {
22 namespace EHABI {
23 template <typename ET>
24 class PrinterContext {
25   StreamWriter &SW;
26   const object::ELFFile<ET> *ELF;
27
28   typedef typename object::ELFFile<ET>::Elf_Sym Elf_Sym;
29   typedef typename object::ELFFile<ET>::Elf_Shdr Elf_Shdr;
30
31   typedef typename object::ELFFile<ET>::Elf_Rel_Iter Elf_Rel_iterator;
32   typedef typename object::ELFFile<ET>::Elf_Sym_Iter Elf_Sym_iterator;
33   typedef typename object::ELFFile<ET>::Elf_Shdr_Iter Elf_Shdr_iterator;
34
35   static const size_t IndexTableEntrySize;
36
37   static uint64_t PREL31(uint32_t Address, uint32_t Place) {
38     uint64_t Location = Address & 0x7fffffff;
39     if (Location & 0x04000000)
40       Location |= (uint64_t) ~0x7fffffff;
41     return Location + Place;
42   }
43
44   ErrorOr<StringRef> FunctionAtAddress(unsigned Section, uint64_t Address) const;
45   const Elf_Shdr *FindExceptionTable(unsigned IndexTableIndex,
46                                      off_t IndexTableOffset) const;
47
48   void PrintIndexTable(unsigned SectionIndex, const Elf_Shdr *IT) const;
49   void PrintExceptionTable(const Elf_Shdr *IT, const Elf_Shdr *EHT,
50                            uint64_t TableEntryOffset) const;
51   void PrintOpcodes(const uint8_t *Entry, size_t Length, off_t Offset) const;
52
53 public:
54   PrinterContext(StreamWriter &Writer, const object::ELFFile<ET> *File)
55     : SW(Writer), ELF(File) {}
56
57   void PrintUnwindInformation() const;
58 };
59
60 template <typename ET>
61 const size_t PrinterContext<ET>::IndexTableEntrySize = 8;
62
63 template <typename ET>
64 ErrorOr<StringRef> PrinterContext<ET>::FunctionAtAddress(unsigned Section,
65                                                          uint64_t Address) const {
66   for (Elf_Sym_iterator SI = ELF->begin_symbols(), SE = ELF->end_symbols();
67        SI != SE; ++SI)
68     if (SI->st_shndx == Section && SI->st_value == Address &&
69         SI->getType() == ELF::STT_FUNC)
70       return ELF->getSymbolName(SI);
71   return readobj_error::unknown_symbol;
72 }
73
74 template <typename ET>
75 const typename object::ELFFile<ET>::Elf_Shdr *
76 PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex,
77                                        off_t IndexTableOffset) const {
78   /// Iterate through the sections, searching for the relocation section
79   /// associated with the unwind index table section specified by
80   /// IndexSectionIndex.  Iterate the associated section searching for the
81   /// relocation associated with the index table entry specified by
82   /// IndexTableOffset.  The symbol is the section symbol for the exception
83   /// handling table.  Use this symbol to recover the actual exception handling
84   /// table.
85
86   for (Elf_Shdr_iterator SI = ELF->begin_sections(), SE = ELF->end_sections();
87        SI != SE; ++SI) {
88     if (SI->sh_type == ELF::SHT_REL && SI->sh_info == IndexSectionIndex) {
89       for (Elf_Rel_iterator RI = ELF->begin_rel(&*SI), RE = ELF->end_rel(&*SI);
90            RI != RE; ++RI) {
91         if (RI->r_offset == IndexTableOffset) {
92           typename object::ELFFile<ET>::Elf_Rela RelA;
93           RelA.r_offset = RI->r_offset;
94           RelA.r_info = RI->r_info;
95           RelA.r_addend = 0;
96
97           std::pair<const Elf_Shdr *, const Elf_Sym *> Symbol =
98             ELF->getRelocationSymbol(&(*SI), &RelA);
99
100           return ELF->getSection(Symbol.second);
101         }
102       }
103     }
104   }
105   return NULL;
106 }
107
108 template <typename ET>
109 void PrinterContext<ET>::PrintExceptionTable(const Elf_Shdr *IT,
110                                              const Elf_Shdr *EHT,
111                                              uint64_t TableEntryOffset) const {
112   ErrorOr<ArrayRef<uint8_t> > Contents = ELF->getSectionContents(EHT);
113   if (!Contents)
114     return;
115
116   /// ARM EHABI Section 6.2 - The generic model
117   ///
118   /// An exception-handling table entry for the generic model is laid out as:
119   ///
120   ///  3 3
121   ///  1 0                            0
122   /// +-+------------------------------+
123   /// |0|  personality routine offset  |
124   /// +-+------------------------------+
125   /// |  personality routine data ...  |
126   ///
127   ///
128   /// ARM EHABI Section 6.3 - The ARM-defined compact model
129   ///
130   /// An exception-handling table entry for the compact model looks like:
131   ///
132   ///  3 3 2 2  2 2
133   ///  1 0 8 7  4 3                     0
134   /// +-+---+----+-----------------------+
135   /// |1| 0 | Ix | data for pers routine |
136   /// +-+---+----+-----------------------+
137   /// |  more personality routine data   |
138
139   const support::ulittle32_t Word =
140     *reinterpret_cast<const support::ulittle32_t *>(Contents->data() + TableEntryOffset);
141
142   if (Word & 0x80000000) {
143     SW.printString("Model", StringRef("Compact"));
144
145     unsigned PersonalityIndex = (Word & 0x0f000000) >> 24;
146     SW.printNumber("PersonalityIndex", PersonalityIndex);
147
148     switch (PersonalityIndex) {
149     case AEABI_UNWIND_CPP_PR0:
150       llvm_unreachable("Personality 0 should be compact inline!");
151       break;
152     case AEABI_UNWIND_CPP_PR1:
153     case AEABI_UNWIND_CPP_PR2:
154       unsigned AdditionalWords = (Word & 0x00ff0000) >> 16;
155       PrintOpcodes(Contents->data() + TableEntryOffset, 2 + 4 * AdditionalWords,
156                    2);
157       break;
158     }
159   } else {
160     SW.printString("Model", StringRef("Generic"));
161
162     uint64_t Address = PREL31(Word, EHT->sh_addr);
163     SW.printHex("PersonalityRoutineAddress", Address);
164     if (ErrorOr<StringRef> Name = FunctionAtAddress(EHT->sh_link, Address))
165       SW.printString("PersonalityRoutineName", *Name);
166   }
167 }
168
169 template <typename ET>
170 void PrinterContext<ET>::PrintOpcodes(const uint8_t *Entry,
171                                       size_t Length, off_t Offset) const {
172   ListScope OCC(SW, "Opcodes");
173   for (unsigned OCI = Offset; OCI < Length + Offset; OCI++)
174     SW.printHex("Opcode", Entry[OCI ^ 0x3]);
175 }
176
177 template <typename ET>
178 void PrinterContext<ET>::PrintIndexTable(unsigned SectionIndex,
179                                          const Elf_Shdr *IT) const {
180   ErrorOr<ArrayRef<uint8_t> > Contents = ELF->getSectionContents(IT);
181   if (!Contents)
182     return;
183
184   /// ARM EHABI Section 5 - Index Table Entries
185   /// * The first word contains a PREL31 offset to the start of a function with
186   ///   bit 31 clear
187   /// * The second word contains one of:
188   ///   - The PREL31 offset of the start of the table entry for the function,
189   ///     with bit 31 clear
190   ///   - The exception-handling table entry itself with bit 31 set
191   ///   - The special bit pattern EXIDX_CANTUNWIND, indicating that associated
192   ///     frames cannot be unwound
193
194   const support::ulittle32_t *Data =
195     reinterpret_cast<const support::ulittle32_t *>(Contents->data());
196   const unsigned Entries = IT->sh_size / IndexTableEntrySize;
197
198   ListScope E(SW, "Entries");
199   for (unsigned Entry = 0; Entry < Entries; ++Entry) {
200     DictScope E(SW, "Entry");
201
202     const support::ulittle32_t Word0 =
203       Data[Entry * (IndexTableEntrySize / sizeof(*Data)) + 0];
204     const support::ulittle32_t Word1 =
205       Data[Entry * (IndexTableEntrySize / sizeof(*Data)) + 1];
206
207     if (Word0 & 0x80000000) {
208       errs() << "corrupt unwind data in section " << SectionIndex << "\n";
209       continue;
210     }
211
212     const uint64_t Offset = PREL31(Word0, IT->sh_addr);
213     SW.printHex("FunctionAddress", Offset);
214     if (ErrorOr<StringRef> Name = FunctionAtAddress(IT->sh_link, Offset))
215       SW.printString("FunctionName", *Name);
216
217     if (Word1 == EXIDX_CANTUNWIND) {
218       SW.printString("Model", StringRef("CantUnwind"));
219       continue;
220     }
221
222     if (Word1 & 0x80000000) {
223       SW.printString("Model", StringRef("Compact (Inline)"));
224
225       unsigned PersonalityIndex = (Word1 & 0x0f000000) >> 24;
226       SW.printNumber("PersonalityIndex", PersonalityIndex);
227
228       PrintOpcodes(Contents->data() + Entry * IndexTableEntrySize + 4, 3, 1);
229     } else {
230       const Elf_Shdr *EHT =
231         FindExceptionTable(SectionIndex, Entry * IndexTableEntrySize + 4);
232
233       if (ErrorOr<StringRef> Name = ELF->getSectionName(EHT))
234         SW.printString("ExceptionHandlingTable", *Name);
235
236       uint64_t TableEntryOffset = PREL31(Word1, IT->sh_addr);
237       SW.printHex("TableEntryOffset", TableEntryOffset);
238
239       PrintExceptionTable(IT, EHT, TableEntryOffset);
240     }
241   }
242 }
243
244 template <typename ET>
245 void PrinterContext<ET>::PrintUnwindInformation() const {
246   DictScope UI(SW, "UnwindInformation");
247
248   int SectionIndex = 0;
249   for (Elf_Shdr_iterator SI = ELF->begin_sections(), SE = ELF->end_sections();
250        SI != SE; ++SI, ++SectionIndex) {
251     if (SI->sh_type == ELF::SHT_ARM_EXIDX) {
252       const Elf_Shdr *IT = &(*SI);
253
254       DictScope UIT(SW, "UnwindIndexTable");
255
256       SW.printNumber("SectionIndex", SectionIndex);
257       if (ErrorOr<StringRef> SectionName = ELF->getSectionName(IT))
258         SW.printString("SectionName", *SectionName);
259       SW.printHex("SectionOffset", IT->sh_offset);
260
261       PrintIndexTable(SectionIndex, IT);
262     }
263   }
264 }
265 }
266 }
267 }
268
269 #endif
270