LCOV - code coverage report
Current view: top level - lib/DebugInfo/DWARF - DWARFDebugRnglists.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 191 197 97.0 %
Date: 2018-07-13 00:08:38 Functions: 15 15 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DWARFDebugRnglists.cpp ---------------------------------------------===//
       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             : #include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
      11             : #include "llvm/BinaryFormat/Dwarf.h"
      12             : #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
      13             : #include "llvm/Support/Error.h"
      14             : #include "llvm/Support/Format.h"
      15             : #include "llvm/Support/raw_ostream.h"
      16             : 
      17             : using namespace llvm;
      18             : 
      19          31 : void DWARFDebugRnglistTable::clear() {
      20          31 :   HeaderData = {};
      21             :   Offsets.clear();
      22             :   Ranges.clear();
      23          31 : }
      24             : 
      25             : template <typename... Ts>
      26          90 : static Error createError(char const *Fmt, const Ts &... Vals) {
      27             :   std::string Buffer;
      28          90 :   raw_string_ostream Stream(Buffer);
      29         180 :   Stream << format(Fmt, Vals...);
      30         270 :   return make_error<StringError>(Stream.str(), inconvertibleErrorCode());
      31             : }
      32             : 
      33         107 : Error DWARFDebugRnglistTable::extractHeaderAndOffsets(DWARFDataExtractor Data,
      34             :                                                       uint32_t *OffsetPtr) {
      35         107 :   HeaderOffset = *OffsetPtr;
      36             :   // Read and verify the length field.
      37         107 :   if (!Data.isValidOffsetForDataOfSize(*OffsetPtr, sizeof(uint32_t)))
      38             :     return createError("section is not large enough to contain a "
      39             :                        ".debug_rnglists table length at offset 0x%" PRIx32,
      40          71 :                        *OffsetPtr);
      41             :   // TODO: Add support for DWARF64.
      42          36 :   HeaderData.Length = Data.getU32(OffsetPtr);
      43          36 :   if (HeaderData.Length == 0xffffffffu)
      44             :     return createError(
      45             :         "DWARF64 is not supported in .debug_rnglists at offset 0x%" PRIx32,
      46           1 :         HeaderOffset);
      47          35 :   Format = dwarf::DwarfFormat::DWARF32;
      48          35 :   if (HeaderData.Length + sizeof(uint32_t) < sizeof(Header))
      49             :     return createError(".debug_rnglists table at offset 0x%" PRIx32
      50             :                        " has too small length (0x%" PRIx32
      51             :                        ") to contain a complete header",
      52           1 :                        HeaderOffset, length());
      53          34 :   uint32_t End = HeaderOffset + length();
      54          34 :   if (!Data.isValidOffsetForDataOfSize(HeaderOffset, End - HeaderOffset))
      55             :     return createError(
      56             :         "section is not large enough to contain a .debug_rnglists table "
      57             :         "of length 0x%" PRIx32 " at offset 0x%" PRIx32,
      58           1 :         length(), HeaderOffset);
      59             : 
      60          33 :   HeaderData.Version = Data.getU16(OffsetPtr);
      61          33 :   HeaderData.AddrSize = Data.getU8(OffsetPtr);
      62          33 :   HeaderData.SegSize = Data.getU8(OffsetPtr);
      63          33 :   HeaderData.OffsetEntryCount = Data.getU32(OffsetPtr);
      64             : 
      65             :   // Perform basic validation of the remaining header fields.
      66          33 :   if (HeaderData.Version != 5)
      67             :     return createError("unrecognised .debug_rnglists table version %" PRIu16
      68             :                        " in table at offset 0x%" PRIx32,
      69           1 :                        HeaderData.Version, HeaderOffset);
      70          32 :   if (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)
      71             :     return createError(".debug_rnglists table at offset 0x%" PRIx32
      72             :                        " has unsupported address size %hhu",
      73           1 :                        HeaderOffset, HeaderData.AddrSize);
      74          31 :   if (HeaderData.SegSize != 0)
      75             :     return createError(".debug_rnglists table at offset 0x%" PRIx32
      76             :                        " has unsupported segment selector size %" PRIu8,
      77           1 :                        HeaderOffset, HeaderData.SegSize);
      78          60 :   if (End < HeaderOffset + sizeof(HeaderData) +
      79          30 :                 HeaderData.OffsetEntryCount * sizeof(uint32_t))
      80             :     return createError(".debug_rnglists table at offset 0x%" PRIx32
      81             :                        " has more offset entries (%" PRIu32
      82             :                        ") than there is space for",
      83           1 :                        HeaderOffset, HeaderData.OffsetEntryCount);
      84             :   Data.setAddressSize(HeaderData.AddrSize);
      85          67 :   for (uint32_t I = 0; I < HeaderData.OffsetEntryCount; ++I)
      86          38 :     Offsets.push_back(Data.getU32(OffsetPtr));
      87             :   return Error::success();
      88             : }
      89             : 
      90          59 : Error DWARFDebugRnglist::RangeListEntry::extract(DWARFDataExtractor Data,
      91             :                                                  uint32_t End,
      92             :                                                  uint32_t *OffsetPtr) {
      93          59 :   Offset = *OffsetPtr;
      94          59 :   SectionIndex = -1ULL;
      95             :   // The caller should guarantee that we have at least 1 byte available, so
      96             :   // we just assert instead of revalidate.
      97             :   assert(*OffsetPtr < End &&
      98             :          "not enough space to extract a rangelist encoding");
      99          59 :   uint8_t Encoding = Data.getU8(OffsetPtr);
     100             : 
     101          59 :   switch (Encoding) {
     102          19 :   case dwarf::DW_RLE_end_of_list:
     103          19 :     Value0 = Value1 = 0;
     104          19 :     break;
     105             :   // TODO: Support other encodings.
     106           2 :   case dwarf::DW_RLE_base_addressx:
     107             :     return createError("unsupported rnglists encoding DW_RLE_base_addressx "
     108             :                        "at offset 0x%" PRIx32,
     109           2 :                        *OffsetPtr - 1);
     110           2 :   case dwarf::DW_RLE_startx_endx:
     111             :     return createError("unsupported rnglists encoding DW_RLE_startx_endx at "
     112             :                        "offset 0x%" PRIx32,
     113           2 :                        *OffsetPtr - 1);
     114           2 :   case dwarf::DW_RLE_startx_length:
     115             :     return createError("unsupported rnglists encoding DW_RLE_startx_length "
     116             :                        "at offset 0x%" PRIx32,
     117           2 :                        *OffsetPtr - 1);
     118           7 :   case dwarf::DW_RLE_offset_pair: {
     119           7 :     uint32_t PreviousOffset = *OffsetPtr - 1;
     120           7 :     Value0 = Data.getULEB128(OffsetPtr);
     121           7 :     Value1 = Data.getULEB128(OffsetPtr);
     122           7 :     if (End < *OffsetPtr)
     123             :       return createError("read past end of table when reading "
     124             :                          "DW_RLE_offset_pair encoding at offset 0x%" PRIx32,
     125           0 :                          PreviousOffset);
     126             :     break;
     127             :   }
     128           5 :   case dwarf::DW_RLE_base_address: {
     129           5 :     if ((End - *OffsetPtr) < Data.getAddressSize())
     130             :       return createError("insufficient space remaining in table for "
     131             :                          "DW_RLE_base_address encoding at offset 0x%" PRIx32,
     132           0 :                          *OffsetPtr - 1);
     133          10 :     Value0 = Data.getRelocatedAddress(OffsetPtr, &SectionIndex);
     134           5 :     break;
     135             :   }
     136          14 :   case dwarf::DW_RLE_start_end: {
     137          14 :     if ((End - *OffsetPtr) < unsigned(Data.getAddressSize() * 2))
     138             :       return createError("insufficient space remaining in table for "
     139             :                          "DW_RLE_start_end encoding "
     140             :                          "at offset 0x%" PRIx32,
     141           1 :                          *OffsetPtr - 1);
     142          26 :     Value0 = Data.getRelocatedAddress(OffsetPtr, &SectionIndex);
     143          13 :     Value1 = Data.getRelocatedAddress(OffsetPtr);
     144          13 :     break;
     145             :   }
     146           7 :   case dwarf::DW_RLE_start_length: {
     147           7 :     uint32_t PreviousOffset = *OffsetPtr - 1;
     148          14 :     Value0 = Data.getRelocatedAddress(OffsetPtr, &SectionIndex);
     149           7 :     Value1 = Data.getULEB128(OffsetPtr);
     150           7 :     if (End < *OffsetPtr)
     151             :       return createError("read past end of table when reading "
     152             :                          "DW_RLE_start_length encoding at offset 0x%" PRIx32,
     153           1 :                          PreviousOffset);
     154             :     break;
     155             :   }
     156           1 :   default:
     157             :     return createError("unknown rnglists encoding 0x%" PRIx32
     158             :                        " at offset 0x%" PRIx32,
     159           1 :                        uint32_t(Encoding), *OffsetPtr - 1);
     160             :   }
     161             : 
     162          50 :   EntryKind = Encoding;
     163             :   return Error::success();
     164             : }
     165             : 
     166           5 : DWARFAddressRangesVector DWARFDebugRnglist::getAbsoluteRanges(
     167             :     llvm::Optional<BaseAddress> BaseAddr) const {
     168             :   DWARFAddressRangesVector Res;
     169           5 :   for (const RangeListEntry &RLE : Entries) {
     170          15 :     if (RLE.EntryKind == dwarf::DW_RLE_end_of_list)
     171             :       break;
     172          13 :     if (RLE.EntryKind == dwarf::DW_RLE_base_address) {
     173           3 :       BaseAddr = {RLE.Value0, RLE.SectionIndex};
     174           3 :       continue;
     175             :     }
     176             : 
     177             :     DWARFAddressRange E;
     178           7 :     E.SectionIndex = RLE.SectionIndex;
     179           7 :     if (BaseAddr && E.SectionIndex == -1ULL)
     180           3 :       E.SectionIndex = BaseAddr->SectionIndex;
     181             : 
     182           7 :     switch (RLE.EntryKind) {
     183           3 :     case dwarf::DW_RLE_offset_pair:
     184           3 :       E.LowPC = RLE.Value0;
     185           3 :       E.HighPC = RLE.Value1;
     186           3 :       if (BaseAddr) {
     187           3 :         E.LowPC += BaseAddr->Address;
     188           3 :         E.HighPC += BaseAddr->Address;
     189             :       }
     190             :       break;
     191           2 :     case dwarf::DW_RLE_start_end:
     192           2 :       E.LowPC = RLE.Value0;
     193           2 :       E.HighPC = RLE.Value1;
     194           2 :       break;
     195           2 :     case dwarf::DW_RLE_start_length:
     196           2 :       E.LowPC = RLE.Value0;
     197           2 :       E.HighPC = E.LowPC + RLE.Value1;
     198           2 :       break;
     199           0 :     default:
     200             :       // Unsupported encodings should have been reported during extraction,
     201             :       // so we should not run into any here.
     202           0 :       llvm_unreachable("Unsupported range list encoding");
     203             :     }
     204           7 :     Res.push_back(E);
     205             :   }
     206           5 :   return Res;
     207             : }
     208             : 
     209          31 : Error DWARFDebugRnglist::extract(DWARFDataExtractor Data, uint32_t HeaderOffset,
     210             :                                  uint32_t End, uint32_t *OffsetPtr) {
     211          31 :   if (*OffsetPtr < HeaderOffset || *OffsetPtr >= End)
     212           2 :     return createError("invalid range list offset 0x%" PRIx32, *OffsetPtr);
     213          29 :   Entries.clear();
     214          91 :   while (*OffsetPtr < End) {
     215          59 :     RangeListEntry Entry{0, 0, 0, 0, 0};
     216         118 :     if (Error E = Entry.extract(Data, End, OffsetPtr))
     217             :       return E;
     218          50 :     Entries.push_back(Entry);
     219          50 :     if (Entry.EntryKind == dwarf::DW_RLE_end_of_list)
     220             :       return Error::success();
     221             :   }
     222             :   return createError(
     223             :       "no end of list marker detected at end of .debug_rnglists table "
     224             :       "starting at offset 0x%" PRIx32,
     225           1 :       HeaderOffset);
     226             : }
     227             : 
     228          31 : Error DWARFDebugRnglistTable::extract(DWARFDataExtractor Data,
     229             :                                       uint32_t *OffsetPtr) {
     230          31 :   clear();
     231          62 :   if (Error E = extractHeaderAndOffsets(Data, OffsetPtr))
     232             :     return E;
     233             : 
     234          22 :   Data.setAddressSize(HeaderData.AddrSize);
     235          22 :   uint32_t End = HeaderOffset + length();
     236          36 :   while (*OffsetPtr < End) {
     237             :     DWARFDebugRnglist CurrentRangeList;
     238          24 :     uint32_t Off = *OffsetPtr;
     239          48 :     if (Error E = CurrentRangeList.extract(Data, HeaderOffset, End, OffsetPtr))
     240             :       return E;
     241          14 :     Ranges[Off] = CurrentRangeList;
     242             :   }
     243             : 
     244             :   assert(*OffsetPtr == End &&
     245             :          "mismatch between expected length of .debug_rnglists table and length "
     246             :          "of extracted data");
     247             :   return Error::success();
     248             : }
     249             : 
     250          34 : static void dumpRangeEntry(raw_ostream &OS,
     251             :                            DWARFDebugRnglist::RangeListEntry Entry,
     252             :                            uint8_t AddrSize, uint8_t MaxEncodingStringLength,
     253             :                            uint64_t &CurrentBase, DIDumpOptions DumpOpts) {
     254             :   auto PrintRawEntry = [](raw_ostream &OS,
     255             :                           DWARFDebugRnglist::RangeListEntry Entry,
     256           8 :                           uint8_t AddrSize, DIDumpOptions DumpOpts) {
     257           8 :     if (DumpOpts.Verbose) {
     258           4 :       DumpOpts.DisplayRawContents = true;
     259           8 :       DWARFAddressRange(Entry.Value0, Entry.Value1)
     260           4 :           .dump(OS, AddrSize, DumpOpts);
     261           4 :       OS << " => ";
     262             :     }
     263           8 :   };
     264             : 
     265          34 :   if (DumpOpts.Verbose) {
     266             :     // Print the section offset in verbose mode.
     267          30 :     OS << format("0x%8.8" PRIx32 ":", Entry.Offset);
     268          15 :     auto EncodingString = dwarf::RangeListEncodingString(Entry.EntryKind);
     269             :     // Unsupported encodings should have been reported during parsing.
     270             :     assert(!EncodingString.empty() && "Unknown range entry encoding");
     271          15 :     OS << format(" [%s%*c", EncodingString.data(),
     272          30 :                  MaxEncodingStringLength - EncodingString.size() + 1, ']');
     273          15 :     if (Entry.EntryKind != dwarf::DW_RLE_end_of_list)
     274           9 :       OS << ": ";
     275             :   }
     276             : 
     277          34 :   switch (Entry.EntryKind) {
     278          14 :   case dwarf::DW_RLE_end_of_list:
     279          14 :     OS << (DumpOpts.Verbose ? "" : "<End of list>");
     280          14 :     break;
     281           2 :   case dwarf::DW_RLE_base_address:
     282             :     // In non-verbose mode we do not print anything for this entry.
     283           2 :     CurrentBase = Entry.Value0;
     284           2 :     if (!DumpOpts.Verbose)
     285           1 :       return;
     286           2 :     OS << format(" 0x%*.*" PRIx64, AddrSize * 2, AddrSize * 2, Entry.Value0);
     287           1 :     break;
     288           4 :   case dwarf::DW_RLE_start_length:
     289           4 :     PrintRawEntry(OS, Entry, AddrSize, DumpOpts);
     290           8 :     DWARFAddressRange(Entry.Value0, Entry.Value0 + Entry.Value1)
     291           4 :         .dump(OS, AddrSize, DumpOpts);
     292           4 :     break;
     293           4 :   case dwarf::DW_RLE_offset_pair:
     294           4 :     PrintRawEntry(OS, Entry, AddrSize, DumpOpts);
     295           8 :     DWARFAddressRange(Entry.Value0 + CurrentBase, Entry.Value1 + CurrentBase)
     296           4 :         .dump(OS, AddrSize, DumpOpts);
     297           4 :     break;
     298          10 :   case dwarf::DW_RLE_start_end:
     299          10 :     DWARFAddressRange(Entry.Value0, Entry.Value1).dump(OS, AddrSize, DumpOpts);
     300          10 :     break;
     301           0 :   default:
     302           0 :     llvm_unreachable("Unsupported range list encoding");
     303             :   }
     304          33 :   OS << "\n";
     305             : }
     306             : 
     307          12 : void DWARFDebugRnglistTable::dump(raw_ostream &OS,
     308             :                                   DIDumpOptions DumpOpts) const {
     309          12 :   if (DumpOpts.Verbose)
     310          10 :     OS << format("0x%8.8" PRIx32 ": ", HeaderOffset);
     311          24 :   OS << format("Range List Header: length = 0x%8.8" PRIx32
     312             :                ", version = 0x%4.4" PRIx16 ", "
     313             :                "addr_size = 0x%2.2" PRIx8 ", seg_size = 0x%2.2" PRIx8
     314             :                ", offset_entry_count = "
     315             :                "0x%8.8" PRIx32 "\n",
     316             :                HeaderData.Length, HeaderData.Version, HeaderData.AddrSize,
     317             :                HeaderData.SegSize, HeaderData.OffsetEntryCount);
     318             : 
     319          12 :   if (HeaderData.OffsetEntryCount > 0) {
     320           3 :     OS << "Offsets: [";
     321           3 :     for (const auto &Off : Offsets) {
     322          10 :       OS << format("\n0x%8.8" PRIx32, Off);
     323           5 :       if (DumpOpts.Verbose)
     324           2 :         OS << format(" => 0x%8.8" PRIx32,
     325           2 :                      Off + HeaderOffset + sizeof(HeaderData));
     326             :     }
     327           3 :     OS << "\n]\n";
     328             :   }
     329          12 :   OS << "Ranges:\n";
     330             : 
     331             :   // Determine the length of the longest encoding string we have in the table,
     332             :   // so we can align the output properly. We only need this in verbose mode.
     333          12 :   size_t MaxEncodingStringLength = 0;
     334          12 :   if (DumpOpts.Verbose) {
     335          11 :     for (const auto &List : Ranges)
     336           6 :       for (const auto &Entry : List.second.getEntries())
     337          15 :         MaxEncodingStringLength =
     338          15 :             std::max(MaxEncodingStringLength,
     339          45 :                      dwarf::RangeListEncodingString(Entry.EntryKind).size());
     340             :   }
     341             : 
     342          12 :   uint64_t CurrentBase = 0;
     343          26 :   for (const auto &List : Ranges)
     344          14 :     for (const auto &Entry : List.second.getEntries())
     345          34 :       dumpRangeEntry(OS, Entry, HeaderData.AddrSize, MaxEncodingStringLength,
     346             :                      CurrentBase, DumpOpts);
     347          12 : }
     348             : 
     349          84 : uint32_t DWARFDebugRnglistTable::length() const {
     350          84 :   if (HeaderData.Length == 0)
     351             :     return 0;
     352             :   // TODO: DWARF64 support.
     353          82 :   return HeaderData.Length + sizeof(uint32_t);
     354             : }
     355             : 
     356             : Expected<DWARFDebugRnglist>
     357           7 : DWARFDebugRnglistTable::findRangeList(DWARFDataExtractor Data,
     358             :                                       uint32_t Offset) {
     359             :   auto Entry = Ranges.find(Offset);
     360           7 :   if (Entry != Ranges.end())
     361             :     return Entry->second;
     362             : 
     363             :   // Extract the rangelist from the section and enter it into the ranges map.
     364             :   DWARFDebugRnglist RngList;
     365           7 :   uint32_t End = HeaderOffset + length();
     366           7 :   uint32_t StartingOffset = Offset;
     367          14 :   if (Error E = RngList.extract(Data, HeaderOffset, End, &Offset))
     368             :     return std::move(E);
     369           5 :   Ranges[StartingOffset] = RngList;
     370             :   return RngList;
     371             : }

Generated by: LCOV version 1.13