10b57cec5SDimitry Andric //===- DWARFDebugArangeSet.cpp --------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h" 10*5ffd83dbSDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 11*5ffd83dbSDimitry Andric #include "llvm/Support/Errc.h" 120b57cec5SDimitry Andric #include "llvm/Support/Format.h" 130b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 140b57cec5SDimitry Andric #include <cassert> 150b57cec5SDimitry Andric #include <cinttypes> 160b57cec5SDimitry Andric #include <cstdint> 170b57cec5SDimitry Andric #include <cstring> 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric using namespace llvm; 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric void DWARFDebugArangeSet::Descriptor::dump(raw_ostream &OS, 220b57cec5SDimitry Andric uint32_t AddressSize) const { 230b57cec5SDimitry Andric OS << format("[0x%*.*" PRIx64 ", ", AddressSize * 2, AddressSize * 2, Address) 240b57cec5SDimitry Andric << format(" 0x%*.*" PRIx64 ")", AddressSize * 2, AddressSize * 2, 250b57cec5SDimitry Andric getEndAddress()); 260b57cec5SDimitry Andric } 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric void DWARFDebugArangeSet::clear() { 298bcb0991SDimitry Andric Offset = -1ULL; 300b57cec5SDimitry Andric std::memset(&HeaderData, 0, sizeof(Header)); 310b57cec5SDimitry Andric ArangeDescriptors.clear(); 320b57cec5SDimitry Andric } 330b57cec5SDimitry Andric 34*5ffd83dbSDimitry Andric Error DWARFDebugArangeSet::extract(DWARFDataExtractor data, 35*5ffd83dbSDimitry Andric uint64_t *offset_ptr) { 36*5ffd83dbSDimitry Andric assert(data.isValidOffset(*offset_ptr)); 370b57cec5SDimitry Andric ArangeDescriptors.clear(); 380b57cec5SDimitry Andric Offset = *offset_ptr; 390b57cec5SDimitry Andric 40*5ffd83dbSDimitry Andric // 7.21 Address Range Table (extract) 410b57cec5SDimitry Andric // Each set of entries in the table of address ranges contained in 42*5ffd83dbSDimitry Andric // the .debug_aranges section begins with a header containing: 43*5ffd83dbSDimitry Andric // 1. unit_length (initial length) 44*5ffd83dbSDimitry Andric // A 4-byte (32-bit DWARF) or 12-byte (64-bit DWARF) length containing 45*5ffd83dbSDimitry Andric // the length of the set of entries for this compilation unit, 46*5ffd83dbSDimitry Andric // not including the length field itself. 47*5ffd83dbSDimitry Andric // 2. version (uhalf) 48*5ffd83dbSDimitry Andric // The value in this field is 2. 49*5ffd83dbSDimitry Andric // 3. debug_info_offset (section offset) 50*5ffd83dbSDimitry Andric // A 4-byte (32-bit DWARF) or 8-byte (64-bit DWARF) offset into the 51*5ffd83dbSDimitry Andric // .debug_info section of the compilation unit header. 52*5ffd83dbSDimitry Andric // 4. address_size (ubyte) 53*5ffd83dbSDimitry Andric // 5. segment_selector_size (ubyte) 54*5ffd83dbSDimitry Andric // This header is followed by a series of tuples. Each tuple consists of 55*5ffd83dbSDimitry Andric // a segment, an address and a length. The segment selector size is given by 56*5ffd83dbSDimitry Andric // the segment_selector_size field of the header; the address and length 57*5ffd83dbSDimitry Andric // size are each given by the address_size field of the header. Each set of 58*5ffd83dbSDimitry Andric // tuples is terminated by a 0 for the segment, a 0 for the address and 0 59*5ffd83dbSDimitry Andric // for the length. If the segment_selector_size field in the header is zero, 60*5ffd83dbSDimitry Andric // the segment selectors are omitted from all tuples, including 61*5ffd83dbSDimitry Andric // the terminating tuple. 620b57cec5SDimitry Andric 63*5ffd83dbSDimitry Andric Error Err = Error::success(); 64*5ffd83dbSDimitry Andric std::tie(HeaderData.Length, HeaderData.Format) = 65*5ffd83dbSDimitry Andric data.getInitialLength(offset_ptr, &Err); 66*5ffd83dbSDimitry Andric HeaderData.Version = data.getU16(offset_ptr, &Err); 67*5ffd83dbSDimitry Andric HeaderData.CuOffset = data.getUnsigned( 68*5ffd83dbSDimitry Andric offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err); 69*5ffd83dbSDimitry Andric HeaderData.AddrSize = data.getU8(offset_ptr, &Err); 70*5ffd83dbSDimitry Andric HeaderData.SegSize = data.getU8(offset_ptr, &Err); 71*5ffd83dbSDimitry Andric if (Err) { 72*5ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 73*5ffd83dbSDimitry Andric "parsing address ranges table at offset 0x%" PRIx64 74*5ffd83dbSDimitry Andric ": %s", 75*5ffd83dbSDimitry Andric Offset, toString(std::move(Err)).c_str()); 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 78*5ffd83dbSDimitry Andric // Perform basic validation of the header fields. 79*5ffd83dbSDimitry Andric uint64_t full_length = 80*5ffd83dbSDimitry Andric dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length; 81*5ffd83dbSDimitry Andric if (!data.isValidOffsetForDataOfSize(Offset, full_length)) 82*5ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 83*5ffd83dbSDimitry Andric "the length of address range table at offset " 84*5ffd83dbSDimitry Andric "0x%" PRIx64 " exceeds section size", 85*5ffd83dbSDimitry Andric Offset); 86*5ffd83dbSDimitry Andric if (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8) 87*5ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 88*5ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 89*5ffd83dbSDimitry Andric " has unsupported address size: %d " 90*5ffd83dbSDimitry Andric "(4 and 8 supported)", 91*5ffd83dbSDimitry Andric Offset, HeaderData.AddrSize); 92*5ffd83dbSDimitry Andric if (HeaderData.SegSize != 0) 93*5ffd83dbSDimitry Andric return createStringError(errc::not_supported, 94*5ffd83dbSDimitry Andric "non-zero segment selector size in address range " 95*5ffd83dbSDimitry Andric "table at offset 0x%" PRIx64 " is not supported", 96*5ffd83dbSDimitry Andric Offset); 97*5ffd83dbSDimitry Andric 98*5ffd83dbSDimitry Andric // The first tuple following the header in each set begins at an offset that 99*5ffd83dbSDimitry Andric // is a multiple of the size of a single tuple (that is, twice the size of 100*5ffd83dbSDimitry Andric // an address because we do not support non-zero segment selector sizes). 101*5ffd83dbSDimitry Andric // Therefore, the full length should also be a multiple of the tuple size. 1020b57cec5SDimitry Andric const uint32_t tuple_size = HeaderData.AddrSize * 2; 103*5ffd83dbSDimitry Andric if (full_length % tuple_size != 0) 104*5ffd83dbSDimitry Andric return createStringError( 105*5ffd83dbSDimitry Andric errc::invalid_argument, 106*5ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 107*5ffd83dbSDimitry Andric " has length that is not a multiple of the tuple size", 108*5ffd83dbSDimitry Andric Offset); 109*5ffd83dbSDimitry Andric 110*5ffd83dbSDimitry Andric // The header is padded, if necessary, to the appropriate boundary. 111*5ffd83dbSDimitry Andric const uint32_t header_size = *offset_ptr - Offset; 1120b57cec5SDimitry Andric uint32_t first_tuple_offset = 0; 1130b57cec5SDimitry Andric while (first_tuple_offset < header_size) 1140b57cec5SDimitry Andric first_tuple_offset += tuple_size; 1150b57cec5SDimitry Andric 116*5ffd83dbSDimitry Andric // There should be space for at least one tuple. 117*5ffd83dbSDimitry Andric if (full_length <= first_tuple_offset) 118*5ffd83dbSDimitry Andric return createStringError( 119*5ffd83dbSDimitry Andric errc::invalid_argument, 120*5ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 121*5ffd83dbSDimitry Andric " has an insufficient length to contain any entries", 122*5ffd83dbSDimitry Andric Offset); 123*5ffd83dbSDimitry Andric 1240b57cec5SDimitry Andric *offset_ptr = Offset + first_tuple_offset; 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric Descriptor arangeDescriptor; 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric static_assert(sizeof(arangeDescriptor.Address) == 1290b57cec5SDimitry Andric sizeof(arangeDescriptor.Length), 1300b57cec5SDimitry Andric "Different datatypes for addresses and sizes!"); 1310b57cec5SDimitry Andric assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize); 1320b57cec5SDimitry Andric 133*5ffd83dbSDimitry Andric uint64_t end_offset = Offset + full_length; 134*5ffd83dbSDimitry Andric while (*offset_ptr < end_offset) { 1350b57cec5SDimitry Andric arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize); 1360b57cec5SDimitry Andric arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize); 1370b57cec5SDimitry Andric 138*5ffd83dbSDimitry Andric if (arangeDescriptor.Length == 0) { 1390b57cec5SDimitry Andric // Each set of tuples is terminated by a 0 for the address and 0 1400b57cec5SDimitry Andric // for the length. 141*5ffd83dbSDimitry Andric if (arangeDescriptor.Address == 0 && *offset_ptr == end_offset) 142*5ffd83dbSDimitry Andric return ErrorSuccess(); 143*5ffd83dbSDimitry Andric return createStringError( 144*5ffd83dbSDimitry Andric errc::invalid_argument, 145*5ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 146*5ffd83dbSDimitry Andric " has an invalid tuple (length = 0) at offset 0x%" PRIx64, 147*5ffd83dbSDimitry Andric Offset, *offset_ptr - tuple_size); 1480b57cec5SDimitry Andric } 1490b57cec5SDimitry Andric 150*5ffd83dbSDimitry Andric ArangeDescriptors.push_back(arangeDescriptor); 1510b57cec5SDimitry Andric } 152*5ffd83dbSDimitry Andric 153*5ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 154*5ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 155*5ffd83dbSDimitry Andric " is not terminated by null entry", 156*5ffd83dbSDimitry Andric Offset); 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric void DWARFDebugArangeSet::dump(raw_ostream &OS) const { 160*5ffd83dbSDimitry Andric int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format); 161*5ffd83dbSDimitry Andric OS << "Address Range Header: " 162*5ffd83dbSDimitry Andric << format("length = 0x%0*" PRIx64 ", ", OffsetDumpWidth, HeaderData.Length) 163*5ffd83dbSDimitry Andric << "format = " << dwarf::FormatString(HeaderData.Format) << ", " 164*5ffd83dbSDimitry Andric << format("version = 0x%4.4x, ", HeaderData.Version) 165*5ffd83dbSDimitry Andric << format("cu_offset = 0x%0*" PRIx64 ", ", OffsetDumpWidth, 166*5ffd83dbSDimitry Andric HeaderData.CuOffset) 167*5ffd83dbSDimitry Andric << format("addr_size = 0x%2.2x, ", HeaderData.AddrSize) 168*5ffd83dbSDimitry Andric << format("seg_size = 0x%2.2x\n", HeaderData.SegSize); 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric for (const auto &Desc : ArangeDescriptors) { 1710b57cec5SDimitry Andric Desc.dump(OS, HeaderData.AddrSize); 1720b57cec5SDimitry Andric OS << '\n'; 1730b57cec5SDimitry Andric } 1740b57cec5SDimitry Andric } 175