1 | //===--- ARMEHABIPrinter.h - ARM EHABI Unwind Information Printer ----------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLVM_TOOLS_LLVM_READOBJ_ARMEHABIPRINTER_H |
10 | #define LLVM_TOOLS_LLVM_READOBJ_ARMEHABIPRINTER_H |
11 | |
12 | #include "llvm-readobj.h" |
13 | #include "llvm/ADT/STLExtras.h" |
14 | #include "llvm/Object/ELF.h" |
15 | #include "llvm/Object/ELFTypes.h" |
16 | #include "llvm/Support/ARMEHABI.h" |
17 | #include "llvm/Support/Debug.h" |
18 | #include "llvm/Support/Endian.h" |
19 | #include "llvm/Support/Format.h" |
20 | #include "llvm/Support/ScopedPrinter.h" |
21 | #include "llvm/Support/type_traits.h" |
22 | |
23 | namespace llvm { |
24 | namespace ARM { |
25 | namespace EHABI { |
26 | |
27 | class OpcodeDecoder { |
28 | ScopedPrinter &SW; |
29 | raw_ostream &OS; |
30 | |
31 | struct RingEntry { |
32 | uint8_t Mask; |
33 | uint8_t Value; |
34 | void (OpcodeDecoder::*Routine)(const uint8_t *Opcodes, unsigned &OI); |
35 | }; |
36 | static ArrayRef<RingEntry> ring(); |
37 | |
38 | void Decode_00xxxxxx(const uint8_t *Opcodes, unsigned &OI); |
39 | void Decode_01xxxxxx(const uint8_t *Opcodes, unsigned &OI); |
40 | void Decode_1000iiii_iiiiiiii(const uint8_t *Opcodes, unsigned &OI); |
41 | void Decode_10011101(const uint8_t *Opcodes, unsigned &OI); |
42 | void Decode_10011111(const uint8_t *Opcodes, unsigned &OI); |
43 | void Decode_1001nnnn(const uint8_t *Opcodes, unsigned &OI); |
44 | void Decode_10100nnn(const uint8_t *Opcodes, unsigned &OI); |
45 | void Decode_10101nnn(const uint8_t *Opcodes, unsigned &OI); |
46 | void Decode_10110000(const uint8_t *Opcodes, unsigned &OI); |
47 | void Decode_10110001_0000iiii(const uint8_t *Opcodes, unsigned &OI); |
48 | void Decode_10110010_uleb128(const uint8_t *Opcodes, unsigned &OI); |
49 | void Decode_10110011_sssscccc(const uint8_t *Opcodes, unsigned &OI); |
50 | void Decode_101101nn(const uint8_t *Opcodes, unsigned &OI); |
51 | void Decode_10111nnn(const uint8_t *Opcodes, unsigned &OI); |
52 | void Decode_11000110_sssscccc(const uint8_t *Opcodes, unsigned &OI); |
53 | void Decode_11000111_0000iiii(const uint8_t *Opcodes, unsigned &OI); |
54 | void Decode_11001000_sssscccc(const uint8_t *Opcodes, unsigned &OI); |
55 | void Decode_11001001_sssscccc(const uint8_t *Opcodes, unsigned &OI); |
56 | void Decode_11001yyy(const uint8_t *Opcodes, unsigned &OI); |
57 | void Decode_11000nnn(const uint8_t *Opcodes, unsigned &OI); |
58 | void Decode_11010nnn(const uint8_t *Opcodes, unsigned &OI); |
59 | void Decode_11xxxyyy(const uint8_t *Opcodes, unsigned &OI); |
60 | |
61 | void PrintGPR(uint16_t GPRMask); |
62 | void PrintRegisters(uint32_t Mask, StringRef Prefix); |
63 | |
64 | public: |
65 | OpcodeDecoder(ScopedPrinter &SW) : SW(SW), OS(SW.getOStream()) {} |
66 | void Decode(const uint8_t *Opcodes, off_t Offset, size_t Length); |
67 | }; |
68 | |
69 | inline ArrayRef<OpcodeDecoder::RingEntry> OpcodeDecoder::ring() { |
70 | static const OpcodeDecoder::RingEntry Ring[] = { |
71 | {.Mask: 0xc0, .Value: 0x00, .Routine: &OpcodeDecoder::Decode_00xxxxxx}, |
72 | {.Mask: 0xc0, .Value: 0x40, .Routine: &OpcodeDecoder::Decode_01xxxxxx}, |
73 | {.Mask: 0xf0, .Value: 0x80, .Routine: &OpcodeDecoder::Decode_1000iiii_iiiiiiii}, |
74 | {.Mask: 0xff, .Value: 0x9d, .Routine: &OpcodeDecoder::Decode_10011101}, |
75 | {.Mask: 0xff, .Value: 0x9f, .Routine: &OpcodeDecoder::Decode_10011111}, |
76 | {.Mask: 0xf0, .Value: 0x90, .Routine: &OpcodeDecoder::Decode_1001nnnn}, |
77 | {.Mask: 0xf8, .Value: 0xa0, .Routine: &OpcodeDecoder::Decode_10100nnn}, |
78 | {.Mask: 0xf8, .Value: 0xa8, .Routine: &OpcodeDecoder::Decode_10101nnn}, |
79 | {.Mask: 0xff, .Value: 0xb0, .Routine: &OpcodeDecoder::Decode_10110000}, |
80 | {.Mask: 0xff, .Value: 0xb1, .Routine: &OpcodeDecoder::Decode_10110001_0000iiii}, |
81 | {.Mask: 0xff, .Value: 0xb2, .Routine: &OpcodeDecoder::Decode_10110010_uleb128}, |
82 | {.Mask: 0xff, .Value: 0xb3, .Routine: &OpcodeDecoder::Decode_10110011_sssscccc}, |
83 | {.Mask: 0xfc, .Value: 0xb4, .Routine: &OpcodeDecoder::Decode_101101nn}, |
84 | {.Mask: 0xf8, .Value: 0xb8, .Routine: &OpcodeDecoder::Decode_10111nnn}, |
85 | {.Mask: 0xff, .Value: 0xc6, .Routine: &OpcodeDecoder::Decode_11000110_sssscccc}, |
86 | {.Mask: 0xff, .Value: 0xc7, .Routine: &OpcodeDecoder::Decode_11000111_0000iiii}, |
87 | {.Mask: 0xff, .Value: 0xc8, .Routine: &OpcodeDecoder::Decode_11001000_sssscccc}, |
88 | {.Mask: 0xff, .Value: 0xc9, .Routine: &OpcodeDecoder::Decode_11001001_sssscccc}, |
89 | {.Mask: 0xc8, .Value: 0xc8, .Routine: &OpcodeDecoder::Decode_11001yyy}, |
90 | {.Mask: 0xf8, .Value: 0xc0, .Routine: &OpcodeDecoder::Decode_11000nnn}, |
91 | {.Mask: 0xf8, .Value: 0xd0, .Routine: &OpcodeDecoder::Decode_11010nnn}, |
92 | {.Mask: 0xc0, .Value: 0xc0, .Routine: &OpcodeDecoder::Decode_11xxxyyy}, |
93 | }; |
94 | return ArrayRef(Ring); |
95 | } |
96 | |
97 | inline void OpcodeDecoder::Decode_00xxxxxx(const uint8_t *Opcodes, |
98 | unsigned &OI) { |
99 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
100 | SW.startLine() << format(Fmt: "0x%02X ; vsp = vsp + %u\n" , Vals: Opcode, |
101 | Vals: ((Opcode & 0x3f) << 2) + 4); |
102 | } |
103 | inline void OpcodeDecoder::Decode_01xxxxxx(const uint8_t *Opcodes, |
104 | unsigned &OI) { |
105 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
106 | SW.startLine() << format(Fmt: "0x%02X ; vsp = vsp - %u\n" , Vals: Opcode, |
107 | Vals: ((Opcode & 0x3f) << 2) + 4); |
108 | } |
109 | inline void OpcodeDecoder::Decode_1000iiii_iiiiiiii(const uint8_t *Opcodes, |
110 | unsigned &OI) { |
111 | uint8_t Opcode0 = Opcodes[OI++ ^ 3]; |
112 | uint8_t Opcode1 = Opcodes[OI++ ^ 3]; |
113 | |
114 | uint16_t GPRMask = (Opcode1 << 4) | ((Opcode0 & 0x0f) << 12); |
115 | SW.startLine() |
116 | << format(Fmt: "0x%02X 0x%02X ; %s" , |
117 | Vals: Opcode0, Vals: Opcode1, Vals: GPRMask ? "pop " : "refuse to unwind" ); |
118 | if (GPRMask) |
119 | PrintGPR(GPRMask); |
120 | OS << '\n'; |
121 | } |
122 | inline void OpcodeDecoder::Decode_10011101(const uint8_t *Opcodes, |
123 | unsigned &OI) { |
124 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
125 | SW.startLine() << format(Fmt: "0x%02X ; reserved (ARM MOVrr)\n" , Vals: Opcode); |
126 | } |
127 | inline void OpcodeDecoder::Decode_10011111(const uint8_t *Opcodes, |
128 | unsigned &OI) { |
129 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
130 | SW.startLine() << format(Fmt: "0x%02X ; reserved (WiMMX MOVrr)\n" , Vals: Opcode); |
131 | } |
132 | inline void OpcodeDecoder::Decode_1001nnnn(const uint8_t *Opcodes, |
133 | unsigned &OI) { |
134 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
135 | SW.startLine() << format(Fmt: "0x%02X ; vsp = r%u\n" , Vals: Opcode, Vals: (Opcode & 0x0f)); |
136 | } |
137 | inline void OpcodeDecoder::Decode_10100nnn(const uint8_t *Opcodes, |
138 | unsigned &OI) { |
139 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
140 | SW.startLine() << format(Fmt: "0x%02X ; pop " , Vals: Opcode); |
141 | PrintGPR(GPRMask: (((1 << ((Opcode & 0x7) + 1)) - 1) << 4)); |
142 | OS << '\n'; |
143 | } |
144 | inline void OpcodeDecoder::Decode_10101nnn(const uint8_t *Opcodes, |
145 | unsigned &OI) { |
146 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
147 | SW.startLine() << format(Fmt: "0x%02X ; pop " , Vals: Opcode); |
148 | PrintGPR(GPRMask: (((1 << ((Opcode & 0x7) + 1)) - 1) << 4) | (1 << 14)); |
149 | OS << '\n'; |
150 | } |
151 | inline void OpcodeDecoder::Decode_10110000(const uint8_t *Opcodes, |
152 | unsigned &OI) { |
153 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
154 | SW.startLine() << format(Fmt: "0x%02X ; finish\n" , Vals: Opcode); |
155 | } |
156 | inline void OpcodeDecoder::Decode_10110001_0000iiii(const uint8_t *Opcodes, |
157 | unsigned &OI) { |
158 | uint8_t Opcode0 = Opcodes[OI++ ^ 3]; |
159 | uint8_t Opcode1 = Opcodes[OI++ ^ 3]; |
160 | |
161 | SW.startLine() << format(Fmt: "0x%02X 0x%02X ; %s" , Vals: Opcode0, Vals: Opcode1, |
162 | Vals: (Opcode1 & 0xf0) ? "spare" : "pop " ); |
163 | if (((Opcode1 & 0xf0) == 0x00) && Opcode1) |
164 | PrintGPR(GPRMask: (Opcode1 & 0x0f)); |
165 | OS << '\n'; |
166 | } |
167 | inline void OpcodeDecoder::Decode_10110010_uleb128(const uint8_t *Opcodes, |
168 | unsigned &OI) { |
169 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
170 | SW.startLine() << format(Fmt: "0x%02X " , Vals: Opcode); |
171 | |
172 | SmallVector<uint8_t, 4> ULEB; |
173 | do { ULEB.push_back(Elt: Opcodes[OI ^ 3]); } while (Opcodes[OI++ ^ 3] & 0x80); |
174 | |
175 | for (unsigned BI = 0, BE = ULEB.size(); BI != BE; ++BI) |
176 | OS << format(Fmt: "0x%02X " , Vals: ULEB[BI]); |
177 | |
178 | uint64_t Value = 0; |
179 | for (unsigned BI = 0, BE = ULEB.size(); BI != BE; ++BI) |
180 | Value = Value | ((ULEB[BI] & 0x7f) << (7 * BI)); |
181 | |
182 | OS << format(Fmt: "; vsp = vsp + %" PRIu64 "\n" , Vals: 0x204 + (Value << 2)); |
183 | } |
184 | inline void OpcodeDecoder::Decode_10110011_sssscccc(const uint8_t *Opcodes, |
185 | unsigned &OI) { |
186 | uint8_t Opcode0 = Opcodes[OI++ ^ 3]; |
187 | uint8_t Opcode1 = Opcodes[OI++ ^ 3]; |
188 | SW.startLine() << format(Fmt: "0x%02X 0x%02X ; pop " , Vals: Opcode0, Vals: Opcode1); |
189 | uint8_t Start = ((Opcode1 & 0xf0) >> 4); |
190 | uint8_t Count = ((Opcode1 & 0x0f) >> 0); |
191 | PrintRegisters(Mask: (((1 << (Count + 1)) - 1) << Start), Prefix: "d" ); |
192 | OS << '\n'; |
193 | } |
194 | inline void OpcodeDecoder::Decode_101101nn(const uint8_t *Opcodes, |
195 | unsigned &OI) { |
196 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
197 | SW.startLine() << format(Fmt: "0x%02X ; %s\n" , Vals: Opcode, |
198 | Vals: (Opcode == 0xb4) ? "pop ra_auth_code" : "spare" ); |
199 | } |
200 | inline void OpcodeDecoder::Decode_10111nnn(const uint8_t *Opcodes, |
201 | unsigned &OI) { |
202 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
203 | SW.startLine() << format(Fmt: "0x%02X ; pop " , Vals: Opcode); |
204 | PrintRegisters(Mask: (((1 << ((Opcode & 0x07) + 1)) - 1) << 8), Prefix: "d" ); |
205 | OS << '\n'; |
206 | } |
207 | inline void OpcodeDecoder::Decode_11000110_sssscccc(const uint8_t *Opcodes, |
208 | unsigned &OI) { |
209 | uint8_t Opcode0 = Opcodes[OI++ ^ 3]; |
210 | uint8_t Opcode1 = Opcodes[OI++ ^ 3]; |
211 | SW.startLine() << format(Fmt: "0x%02X 0x%02X ; pop " , Vals: Opcode0, Vals: Opcode1); |
212 | uint8_t Start = ((Opcode1 & 0xf0) >> 4); |
213 | uint8_t Count = ((Opcode1 & 0x0f) >> 0); |
214 | PrintRegisters(Mask: (((1 << (Count + 1)) - 1) << Start), Prefix: "wR" ); |
215 | OS << '\n'; |
216 | } |
217 | inline void OpcodeDecoder::Decode_11000111_0000iiii(const uint8_t *Opcodes, |
218 | unsigned &OI) { |
219 | uint8_t Opcode0 = Opcodes[OI++ ^ 3]; |
220 | uint8_t Opcode1 = Opcodes[OI++ ^ 3]; |
221 | SW.startLine() |
222 | << format(Fmt: "0x%02X 0x%02X ; %s" , Vals: Opcode0, Vals: Opcode1, |
223 | Vals: ((Opcode1 & 0xf0) || Opcode1 == 0x00) ? "spare" : "pop " ); |
224 | if ((Opcode1 & 0xf0) == 0x00 && Opcode1) |
225 | PrintRegisters(Mask: Opcode1 & 0x0f, Prefix: "wCGR" ); |
226 | OS << '\n'; |
227 | } |
228 | inline void OpcodeDecoder::Decode_11001000_sssscccc(const uint8_t *Opcodes, |
229 | unsigned &OI) { |
230 | uint8_t Opcode0 = Opcodes[OI++ ^ 3]; |
231 | uint8_t Opcode1 = Opcodes[OI++ ^ 3]; |
232 | SW.startLine() << format(Fmt: "0x%02X 0x%02X ; pop " , Vals: Opcode0, Vals: Opcode1); |
233 | uint8_t Start = 16 + ((Opcode1 & 0xf0) >> 4); |
234 | uint8_t Count = ((Opcode1 & 0x0f) >> 0); |
235 | PrintRegisters(Mask: (((1 << (Count + 1)) - 1) << Start), Prefix: "d" ); |
236 | OS << '\n'; |
237 | } |
238 | inline void OpcodeDecoder::Decode_11001001_sssscccc(const uint8_t *Opcodes, |
239 | unsigned &OI) { |
240 | uint8_t Opcode0 = Opcodes[OI++ ^ 3]; |
241 | uint8_t Opcode1 = Opcodes[OI++ ^ 3]; |
242 | SW.startLine() << format(Fmt: "0x%02X 0x%02X ; pop " , Vals: Opcode0, Vals: Opcode1); |
243 | uint8_t Start = ((Opcode1 & 0xf0) >> 4); |
244 | uint8_t Count = ((Opcode1 & 0x0f) >> 0); |
245 | PrintRegisters(Mask: (((1 << (Count + 1)) - 1) << Start), Prefix: "d" ); |
246 | OS << '\n'; |
247 | } |
248 | inline void OpcodeDecoder::Decode_11001yyy(const uint8_t *Opcodes, |
249 | unsigned &OI) { |
250 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
251 | SW.startLine() << format(Fmt: "0x%02X ; spare\n" , Vals: Opcode); |
252 | } |
253 | inline void OpcodeDecoder::Decode_11000nnn(const uint8_t *Opcodes, |
254 | unsigned &OI) { |
255 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
256 | SW.startLine() << format(Fmt: "0x%02X ; pop " , Vals: Opcode); |
257 | PrintRegisters(Mask: (((1 << ((Opcode & 0x07) + 1)) - 1) << 10), Prefix: "wR" ); |
258 | OS << '\n'; |
259 | } |
260 | inline void OpcodeDecoder::Decode_11010nnn(const uint8_t *Opcodes, |
261 | unsigned &OI) { |
262 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
263 | SW.startLine() << format(Fmt: "0x%02X ; pop " , Vals: Opcode); |
264 | PrintRegisters(Mask: (((1 << ((Opcode & 0x07) + 1)) - 1) << 8), Prefix: "d" ); |
265 | OS << '\n'; |
266 | } |
267 | inline void OpcodeDecoder::Decode_11xxxyyy(const uint8_t *Opcodes, |
268 | unsigned &OI) { |
269 | uint8_t Opcode = Opcodes[OI++ ^ 3]; |
270 | SW.startLine() << format(Fmt: "0x%02X ; spare\n" , Vals: Opcode); |
271 | } |
272 | |
273 | inline void OpcodeDecoder::PrintGPR(uint16_t GPRMask) { |
274 | static const char *GPRRegisterNames[16] = { |
275 | "r0" , "r1" , "r2" , "r3" , "r4" , "r5" , "r6" , "r7" , "r8" , "r9" , "r10" , |
276 | "fp" , "ip" , "sp" , "lr" , "pc" |
277 | }; |
278 | |
279 | OS << '{'; |
280 | bool Comma = false; |
281 | for (unsigned RI = 0, RE = 17; RI < RE; ++RI) { |
282 | if (GPRMask & (1 << RI)) { |
283 | if (Comma) |
284 | OS << ", " ; |
285 | OS << GPRRegisterNames[RI]; |
286 | Comma = true; |
287 | } |
288 | } |
289 | OS << '}'; |
290 | } |
291 | |
292 | inline void OpcodeDecoder::PrintRegisters(uint32_t VFPMask, StringRef Prefix) { |
293 | OS << '{'; |
294 | bool Comma = false; |
295 | for (unsigned RI = 0, RE = 32; RI < RE; ++RI) { |
296 | if (VFPMask & (1 << RI)) { |
297 | if (Comma) |
298 | OS << ", " ; |
299 | OS << Prefix << RI; |
300 | Comma = true; |
301 | } |
302 | } |
303 | OS << '}'; |
304 | } |
305 | |
306 | inline void OpcodeDecoder::Decode(const uint8_t *Opcodes, off_t Offset, |
307 | size_t Length) { |
308 | for (unsigned OCI = Offset; OCI < Length + Offset; ) { |
309 | bool Decoded = false; |
310 | for (const auto &RE : ring()) { |
311 | if ((Opcodes[OCI ^ 3] & RE.Mask) == RE.Value) { |
312 | (this->*RE.Routine)(Opcodes, OCI); |
313 | Decoded = true; |
314 | break; |
315 | } |
316 | } |
317 | if (!Decoded) |
318 | SW.startLine() << format(Fmt: "0x%02X ; reserved\n" , Vals: Opcodes[OCI++ ^ 3]); |
319 | } |
320 | } |
321 | |
322 | template <typename ET> |
323 | class PrinterContext { |
324 | typedef typename ET::Sym Elf_Sym; |
325 | typedef typename ET::Shdr Elf_Shdr; |
326 | typedef typename ET::Rel Elf_Rel; |
327 | typedef typename ET::Word Elf_Word; |
328 | |
329 | ScopedPrinter &SW; |
330 | const object::ELFFile<ET> &ELF; |
331 | StringRef FileName; |
332 | const Elf_Shdr *Symtab; |
333 | ArrayRef<Elf_Word> ShndxTable; |
334 | |
335 | static const size_t IndexTableEntrySize; |
336 | |
337 | static uint64_t PREL31(uint32_t Address, uint32_t Place) { |
338 | uint64_t Location = Address & 0x7fffffff; |
339 | if (Location & 0x40000000) |
340 | Location |= (uint64_t) ~0x7fffffff; |
341 | return Location + Place; |
342 | } |
343 | |
344 | ErrorOr<StringRef> |
345 | FunctionAtAddress(uint64_t Address, |
346 | std::optional<unsigned> SectionIndex) const; |
347 | const Elf_Shdr *FindExceptionTable(unsigned IndexTableIndex, |
348 | off_t IndexTableOffset) const; |
349 | |
350 | void PrintIndexTable(unsigned SectionIndex, const Elf_Shdr *IT) const; |
351 | void PrintExceptionTable(const Elf_Shdr &EHT, |
352 | uint64_t TableEntryOffset) const; |
353 | void PrintOpcodes(const uint8_t *Entry, size_t Length, off_t Offset) const; |
354 | |
355 | public: |
356 | PrinterContext(ScopedPrinter &SW, const object::ELFFile<ET> &ELF, |
357 | StringRef FileName, const Elf_Shdr *Symtab) |
358 | : SW(SW), ELF(ELF), FileName(FileName), Symtab(Symtab) {} |
359 | |
360 | void PrintUnwindInformation() const; |
361 | }; |
362 | |
363 | template <typename ET> |
364 | const size_t PrinterContext<ET>::IndexTableEntrySize = 8; |
365 | |
366 | template <typename ET> |
367 | ErrorOr<StringRef> PrinterContext<ET>::FunctionAtAddress( |
368 | uint64_t Address, std::optional<unsigned> SectionIndex) const { |
369 | if (!Symtab) |
370 | return inconvertibleErrorCode(); |
371 | auto StrTableOrErr = ELF.getStringTableForSymtab(*Symtab); |
372 | if (!StrTableOrErr) |
373 | reportError(StrTableOrErr.takeError(), FileName); |
374 | StringRef StrTable = *StrTableOrErr; |
375 | |
376 | for (const Elf_Sym &Sym : unwrapOrError(FileName, ELF.symbols(Symtab))) { |
377 | if (SectionIndex && *SectionIndex != Sym.st_shndx) |
378 | continue; |
379 | |
380 | if (Sym.st_value == Address && Sym.getType() == ELF::STT_FUNC) { |
381 | auto NameOrErr = Sym.getName(StrTable); |
382 | if (!NameOrErr) { |
383 | // TODO: Actually report errors helpfully. |
384 | consumeError(NameOrErr.takeError()); |
385 | return inconvertibleErrorCode(); |
386 | } |
387 | return *NameOrErr; |
388 | } |
389 | } |
390 | |
391 | return inconvertibleErrorCode(); |
392 | } |
393 | |
394 | template <typename ET> |
395 | const typename ET::Shdr * |
396 | PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex, |
397 | off_t IndexTableOffset) const { |
398 | /// Iterate through the sections, searching for the relocation section |
399 | /// associated with the unwind index table section specified by |
400 | /// IndexSectionIndex. Iterate the associated section searching for the |
401 | /// relocation associated with the index table entry specified by |
402 | /// IndexTableOffset. The symbol is the section symbol for the exception |
403 | /// handling table. Use this symbol to recover the actual exception handling |
404 | /// table. |
405 | |
406 | for (const Elf_Shdr &Sec : unwrapOrError(FileName, ELF.sections())) { |
407 | if (Sec.sh_type != ELF::SHT_REL || Sec.sh_info != IndexSectionIndex) |
408 | continue; |
409 | |
410 | auto SymTabOrErr = ELF.getSection(Sec.sh_link); |
411 | if (!SymTabOrErr) |
412 | reportError(SymTabOrErr.takeError(), FileName); |
413 | const Elf_Shdr *SymTab = *SymTabOrErr; |
414 | |
415 | for (const Elf_Rel &R : unwrapOrError(FileName, ELF.rels(Sec))) { |
416 | if (R.r_offset != static_cast<unsigned>(IndexTableOffset)) |
417 | continue; |
418 | |
419 | typename ET::Rela RelA; |
420 | RelA.r_offset = R.r_offset; |
421 | RelA.r_info = R.r_info; |
422 | RelA.r_addend = 0; |
423 | |
424 | const Elf_Sym *Symbol = |
425 | unwrapOrError(FileName, ELF.getRelocationSymbol(RelA, SymTab)); |
426 | |
427 | auto Ret = ELF.getSection(*Symbol, SymTab, ShndxTable); |
428 | if (!Ret) |
429 | report_fatal_error(reason: Twine(errorToErrorCode(Ret.takeError()).message())); |
430 | return *Ret; |
431 | } |
432 | } |
433 | return nullptr; |
434 | } |
435 | |
436 | template <typename ET> |
437 | static const typename ET::Shdr * |
438 | findSectionContainingAddress(const object::ELFFile<ET> &Obj, StringRef FileName, |
439 | uint64_t Address) { |
440 | for (const typename ET::Shdr &Sec : unwrapOrError(FileName, Obj.sections())) |
441 | if (Address >= Sec.sh_addr && Address < Sec.sh_addr + Sec.sh_size) |
442 | return &Sec; |
443 | return nullptr; |
444 | } |
445 | |
446 | template <typename ET> |
447 | void PrinterContext<ET>::PrintExceptionTable(const Elf_Shdr &EHT, |
448 | uint64_t TableEntryOffset) const { |
449 | // TODO: handle failure. |
450 | Expected<ArrayRef<uint8_t>> Contents = ELF.getSectionContents(EHT); |
451 | if (!Contents) |
452 | return; |
453 | |
454 | /// ARM EHABI Section 6.2 - The generic model |
455 | /// |
456 | /// An exception-handling table entry for the generic model is laid out as: |
457 | /// |
458 | /// 3 3 |
459 | /// 1 0 0 |
460 | /// +-+------------------------------+ |
461 | /// |0| personality routine offset | |
462 | /// +-+------------------------------+ |
463 | /// | personality routine data ... | |
464 | /// |
465 | /// |
466 | /// ARM EHABI Section 6.3 - The ARM-defined compact model |
467 | /// |
468 | /// An exception-handling table entry for the compact model looks like: |
469 | /// |
470 | /// 3 3 2 2 2 2 |
471 | /// 1 0 8 7 4 3 0 |
472 | /// +-+---+----+-----------------------+ |
473 | /// |1| 0 | Ix | data for pers routine | |
474 | /// +-+---+----+-----------------------+ |
475 | /// | more personality routine data | |
476 | |
477 | const support::ulittle32_t Word = |
478 | *reinterpret_cast<const support::ulittle32_t *>(Contents->data() + TableEntryOffset); |
479 | |
480 | if (Word & 0x80000000) { |
481 | SW.printString(Label: "Model" , Value: StringRef("Compact" )); |
482 | |
483 | unsigned PersonalityIndex = (Word & 0x0f000000) >> 24; |
484 | SW.printNumber(Label: "PersonalityIndex" , Value: PersonalityIndex); |
485 | |
486 | switch (PersonalityIndex) { |
487 | case AEABI_UNWIND_CPP_PR0: |
488 | PrintOpcodes(Entry: Contents->data() + TableEntryOffset, Length: 3, Offset: 1); |
489 | break; |
490 | case AEABI_UNWIND_CPP_PR1: |
491 | case AEABI_UNWIND_CPP_PR2: |
492 | unsigned AdditionalWords = (Word & 0x00ff0000) >> 16; |
493 | PrintOpcodes(Entry: Contents->data() + TableEntryOffset, Length: 2 + 4 * AdditionalWords, |
494 | Offset: 2); |
495 | break; |
496 | } |
497 | } else { |
498 | SW.printString(Label: "Model" , Value: StringRef("Generic" )); |
499 | const bool IsRelocatable = ELF.getHeader().e_type == ELF::ET_REL; |
500 | uint64_t Address = IsRelocatable |
501 | ? PREL31(Address: Word, Place: EHT.sh_addr) |
502 | : PREL31(Address: Word, Place: EHT.sh_addr + TableEntryOffset); |
503 | SW.printHex(Label: "PersonalityRoutineAddress" , Value: Address); |
504 | std::optional<unsigned> SecIndex = |
505 | IsRelocatable ? std::optional<unsigned>(EHT.sh_link) : std::nullopt; |
506 | if (ErrorOr<StringRef> Name = FunctionAtAddress(Address, SectionIndex: SecIndex)) |
507 | SW.printString(Label: "PersonalityRoutineName" , Value: *Name); |
508 | } |
509 | } |
510 | |
511 | template <typename ET> |
512 | void PrinterContext<ET>::PrintOpcodes(const uint8_t *Entry, |
513 | size_t Length, off_t Offset) const { |
514 | ListScope OCC(SW, "Opcodes" ); |
515 | OpcodeDecoder(SW).Decode(Opcodes: Entry, Offset, Length); |
516 | } |
517 | |
518 | template <typename ET> |
519 | void PrinterContext<ET>::PrintIndexTable(unsigned SectionIndex, |
520 | const Elf_Shdr *IT) const { |
521 | // TODO: handle failure. |
522 | Expected<ArrayRef<uint8_t>> Contents = ELF.getSectionContents(*IT); |
523 | if (!Contents) |
524 | return; |
525 | |
526 | /// ARM EHABI Section 5 - Index Table Entries |
527 | /// * The first word contains a PREL31 offset to the start of a function with |
528 | /// bit 31 clear |
529 | /// * The second word contains one of: |
530 | /// - The PREL31 offset of the start of the table entry for the function, |
531 | /// with bit 31 clear |
532 | /// - The exception-handling table entry itself with bit 31 set |
533 | /// - The special bit pattern EXIDX_CANTUNWIND, indicating that associated |
534 | /// frames cannot be unwound |
535 | |
536 | const support::ulittle32_t *Data = |
537 | reinterpret_cast<const support::ulittle32_t *>(Contents->data()); |
538 | const unsigned Entries = IT->sh_size / IndexTableEntrySize; |
539 | const bool IsRelocatable = ELF.getHeader().e_type == ELF::ET_REL; |
540 | |
541 | ListScope E(SW, "Entries" ); |
542 | for (unsigned Entry = 0; Entry < Entries; ++Entry) { |
543 | DictScope E(SW, "Entry" ); |
544 | |
545 | const support::ulittle32_t Word0 = |
546 | Data[Entry * (IndexTableEntrySize / sizeof(*Data)) + 0]; |
547 | const support::ulittle32_t Word1 = |
548 | Data[Entry * (IndexTableEntrySize / sizeof(*Data)) + 1]; |
549 | |
550 | if (Word0 & 0x80000000) { |
551 | errs() << "corrupt unwind data in section " << SectionIndex << "\n" ; |
552 | continue; |
553 | } |
554 | |
555 | // FIXME: For a relocatable object ideally we might want to: |
556 | // 1) Find a relocation for the offset of Word0. |
557 | // 2) Verify this relocation is of an expected type (R_ARM_PREL31) and |
558 | // verify the symbol index. |
559 | // 3) Resolve the relocation using it's symbol value, addend etc. |
560 | // Currently the code assumes that Word0 contains an addend of a |
561 | // R_ARM_PREL31 REL relocation that references a section symbol. RELA |
562 | // relocations are not supported and it works because addresses of sections |
563 | // are nulls in relocatable objects. |
564 | // |
565 | // For a non-relocatable object, Word0 contains a place-relative signed |
566 | // offset to the referenced entity. |
567 | const uint64_t Address = |
568 | IsRelocatable |
569 | ? PREL31(Address: Word0, Place: IT->sh_addr) |
570 | : PREL31(Address: Word0, Place: IT->sh_addr + Entry * IndexTableEntrySize); |
571 | SW.printHex(Label: "FunctionAddress" , Value: Address); |
572 | |
573 | // In a relocatable output we might have many .ARM.exidx sections linked to |
574 | // their code sections via the sh_link field. For a non-relocatable ELF file |
575 | // the sh_link field is not reliable, because we have one .ARM.exidx section |
576 | // normally, but might have many code sections. |
577 | std::optional<unsigned> SecIndex = |
578 | IsRelocatable ? std::optional<unsigned>(IT->sh_link) : std::nullopt; |
579 | if (ErrorOr<StringRef> Name = FunctionAtAddress(Address, SectionIndex: SecIndex)) |
580 | SW.printString(Label: "FunctionName" , Value: *Name); |
581 | |
582 | if (Word1 == EXIDX_CANTUNWIND) { |
583 | SW.printString(Label: "Model" , Value: StringRef("CantUnwind" )); |
584 | continue; |
585 | } |
586 | |
587 | if (Word1 & 0x80000000) { |
588 | SW.printString(Label: "Model" , Value: StringRef("Compact (Inline)" )); |
589 | |
590 | unsigned PersonalityIndex = (Word1 & 0x0f000000) >> 24; |
591 | SW.printNumber(Label: "PersonalityIndex" , Value: PersonalityIndex); |
592 | |
593 | PrintOpcodes(Entry: Contents->data() + Entry * IndexTableEntrySize + 4, Length: 3, Offset: 1); |
594 | } else { |
595 | const Elf_Shdr *EHT; |
596 | uint64_t TableEntryAddress; |
597 | if (IsRelocatable) { |
598 | TableEntryAddress = PREL31(Address: Word1, Place: IT->sh_addr); |
599 | EHT = FindExceptionTable(IndexSectionIndex: SectionIndex, IndexTableOffset: Entry * IndexTableEntrySize + 4); |
600 | } else { |
601 | TableEntryAddress = |
602 | PREL31(Address: Word1, Place: IT->sh_addr + Entry * IndexTableEntrySize + 4); |
603 | EHT = findSectionContainingAddress(ELF, FileName, TableEntryAddress); |
604 | } |
605 | |
606 | if (EHT) |
607 | // TODO: handle failure. |
608 | if (Expected<StringRef> Name = ELF.getSectionName(*EHT)) |
609 | SW.printString(Label: "ExceptionHandlingTable" , Value: *Name); |
610 | |
611 | SW.printHex(Label: IsRelocatable ? "TableEntryOffset" : "TableEntryAddress" , |
612 | Value: TableEntryAddress); |
613 | if (EHT) { |
614 | if (IsRelocatable) |
615 | PrintExceptionTable(EHT: *EHT, TableEntryOffset: TableEntryAddress); |
616 | else |
617 | PrintExceptionTable(EHT: *EHT, TableEntryOffset: TableEntryAddress - EHT->sh_addr); |
618 | } |
619 | } |
620 | } |
621 | } |
622 | |
623 | template <typename ET> |
624 | void PrinterContext<ET>::PrintUnwindInformation() const { |
625 | DictScope UI(SW, "UnwindInformation" ); |
626 | |
627 | int SectionIndex = 0; |
628 | for (const Elf_Shdr &Sec : unwrapOrError(FileName, ELF.sections())) { |
629 | if (Sec.sh_type == ELF::SHT_ARM_EXIDX) { |
630 | DictScope UIT(SW, "UnwindIndexTable" ); |
631 | |
632 | SW.printNumber(Label: "SectionIndex" , Value: SectionIndex); |
633 | // TODO: handle failure. |
634 | if (Expected<StringRef> SectionName = ELF.getSectionName(Sec)) |
635 | SW.printString(Label: "SectionName" , Value: *SectionName); |
636 | SW.printHex("SectionOffset" , Sec.sh_offset); |
637 | |
638 | PrintIndexTable(SectionIndex, IT: &Sec); |
639 | } |
640 | ++SectionIndex; |
641 | } |
642 | } |
643 | } |
644 | } |
645 | } |
646 | |
647 | #endif |
648 | |