forked from xoofx/LibObjectFile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDwarfUnit.cs
More file actions
240 lines (183 loc) · 6.91 KB
/
Copy pathDwarfUnit.cs
File metadata and controls
240 lines (183 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
using System;
using System.Diagnostics;
using LibObjectFile.Diagnostics;
namespace LibObjectFile.Dwarf;
/// <summary>
/// Base class for a Dwarf Unit.
/// </summary>
public abstract class DwarfUnit : DwarfContainer
{
private ObjectFileElementHolder<DwarfDIE> _root;
/// <summary>
/// Gets or sets the encoding of this unit.
/// </summary>
public bool Is64BitEncoding { get; set; }
/// <summary>
/// Gets or sets the address size used by this unit.
/// </summary>
public DwarfAddressSize AddressSize { get; set; }
/// <summary>
/// Gets or sets the version of this unit.
/// </summary>
public ushort Version { get; set; }
/// <summary>
/// Gets or sets the kind of this unit.
/// </summary>
public DwarfUnitKindEx Kind { get; set; }
/// <summary>
/// Gets the abbreviation offset, only valid once the layout has been calculated through <see cref="DwarfFile.UpdateLayout"/>.
/// </summary>
public ulong DebugAbbreviationOffset { get; internal set; }
/// <summary>
/// Gets the unit length, only valid once the layout has been calculated through <see cref="DwarfFile.UpdateLayout"/>.
/// </summary>
public ulong UnitLength { get; internal set; }
/// <summary>
/// Gets or sets the root <see cref="DwarfDIE"/> of this compilation unit.
/// </summary>
public DwarfDIE? Root
{
get => _root;
set => _root.Set(this, value);
}
/// <summary>
/// Gets the abbreviation associated with the <see cref="Root"/> <see cref="DwarfDIE"/>.
/// </summary>
/// <remarks>
/// This abbreviation is automatically setup after reading or after updating the layout through <see cref="DwarfFile.UpdateLayout"/>.
/// </remarks>
public DwarfAbbreviation? Abbreviation { get; internal set; }
public override void Verify(DwarfVerifyContext context)
{
var diagnostics = context.Diagnostics;
if (Version < 2 || Version > 5)
{
diagnostics.Error(DiagnosticId.DWARF_ERR_VersionNotSupported, $"Version .debug_info {Version} not supported");
}
if (AddressSize == DwarfAddressSize.None)
{
diagnostics.Error(DiagnosticId.DWARF_ERR_InvalidAddressSize, $"Address size for .debug_info cannot be None/0");
}
Root?.Verify(context);
}
protected override void ValidateParent(ObjectElement parent)
{
if (!(parent is DwarfSection))
{
throw new ArgumentException($"Parent must inherit from type {nameof(DwarfSection)}");
}
}
protected override void UpdateLayoutCore(DwarfLayoutContext context)
{
var offset = this.Position;
// 1. unit_length
offset += DwarfHelper.SizeOfUnitLength(Is64BitEncoding);
var offsetAfterUnitLength = offset;
// 2. version (uhalf)
offset += sizeof(ushort); // WriteU16(unit.Version);
if (Version >= 5)
{
// 3. unit_type (ubyte)
offset += 1; // WriteU8(unit.Kind.Value);
}
// Update the layout specific to the Unit instance
offset += GetLayoutHeaderSize();
Abbreviation = null;
// Compute the full layout of all DIE and attributes (once abbreviation are calculated)
if (Root != null)
{
// Before updating the layout, we need to compute the abbreviation
Abbreviation = new DwarfAbbreviation();
context.File.AbbreviationTable.Abbreviations.Add(Abbreviation);
Root.UpdateAbbreviationItem(context);
DebugAbbreviationOffset = Abbreviation.Position;
Root.Position = offset;
Root.UpdateLayout(context);
offset += Root.Size;
}
Size = offset - Position;
UnitLength = offset - offsetAfterUnitLength;
}
protected abstract ulong GetLayoutHeaderSize();
protected abstract void ReadHeader(DwarfReader reader);
protected abstract void WriteHeader(DwarfWriter writer);
public override void Read(DwarfReader reader)
{
reader.CurrentUnit = this;
foreach (var abbreviation in reader.File.AbbreviationTable.Abbreviations)
{
if (abbreviation.Position == DebugAbbreviationOffset)
{
Abbreviation = abbreviation;
break;
}
}
Root = DwarfDIE.ReadInstance(reader);
reader.ResolveAttributeReferenceWithinCompilationUnit();
Size = reader.Position - Position;
}
internal static DwarfUnit? ReadInstance(DwarfReader reader, out ulong offsetEndOfUnit)
{
var startOffset = reader.Position;
DwarfUnit? unit = null;
// 1. unit_length
var unit_length = reader.ReadUnitLength();
offsetEndOfUnit = (ulong)reader.Position + unit_length;
// 2. version (uhalf)
var version = reader.ReadU16();
DwarfUnitKindEx unitKind = reader.DefaultUnitKind;
if (version < 2 || version > 5)
{
reader.Diagnostics.Error(DiagnosticId.DWARF_ERR_VersionNotSupported, $"Version {version} is not supported");
return null;
}
if (version >= 5)
{
// 3. unit_type (ubyte)
unitKind = new DwarfUnitKindEx(reader.ReadU8());
}
switch (unitKind.Value)
{
case DwarfUnitKind.Compile:
case DwarfUnitKind.Partial:
unit = new DwarfCompilationUnit();
break;
default:
reader.Diagnostics.Error(DiagnosticId.DWARF_ERR_UnsupportedUnitType, $"Unit Type {unitKind} is not supported");
return null;
}
unit.UnitLength = unit_length;
unit.Kind = unitKind;
unit.Is64BitEncoding = reader.Is64BitEncoding;
unit.Position = startOffset;
unit.Version = version;
unit.ReadHeader(reader);
unit.Read(reader);
return unit;
}
public override void Write(DwarfWriter writer)
{
var startOffset = writer.Position;
Debug.Assert(Position == writer.Position);
// 1. unit_length
Is64BitEncoding = Is64BitEncoding;
writer.WriteUnitLength(UnitLength);
var offsetAfterUnitLength = writer.Position;
// 2. version (uhalf)
writer.WriteU16(Version);
if (Version >= 5)
{
// 3. unit_type (ubyte)
writer.WriteU8((byte)Kind.Value);
}
WriteHeader(writer);
writer.AddressSize = AddressSize;
Root?.Write(writer);
// TODO: check size of unit length
Debug.Assert(Size == writer.Position - startOffset);
Debug.Assert(UnitLength == writer.Position - offsetAfterUnitLength);
}
}