1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef ABBREVIATION_TABLE_H
6#define ABBREVIATION_TABLE_H
7
8#include <util/DoublyLinkedList.h>
9#include <util/OpenHashTable.h>
10
11#include "DataReader.h"
12#include "Dwarf.h"
13
14
15struct AbbreviationTableEntry {
16	uint32					code;
17	off_t					offset;
18	off_t					size;
19	AbbreviationTableEntry*	next;
20
21	AbbreviationTableEntry(uint32 code, off_t offset, off_t size)
22		:
23		code(code),
24		offset(offset),
25		size(size)
26	{
27	}
28};
29
30
31struct AbbreviationEntry {
32	AbbreviationEntry()
33	{
34	}
35
36	AbbreviationEntry(uint32 code, const void* data, off_t size)
37	{
38		SetTo(code, data, size);
39	}
40
41	void SetTo(uint32 code, const void* data, off_t size)
42	{
43		fCode = code;
44		fAttributesReader.SetTo(data, size, 4);
45			// address size doesn't matter here
46		fTag = fAttributesReader.ReadUnsignedLEB128(0);
47		fHasChildren = fAttributesReader.Read<uint8>(0);
48		fData = fAttributesReader.Data();
49		fSize = fAttributesReader.BytesRemaining();
50	}
51
52	uint32	Code() const		{ return fCode; }
53	uint32	Tag() const			{ return fTag; }
54	bool	HasChildren() const	{ return fHasChildren == DW_CHILDREN_yes; }
55
56	bool GetNextAttribute(uint32& name, uint32& form)
57	{
58		name = fAttributesReader.ReadUnsignedLEB128(0);
59		form = fAttributesReader.ReadUnsignedLEB128(0);
60		return !fAttributesReader.HasOverflow() && (name != 0 || form != 0);
61	}
62
63private:
64	uint32		fCode;
65	const void*	fData;
66	off_t		fSize;
67	uint32		fTag;
68	uint8		fHasChildren;
69	DataReader	fAttributesReader;
70};
71
72
73struct AbbreviationTableHashDefinition {
74	typedef uint32					KeyType;
75	typedef	AbbreviationTableEntry	ValueType;
76
77	size_t HashKey(uint32 key) const
78	{
79		return (size_t)key;
80	}
81
82	size_t Hash(AbbreviationTableEntry* value) const
83	{
84		return HashKey(value->code);
85	}
86
87	bool Compare(uint32 key, AbbreviationTableEntry* value) const
88	{
89		return value->code == key;
90	}
91
92	AbbreviationTableEntry*& GetLink(AbbreviationTableEntry* value) const
93	{
94		return value->next;
95	}
96};
97
98
99class AbbreviationTable : public DoublyLinkedListLinkImpl<AbbreviationTable> {
100public:
101								AbbreviationTable(off_t offset);
102								~AbbreviationTable();
103
104			status_t			Init(const void* section, off_t sectionSize);
105
106			off_t				Offset() const	{ return fOffset; }
107
108			bool				GetAbbreviationEntry(uint32 code,
109									AbbreviationEntry& entry);
110
111private:
112			typedef BOpenHashTable<AbbreviationTableHashDefinition> EntryTable;
113
114private:
115			status_t			_ParseAbbreviationEntry(
116									DataReader& abbrevReader, bool& _nullEntry);
117
118private:
119			off_t				fOffset;
120			const uint8*		fData;
121			off_t				fSize;
122			EntryTable			fEntryTable;
123};
124
125
126#endif	// ABBREVIATION_TABLE_H
127