xref: /freebsd-src/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugArangeSet.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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*349cc55cSDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h"
12e8d8bef9SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
135ffd83dbSDimitry Andric #include "llvm/Support/Errc.h"
140b57cec5SDimitry Andric #include "llvm/Support/Format.h"
150b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
160b57cec5SDimitry Andric #include <cassert>
170b57cec5SDimitry Andric #include <cinttypes>
180b57cec5SDimitry Andric #include <cstdint>
190b57cec5SDimitry Andric #include <cstring>
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
dump(raw_ostream & OS,uint32_t AddressSize) const230b57cec5SDimitry Andric void DWARFDebugArangeSet::Descriptor::dump(raw_ostream &OS,
240b57cec5SDimitry Andric                                            uint32_t AddressSize) const {
25e8d8bef9SDimitry Andric   OS << '[';
26e8d8bef9SDimitry Andric   DWARFFormValue::dumpAddress(OS, AddressSize, Address);
27e8d8bef9SDimitry Andric   OS << ", ";
28e8d8bef9SDimitry Andric   DWARFFormValue::dumpAddress(OS, AddressSize, getEndAddress());
29e8d8bef9SDimitry Andric   OS << ')';
300b57cec5SDimitry Andric }
310b57cec5SDimitry Andric 
clear()320b57cec5SDimitry Andric void DWARFDebugArangeSet::clear() {
338bcb0991SDimitry Andric   Offset = -1ULL;
340b57cec5SDimitry Andric   std::memset(&HeaderData, 0, sizeof(Header));
350b57cec5SDimitry Andric   ArangeDescriptors.clear();
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
extract(DWARFDataExtractor data,uint64_t * offset_ptr,function_ref<void (Error)> WarningHandler)385ffd83dbSDimitry Andric Error DWARFDebugArangeSet::extract(DWARFDataExtractor data,
39e8d8bef9SDimitry Andric                                    uint64_t *offset_ptr,
40e8d8bef9SDimitry Andric                                    function_ref<void(Error)> WarningHandler) {
415ffd83dbSDimitry Andric   assert(data.isValidOffset(*offset_ptr));
420b57cec5SDimitry Andric   ArangeDescriptors.clear();
430b57cec5SDimitry Andric   Offset = *offset_ptr;
440b57cec5SDimitry Andric 
455ffd83dbSDimitry Andric   // 7.21 Address Range Table (extract)
460b57cec5SDimitry Andric   // Each set of entries in the table of address ranges contained in
475ffd83dbSDimitry Andric   // the .debug_aranges section begins with a header containing:
485ffd83dbSDimitry Andric   // 1. unit_length (initial length)
495ffd83dbSDimitry Andric   //    A 4-byte (32-bit DWARF) or 12-byte (64-bit DWARF) length containing
505ffd83dbSDimitry Andric   //    the length of the set of entries for this compilation unit,
515ffd83dbSDimitry Andric   //    not including the length field itself.
525ffd83dbSDimitry Andric   // 2. version (uhalf)
535ffd83dbSDimitry Andric   //    The value in this field is 2.
545ffd83dbSDimitry Andric   // 3. debug_info_offset (section offset)
555ffd83dbSDimitry Andric   //    A 4-byte (32-bit DWARF) or 8-byte (64-bit DWARF) offset into the
565ffd83dbSDimitry Andric   //    .debug_info section of the compilation unit header.
575ffd83dbSDimitry Andric   // 4. address_size (ubyte)
585ffd83dbSDimitry Andric   // 5. segment_selector_size (ubyte)
595ffd83dbSDimitry Andric   // This header is followed by a series of tuples. Each tuple consists of
605ffd83dbSDimitry Andric   // a segment, an address and a length. The segment selector size is given by
615ffd83dbSDimitry Andric   // the segment_selector_size field of the header; the address and length
625ffd83dbSDimitry Andric   // size are each given by the address_size field of the header. Each set of
635ffd83dbSDimitry Andric   // tuples is terminated by a 0 for the segment, a 0 for the address and 0
645ffd83dbSDimitry Andric   // for the length. If the segment_selector_size field in the header is zero,
655ffd83dbSDimitry Andric   // the segment selectors are omitted from all tuples, including
665ffd83dbSDimitry Andric   // the terminating tuple.
670b57cec5SDimitry Andric 
685ffd83dbSDimitry Andric   Error Err = Error::success();
695ffd83dbSDimitry Andric   std::tie(HeaderData.Length, HeaderData.Format) =
705ffd83dbSDimitry Andric       data.getInitialLength(offset_ptr, &Err);
715ffd83dbSDimitry Andric   HeaderData.Version = data.getU16(offset_ptr, &Err);
725ffd83dbSDimitry Andric   HeaderData.CuOffset = data.getUnsigned(
735ffd83dbSDimitry Andric       offset_ptr, dwarf::getDwarfOffsetByteSize(HeaderData.Format), &Err);
745ffd83dbSDimitry Andric   HeaderData.AddrSize = data.getU8(offset_ptr, &Err);
755ffd83dbSDimitry Andric   HeaderData.SegSize = data.getU8(offset_ptr, &Err);
765ffd83dbSDimitry Andric   if (Err) {
775ffd83dbSDimitry Andric     return createStringError(errc::invalid_argument,
785ffd83dbSDimitry Andric                              "parsing address ranges table at offset 0x%" PRIx64
795ffd83dbSDimitry Andric                              ": %s",
805ffd83dbSDimitry Andric                              Offset, toString(std::move(Err)).c_str());
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric 
835ffd83dbSDimitry Andric   // Perform basic validation of the header fields.
845ffd83dbSDimitry Andric   uint64_t full_length =
855ffd83dbSDimitry Andric       dwarf::getUnitLengthFieldByteSize(HeaderData.Format) + HeaderData.Length;
865ffd83dbSDimitry Andric   if (!data.isValidOffsetForDataOfSize(Offset, full_length))
875ffd83dbSDimitry Andric     return createStringError(errc::invalid_argument,
885ffd83dbSDimitry Andric                              "the length of address range table at offset "
895ffd83dbSDimitry Andric                              "0x%" PRIx64 " exceeds section size",
905ffd83dbSDimitry Andric                              Offset);
91*349cc55cSDimitry Andric   if (Error SizeErr = DWARFContext::checkAddressSizeSupported(
92*349cc55cSDimitry Andric           HeaderData.AddrSize, errc::invalid_argument,
93*349cc55cSDimitry Andric           "address range table at offset 0x%" PRIx64, Offset))
94*349cc55cSDimitry Andric     return SizeErr;
955ffd83dbSDimitry Andric   if (HeaderData.SegSize != 0)
965ffd83dbSDimitry Andric     return createStringError(errc::not_supported,
975ffd83dbSDimitry Andric                              "non-zero segment selector size in address range "
985ffd83dbSDimitry Andric                              "table at offset 0x%" PRIx64 " is not supported",
995ffd83dbSDimitry Andric                              Offset);
1005ffd83dbSDimitry Andric 
1015ffd83dbSDimitry Andric   // The first tuple following the header in each set begins at an offset that
1025ffd83dbSDimitry Andric   // is a multiple of the size of a single tuple (that is, twice the size of
1035ffd83dbSDimitry Andric   // an address because we do not support non-zero segment selector sizes).
1045ffd83dbSDimitry Andric   // Therefore, the full length should also be a multiple of the tuple size.
1050b57cec5SDimitry Andric   const uint32_t tuple_size = HeaderData.AddrSize * 2;
1065ffd83dbSDimitry Andric   if (full_length % tuple_size != 0)
1075ffd83dbSDimitry Andric     return createStringError(
1085ffd83dbSDimitry Andric         errc::invalid_argument,
1095ffd83dbSDimitry Andric         "address range table at offset 0x%" PRIx64
1105ffd83dbSDimitry Andric         " has length that is not a multiple of the tuple size",
1115ffd83dbSDimitry Andric         Offset);
1125ffd83dbSDimitry Andric 
1135ffd83dbSDimitry Andric   // The header is padded, if necessary, to the appropriate boundary.
1145ffd83dbSDimitry Andric   const uint32_t header_size = *offset_ptr - Offset;
1150b57cec5SDimitry Andric   uint32_t first_tuple_offset = 0;
1160b57cec5SDimitry Andric   while (first_tuple_offset < header_size)
1170b57cec5SDimitry Andric     first_tuple_offset += tuple_size;
1180b57cec5SDimitry Andric 
1195ffd83dbSDimitry Andric   // There should be space for at least one tuple.
1205ffd83dbSDimitry Andric   if (full_length <= first_tuple_offset)
1215ffd83dbSDimitry Andric     return createStringError(
1225ffd83dbSDimitry Andric         errc::invalid_argument,
1235ffd83dbSDimitry Andric         "address range table at offset 0x%" PRIx64
1245ffd83dbSDimitry Andric         " has an insufficient length to contain any entries",
1255ffd83dbSDimitry Andric         Offset);
1265ffd83dbSDimitry Andric 
1270b57cec5SDimitry Andric   *offset_ptr = Offset + first_tuple_offset;
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   Descriptor arangeDescriptor;
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   static_assert(sizeof(arangeDescriptor.Address) ==
1320b57cec5SDimitry Andric                     sizeof(arangeDescriptor.Length),
1330b57cec5SDimitry Andric                 "Different datatypes for addresses and sizes!");
1340b57cec5SDimitry Andric   assert(sizeof(arangeDescriptor.Address) >= HeaderData.AddrSize);
1350b57cec5SDimitry Andric 
1365ffd83dbSDimitry Andric   uint64_t end_offset = Offset + full_length;
1375ffd83dbSDimitry Andric   while (*offset_ptr < end_offset) {
138eaeb601bSDimitry Andric     uint64_t EntryOffset = *offset_ptr;
1390b57cec5SDimitry Andric     arangeDescriptor.Address = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
1400b57cec5SDimitry Andric     arangeDescriptor.Length = data.getUnsigned(offset_ptr, HeaderData.AddrSize);
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric     // Each set of tuples is terminated by a 0 for the address and 0
1430b57cec5SDimitry Andric     // for the length.
144eaeb601bSDimitry Andric     if (arangeDescriptor.Length == 0 && arangeDescriptor.Address == 0) {
145eaeb601bSDimitry Andric       if (*offset_ptr == end_offset)
1465ffd83dbSDimitry Andric         return ErrorSuccess();
147e8d8bef9SDimitry Andric       WarningHandler(createStringError(
1485ffd83dbSDimitry Andric           errc::invalid_argument,
1495ffd83dbSDimitry Andric           "address range table at offset 0x%" PRIx64
150eaeb601bSDimitry Andric           " has a premature terminator entry at offset 0x%" PRIx64,
151e8d8bef9SDimitry Andric           Offset, EntryOffset));
1520b57cec5SDimitry Andric     }
1530b57cec5SDimitry Andric 
1545ffd83dbSDimitry Andric     ArangeDescriptors.push_back(arangeDescriptor);
1550b57cec5SDimitry Andric   }
1565ffd83dbSDimitry Andric 
1575ffd83dbSDimitry Andric   return createStringError(errc::invalid_argument,
1585ffd83dbSDimitry Andric                            "address range table at offset 0x%" PRIx64
1595ffd83dbSDimitry Andric                            " is not terminated by null entry",
1605ffd83dbSDimitry Andric                            Offset);
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric 
dump(raw_ostream & OS) const1630b57cec5SDimitry Andric void DWARFDebugArangeSet::dump(raw_ostream &OS) const {
1645ffd83dbSDimitry Andric   int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(HeaderData.Format);
1655ffd83dbSDimitry Andric   OS << "Address Range Header: "
1665ffd83dbSDimitry Andric      << format("length = 0x%0*" PRIx64 ", ", OffsetDumpWidth, HeaderData.Length)
1675ffd83dbSDimitry Andric      << "format = " << dwarf::FormatString(HeaderData.Format) << ", "
1685ffd83dbSDimitry Andric      << format("version = 0x%4.4x, ", HeaderData.Version)
1695ffd83dbSDimitry Andric      << format("cu_offset = 0x%0*" PRIx64 ", ", OffsetDumpWidth,
1705ffd83dbSDimitry Andric                HeaderData.CuOffset)
1715ffd83dbSDimitry Andric      << format("addr_size = 0x%2.2x, ", HeaderData.AddrSize)
1725ffd83dbSDimitry Andric      << format("seg_size = 0x%2.2x\n", HeaderData.SegSize);
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   for (const auto &Desc : ArangeDescriptors) {
1750b57cec5SDimitry Andric     Desc.dump(OS, HeaderData.AddrSize);
1760b57cec5SDimitry Andric     OS << '\n';
1770b57cec5SDimitry Andric   }
1780b57cec5SDimitry Andric }
179