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" 11*e8d8bef9SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 125ffd83dbSDimitry Andric #include "llvm/Support/Errc.h" 130b57cec5SDimitry Andric #include "llvm/Support/Format.h" 140b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 150b57cec5SDimitry Andric #include <cassert> 160b57cec5SDimitry Andric #include <cinttypes> 170b57cec5SDimitry Andric #include <cstdint> 180b57cec5SDimitry Andric #include <cstring> 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric using namespace llvm; 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric void DWARFDebugArangeSet::Descriptor::dump(raw_ostream &OS, 230b57cec5SDimitry Andric uint32_t AddressSize) const { 24*e8d8bef9SDimitry Andric OS << '['; 25*e8d8bef9SDimitry Andric DWARFFormValue::dumpAddress(OS, AddressSize, Address); 26*e8d8bef9SDimitry Andric OS << ", "; 27*e8d8bef9SDimitry Andric DWARFFormValue::dumpAddress(OS, AddressSize, getEndAddress()); 28*e8d8bef9SDimitry Andric OS << ')'; 290b57cec5SDimitry Andric } 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric void DWARFDebugArangeSet::clear() { 328bcb0991SDimitry Andric Offset = -1ULL; 330b57cec5SDimitry Andric std::memset(&HeaderData, 0, sizeof(Header)); 340b57cec5SDimitry Andric ArangeDescriptors.clear(); 350b57cec5SDimitry Andric } 360b57cec5SDimitry Andric 375ffd83dbSDimitry Andric Error DWARFDebugArangeSet::extract(DWARFDataExtractor data, 38*e8d8bef9SDimitry Andric uint64_t *offset_ptr, 39*e8d8bef9SDimitry Andric function_ref<void(Error)> WarningHandler) { 405ffd83dbSDimitry Andric assert(data.isValidOffset(*offset_ptr)); 410b57cec5SDimitry Andric ArangeDescriptors.clear(); 420b57cec5SDimitry Andric Offset = *offset_ptr; 430b57cec5SDimitry Andric 445ffd83dbSDimitry Andric // 7.21 Address Range Table (extract) 450b57cec5SDimitry Andric // Each set of entries in the table of address ranges contained in 465ffd83dbSDimitry Andric // the .debug_aranges section begins with a header containing: 475ffd83dbSDimitry Andric // 1. unit_length (initial length) 485ffd83dbSDimitry Andric // A 4-byte (32-bit DWARF) or 12-byte (64-bit DWARF) length containing 495ffd83dbSDimitry Andric // the length of the set of entries for this compilation unit, 505ffd83dbSDimitry Andric // not including the length field itself. 515ffd83dbSDimitry Andric // 2. version (uhalf) 525ffd83dbSDimitry Andric // The value in this field is 2. 535ffd83dbSDimitry Andric // 3. debug_info_offset (section offset) 545ffd83dbSDimitry Andric // A 4-byte (32-bit DWARF) or 8-byte (64-bit DWARF) offset into the 555ffd83dbSDimitry Andric // .debug_info section of the compilation unit header. 565ffd83dbSDimitry Andric // 4. address_size (ubyte) 575ffd83dbSDimitry Andric // 5. segment_selector_size (ubyte) 585ffd83dbSDimitry Andric // This header is followed by a series of tuples. Each tuple consists of 595ffd83dbSDimitry Andric // a segment, an address and a length. The segment selector size is given by 605ffd83dbSDimitry Andric // the segment_selector_size field of the header; the address and length 615ffd83dbSDimitry Andric // size are each given by the address_size field of the header. Each set of 625ffd83dbSDimitry Andric // tuples is terminated by a 0 for the segment, a 0 for the address and 0 635ffd83dbSDimitry Andric // for the length. If the segment_selector_size field in the header is zero, 645ffd83dbSDimitry Andric // the segment selectors are omitted from all tuples, including 655ffd83dbSDimitry Andric // the terminating tuple. 660b57cec5SDimitry Andric 675ffd83dbSDimitry Andric Error Err = Error::success(); 685ffd83dbSDimitry Andric std::tie(HeaderData.Length, HeaderData.Format) = 695ffd83dbSDimitry Andric data.getInitialLength(offset_ptr, &Err); 705ffd83dbSDimitry Andric HeaderData.Version = data.getU16(offset_ptr, &Err); 715ffd83dbSDimitry Andric HeaderData.CuOffset = data.getUnsigned( 725ffd83dbSDimitry Andric offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err); 735ffd83dbSDimitry Andric HeaderData.AddrSize = data.getU8(offset_ptr, &Err); 745ffd83dbSDimitry Andric HeaderData.SegSize = data.getU8(offset_ptr, &Err); 755ffd83dbSDimitry Andric if (Err) { 765ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 775ffd83dbSDimitry Andric "parsing address ranges table at offset 0x%" PRIx64 785ffd83dbSDimitry Andric ": %s", 795ffd83dbSDimitry Andric Offset, toString(std::move(Err)).c_str()); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric 825ffd83dbSDimitry Andric // Perform basic validation of the header fields. 835ffd83dbSDimitry Andric uint64_t full_length = 845ffd83dbSDimitry Andric dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length; 855ffd83dbSDimitry Andric if (!data.isValidOffsetForDataOfSize(Offset, full_length)) 865ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 875ffd83dbSDimitry Andric "the length of address range table at offset " 885ffd83dbSDimitry Andric "0x%" PRIx64 " exceeds section size", 895ffd83dbSDimitry Andric Offset); 905ffd83dbSDimitry Andric if (HeaderData.AddrSize != 4 && HeaderData.AddrSize != 8) 915ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 925ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 935ffd83dbSDimitry Andric " has unsupported address size: %d " 945ffd83dbSDimitry Andric "(4 and 8 supported)", 955ffd83dbSDimitry Andric Offset, HeaderData.AddrSize); 965ffd83dbSDimitry Andric if (HeaderData.SegSize != 0) 975ffd83dbSDimitry Andric return createStringError(errc::not_supported, 985ffd83dbSDimitry Andric "non-zero segment selector size in address range " 995ffd83dbSDimitry Andric "table at offset 0x%" PRIx64 " is not supported", 1005ffd83dbSDimitry Andric Offset); 1015ffd83dbSDimitry Andric 1025ffd83dbSDimitry Andric // The first tuple following the header in each set begins at an offset that 1035ffd83dbSDimitry Andric // is a multiple of the size of a single tuple (that is, twice the size of 1045ffd83dbSDimitry Andric // an address because we do not support non-zero segment selector sizes). 1055ffd83dbSDimitry Andric // Therefore, the full length should also be a multiple of the tuple size. 1060b57cec5SDimitry Andric const uint32_t tuple_size = HeaderData.AddrSize * 2; 1075ffd83dbSDimitry Andric if (full_length % tuple_size != 0) 1085ffd83dbSDimitry Andric return createStringError( 1095ffd83dbSDimitry Andric errc::invalid_argument, 1105ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 1115ffd83dbSDimitry Andric " has length that is not a multiple of the tuple size", 1125ffd83dbSDimitry Andric Offset); 1135ffd83dbSDimitry Andric 1145ffd83dbSDimitry Andric // The header is padded, if necessary, to the appropriate boundary. 1155ffd83dbSDimitry Andric const uint32_t header_size = *offset_ptr - Offset; 1160b57cec5SDimitry Andric uint32_t first_tuple_offset = 0; 1170b57cec5SDimitry Andric while (first_tuple_offset < header_size) 1180b57cec5SDimitry Andric first_tuple_offset += tuple_size; 1190b57cec5SDimitry Andric 1205ffd83dbSDimitry Andric // There should be space for at least one tuple. 1215ffd83dbSDimitry Andric if (full_length <= first_tuple_offset) 1225ffd83dbSDimitry Andric return createStringError( 1235ffd83dbSDimitry Andric errc::invalid_argument, 1245ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 1255ffd83dbSDimitry Andric " has an insufficient length to contain any entries", 1265ffd83dbSDimitry Andric Offset); 1275ffd83dbSDimitry Andric 1280b57cec5SDimitry Andric *offset_ptr = Offset + first_tuple_offset; 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric Descriptor arangeDescriptor; 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric static_assert(sizeof(arangeDescriptor.Address) == 1330b57cec5SDimitry Andric sizeof(arangeDescriptor.Length), 1340b57cec5SDimitry Andric "Different datatypes for addresses and sizes!"); 1350b57cec5SDimitry Andric assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize); 1360b57cec5SDimitry Andric 1375ffd83dbSDimitry Andric uint64_t end_offset = Offset + full_length; 1385ffd83dbSDimitry Andric while (*offset_ptr < end_offset) { 139eaeb601bSDimitry Andric uint64_t EntryOffset = *offset_ptr; 1400b57cec5SDimitry Andric arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize); 1410b57cec5SDimitry Andric arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize); 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric // Each set of tuples is terminated by a 0 for the address and 0 1440b57cec5SDimitry Andric // for the length. 145eaeb601bSDimitry Andric if (arangeDescriptor.Length == 0 && arangeDescriptor.Address == 0) { 146eaeb601bSDimitry Andric if (*offset_ptr == end_offset) 1475ffd83dbSDimitry Andric return ErrorSuccess(); 148*e8d8bef9SDimitry Andric WarningHandler(createStringError( 1495ffd83dbSDimitry Andric errc::invalid_argument, 1505ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 151eaeb601bSDimitry Andric " has a premature terminator entry at offset 0x%" PRIx64, 152*e8d8bef9SDimitry Andric Offset, EntryOffset)); 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric 1555ffd83dbSDimitry Andric ArangeDescriptors.push_back(arangeDescriptor); 1560b57cec5SDimitry Andric } 1575ffd83dbSDimitry Andric 1585ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 1595ffd83dbSDimitry Andric "address range table at offset 0x%" PRIx64 1605ffd83dbSDimitry Andric " is not terminated by null entry", 1615ffd83dbSDimitry Andric Offset); 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric void DWARFDebugArangeSet::dump(raw_ostream &OS) const { 1655ffd83dbSDimitry Andric int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format); 1665ffd83dbSDimitry Andric OS << "Address Range Header: " 1675ffd83dbSDimitry Andric << format("length = 0x%0*" PRIx64 ", ", OffsetDumpWidth, HeaderData.Length) 1685ffd83dbSDimitry Andric << "format = " << dwarf::FormatString(HeaderData.Format) << ", " 1695ffd83dbSDimitry Andric << format("version = 0x%4.4x, ", HeaderData.Version) 1705ffd83dbSDimitry Andric << format("cu_offset = 0x%0*" PRIx64 ", ", OffsetDumpWidth, 1715ffd83dbSDimitry Andric HeaderData.CuOffset) 1725ffd83dbSDimitry Andric << format("addr_size = 0x%2.2x, ", HeaderData.AddrSize) 1735ffd83dbSDimitry Andric << format("seg_size = 0x%2.2x\n", HeaderData.SegSize); 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric for (const auto &Desc : ArangeDescriptors) { 1760b57cec5SDimitry Andric Desc.dump(OS, HeaderData.AddrSize); 1770b57cec5SDimitry Andric OS << '\n'; 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric } 180