1226584Sdim//===-- DWARFDebugArangeSet.cpp -------------------------------------------===//
2226584Sdim//
3226584Sdim//                     The LLVM Compiler Infrastructure
4226584Sdim//
5226584Sdim// This file is distributed under the University of Illinois Open Source
6226584Sdim// License. See LICENSE.TXT for details.
7226584Sdim//
8226584Sdim//===----------------------------------------------------------------------===//
9226584Sdim
10226584Sdim#include "DWARFDebugArangeSet.h"
11226584Sdim#include "llvm/Support/Format.h"
12226584Sdim#include "llvm/Support/raw_ostream.h"
13226584Sdim#include <algorithm>
14226584Sdim#include <cassert>
15226584Sdimusing namespace llvm;
16226584Sdim
17226584Sdimvoid DWARFDebugArangeSet::clear() {
18226584Sdim  Offset = -1U;
19249423Sdim  std::memset(&HeaderData, 0, sizeof(Header));
20226584Sdim  ArangeDescriptors.clear();
21226584Sdim}
22226584Sdim
23226584Sdimbool
24226584SdimDWARFDebugArangeSet::extract(DataExtractor data, uint32_t *offset_ptr) {
25226584Sdim  if (data.isValidOffset(*offset_ptr)) {
26226584Sdim    ArangeDescriptors.clear();
27226584Sdim    Offset = *offset_ptr;
28226584Sdim
29226584Sdim    // 7.20 Address Range Table
30226584Sdim    //
31226584Sdim    // Each set of entries in the table of address ranges contained in
32226584Sdim    // the .debug_aranges section begins with a header consisting of: a
33226584Sdim    // 4-byte length containing the length of the set of entries for this
34226584Sdim    // compilation unit, not including the length field itself; a 2-byte
35226584Sdim    // version identifier containing the value 2 for DWARF Version 2; a
36226584Sdim    // 4-byte offset into the.debug_infosection; a 1-byte unsigned integer
37226584Sdim    // containing the size in bytes of an address (or the offset portion of
38226584Sdim    // an address for segmented addressing) on the target system; and a
39226584Sdim    // 1-byte unsigned integer containing the size in bytes of a segment
40226584Sdim    // descriptor on the target system. This header is followed by a series
41226584Sdim    // of tuples. Each tuple consists of an address and a length, each in
42226584Sdim    // the size appropriate for an address on the target architecture.
43249423Sdim    HeaderData.Length = data.getU32(offset_ptr);
44249423Sdim    HeaderData.Version = data.getU16(offset_ptr);
45249423Sdim    HeaderData.CuOffset = data.getU32(offset_ptr);
46249423Sdim    HeaderData.AddrSize = data.getU8(offset_ptr);
47249423Sdim    HeaderData.SegSize = data.getU8(offset_ptr);
48226584Sdim
49226584Sdim    // Perform basic validation of the header fields.
50249423Sdim    if (!data.isValidOffsetForDataOfSize(Offset, HeaderData.Length) ||
51249423Sdim        (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)) {
52226584Sdim      clear();
53226584Sdim      return false;
54226584Sdim    }
55226584Sdim
56226584Sdim    // The first tuple following the header in each set begins at an offset
57226584Sdim    // that is a multiple of the size of a single tuple (that is, twice the
58226584Sdim    // size of an address). The header is padded, if necessary, to the
59226584Sdim    // appropriate boundary.
60226584Sdim    const uint32_t header_size = *offset_ptr - Offset;
61249423Sdim    const uint32_t tuple_size = HeaderData.AddrSize * 2;
62226584Sdim    uint32_t first_tuple_offset = 0;
63226584Sdim    while (first_tuple_offset < header_size)
64226584Sdim      first_tuple_offset += tuple_size;
65226584Sdim
66226584Sdim    *offset_ptr = Offset + first_tuple_offset;
67226584Sdim
68226584Sdim    Descriptor arangeDescriptor;
69226584Sdim
70226584Sdim    assert(sizeof(arangeDescriptor.Address) == sizeof(arangeDescriptor.Length));
71249423Sdim    assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);
72226584Sdim
73226584Sdim    while (data.isValidOffset(*offset_ptr)) {
74249423Sdim      arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
75249423Sdim      arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
76226584Sdim
77226584Sdim      // Each set of tuples is terminated by a 0 for the address and 0
78226584Sdim      // for the length.
79226584Sdim      if (arangeDescriptor.Address || arangeDescriptor.Length)
80226584Sdim        ArangeDescriptors.push_back(arangeDescriptor);
81226584Sdim      else
82226584Sdim        break; // We are done if we get a zero address and length
83226584Sdim    }
84226584Sdim
85226584Sdim    return !ArangeDescriptors.empty();
86226584Sdim  }
87226584Sdim  return false;
88226584Sdim}
89226584Sdim
90226584Sdimvoid DWARFDebugArangeSet::dump(raw_ostream &OS) const {
91226584Sdim  OS << format("Address Range Header: length = 0x%8.8x, version = 0x%4.4x, ",
92249423Sdim               HeaderData.Length, HeaderData.Version)
93226584Sdim     << format("cu_offset = 0x%8.8x, addr_size = 0x%2.2x, seg_size = 0x%2.2x\n",
94249423Sdim               HeaderData.CuOffset, HeaderData.AddrSize, HeaderData.SegSize);
95226584Sdim
96249423Sdim  const uint32_t hex_width = HeaderData.AddrSize * 2;
97226584Sdim  for (DescriptorConstIter pos = ArangeDescriptors.begin(),
98226584Sdim       end = ArangeDescriptors.end(); pos != end; ++pos)
99234353Sdim    OS << format("[0x%*.*" PRIx64 " -", hex_width, hex_width, pos->Address)
100234353Sdim       << format(" 0x%*.*" PRIx64 ")\n",
101234353Sdim                 hex_width, hex_width, pos->getEndAddress());
102226584Sdim}
103