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