LLVM API Documentation

DWARFDebugArangeSet.cpp
Go to the documentation of this file.
00001 //===-- DWARFDebugArangeSet.cpp -------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "DWARFDebugArangeSet.h"
00011 #include "llvm/Support/Format.h"
00012 #include "llvm/Support/raw_ostream.h"
00013 #include <algorithm>
00014 #include <cassert>
00015 using namespace llvm;
00016 
00017 void DWARFDebugArangeSet::clear() {
00018   Offset = -1U;
00019   std::memset(&HeaderData, 0, sizeof(Header));
00020   ArangeDescriptors.clear();
00021 }
00022 
00023 bool
00024 DWARFDebugArangeSet::extract(DataExtractor data, uint32_t *offset_ptr) {
00025   if (data.isValidOffset(*offset_ptr)) {
00026     ArangeDescriptors.clear();
00027     Offset = *offset_ptr;
00028 
00029     // 7.20 Address Range Table
00030     //
00031     // Each set of entries in the table of address ranges contained in
00032     // the .debug_aranges section begins with a header consisting of: a
00033     // 4-byte length containing the length of the set of entries for this
00034     // compilation unit, not including the length field itself; a 2-byte
00035     // version identifier containing the value 2 for DWARF Version 2; a
00036     // 4-byte offset into the.debug_infosection; a 1-byte unsigned integer
00037     // containing the size in bytes of an address (or the offset portion of
00038     // an address for segmented addressing) on the target system; and a
00039     // 1-byte unsigned integer containing the size in bytes of a segment
00040     // descriptor on the target system. This header is followed by a series
00041     // of tuples. Each tuple consists of an address and a length, each in
00042     // the size appropriate for an address on the target architecture.
00043     HeaderData.Length = data.getU32(offset_ptr);
00044     HeaderData.Version = data.getU16(offset_ptr);
00045     HeaderData.CuOffset = data.getU32(offset_ptr);
00046     HeaderData.AddrSize = data.getU8(offset_ptr);
00047     HeaderData.SegSize = data.getU8(offset_ptr);
00048 
00049     // Perform basic validation of the header fields.
00050     if (!data.isValidOffsetForDataOfSize(Offset, HeaderData.Length) ||
00051         (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)) {
00052       clear();
00053       return false;
00054     }
00055 
00056     // The first tuple following the header in each set begins at an offset
00057     // that is a multiple of the size of a single tuple (that is, twice the
00058     // size of an address). The header is padded, if necessary, to the
00059     // appropriate boundary.
00060     const uint32_t header_size = *offset_ptr - Offset;
00061     const uint32_t tuple_size = HeaderData.AddrSize * 2;
00062     uint32_t first_tuple_offset = 0;
00063     while (first_tuple_offset < header_size)
00064       first_tuple_offset += tuple_size;
00065 
00066     *offset_ptr = Offset + first_tuple_offset;
00067 
00068     Descriptor arangeDescriptor;
00069 
00070     static_assert(sizeof(arangeDescriptor.Address) ==
00071                       sizeof(arangeDescriptor.Length),
00072                   "Different datatypes for addresses and sizes!");
00073     assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);
00074 
00075     while (data.isValidOffset(*offset_ptr)) {
00076       arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
00077       arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
00078 
00079       // Each set of tuples is terminated by a 0 for the address and 0
00080       // for the length.
00081       if (arangeDescriptor.Address || arangeDescriptor.Length)
00082         ArangeDescriptors.push_back(arangeDescriptor);
00083       else
00084         break; // We are done if we get a zero address and length
00085     }
00086 
00087     return !ArangeDescriptors.empty();
00088   }
00089   return false;
00090 }
00091 
00092 void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
00093   OS << format("Address Range Header: length = 0x%8.8x, version = 0x%4.4x, ",
00094                HeaderData.Length, HeaderData.Version)
00095      << format("cu_offset = 0x%8.8x, addr_size = 0x%2.2x, seg_size = 0x%2.2x\n",
00096                HeaderData.CuOffset, HeaderData.AddrSize, HeaderData.SegSize);
00097 
00098   const uint32_t hex_width = HeaderData.AddrSize * 2;
00099   for (const auto &Desc : ArangeDescriptors) {
00100     OS << format("[0x%*.*" PRIx64 " -", hex_width, hex_width, Desc.Address)
00101        << format(" 0x%*.*" PRIx64 ")\n",
00102                  hex_width, hex_width, Desc.getEndAddress());
00103   }
00104 }