-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathelf32_format_test.go
213 lines (204 loc) · 4.83 KB
/
elf32_format_test.go
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
package elf_reader
import (
"os"
"testing"
)
// Returns the content of the file with the given name.
func fileBytes(filename string, t *testing.T) []byte {
toReturn, e := os.ReadFile(filename)
if e != nil {
t.Logf("Failed reading file: %s\n", e)
t.FailNow()
}
return toReturn
}
func parseTestELF32(filename string, t *testing.T) *ELF32File {
testFile := fileBytes(filename, t)
f, e := ParseELF32File(testFile)
if e != nil {
t.Logf("Error parsing file %s: %s\n", filename, e)
t.FailNow()
}
return f
}
func TestParseELF32File(t *testing.T) {
f := parseTestELF32("test_data/sleep_arm32", t)
var name string
var e error
if len(f.Sections) != 30 {
t.Logf("Expected 30 sections, got %d\n", len(f.Sections))
t.Fail()
}
for i := range f.Sections {
if i != 0 {
name, e = f.GetSectionName(uint16(i))
} else {
name, e = "<null section>", nil
}
if e != nil {
t.Logf("Error getting section %d name: %s\n", i, e)
t.FailNow()
}
t.Logf("Section %s (index %d): %s\n", name, i, &(f.Sections[i]))
}
if len(f.Segments) != 9 {
t.Logf("Expected 9 segments, got %d\n", len(f.Segments))
t.Fail()
}
for i := range f.Segments {
t.Logf("Segment %d: %s\n", i, &(f.Segments[i]))
}
}
func TestParseSymbols32(t *testing.T) {
f := parseTestELF32("test_data/sleep_arm32", t)
// Test reading the .dynsym section
found := false
for i := range f.Sections {
if !f.IsSymbolTable(uint16(i)) {
continue
}
name, e := f.GetSectionName(uint16(i))
if e != nil {
t.Logf("Failed getting symbol table section name: %s\n", e)
t.FailNow()
}
if name != ".dynsym" {
continue
}
found = true
symbols, names, e := f.GetSymbolTable(uint16(i))
if e != nil {
t.Logf("Failed parsing symbol table: %s\n", e)
t.FailNow()
}
if len(symbols) != 8 {
t.Logf("Expected 8 dynsym entries, got %d\n", len(symbols))
t.Fail()
}
for j := range symbols {
t.Logf("Symbol %s (index %d): %s\n", names[j], j, &(symbols[j]))
}
}
if !found {
t.Logf("Couldn't find .dynsym section in test file.\n")
t.Fail()
}
}
func TestParseRelocations32(t *testing.T) {
f := parseTestELF32("test_data/sleep_arm32", t)
found := false
for i := range f.Sections {
// Look for the .rel.plt section
if !f.IsRelocationTable(uint16(i)) {
continue
}
name, e := f.GetSectionName(uint16(i))
if e != nil {
t.Logf("Failed getting relocation section name: %s\n", e)
t.FailNow()
}
if name != ".rel.plt" {
continue
}
found = true
relocations, e := f.GetRelocationTable(uint16(i))
if e != nil {
t.Logf("Failed parsing relocation table: %s\n", e)
t.FailNow()
}
if len(relocations) != 7 {
t.Logf("Expected 7 relocations, got %d\n", len(relocations))
t.Fail()
}
for j, r := range relocations {
t.Logf("Relocation %d: %s\n", j, r)
}
}
if !found {
t.Logf("Couldn't find .rel.plt section in the test file.\n")
t.Fail()
}
}
func TestParseDynamicTable32(t *testing.T) {
f := parseTestELF32("test_data/sleep_arm32", t)
found := false
for i := range f.Sections {
if !f.IsDynamicSection(uint16(i)) {
continue
}
entries, e := f.GetDynamicTable(uint16(i))
if e != nil {
t.Logf("Failed parsing the dynamic section: %s\n", e)
t.FailNow()
}
found = true
if entries[len(entries)-1].Tag != 0 {
t.Logf("The last dynamic entry tag wasn't 0.\n")
t.Fail()
}
for j := range entries {
entry := &(entries[j])
t.Logf("Dynamic linking table entry %d: %s\n", j, entry)
if entry.Tag == 0 {
break
}
}
break
}
if !found {
t.Logf("Couldn't find .dynamic section in the test file.\n")
t.Fail()
}
}
func TestParseVersionRequirements32(t *testing.T) {
f := parseTestELF32("test_data/sleep_arm32", t)
found := false
for i := range f.Sections {
if !f.IsVersionRequirementSection(uint16(i)) {
continue
}
found = true
need, aux, e := f.ParseVersionRequirementSection(uint16(i))
if e != nil {
t.Logf("Failed parsing version requirement section: %s\n", e)
t.FailNow()
}
for j, n := range need {
t.Logf("File %d: %s\n", j, &n)
for k, x := range aux[j] {
t.Logf(" Requirement %d: %s\n", k, &x)
}
}
}
if !found {
t.Logf("Couldn't find the GNU version requirement section in the " +
"test file.\n")
t.Fail()
}
}
func TestParseVersionDefinitions32(t *testing.T) {
f := parseTestELF32("test_data/ld-linux_arm32.so", t)
found := false
for i := range f.Sections {
if !f.IsVersionDefinitionSection(uint16(i)) {
continue
}
found = true
def, aux, e := f.ParseVersionDefinitionSection(uint16(i))
if e != nil {
t.Logf("Failed parsing version definition section: %s\n", e)
t.FailNow()
}
for j, d := range def {
t.Logf("Definition %d: %s\n", j, &d)
for k, x := range aux[j] {
t.Logf(" Aux %d: %s\n", k, &x)
}
}
}
if !found {
t.Logf("Couldn't find the GNU version definition section in the " +
"test shared library file.\n")
t.Fail()
}
}