-
Notifications
You must be signed in to change notification settings - Fork 2
/
uprintf.h
3801 lines (3238 loc) · 126 KB
/
uprintf.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Usage and other information can be found in README at https://github.com/spevnev/uprintf
// Bugs and suggestions can be submitted to https://github.com/spevnev/uprintf/issues
// MIT License
//
// Copyright (c) 2024 Serhii Pievniev
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ====================== CHECKS ==========================
// clang-format off
#ifndef __linux__
#error [ERROR] uprintf only supports Linux
#endif
#ifdef __cplusplus
#error [ERROR] uprintf does NOT support C++
#endif
// clang-format on
// ====================== HEADER ==========================
#ifndef UPRINTF_H
#define UPRINTF_H
void _upf_uprintf(const char *file, int line, const char *fmt, const char *args, ...);
// If variadic arguments were to be stringified directly, the arguments which
// use macros would stringify to the macro name instead of being expanded, but
// by calling another macro the argument-macros will be expanded and stringified
// to their real values.
#define _upf_stringify_va_args(...) #__VA_ARGS__
// The noop following uprintf is required to guarantee that return PC of the
// function is within the same scope. This is especially problematic if uprintf
// is the last call in the function because then its return PC is that of the
// caller, which optimizes two returns to one.
#define uprintf(fmt, ...) \
do { \
_upf_uprintf(__FILE__, __LINE__, fmt, _upf_stringify_va_args(__VA_ARGS__), __VA_ARGS__); \
__asm__ volatile("nop"); \
} while (0)
#ifdef UPRINTF_TEST
extern int _upf_test_status;
#endif
#endif // UPRINTF_H
// ====================== SOURCE ==========================
#ifdef UPRINTF_IMPLEMENTATION
// ===================== OPTIONS ==========================
#ifndef UPRINTF_INDENTATION_WIDTH
#define UPRINTF_INDENTATION_WIDTH 4
#endif
#ifndef UPRINTF_MAX_DEPTH
#define UPRINTF_MAX_DEPTH 10
#endif
#ifndef UPRINTF_IGNORE_STDIO_FILE
#define UPRINTF_IGNORE_STDIO_FILE true
#endif
#ifndef UPRINTF_ARRAY_COMPRESSION_THRESHOLD
#define UPRINTF_ARRAY_COMPRESSION_THRESHOLD 4
#endif
#ifndef UPRINTF_MAX_STRING_LENGTH
#define UPRINTF_MAX_STRING_LENGTH 200
#endif
// ===================== INCLUDES =========================
#define __USE_XOPEN_EXTENDED
#define _GNU_SOURCE
#include <dlfcn.h>
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <link.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
// =================== DECLARATIONS =======================
// Feature test macros might not work since it is possible that the header has
// been included before this one and thus expanded without the macro, so the
// functions must be declared here.
ssize_t readlink(const char *path, char *buf, size_t bufsiz);
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
// Part of dl_phdr_info used instead of it, since it may not be defined, but we
// also cannot define it ourselves since it could cause redefinition error.
typedef struct {
Elf64_Addr dlpi_addr;
const char *dlpi_name;
} _upf_dl_phdr_info;
struct dl_phdr_info;
int dl_iterate_phdr(int (*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data);
// ===================== dwarf.h ==========================
// dwarf.h's location is inconsistent and the package containing it may not be
// install, so we include a partial implementation of the header.
#define DW_UT_compile 0x01
#define DW_TAG_array_type 0x01
#define DW_TAG_enumeration_type 0x04
#define DW_TAG_formal_parameter 0x05
#define DW_TAG_lexical_block 0x0b
#define DW_TAG_member 0x0d
#define DW_TAG_pointer_type 0x0f
#define DW_TAG_compile_unit 0x11
#define DW_TAG_structure_type 0x13
#define DW_TAG_subroutine_type 0x15
#define DW_TAG_typedef 0x16
#define DW_TAG_union_type 0x17
#define DW_TAG_unspecified_parameters 0x18
#define DW_TAG_inlined_subroutine 0x1d
#define DW_TAG_subrange_type 0x21
#define DW_TAG_base_type 0x24
#define DW_TAG_const_type 0x26
#define DW_TAG_enumerator 0x28
#define DW_TAG_subprogram 0x2e
#define DW_TAG_variable 0x34
#define DW_TAG_volatile_type 0x35
#define DW_TAG_restrict_type 0x37
#define DW_TAG_atomic_type 0x47
#define DW_TAG_call_site 0x48
#define DW_TAG_call_site_parameter 0x49
#define DW_FORM_addr 0x01
#define DW_FORM_block2 0x03
#define DW_FORM_block4 0x04
#define DW_FORM_data2 0x05
#define DW_FORM_data4 0x06
#define DW_FORM_data8 0x07
#define DW_FORM_string 0x08
#define DW_FORM_block 0x09
#define DW_FORM_block1 0x0a
#define DW_FORM_data1 0x0b
#define DW_FORM_flag 0x0c
#define DW_FORM_sdata 0x0d
#define DW_FORM_strp 0x0e
#define DW_FORM_udata 0x0f
#define DW_FORM_ref_addr 0x10
#define DW_FORM_ref1 0x11
#define DW_FORM_ref2 0x12
#define DW_FORM_ref4 0x13
#define DW_FORM_ref8 0x14
#define DW_FORM_ref_udata 0x15
#define DW_FORM_indirect 0x16
#define DW_FORM_sec_offset 0x17
#define DW_FORM_exprloc 0x18
#define DW_FORM_flag_present 0x19
#define DW_FORM_strx 0x1a
#define DW_FORM_addrx 0x1b
#define DW_FORM_ref_sup4 0x1c
#define DW_FORM_strp_sup 0x1d
#define DW_FORM_data16 0x1e
#define DW_FORM_line_strp 0x1f
#define DW_FORM_ref_sig8 0x20
#define DW_FORM_implicit_const 0x21
#define DW_FORM_loclistx 0x22
#define DW_FORM_rnglistx 0x23
#define DW_FORM_ref_sup8 0x24
#define DW_FORM_strx1 0x25
#define DW_FORM_strx2 0x26
#define DW_FORM_strx3 0x27
#define DW_FORM_strx4 0x28
#define DW_FORM_addrx1 0x29
#define DW_FORM_addrx2 0x2a
#define DW_FORM_addrx3 0x2b
#define DW_FORM_addrx4 0x2c
#define DW_AT_name 0x03
#define DW_AT_byte_size 0x0b
#define DW_AT_bit_offset 0x0c
#define DW_AT_bit_size 0x0d
#define DW_AT_low_pc 0x11
#define DW_AT_high_pc 0x12
#define DW_AT_language 0x13
#define DW_AT_const_value 0x1c
#define DW_AT_upper_bound 0x2f
#define DW_AT_abstract_origin 0x31
#define DW_AT_count 0x37
#define DW_AT_data_member_location 0x38
#define DW_AT_encoding 0x3e
#define DW_AT_type 0x49
#define DW_AT_ranges 0x55
#define DW_AT_data_bit_offset 0x6b
#define DW_AT_str_offsets_base 0x72
#define DW_AT_addr_base 0x73
#define DW_AT_rnglists_base 0x74
#define DW_ATE_address 0x01
#define DW_ATE_boolean 0x02
#define DW_ATE_complex_float 0x03
#define DW_ATE_float 0x04
#define DW_ATE_signed 0x05
#define DW_ATE_signed_char 0x06
#define DW_ATE_unsigned 0x07
#define DW_ATE_unsigned_char 0x08
#define DW_ATE_imaginary_float 0x09
#define DW_ATE_packed_decimal 0x0a
#define DW_ATE_numeric_string 0x0b
#define DW_ATE_edited 0x0c
#define DW_ATE_signed_fixed 0x0d
#define DW_ATE_unsigned_fixed 0x0e
#define DW_ATE_decimal_float 0x0f
#define DW_ATE_UTF 0x10
#define DW_ATE_UCS 0x11
#define DW_ATE_ASCII 0x12
#define DW_RLE_end_of_list 0x00
#define DW_RLE_base_addressx 0x01
#define DW_RLE_startx_endx 0x02
#define DW_RLE_startx_length 0x03
#define DW_RLE_offset_pair 0x04
#define DW_RLE_base_address 0x05
#define DW_RLE_start_end 0x06
#define DW_RLE_start_length 0x07
#define DW_LANG_C 0x0002
#define DW_LANG_C89 0x0001
#define DW_LANG_C99 0x000c
#define DW_LANG_C11 0x001d
#define DW_LANG_C17 0x002c
// ===================== TESTING ==========================
#ifdef UPRINTF_TEST
int _upf_test_status = EXIT_SUCCESS;
#define _UPF_SET_TEST_STATUS(status) _upf_test_status = status
#else
#define _UPF_SET_TEST_STATUS(status) (void) status
#endif
// ====================== ERRORS ==========================
#define _UPF_LOG(type, ...) \
do { \
fprintf(stderr, "(uprintf) [%s] ", (type)); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
} while (0)
#define _UPF_WARN(...) \
do { \
_UPF_LOG("WARNING", __VA_ARGS__); \
_UPF_SET_TEST_STATUS(EXIT_FAILURE); \
} while (0)
#define _UPF_ERROR(...) \
do { \
_UPF_LOG("ERROR", __VA_ARGS__); \
_UPF_SET_TEST_STATUS(EXIT_FAILURE); \
longjmp(_upf_state.jmp_buf, EXIT_FAILURE); \
} while (0)
#define _UPF_ASSERT(condition) \
do { \
if (!(condition)) _UPF_ERROR("Assert (%s) failed at %s:%d.", #condition, __FILE__, __LINE__); \
} while (0)
#define _UPF_OUT_OF_MEMORY() _UPF_ERROR("Process ran out of memory.")
// ====================== VECTOR ==========================
#define _UPF_INITIAL_VECTOR_CAPACITY 4
#define _UPF_VECTOR_TYPEDEF(name, type) \
typedef struct { \
uint32_t capacity; \
uint32_t length; \
type *data; \
} name
#define _UPF_VECTOR_INIT(vec, a) \
do { \
(vec)->capacity = 0; \
(vec)->length = 0; \
(vec)->data = NULL; \
} while (0)
#define _UPF_VECTOR_PUSH(vec, element) \
do { \
if ((vec)->capacity == 0) { \
(vec)->capacity = _UPF_INITIAL_VECTOR_CAPACITY; \
uint32_t size = (vec)->capacity * sizeof(*(vec)->data); \
(vec)->data = _upf_arena_alloc(&_upf_state.arena, size); \
} else if ((vec)->capacity == (vec)->length) { \
uint32_t old_size = (vec)->capacity * sizeof(*(vec)->data); \
(vec)->capacity *= 2; \
void *new_data = _upf_arena_alloc(&_upf_state.arena, old_size * 2); \
memcpy(new_data, (vec)->data, old_size); \
(vec)->data = new_data; \
} \
(vec)->data[(vec)->length++] = (element); \
} while (0)
#define _UPF_VECTOR_COPY(dst, src) \
do { \
(dst)->capacity = (src)->length; \
(dst)->length = (src)->length; \
uint32_t size = (dst)->capacity * sizeof(*(dst)->data); \
(dst)->data = _upf_arena_alloc(&_upf_state.arena, size); \
memcpy((dst)->data, (src)->data, size); \
} while (0)
#define _UPF_VECTOR_TOP(vec) (vec)->data[(vec)->length - 1]
#define _UPF_VECTOR_POP(vec) (vec)->length--
// ====================== TYPES ===========================
typedef struct _upf_arena_region {
uint8_t *data;
size_t capacity;
size_t length;
struct _upf_arena_region *next;
} _upf_arena_region;
typedef struct {
_upf_arena_region *tail;
_upf_arena_region *head;
} _upf_arena;
_UPF_VECTOR_TYPEDEF(_upf_size_t_vec, size_t);
_UPF_VECTOR_TYPEDEF(_upf_cstr_vec, const char *);
typedef struct {
uint64_t name;
uint64_t form;
int64_t implicit_const;
} _upf_attr;
_UPF_VECTOR_TYPEDEF(_upf_attr_vec, _upf_attr);
typedef struct {
uint64_t code;
uint64_t tag;
bool has_children;
_upf_attr_vec attrs;
} _upf_abbrev;
_UPF_VECTOR_TYPEDEF(_upf_abbrev_vec, _upf_abbrev);
typedef struct _upf_type _upf_type;
_UPF_VECTOR_TYPEDEF(_upf_type_ptr_vec, struct _upf_type *);
enum _upf_type_kind {
_UPF_TK_STRUCT,
_UPF_TK_UNION,
_UPF_TK_ENUM,
_UPF_TK_ARRAY,
_UPF_TK_POINTER,
_UPF_TK_FUNCTION,
_UPF_TK_U1,
_UPF_TK_U2,
_UPF_TK_U4,
_UPF_TK_U8,
_UPF_TK_S1,
_UPF_TK_S2,
_UPF_TK_S4,
_UPF_TK_S8,
_UPF_TK_F4,
_UPF_TK_F8,
_UPF_TK_BOOL,
_UPF_TK_SCHAR,
_UPF_TK_UCHAR,
_UPF_TK_VOID,
_UPF_TK_UNKNOWN
};
typedef struct {
const char *name;
_upf_type *type;
size_t offset;
int bit_size; // non-zero means bit field
} _upf_member;
_UPF_VECTOR_TYPEDEF(_upf_member_vec, _upf_member);
typedef struct {
const char *name;
int64_t value;
} _upf_enum;
_UPF_VECTOR_TYPEDEF(_upf_enum_vec, _upf_enum);
#define _UPF_MOD_CONST 1 << 0
#define _UPF_MOD_VOLATILE 1 << 1
#define _UPF_MOD_RESTRICT 1 << 2
#define _UPF_MOD_ATOMIC 1 << 3
struct _upf_type {
const char *name;
enum _upf_type_kind kind;
int modifiers;
size_t size;
union {
struct {
_upf_member_vec members;
} cstruct;
struct {
_upf_type *underlying_type;
_upf_enum_vec enums;
} cenum;
struct {
_upf_type *element_type;
_upf_size_t_vec lengths;
} array;
struct {
_upf_type *type;
} pointer;
struct {
_upf_type *return_type;
_upf_type_ptr_vec arg_types;
} function;
} as;
};
typedef struct {
uint64_t start;
uint64_t end;
} _upf_range;
_UPF_VECTOR_TYPEDEF(_upf_range_vec, _upf_range);
typedef struct {
const uint8_t *die;
const char *name;
} _upf_named_type;
_UPF_VECTOR_TYPEDEF(_upf_named_type_vec, _upf_named_type);
typedef struct {
const char *name;
const uint8_t *return_type_die;
_upf_named_type_vec args;
bool is_variadic;
uint64_t pc;
} _upf_function;
_UPF_VECTOR_TYPEDEF(_upf_function_vec, _upf_function);
typedef struct {
const char *name;
uint64_t pc;
} _upf_extern_function;
_UPF_VECTOR_TYPEDEF(_upf_extern_function_vec, _upf_extern_function);
typedef struct {
const void *data;
const _upf_type *type;
bool is_visited;
int id;
} _upf_indexed_struct;
_UPF_VECTOR_TYPEDEF(_upf_indexed_struct_vec, _upf_indexed_struct);
struct _upf_scope;
_UPF_VECTOR_TYPEDEF(_upf_scope_vec, struct _upf_scope);
typedef struct _upf_scope {
_upf_range_vec ranges;
_upf_named_type_vec vars;
_upf_scope_vec scopes;
} _upf_scope;
typedef struct {
int depth;
_upf_scope *scope;
} _upf_scope_stack_entry;
_UPF_VECTOR_TYPEDEF(_upf_scope_stack, _upf_scope_stack_entry);
typedef struct {
const uint8_t *die;
_upf_type *type_ptr;
} _upf_type_map_entry;
_UPF_VECTOR_TYPEDEF(_upf_type_map_vec, _upf_type_map_entry);
typedef struct {
const uint8_t *base;
_upf_scope scope;
uint64_t addr_base;
uint64_t str_offsets_base;
uint64_t rnglists_base;
_upf_abbrev_vec abbrevs;
_upf_named_type_vec types;
_upf_function_vec functions;
} _upf_cu;
_UPF_VECTOR_TYPEDEF(_upf_cu_vec, _upf_cu);
enum _upf_token_kind {
_UPF_TOK_NONE,
_UPF_TOK_NUMBER,
_UPF_TOK_STRING,
_UPF_TOK_ID,
_UPF_TOK_TYPE_SPECIFIER,
_UPF_TOK_TYPE_QUALIFIER,
_UPF_TOK_OPEN_PAREN,
_UPF_TOK_CLOSE_PAREN,
_UPF_TOK_OPEN_BRACKET,
_UPF_TOK_CLOSE_BRACKET,
_UPF_TOK_COMMA,
_UPF_TOK_DOT,
_UPF_TOK_ARROW,
_UPF_TOK_INCREMENT,
_UPF_TOK_DECREMENT,
_UPF_TOK_PLUS,
_UPF_TOK_MINUS,
_UPF_TOK_STAR,
_UPF_TOK_EXCLAMATION,
_UPF_TOK_TILDE,
_UPF_TOK_AMPERSAND,
_UPF_TOK_QUESTION,
_UPF_TOK_COLON,
_UPF_TOK_ASSIGNMENT,
_UPF_TOK_COMPARISON,
_UPF_TOK_MATH
};
typedef struct {
enum _upf_token_kind kind;
const char *string;
} _upf_token;
_UPF_VECTOR_TYPEDEF(_upf_token_vec, _upf_token);
typedef struct {
_upf_token_vec tokens;
size_t idx;
} _upf_tokenizer;
enum _upf_base_type {
_UPF_BT_INT_TYPE,
_UPF_BT_TYPE,
_UPF_BT_VARIABLE,
_UPF_BT_FUNCTION,
};
typedef struct {
_upf_cu *cu;
int dereference;
int suffix_calls;
union {
const char *name;
_upf_type *type;
} base;
enum _upf_base_type base_type;
_upf_cstr_vec members;
} _upf_parser_state;
// =================== GLOBAL STATE =======================
struct _upf_state {
_upf_arena arena;
// has _upf_init finished
bool is_init;
// file loaded by dynamic linker (only with info required to run it)
const uint8_t *base;
// mmap-ed file (with debug info)
uint8_t *file;
off_t file_size;
// DWARF info
bool is64bit;
uint8_t offset_size;
uint8_t address_size;
// DWARF sections
const uint8_t *die;
size_t die_size;
const uint8_t *abbrev;
const char *str;
const char *line_str;
const uint8_t *str_offsets;
const uint8_t *addr;
const uint8_t *rnglists;
// parsed DWARF info
_upf_type_map_vec type_map;
_upf_cu_vec cus;
_upf_extern_function_vec extern_functions;
// sequential id for handling circular structs
int circular_id;
// valid memory regions
_upf_range_vec addresses;
// bprintf
char *buffer;
int size;
char *ptr;
int free;
// error handling
jmp_buf jmp_buf;
const char *file_path;
int line;
};
static struct _upf_state _upf_state = {0};
// ====================== ARENA ===========================
#define _UPF_INITIAL_ARENA_SIZE 65535
static _upf_arena_region *_upf_arena_alloc_region(size_t capacity) {
_upf_arena_region *region = (_upf_arena_region *) malloc(sizeof(*region));
if (region == NULL) _UPF_OUT_OF_MEMORY();
region->capacity = capacity;
region->data = (uint8_t *) malloc(region->capacity * sizeof(*region->data));
if (region->data == NULL) _UPF_OUT_OF_MEMORY();
region->length = 0;
region->next = NULL;
return region;
}
static void _upf_arena_init(_upf_arena *a) {
_UPF_ASSERT(a != NULL);
_upf_arena_region *region = _upf_arena_alloc_region(_UPF_INITIAL_ARENA_SIZE);
a->head = region;
a->tail = region;
}
static void *_upf_arena_alloc(_upf_arena *a, size_t size) {
_UPF_ASSERT(a != NULL && a->head != NULL && a->tail != NULL);
size_t alignment = (a->head->length % sizeof(void *));
if (alignment > 0) alignment = sizeof(void *) - alignment;
if (alignment + size > a->head->capacity - a->head->length) {
_upf_arena_region *region = _upf_arena_alloc_region(a->head->capacity * 2);
a->head->next = region;
a->head = region;
alignment = 0;
}
void *memory = a->head->data + a->head->length + alignment;
a->head->length += alignment + size;
return memory;
}
static void _upf_arena_free(_upf_arena *a) {
if (a == NULL || a->head == NULL || a->tail == NULL) return;
_upf_arena_region *region = a->tail;
while (region != NULL) {
_upf_arena_region *next = region->next;
free(region->data);
free(region);
region = next;
}
a->head = NULL;
a->tail = NULL;
}
// Copies [begin, end) to arena-allocated string
static char *_upf_arena_string(_upf_arena *a, const char *begin, const char *end) {
_UPF_ASSERT(a != NULL && begin != NULL && end != NULL);
size_t len = end - begin;
char *string = (char *) _upf_arena_alloc(a, len + 2);
memcpy(string, begin, len);
string[len] = '\0';
return string;
}
static char *_upf_arena_concat2(_upf_arena *a, ...) {
_UPF_ASSERT(a != NULL);
va_list va_args;
size_t size = 0;
va_start(va_args, a);
while (true) {
const char *str = va_arg(va_args, const char *);
if (str == NULL) break;
size += strlen(str);
}
va_end(va_args);
char *result = _upf_arena_alloc(a, size + 1);
result[size] = '\0';
char *p = result;
va_start(va_args, a);
while (true) {
const char *str = va_arg(va_args, const char *);
if (str == NULL) break;
size_t len = strlen(str);
memcpy(p, str, len);
p += len;
}
va_end(va_args);
return result;
}
#define _upf_arena_concat(a, ...) _upf_arena_concat2((a), __VA_ARGS__, NULL)
// ====================== DWARF ===========================
// Converts unsigned LEB128 to uint64_t and returns the size of LEB in bytes
static size_t _upf_uLEB_to_uint64(const uint8_t *leb, uint64_t *result) {
_UPF_ASSERT(leb != NULL && result != NULL);
static const uint8_t BITS_MASK = 0x7f; // 01111111
static const uint8_t CONTINUE_MASK = 0x80; // 10000000
size_t i = 0;
int shift = 0;
*result = 0;
while (true) {
uint8_t b = leb[i++];
*result |= (((uint64_t) (b & BITS_MASK)) << shift);
if ((b & CONTINUE_MASK) == 0) break;
shift += 7;
}
return i;
}
// Converts signed LEB128 to int64_t and returns the size of LEB in bytes
static size_t _upf_LEB_to_int64(const uint8_t *leb, int64_t *result) {
_UPF_ASSERT(leb != NULL && result != NULL);
static const uint8_t BITS_MASK = 0x7f; // 01111111
static const uint8_t CONTINUE_MASK = 0x80; // 10000000
static const uint8_t SIGN_MASK = 0x40; // 01000000
size_t i = 0;
uint8_t b = 0;
size_t shift = 0;
*result = 0;
while (true) {
b = leb[i++];
*result |= (((uint64_t) (b & BITS_MASK)) << shift);
shift += 7;
if ((b & CONTINUE_MASK) == 0) break;
}
if ((shift < sizeof(*result) * 8) && (b & SIGN_MASK)) *result |= -(1 << shift);
return i;
}
static bool _upf_is_primitive(const _upf_type *type) {
_UPF_ASSERT(type != NULL);
switch (type->kind) {
case _UPF_TK_STRUCT:
case _UPF_TK_UNION:
case _UPF_TK_ARRAY:
return false;
case _UPF_TK_FUNCTION:
case _UPF_TK_ENUM:
case _UPF_TK_POINTER:
case _UPF_TK_U1:
case _UPF_TK_U2:
case _UPF_TK_U4:
case _UPF_TK_U8:
case _UPF_TK_S1:
case _UPF_TK_S2:
case _UPF_TK_S4:
case _UPF_TK_S8:
case _UPF_TK_F4:
case _UPF_TK_F8:
case _UPF_TK_BOOL:
case _UPF_TK_SCHAR:
case _UPF_TK_UCHAR:
case _UPF_TK_VOID:
case _UPF_TK_UNKNOWN:
return true;
}
_UPF_ERROR("Invalid type: %d.", type->kind);
}
static uint64_t _upf_offset_cast(const uint8_t *die) {
_UPF_ASSERT(die != NULL);
uint64_t offset = 0;
memcpy(&offset, die, _upf_state.offset_size);
return offset;
}
static uint64_t _upf_address_cast(const uint8_t *die) {
_UPF_ASSERT(die != NULL);
uint64_t address = 0;
memcpy(&address, die, _upf_state.address_size);
return address;
}
static size_t _upf_get_attr_size(const uint8_t *die, uint64_t form) {
_UPF_ASSERT(die != NULL);
switch (form) {
case DW_FORM_addr:
return _upf_state.address_size;
case DW_FORM_strx1:
case DW_FORM_addrx1:
case DW_FORM_flag:
case DW_FORM_ref1:
case DW_FORM_data1:
return 1;
case DW_FORM_strx2:
case DW_FORM_addrx2:
case DW_FORM_ref2:
case DW_FORM_data2:
return 2;
case DW_FORM_strx3:
case DW_FORM_addrx3:
return 3;
case DW_FORM_ref_sup4:
case DW_FORM_strx4:
case DW_FORM_addrx4:
case DW_FORM_ref4:
case DW_FORM_data4:
return 4;
case DW_FORM_ref_sig8:
case DW_FORM_ref_sup8:
case DW_FORM_ref8:
case DW_FORM_data8:
return 8;
case DW_FORM_data16:
return 16;
case DW_FORM_block1: {
uint8_t length;
memcpy(&length, die, sizeof(length));
return sizeof(length) + length;
}
case DW_FORM_block2: {
uint16_t length;
memcpy(&length, die, sizeof(length));
return sizeof(length) + length;
}
case DW_FORM_block4: {
uint32_t length;
memcpy(&length, die, sizeof(length));
return sizeof(length) + length;
}
case DW_FORM_sdata: {
int64_t result;
return _upf_LEB_to_int64(die, &result);
} break;
case DW_FORM_loclistx:
case DW_FORM_rnglistx:
case DW_FORM_addrx:
case DW_FORM_strx:
case DW_FORM_ref_udata:
case DW_FORM_udata: {
uint64_t result;
return _upf_uLEB_to_uint64(die, &result);
} break;
case DW_FORM_string:
return strlen((const char *) die) + 1;
case DW_FORM_exprloc:
case DW_FORM_block: {
uint64_t length;
size_t leb_size = _upf_uLEB_to_uint64(die, &length);
return leb_size + length;
} break;
case DW_FORM_line_strp:
case DW_FORM_strp_sup:
case DW_FORM_sec_offset:
case DW_FORM_ref_addr:
case DW_FORM_strp:
return _upf_state.offset_size;
case DW_FORM_indirect: {
uint64_t form;
size_t offset = _upf_uLEB_to_uint64(die, &form);
return _upf_get_attr_size(die + offset, form);
} break;
case DW_FORM_flag_present:
case DW_FORM_implicit_const:
return 0;
}
_UPF_ERROR("Invalid DWARF attribute type(form): %lu.", form);
}
static uint64_t _upf_get_x_offset(const uint8_t *die, uint64_t form) {
_UPF_ASSERT(die != NULL);
uint64_t offset = 0;
switch (form) {
case DW_FORM_strx1:
case DW_FORM_addrx1:
memcpy(&offset, die, 1);
return offset;
case DW_FORM_strx2:
case DW_FORM_addrx2:
memcpy(&offset, die, 2);
return offset;
case DW_FORM_strx3:
case DW_FORM_addrx3:
memcpy(&offset, die, 3);
return offset;
case DW_FORM_strx4:
case DW_FORM_addrx4:
memcpy(&offset, die, 4);
return offset;
case DW_FORM_addrx:
case DW_FORM_strx:
_upf_uLEB_to_uint64(die, &offset);
return offset;
}
_UPF_ERROR("Invalid DWARF x-* type(form): %lu.", form);
}
static const char *_upf_get_str(const _upf_cu *cu, const uint8_t *die, uint64_t form) {
_UPF_ASSERT(cu != NULL && die != NULL);
switch (form) {
case DW_FORM_strp:
return _upf_state.str + _upf_offset_cast(die);
case DW_FORM_line_strp:
_UPF_ASSERT(_upf_state.line_str != NULL);
return _upf_state.line_str + _upf_offset_cast(die);
case DW_FORM_string:
return (const char *) die;
case DW_FORM_strx:
case DW_FORM_strx1:
case DW_FORM_strx2:
case DW_FORM_strx3:
case DW_FORM_strx4: {
_UPF_ASSERT(_upf_state.str_offsets != NULL && cu->str_offsets_base != UINT64_MAX);
uint64_t offset = _upf_get_x_offset(die, form) * _upf_state.offset_size;
return _upf_state.str + _upf_offset_cast(_upf_state.str_offsets + cu->str_offsets_base + offset);
}
}
_UPF_ERROR("Invalid DWARF string type(form): %lu.", form);
}
static uint64_t _upf_get_ref(const uint8_t *die, uint64_t form) {
_UPF_ASSERT(die != NULL);
uint64_t ref = 0;
switch (form) {
case DW_FORM_ref1:
case DW_FORM_ref2:
case DW_FORM_ref4:
case DW_FORM_ref8:
memcpy(&ref, die, _upf_get_attr_size(die, form));
return ref;
case DW_FORM_ref_udata:
_upf_uLEB_to_uint64(die, &ref);
return ref;
}
_UPF_ERROR("Only references within single compilation unit are supported.");
}
static bool _upf_is_data(uint64_t form) {
switch (form) {
case DW_FORM_data1: