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" 105ffd83dbSDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 115ffd83dbSDimitry 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 345ffd83dbSDimitry Andric Error DWARFDebugArangeSet::extract(DWARFDataExtractor data, 355ffd83dbSDimitry Andric uint64_t *offset_ptr) { 365ffd83dbSDimitry Andric assert(data.isValidOffset(*offset_ptr)); 370b57cec5SDimitry Andric ArangeDescriptors.clear(); 380b57cec5SDimitry Andric Offset = *offset_ptr; 390b57cec5SDimitry Andric 405ffd83dbSDimitry Andric // 7.21 Address Range Table (extract) 410b57cec5SDimitry Andric // Each set of entries in the table of address ranges contained in 425ffd83dbSDimitry Andric // the .debug_aranges section begins with a header containing: 435ffd83dbSDimitry Andric // 1. unit_length (initial length) 445ffd83dbSDimitry Andric // A 4-byte (32-bit DWARF) or 12-byte (64-bit DWARF) length containing 455ffd83dbSDimitry Andric // the length of the set of entries for this compilation unit, 465ffd83dbSDimitry Andric // not including the length field itself. 475ffd83dbSDimitry Andric // 2. version (uhalf) 485ffd83dbSDimitry Andric // The value in this field is 2. 495ffd83dbSDimitry Andric // 3. debug_info_offset (section offset) 505ffd83dbSDimitry Andric // A 4-byte (32-bit DWARF) or 8-byte (64-bit DWARF) offset into the 515ffd83dbSDimitry Andric // .debug_info section of the compilation unit header. 525ffd83dbSDimitry Andric // 4. address_size (ubyte) 535ffd83dbSDimitry Andric // 5. segment_selector_size (ubyte) 545ffd83dbSDimitry Andric // This header is followed by a series of tuples. Each tuple consists of 555ffd83dbSDimitry Andric // a segment, an address and a length. The segment selector size is given by 565ffd83dbSDimitry Andric // the segment_selector_size field of the header; the address and length 575ffd83dbSDimitry Andric // size are each given by the address_size field of the header. Each set of 585ffd83dbSDimitry Andric // tuples is terminated by a 0 for the segment, a 0 for the address and 0 595ffd83dbSDimitry Andric // for the length. If the segment_selector_size field in the header is zero, 605ffd83dbSDimitry Andric // the segment selectors are omitted from all tuples, including 615ffd83dbSDimitry Andric // the terminating tuple. 620b57cec5SDimitry Andric 635ffd83dbSDimitry Andric Error Err = Error::success(); 645ffd83dbSDimitry Andric std::tie(HeaderData.Length, HeaderData.Format) = 655ffd83dbSDimitry Andric data.getInitialLength(offset_ptr, &Err); 665ffd83dbSDimitry Andric HeaderData.Version = data.getU16(offset_ptr, &Err); 675ffd83dbSDimitry Andric HeaderData.CuOffset = data.getUnsigned( 685ffd83dbSDimitry Andric offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err); 695ffd83dbSDimitry Andric HeaderData.AddrSize = data.getU8(offset_ptr, &Err); 705ffd83dbSDimitry Andric HeaderData.SegSize = data.getU8(offset_ptr, &Err); 715ffd83dbSDimitry Andric if (Err) { 725ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 735ffd83dbSDimitry Andric "parsing address ranges table at offset 0x%" PRIx64 745ffd83dbSDimitry Andric ": %s", 755ffd83dbSDimitry Andric Offset, toString(std::move(Err)).c_str()); 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 785ffd83dbSDimitry Andric // Perform basic validation of the header fields. 795ffd83dbSDimitry Andric uint64_t full_length = 805ffd83dbSDimitry Andric dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length; 815ffd83dbSDimitry Andric if (!data.isValidOffsetForDataOfSize(Offset, full_length)) 825ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 835ffd83dbSDimitry Andric "the length of address range table at offset " 845ffd83dbSDimitry Andric "0x%" PRIx64 " exceeds section size", 855ffd83dbSDimitry Andric Offset); 865ffd83dbSDimitry Andric if (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8) 875ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 885ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 895ffd83dbSDimitry Andric " has unsupported address size: %d " 905ffd83dbSDimitry Andric "(4 and 8 supported)", 915ffd83dbSDimitry Andric Offset, HeaderData.AddrSize); 925ffd83dbSDimitry Andric if (HeaderData.SegSize != 0) 935ffd83dbSDimitry Andric return createStringError(errc::not_supported, 945ffd83dbSDimitry Andric "non-zero segment selector size in address range " 955ffd83dbSDimitry Andric "table at offset 0x%" PRIx64 " is not supported", 965ffd83dbSDimitry Andric Offset); 975ffd83dbSDimitry Andric 985ffd83dbSDimitry Andric // The first tuple following the header in each set begins at an offset that 995ffd83dbSDimitry Andric // is a multiple of the size of a single tuple (that is, twice the size of 1005ffd83dbSDimitry Andric // an address because we do not support non-zero segment selector sizes). 1015ffd83dbSDimitry Andric // Therefore, the full length should also be a multiple of the tuple size. 1020b57cec5SDimitry Andric const uint32_t tuple_size = HeaderData.AddrSize * 2; 1035ffd83dbSDimitry Andric if (full_length % tuple_size != 0) 1045ffd83dbSDimitry Andric return createStringError( 1055ffd83dbSDimitry Andric errc::invalid_argument, 1065ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 1075ffd83dbSDimitry Andric " has length that is not a multiple of the tuple size", 1085ffd83dbSDimitry Andric Offset); 1095ffd83dbSDimitry Andric 1105ffd83dbSDimitry Andric // The header is padded, if necessary, to the appropriate boundary. 1115ffd83dbSDimitry 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 1165ffd83dbSDimitry Andric // There should be space for at least one tuple. 1175ffd83dbSDimitry Andric if (full_length <= first_tuple_offset) 1185ffd83dbSDimitry Andric return createStringError( 1195ffd83dbSDimitry Andric errc::invalid_argument, 1205ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 1215ffd83dbSDimitry Andric " has an insufficient length to contain any entries", 1225ffd83dbSDimitry Andric Offset); 1235ffd83dbSDimitry 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 1335ffd83dbSDimitry Andric uint64_t end_offset = Offset + full_length; 1345ffd83dbSDimitry Andric while (*offset_ptr < end_offset) { 135*eaeb601bSDimitry Andric uint64_t EntryOffset = *offset_ptr; 1360b57cec5SDimitry Andric arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize); 1370b57cec5SDimitry Andric arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize); 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric // Each set of tuples is terminated by a 0 for the address and 0 1400b57cec5SDimitry Andric // for the length. 141*eaeb601bSDimitry Andric if (arangeDescriptor.Length == 0 && arangeDescriptor.Address == 0) { 142*eaeb601bSDimitry Andric if (*offset_ptr == end_offset) 1435ffd83dbSDimitry Andric return ErrorSuccess(); 1445ffd83dbSDimitry Andric return createStringError( 1455ffd83dbSDimitry Andric errc::invalid_argument, 1465ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 147*eaeb601bSDimitry Andric " has a premature terminator entry at offset 0x%" PRIx64, 148*eaeb601bSDimitry Andric Offset, EntryOffset); 1490b57cec5SDimitry Andric } 1500b57cec5SDimitry Andric 1515ffd83dbSDimitry Andric ArangeDescriptors.push_back(arangeDescriptor); 1520b57cec5SDimitry Andric } 1535ffd83dbSDimitry Andric 1545ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 1555ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 1565ffd83dbSDimitry Andric " is not terminated by null entry", 1575ffd83dbSDimitry Andric Offset); 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric void DWARFDebugArangeSet::dump(raw_ostream &OS) const { 1615ffd83dbSDimitry Andric int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format); 1625ffd83dbSDimitry Andric OS << "Address Range Header: " 1635ffd83dbSDimitry Andric << format("length = 0x%0*" PRIx64 ", ", OffsetDumpWidth, HeaderData.Length) 1645ffd83dbSDimitry Andric << "format = " << dwarf::FormatString(HeaderData.Format) << ", " 1655ffd83dbSDimitry Andric << format("version = 0x%4.4x, ", HeaderData.Version) 1665ffd83dbSDimitry Andric << format("cu_offset = 0x%0*" PRIx64 ", ", OffsetDumpWidth, 1675ffd83dbSDimitry Andric HeaderData.CuOffset) 1685ffd83dbSDimitry Andric << format("addr_size = 0x%2.2x, ", HeaderData.AddrSize) 1695ffd83dbSDimitry Andric << format("seg_size = 0x%2.2x\n", HeaderData.SegSize); 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric for (const auto &Desc : ArangeDescriptors) { 1720b57cec5SDimitry Andric Desc.dump(OS, HeaderData.AddrSize); 1730b57cec5SDimitry Andric OS << '\n'; 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric } 176