OpenOCD
replacements.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 /***************************************************************************
4  * Copyright (C) 2006 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007,2008 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2008 by Spencer Oliver *
11  * spen@spen-soft.co.uk *
12  ***************************************************************************/
13 
14 #ifndef OPENOCD_HELPER_REPLACEMENTS_H
15 #define OPENOCD_HELPER_REPLACEMENTS_H
16 
17 #include <stdint.h>
18 #include <helper/system.h>
19 #include <helper/bits.h>
20 
21 /* MIN,MAX macros */
22 #ifndef MIN
23 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
24 #endif
25 #ifndef MAX
26 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
27 #endif
28 
29 /* for systems that do not support ENOTSUP
30  * win32 being one of them */
31 #ifndef ENOTSUP
32 #define ENOTSUP 134 /* Not supported */
33 #endif
34 
35 /* for systems that do not support O_BINARY
36  * linux being one of them */
37 #ifndef O_BINARY
38 #define O_BINARY 0
39 #endif
40 
41 #ifndef HAVE_SYS_TIME_H
42 
43 #ifndef _TIMEVAL_DEFINED
44 #define _TIMEVAL_DEFINED
45 
46 struct timeval {
47  long tv_sec;
48  long tv_usec;
49 };
50 
51 #endif /* _TIMEVAL_DEFINED */
52 
53 #endif
54 
55 void *clear_malloc(size_t size);
56 void *fill_malloc(size_t size);
57 
58 #ifndef IN_REPLACEMENTS_C
59 /*
60  * Now you have 3 ways for the malloc function:
61  *
62  * 1. Do not change anything, use the original malloc
63  *
64  * 2. Use the clear_malloc function instead of the original malloc.
65  * In this case you must use the following define:
66  * #define malloc((_a)) clear_malloc((_a))
67  *
68  * 3. Use the fill_malloc function instead of the original malloc.
69  * In this case you must use the following define:
70  * #define malloc((_a)) fill_malloc((_a))
71  *
72  * We have figured out that there could exist some malloc problems
73  * where variables are using without to be initialise. To find this
74  * places, use the fill_malloc function. With this function we want
75  * to initialize memory to some known bad state. This is quite easily
76  * spotted in the debugger and will trap to an invalid address.
77  *
78  * clear_malloc can be used if you want to set not initialise
79  * variable to 0.
80  *
81  * If you do not want to change the malloc function, to not use one of
82  * the following macros. Which is the default way.
83  */
84 
85 /* #define malloc(_a) clear_malloc(_a)
86  * #define malloc(_a) fill_malloc(_a) */
87 #endif /* IN_REPLACEMENTS_C */
88 
89 /* GNU extensions to the C library that may be missing on some systems */
90 #ifndef HAVE_STRNDUP
91 char *strndup(const char *s, size_t n);
92 #endif /* HAVE_STRNDUP */
93 
94 #ifndef HAVE_STRNLEN
95 size_t strnlen(const char *s, size_t maxlen);
96 #endif /* HAVE_STRNLEN */
97 
98 #ifndef HAVE_USLEEP
99 #ifdef _WIN32
100 static inline unsigned int usleep(unsigned int usecs)
101 {
102  Sleep((usecs/1000));
103  return 0;
104 }
105 #else
106 #error no usleep defined for your platform
107 #endif
108 #endif /* HAVE_USLEEP */
109 
110 /* Windows specific */
111 #ifdef _WIN32
112 
113 #include <windows.h>
114 #include <time.h>
115 
116 /* Windows does not declare sockaddr_un */
117 #define UNIX_PATH_LEN 108
118 struct sockaddr_un {
119  uint16_t sun_family;
120  char sun_path[UNIX_PATH_LEN];
121 };
122 
123 /* win32 systems do not support ETIMEDOUT */
124 
125 #ifndef ETIMEDOUT
126 #define ETIMEDOUT WSAETIMEDOUT
127 #endif
128 
129 #if IS_MINGW == 1
130 #if defined(__x86_64__) || defined(__i386__)
131 static inline unsigned char inb(unsigned short int port)
132 {
133  unsigned char _v;
134  __asm__ __volatile__ ("inb %w1,%0" : "=a" (_v) : "Nd" (port));
135  return _v;
136 }
137 
138 static inline void outb(unsigned char value, unsigned short int port)
139 {
140  __asm__ __volatile__ ("outb %b0,%w1" : : "a" (value), "Nd" (port));
141 }
142 #endif
143 
144 /* mingw does not have ffs, so use gcc builtin types */
145 #define ffs __builtin_ffs
146 
147 #endif /* IS_MINGW */
148 
149 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv);
150 
151 #endif /* _WIN32 */
152 
153 /* Socket descriptor is unsigned on Windows.
154  * Add a cast to prevent sign mismatch warnings (-Wsign-compare). */
155 #ifdef _WIN32
156 #define OCD_FD_SET(fd, set) FD_SET((SOCKET)(fd), (set))
157 #define OCD_FD_CLR(fd, set) FD_CLR((SOCKET)(fd), (set))
158 #define OCD_FD_ISSET(fd, set) FD_ISSET((SOCKET)(fd), (set))
159 #else
160 #define OCD_FD_SET(fd, set) FD_SET((fd), (set))
161 #define OCD_FD_CLR(fd, set) FD_CLR((fd), (set))
162 #define OCD_FD_ISSET(fd, set) FD_ISSET((fd), (set))
163 #endif
164 
165 /* generic socket functions for Windows and Posix */
166 static inline int write_socket(int handle, const void *buffer, unsigned int count)
167 {
168 #ifdef _WIN32
169  return send(handle, buffer, count, 0);
170 #else
171  return write(handle, buffer, count);
172 #endif
173 }
174 
175 static inline int read_socket(int handle, void *buffer, unsigned int count)
176 {
177 #ifdef _WIN32
178  return recv(handle, buffer, count, 0);
179 #else
180  return read(handle, buffer, count);
181 #endif
182 }
183 
184 static inline int close_socket(int sock)
185 {
186 #ifdef _WIN32
187  return closesocket(sock);
188 #else
189  return close(sock);
190 #endif
191 }
192 
193 static inline void socket_block(int fd)
194 {
195 #ifdef _WIN32
196  unsigned long nonblock = 0;
197  ioctlsocket(fd, FIONBIO, &nonblock);
198 #else
199  int oldopts = fcntl(fd, F_GETFL, 0);
200  fcntl(fd, F_SETFL, oldopts & ~O_NONBLOCK);
201 #endif
202 }
203 
204 static inline void socket_nonblock(int fd)
205 {
206 #ifdef _WIN32
207  unsigned long nonblock = 1;
208  ioctlsocket(fd, FIONBIO, &nonblock);
209 #else
210  int oldopts = fcntl(fd, F_GETFL, 0);
211  fcntl(fd, F_SETFL, oldopts | O_NONBLOCK);
212 #endif
213 }
214 
215 static inline int socket_select(int max_fd,
216  fd_set *rfds,
217  fd_set *wfds,
218  fd_set *efds,
219  struct timeval *tv)
220 {
221 #ifdef _WIN32
222  return win_select(max_fd, rfds, wfds, efds, tv);
223 #else
224  return select(max_fd, rfds, wfds, efds, tv);
225 #endif
226 }
227 
228 static inline int socket_recv_timeout(int fd, unsigned long timeout_msec)
229 {
230 #ifdef _WIN32
231  DWORD timeout = timeout_msec;
232  return setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout,
233  sizeof(timeout));
234 #else
235  struct timeval tv;
236  tv.tv_sec = timeout_msec / 1000;
237  tv.tv_usec = (timeout_msec % 1000) * 1000;
238  return setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(tv));
239 #endif
240 }
241 
242 static inline int socket_send_timeout(int fd, unsigned long timeout_msec)
243 {
244 #ifdef _WIN32
245  DWORD timeout = timeout_msec;
246  return setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout,
247  sizeof(timeout));
248 #else
249  struct timeval tv;
250  tv.tv_sec = timeout_msec / 1000;
251  tv.tv_usec = (timeout_msec % 1000) * 1000;
252  return setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&tv, sizeof(tv));
253 #endif
254 }
255 
256 #ifndef HAVE_ELF_H
257 
258 typedef uint32_t Elf32_Addr;
259 typedef uint16_t Elf32_Half;
260 typedef uint32_t Elf32_Off;
261 typedef uint32_t Elf32_Word;
262 typedef uint32_t Elf32_Size;
263 typedef uint16_t Elf32_Section;
264 
265 #define EI_NIDENT 16
266 
267 typedef struct {
268  unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
269  Elf32_Half e_type; /* Object file type */
270  Elf32_Half e_machine; /* Architecture */
271  Elf32_Word e_version; /* Object file version */
272  Elf32_Addr e_entry; /* Entry point virtual address */
273  Elf32_Off e_phoff; /* Program header table file offset */
274  Elf32_Off e_shoff; /* Section header table file offset */
275  Elf32_Word e_flags; /* Processor-specific flags */
276  Elf32_Half e_ehsize; /* ELF header size in bytes */
277  Elf32_Half e_phentsize; /* Program header table entry size */
278  Elf32_Half e_phnum; /* Program header table entry count */
279  Elf32_Half e_shentsize; /* Section header table entry size */
280  Elf32_Half e_shnum; /* Section header table entry count */
281  Elf32_Half e_shstrndx; /* Section header string table index */
282 } Elf32_Ehdr;
283 
284 #define ELFMAG "\177ELF"
285 #define SELFMAG 4
286 
287 #define EI_CLASS 4 /* File class byte index */
288 #define ELFCLASS32 1 /* 32-bit objects */
289 #define ELFCLASS64 2 /* 64-bit objects */
290 
291 #define EI_DATA 5 /* Data encoding byte index */
292 #define ELFDATA2LSB 1 /* 2's complement, little endian */
293 #define ELFDATA2MSB 2 /* 2's complement, big endian */
294 
295 typedef struct {
296  Elf32_Word sh_name; /* Section name (string tbl index) */
297  Elf32_Word sh_type; /* Section type */
298  Elf32_Word sh_flags; /* Section flags */
299  Elf32_Addr sh_addr; /* Section virtual addr at execution */
300  Elf32_Off sh_offset; /* Section file offset */
301  Elf32_Word sh_size; /* Section size in bytes */
302  Elf32_Word sh_link; /* Link to another section */
303  Elf32_Word sh_info; /* Additional section information */
304  Elf32_Word sh_addralign; /* Section alignment */
305  Elf32_Word sh_entsize; /* Entry size if section holds table */
306 } Elf32_Shdr;
307 
308 #define SHT_SYMTAB 2 /* Symbol table */
309 
310 #define SHF_WRITE BIT(0) /* Writable */
311 #define SHF_ALLOC BIT(1) /* Occupies memory during execution */
312 #define SHF_EXECINSTR BIT(2) /* Executable */
313 
314 typedef struct {
315  Elf32_Word st_name; /* Symbol name (string tbl index) */
316  Elf32_Addr st_value; /* Symbol value */
317  Elf32_Word st_size; /* Symbol size */
318  unsigned char st_info; /* Symbol type and binding */
319  unsigned char st_other; /* Symbol visibility */
320  Elf32_Section st_shndx; /* Section index */
321 } Elf32_Sym;
322 
323 typedef struct {
324  Elf32_Word p_type; /* Segment type */
325  Elf32_Off p_offset; /* Segment file offset */
326  Elf32_Addr p_vaddr; /* Segment virtual address */
327  Elf32_Addr p_paddr; /* Segment physical address */
328  Elf32_Size p_filesz; /* Segment size in file */
329  Elf32_Size p_memsz; /* Segment size in memory */
330  Elf32_Word p_flags; /* Segment flags */
331  Elf32_Size p_align; /* Segment alignment */
332 } Elf32_Phdr;
333 
334 #define PT_LOAD 1 /* Loadable program segment */
335 
336 #endif /* HAVE_ELF_H */
337 
338 #ifndef HAVE_ELF64
339 
340 typedef uint64_t Elf64_Addr;
341 typedef uint16_t Elf64_Half;
342 typedef uint64_t Elf64_Off;
343 typedef uint32_t Elf64_Word;
344 typedef uint64_t Elf64_Xword;
345 
346 typedef struct {
347  unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
348  Elf64_Half e_type; /* Object file type */
349  Elf64_Half e_machine; /* Architecture */
350  Elf64_Word e_version; /* Object file version */
351  Elf64_Addr e_entry; /* Entry point virtual address */
352  Elf64_Off e_phoff; /* Program header table file offset */
353  Elf64_Off e_shoff; /* Section header table file offset */
354  Elf64_Word e_flags; /* Processor-specific flags */
355  Elf64_Half e_ehsize; /* ELF header size in bytes */
356  Elf64_Half e_phentsize; /* Program header table entry size */
357  Elf64_Half e_phnum; /* Program header table entry count */
358  Elf64_Half e_shentsize; /* Section header table entry size */
359  Elf64_Half e_shnum; /* Section header table entry count */
360  Elf64_Half e_shstrndx; /* Section header string table index */
361 } Elf64_Ehdr;
362 
363 typedef struct {
364  Elf64_Word p_type; /* Segment type */
365  Elf64_Word p_flags; /* Segment flags */
366  Elf64_Off p_offset; /* Segment file offset */
367  Elf64_Addr p_vaddr; /* Segment virtual address */
368  Elf64_Addr p_paddr; /* Segment physical address */
369  Elf64_Xword p_filesz; /* Segment size in file */
370  Elf64_Xword p_memsz; /* Segment size in memory */
371  Elf64_Xword p_align; /* Segment alignment */
372 } Elf64_Phdr;
373 
374 #endif /* HAVE_ELF64 */
375 
376 #endif /* OPENOCD_HELPER_REPLACEMENTS_H */
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
static int socket_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
Definition: replacements.h:215
uint16_t Elf32_Half
Definition: replacements.h:259
uint32_t Elf32_Addr
Definition: replacements.h:258
static void socket_block(int fd)
Definition: replacements.h:193
static int read_socket(int handle, void *buffer, unsigned int count)
Definition: replacements.h:175
uint64_t Elf64_Xword
Definition: replacements.h:344
static int socket_recv_timeout(int fd, unsigned long timeout_msec)
Definition: replacements.h:228
static int close_socket(int sock)
Definition: replacements.h:184
uint32_t Elf32_Off
Definition: replacements.h:260
uint64_t Elf64_Off
Definition: replacements.h:342
static int socket_send_timeout(int fd, unsigned long timeout_msec)
Definition: replacements.h:242
char * strndup(const char *s, size_t n)
Definition: replacements.c:71
static int write_socket(int handle, const void *buffer, unsigned int count)
Definition: replacements.h:166
uint32_t Elf64_Word
Definition: replacements.h:343
void * clear_malloc(size_t size)
Definition: replacements.c:31
uint16_t Elf32_Section
Definition: replacements.h:263
uint32_t Elf32_Size
Definition: replacements.h:262
uint16_t Elf64_Half
Definition: replacements.h:341
#define EI_NIDENT
Definition: replacements.h:265
static void socket_nonblock(int fd)
Definition: replacements.h:204
uint64_t Elf64_Addr
Definition: replacements.h:340
void * fill_malloc(size_t size)
Definition: replacements.c:39
uint32_t Elf32_Word
Definition: replacements.h:261
size_t strnlen(const char *s, size_t maxlen)
Definition: replacements.c:63
Elf32_Off e_shoff
Definition: replacements.h:274
Elf32_Half e_ehsize
Definition: replacements.h:276
Elf32_Half e_shnum
Definition: replacements.h:280
Elf32_Half e_machine
Definition: replacements.h:270
Elf32_Off e_phoff
Definition: replacements.h:273
Elf32_Half e_phnum
Definition: replacements.h:278
Elf32_Half e_shstrndx
Definition: replacements.h:281
Elf32_Half e_type
Definition: replacements.h:269
Elf32_Word e_flags
Definition: replacements.h:275
Elf32_Word e_version
Definition: replacements.h:271
Elf32_Half e_shentsize
Definition: replacements.h:279
Elf32_Addr e_entry
Definition: replacements.h:272
Elf32_Half e_phentsize
Definition: replacements.h:277
Elf32_Addr p_vaddr
Definition: replacements.h:326
Elf32_Size p_align
Definition: replacements.h:331
Elf32_Word p_flags
Definition: replacements.h:330
Elf32_Size p_memsz
Definition: replacements.h:329
Elf32_Word p_type
Definition: replacements.h:324
Elf32_Size p_filesz
Definition: replacements.h:328
Elf32_Off p_offset
Definition: replacements.h:325
Elf32_Addr p_paddr
Definition: replacements.h:327
Elf32_Word sh_entsize
Definition: replacements.h:305
Elf32_Word sh_flags
Definition: replacements.h:298
Elf32_Word sh_addralign
Definition: replacements.h:304
Elf32_Off sh_offset
Definition: replacements.h:300
Elf32_Word sh_name
Definition: replacements.h:296
Elf32_Addr sh_addr
Definition: replacements.h:299
Elf32_Word sh_size
Definition: replacements.h:301
Elf32_Word sh_type
Definition: replacements.h:297
Elf32_Word sh_link
Definition: replacements.h:302
Elf32_Word sh_info
Definition: replacements.h:303
Elf32_Word st_size
Definition: replacements.h:317
unsigned char st_other
Definition: replacements.h:319
Elf32_Word st_name
Definition: replacements.h:315
unsigned char st_info
Definition: replacements.h:318
Elf32_Addr st_value
Definition: replacements.h:316
Elf32_Section st_shndx
Definition: replacements.h:320
Elf64_Half e_type
Definition: replacements.h:348
Elf64_Half e_shentsize
Definition: replacements.h:358
Elf64_Half e_shnum
Definition: replacements.h:359
Elf64_Word e_version
Definition: replacements.h:350
Elf64_Half e_ehsize
Definition: replacements.h:355
Elf64_Off e_shoff
Definition: replacements.h:353
Elf64_Addr e_entry
Definition: replacements.h:351
Elf64_Half e_phentsize
Definition: replacements.h:356
Elf64_Off e_phoff
Definition: replacements.h:352
Elf64_Half e_machine
Definition: replacements.h:349
Elf64_Word e_flags
Definition: replacements.h:354
Elf64_Half e_shstrndx
Definition: replacements.h:360
Elf64_Half e_phnum
Definition: replacements.h:357
Elf64_Xword p_memsz
Definition: replacements.h:370
Elf64_Addr p_vaddr
Definition: replacements.h:367
Elf64_Addr p_paddr
Definition: replacements.h:368
Elf64_Off p_offset
Definition: replacements.h:366
Elf64_Xword p_align
Definition: replacements.h:371
Elf64_Word p_flags
Definition: replacements.h:365
Elf64_Word p_type
Definition: replacements.h:364
Elf64_Xword p_filesz
Definition: replacements.h:369
Definition: psoc6.c:83
long tv_sec
Definition: replacements.h:47
long tv_usec
Definition: replacements.h:48
uint8_t count[4]
Definition: vdebug.c:22
#define DWORD
Definition: x86_32_common.h:33