LCOV - code coverage report
Current view: top level - lib/DebugInfo/DWARF - DWARFDebugRnglists.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 189 196 96.4 %
Date: 2018-06-17 00:07:59 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          88 : static Error createError(char const *Fmt, const Ts &... Vals) {
      27             :   std::string Buffer;
      28          88 :   raw_string_ostream Stream(Buffer);
      29         176 :   Stream << format(Fmt, Vals...);
      30         264 :   return make_error<StringError>(Stream.str(), inconvertibleErrorCode());
      31             : }
      32             : 
      33         103 : Error DWARFDebugRnglistTable::extractHeaderAndOffsets(DWARFDataExtractor Data,
      34             :                                                       uint32_t *OffsetPtr) {
      35         103 :   HeaderOffset = *OffsetPtr;
      36             :   // Read and verify the length field.
      37         103 :   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          32 :   HeaderData.Length = Data.getU32(OffsetPtr);
      43          32 :   if (HeaderData.Length == 0xffffffffu)
      44             :     return createError(
      45             :         "DWARF64 is not supported in .debug_rnglists at offset 0x%" PRIx32,
      46           1 :         HeaderOffset);
      47          31 :   Format = dwarf::DwarfFormat::DWARF32;
      48          31 :   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          30 :   uint32_t End = HeaderOffset + length();
      54          30 :   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          29 :   HeaderData.Version = Data.getU16(OffsetPtr);
      61          29 :   HeaderData.AddrSize = Data.getU8(OffsetPtr);
      62          29 :   HeaderData.SegSize = Data.getU8(OffsetPtr);
      63          29 :   HeaderData.OffsetEntryCount = Data.getU32(OffsetPtr);
      64             : 
      65             :   // Perform basic validation of the remaining header fields.
      66          29 :   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          28 :   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          27 :   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          52 :   if (End < HeaderOffset + sizeof(HeaderData) +
      79          26 :                 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          47 :   for (uint32_t I = 0; I < HeaderData.OffsetEntryCount; ++I)
      86          22 :     Offsets.push_back(Data.getU32(OffsetPtr));
      87             :   return Error::success();
      88             : }
      89             : 
      90          53 : Error DWARFDebugRnglist::RangeListEntry::extract(DWARFDataExtractor Data,
      91             :                                                  uint32_t End,
      92             :                                                  uint32_t *OffsetPtr) {
      93          53 :   Offset = *OffsetPtr;
      94          53 :   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          53 :   uint8_t Encoding = Data.getU8(OffsetPtr);
     100             : 
     101          53 :   switch (Encoding) {
     102          17 :   case dwarf::DW_RLE_end_of_list:
     103          17 :     Value0 = Value1 = 0;
     104          17 :     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           6 :   case dwarf::DW_RLE_offset_pair: {
     119           6 :     uint32_t PreviousOffset = *OffsetPtr - 1;
     120           6 :     Value0 = Data.getULEB128(OffsetPtr);
     121           6 :     Value1 = Data.getULEB128(OffsetPtr);
     122           6 :     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           4 :   case dwarf::DW_RLE_base_address: {
     129           4 :     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           8 :     Value0 = Data.getRelocatedAddress(OffsetPtr, &SectionIndex);
     134           4 :     break;
     135             :   }
     136          13 :   case dwarf::DW_RLE_start_end: {
     137          13 :     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          24 :     Value0 = Data.getRelocatedAddress(OffsetPtr, &SectionIndex);
     143          12 :     Value1 = Data.getRelocatedAddress(OffsetPtr);
     144          12 :     break;
     145             :   }
     146           6 :   case dwarf::DW_RLE_start_length: {
     147           6 :     uint32_t PreviousOffset = *OffsetPtr - 1;
     148          12 :     Value0 = Data.getRelocatedAddress(OffsetPtr, &SectionIndex);
     149           6 :     Value1 = Data.getULEB128(OffsetPtr);
     150           6 :     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          44 :   EntryKind = Encoding;
     163             :   return Error::success();
     164             : }
     165             : 
     166           3 : DWARFAddressRangesVector DWARFDebugRnglist::getAbsoluteRanges(
     167             :     llvm::Optional<BaseAddress> BaseAddr) const {
     168             :   DWARFAddressRangesVector Res;
     169           3 :   for (const RangeListEntry &RLE : Entries) {
     170           9 :     if (RLE.EntryKind == dwarf::DW_RLE_end_of_list)
     171             :       break;
     172           8 :     if (RLE.EntryKind == dwarf::DW_RLE_base_address) {
     173           2 :       BaseAddr = {RLE.Value0, RLE.SectionIndex};
     174           2 :       continue;
     175             :     }
     176             : 
     177             :     DWARFAddressRange E;
     178           4 :     E.SectionIndex = RLE.SectionIndex;
     179           4 :     if (BaseAddr && E.SectionIndex == -1ULL)
     180           2 :       E.SectionIndex = BaseAddr->SectionIndex;
     181             : 
     182           4 :     switch (RLE.EntryKind) {
     183           2 :     case dwarf::DW_RLE_offset_pair:
     184           2 :       E.LowPC = RLE.Value0;
     185           2 :       E.HighPC = RLE.Value1;
     186           2 :       if (BaseAddr) {
     187           2 :         E.LowPC += BaseAddr->Address;
     188           2 :         E.HighPC += BaseAddr->Address;
     189             :       }
     190             :       break;
     191           1 :     case dwarf::DW_RLE_start_end:
     192           1 :       E.LowPC = RLE.Value0;
     193           1 :       E.HighPC = RLE.Value1;
     194           1 :       break;
     195           1 :     case dwarf::DW_RLE_start_length:
     196           1 :       E.LowPC = RLE.Value0;
     197           1 :       E.HighPC = E.LowPC + RLE.Value1;
     198           1 :       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           4 :     Res.push_back(E);
     205             :   }
     206           3 :   return Res;
     207             : }
     208             : 
     209          27 : Error DWARFDebugRnglist::extract(DWARFDataExtractor Data, uint32_t HeaderOffset,
     210             :                                  uint32_t End, uint32_t *OffsetPtr) {
     211          27 :   Entries.clear();
     212          81 :   while (*OffsetPtr < End) {
     213          53 :     RangeListEntry Entry{0, 0, 0, 0, 0};
     214         106 :     if (Error E = Entry.extract(Data, End, OffsetPtr))
     215             :       return E;
     216          44 :     Entries.push_back(Entry);
     217          44 :     if (Entry.EntryKind == dwarf::DW_RLE_end_of_list)
     218             :       return Error::success();
     219             :   }
     220             :   return createError(
     221             :       "no end of list marker detected at end of .debug_rnglists table "
     222             :       "starting at offset 0x%" PRIx32,
     223           1 :       HeaderOffset);
     224             : }
     225             : 
     226          31 : Error DWARFDebugRnglistTable::extract(DWARFDataExtractor Data,
     227             :                                       uint32_t *OffsetPtr) {
     228          31 :   clear();
     229          62 :   if (Error E = extractHeaderAndOffsets(Data, OffsetPtr))
     230             :     return E;
     231             : 
     232          22 :   Data.setAddressSize(HeaderData.AddrSize);
     233          22 :   uint32_t End = HeaderOffset + length();
     234          36 :   while (*OffsetPtr < End) {
     235             :     DWARFDebugRnglist CurrentRangeList;
     236          24 :     uint32_t Off = *OffsetPtr;
     237          48 :     if (Error E = CurrentRangeList.extract(Data, HeaderOffset, End, OffsetPtr))
     238             :       return E;
     239          14 :     Ranges[Off] = CurrentRangeList;
     240             :   }
     241             : 
     242             :   assert(*OffsetPtr == End &&
     243             :          "mismatch between expected length of .debug_rnglists table and length "
     244             :          "of extracted data");
     245             :   return Error::success();
     246             : }
     247             : 
     248          34 : static void dumpRangeEntry(raw_ostream &OS,
     249             :                            DWARFDebugRnglist::RangeListEntry Entry,
     250             :                            uint8_t AddrSize, uint8_t MaxEncodingStringLength,
     251             :                            uint64_t &CurrentBase, DIDumpOptions DumpOpts) {
     252             :   auto PrintRawEntry = [](raw_ostream &OS,
     253             :                           DWARFDebugRnglist::RangeListEntry Entry,
     254           8 :                           uint8_t AddrSize, DIDumpOptions DumpOpts) {
     255           8 :     if (DumpOpts.Verbose) {
     256           4 :       DumpOpts.DisplayRawContents = true;
     257           8 :       DWARFAddressRange(Entry.Value0, Entry.Value1)
     258           4 :           .dump(OS, AddrSize, DumpOpts);
     259           4 :       OS << " => ";
     260             :     }
     261           8 :   };
     262             : 
     263          34 :   if (DumpOpts.Verbose) {
     264             :     // Print the section offset in verbose mode.
     265          30 :     OS << format("0x%8.8" PRIx32 ":", Entry.Offset);
     266          15 :     auto EncodingString = dwarf::RangeListEncodingString(Entry.EntryKind);
     267             :     // Unsupported encodings should have been reported during parsing.
     268             :     assert(!EncodingString.empty() && "Unknown range entry encoding");
     269          15 :     OS << format(" [%s%*c", EncodingString.data(),
     270          30 :                  MaxEncodingStringLength - EncodingString.size() + 1, ']');
     271          15 :     if (Entry.EntryKind != dwarf::DW_RLE_end_of_list)
     272           9 :       OS << ": ";
     273             :   }
     274             : 
     275          34 :   switch (Entry.EntryKind) {
     276          14 :   case dwarf::DW_RLE_end_of_list:
     277          14 :     OS << (DumpOpts.Verbose ? "" : "<End of list>");
     278          14 :     break;
     279           2 :   case dwarf::DW_RLE_base_address:
     280             :     // In non-verbose mode we do not print anything for this entry.
     281           2 :     CurrentBase = Entry.Value0;
     282           2 :     if (!DumpOpts.Verbose)
     283           1 :       return;
     284           2 :     OS << format(" 0x%*.*" PRIx64, AddrSize * 2, AddrSize * 2, Entry.Value0);
     285           1 :     break;
     286           4 :   case dwarf::DW_RLE_start_length:
     287           4 :     PrintRawEntry(OS, Entry, AddrSize, DumpOpts);
     288           8 :     DWARFAddressRange(Entry.Value0, Entry.Value0 + Entry.Value1)
     289           4 :         .dump(OS, AddrSize, DumpOpts);
     290           4 :     break;
     291           4 :   case dwarf::DW_RLE_offset_pair:
     292           4 :     PrintRawEntry(OS, Entry, AddrSize, DumpOpts);
     293           8 :     DWARFAddressRange(Entry.Value0 + CurrentBase, Entry.Value1 + CurrentBase)
     294           4 :         .dump(OS, AddrSize, DumpOpts);
     295           4 :     break;
     296          10 :   case dwarf::DW_RLE_start_end:
     297          10 :     DWARFAddressRange(Entry.Value0, Entry.Value1).dump(OS, AddrSize, DumpOpts);
     298          10 :     break;
     299           0 :   default:
     300           0 :     llvm_unreachable("Unsupported range list encoding");
     301             :   }
     302          33 :   OS << "\n";
     303             : }
     304             : 
     305          12 : void DWARFDebugRnglistTable::dump(raw_ostream &OS,
     306             :                                   DIDumpOptions DumpOpts) const {
     307          12 :   if (DumpOpts.Verbose)
     308          10 :     OS << format("0x%8.8" PRIx32 ": ", HeaderOffset);
     309          24 :   OS << format("Range List Header: length = 0x%8.8" PRIx32
     310             :                ", version = 0x%4.4" PRIx16 ", "
     311             :                "addr_size = 0x%2.2" PRIx8 ", seg_size = 0x%2.2" PRIx8
     312             :                ", offset_entry_count = "
     313             :                "0x%8.8" PRIx32 "\n",
     314             :                HeaderData.Length, HeaderData.Version, HeaderData.AddrSize,
     315             :                HeaderData.SegSize, HeaderData.OffsetEntryCount);
     316             : 
     317          12 :   if (HeaderData.OffsetEntryCount > 0) {
     318           3 :     OS << "Offsets: [";
     319           3 :     for (const auto &Off : Offsets) {
     320          10 :       OS << format("\n0x%8.8" PRIx32, Off);
     321           5 :       if (DumpOpts.Verbose)
     322           2 :         OS << format(" => 0x%8.8" PRIx32,
     323           2 :                      Off + HeaderOffset + sizeof(HeaderData));
     324             :     }
     325           3 :     OS << "\n]\n";
     326             :   }
     327          12 :   OS << "Ranges:\n";
     328             : 
     329             :   // Determine the length of the longest encoding string we have in the table,
     330             :   // so we can align the output properly. We only need this in verbose mode.
     331          12 :   size_t MaxEncodingStringLength = 0;
     332          12 :   if (DumpOpts.Verbose) {
     333          11 :     for (const auto &List : Ranges)
     334           6 :       for (const auto &Entry : List.second.getEntries())
     335          15 :         MaxEncodingStringLength =
     336          15 :             std::max(MaxEncodingStringLength,
     337          45 :                      dwarf::RangeListEncodingString(Entry.EntryKind).size());
     338             :   }
     339             : 
     340          12 :   uint64_t CurrentBase = 0;
     341          26 :   for (const auto &List : Ranges)
     342          14 :     for (const auto &Entry : List.second.getEntries())
     343          34 :       dumpRangeEntry(OS, Entry, HeaderData.AddrSize, MaxEncodingStringLength,
     344             :                      CurrentBase, DumpOpts);
     345          12 : }
     346             : 
     347          76 : uint32_t DWARFDebugRnglistTable::length() const {
     348          76 :   if (HeaderData.Length == 0)
     349             :     return 0;
     350             :   // TODO: DWARF64 support.
     351          74 :   return HeaderData.Length + sizeof(uint32_t);
     352             : }
     353             : 
     354             : Optional<DWARFDebugRnglist>
     355           3 : DWARFDebugRnglistTable::findRangeList(DWARFDataExtractor Data,
     356             :                                       uint32_t Offset) {
     357             :   auto Entry = Ranges.find(Offset);
     358           3 :   if (Entry != Ranges.end())
     359             :     return Entry->second;
     360             : 
     361             :   // Extract the rangelist from the section and enter it into the ranges map.
     362             :   DWARFDebugRnglist RngList;
     363           3 :   uint32_t End = HeaderOffset + length();
     364           3 :   uint32_t StartingOffset = Offset;
     365           6 :   if (Error E = RngList.extract(Data, HeaderOffset, End, &Offset)) {
     366           0 :     llvm::consumeError(std::move(E));
     367             :     return None;
     368             :   }
     369           3 :   Ranges[StartingOffset] = RngList;
     370             :   return RngList;
     371             : }

Generated by: LCOV version 1.13