forked from sellicott/tcc-riscv32
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tccrun.c
1375 lines (1277 loc) · 38 KB
/
tccrun.c
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
/*
* TCC - Tiny C Compiler - Support for -run switch
*
* Copyright (c) 2001-2004 Fabrice Bellard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tcc.h"
/* only native compiler supports -run */
#ifdef TCC_IS_NATIVE
#ifdef CONFIG_TCC_BACKTRACE
typedef struct rt_context
{
/* --> tccelf.c:tcc_add_btstub wants those below in that order: */
union {
struct {
Stab_Sym *stab_sym, *stab_sym_end;
char *stab_str;
};
struct {
unsigned char *dwarf_line, *dwarf_line_end, *dwarf_line_str;
};
};
addr_t dwarf;
ElfW(Sym) *esym_start, *esym_end;
char *elf_str;
addr_t prog_base;
void *bounds_start;
struct rt_context *next;
/* <-- */
int num_callers;
addr_t ip, fp, sp;
void *top_func;
jmp_buf jmp_buf;
char do_jmp;
} rt_context;
static rt_context g_rtctxt;
static void set_exception_handler(void);
static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap);
static void rt_exit(int code);
#endif /* CONFIG_TCC_BACKTRACE */
/* defined when included from lib/bt-exe.c */
#ifndef CONFIG_TCC_BACKTRACE_ONLY
#ifndef _WIN32
# include <sys/mman.h>
#endif
static void set_pages_executable(TCCState *s1, int mode, void *ptr, unsigned long length);
static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff);
#ifdef _WIN64
static void *win64_add_function_table(TCCState *s1);
static void win64_del_function_table(void *);
#endif
/* ------------------------------------------------------------- */
/* Do all relocations (needed before using tcc_get_symbol())
Returns -1 on error. */
LIBTCCAPI int tcc_relocate(TCCState *s1, void *ptr)
{
int size;
addr_t ptr_diff = 0;
if (TCC_RELOCATE_AUTO != ptr)
return tcc_relocate_ex(s1, ptr, 0);
size = tcc_relocate_ex(s1, NULL, 0);
if (size < 0)
return -1;
#ifdef HAVE_SELINUX
{
/* Using mmap instead of malloc */
void *prx;
char tmpfname[] = "/tmp/.tccrunXXXXXX";
int fd = mkstemp(tmpfname);
unlink(tmpfname);
ftruncate(fd, size);
size = (size + (PAGESIZE-1)) & ~(PAGESIZE-1);
ptr = mmap(NULL, size * 2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
/* mmap RX memory at a fixed distance */
prx = mmap((char*)ptr + size, size, PROT_READ|PROT_EXEC, MAP_SHARED|MAP_FIXED, fd, 0);
if (ptr == MAP_FAILED || prx == MAP_FAILED)
tcc_error("tccrun: could not map memory");
ptr_diff = (char*)prx - (char*)ptr;
close(fd);
//printf("map %p %p %p\n", ptr, prx, (void*)ptr_diff);
}
#else
ptr = tcc_malloc(size);
#endif
tcc_relocate_ex(s1, ptr, ptr_diff); /* no more errors expected */
dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, (void*)(addr_t)size);
dynarray_add(&s1->runtime_mem, &s1->nb_runtime_mem, ptr);
return 0;
}
ST_FUNC void tcc_run_free(TCCState *s1)
{
int i;
for (i = 0; i < s1->nb_runtime_mem; i += 2) {
unsigned size = (unsigned)(addr_t)s1->runtime_mem[i];
void *ptr = s1->runtime_mem[i+1];
#ifdef HAVE_SELINUX
munmap(ptr, size * 2);
#else
/* unprotect memory to make it usable for malloc again */
set_pages_executable(s1, 2, ptr, size);
#ifdef _WIN64
win64_del_function_table(*(void**)ptr);
#endif
tcc_free(ptr);
#endif
}
tcc_free(s1->runtime_mem);
}
static void run_cdtors(TCCState *s1, const char *start, const char *end,
int argc, char **argv, char **envp)
{
void **a = (void **)get_sym_addr(s1, start, 0, 0);
void **b = (void **)get_sym_addr(s1, end, 0, 0);
while (a != b)
((void(*)(int, char **, char **))*a++)(argc, argv, envp);
}
/* launch the compiled program with the given arguments */
LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
{
int (*prog_main)(int, char **, char **), ret;
#ifdef CONFIG_TCC_BACKTRACE
rt_context *rc = &g_rtctxt;
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
char **envp = NULL;
#elif defined(__OpenBSD__) || defined(__NetBSD__)
extern char **environ;
char **envp = environ;
#else
char **envp = environ;
#endif
s1->runtime_main = s1->nostdlib ? "_start" : "main";
if ((s1->dflag & 16) && (addr_t)-1 == get_sym_addr(s1, s1->runtime_main, 0, 1))
return 0;
#ifdef CONFIG_TCC_BACKTRACE
if (s1->do_debug)
tcc_add_symbol(s1, "exit", rt_exit);
#endif
if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0)
return -1;
prog_main = (void*)get_sym_addr(s1, s1->runtime_main, 1, 1);
#ifdef CONFIG_TCC_BACKTRACE
memset(rc, 0, sizeof *rc);
if (s1->do_debug) {
void *p;
if (s1->dwarf) {
rc->dwarf_line = dwarf_line_section->data;
rc->dwarf_line_end = dwarf_line_section->data + dwarf_line_section->data_offset;
if (dwarf_line_str_section)
rc->dwarf_line_str = dwarf_line_str_section->data;
}
else
{
rc->stab_sym = (Stab_Sym *)stab_section->data;
rc->stab_sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
rc->stab_str = (char *)stab_section->link->data;
}
rc->dwarf = s1->dwarf;
rc->esym_start = (ElfW(Sym) *)(symtab_section->data);
rc->esym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
rc->elf_str = (char *)symtab_section->link->data;
#if PTR_SIZE == 8
rc->prog_base = text_section->sh_addr & 0xffffffff00000000ULL;
#endif
rc->top_func = tcc_get_symbol(s1, "main");
rc->num_callers = s1->rt_num_callers;
rc->do_jmp = 1;
if ((p = tcc_get_symbol(s1, "__rt_error")))
*(void**)p = _rt_error;
#ifdef CONFIG_TCC_BCHECK
if (s1->do_bounds_check) {
rc->bounds_start = (void*)bounds_section->sh_addr;
if ((p = tcc_get_symbol(s1, "__bound_init")))
((void(*)(void*,int))p)(rc->bounds_start, 1);
}
#endif
set_exception_handler();
}
#endif
errno = 0; /* clean errno value */
fflush(stdout);
fflush(stderr);
/* These aren't C symbols, so don't need leading underscore handling. */
run_cdtors(s1, "__init_array_start", "__init_array_end", argc, argv, envp);
#ifdef CONFIG_TCC_BACKTRACE
if (!rc->do_jmp || !(ret = setjmp(rc->jmp_buf)))
#endif
{
ret = prog_main(argc, argv, envp);
}
run_cdtors(s1, "__fini_array_start", "__fini_array_end", 0, NULL, NULL);
if ((s1->dflag & 16) && ret)
fprintf(s1->ppfp, "[returns %d]\n", ret), fflush(s1->ppfp);
return ret;
}
#define DEBUG_RUNMEN 0
/* enable rx/ro/rw permissions */
#define CONFIG_RUNMEM_RO 1
#if CONFIG_RUNMEM_RO
# define PAGE_ALIGN PAGESIZE
#elif defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
/* To avoid that x86 processors would reload cached instructions
each time when data is written in the near, we need to make
sure that code and data do not share the same 64 byte unit */
# define PAGE_ALIGN 64
#else
# define PAGE_ALIGN 1
#endif
/* relocate code. Return -1 on error, required size if ptr is NULL,
otherwise copy code into buffer passed by the caller */
static int tcc_relocate_ex(TCCState *s1, void *ptr, addr_t ptr_diff)
{
Section *s;
unsigned offset, length, align, max_align, i, k, f;
unsigned n, copy;
addr_t mem, addr;
if (NULL == ptr) {
s1->nb_errors = 0;
#ifdef TCC_TARGET_PE
pe_output_file(s1, NULL);
#else
tcc_add_runtime(s1);
resolve_common_syms(s1);
build_got_entries(s1, 0);
#endif
if (s1->nb_errors)
return -1;
}
offset = max_align = 0, mem = (addr_t)ptr;
#ifdef _WIN64
offset += sizeof (void*); /* space for function_table pointer */
#endif
copy = 0;
redo:
for (k = 0; k < 3; ++k) { /* 0:rx, 1:ro, 2:rw sections */
n = 0; addr = 0;
for(i = 1; i < s1->nb_sections; i++) {
static const char shf[] = {
SHF_ALLOC|SHF_EXECINSTR, SHF_ALLOC, SHF_ALLOC|SHF_WRITE
};
s = s1->sections[i];
if (shf[k] != (s->sh_flags & (SHF_ALLOC|SHF_WRITE|SHF_EXECINSTR)))
continue;
length = s->data_offset;
if (copy) {
if (addr == 0)
addr = s->sh_addr;
n = (s->sh_addr - addr) + length;
ptr = (void*)s->sh_addr;
if (k == 0)
ptr = (void*)(s->sh_addr - ptr_diff);
if (NULL == s->data || s->sh_type == SHT_NOBITS)
memset(ptr, 0, length);
else
memcpy(ptr, s->data, length);
#ifdef _WIN64
if (s == s1->uw_pdata)
*(void**)mem = win64_add_function_table(s1);
#endif
if (s->data) {
tcc_free(s->data);
s->data = NULL;
s->data_allocated = 0;
}
s->data_offset = 0;
continue;
}
align = s->sh_addralign - 1;
if (++n == 1 && align < (PAGE_ALIGN - 1))
align = (PAGE_ALIGN - 1);
if (max_align < align)
max_align = align;
addr = k ? mem : mem + ptr_diff;
offset += -(addr + offset) & align;
s->sh_addr = mem ? addr + offset : 0;
offset += length;
#if DEBUG_RUNMEN
if (mem)
printf("%d: %-16s %p len %04x align %04x\n",
k, s->name, (void*)s->sh_addr, length, align + 1);
#endif
}
if (copy) { /* set permissions */
if (k == 0 && ptr_diff)
continue; /* not with HAVE_SELINUX */
f = k;
#if !CONFIG_RUNMEM_RO
if (f != 0)
continue;
f = 3; /* change only SHF_EXECINSTR to rwx */
#endif
#if DEBUG_RUNMEN
printf("protect %d %p %04x\n", f, (void*)addr, n);
#endif
if (n)
set_pages_executable(s1, f, (void*)addr, n);
}
}
if (copy)
return 0;
/* relocate symbols */
relocate_syms(s1, s1->symtab, !(s1->nostdlib));
if (s1->nb_errors)
return -1;
if (0 == mem)
return offset + max_align;
#ifdef TCC_TARGET_PE
s1->pe_imagebase = mem;
#endif
/* relocate sections */
#ifndef TCC_TARGET_PE
relocate_plt(s1);
#endif
relocate_sections(s1);
copy = 1;
goto redo;
}
/* ------------------------------------------------------------- */
/* allow to run code in memory */
static void set_pages_executable(TCCState *s1, int mode, void *ptr, unsigned long length)
{
#ifdef _WIN32
static const unsigned char protect[] = {
PAGE_EXECUTE_READ,
PAGE_READONLY,
PAGE_READWRITE,
PAGE_EXECUTE_READWRITE
};
DWORD old;
VirtualProtect(ptr, length, protect[mode], &old);
#else
static const unsigned char protect[] = {
PROT_READ | PROT_EXEC,
PROT_READ,
PROT_READ | PROT_WRITE,
PROT_READ | PROT_WRITE | PROT_EXEC
};
addr_t start, end;
start = (addr_t)ptr & ~(PAGESIZE - 1);
end = (addr_t)ptr + length;
end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
if (mprotect((void *)start, end - start, protect[mode]))
tcc_error("mprotect failed: did you mean to configure --with-selinux?");
/* XXX: BSD sometimes dump core with bad system call */
# if (defined TCC_TARGET_ARM && !TARGETOS_BSD) || defined TCC_TARGET_ARM64
if (mode == 0 || mode == 3) {
void __clear_cache(void *beginning, void *end);
__clear_cache(ptr, (char *)ptr + length);
}
# endif
#endif
}
#ifdef _WIN64
static void *win64_add_function_table(TCCState *s1)
{
void *p = NULL;
if (s1->uw_pdata) {
p = (void*)s1->uw_pdata->sh_addr;
RtlAddFunctionTable(
(RUNTIME_FUNCTION*)p,
s1->uw_pdata->data_offset / sizeof (RUNTIME_FUNCTION),
s1->pe_imagebase
);
s1->uw_pdata = NULL;
}
return p;
}
static void win64_del_function_table(void *p)
{
if (p) {
RtlDeleteFunctionTable((RUNTIME_FUNCTION*)p);
}
}
#endif
#endif //ndef CONFIG_TCC_BACKTRACE_ONLY
/* ------------------------------------------------------------- */
#ifdef CONFIG_TCC_BACKTRACE
static int rt_vprintf(const char *fmt, va_list ap)
{
int ret = vfprintf(stderr, fmt, ap);
fflush(stderr);
return ret;
}
static int rt_printf(const char *fmt, ...)
{
va_list ap;
int r;
va_start(ap, fmt);
r = rt_vprintf(fmt, ap);
va_end(ap);
return r;
}
static char *rt_elfsym(rt_context *rc, addr_t wanted_pc, addr_t *func_addr)
{
ElfW(Sym) *esym;
for (esym = rc->esym_start + 1; esym < rc->esym_end; ++esym) {
int type = ELFW(ST_TYPE)(esym->st_info);
if ((type == STT_FUNC || type == STT_GNU_IFUNC)
&& wanted_pc >= esym->st_value
&& wanted_pc < esym->st_value + esym->st_size) {
*func_addr = esym->st_value;
return rc->elf_str + esym->st_name;
}
}
return NULL;
}
#define INCLUDE_STACK_SIZE 32
/* print the position in the source file of PC value 'pc' by reading
the stabs debug information */
static addr_t rt_printline (rt_context *rc, addr_t wanted_pc,
const char *msg, const char *skip)
{
char func_name[128];
addr_t func_addr, last_pc, pc;
const char *incl_files[INCLUDE_STACK_SIZE];
int incl_index, last_incl_index, len, last_line_num, i;
const char *str, *p;
Stab_Sym *sym;
next:
func_name[0] = '\0';
func_addr = 0;
incl_index = 0;
last_pc = (addr_t)-1;
last_line_num = 1;
last_incl_index = 0;
for (sym = rc->stab_sym + 1; sym < rc->stab_sym_end; ++sym) {
str = rc->stab_str + sym->n_strx;
pc = sym->n_value;
switch(sym->n_type) {
case N_SLINE:
if (func_addr)
goto rel_pc;
case N_SO:
case N_SOL:
goto abs_pc;
case N_FUN:
if (sym->n_strx == 0) /* end of function */
goto rel_pc;
abs_pc:
#if PTR_SIZE == 8
/* Stab_Sym.n_value is only 32bits */
pc += rc->prog_base;
#endif
goto check_pc;
rel_pc:
pc += func_addr;
check_pc:
if (pc >= wanted_pc && wanted_pc >= last_pc)
goto found;
break;
}
switch(sym->n_type) {
/* function start or end */
case N_FUN:
if (sym->n_strx == 0)
goto reset_func;
p = strchr(str, ':');
if (0 == p || (len = p - str + 1, len > sizeof func_name))
len = sizeof func_name;
pstrcpy(func_name, len, str);
func_addr = pc;
break;
/* line number info */
case N_SLINE:
last_pc = pc;
last_line_num = sym->n_desc;
last_incl_index = incl_index;
break;
/* include files */
case N_BINCL:
if (incl_index < INCLUDE_STACK_SIZE)
incl_files[incl_index++] = str;
break;
case N_EINCL:
if (incl_index > 1)
incl_index--;
break;
/* start/end of translation unit */
case N_SO:
incl_index = 0;
if (sym->n_strx) {
/* do not add path */
len = strlen(str);
if (len > 0 && str[len - 1] != '/')
incl_files[incl_index++] = str;
}
reset_func:
func_name[0] = '\0';
func_addr = 0;
last_pc = (addr_t)-1;
break;
/* alternative file name (from #line or #include directives) */
case N_SOL:
if (incl_index)
incl_files[incl_index-1] = str;
break;
}
}
func_name[0] = '\0';
func_addr = 0;
last_incl_index = 0;
/* we try symtab symbols (no line number info) */
p = rt_elfsym(rc, wanted_pc, &func_addr);
if (p) {
pstrcpy(func_name, sizeof func_name, p);
goto found;
}
if ((rc = rc->next))
goto next;
found:
i = last_incl_index;
if (i > 0) {
str = incl_files[--i];
if (skip[0] && strstr(str, skip))
return (addr_t)-1;
rt_printf("%s:%d: ", str, last_line_num);
} else
rt_printf("%08llx : ", (long long)wanted_pc);
rt_printf("%s %s", msg, func_name[0] ? func_name : "???");
#if 0
if (--i >= 0) {
rt_printf(" (included from ");
for (;;) {
rt_printf("%s", incl_files[i]);
if (--i < 0)
break;
rt_printf(", ");
}
rt_printf(")");
}
#endif
return func_addr;
}
/* ------------------------------------------------------------- */
/* rt_printline - dwarf version */
#define MAX_128 ((8 * sizeof (long long) + 6) / 7)
#define DIR_TABLE_SIZE (64)
#define FILE_TABLE_SIZE (512)
#define dwarf_read_1(ln,end) \
((ln) < (end) ? *(ln)++ : 0)
#define dwarf_read_2(ln,end) \
((ln) + 2 < (end) ? (ln) += 2, read16le((ln) - 2) : 0)
#define dwarf_read_4(ln,end) \
((ln) + 4 < (end) ? (ln) += 4, read32le((ln) - 4) : 0)
#define dwarf_read_8(ln,end) \
((ln) + 8 < (end) ? (ln) += 8, read64le((ln) - 8) : 0)
#define dwarf_ignore_type(ln, end) /* timestamp/size/md5/... */ \
switch (entry_format[j].form) { \
case DW_FORM_data1: (ln) += 1; break; \
case DW_FORM_data2: (ln) += 2; break; \
case DW_FORM_data4: (ln) += 3; break; \
case DW_FORM_data8: (ln) += 8; break; \
case DW_FORM_data16: (ln) += 16; break; \
case DW_FORM_udata: dwarf_read_uleb128(&(ln), (end)); break; \
default: goto next_line; \
}
static unsigned long long
dwarf_read_uleb128(unsigned char **ln, unsigned char *end)
{
unsigned char *cp = *ln;
unsigned long long retval = 0;
int i;
for (i = 0; i < MAX_128; i++) {
unsigned long long byte = dwarf_read_1(cp, end);
retval |= (byte & 0x7f) << (i * 7);
if ((byte & 0x80) == 0)
break;
}
*ln = cp;
return retval;
}
static long long
dwarf_read_sleb128(unsigned char **ln, unsigned char *end)
{
unsigned char *cp = *ln;
long long retval = 0;
int i;
for (i = 0; i < MAX_128; i++) {
unsigned long long byte = dwarf_read_1(cp, end);
retval |= (byte & 0x7f) << (i * 7);
if ((byte & 0x80) == 0) {
if ((byte & 0x40) && (i + 1) * 7 < 64)
retval |= -1LL << ((i + 1) * 7);
break;
}
}
*ln = cp;
return retval;
}
static addr_t rt_printline_dwarf (rt_context *rc, addr_t wanted_pc,
const char *msg, const char *skip)
{
unsigned char *ln;
unsigned char *cp;
unsigned char *end;
unsigned char *opcode_length;
unsigned long long size;
unsigned int length;
unsigned char version;
unsigned int min_insn_length;
unsigned int max_ops_per_insn;
int line_base;
unsigned int line_range;
unsigned int opcode_base;
unsigned int opindex;
unsigned int col;
unsigned int i;
unsigned int j;
unsigned int len;
unsigned long long value;
struct {
unsigned int type;
unsigned int form;
} entry_format[256];
unsigned int dir_size;
#if 0
char *dirs[DIR_TABLE_SIZE];
#endif
unsigned int filename_size;
struct dwarf_filename_struct {
unsigned int dir_entry;
char *name;
} filename_table[FILE_TABLE_SIZE];
addr_t last_pc;
addr_t pc;
addr_t func_addr;
int line;
char *filename;
char *function;
next:
ln = rc->dwarf_line;
while (ln < rc->dwarf_line_end) {
dir_size = 0;
filename_size = 0;
last_pc = 0;
pc = 0;
func_addr = 0;
line = 1;
filename = NULL;
function = NULL;
length = 4;
size = dwarf_read_4(ln, rc->dwarf_line_end);
if (size == 0xffffffffu) // dwarf 64
length = 8, size = dwarf_read_8(ln, rc->dwarf_line_end);
end = ln + size;
if (end < ln || end > rc->dwarf_line_end)
break;
version = dwarf_read_2(ln, end);
if (version >= 5)
ln += length + 2; // address size, segment selector, prologue Length
else
ln += length; // prologue Length
min_insn_length = dwarf_read_1(ln, end);
if (version >= 4)
max_ops_per_insn = dwarf_read_1(ln, end);
else
max_ops_per_insn = 1;
ln++; // Initial value of 'is_stmt'
line_base = dwarf_read_1(ln, end);
line_base |= line_base >= 0x80 ? ~0xff : 0;
line_range = dwarf_read_1(ln, end);
opcode_base = dwarf_read_1(ln, end);
opcode_length = ln;
ln += opcode_base - 1;
opindex = 0;
if (version >= 5) {
col = dwarf_read_1(ln, end);
for (i = 0; i < col; i++) {
entry_format[i].type = dwarf_read_uleb128(&ln, end);
entry_format[i].form = dwarf_read_uleb128(&ln, end);
}
dir_size = dwarf_read_uleb128(&ln, end);
for (i = 0; i < dir_size; i++) {
for (j = 0; j < col; j++) {
if (entry_format[j].type == DW_LNCT_path) {
if (entry_format[j].form != DW_FORM_line_strp)
goto next_line;
#if 0
value = length == 4 ? dwarf_read_4(ln, end)
: dwarf_read_8(ln, end);
if (i < DIR_TABLE_SIZE)
dirs[i] = (char *)rc->dwarf_line_str + value;
#else
length == 4 ? dwarf_read_4(ln, end)
: dwarf_read_8(ln, end);
#endif
}
else
dwarf_ignore_type(ln, end);
}
}
col = dwarf_read_1(ln, end);
for (i = 0; i < col; i++) {
entry_format[i].type = dwarf_read_uleb128(&ln, end);
entry_format[i].form = dwarf_read_uleb128(&ln, end);
}
filename_size = dwarf_read_uleb128(&ln, end);
for (i = 0; i < filename_size; i++)
for (j = 0; j < col; j++) {
if (entry_format[j].type == DW_LNCT_path) {
if (entry_format[j].form != DW_FORM_line_strp)
goto next_line;
value = length == 4 ? dwarf_read_4(ln, end)
: dwarf_read_8(ln, end);
if (i < FILE_TABLE_SIZE)
filename_table[i].name =
(char *)rc->dwarf_line_str + value;
}
else if (entry_format[j].type == DW_LNCT_directory_index) {
switch (entry_format[j].form) {
case DW_FORM_data1: value = dwarf_read_1(ln, end); break;
case DW_FORM_data2: value = dwarf_read_2(ln, end); break;
case DW_FORM_data4: value = dwarf_read_4(ln, end); break;
case DW_FORM_udata: value = dwarf_read_uleb128(&ln, end); break;
default: goto next_line;
}
if (i < FILE_TABLE_SIZE)
filename_table[i].dir_entry = value;
}
else
dwarf_ignore_type(ln, end);
}
}
else {
while ((dwarf_read_1(ln, end))) {
#if 0
if (++dir_size < DIR_TABLE_SIZE)
dirs[dir_size - 1] = (char *)ln - 1;
#endif
while (dwarf_read_1(ln, end)) {}
}
while ((dwarf_read_1(ln, end))) {
if (++filename_size < FILE_TABLE_SIZE) {
filename_table[filename_size - 1].name = (char *)ln - 1;
while (dwarf_read_1(ln, end)) {}
filename_table[filename_size - 1].dir_entry =
dwarf_read_uleb128(&ln, end);
}
else {
while (dwarf_read_1(ln, end)) {}
dwarf_read_uleb128(&ln, end);
}
dwarf_read_uleb128(&ln, end); // time
dwarf_read_uleb128(&ln, end); // size
}
}
if (filename_size >= 1)
filename = filename_table[0].name;
while (ln < end) {
last_pc = pc;
i = dwarf_read_1(ln, end);
if (i >= opcode_base) {
if (max_ops_per_insn == 1)
pc += ((i - opcode_base) / line_range) * min_insn_length;
else {
pc += (opindex + (i - opcode_base) / line_range) /
max_ops_per_insn * min_insn_length;
opindex = (opindex + (i - opcode_base) / line_range) %
max_ops_per_insn;
}
i = (int)((i - opcode_base) % line_range) + line_base;
check_pc:
if (pc >= wanted_pc && wanted_pc >= last_pc)
goto found;
line += i;
}
else {
switch (i) {
case 0:
len = dwarf_read_uleb128(&ln, end);
cp = ln;
ln += len;
if (len == 0)
goto next_line;
switch (dwarf_read_1(cp, end)) {
case DW_LNE_end_sequence:
break;
case DW_LNE_set_address:
#if PTR_SIZE == 4
pc = dwarf_read_4(cp, end);
#else
pc = dwarf_read_8(cp, end);
#endif
opindex = 0;
break;
case DW_LNE_define_file: /* deprecated */
if (++filename_size < FILE_TABLE_SIZE) {
filename_table[filename_size - 1].name = (char *)ln - 1;
while (dwarf_read_1(ln, end)) {}
filename_table[filename_size - 1].dir_entry =
dwarf_read_uleb128(&ln, end);
}
else {
while (dwarf_read_1(ln, end)) {}
dwarf_read_uleb128(&ln, end);
}
dwarf_read_uleb128(&ln, end); // time
dwarf_read_uleb128(&ln, end); // size
break;
case DW_LNE_hi_user - 1:
function = (char *)cp;
func_addr = pc;
break;
default:
break;
}
break;
case DW_LNS_advance_pc:
if (max_ops_per_insn == 1)
pc += dwarf_read_uleb128(&ln, end) * min_insn_length;
else {
unsigned long long off = dwarf_read_uleb128(&ln, end);
pc += (opindex + off) / max_ops_per_insn *
min_insn_length;
opindex = (opindex + off) % max_ops_per_insn;
}
i = 0;
goto check_pc;
case DW_LNS_advance_line:
line += dwarf_read_sleb128(&ln, end);
break;
case DW_LNS_set_file:
i = dwarf_read_uleb128(&ln, end);
if (i < FILE_TABLE_SIZE && i < filename_size)
filename = filename_table[i].name;
break;
case DW_LNS_const_add_pc:
if (max_ops_per_insn == 1)
pc += ((255 - opcode_base) / line_range) * min_insn_length;
else {
unsigned int off = (255 - opcode_base) / line_range;
pc += ((opindex + off) / max_ops_per_insn) *
min_insn_length;
opindex = (opindex + off) % max_ops_per_insn;
}
i = 0;
goto check_pc;
case DW_LNS_fixed_advance_pc:
i = dwarf_read_2(ln, end);
pc += i;
opindex = 0;
i = 0;
goto check_pc;
default:
for (j = 0; j < opcode_length[i - 1]; j++)
dwarf_read_uleb128 (&ln, end);
break;
}
}
}
next_line:
ln = end;
}
filename = NULL;
func_addr = 0;
/* we try symtab symbols (no line number info) */
function = rt_elfsym(rc, wanted_pc, &func_addr);
if (function)
goto found;
if ((rc = rc->next))
goto next;
found:
if (filename) {
if (skip[0] && strstr(filename, skip))
return (addr_t)-1;
rt_printf("%s:%d: ", filename, line);
}
else
rt_printf("0x%08llx : ", (long long)wanted_pc);
rt_printf("%s %s", msg, function ? function : "???");
return (addr_t)func_addr;
}
/* ------------------------------------------------------------- */
static int rt_get_caller_pc(addr_t *paddr, rt_context *rc, int level);
static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap)
{
rt_context *rc = &g_rtctxt;
addr_t pc = 0;
char skip[100];
int i, level, ret, n;
const char *a, *b, *msg;
if (fp) {
/* we're called from tcc_backtrace. */
rc->fp = (addr_t)fp;
rc->ip = (addr_t)ip;
msg = "";
} else {
/* we're called from signal/exception handler */
msg = "RUNTIME ERROR: ";
}
skip[0] = 0;
/* If fmt is like "^file.c^..." then skip calls from 'file.c' */
if (fmt[0] == '^' && (b = strchr(a = fmt + 1, fmt[0]))) {
memcpy(skip, a, b - a), skip[b - a] = 0;
fmt = b + 1;
}
n = rc->num_callers ? rc->num_callers : 6;
for (i = level = 0; level < n; i++) {
ret = rt_get_caller_pc(&pc, rc, i);
a = "%s";
if (ret != -1) {
if (rc->dwarf)
pc = rt_printline_dwarf(rc, pc, level ? "by" : "at", skip);
else
pc = rt_printline(rc, pc, level ? "by" : "at", skip);
if (pc == (addr_t)-1)
continue;
a = ": %s";
}
if (level == 0) {
rt_printf(a, msg);
rt_vprintf(fmt, ap);
} else if (ret == -1)
break;
rt_printf("\n");
if (ret == -1 || (pc == (addr_t)rc->top_func && pc))
break;
++level;
}