blob: 67589cd01e550e3da26f8e4ce1c8e9e7a08370f2 [file] [log] [blame]
Benjamin Kramera6002fd2011-09-14 01:09:52 +00001//===-- DWARFDebugArangeSet.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
Zachary Turner82af9432015-01-30 18:07:45 +000010#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000011#include "llvm/Support/Format.h"
12#include "llvm/Support/raw_ostream.h"
Benjamin Kramera9e7fb62011-09-14 01:27:48 +000013#include <algorithm>
Benjamin Kramera6002fd2011-09-14 01:09:52 +000014#include <cassert>
15using namespace llvm;
16
17void DWARFDebugArangeSet::clear() {
18 Offset = -1U;
Rafael Espindola43e553d2013-03-20 21:03:41 +000019 std::memset(&HeaderData, 0, sizeof(Header));
Benjamin Kramera6002fd2011-09-14 01:09:52 +000020 ArangeDescriptors.clear();
21}
22
Benjamin Kramera6002fd2011-09-14 01:09:52 +000023bool
24DWARFDebugArangeSet::extract(DataExtractor data, uint32_t *offset_ptr) {
25 if (data.isValidOffset(*offset_ptr)) {
26 ArangeDescriptors.clear();
27 Offset = *offset_ptr;
28
29 // 7.20 Address Range Table
30 //
31 // Each set of entries in the table of address ranges contained in
32 // the .debug_aranges section begins with a header consisting of: a
33 // 4-byte length containing the length of the set of entries for this
34 // compilation unit, not including the length field itself; a 2-byte
35 // version identifier containing the value 2 for DWARF Version 2; a
36 // 4-byte offset into the.debug_infosection; a 1-byte unsigned integer
37 // containing the size in bytes of an address (or the offset portion of
38 // an address for segmented addressing) on the target system; and a
39 // 1-byte unsigned integer containing the size in bytes of a segment
40 // descriptor on the target system. This header is followed by a series
41 // of tuples. Each tuple consists of an address and a length, each in
42 // the size appropriate for an address on the target architecture.
Rafael Espindola43e553d2013-03-20 21:03:41 +000043 HeaderData.Length = data.getU32(offset_ptr);
44 HeaderData.Version = data.getU16(offset_ptr);
45 HeaderData.CuOffset = data.getU32(offset_ptr);
46 HeaderData.AddrSize = data.getU8(offset_ptr);
47 HeaderData.SegSize = data.getU8(offset_ptr);
Benjamin Kramera6002fd2011-09-14 01:09:52 +000048
Benjamin Kramer973b5cd2011-09-14 17:28:13 +000049 // Perform basic validation of the header fields.
Rafael Espindola43e553d2013-03-20 21:03:41 +000050 if (!data.isValidOffsetForDataOfSize(Offset, HeaderData.Length) ||
51 (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)) {
Benjamin Kramer973b5cd2011-09-14 17:28:13 +000052 clear();
53 return false;
54 }
55
Benjamin Kramera6002fd2011-09-14 01:09:52 +000056 // The first tuple following the header in each set begins at an offset
57 // that is a multiple of the size of a single tuple (that is, twice the
58 // size of an address). The header is padded, if necessary, to the
59 // appropriate boundary.
60 const uint32_t header_size = *offset_ptr - Offset;
Rafael Espindola43e553d2013-03-20 21:03:41 +000061 const uint32_t tuple_size = HeaderData.AddrSize * 2;
Benjamin Kramera6002fd2011-09-14 01:09:52 +000062 uint32_t first_tuple_offset = 0;
63 while (first_tuple_offset < header_size)
64 first_tuple_offset += tuple_size;
65
66 *offset_ptr = Offset + first_tuple_offset;
67
68 Descriptor arangeDescriptor;
69
Benjamin Kramer86c77412014-03-15 18:47:07 +000070 static_assert(sizeof(arangeDescriptor.Address) ==
71 sizeof(arangeDescriptor.Length),
72 "Different datatypes for addresses and sizes!");
Rafael Espindola43e553d2013-03-20 21:03:41 +000073 assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);
Benjamin Kramera6002fd2011-09-14 01:09:52 +000074
75 while (data.isValidOffset(*offset_ptr)) {
Rafael Espindola43e553d2013-03-20 21:03:41 +000076 arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
77 arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
Benjamin Kramera6002fd2011-09-14 01:09:52 +000078
79 // Each set of tuples is terminated by a 0 for the address and 0
80 // for the length.
81 if (arangeDescriptor.Address || arangeDescriptor.Length)
82 ArangeDescriptors.push_back(arangeDescriptor);
83 else
84 break; // We are done if we get a zero address and length
85 }
86
87 return !ArangeDescriptors.empty();
88 }
89 return false;
90}
91
92void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
93 OS << format("Address Range Header: length = 0x%8.8x, version = 0x%4.4x, ",
Rafael Espindola43e553d2013-03-20 21:03:41 +000094 HeaderData.Length, HeaderData.Version)
Benjamin Kramera6002fd2011-09-14 01:09:52 +000095 << format("cu_offset = 0x%8.8x, addr_size = 0x%2.2x, seg_size = 0x%2.2x\n",
Rafael Espindola43e553d2013-03-20 21:03:41 +000096 HeaderData.CuOffset, HeaderData.AddrSize, HeaderData.SegSize);
Benjamin Kramera6002fd2011-09-14 01:09:52 +000097
Rafael Espindola43e553d2013-03-20 21:03:41 +000098 const uint32_t hex_width = HeaderData.AddrSize * 2;
Alexey Samsonov1eabf982014-03-13 07:52:54 +000099 for (const auto &Desc : ArangeDescriptors) {
100 OS << format("[0x%*.*" PRIx64 " -", hex_width, hex_width, Desc.Address)
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000101 << format(" 0x%*.*" PRIx64 ")\n",
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000102 hex_width, hex_width, Desc.getEndAddress());
103 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000104}