| Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 1 | //===- DWARFDebugArangeSet.cpp --------------------------------------------===// |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 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 Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h" |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 11 | #include "llvm/Support/Format.h" |
| 12 | #include "llvm/Support/raw_ostream.h" |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 13 | #include <cassert> |
| Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 14 | #include <cinttypes> |
| 15 | #include <cstdint> |
| 16 | #include <cstring> |
| 17 | |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
| 20 | void DWARFDebugArangeSet::clear() { |
| 21 | Offset = -1U; |
| Rafael Espindola | 43e553d | 2013-03-20 21:03:41 +0000 | [diff] [blame] | 22 | std::memset(&HeaderData, 0, sizeof(Header)); |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 23 | ArangeDescriptors.clear(); |
| 24 | } |
| 25 | |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 26 | bool |
| 27 | DWARFDebugArangeSet::extract(DataExtractor data, uint32_t *offset_ptr) { |
| 28 | if (data.isValidOffset(*offset_ptr)) { |
| 29 | ArangeDescriptors.clear(); |
| 30 | Offset = *offset_ptr; |
| 31 | |
| 32 | // 7.20 Address Range Table |
| 33 | // |
| 34 | // Each set of entries in the table of address ranges contained in |
| 35 | // the .debug_aranges section begins with a header consisting of: a |
| 36 | // 4-byte length containing the length of the set of entries for this |
| 37 | // compilation unit, not including the length field itself; a 2-byte |
| 38 | // version identifier containing the value 2 for DWARF Version 2; a |
| 39 | // 4-byte offset into the.debug_infosection; a 1-byte unsigned integer |
| 40 | // containing the size in bytes of an address (or the offset portion of |
| 41 | // an address for segmented addressing) on the target system; and a |
| 42 | // 1-byte unsigned integer containing the size in bytes of a segment |
| 43 | // descriptor on the target system. This header is followed by a series |
| 44 | // of tuples. Each tuple consists of an address and a length, each in |
| 45 | // the size appropriate for an address on the target architecture. |
| Rafael Espindola | 43e553d | 2013-03-20 21:03:41 +0000 | [diff] [blame] | 46 | HeaderData.Length = data.getU32(offset_ptr); |
| 47 | HeaderData.Version = data.getU16(offset_ptr); |
| 48 | HeaderData.CuOffset = data.getU32(offset_ptr); |
| 49 | HeaderData.AddrSize = data.getU8(offset_ptr); |
| 50 | HeaderData.SegSize = data.getU8(offset_ptr); |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 51 | |
| Benjamin Kramer | 973b5cd | 2011-09-14 17:28:13 +0000 | [diff] [blame] | 52 | // Perform basic validation of the header fields. |
| Rafael Espindola | 43e553d | 2013-03-20 21:03:41 +0000 | [diff] [blame] | 53 | if (!data.isValidOffsetForDataOfSize(Offset, HeaderData.Length) || |
| 54 | (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8)) { |
| Benjamin Kramer | 973b5cd | 2011-09-14 17:28:13 +0000 | [diff] [blame] | 55 | clear(); |
| 56 | return false; |
| 57 | } |
| 58 | |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 59 | // The first tuple following the header in each set begins at an offset |
| 60 | // that is a multiple of the size of a single tuple (that is, twice the |
| 61 | // size of an address). The header is padded, if necessary, to the |
| 62 | // appropriate boundary. |
| 63 | const uint32_t header_size = *offset_ptr - Offset; |
| Rafael Espindola | 43e553d | 2013-03-20 21:03:41 +0000 | [diff] [blame] | 64 | const uint32_t tuple_size = HeaderData.AddrSize * 2; |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 65 | uint32_t first_tuple_offset = 0; |
| 66 | while (first_tuple_offset < header_size) |
| 67 | first_tuple_offset += tuple_size; |
| 68 | |
| 69 | *offset_ptr = Offset + first_tuple_offset; |
| 70 | |
| 71 | Descriptor arangeDescriptor; |
| 72 | |
| Benjamin Kramer | 86c7741 | 2014-03-15 18:47:07 +0000 | [diff] [blame] | 73 | static_assert(sizeof(arangeDescriptor.Address) == |
| 74 | sizeof(arangeDescriptor.Length), |
| 75 | "Different datatypes for addresses and sizes!"); |
| Rafael Espindola | 43e553d | 2013-03-20 21:03:41 +0000 | [diff] [blame] | 76 | assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize); |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 77 | |
| 78 | while (data.isValidOffset(*offset_ptr)) { |
| Rafael Espindola | 43e553d | 2013-03-20 21:03:41 +0000 | [diff] [blame] | 79 | arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize); |
| 80 | arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize); |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 81 | |
| 82 | // Each set of tuples is terminated by a 0 for the address and 0 |
| 83 | // for the length. |
| 84 | if (arangeDescriptor.Address || arangeDescriptor.Length) |
| 85 | ArangeDescriptors.push_back(arangeDescriptor); |
| 86 | else |
| 87 | break; // We are done if we get a zero address and length |
| 88 | } |
| 89 | |
| 90 | return !ArangeDescriptors.empty(); |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | void DWARFDebugArangeSet::dump(raw_ostream &OS) const { |
| 96 | OS << format("Address Range Header: length = 0x%8.8x, version = 0x%4.4x, ", |
| Rafael Espindola | 43e553d | 2013-03-20 21:03:41 +0000 | [diff] [blame] | 97 | HeaderData.Length, HeaderData.Version) |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 98 | << format("cu_offset = 0x%8.8x, addr_size = 0x%2.2x, seg_size = 0x%2.2x\n", |
| Rafael Espindola | 43e553d | 2013-03-20 21:03:41 +0000 | [diff] [blame] | 99 | HeaderData.CuOffset, HeaderData.AddrSize, HeaderData.SegSize); |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 100 | |
| Rafael Espindola | 43e553d | 2013-03-20 21:03:41 +0000 | [diff] [blame] | 101 | const uint32_t hex_width = HeaderData.AddrSize * 2; |
| Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 102 | for (const auto &Desc : ArangeDescriptors) { |
| 103 | OS << format("[0x%*.*" PRIx64 " -", hex_width, hex_width, Desc.Address) |
| Benjamin Kramer | f3da529 | 2011-11-05 08:57:40 +0000 | [diff] [blame] | 104 | << format(" 0x%*.*" PRIx64 ")\n", |
| Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 105 | hex_width, hex_width, Desc.getEndAddress()); |
| 106 | } |
| Benjamin Kramer | a6002fd | 2011-09-14 01:09:52 +0000 | [diff] [blame] | 107 | } |