blob: 84e3c634f54fc3454826e0e085bce0838157f0ff [file] [log] [blame]
Eugene Zelenko28db7e62017-03-01 01:14:23 +00001//===- DWARFDebugRangesList.cpp -------------------------------------------===//
Alexey Samsonov034e57a2012-08-27 07:17:47 +00002//
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 Turner82af9432015-01-30 18:07:45 +000010#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Victor Leschukcba595d2018-08-20 09:59:08 +000012#include "llvm/Support/Errc.h"
Alexey Samsonov034e57a2012-08-27 07:17:47 +000013#include "llvm/Support/Format.h"
14#include "llvm/Support/raw_ostream.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000015#include <cinttypes>
16#include <cstdint>
Alexey Samsonov034e57a2012-08-27 07:17:47 +000017
18using namespace llvm;
19
20void DWARFDebugRangeList::clear() {
21 Offset = -1U;
22 AddressSize = 0;
23 Entries.clear();
24}
25
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +000026Error DWARFDebugRangeList::extract(const DWARFDataExtractor &data,
27 uint32_t *offset_ptr) {
Alexey Samsonov034e57a2012-08-27 07:17:47 +000028 clear();
29 if (!data.isValidOffset(*offset_ptr))
Victor Leschukcba595d2018-08-20 09:59:08 +000030 return createStringError(errc::invalid_argument,
31 "invalid range list offset 0x%" PRIx32, *offset_ptr);
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +000032
Alexey Samsonov034e57a2012-08-27 07:17:47 +000033 AddressSize = data.getAddressSize();
34 if (AddressSize != 4 && AddressSize != 8)
Victor Leschukcba595d2018-08-20 09:59:08 +000035 return createStringError(errc::invalid_argument,
36 "invalid address size: %" PRIu8, AddressSize);
Alexey Samsonov034e57a2012-08-27 07:17:47 +000037 Offset = *offset_ptr;
38 while (true) {
George Rimar6957ab52017-08-15 12:32:54 +000039 RangeListEntry Entry;
40 Entry.SectionIndex = -1ULL;
41
Alexey Samsonov034e57a2012-08-27 07:17:47 +000042 uint32_t prev_offset = *offset_ptr;
George Rimar6957ab52017-08-15 12:32:54 +000043 Entry.StartAddress = data.getRelocatedAddress(offset_ptr);
44 Entry.EndAddress =
45 data.getRelocatedAddress(offset_ptr, &Entry.SectionIndex);
George Rimarca532112017-04-24 10:19:45 +000046
Alexey Samsonov034e57a2012-08-27 07:17:47 +000047 // Check that both values were extracted correctly.
48 if (*offset_ptr != prev_offset + 2 * AddressSize) {
49 clear();
Victor Leschukcba595d2018-08-20 09:59:08 +000050 return createStringError(errc::invalid_argument,
51 "invalid range list entry at offset 0x%" PRIx32,
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +000052 prev_offset);
Alexey Samsonov034e57a2012-08-27 07:17:47 +000053 }
George Rimar6957ab52017-08-15 12:32:54 +000054 if (Entry.isEndOfListEntry())
Alexey Samsonov034e57a2012-08-27 07:17:47 +000055 break;
George Rimar6957ab52017-08-15 12:32:54 +000056 Entries.push_back(Entry);
Alexey Samsonov034e57a2012-08-27 07:17:47 +000057 }
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +000058 return Error::success();
Alexey Samsonov034e57a2012-08-27 07:17:47 +000059}
60
61void DWARFDebugRangeList::dump(raw_ostream &OS) const {
Alexey Samsonov1eabf982014-03-13 07:52:54 +000062 for (const RangeListEntry &RLE : Entries) {
NAKAMURA Takumi37338a82012-08-27 10:10:10 +000063 const char *format_str = (AddressSize == 4
Marshall Clowef271cc2012-08-27 22:53:35 +000064 ? "%08x %08" PRIx64 " %08" PRIx64 "\n"
65 : "%08x %016" PRIx64 " %016" PRIx64 "\n");
Alexey Samsonov1eabf982014-03-13 07:52:54 +000066 OS << format(format_str, Offset, RLE.StartAddress, RLE.EndAddress);
Alexey Samsonov034e57a2012-08-27 07:17:47 +000067 }
68 OS << format("%08x <End of list>\n", Offset);
69}
Alexey Samsonovc942e6b2012-09-04 08:12:33 +000070
George Rimar2f95c8b2017-09-04 10:30:39 +000071DWARFAddressRangesVector DWARFDebugRangeList::getAbsoluteRanges(
72 llvm::Optional<BaseAddress> BaseAddr) const {
Alexey Samsonov762343d2014-04-18 17:25:46 +000073 DWARFAddressRangesVector Res;
Alexey Samsonov1eabf982014-03-13 07:52:54 +000074 for (const RangeListEntry &RLE : Entries) {
Alexey Samsonov762343d2014-04-18 17:25:46 +000075 if (RLE.isBaseAddressSelectionEntry(AddressSize)) {
George Rimar2f95c8b2017-09-04 10:30:39 +000076 BaseAddr = {RLE.EndAddress, RLE.SectionIndex};
77 continue;
Alexey Samsonov762343d2014-04-18 17:25:46 +000078 }
George Rimar2f95c8b2017-09-04 10:30:39 +000079
80 DWARFAddressRange E;
81 E.LowPC = RLE.StartAddress;
82 E.HighPC = RLE.EndAddress;
83 E.SectionIndex = RLE.SectionIndex;
84 // Base address of a range list entry is determined by the closest preceding
85 // base address selection entry in the same range list. It defaults to the
86 // base address of the compilation unit if there is no such entry.
87 if (BaseAddr) {
88 E.LowPC += BaseAddr->Address;
89 E.HighPC += BaseAddr->Address;
90 if (E.SectionIndex == -1ULL)
91 E.SectionIndex = BaseAddr->SectionIndex;
92 }
93 Res.push_back(E);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +000094 }
Alexey Samsonov762343d2014-04-18 17:25:46 +000095 return Res;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +000096}