LLVM  14.0.0git
DebugLinesSubsection.cpp
Go to the documentation of this file.
1 //===- DebugLinesSubsection.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/Support/Error.h"
17 #include <cassert>
18 #include <cstdint>
19 
20 using namespace llvm;
21 using namespace llvm::codeview;
22 
24  LineColumnEntry &Item) {
25  const LineBlockFragmentHeader *BlockHeader;
26  BinaryStreamReader Reader(Stream);
27  if (auto EC = Reader.readObject(BlockHeader))
28  return EC;
29  bool HasColumn = Header->Flags & uint16_t(LF_HaveColumns);
30  uint32_t LineInfoSize =
31  BlockHeader->NumLines *
32  (sizeof(LineNumberEntry) + (HasColumn ? sizeof(ColumnNumberEntry) : 0));
33  if (BlockHeader->BlockSize < sizeof(LineBlockFragmentHeader))
34  return make_error<CodeViewError>(cv_error_code::corrupt_record,
35  "Invalid line block record size");
36  uint32_t Size = BlockHeader->BlockSize - sizeof(LineBlockFragmentHeader);
37  if (LineInfoSize > Size)
38  return make_error<CodeViewError>(cv_error_code::corrupt_record,
39  "Invalid line block record size");
40  // The value recorded in BlockHeader->BlockSize includes the size of
41  // LineBlockFragmentHeader.
42  Len = BlockHeader->BlockSize;
43  Item.NameIndex = BlockHeader->NameIndex;
44  if (auto EC = Reader.readArray(Item.LineNumbers, BlockHeader->NumLines))
45  return EC;
46  if (HasColumn) {
47  if (auto EC = Reader.readArray(Item.Columns, BlockHeader->NumLines))
48  return EC;
49  }
50  return Error::success();
51 }
52 
55 
57  if (auto EC = Reader.readObject(Header))
58  return EC;
59 
60  LinesAndColumns.getExtractor().Header = Header;
61  if (auto EC = Reader.readArray(LinesAndColumns, Reader.bytesRemaining()))
62  return EC;
63 
64  return Error::success();
65 }
66 
68  return !!(Header->Flags & LF_HaveColumns);
69 }
70 
73  : DebugSubsection(DebugSubsectionKind::Lines), Checksums(Checksums) {}
74 
76  uint32_t Offset = Checksums.mapChecksumOffset(FileName);
77 
78  Blocks.emplace_back(Offset);
79 }
80 
82  Block &B = Blocks.back();
83  LineNumberEntry LNE;
84  LNE.Flags = Line.getRawData();
85  LNE.Offset = Offset;
86  B.Lines.push_back(LNE);
87 }
88 
90  const LineInfo &Line,
91  uint32_t ColStart,
92  uint32_t ColEnd) {
93  Block &B = Blocks.back();
94  assert(B.Lines.size() == B.Columns.size());
95 
98  CNE.StartColumn = ColStart;
99  CNE.EndColumn = ColEnd;
100  B.Columns.push_back(CNE);
101 }
102 
104  LineFragmentHeader Header;
105  Header.CodeSize = CodeSize;
106  Header.Flags = hasColumnInfo() ? LF_HaveColumns : 0;
107  Header.RelocOffset = RelocOffset;
108  Header.RelocSegment = RelocSegment;
109 
110  if (auto EC = Writer.writeObject(Header))
111  return EC;
112 
113  for (const auto &B : Blocks) {
114  LineBlockFragmentHeader BlockHeader;
115  assert(B.Lines.size() == B.Columns.size() || B.Columns.empty());
116 
117  BlockHeader.NumLines = B.Lines.size();
118  BlockHeader.BlockSize = sizeof(LineBlockFragmentHeader);
119  BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(LineNumberEntry);
120  if (hasColumnInfo())
121  BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(ColumnNumberEntry);
122  BlockHeader.NameIndex = B.ChecksumBufferOffset;
123  if (auto EC = Writer.writeObject(BlockHeader))
124  return EC;
125 
126  if (auto EC = Writer.writeArray(makeArrayRef(B.Lines)))
127  return EC;
128 
129  if (hasColumnInfo()) {
130  if (auto EC = Writer.writeArray(makeArrayRef(B.Columns)))
131  return EC;
132  }
133  }
134  return Error::success();
135 }
136 
138  uint32_t Size = sizeof(LineFragmentHeader);
139  for (const auto &B : Blocks) {
140  Size += sizeof(LineBlockFragmentHeader);
141  Size += B.Lines.size() * sizeof(LineNumberEntry);
142  if (hasColumnInfo())
143  Size += B.Columns.size() * sizeof(ColumnNumberEntry);
144  }
145  return Size;
146 }
147 
149  uint32_t Offset) {
150  RelocOffset = Offset;
151  RelocSegment = Segment;
152 }
153 
155 
156 void DebugLinesSubsection::setFlags(LineFlags Flags) { this->Flags = Flags; }
157 
159  return Flags & LF_HaveColumns;
160 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
BinaryStreamReader.h
llvm::codeview::DebugLinesSubsection::createBlock
void createBlock(StringRef FileName)
Definition: DebugLinesSubsection.cpp:75
llvm::codeview::DebugLinesSubsection::commit
Error commit(BinaryStreamWriter &Writer) const override
Definition: DebugLinesSubsection.cpp:103
llvm::codeview::LineBlockFragmentHeader::NumLines
support::ulittle32_t NumLines
Definition: DebugLinesSubsection.h:44
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::codeview::LineColumnEntry::Columns
FixedStreamArray< ColumnNumberEntry > Columns
Definition: DebugLinesSubsection.h:67
llvm::codeview::LineBlockFragmentHeader
Definition: DebugLinesSubsection.h:39
llvm::codeview::DebugSubsectionKind::Lines
@ Lines
llvm::codeview::DebugChecksumsSubsection
Definition: DebugChecksumsSubsection.h:75
llvm::codeview::LineBlockFragmentHeader::BlockSize
support::ulittle32_t BlockSize
Definition: DebugLinesSubsection.h:45
llvm::codeview::DebugLinesSubsectionRef::initialize
Error initialize(BinaryStreamReader Reader)
Definition: DebugLinesSubsection.cpp:56
llvm::BinaryStreamWriter
Provides write only access to a subclass of WritableBinaryStream.
Definition: BinaryStreamWriter.h:31
llvm::codeview::Line
Definition: Line.h:91
llvm::codeview::LF_HaveColumns
@ LF_HaveColumns
Definition: CodeView.h:568
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
Error.h
llvm::codeview::LineNumberEntry::Flags
support::ulittle32_t Flags
Definition: DebugLinesSubsection.h:55
llvm::codeview::DebugLinesSubsectionRef::DebugLinesSubsectionRef
DebugLinesSubsectionRef()
Definition: DebugLinesSubsection.cpp:53
llvm::codeview::LineFragmentHeader::RelocSegment
support::ulittle16_t RelocSegment
Definition: DebugLinesSubsection.h:33
llvm::codeview::DebugStringTableSubsection
Represents a read-write view of a CodeView string table.
Definition: DebugStringTableSubsection.h:56
llvm::BinaryStreamReader::bytesRemaining
uint64_t bytesRemaining() const
Definition: BinaryStreamReader.h:257
llvm::codeview::ColumnNumberEntry
Definition: DebugLinesSubsection.h:59
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::BinaryStreamWriter::writeArray
Error writeArray(ArrayRef< T > Array)
Writes an array of objects of type T to the underlying stream, as if by using memcpy.
Definition: BinaryStreamWriter.h:151
llvm::codeview::DebugLinesSubsection::setFlags
void setFlags(LineFlags Flags)
Definition: DebugLinesSubsection.cpp:156
llvm::codeview::DebugLinesSubsection::hasColumnInfo
bool hasColumnInfo() const
Definition: DebugLinesSubsection.cpp:158
llvm::codeview::ColumnNumberEntry::StartColumn
support::ulittle16_t StartColumn
Definition: DebugLinesSubsection.h:60
llvm::codeview::LineColumnEntry
Definition: DebugLinesSubsection.h:64
DebugChecksumsSubsection.h
llvm::codeview::LineColumnExtractor::operator()
Error operator()(BinaryStreamRef Stream, uint32_t &Len, LineColumnEntry &Item)
Definition: DebugLinesSubsection.cpp:23
llvm::codeview::LineBlockFragmentHeader::NameIndex
support::ulittle32_t NameIndex
Definition: DebugLinesSubsection.h:40
llvm::codeview::LineColumnEntry::NameIndex
support::ulittle32_t NameIndex
Definition: DebugLinesSubsection.h:65
llvm::codeview::DebugSubsectionRef
Definition: DebugSubsection.h:19
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::codeview::LineNumberEntry
Definition: DebugLinesSubsection.h:53
CodeView.h
llvm::BinaryStreamReader
Provides read only access to a subclass of BinaryStream.
Definition: BinaryStreamReader.h:31
llvm::codeview::LineFragmentHeader
Definition: DebugLinesSubsection.h:31
llvm::codeview::DebugLinesSubsection::addLineInfo
void addLineInfo(uint32_t Offset, const LineInfo &Line)
Definition: DebugLinesSubsection.cpp:81
llvm::codeview::LineNumberEntry::Offset
support::ulittle32_t Offset
Definition: DebugLinesSubsection.h:54
DebugLinesSubsection.h
llvm::codeview::DebugChecksumsSubsection::mapChecksumOffset
uint32_t mapChecksumOffset(StringRef FileName) const
Definition: DebugChecksumsSubsection.cpp:110
ArrayRef.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::codeview::DebugLinesSubsection::addLineAndColumnInfo
void addLineAndColumnInfo(uint32_t Offset, const LineInfo &Line, uint32_t ColStart, uint32_t ColEnd)
Definition: DebugLinesSubsection.cpp:89
llvm::codeview::CompileSym2Flags::EC
@ EC
llvm::BinaryStreamReader::readObject
Error readObject(const T *&Dest)
Get a pointer to an object of type T from the underlying stream, as if by memcpy, and store the resul...
Definition: BinaryStreamReader.h:169
llvm::codeview::LineColumnExtractor::Header
const LineFragmentHeader * Header
Definition: DebugLinesSubsection.h:75
llvm::codeview::DebugLinesSubsection::setRelocationAddress
void setRelocationAddress(uint16_t Segment, uint32_t Offset)
Definition: DebugLinesSubsection.cpp:148
llvm::BinaryStreamWriter::writeObject
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
Definition: BinaryStreamWriter.h:135
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::codeview::LineColumnEntry::LineNumbers
FixedStreamArray< LineNumberEntry > LineNumbers
Definition: DebugLinesSubsection.h:66
uint32_t
llvm::codeview::ColumnNumberEntry::EndColumn
support::ulittle16_t EndColumn
Definition: DebugLinesSubsection.h:61
llvm::codeview::LineInfo
Definition: Line.h:21
llvm::codeview::DebugSubsection
Definition: DebugSubsection.h:32
uint16_t
llvm::codeview::LineFragmentHeader::RelocOffset
support::ulittle32_t RelocOffset
Definition: DebugLinesSubsection.h:32
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::codeview::cv_error_code::corrupt_record
@ corrupt_record
llvm::codeview::DebugLinesSubsectionRef::hasColumnInfo
bool hasColumnInfo() const
Definition: DebugLinesSubsection.cpp:67
llvm::codeview::DebugSubsectionKind
DebugSubsectionKind
Definition: CodeView.h:313
llvm::codeview
Definition: AppendingTypeTableBuilder.h:22
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:476
llvm::VarStreamArray::getExtractor
const Extractor & getExtractor() const
Definition: BinaryStreamArray.h:136
llvm::codeview::LineFragmentHeader::CodeSize
support::ulittle32_t CodeSize
Definition: DebugLinesSubsection.h:35
BinaryStreamWriter.h
llvm::codeview::LineFragmentHeader::Flags
support::ulittle16_t Flags
Definition: DebugLinesSubsection.h:34
CodeViewError.h
llvm::codeview::DebugLinesSubsection::calculateSerializedSize
uint32_t calculateSerializedSize() const override
Definition: DebugLinesSubsection.cpp:137
llvm::codeview::DebugLinesSubsection::DebugLinesSubsection
DebugLinesSubsection(DebugChecksumsSubsection &Checksums, DebugStringTableSubsection &Strings)
Definition: DebugLinesSubsection.cpp:71
llvm::codeview::LineFlags
LineFlags
Definition: CodeView.h:566
llvm::codeview::DebugLinesSubsection::setCodeSize
void setCodeSize(uint32_t Size)
Definition: DebugLinesSubsection.cpp:154
llvm::BinaryStreamReader::readArray
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
Definition: BinaryStreamReader.h:187
llvm::BinaryStreamRef
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Definition: BinaryStreamRef.h:156