-
Notifications
You must be signed in to change notification settings - Fork 17
/
HunkSegment.java
116 lines (95 loc) · 3 KB
/
HunkSegment.java
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
package hunk;
import java.util.ArrayList;
import java.util.List;
import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.ByteArrayProvider;
import ghidra.app.util.bin.ByteProvider;
public class HunkSegment {
private HunkSegmentBlock segBlock;
private HunkSymbolBlock symBlock;
private List<HunkDebugBlock> dbgBlocks;
private List<DebugInfo> dbgInfos;
private List<HunkRelocBlock> relocBlocks;
private HunkExtBlock extBlock;
private String name;
HunkSegment() {
segBlock = null;
symBlock = null;
dbgBlocks = null;
dbgInfos = null;
relocBlocks = null;
extBlock = null;
name = null;
}
HunkType getHunkType() {
return (segBlock != null) ? segBlock.getHunkType() : HunkType.HUNK_BAD_TYPE;
}
void setSizeLongs(int sizeLongs) {
if (segBlock != null) {
segBlock.setSizeLongs(sizeLongs);
}
}
int getSizeLongs() {
return (segBlock != null) ? segBlock.getSizeLongs() : 0;
}
HunkSegmentBlock getSegmentBlock() {
return segBlock;
}
HunkRelocBlock[] getRelocBlocks() {
return (relocBlocks == null) ? null : relocBlocks.toArray(HunkRelocBlock[]::new);
}
String getName() {
return name;
}
public final List<XDefinition> getDefinitions() {
return (extBlock == null) ? null : extBlock.getDefinitions();
}
public final List<XReference> getReferences() {
return (extBlock == null) ? null : extBlock.getReferences();
}
public void parse(List<HunkBlock> blocks) throws HunkParseError {
for (HunkBlock block : blocks) {
if (block.isValidLoadsegBeginHunk()) {
segBlock = (HunkSegmentBlock)block;
} else if (block.getHunkType() == HunkType.HUNK_NAME) {
name = ((HunkNameBlock)block).getName();
} else if (block.getHunkType() == HunkType.HUNK_SYMBOL) {
if (symBlock == null) {
symBlock = (HunkSymbolBlock)block;
} else {
throw new HunkParseError("Duplicate symbols in hunk");
}
} else if (block.getHunkType() == HunkType.HUNK_DEBUG) {
if (dbgBlocks == null) {
dbgBlocks = new ArrayList<>();
}
dbgBlocks.add((HunkDebugBlock)block);
ByteProvider provider = new ByteArrayProvider(((HunkDebugBlock)block).getData());
DebugInfo info = HunkDebug.decode(new BinaryReader(provider, false));
if (info != null) {
if (dbgInfos == null) {
dbgInfos = new ArrayList<>();
}
dbgInfos.add(info);
}
} else if (block.getHunkType() == HunkType.HUNK_ABSRELOC32 ||
block.getHunkType() == HunkType.HUNK_DREL32 ||
block.getHunkType() == HunkType.HUNK_DREL16 ||
block.getHunkType() == HunkType.HUNK_DREL8 ||
block.getHunkType() == HunkType.HUNK_RELOC32SHORT) {
if (relocBlocks == null) {
relocBlocks = new ArrayList<>();
}
relocBlocks.add((HunkRelocBlock)block);
} else if (block.getHunkType() == HunkType.HUNK_EXT) {
if (extBlock == null) {
extBlock = (HunkExtBlock)block;
} else {
throw new HunkParseError("Duplicate EXT block in hunk");
}
} else {
throw new HunkParseError("Invalid hunk block");
}
}
}
}