1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Constants
16
17package dwarf
18
19import "strconv"
20
21// An Attr identifies the attribute type in a DWARF Entry's Field.
22type Attr uint32
23
24const (
25	AttrSibling        Attr = 0x01
26	AttrLocation       Attr = 0x02
27	AttrName           Attr = 0x03
28	AttrOrdering       Attr = 0x09
29	AttrByteSize       Attr = 0x0B
30	AttrBitOffset      Attr = 0x0C
31	AttrBitSize        Attr = 0x0D
32	AttrStmtList       Attr = 0x10
33	AttrLowpc          Attr = 0x11
34	AttrHighpc         Attr = 0x12
35	AttrLanguage       Attr = 0x13
36	AttrDiscr          Attr = 0x15
37	AttrDiscrValue     Attr = 0x16
38	AttrVisibility     Attr = 0x17
39	AttrImport         Attr = 0x18
40	AttrStringLength   Attr = 0x19
41	AttrCommonRef      Attr = 0x1A
42	AttrCompDir        Attr = 0x1B
43	AttrConstValue     Attr = 0x1C
44	AttrContainingType Attr = 0x1D
45	AttrDefaultValue   Attr = 0x1E
46	AttrInline         Attr = 0x20
47	AttrIsOptional     Attr = 0x21
48	AttrLowerBound     Attr = 0x22
49	AttrProducer       Attr = 0x25
50	AttrPrototyped     Attr = 0x27
51	AttrReturnAddr     Attr = 0x2A
52	AttrStartScope     Attr = 0x2C
53	AttrStrideSize     Attr = 0x2E
54	AttrUpperBound     Attr = 0x2F
55	AttrAbstractOrigin Attr = 0x31
56	AttrAccessibility  Attr = 0x32
57	AttrAddrClass      Attr = 0x33
58	AttrArtificial     Attr = 0x34
59	AttrBaseTypes      Attr = 0x35
60	AttrCalling        Attr = 0x36
61	AttrCount          Attr = 0x37
62	AttrDataMemberLoc  Attr = 0x38
63	AttrDeclColumn     Attr = 0x39
64	AttrDeclFile       Attr = 0x3A
65	AttrDeclLine       Attr = 0x3B
66	AttrDeclaration    Attr = 0x3C
67	AttrDiscrList      Attr = 0x3D
68	AttrEncoding       Attr = 0x3E
69	AttrExternal       Attr = 0x3F
70	AttrFrameBase      Attr = 0x40
71	AttrFriend         Attr = 0x41
72	AttrIdentifierCase Attr = 0x42
73	AttrMacroInfo      Attr = 0x43
74	AttrNamelistItem   Attr = 0x44
75	AttrPriority       Attr = 0x45
76	AttrSegment        Attr = 0x46
77	AttrSpecification  Attr = 0x47
78	AttrStaticLink     Attr = 0x48
79	AttrType           Attr = 0x49
80	AttrUseLocation    Attr = 0x4A
81	AttrVarParam       Attr = 0x4B
82	AttrVirtuality     Attr = 0x4C
83	AttrVtableElemLoc  Attr = 0x4D
84	AttrAllocated      Attr = 0x4E
85	AttrAssociated     Attr = 0x4F
86	AttrDataLocation   Attr = 0x50
87	AttrStride         Attr = 0x51
88	AttrEntrypc        Attr = 0x52
89	AttrUseUTF8        Attr = 0x53
90	AttrExtension      Attr = 0x54
91	AttrRanges         Attr = 0x55
92	AttrTrampoline     Attr = 0x56
93	AttrCallColumn     Attr = 0x57
94	AttrCallFile       Attr = 0x58
95	AttrCallLine       Attr = 0x59
96	AttrDescription    Attr = 0x5A
97
98	// Go-specific attributes.
99	AttrGoKind          Attr = 0x2900
100	AttrGoKey           Attr = 0x2901
101	AttrGoElem          Attr = 0x2902
102	AttrGoEmbeddedField Attr = 0x2903
103)
104
105var attrNames = [...]string{
106	AttrSibling:        "Sibling",
107	AttrLocation:       "Location",
108	AttrName:           "Name",
109	AttrOrdering:       "Ordering",
110	AttrByteSize:       "ByteSize",
111	AttrBitOffset:      "BitOffset",
112	AttrBitSize:        "BitSize",
113	AttrStmtList:       "StmtList",
114	AttrLowpc:          "Lowpc",
115	AttrHighpc:         "Highpc",
116	AttrLanguage:       "Language",
117	AttrDiscr:          "Discr",
118	AttrDiscrValue:     "DiscrValue",
119	AttrVisibility:     "Visibility",
120	AttrImport:         "Import",
121	AttrStringLength:   "StringLength",
122	AttrCommonRef:      "CommonRef",
123	AttrCompDir:        "CompDir",
124	AttrConstValue:     "ConstValue",
125	AttrContainingType: "ContainingType",
126	AttrDefaultValue:   "DefaultValue",
127	AttrInline:         "Inline",
128	AttrIsOptional:     "IsOptional",
129	AttrLowerBound:     "LowerBound",
130	AttrProducer:       "Producer",
131	AttrPrototyped:     "Prototyped",
132	AttrReturnAddr:     "ReturnAddr",
133	AttrStartScope:     "StartScope",
134	AttrStrideSize:     "StrideSize",
135	AttrUpperBound:     "UpperBound",
136	AttrAbstractOrigin: "AbstractOrigin",
137	AttrAccessibility:  "Accessibility",
138	AttrAddrClass:      "AddrClass",
139	AttrArtificial:     "Artificial",
140	AttrBaseTypes:      "BaseTypes",
141	AttrCalling:        "Calling",
142	AttrCount:          "Count",
143	AttrDataMemberLoc:  "DataMemberLoc",
144	AttrDeclColumn:     "DeclColumn",
145	AttrDeclFile:       "DeclFile",
146	AttrDeclLine:       "DeclLine",
147	AttrDeclaration:    "Declaration",
148	AttrDiscrList:      "DiscrList",
149	AttrEncoding:       "Encoding",
150	AttrExternal:       "External",
151	AttrFrameBase:      "FrameBase",
152	AttrFriend:         "Friend",
153	AttrIdentifierCase: "IdentifierCase",
154	AttrMacroInfo:      "MacroInfo",
155	AttrNamelistItem:   "NamelistItem",
156	AttrPriority:       "Priority",
157	AttrSegment:        "Segment",
158	AttrSpecification:  "Specification",
159	AttrStaticLink:     "StaticLink",
160	AttrType:           "Type",
161	AttrUseLocation:    "UseLocation",
162	AttrVarParam:       "VarParam",
163	AttrVirtuality:     "Virtuality",
164	AttrVtableElemLoc:  "VtableElemLoc",
165	AttrAllocated:      "Allocated",
166	AttrAssociated:     "Associated",
167	AttrDataLocation:   "DataLocation",
168	AttrStride:         "Stride",
169	AttrEntrypc:        "Entrypc",
170	AttrUseUTF8:        "UseUTF8",
171	AttrExtension:      "Extension",
172	AttrRanges:         "Ranges",
173	AttrTrampoline:     "Trampoline",
174	AttrCallColumn:     "CallColumn",
175	AttrCallFile:       "CallFile",
176	AttrCallLine:       "CallLine",
177	AttrDescription:    "Description",
178}
179
180func (a Attr) String() string {
181	if int(a) < len(attrNames) {
182		s := attrNames[a]
183		if s != "" {
184			return s
185		}
186	}
187	switch a {
188	case AttrGoKind:
189		return "GoKind"
190	case AttrGoKey:
191		return "GoKey"
192	case AttrGoElem:
193		return "GoElem"
194	case AttrGoEmbeddedField:
195		return "GoEmbeddedField"
196	}
197	return strconv.Itoa(int(a))
198}
199
200func (a Attr) GoString() string {
201	if int(a) < len(attrNames) {
202		s := attrNames[a]
203		if s != "" {
204			return "dwarf.Attr" + s
205		}
206	}
207	return "dwarf.Attr(" + strconv.FormatInt(int64(a), 10) + ")"
208}
209
210// A format is a DWARF data encoding format.
211type format uint32
212
213const (
214	// value formats
215	formAddr        format = 0x01
216	formDwarfBlock2 format = 0x03
217	formDwarfBlock4 format = 0x04
218	formData2       format = 0x05
219	formData4       format = 0x06
220	formData8       format = 0x07
221	formString      format = 0x08
222	formDwarfBlock  format = 0x09
223	formDwarfBlock1 format = 0x0A
224	formData1       format = 0x0B
225	formFlag        format = 0x0C
226	formSdata       format = 0x0D
227	formStrp        format = 0x0E
228	formUdata       format = 0x0F
229	formRefAddr     format = 0x10
230	formRef1        format = 0x11
231	formRef2        format = 0x12
232	formRef4        format = 0x13
233	formRef8        format = 0x14
234	formRefUdata    format = 0x15
235	formIndirect    format = 0x16
236	// The following are new in DWARF 4.
237	formSecOffset   format = 0x17
238	formExprloc     format = 0x18
239	formFlagPresent format = 0x19
240	formRefSig8     format = 0x20
241	// Extensions for multi-file compression (.dwz)
242	// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
243	formGnuRefAlt  format = 0x1f20
244	formGnuStrpAlt format = 0x1f21
245)
246
247// A Tag is the classification (the type) of an Entry.
248type Tag uint32
249
250const (
251	TagArrayType              Tag = 0x01
252	TagClassType              Tag = 0x02
253	TagEntryPoint             Tag = 0x03
254	TagEnumerationType        Tag = 0x04
255	TagFormalParameter        Tag = 0x05
256	TagImportedDeclaration    Tag = 0x08
257	TagLabel                  Tag = 0x0A
258	TagLexDwarfBlock          Tag = 0x0B
259	TagMember                 Tag = 0x0D
260	TagPointerType            Tag = 0x0F
261	TagReferenceType          Tag = 0x10
262	TagCompileUnit            Tag = 0x11
263	TagStringType             Tag = 0x12
264	TagStructType             Tag = 0x13
265	TagSubroutineType         Tag = 0x15
266	TagTypedef                Tag = 0x16
267	TagUnionType              Tag = 0x17
268	TagUnspecifiedParameters  Tag = 0x18
269	TagVariant                Tag = 0x19
270	TagCommonDwarfBlock       Tag = 0x1A
271	TagCommonInclusion        Tag = 0x1B
272	TagInheritance            Tag = 0x1C
273	TagInlinedSubroutine      Tag = 0x1D
274	TagModule                 Tag = 0x1E
275	TagPtrToMemberType        Tag = 0x1F
276	TagSetType                Tag = 0x20
277	TagSubrangeType           Tag = 0x21
278	TagWithStmt               Tag = 0x22
279	TagAccessDeclaration      Tag = 0x23
280	TagBaseType               Tag = 0x24
281	TagCatchDwarfBlock        Tag = 0x25
282	TagConstType              Tag = 0x26
283	TagConstant               Tag = 0x27
284	TagEnumerator             Tag = 0x28
285	TagFileType               Tag = 0x29
286	TagFriend                 Tag = 0x2A
287	TagNamelist               Tag = 0x2B
288	TagNamelistItem           Tag = 0x2C
289	TagPackedType             Tag = 0x2D
290	TagSubprogram             Tag = 0x2E
291	TagTemplateTypeParameter  Tag = 0x2F
292	TagTemplateValueParameter Tag = 0x30
293	TagThrownType             Tag = 0x31
294	TagTryDwarfBlock          Tag = 0x32
295	TagVariantPart            Tag = 0x33
296	TagVariable               Tag = 0x34
297	TagVolatileType           Tag = 0x35
298	// The following are new in DWARF 3.
299	TagDwarfProcedure  Tag = 0x36
300	TagRestrictType    Tag = 0x37
301	TagInterfaceType   Tag = 0x38
302	TagNamespace       Tag = 0x39
303	TagImportedModule  Tag = 0x3A
304	TagUnspecifiedType Tag = 0x3B
305	TagPartialUnit     Tag = 0x3C
306	TagImportedUnit    Tag = 0x3D
307	TagMutableType     Tag = 0x3E // Later removed from DWARF.
308	TagCondition       Tag = 0x3F
309	TagSharedType      Tag = 0x40
310	// The following are new in DWARF 4.
311	TagTypeUnit            Tag = 0x41
312	TagRvalueReferenceType Tag = 0x42
313	TagTemplateAlias       Tag = 0x43
314)
315
316var tagNames = [...]string{
317	TagArrayType:              "ArrayType",
318	TagClassType:              "ClassType",
319	TagEntryPoint:             "EntryPoint",
320	TagEnumerationType:        "EnumerationType",
321	TagFormalParameter:        "FormalParameter",
322	TagImportedDeclaration:    "ImportedDeclaration",
323	TagLabel:                  "Label",
324	TagLexDwarfBlock:          "LexDwarfBlock",
325	TagMember:                 "Member",
326	TagPointerType:            "PointerType",
327	TagReferenceType:          "ReferenceType",
328	TagCompileUnit:            "CompileUnit",
329	TagStringType:             "StringType",
330	TagStructType:             "StructType",
331	TagSubroutineType:         "SubroutineType",
332	TagTypedef:                "Typedef",
333	TagUnionType:              "UnionType",
334	TagUnspecifiedParameters:  "UnspecifiedParameters",
335	TagVariant:                "Variant",
336	TagCommonDwarfBlock:       "CommonDwarfBlock",
337	TagCommonInclusion:        "CommonInclusion",
338	TagInheritance:            "Inheritance",
339	TagInlinedSubroutine:      "InlinedSubroutine",
340	TagModule:                 "Module",
341	TagPtrToMemberType:        "PtrToMemberType",
342	TagSetType:                "SetType",
343	TagSubrangeType:           "SubrangeType",
344	TagWithStmt:               "WithStmt",
345	TagAccessDeclaration:      "AccessDeclaration",
346	TagBaseType:               "BaseType",
347	TagCatchDwarfBlock:        "CatchDwarfBlock",
348	TagConstType:              "ConstType",
349	TagConstant:               "Constant",
350	TagEnumerator:             "Enumerator",
351	TagFileType:               "FileType",
352	TagFriend:                 "Friend",
353	TagNamelist:               "Namelist",
354	TagNamelistItem:           "NamelistItem",
355	TagPackedType:             "PackedType",
356	TagSubprogram:             "Subprogram",
357	TagTemplateTypeParameter:  "TemplateTypeParameter",
358	TagTemplateValueParameter: "TemplateValueParameter",
359	TagThrownType:             "ThrownType",
360	TagTryDwarfBlock:          "TryDwarfBlock",
361	TagVariantPart:            "VariantPart",
362	TagVariable:               "Variable",
363	TagVolatileType:           "VolatileType",
364	TagDwarfProcedure:         "DwarfProcedure",
365	TagRestrictType:           "RestrictType",
366	TagInterfaceType:          "InterfaceType",
367	TagNamespace:              "Namespace",
368	TagImportedModule:         "ImportedModule",
369	TagUnspecifiedType:        "UnspecifiedType",
370	TagPartialUnit:            "PartialUnit",
371	TagImportedUnit:           "ImportedUnit",
372	TagMutableType:            "MutableType",
373	TagCondition:              "Condition",
374	TagSharedType:             "SharedType",
375	TagTypeUnit:               "TypeUnit",
376	TagRvalueReferenceType:    "RvalueReferenceType",
377	TagTemplateAlias:          "TemplateAlias",
378}
379
380func (t Tag) String() string {
381	if int(t) < len(tagNames) {
382		s := tagNames[t]
383		if s != "" {
384			return s
385		}
386	}
387	return strconv.Itoa(int(t))
388}
389
390func (t Tag) GoString() string {
391	if int(t) < len(tagNames) {
392		s := tagNames[t]
393		if s != "" {
394			return "dwarf.Tag" + s
395		}
396	}
397	return "dwarf.Tag(" + strconv.FormatInt(int64(t), 10) + ")"
398}
399
400// Location expression operators.
401// The debug info encodes value locations like 8(R3)
402// as a sequence of these op codes.
403// This package does not implement full expressions;
404// the opPlusUconst operator is expected by the type parser.
405const (
406	opAddr       = 0x03 /* 1 op, const addr */
407	opDeref      = 0x06
408	opConst1u    = 0x08 /* 1 op, 1 byte const */
409	opConst1s    = 0x09 /*	" signed */
410	opConst2u    = 0x0A /* 1 op, 2 byte const  */
411	opConst2s    = 0x0B /*	" signed */
412	opConst4u    = 0x0C /* 1 op, 4 byte const */
413	opConst4s    = 0x0D /*	" signed */
414	opConst8u    = 0x0E /* 1 op, 8 byte const */
415	opConst8s    = 0x0F /*	" signed */
416	opConstu     = 0x10 /* 1 op, LEB128 const */
417	opConsts     = 0x11 /*	" signed */
418	opDup        = 0x12
419	opDrop       = 0x13
420	opOver       = 0x14
421	opPick       = 0x15 /* 1 op, 1 byte stack index */
422	opSwap       = 0x16
423	opRot        = 0x17
424	opXderef     = 0x18
425	opAbs        = 0x19
426	opAnd        = 0x1A
427	opDiv        = 0x1B
428	opMinus      = 0x1C
429	opMod        = 0x1D
430	opMul        = 0x1E
431	opNeg        = 0x1F
432	opNot        = 0x20
433	opOr         = 0x21
434	opPlus       = 0x22
435	opPlusUconst = 0x23 /* 1 op, ULEB128 addend */
436	opShl        = 0x24
437	opShr        = 0x25
438	opShra       = 0x26
439	opXor        = 0x27
440	opSkip       = 0x2F /* 1 op, signed 2-byte constant */
441	opBra        = 0x28 /* 1 op, signed 2-byte constant */
442	opEq         = 0x29
443	opGe         = 0x2A
444	opGt         = 0x2B
445	opLe         = 0x2C
446	opLt         = 0x2D
447	opNe         = 0x2E
448	opLit0       = 0x30
449	/* OpLitN = OpLit0 + N for N = 0..31 */
450	opReg0 = 0x50
451	/* OpRegN = OpReg0 + N for N = 0..31 */
452	opBreg0 = 0x70 /* 1 op, signed LEB128 constant */
453	/* OpBregN = OpBreg0 + N for N = 0..31 */
454	opRegx       = 0x90 /* 1 op, ULEB128 register */
455	opFbreg      = 0x91 /* 1 op, SLEB128 offset */
456	opBregx      = 0x92 /* 2 op, ULEB128 reg; SLEB128 off */
457	opPiece      = 0x93 /* 1 op, ULEB128 size of piece */
458	opDerefSize  = 0x94 /* 1-byte size of data retrieved */
459	opXderefSize = 0x95 /* 1-byte size of data retrieved */
460	opNop        = 0x96
461	/* next four new in Dwarf v3 */
462	opPushObjAddr = 0x97
463	opCall2       = 0x98 /* 2-byte offset of DIE */
464	opCall4       = 0x99 /* 4-byte offset of DIE */
465	opCallRef     = 0x9A /* 4- or 8- byte offset of DIE */
466	/* 0xE0-0xFF reserved for user-specific */
467)
468
469// Basic type encodings -- the value for AttrEncoding in a TagBaseType Entry.
470const (
471	encAddress        = 0x01
472	encBoolean        = 0x02
473	encComplexFloat   = 0x03
474	encFloat          = 0x04
475	encSigned         = 0x05
476	encSignedChar     = 0x06
477	encUnsigned       = 0x07
478	encUnsignedChar   = 0x08
479	encImaginaryFloat = 0x09
480)
481