xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-readobj/ARMEHABIPrinter.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===--- ARMEHABIPrinter.h - ARM EHABI Unwind Information Printer ----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #ifndef LLVM_TOOLS_LLVM_READOBJ_ARMEHABIPRINTER_H
100b57cec5SDimitry Andric #define LLVM_TOOLS_LLVM_READOBJ_ARMEHABIPRINTER_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "llvm-readobj.h"
130b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
140b57cec5SDimitry Andric #include "llvm/Object/ELF.h"
150b57cec5SDimitry Andric #include "llvm/Object/ELFTypes.h"
160b57cec5SDimitry Andric #include "llvm/Support/ARMEHABI.h"
170b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
180b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
190b57cec5SDimitry Andric #include "llvm/Support/Format.h"
200b57cec5SDimitry Andric #include "llvm/Support/ScopedPrinter.h"
210b57cec5SDimitry Andric #include "llvm/Support/type_traits.h"
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric namespace llvm {
240b57cec5SDimitry Andric namespace ARM {
250b57cec5SDimitry Andric namespace EHABI {
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric class OpcodeDecoder {
280b57cec5SDimitry Andric   ScopedPrinter &SW;
290b57cec5SDimitry Andric   raw_ostream &OS;
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric   struct RingEntry {
320b57cec5SDimitry Andric     uint8_t Mask;
330b57cec5SDimitry Andric     uint8_t Value;
340b57cec5SDimitry Andric     void (OpcodeDecoder::*Routine)(const uint8_t *Opcodes, unsigned &OI);
350b57cec5SDimitry Andric   };
360b57cec5SDimitry Andric   static ArrayRef<RingEntry> ring();
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   void Decode_00xxxxxx(const uint8_t *Opcodes, unsigned &OI);
390b57cec5SDimitry Andric   void Decode_01xxxxxx(const uint8_t *Opcodes, unsigned &OI);
400b57cec5SDimitry Andric   void Decode_1000iiii_iiiiiiii(const uint8_t *Opcodes, unsigned &OI);
410b57cec5SDimitry Andric   void Decode_10011101(const uint8_t *Opcodes, unsigned &OI);
420b57cec5SDimitry Andric   void Decode_10011111(const uint8_t *Opcodes, unsigned &OI);
430b57cec5SDimitry Andric   void Decode_1001nnnn(const uint8_t *Opcodes, unsigned &OI);
440b57cec5SDimitry Andric   void Decode_10100nnn(const uint8_t *Opcodes, unsigned &OI);
450b57cec5SDimitry Andric   void Decode_10101nnn(const uint8_t *Opcodes, unsigned &OI);
460b57cec5SDimitry Andric   void Decode_10110000(const uint8_t *Opcodes, unsigned &OI);
470b57cec5SDimitry Andric   void Decode_10110001_0000iiii(const uint8_t *Opcodes, unsigned &OI);
480b57cec5SDimitry Andric   void Decode_10110010_uleb128(const uint8_t *Opcodes, unsigned &OI);
490b57cec5SDimitry Andric   void Decode_10110011_sssscccc(const uint8_t *Opcodes, unsigned &OI);
500b57cec5SDimitry Andric   void Decode_101101nn(const uint8_t *Opcodes, unsigned &OI);
510b57cec5SDimitry Andric   void Decode_10111nnn(const uint8_t *Opcodes, unsigned &OI);
520b57cec5SDimitry Andric   void Decode_11000110_sssscccc(const uint8_t *Opcodes, unsigned &OI);
530b57cec5SDimitry Andric   void Decode_11000111_0000iiii(const uint8_t *Opcodes, unsigned &OI);
540b57cec5SDimitry Andric   void Decode_11001000_sssscccc(const uint8_t *Opcodes, unsigned &OI);
550b57cec5SDimitry Andric   void Decode_11001001_sssscccc(const uint8_t *Opcodes, unsigned &OI);
560b57cec5SDimitry Andric   void Decode_11001yyy(const uint8_t *Opcodes, unsigned &OI);
570b57cec5SDimitry Andric   void Decode_11000nnn(const uint8_t *Opcodes, unsigned &OI);
580b57cec5SDimitry Andric   void Decode_11010nnn(const uint8_t *Opcodes, unsigned &OI);
590b57cec5SDimitry Andric   void Decode_11xxxyyy(const uint8_t *Opcodes, unsigned &OI);
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   void PrintGPR(uint16_t GPRMask);
620b57cec5SDimitry Andric   void PrintRegisters(uint32_t Mask, StringRef Prefix);
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric public:
OpcodeDecoder(ScopedPrinter & SW)650b57cec5SDimitry Andric   OpcodeDecoder(ScopedPrinter &SW) : SW(SW), OS(SW.getOStream()) {}
660b57cec5SDimitry Andric   void Decode(const uint8_t *Opcodes, off_t Offset, size_t Length);
670b57cec5SDimitry Andric };
680b57cec5SDimitry Andric 
ring()690b57cec5SDimitry Andric inline ArrayRef<OpcodeDecoder::RingEntry> OpcodeDecoder::ring() {
700b57cec5SDimitry Andric   static const OpcodeDecoder::RingEntry Ring[] = {
710b57cec5SDimitry Andric       {0xc0, 0x00, &OpcodeDecoder::Decode_00xxxxxx},
720b57cec5SDimitry Andric       {0xc0, 0x40, &OpcodeDecoder::Decode_01xxxxxx},
730b57cec5SDimitry Andric       {0xf0, 0x80, &OpcodeDecoder::Decode_1000iiii_iiiiiiii},
740b57cec5SDimitry Andric       {0xff, 0x9d, &OpcodeDecoder::Decode_10011101},
750b57cec5SDimitry Andric       {0xff, 0x9f, &OpcodeDecoder::Decode_10011111},
760b57cec5SDimitry Andric       {0xf0, 0x90, &OpcodeDecoder::Decode_1001nnnn},
770b57cec5SDimitry Andric       {0xf8, 0xa0, &OpcodeDecoder::Decode_10100nnn},
780b57cec5SDimitry Andric       {0xf8, 0xa8, &OpcodeDecoder::Decode_10101nnn},
790b57cec5SDimitry Andric       {0xff, 0xb0, &OpcodeDecoder::Decode_10110000},
800b57cec5SDimitry Andric       {0xff, 0xb1, &OpcodeDecoder::Decode_10110001_0000iiii},
810b57cec5SDimitry Andric       {0xff, 0xb2, &OpcodeDecoder::Decode_10110010_uleb128},
820b57cec5SDimitry Andric       {0xff, 0xb3, &OpcodeDecoder::Decode_10110011_sssscccc},
830b57cec5SDimitry Andric       {0xfc, 0xb4, &OpcodeDecoder::Decode_101101nn},
840b57cec5SDimitry Andric       {0xf8, 0xb8, &OpcodeDecoder::Decode_10111nnn},
850b57cec5SDimitry Andric       {0xff, 0xc6, &OpcodeDecoder::Decode_11000110_sssscccc},
860b57cec5SDimitry Andric       {0xff, 0xc7, &OpcodeDecoder::Decode_11000111_0000iiii},
870b57cec5SDimitry Andric       {0xff, 0xc8, &OpcodeDecoder::Decode_11001000_sssscccc},
880b57cec5SDimitry Andric       {0xff, 0xc9, &OpcodeDecoder::Decode_11001001_sssscccc},
890b57cec5SDimitry Andric       {0xc8, 0xc8, &OpcodeDecoder::Decode_11001yyy},
900b57cec5SDimitry Andric       {0xf8, 0xc0, &OpcodeDecoder::Decode_11000nnn},
910b57cec5SDimitry Andric       {0xf8, 0xd0, &OpcodeDecoder::Decode_11010nnn},
920b57cec5SDimitry Andric       {0xc0, 0xc0, &OpcodeDecoder::Decode_11xxxyyy},
930b57cec5SDimitry Andric   };
94*bdd1243dSDimitry Andric   return ArrayRef(Ring);
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric 
Decode_00xxxxxx(const uint8_t * Opcodes,unsigned & OI)970b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_00xxxxxx(const uint8_t *Opcodes,
980b57cec5SDimitry Andric                                            unsigned &OI) {
990b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
1000b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; vsp = vsp + %u\n", Opcode,
1010b57cec5SDimitry Andric                            ((Opcode & 0x3f) << 2) + 4);
1020b57cec5SDimitry Andric }
Decode_01xxxxxx(const uint8_t * Opcodes,unsigned & OI)1030b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_01xxxxxx(const uint8_t *Opcodes,
1040b57cec5SDimitry Andric                                            unsigned &OI) {
1050b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
1060b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; vsp = vsp - %u\n", Opcode,
1070b57cec5SDimitry Andric                            ((Opcode & 0x3f) << 2) + 4);
1080b57cec5SDimitry Andric }
Decode_1000iiii_iiiiiiii(const uint8_t * Opcodes,unsigned & OI)1090b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_1000iiii_iiiiiiii(const uint8_t *Opcodes,
1100b57cec5SDimitry Andric                                                     unsigned &OI) {
1110b57cec5SDimitry Andric   uint8_t Opcode0 = Opcodes[OI++ ^ 3];
1120b57cec5SDimitry Andric   uint8_t Opcode1 = Opcodes[OI++ ^ 3];
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   uint16_t GPRMask = (Opcode1 << 4) | ((Opcode0 & 0x0f) << 12);
1150b57cec5SDimitry Andric   SW.startLine()
1160b57cec5SDimitry Andric     << format("0x%02X 0x%02X ; %s",
1170b57cec5SDimitry Andric               Opcode0, Opcode1, GPRMask ? "pop " : "refuse to unwind");
1180b57cec5SDimitry Andric   if (GPRMask)
1190b57cec5SDimitry Andric     PrintGPR(GPRMask);
1200b57cec5SDimitry Andric   OS << '\n';
1210b57cec5SDimitry Andric }
Decode_10011101(const uint8_t * Opcodes,unsigned & OI)1220b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_10011101(const uint8_t *Opcodes,
1230b57cec5SDimitry Andric                                            unsigned &OI) {
1240b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
1250b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; reserved (ARM MOVrr)\n", Opcode);
1260b57cec5SDimitry Andric }
Decode_10011111(const uint8_t * Opcodes,unsigned & OI)1270b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_10011111(const uint8_t *Opcodes,
1280b57cec5SDimitry Andric                                            unsigned &OI) {
1290b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
1300b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; reserved (WiMMX MOVrr)\n", Opcode);
1310b57cec5SDimitry Andric }
Decode_1001nnnn(const uint8_t * Opcodes,unsigned & OI)1320b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_1001nnnn(const uint8_t *Opcodes,
1330b57cec5SDimitry Andric                                            unsigned &OI) {
1340b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
1350b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; vsp = r%u\n", Opcode, (Opcode & 0x0f));
1360b57cec5SDimitry Andric }
Decode_10100nnn(const uint8_t * Opcodes,unsigned & OI)1370b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_10100nnn(const uint8_t *Opcodes,
1380b57cec5SDimitry Andric                                            unsigned &OI) {
1390b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
1400b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; pop ", Opcode);
1410b57cec5SDimitry Andric   PrintGPR((((1 << ((Opcode & 0x7) + 1)) - 1) << 4));
1420b57cec5SDimitry Andric   OS << '\n';
1430b57cec5SDimitry Andric }
Decode_10101nnn(const uint8_t * Opcodes,unsigned & OI)1440b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_10101nnn(const uint8_t *Opcodes,
1450b57cec5SDimitry Andric                                            unsigned &OI) {
1460b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
1470b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; pop ", Opcode);
1480b57cec5SDimitry Andric   PrintGPR((((1 << ((Opcode & 0x7) + 1)) - 1) << 4) | (1 << 14));
1490b57cec5SDimitry Andric   OS << '\n';
1500b57cec5SDimitry Andric }
Decode_10110000(const uint8_t * Opcodes,unsigned & OI)1510b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_10110000(const uint8_t *Opcodes,
1520b57cec5SDimitry Andric                                            unsigned &OI) {
1530b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
1540b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; finish\n", Opcode);
1550b57cec5SDimitry Andric }
Decode_10110001_0000iiii(const uint8_t * Opcodes,unsigned & OI)1560b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_10110001_0000iiii(const uint8_t *Opcodes,
1570b57cec5SDimitry Andric                                                     unsigned &OI) {
1580b57cec5SDimitry Andric   uint8_t Opcode0 = Opcodes[OI++ ^ 3];
1590b57cec5SDimitry Andric   uint8_t Opcode1 = Opcodes[OI++ ^ 3];
1600b57cec5SDimitry Andric 
1610eae32dcSDimitry Andric   SW.startLine() << format("0x%02X 0x%02X ; %s", Opcode0, Opcode1,
1620eae32dcSDimitry Andric                            (Opcode1 & 0xf0) ? "spare" : "pop ");
1630b57cec5SDimitry Andric   if (((Opcode1 & 0xf0) == 0x00) && Opcode1)
1640b57cec5SDimitry Andric     PrintGPR((Opcode1 & 0x0f));
1650b57cec5SDimitry Andric   OS << '\n';
1660b57cec5SDimitry Andric }
Decode_10110010_uleb128(const uint8_t * Opcodes,unsigned & OI)1670b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_10110010_uleb128(const uint8_t *Opcodes,
1680b57cec5SDimitry Andric                                                    unsigned &OI) {
1690b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
1700b57cec5SDimitry Andric   SW.startLine() << format("0x%02X ", Opcode);
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   SmallVector<uint8_t, 4> ULEB;
1730b57cec5SDimitry Andric   do { ULEB.push_back(Opcodes[OI ^ 3]); } while (Opcodes[OI++ ^ 3] & 0x80);
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   for (unsigned BI = 0, BE = ULEB.size(); BI != BE; ++BI)
1760b57cec5SDimitry Andric     OS << format("0x%02X ", ULEB[BI]);
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric   uint64_t Value = 0;
1790b57cec5SDimitry Andric   for (unsigned BI = 0, BE = ULEB.size(); BI != BE; ++BI)
1800b57cec5SDimitry Andric     Value = Value | ((ULEB[BI] & 0x7f) << (7 * BI));
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   OS << format("; vsp = vsp + %" PRIu64 "\n", 0x204 + (Value << 2));
1830b57cec5SDimitry Andric }
Decode_10110011_sssscccc(const uint8_t * Opcodes,unsigned & OI)1840b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_10110011_sssscccc(const uint8_t *Opcodes,
1850b57cec5SDimitry Andric                                                     unsigned &OI) {
1860b57cec5SDimitry Andric   uint8_t Opcode0 = Opcodes[OI++ ^ 3];
1870b57cec5SDimitry Andric   uint8_t Opcode1 = Opcodes[OI++ ^ 3];
1880b57cec5SDimitry Andric   SW.startLine() << format("0x%02X 0x%02X ; pop ", Opcode0, Opcode1);
1890b57cec5SDimitry Andric   uint8_t Start = ((Opcode1 & 0xf0) >> 4);
1900b57cec5SDimitry Andric   uint8_t Count = ((Opcode1 & 0x0f) >> 0);
1910b57cec5SDimitry Andric   PrintRegisters((((1 << (Count + 1)) - 1) << Start), "d");
1920b57cec5SDimitry Andric   OS << '\n';
1930b57cec5SDimitry Andric }
Decode_101101nn(const uint8_t * Opcodes,unsigned & OI)1940b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_101101nn(const uint8_t *Opcodes,
1950b57cec5SDimitry Andric                                            unsigned &OI) {
1960b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
1970eae32dcSDimitry Andric   SW.startLine() << format("0x%02X      ; %s\n", Opcode,
1980eae32dcSDimitry Andric                            (Opcode == 0xb4) ? "pop ra_auth_code" : "spare");
1990b57cec5SDimitry Andric }
Decode_10111nnn(const uint8_t * Opcodes,unsigned & OI)2000b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_10111nnn(const uint8_t *Opcodes,
2010b57cec5SDimitry Andric                                            unsigned &OI) {
2020b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
2030b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; pop ", Opcode);
2040b57cec5SDimitry Andric   PrintRegisters((((1 << ((Opcode & 0x07) + 1)) - 1) << 8), "d");
2050b57cec5SDimitry Andric   OS << '\n';
2060b57cec5SDimitry Andric }
Decode_11000110_sssscccc(const uint8_t * Opcodes,unsigned & OI)2070b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_11000110_sssscccc(const uint8_t *Opcodes,
2080b57cec5SDimitry Andric                                                     unsigned &OI) {
2090b57cec5SDimitry Andric   uint8_t Opcode0 = Opcodes[OI++ ^ 3];
2100b57cec5SDimitry Andric   uint8_t Opcode1 = Opcodes[OI++ ^ 3];
2110b57cec5SDimitry Andric   SW.startLine() << format("0x%02X 0x%02X ; pop ", Opcode0, Opcode1);
2120b57cec5SDimitry Andric   uint8_t Start = ((Opcode1 & 0xf0) >> 4);
2130b57cec5SDimitry Andric   uint8_t Count = ((Opcode1 & 0x0f) >> 0);
2140b57cec5SDimitry Andric   PrintRegisters((((1 << (Count + 1)) - 1) << Start), "wR");
2150b57cec5SDimitry Andric   OS << '\n';
2160b57cec5SDimitry Andric }
Decode_11000111_0000iiii(const uint8_t * Opcodes,unsigned & OI)2170b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_11000111_0000iiii(const uint8_t *Opcodes,
2180b57cec5SDimitry Andric                                                     unsigned &OI) {
2190b57cec5SDimitry Andric   uint8_t Opcode0 = Opcodes[OI++ ^ 3];
2200b57cec5SDimitry Andric   uint8_t Opcode1 = Opcodes[OI++ ^ 3];
2210b57cec5SDimitry Andric   SW.startLine()
2220b57cec5SDimitry Andric     << format("0x%02X 0x%02X ; %s", Opcode0, Opcode1,
2230b57cec5SDimitry Andric               ((Opcode1 & 0xf0) || Opcode1 == 0x00) ? "spare" : "pop ");
2240b57cec5SDimitry Andric   if ((Opcode1 & 0xf0) == 0x00 && Opcode1)
2250b57cec5SDimitry Andric       PrintRegisters(Opcode1 & 0x0f, "wCGR");
2260b57cec5SDimitry Andric   OS << '\n';
2270b57cec5SDimitry Andric }
Decode_11001000_sssscccc(const uint8_t * Opcodes,unsigned & OI)2280b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_11001000_sssscccc(const uint8_t *Opcodes,
2290b57cec5SDimitry Andric                                                     unsigned &OI) {
2300b57cec5SDimitry Andric   uint8_t Opcode0 = Opcodes[OI++ ^ 3];
2310b57cec5SDimitry Andric   uint8_t Opcode1 = Opcodes[OI++ ^ 3];
2320b57cec5SDimitry Andric   SW.startLine() << format("0x%02X 0x%02X ; pop ", Opcode0, Opcode1);
2330b57cec5SDimitry Andric   uint8_t Start = 16 + ((Opcode1 & 0xf0) >> 4);
2340b57cec5SDimitry Andric   uint8_t Count = ((Opcode1 & 0x0f) >> 0);
2350b57cec5SDimitry Andric   PrintRegisters((((1 << (Count + 1)) - 1) << Start), "d");
2360b57cec5SDimitry Andric   OS << '\n';
2370b57cec5SDimitry Andric }
Decode_11001001_sssscccc(const uint8_t * Opcodes,unsigned & OI)2380b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_11001001_sssscccc(const uint8_t *Opcodes,
2390b57cec5SDimitry Andric                                                     unsigned &OI) {
2400b57cec5SDimitry Andric   uint8_t Opcode0 = Opcodes[OI++ ^ 3];
2410b57cec5SDimitry Andric   uint8_t Opcode1 = Opcodes[OI++ ^ 3];
2420b57cec5SDimitry Andric   SW.startLine() << format("0x%02X 0x%02X ; pop ", Opcode0, Opcode1);
2430b57cec5SDimitry Andric   uint8_t Start = ((Opcode1 & 0xf0) >> 4);
2440b57cec5SDimitry Andric   uint8_t Count = ((Opcode1 & 0x0f) >> 0);
2450b57cec5SDimitry Andric   PrintRegisters((((1 << (Count + 1)) - 1) << Start), "d");
2460b57cec5SDimitry Andric   OS << '\n';
2470b57cec5SDimitry Andric }
Decode_11001yyy(const uint8_t * Opcodes,unsigned & OI)2480b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_11001yyy(const uint8_t *Opcodes,
2490b57cec5SDimitry Andric                                            unsigned &OI) {
2500b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
2510b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; spare\n", Opcode);
2520b57cec5SDimitry Andric }
Decode_11000nnn(const uint8_t * Opcodes,unsigned & OI)2530b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_11000nnn(const uint8_t *Opcodes,
2540b57cec5SDimitry Andric                                            unsigned &OI) {
2550b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
2560b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; pop ", Opcode);
2570b57cec5SDimitry Andric   PrintRegisters((((1 << ((Opcode & 0x07) + 1)) - 1) << 10), "wR");
2580b57cec5SDimitry Andric   OS << '\n';
2590b57cec5SDimitry Andric }
Decode_11010nnn(const uint8_t * Opcodes,unsigned & OI)2600b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_11010nnn(const uint8_t *Opcodes,
2610b57cec5SDimitry Andric                                            unsigned &OI) {
2620b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
2630b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; pop ", Opcode);
2640b57cec5SDimitry Andric   PrintRegisters((((1 << ((Opcode & 0x07) + 1)) - 1) << 8), "d");
2650b57cec5SDimitry Andric   OS << '\n';
2660b57cec5SDimitry Andric }
Decode_11xxxyyy(const uint8_t * Opcodes,unsigned & OI)2670b57cec5SDimitry Andric inline void OpcodeDecoder::Decode_11xxxyyy(const uint8_t *Opcodes,
2680b57cec5SDimitry Andric                                            unsigned &OI) {
2690b57cec5SDimitry Andric   uint8_t Opcode = Opcodes[OI++ ^ 3];
2700b57cec5SDimitry Andric   SW.startLine() << format("0x%02X      ; spare\n", Opcode);
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric 
PrintGPR(uint16_t GPRMask)2730b57cec5SDimitry Andric inline void OpcodeDecoder::PrintGPR(uint16_t GPRMask) {
2740b57cec5SDimitry Andric   static const char *GPRRegisterNames[16] = {
2750b57cec5SDimitry Andric     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
2760b57cec5SDimitry Andric     "fp", "ip", "sp", "lr", "pc"
2770b57cec5SDimitry Andric   };
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric   OS << '{';
2800b57cec5SDimitry Andric   bool Comma = false;
2810b57cec5SDimitry Andric   for (unsigned RI = 0, RE = 17; RI < RE; ++RI) {
2820b57cec5SDimitry Andric     if (GPRMask & (1 << RI)) {
2830b57cec5SDimitry Andric       if (Comma)
2840b57cec5SDimitry Andric         OS << ", ";
2850b57cec5SDimitry Andric       OS << GPRRegisterNames[RI];
2860b57cec5SDimitry Andric       Comma = true;
2870b57cec5SDimitry Andric     }
2880b57cec5SDimitry Andric   }
2890b57cec5SDimitry Andric   OS << '}';
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric 
PrintRegisters(uint32_t VFPMask,StringRef Prefix)2920b57cec5SDimitry Andric inline void OpcodeDecoder::PrintRegisters(uint32_t VFPMask, StringRef Prefix) {
2930b57cec5SDimitry Andric   OS << '{';
2940b57cec5SDimitry Andric   bool Comma = false;
2950b57cec5SDimitry Andric   for (unsigned RI = 0, RE = 32; RI < RE; ++RI) {
2960b57cec5SDimitry Andric     if (VFPMask & (1 << RI)) {
2970b57cec5SDimitry Andric       if (Comma)
2980b57cec5SDimitry Andric         OS << ", ";
2990b57cec5SDimitry Andric       OS << Prefix << RI;
3000b57cec5SDimitry Andric       Comma = true;
3010b57cec5SDimitry Andric     }
3020b57cec5SDimitry Andric   }
3030b57cec5SDimitry Andric   OS << '}';
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric 
Decode(const uint8_t * Opcodes,off_t Offset,size_t Length)3060b57cec5SDimitry Andric inline void OpcodeDecoder::Decode(const uint8_t *Opcodes, off_t Offset,
3070b57cec5SDimitry Andric                                   size_t Length) {
3080b57cec5SDimitry Andric   for (unsigned OCI = Offset; OCI < Length + Offset; ) {
3090b57cec5SDimitry Andric     bool Decoded = false;
3100b57cec5SDimitry Andric     for (const auto &RE : ring()) {
3110b57cec5SDimitry Andric       if ((Opcodes[OCI ^ 3] & RE.Mask) == RE.Value) {
3120b57cec5SDimitry Andric         (this->*RE.Routine)(Opcodes, OCI);
3130b57cec5SDimitry Andric         Decoded = true;
3140b57cec5SDimitry Andric         break;
3150b57cec5SDimitry Andric       }
3160b57cec5SDimitry Andric     }
3170b57cec5SDimitry Andric     if (!Decoded)
3180b57cec5SDimitry Andric       SW.startLine() << format("0x%02X      ; reserved\n", Opcodes[OCI++ ^ 3]);
3190b57cec5SDimitry Andric   }
3200b57cec5SDimitry Andric }
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric template <typename ET>
3230b57cec5SDimitry Andric class PrinterContext {
3240b57cec5SDimitry Andric   typedef typename ET::Sym Elf_Sym;
3250b57cec5SDimitry Andric   typedef typename ET::Shdr Elf_Shdr;
3260b57cec5SDimitry Andric   typedef typename ET::Rel Elf_Rel;
3270b57cec5SDimitry Andric   typedef typename ET::Word Elf_Word;
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric   ScopedPrinter &SW;
330e8d8bef9SDimitry Andric   const object::ELFFile<ET> &ELF;
3318bcb0991SDimitry Andric   StringRef FileName;
3320b57cec5SDimitry Andric   const Elf_Shdr *Symtab;
3330b57cec5SDimitry Andric   ArrayRef<Elf_Word> ShndxTable;
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric   static const size_t IndexTableEntrySize;
3360b57cec5SDimitry Andric 
PREL31(uint32_t Address,uint32_t Place)3370b57cec5SDimitry Andric   static uint64_t PREL31(uint32_t Address, uint32_t Place) {
3380b57cec5SDimitry Andric     uint64_t Location = Address & 0x7fffffff;
339e8d8bef9SDimitry Andric     if (Location & 0x40000000)
3400b57cec5SDimitry Andric       Location |= (uint64_t) ~0x7fffffff;
3410b57cec5SDimitry Andric     return Location + Place;
3420b57cec5SDimitry Andric   }
3430b57cec5SDimitry Andric 
344*bdd1243dSDimitry Andric   ErrorOr<StringRef>
345*bdd1243dSDimitry Andric   FunctionAtAddress(uint64_t Address,
346*bdd1243dSDimitry Andric                     std::optional<unsigned> SectionIndex) const;
3470b57cec5SDimitry Andric   const Elf_Shdr *FindExceptionTable(unsigned IndexTableIndex,
3480b57cec5SDimitry Andric                                      off_t IndexTableOffset) const;
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric   void PrintIndexTable(unsigned SectionIndex, const Elf_Shdr *IT) const;
351e8d8bef9SDimitry Andric   void PrintExceptionTable(const Elf_Shdr &EHT,
3520b57cec5SDimitry Andric                            uint64_t TableEntryOffset) const;
3530b57cec5SDimitry Andric   void PrintOpcodes(const uint8_t *Entry, size_t Length, off_t Offset) const;
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric public:
PrinterContext(ScopedPrinter & SW,const object::ELFFile<ET> & ELF,StringRef FileName,const Elf_Shdr * Symtab)356e8d8bef9SDimitry Andric   PrinterContext(ScopedPrinter &SW, const object::ELFFile<ET> &ELF,
3578bcb0991SDimitry Andric                  StringRef FileName, const Elf_Shdr *Symtab)
3588bcb0991SDimitry Andric       : SW(SW), ELF(ELF), FileName(FileName), Symtab(Symtab) {}
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric   void PrintUnwindInformation() const;
3610b57cec5SDimitry Andric };
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric template <typename ET>
3640b57cec5SDimitry Andric const size_t PrinterContext<ET>::IndexTableEntrySize = 8;
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric template <typename ET>
FunctionAtAddress(uint64_t Address,std::optional<unsigned> SectionIndex)367*bdd1243dSDimitry Andric ErrorOr<StringRef> PrinterContext<ET>::FunctionAtAddress(
368*bdd1243dSDimitry Andric     uint64_t Address, std::optional<unsigned> SectionIndex) const {
3690b57cec5SDimitry Andric   if (!Symtab)
370e8d8bef9SDimitry Andric     return inconvertibleErrorCode();
371e8d8bef9SDimitry Andric   auto StrTableOrErr = ELF.getStringTableForSymtab(*Symtab);
3720b57cec5SDimitry Andric   if (!StrTableOrErr)
3738bcb0991SDimitry Andric     reportError(StrTableOrErr.takeError(), FileName);
3740b57cec5SDimitry Andric   StringRef StrTable = *StrTableOrErr;
3750b57cec5SDimitry Andric 
376e8d8bef9SDimitry Andric   for (const Elf_Sym &Sym : unwrapOrError(FileName, ELF.symbols(Symtab))) {
377e8d8bef9SDimitry Andric     if (SectionIndex && *SectionIndex != Sym.st_shndx)
378e8d8bef9SDimitry Andric       continue;
379e8d8bef9SDimitry Andric 
380e8d8bef9SDimitry Andric     if (Sym.st_value == Address && Sym.getType() == ELF::STT_FUNC) {
3810b57cec5SDimitry Andric       auto NameOrErr = Sym.getName(StrTable);
3820b57cec5SDimitry Andric       if (!NameOrErr) {
3830b57cec5SDimitry Andric         // TODO: Actually report errors helpfully.
3840b57cec5SDimitry Andric         consumeError(NameOrErr.takeError());
385e8d8bef9SDimitry Andric         return inconvertibleErrorCode();
3860b57cec5SDimitry Andric       }
3870b57cec5SDimitry Andric       return *NameOrErr;
3880b57cec5SDimitry Andric     }
389e8d8bef9SDimitry Andric   }
390e8d8bef9SDimitry Andric 
391e8d8bef9SDimitry Andric   return inconvertibleErrorCode();
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric template <typename ET>
3950b57cec5SDimitry Andric const typename ET::Shdr *
FindExceptionTable(unsigned IndexSectionIndex,off_t IndexTableOffset)3960b57cec5SDimitry Andric PrinterContext<ET>::FindExceptionTable(unsigned IndexSectionIndex,
3970b57cec5SDimitry Andric                                        off_t IndexTableOffset) const {
3980b57cec5SDimitry Andric   /// Iterate through the sections, searching for the relocation section
3990b57cec5SDimitry Andric   /// associated with the unwind index table section specified by
4000b57cec5SDimitry Andric   /// IndexSectionIndex.  Iterate the associated section searching for the
4010b57cec5SDimitry Andric   /// relocation associated with the index table entry specified by
4020b57cec5SDimitry Andric   /// IndexTableOffset.  The symbol is the section symbol for the exception
4030b57cec5SDimitry Andric   /// handling table.  Use this symbol to recover the actual exception handling
4040b57cec5SDimitry Andric   /// table.
4050b57cec5SDimitry Andric 
406e8d8bef9SDimitry Andric   for (const Elf_Shdr &Sec : unwrapOrError(FileName, ELF.sections())) {
4070b57cec5SDimitry Andric     if (Sec.sh_type != ELF::SHT_REL || Sec.sh_info != IndexSectionIndex)
4080b57cec5SDimitry Andric       continue;
4090b57cec5SDimitry Andric 
410e8d8bef9SDimitry Andric     auto SymTabOrErr = ELF.getSection(Sec.sh_link);
4110b57cec5SDimitry Andric     if (!SymTabOrErr)
4128bcb0991SDimitry Andric       reportError(SymTabOrErr.takeError(), FileName);
4130b57cec5SDimitry Andric     const Elf_Shdr *SymTab = *SymTabOrErr;
4140b57cec5SDimitry Andric 
415e8d8bef9SDimitry Andric     for (const Elf_Rel &R : unwrapOrError(FileName, ELF.rels(Sec))) {
4160b57cec5SDimitry Andric       if (R.r_offset != static_cast<unsigned>(IndexTableOffset))
4170b57cec5SDimitry Andric         continue;
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric       typename ET::Rela RelA;
4200b57cec5SDimitry Andric       RelA.r_offset = R.r_offset;
4210b57cec5SDimitry Andric       RelA.r_info = R.r_info;
4220b57cec5SDimitry Andric       RelA.r_addend = 0;
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric       const Elf_Sym *Symbol =
425e8d8bef9SDimitry Andric           unwrapOrError(FileName, ELF.getRelocationSymbol(RelA, SymTab));
4260b57cec5SDimitry Andric 
427e8d8bef9SDimitry Andric       auto Ret = ELF.getSection(*Symbol, SymTab, ShndxTable);
4280b57cec5SDimitry Andric       if (!Ret)
429349cc55cSDimitry Andric         report_fatal_error(Twine(errorToErrorCode(Ret.takeError()).message()));
4300b57cec5SDimitry Andric       return *Ret;
4310b57cec5SDimitry Andric     }
4320b57cec5SDimitry Andric   }
4330b57cec5SDimitry Andric   return nullptr;
4340b57cec5SDimitry Andric }
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric template <typename ET>
437e8d8bef9SDimitry Andric static const typename ET::Shdr *
findSectionContainingAddress(const object::ELFFile<ET> & Obj,StringRef FileName,uint64_t Address)438e8d8bef9SDimitry Andric findSectionContainingAddress(const object::ELFFile<ET> &Obj, StringRef FileName,
439e8d8bef9SDimitry Andric                              uint64_t Address) {
440e8d8bef9SDimitry Andric   for (const typename ET::Shdr &Sec : unwrapOrError(FileName, Obj.sections()))
441e8d8bef9SDimitry Andric     if (Address >= Sec.sh_addr && Address < Sec.sh_addr + Sec.sh_size)
442e8d8bef9SDimitry Andric       return &Sec;
443e8d8bef9SDimitry Andric   return nullptr;
444e8d8bef9SDimitry Andric }
445e8d8bef9SDimitry Andric 
446e8d8bef9SDimitry Andric template <typename ET>
PrintExceptionTable(const Elf_Shdr & EHT,uint64_t TableEntryOffset)447e8d8bef9SDimitry Andric void PrinterContext<ET>::PrintExceptionTable(const Elf_Shdr &EHT,
4480b57cec5SDimitry Andric                                              uint64_t TableEntryOffset) const {
449e8d8bef9SDimitry Andric   // TODO: handle failure.
450e8d8bef9SDimitry Andric   Expected<ArrayRef<uint8_t>> Contents = ELF.getSectionContents(EHT);
4510b57cec5SDimitry Andric   if (!Contents)
4520b57cec5SDimitry Andric     return;
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric   /// ARM EHABI Section 6.2 - The generic model
4550b57cec5SDimitry Andric   ///
4560b57cec5SDimitry Andric   /// An exception-handling table entry for the generic model is laid out as:
4570b57cec5SDimitry Andric   ///
4580b57cec5SDimitry Andric   ///  3 3
4590b57cec5SDimitry Andric   ///  1 0                            0
4600b57cec5SDimitry Andric   /// +-+------------------------------+
4610b57cec5SDimitry Andric   /// |0|  personality routine offset  |
4620b57cec5SDimitry Andric   /// +-+------------------------------+
4630b57cec5SDimitry Andric   /// |  personality routine data ...  |
4640b57cec5SDimitry Andric   ///
4650b57cec5SDimitry Andric   ///
4660b57cec5SDimitry Andric   /// ARM EHABI Section 6.3 - The ARM-defined compact model
4670b57cec5SDimitry Andric   ///
4680b57cec5SDimitry Andric   /// An exception-handling table entry for the compact model looks like:
4690b57cec5SDimitry Andric   ///
4700b57cec5SDimitry Andric   ///  3 3 2 2  2 2
4710b57cec5SDimitry Andric   ///  1 0 8 7  4 3                     0
4720b57cec5SDimitry Andric   /// +-+---+----+-----------------------+
4730b57cec5SDimitry Andric   /// |1| 0 | Ix | data for pers routine |
4740b57cec5SDimitry Andric   /// +-+---+----+-----------------------+
4750b57cec5SDimitry Andric   /// |  more personality routine data   |
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric   const support::ulittle32_t Word =
4780b57cec5SDimitry Andric     *reinterpret_cast<const support::ulittle32_t *>(Contents->data() + TableEntryOffset);
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   if (Word & 0x80000000) {
4810b57cec5SDimitry Andric     SW.printString("Model", StringRef("Compact"));
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric     unsigned PersonalityIndex = (Word & 0x0f000000) >> 24;
4840b57cec5SDimitry Andric     SW.printNumber("PersonalityIndex", PersonalityIndex);
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric     switch (PersonalityIndex) {
4870b57cec5SDimitry Andric     case AEABI_UNWIND_CPP_PR0:
4880b57cec5SDimitry Andric       PrintOpcodes(Contents->data() + TableEntryOffset, 3, 1);
4890b57cec5SDimitry Andric       break;
4900b57cec5SDimitry Andric     case AEABI_UNWIND_CPP_PR1:
4910b57cec5SDimitry Andric     case AEABI_UNWIND_CPP_PR2:
4920b57cec5SDimitry Andric       unsigned AdditionalWords = (Word & 0x00ff0000) >> 16;
4930b57cec5SDimitry Andric       PrintOpcodes(Contents->data() + TableEntryOffset, 2 + 4 * AdditionalWords,
4940b57cec5SDimitry Andric                    2);
4950b57cec5SDimitry Andric       break;
4960b57cec5SDimitry Andric     }
4970b57cec5SDimitry Andric   } else {
4980b57cec5SDimitry Andric     SW.printString("Model", StringRef("Generic"));
499e8d8bef9SDimitry Andric     const bool IsRelocatable = ELF.getHeader().e_type == ELF::ET_REL;
500e8d8bef9SDimitry Andric     uint64_t Address = IsRelocatable
501e8d8bef9SDimitry Andric                            ? PREL31(Word, EHT.sh_addr)
502e8d8bef9SDimitry Andric                            : PREL31(Word, EHT.sh_addr + TableEntryOffset);
5030b57cec5SDimitry Andric     SW.printHex("PersonalityRoutineAddress", Address);
504*bdd1243dSDimitry Andric     std::optional<unsigned> SecIndex =
505*bdd1243dSDimitry Andric         IsRelocatable ? std::optional<unsigned>(EHT.sh_link) : std::nullopt;
506e8d8bef9SDimitry Andric     if (ErrorOr<StringRef> Name = FunctionAtAddress(Address, SecIndex))
5070b57cec5SDimitry Andric       SW.printString("PersonalityRoutineName", *Name);
5080b57cec5SDimitry Andric   }
5090b57cec5SDimitry Andric }
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric template <typename ET>
PrintOpcodes(const uint8_t * Entry,size_t Length,off_t Offset)5120b57cec5SDimitry Andric void PrinterContext<ET>::PrintOpcodes(const uint8_t *Entry,
5130b57cec5SDimitry Andric                                       size_t Length, off_t Offset) const {
5140b57cec5SDimitry Andric   ListScope OCC(SW, "Opcodes");
5150eae32dcSDimitry Andric   OpcodeDecoder(SW).Decode(Entry, Offset, Length);
5160b57cec5SDimitry Andric }
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric template <typename ET>
PrintIndexTable(unsigned SectionIndex,const Elf_Shdr * IT)5190b57cec5SDimitry Andric void PrinterContext<ET>::PrintIndexTable(unsigned SectionIndex,
5200b57cec5SDimitry Andric                                          const Elf_Shdr *IT) const {
521e8d8bef9SDimitry Andric   // TODO: handle failure.
522e8d8bef9SDimitry Andric   Expected<ArrayRef<uint8_t>> Contents = ELF.getSectionContents(*IT);
5230b57cec5SDimitry Andric   if (!Contents)
5240b57cec5SDimitry Andric     return;
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric   /// ARM EHABI Section 5 - Index Table Entries
5270b57cec5SDimitry Andric   /// * The first word contains a PREL31 offset to the start of a function with
5280b57cec5SDimitry Andric   ///   bit 31 clear
5290b57cec5SDimitry Andric   /// * The second word contains one of:
5300b57cec5SDimitry Andric   ///   - The PREL31 offset of the start of the table entry for the function,
5310b57cec5SDimitry Andric   ///     with bit 31 clear
5320b57cec5SDimitry Andric   ///   - The exception-handling table entry itself with bit 31 set
5330b57cec5SDimitry Andric   ///   - The special bit pattern EXIDX_CANTUNWIND, indicating that associated
5340b57cec5SDimitry Andric   ///     frames cannot be unwound
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   const support::ulittle32_t *Data =
5370b57cec5SDimitry Andric     reinterpret_cast<const support::ulittle32_t *>(Contents->data());
5380b57cec5SDimitry Andric   const unsigned Entries = IT->sh_size / IndexTableEntrySize;
539e8d8bef9SDimitry Andric   const bool IsRelocatable = ELF.getHeader().e_type == ELF::ET_REL;
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric   ListScope E(SW, "Entries");
5420b57cec5SDimitry Andric   for (unsigned Entry = 0; Entry < Entries; ++Entry) {
5430b57cec5SDimitry Andric     DictScope E(SW, "Entry");
5440b57cec5SDimitry Andric 
5450b57cec5SDimitry Andric     const support::ulittle32_t Word0 =
5460b57cec5SDimitry Andric       Data[Entry * (IndexTableEntrySize / sizeof(*Data)) + 0];
5470b57cec5SDimitry Andric     const support::ulittle32_t Word1 =
5480b57cec5SDimitry Andric       Data[Entry * (IndexTableEntrySize / sizeof(*Data)) + 1];
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric     if (Word0 & 0x80000000) {
5510b57cec5SDimitry Andric       errs() << "corrupt unwind data in section " << SectionIndex << "\n";
5520b57cec5SDimitry Andric       continue;
5530b57cec5SDimitry Andric     }
5540b57cec5SDimitry Andric 
555e8d8bef9SDimitry Andric     // FIXME: For a relocatable object ideally we might want to:
556e8d8bef9SDimitry Andric     // 1) Find a relocation for the offset of Word0.
557e8d8bef9SDimitry Andric     // 2) Verify this relocation is of an expected type (R_ARM_PREL31) and
558e8d8bef9SDimitry Andric     //    verify the symbol index.
559e8d8bef9SDimitry Andric     // 3) Resolve the relocation using it's symbol value, addend etc.
560e8d8bef9SDimitry Andric     // Currently the code assumes that Word0 contains an addend of a
561e8d8bef9SDimitry Andric     // R_ARM_PREL31 REL relocation that references a section symbol. RELA
562e8d8bef9SDimitry Andric     // relocations are not supported and it works because addresses of sections
563e8d8bef9SDimitry Andric     // are nulls in relocatable objects.
564e8d8bef9SDimitry Andric     //
565e8d8bef9SDimitry Andric     // For a non-relocatable object, Word0 contains a place-relative signed
566e8d8bef9SDimitry Andric     // offset to the referenced entity.
567e8d8bef9SDimitry Andric     const uint64_t Address =
568e8d8bef9SDimitry Andric         IsRelocatable
569e8d8bef9SDimitry Andric             ? PREL31(Word0, IT->sh_addr)
570e8d8bef9SDimitry Andric             : PREL31(Word0, IT->sh_addr + Entry * IndexTableEntrySize);
571e8d8bef9SDimitry Andric     SW.printHex("FunctionAddress", Address);
572e8d8bef9SDimitry Andric 
573e8d8bef9SDimitry Andric     // In a relocatable output we might have many .ARM.exidx sections linked to
574e8d8bef9SDimitry Andric     // their code sections via the sh_link field. For a non-relocatable ELF file
575e8d8bef9SDimitry Andric     // the sh_link field is not reliable, because we have one .ARM.exidx section
576e8d8bef9SDimitry Andric     // normally, but might have many code sections.
577*bdd1243dSDimitry Andric     std::optional<unsigned> SecIndex =
578*bdd1243dSDimitry Andric         IsRelocatable ? std::optional<unsigned>(IT->sh_link) : std::nullopt;
579e8d8bef9SDimitry Andric     if (ErrorOr<StringRef> Name = FunctionAtAddress(Address, SecIndex))
5800b57cec5SDimitry Andric       SW.printString("FunctionName", *Name);
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric     if (Word1 == EXIDX_CANTUNWIND) {
5830b57cec5SDimitry Andric       SW.printString("Model", StringRef("CantUnwind"));
5840b57cec5SDimitry Andric       continue;
5850b57cec5SDimitry Andric     }
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric     if (Word1 & 0x80000000) {
5880b57cec5SDimitry Andric       SW.printString("Model", StringRef("Compact (Inline)"));
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric       unsigned PersonalityIndex = (Word1 & 0x0f000000) >> 24;
5910b57cec5SDimitry Andric       SW.printNumber("PersonalityIndex", PersonalityIndex);
5920b57cec5SDimitry Andric 
5930b57cec5SDimitry Andric       PrintOpcodes(Contents->data() + Entry * IndexTableEntrySize + 4, 3, 1);
5940b57cec5SDimitry Andric     } else {
595e8d8bef9SDimitry Andric       const Elf_Shdr *EHT;
596e8d8bef9SDimitry Andric       uint64_t TableEntryAddress;
597e8d8bef9SDimitry Andric       if (IsRelocatable) {
598e8d8bef9SDimitry Andric         TableEntryAddress = PREL31(Word1, IT->sh_addr);
599e8d8bef9SDimitry Andric         EHT = FindExceptionTable(SectionIndex, Entry * IndexTableEntrySize + 4);
600e8d8bef9SDimitry Andric       } else {
601e8d8bef9SDimitry Andric         TableEntryAddress =
602e8d8bef9SDimitry Andric             PREL31(Word1, IT->sh_addr + Entry * IndexTableEntrySize + 4);
603e8d8bef9SDimitry Andric         EHT = findSectionContainingAddress(ELF, FileName, TableEntryAddress);
604e8d8bef9SDimitry Andric       }
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric       if (EHT)
607e8d8bef9SDimitry Andric         // TODO: handle failure.
608e8d8bef9SDimitry Andric         if (Expected<StringRef> Name = ELF.getSectionName(*EHT))
6090b57cec5SDimitry Andric           SW.printString("ExceptionHandlingTable", *Name);
6100b57cec5SDimitry Andric 
611e8d8bef9SDimitry Andric       SW.printHex(IsRelocatable ? "TableEntryOffset" : "TableEntryAddress",
612e8d8bef9SDimitry Andric                   TableEntryAddress);
613e8d8bef9SDimitry Andric       if (EHT) {
614e8d8bef9SDimitry Andric         if (IsRelocatable)
615e8d8bef9SDimitry Andric           PrintExceptionTable(*EHT, TableEntryAddress);
616e8d8bef9SDimitry Andric         else
617e8d8bef9SDimitry Andric           PrintExceptionTable(*EHT, TableEntryAddress - EHT->sh_addr);
618e8d8bef9SDimitry Andric       }
6190b57cec5SDimitry Andric     }
6200b57cec5SDimitry Andric   }
6210b57cec5SDimitry Andric }
6220b57cec5SDimitry Andric 
6230b57cec5SDimitry Andric template <typename ET>
PrintUnwindInformation()6240b57cec5SDimitry Andric void PrinterContext<ET>::PrintUnwindInformation() const {
6250b57cec5SDimitry Andric   DictScope UI(SW, "UnwindInformation");
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric   int SectionIndex = 0;
628e8d8bef9SDimitry Andric   for (const Elf_Shdr &Sec : unwrapOrError(FileName, ELF.sections())) {
6290b57cec5SDimitry Andric     if (Sec.sh_type == ELF::SHT_ARM_EXIDX) {
6300b57cec5SDimitry Andric       DictScope UIT(SW, "UnwindIndexTable");
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric       SW.printNumber("SectionIndex", SectionIndex);
633e8d8bef9SDimitry Andric       // TODO: handle failure.
634e8d8bef9SDimitry Andric       if (Expected<StringRef> SectionName = ELF.getSectionName(Sec))
6350b57cec5SDimitry Andric         SW.printString("SectionName", *SectionName);
6360b57cec5SDimitry Andric       SW.printHex("SectionOffset", Sec.sh_offset);
6370b57cec5SDimitry Andric 
6380b57cec5SDimitry Andric       PrintIndexTable(SectionIndex, &Sec);
6390b57cec5SDimitry Andric     }
6400b57cec5SDimitry Andric     ++SectionIndex;
6410b57cec5SDimitry Andric   }
6420b57cec5SDimitry Andric }
6430b57cec5SDimitry Andric }
6440b57cec5SDimitry Andric }
6450b57cec5SDimitry Andric }
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric #endif
648