-
Notifications
You must be signed in to change notification settings - Fork 9
/
binutils.h
676 lines (548 loc) · 17.9 KB
/
binutils.h
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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
// binutils.h
// hosts2zones
//
// Created by Dr. Rolf Jansen on 2016-08-13.
// Copyright (c) 2016 projectworld.net. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define noerr 0
typedef unsigned char uchar;
typedef unsigned int uint;
typedef long long llong;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define b2_0 0
#define b2_1 1
#define b4_0 0
#define b4_1 1
#define b4_2 2
#define b4_3 3
#define b8_0 0
#define b8_1 1
#define b8_2 2
#define b8_3 3
#define b8_4 4
#define b8_5 5
#define b8_6 6
#define b8_7 7
#define swapInt16(x) _swapInt16(x)
#define swapInt32(x) _swapInt32(x)
#define swapInt64(x) _swapInt64(x)
#if defined(__i386__) || defined(__x86_64__)
static inline uint16_t _swapInt16(uint16_t x)
{
__asm__("rolw $8,%0" : "+q" (x));
return x;
}
static inline uint32_t _swapInt32(uint32_t x)
{
__asm__("bswapl %0" : "+q" (x));
return x;
}
#else
static inline uint16_t _swapInt16(uint16_t x)
{
uint16_t z;
uint8_t *p = (uint8_t *)&x;
uint8_t *q = (uint8_t *)&z;
q[0] = p[1];
q[1] = p[0];
return z;
}
static inline uint32_t _swapInt32(uint32_t x)
{
uint32_t z;
uint8_t *p = (uint8_t *)&x;
uint8_t *q = (uint8_t *)&z;
q[0] = p[3];
q[1] = p[2];
q[2] = p[1];
q[3] = p[0];
return z;
}
#endif
#if defined(__x86_64__)
static inline uint64_t _swapInt64(uint64_t x)
{
__asm__("bswapq %0" : "+q" (x));
return x;
}
#else
static inline uint64_t _swapInt64(uint64_t x)
{
uint64_t z;
uint8_t *p = (uint8_t *)&x;
uint8_t *q = (uint8_t *)&z;
q[0] = p[7];
q[1] = p[6];
q[2] = p[5];
q[3] = p[4];
q[4] = p[3];
q[5] = p[2];
q[6] = p[1];
q[7] = p[0];
return z;
}
#endif
#else
#define b2_0 1
#define b2_1 0
#define b4_0 3
#define b4_1 2
#define b4_2 1
#define b4_3 0
#define b8_0 7
#define b8_1 6
#define b8_2 5
#define b8_3 4
#define b8_4 3
#define b8_5 2
#define b8_6 1
#define b8_7 0
#define swapInt16(x) (x)
#define swapInt32(x) (x)
#define swapInt64(x) (x)
#endif
#if defined(__x86_64__)
#include <x86intrin.h>
static const __m128i nul16 = {0x0000000000000000ULL, 0x0000000000000000ULL}; // 16 bytes with nul
static const __m128i lfd16 = {0x0A0A0A0A0A0A0A0AULL, 0x0A0A0A0A0A0A0A0AULL}; // 16 bytes with line feed
static const __m128i col16 = {0x3A3A3A3A3A3A3A3AULL, 0x3A3A3A3A3A3A3A3AULL}; // 16 bytes with colon ':' limit
static const __m128i vtl16 = {0x7C7C7C7C7C7C7C7CULL, 0x7C7C7C7C7C7C7C7CULL}; // 16 bytes with vertical line '|' limit
static const __m128i blk16 = {0x2020202020202020ULL, 0x2020202020202020ULL}; // 16 bytes with inner blank limit
static const __m128i obl16 = {0x2121212121212121ULL, 0x2121212121212121ULL}; // 16 bytes with outer blank limit
static const __m128i dot16 = {0x2E2E2E2E2E2E2E2EULL, 0x2E2E2E2E2E2E2E2EULL}; // 16 bytes with dots '.'
// Drop-in replacement for strlen(), utilizing some builtin SSSE3 instructions
static inline int strvlen(const char *str)
{
if (!str || !*str)
return 0;
unsigned bmask;
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128((__m128i *)str), nul16)))
return __builtin_ctz(bmask);
for (int len = 16 - (intptr_t)str%16;; len += 16)
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_load_si128((__m128i *)&str[len]), nul16)))
return len + __builtin_ctz(bmask);
}
static inline int linelen(const char *line)
{
if (!line || !*line)
return 0;
unsigned bmask;
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128((__m128i *)line), nul16))
| (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128((__m128i *)line), lfd16)))
return __builtin_ctz(bmask);
for (int len = 16 - (intptr_t)line%16;; len += 16)
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_load_si128((__m128i *)&line[len]), nul16))
| (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_load_si128((__m128i *)&line[len]), lfd16)))
return len + __builtin_ctz(bmask);
}
static inline int taglen(const char *tag)
{
if (!tag || !*tag)
return 0;
unsigned bmask;
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128((__m128i *)tag), nul16))
| (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128((__m128i *)tag), col16)))
return __builtin_ctz(bmask);
for (int len = 16 - (intptr_t)tag%16;; len += 16)
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_load_si128((__m128i *)&tag[len]), nul16))
| (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_load_si128((__m128i *)&tag[len]), col16)))
return len + __builtin_ctz(bmask);
}
static inline int fieldlen(const char *field)
{
if (!field || !*field)
return 0;
unsigned bmask;
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128((__m128i *)field), nul16))
| (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128((__m128i *)field), vtl16)))
return __builtin_ctz(bmask);
for (int len = 16 - (intptr_t)field%16;; len += 16)
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_load_si128((__m128i *)&field[len]), nul16))
| (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_load_si128((__m128i *)&field[len]), vtl16)))
return len + __builtin_ctz(bmask);
}
static inline int domainlen(const char *domain)
{
if (!domain || !*domain)
return 0;
unsigned bmask;
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128((__m128i *)domain), nul16))
| (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128((__m128i *)domain), dot16)))
return __builtin_ctz(bmask);
for (int len = 16 - (intptr_t)domain%16;; len += 16)
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_load_si128((__m128i *)&domain[len]), nul16))
| (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_load_si128((__m128i *)&domain[len]), dot16)))
return len + __builtin_ctz(bmask);
}
static inline int wordlen(const char *word)
{
if (!word || !*word)
return 0;
unsigned bmask;
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(blk16, _mm_max_epu8(blk16, _mm_loadu_si128((__m128i *)word)))))
return __builtin_ctz(bmask); // ^^^^^^^ unsigned comparison (a >= b) is identical to a == maxu(a, b) ^^^^^^^
for (int len = 16 - (intptr_t)word%16;; len += 16)
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(blk16, _mm_max_epu8(blk16, _mm_load_si128((__m128i *)&word[len])))))
return len + __builtin_ctz(bmask);
}
static inline int blanklen(const char *blank)
{
if (!blank || !*blank)
return 0;
unsigned bmask;
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_loadu_si128((__m128i *)blank), nul16))
| (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(obl16, _mm_min_epu8(obl16, _mm_loadu_si128((__m128i *)blank)))))
return __builtin_ctz(bmask); // ^^^^^^^ unsigned comparison (a <= b) is identical to a == minu(a, b) ^^^^^^^
for (int len = 16 - (intptr_t)blank%16;; len += 16)
if (bmask = (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(_mm_load_si128((__m128i *)&blank[len]), nul16))
| (unsigned)_mm_movemask_epi8(_mm_cmpeq_epi8(obl16, _mm_min_epu8(obl16, _mm_load_si128((__m128i *)&blank[len])))))
return len + __builtin_ctz(bmask);
}
// String copying from src to dst.
// m: Max. capacity of dst, including the final nul.
// A value of 0 would indicate that the capacity of dst matches the size of src (including nul)
// l: On entry, src length or 0, on exit, the length of src, maybe NULL
// Returns the length of the resulting string in dst.
static inline int strmlcpy(char *dst, const char *src, int m, int *l)
{
int k, n;
if (l)
{
if (!*l)
*l = strvlen(src);
k = *l;
}
else
k = strvlen(src);
if (!m)
n = k;
else
n = (k < m) ? k : m-1;
switch (n)
{
default:
if ((intptr_t)dst&0xF || (intptr_t)src&0xF)
for (k = 0; k < n>>4<<1; k += 2)
((uint64_t *)dst)[k] = ((uint64_t *)src)[k], ((uint64_t *)dst)[k+1] = ((uint64_t *)src)[k+1];
else
for (k = 0; k < n>>4; k++)
_mm_store_si128(&((__m128i *)dst)[k], _mm_load_si128(&((__m128i *)src)[k]));
case 8 ... 15:
if ((k = n>>4<<1) < n>>3)
((uint64_t *)dst)[k] = ((uint64_t *)src)[k];
case 4 ... 7:
if ((k = n>>3<<1) < n>>2)
((uint32_t *)dst)[k] = ((uint32_t *)src)[k];
case 2 ... 3:
if ((k = n>>2<<1) < n>>1)
((uint16_t *)dst)[k] = ((uint16_t *)src)[k];
case 1:
if ((k = n>>1<<1) < n)
dst[k] = src[k];
case 0:
;
}
dst[n] = '\0';
return n;
}
#else
#define strvlen(s) (int)strlen(s)
static inline int linelen(const char *line)
{
if (!line || !*line)
return 0;
int l;
for (l = 0; line[l] && line[l] != '\n'; l++)
;
return l;
}
static inline int taglen(const char *tag)
{
if (!tag || !*tag)
return 0;
int l;
for (l = 0; tag[l] && tag[l] != ':'; l++)
;
return l;
}
static inline int fieldlen(const char *field)
{
if (!field || !*field)
return 0;
int l;
for (l = 0; field[l] && field[l] != '|'; l++)
;
return l;
}
static inline int domainlen(const char *domain)
{
if (!domain || !*domain)
return 0;
int l;
for (l = 0; domain[l] && domain[l] != '.'; l++)
;
return l;
}
static inline int wordlen(const char *word)
{
if (!word || !*word)
return 0;
int l;
for (l = 0; (uchar)word[l] > ' '; l++)
;
return l;
}
static inline int blanklen(const char *blank)
{
if (!blank || !*blank)
return 0;
int l;
for (l = 0; blank[l] && (uchar)blank[l] <= ' '; l++)
;
return l;
}
// String copying from src to dst.
// m: Max. capacity of dst, including the final nul.
// A value of 0 would indicate that the capacity of dst matches the size of src (including nul)
// l: On entry, src length or 0, on exit, the length of src, maybe NULL
// Returns the length of the resulting string in dst.
static inline int strmlcpy(char *dst, const char *src, int m, int *l)
{
int k, n;
if (l)
{
if (!*l)
*l = (int)strlen(src);
k = *l;
}
else
k = (int)strlen(src);
if (!m)
n = k;
else
n = (k < m) ? k : m-1;
strlcpy(dst, src, n+1);
return n;
}
#endif
// forward skip white space !!! s MUST NOT be NULL !!!
static inline char *skip(char *s)
{
for (;;)
if ((uchar)*s <= ' ')
s++;
else
return s;
}
// backward skip white space !!! s MUST NOT be NULL !!!
static inline char *bskip(char *s)
{
for (;;)
if ((uchar)*--s > ' ')
return s+1;
}
static inline char *trim(char *s)
{
*bskip(s+strvlen(s)) = '\0';
return skip(s);
}
static inline char *lowercase(char *s, int n)
{
if (s)
{
char c, *p = s;
for (int i = 0; i < n && (c = *p); i++)
if ('A' <= c && c <= 'Z')
*p++ = c + 0x20;
else
p++;
}
return s;
}
static inline char *uppercase(char *s, int n)
{
if (s)
{
char c, *p = s;
for (int i = 0; i < n && (c = *p); i++)
if ('a' <= c && c <= 'z')
*p++ = c - 0x20;
else
p++;
}
return s;
}
#define CMP(a, b, len) cmp##len(a, b)
#define cmp(a, b, len) CMP(a, b, len)
static inline bool cmp1(const void *a, const void *b)
{
return *(uint8_t *)a == *(uint8_t *)b;
}
static inline bool cmp2(const void *a, const void *b)
{
return *(uint16_t *)a == *(uint16_t *)b;
}
static inline bool cmp3(const void *a, const void *b)
{
return cmp1(a, b) && cmp2((uint8_t *)a+1, (uint8_t *)b+1);
}
static inline bool cmp4(const void *a, const void *b)
{
return *(uint32_t *)a == *(uint32_t *)b;
}
static inline bool cmp5(const void *a, const void *b)
{
return cmp1(a, b) && cmp4((uint8_t *)a+1, (uint8_t *)b+1);
}
static inline bool cmp6(const void *a, const void *b)
{
return cmp2(a, b) && cmp4((uint8_t *)a+2, (uint8_t *)b+2);
}
static inline bool cmp7(const void *a, const void *b)
{
return cmp3(a, b) && cmp4((uint8_t *)a+3, (uint8_t *)b+3);
}
static inline bool cmp8(const void *a, const void *b)
{
#if !defined(__arm__)
return *(uint64_t *)a == *(uint64_t *)b;
#else
return cmp4(a, b) && cmp4((uint8_t *)a+4, (uint8_t *)b+4);
#endif
}
static inline bool cmp9(const void *a, const void *b)
{
return cmp1(a, b) && cmp8((uint8_t *)a+1, (uint8_t *)b+1);
}
static inline bool cmp10(const void *a, const void *b)
{
return cmp2(a, b) && cmp8((uint8_t *)a+2, (uint8_t *)b+2);
}
static inline bool cmp11(const void *a, const void *b)
{
return cmp3(a, b) && cmp8((uint8_t *)a+3, (uint8_t *)b+3);
}
static inline bool cmp12(const void *a, const void *b)
{
return cmp4(a, b) && cmp8((uint8_t *)a+4, (uint8_t *)b+4);
}
static inline bool cmp13(const void *a, const void *b)
{
return cmp5(a, b) && cmp8((uint8_t *)a+5, (uint8_t *)b+5);
}
static inline bool cmp14(const void *a, const void *b)
{
return cmp6(a, b) && cmp8((uint8_t *)a+6, (uint8_t *)b+6);
}
static inline bool cmp15(const void *a, const void *b)
{
return cmp7(a, b) && cmp8((uint8_t *)a+7, (uint8_t *)b+7);
}
static inline bool cmp16(const void *a, const void *b)
{
return cmp8(a, b) && cmp8((uint8_t *)a+8, (uint8_t *)b+8);
}
#define CPY(a, b, len) cpy##len(a, b)
#define cpy(a, b, len) CPY(a, b, len)
static inline void cpy1(void *a, const void *b)
{
*(uint8_t *)a = *(uint8_t *)b;
}
static inline void cpy2(void *a, const void *b)
{
*(uint16_t *)a = *(uint16_t *)b;
}
static inline void cpy3(void *a, const void *b)
{
cpy2(a, b), cpy1((uint8_t *)a+2, (uint8_t *)b+2);
}
static inline void cpy4(void *a, const void *b)
{
*(uint32_t *)a = *(uint32_t *)b;
}
static inline void cpy5(void *a, const void *b)
{
cpy4(a, b), cpy1((uint8_t *)a+4, (uint8_t *)b+4);
}
static inline void cpy6(void *a, const void *b)
{
cpy4(a, b), cpy2((uint8_t *)a+4, (uint8_t *)b+4);
}
static inline void cpy7(void *a, const void *b)
{
cpy4(a, b), cpy3((uint8_t *)a+4, (uint8_t *)b+4);
}
static inline void cpy8(void *a, const void *b)
{
#if !defined(__arm__)
*(uint64_t *)a = *(uint64_t *)b;
#else
cpy4(a, b), cpy4((uint8_t *)a+4, (uint8_t *)b+4);
#endif
}
static inline void cpy9(void *a, const void *b)
{
cpy8(a, b), cpy1((uint8_t *)a+8, (uint8_t *)b+8);
}
static inline void cpy10(void *a, const void *b)
{
cpy8(a, b), cpy2((uint8_t *)a+8, (uint8_t *)b+8);
}
static inline void cpy11(void *a, const void *b)
{
cpy8(a, b), cpy3((uint8_t *)a+8, (uint8_t *)b+8);
}
static inline void cpy12(void *a, const void *b)
{
cpy8(a, b), cpy4((uint8_t *)a+8, (uint8_t *)b+8);
}
static inline void cpy13(void *a, const void *b)
{
cpy8(a, b), cpy5((uint8_t *)a+8, (uint8_t *)b+8);
}
static inline void cpy14(void *a, const void *b)
{
cpy8(a, b), cpy6((uint8_t *)a+8, (uint8_t *)b+8);
}
static inline void cpy15(void *a, const void *b)
{
cpy8(a, b), cpy7((uint8_t *)a+8, (uint8_t *)b+8);
}
static inline void cpy16(void *a, const void *b)
{
cpy8(a, b), cpy8((uint8_t *)a+8, (uint8_t *)b+8);
}
#pragma mark ••• Fencing Memory Allocation Wrappers •••
// void pointer reference
#define VPR(p) (void **)&(p)
typedef struct
{
ssize_t size;
size_t check;
char payload[16];
// size_t zerowall; // the allocation routines allocate sizeof(size_t) extra space and set this to zero
} allocation;
#define allocationMetaSize (offsetof(allocation, payload) - offsetof(allocation, size))
extern ssize_t gAllocationTotal;
void *allocate(ssize_t size, bool cleanout);
void *reallocate(void *p, ssize_t size, bool cleanout, bool free_on_error);
void deallocate(void **p, bool cleanout);
void deallocate_batch(unsigned cleanout, ...);