1
2
3
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
30
31
32
33
34
36
37
38
39
40
41
52
53
54
55
56
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
101
102
103
104
105
106
107
108
109
110
111
112
113
122
123
124
125
126
127
128
129
130
131
132
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
198
199
201
202
203
204
205
206
207
209
210
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* ... */
#ifndef OPENOCD_HELPER_REPLACEMENTS_H
#define OPENOCD_HELPER_REPLACEMENTS_H
#include <stdint.h>
#include <helper/system.h>
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
/* ... */
#ifndef ENOTSUP
#define ENOTSUP 134
#endif
/* ... */
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef HAVE_SYS_TIME_H
#ifndef _TIMEVAL_DEFINED
#define _TIMEVAL_DEFINED
struct timeval {
long tv_sec;
long tv_usec;
...};
/* ... */
#endif
/* ... */
#endif
#ifndef HAVE_GETTIMEOFDAY
#ifdef _WIN32
struct timezone {
int tz_minuteswest;
int tz_dsttime;
...};/* ... */
#endif
struct timezone;
int gettimeofday(struct timeval *tv, struct timezone *tz);
/* ... */
#endif
void *clear_malloc(size_t size);
void *fill_malloc(size_t size);
#ifndef IN_REPLACEMENTS_C
/* ... */
/* ... */
/* ... */#endif
#ifndef HAVE_STRNDUP
char *strndup(const char *s, size_t n);
#endif
#ifndef HAVE_STRNLEN
size_t strnlen(const char *s, size_t maxlen);
#endif
#ifndef HAVE_USLEEP
#ifdef _WIN32
static inline unsigned usleep(unsigned int usecs)
{
Sleep((usecs/1000));
return 0;
}usleep (unsigned int usecs) { ... }
/* ... */#else
#error no usleep defined for your platform
#endif/* ... */
#endif
#ifdef _WIN32
#include <windows.h>
#include <time.h>
#define UNIX_PATH_LEN 108
struct sockaddr_un {
uint16_t sun_family;
char sun_path[UNIX_PATH_LEN];
...};
#ifndef ETIMEDOUT
#define ETIMEDOUT WSAETIMEDOUT
#endif
#if IS_MINGW == 1
static inline unsigned char inb(unsigned short int port)
{
unsigned char _v;
__asm__ __volatile__ ("inb %w1,%0" : "=a" (_v) : "Nd" (port));
return _v;
}{ ... }
static inline void outb(unsigned char value, unsigned short int port)
{
__asm__ __volatile__ ("outb %b0,%w1" : : "a" (value), "Nd" (port));
}{ ... }
#define ffs __builtin_ffs
/* ... */
#endif
int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv);
/* ... */
#endif
static inline int write_socket(int handle, const void *buffer, unsigned int count)
{
#ifdef _WIN32
return send(handle, buffer, count, 0);
#else
return write(handle, buffer, count);
#endif
}{ ... }
static inline int read_socket(int handle, void *buffer, unsigned int count)
{
#ifdef _WIN32
return recv(handle, buffer, count, 0);
#else
return read(handle, buffer, count);
#endif
}{ ... }
static inline int close_socket(int sock)
{
#ifdef _WIN32
return closesocket(sock);
#else
return close(sock);
#endif
}{ ... }
static inline void socket_block(int fd)
{
#ifdef _WIN32
unsigned long nonblock = 0;
ioctlsocket(fd, FIONBIO, &nonblock);/* ... */
#else
int oldopts = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, oldopts & ~O_NONBLOCK);/* ... */
#endif
}{ ... }
static inline void socket_nonblock(int fd)
{
#ifdef _WIN32
unsigned long nonblock = 1;
ioctlsocket(fd, FIONBIO, &nonblock);/* ... */
#else
int oldopts = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, oldopts | O_NONBLOCK);/* ... */
#endif
}{ ... }
static inline int socket_select(int max_fd,
fd_set *rfds,
fd_set *wfds,
fd_set *efds,
struct timeval *tv)
{
#ifdef _WIN32
return win_select(max_fd, rfds, wfds, efds, tv);
#else
return select(max_fd, rfds, wfds, efds, tv);
#endif
}{ ... }
#ifndef HAVE_ELF_H
typedef uint32_t Elf32_Addr;
typedef uint16_t Elf32_Half;
typedef uint32_t Elf32_Off;
typedef uint32_t Elf32_Word;
typedef uint32_t Elf32_Size;
#define EI_NIDENT 16
typedef struct {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
...} Elf32_Ehdr;
typedef struct {
Elf32_Word sh_name;
/* ... */
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Size sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Size sh_addralign;
Elf32_Size sh_entsize;
...} Elf32_Shdr;
typedef struct {
Elf32_Word st_name;
Elf32_Addr st_value;
Elf32_Size st_size;
unsigned char st_info;
unsigned char st_other;
Elf32_Half st_shndx;
...} Elf32_Sym;
#define ELFMAG "\177ELF"
#define SELFMAG 4
#define EI_CLASS 4
#define ELFCLASS32 1
#define ELFCLASS64 2
#define EI_DATA 5
#define ELFDATA2LSB 1
#define ELFDATA2MSB 2
8 defines
typedef struct {
Elf32_Word p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
Elf32_Size p_filesz;
Elf32_Size p_memsz;
Elf32_Word p_flags;
Elf32_Size p_align;
...} Elf32_Phdr;
#define PT_LOAD 1
/* ... */
#endif
#ifndef HAVE_ELF64
typedef uint64_t Elf64_Addr;
typedef uint16_t Elf64_Half;
typedef uint64_t Elf64_Off;
typedef uint32_t Elf64_Word;
typedef uint64_t Elf64_Xword;
typedef struct {
unsigned char e_ident[EI_NIDENT];
Elf64_Half e_type;
Elf64_Half e_machine;
Elf64_Word e_version;
Elf64_Addr e_entry;
Elf64_Off e_phoff;
Elf64_Off e_shoff;
Elf64_Word e_flags;
Elf64_Half e_ehsize;
Elf64_Half e_phentsize;
Elf64_Half e_phnum;
Elf64_Half e_shentsize;
Elf64_Half e_shnum;
Elf64_Half e_shstrndx;
...} Elf64_Ehdr;
typedef struct {
Elf64_Word p_type;
Elf64_Word p_flags;
Elf64_Off p_offset;
Elf64_Addr p_vaddr;
Elf64_Addr p_paddr;
Elf64_Xword p_filesz;
Elf64_Xword p_memsz;
Elf64_Xword p_align;
...} Elf64_Phdr;
/* ... */
#endif
/* ... */
#endif