-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgdylib.go
263 lines (226 loc) · 5.99 KB
/
gdylib.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package gdylib
import (
"bytes"
"encoding/binary"
"github.com/lateralusd/gdylib/internal/macho"
"io"
"os"
)
type LoadType uint32
const (
WEAK LoadType = iota
DYLIB
RPATH
)
type config struct {
loadType LoadType
removeCodeSig bool
dylibPath string
binaryPath string
byteOrder binary.ByteOrder
bts *bytes.Buffer
loads *bytes.Buffer
f *os.File
}
type Option = func(c *config)
func Run(binaryPath, dylibPath string, opts ...Option) (io.Reader, error) {
c := configFromOpts(dylibPath, binaryPath, opts...)
f, err := os.OpenFile(binaryPath, os.O_RDONLY, os.ModePerm)
if err != nil {
return nil, err
}
defer f.Close()
c.f = f
c.getByteOrder()
start := make([]byte, 4)
c.f.ReadAt(start, 0)
magic := binary.LittleEndian.Uint32(start)
var wHeader wrapperHeader
wHeader.magic = macho.Magic(magic)
if wHeader.magic == macho.M32 {
binary.Read(c.f, binary.LittleEndian, &wHeader.m32)
} else {
binary.Read(c.f, binary.LittleEndian, &wHeader.m64)
}
var filetype macho.Filetype
var ncmds uint32
var sizeofcmds uint32
if wHeader.magic == macho.M32 {
filetype = wHeader.m32.Filetype
ncmds = wHeader.m32.NCmds
sizeofcmds = wHeader.m32.SizeOfCmds
} else {
filetype = wHeader.m64.Filetype
ncmds = wHeader.m64.NCmds
sizeofcmds = wHeader.m64.SizeOfCmds
}
if filetype != macho.MH_EXECUTE {
return nil, ErrNotExecute
}
var loads []macho.Load
var lcDataSize uint32
off, _ := c.f.Seek(0, io.SeekCurrent)
for i := 0; i < int(ncmds); i++ {
var ld macho.LoadHeader
binary.Read(c.f, binary.LittleEndian, &ld)
switch ld.Cmd {
case macho.LC_CODE_SIGNATURE:
if c.removeCodeSig {
if i != int(ncmds-1) {
return nil, ErrNotLastCommand
}
var lcCode macho.LCCode
binary.Read(c.f, binary.LittleEndian, &lcCode)
lcDataSize = lcCode.DataSize
f.Seek(off, 0)
// write zero in the place where LC_CODE_SIGNATURE was
buffer := zeroSlice(int(ld.Size))
loads = append(loads, macho.Load{
LoadHeader: macho.LoadHeader{Cmd: ld.Cmd, Size: ld.Size},
Raw: buffer,
})
} else {
f.Seek(off, 0)
buffer := make([]byte, ld.Size)
binary.Read(c.f, c.byteOrder, &buffer)
loads = append(loads, macho.Load{
LoadHeader: macho.LoadHeader{Cmd: ld.Cmd, Size: ld.Size},
Raw: buffer,
})
}
default:
f.Seek(off, 0)
buffer := make([]byte, ld.Size)
binary.Read(c.f, binary.LittleEndian, &buffer)
loads = append(loads, macho.Load{
LoadHeader: macho.LoadHeader{Cmd: ld.Cmd, Size: ld.Size},
Raw: buffer,
})
}
off += int64(ld.Size)
f.Seek(off, 0)
}
var loadHeader macho.LoadHeader
switch c.loadType {
case DYLIB:
loadHeader.Cmd = macho.LC_LOAD_DYLIB
case WEAK:
loadHeader.Cmd = macho.LC_LOAD_WEAK_DYLIB
case RPATH:
loadHeader.Cmd = macho.LC_RPATH
default:
return nil, ErrTypeNotSupported
}
cmdSize := c.getCmdSize()
loadHeader.Size = cmdSize
cmdBuffer := make([]byte, cmdSize)
if c.removeCodeSig {
for _, load := range loads {
switch load.Cmd {
case macho.LC_CODE_SIGNATURE:
for i := 0; i < len(load.Raw); i++ {
cmdBuffer[i] = load.Raw[i]
}
rest := make([]byte, len(cmdBuffer)-len(load.Raw))
f.ReadAt(rest, off)
for i := 0; i < len(rest); i++ {
cmdBuffer[i+len(load.Raw)] = rest[i]
}
ncmds -= 1
sizeofcmds -= load.Size
}
}
} else {
f.ReadAt(cmdBuffer, off)
}
hasSpace := true
for _, b := range cmdBuffer {
if b != 0 {
hasSpace = false
}
}
if !hasSpace {
return nil, ErrNotEnoughSpace
}
if wHeader.magic == macho.M32 {
wHeader.m32.SizeOfCmds = sizeofcmds
wHeader.m32.NCmds = ncmds
wHeader.m32.NCmds += 1
wHeader.m32.SizeOfCmds += cmdSize
binary.Write(c.bts, binary.LittleEndian, wHeader.m32)
} else {
wHeader.m64.SizeOfCmds = sizeofcmds
wHeader.m64.NCmds = ncmds
wHeader.m64.NCmds += 1
wHeader.m64.SizeOfCmds += cmdSize
binary.Write(c.bts, binary.LittleEndian, wHeader.m64)
}
end, _ := c.f.Seek(0, io.SeekEnd)
// write the loads to buffer
for _, load := range loads {
if c.removeCodeSig {
if load.Cmd != macho.LC_CODE_SIGNATURE {
switch load.Cmd {
case macho.LC_SEGMENT:
var seg macho.Segment
bt := bytes.NewBuffer(load.Raw[8:])
binary.Read(bt, binary.LittleEndian, &seg)
if string(stripNull(seg.SegName[:])) == "__LINKEDIT" {
seg.FileSize -= lcDataSize
newL := new(bytes.Buffer)
binary.Write(newL, binary.LittleEndian, load.LoadHeader)
binary.Write(newL, binary.LittleEndian, seg)
load.Raw = newL.Bytes()
}
case macho.LC_SEGMENT_64:
var seg macho.Segment64
bt := bytes.NewBuffer(load.Raw[8:])
binary.Read(bt, binary.LittleEndian, &seg)
if string(stripNull(seg.SegName[:])) == "__LINKEDIT" {
seg.FileSize -= uint64(lcDataSize)
newL := new(bytes.Buffer)
binary.Write(newL, binary.LittleEndian, load.LoadHeader)
binary.Write(newL, binary.LittleEndian, seg)
load.Raw = newL.Bytes()
}
case macho.LC_SYMTAB:
var symtab macho.Symtab
bt := bytes.NewBuffer(load.Raw[8:])
binary.Read(bt, binary.LittleEndian, &symtab)
sz := end - int64(lcDataSize)
diffSize := int64(symtab.StrOff+symtab.StrSize) - sz
newSize := symtab.StrSize - uint32(diffSize)
symtab.StrSize = newSize
newS := new(bytes.Buffer)
binary.Write(newS, binary.LittleEndian, load.LoadHeader)
binary.Write(newS, binary.LittleEndian, symtab)
load.Raw = newS.Bytes()
}
c.bts.Write(load.Raw)
}
} else {
c.bts.Write(load.Raw)
}
}
c.writeLoad(loadHeader)
currentOff := len(c.bts.Bytes())
count := end - int64(currentOff)
if c.removeCodeSig {
count -= int64(lcDataSize)
}
rest := make([]byte, count)
f.Seek(int64(currentOff), io.SeekStart)
binary.Read(c.f, binary.LittleEndian, &rest)
c.bts.Write(rest)
return c.bts, nil
}
func WithLoadType(t LoadType) Option {
return Option(func(c *config) {
c.loadType = t
})
}
func WithRemoveCodeSig(remove bool) Option {
return Option(func(c *config) {
c.removeCodeSig = remove
})
}