Subversion Repositories Kolibri OS

Rev

Rev 6460 | Rev 7520 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6429 siemargl 1
/*
2
 *  TCC - Tiny C Compiler
3
 *
4
 *  Copyright (c) 2001-2004 Fabrice Bellard
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
#include "tcc.h"
22
 
23
/********************************************************/
24
/* global variables */
25
 
26
/* use GNU C extensions */
27
ST_DATA int gnu_ext = 1;
28
 
29
/* use TinyCC extensions */
30
ST_DATA int tcc_ext = 1;
31
 
32
/* XXX: get rid of this ASAP */
33
ST_DATA struct TCCState *tcc_state;
34
 
35
/********************************************************/
36
 
37
#ifdef ONE_SOURCE
38
#include "tccpp.c"
39
#include "tccgen.c"
40
#include "tccelf.c"
41
#ifdef TCC_IS_NATIVE
42
# include "tccrun.c"
43
#endif
44
#ifdef TCC_TARGET_I386
45
#include "i386-gen.c"
46
#endif
47
#ifdef TCC_TARGET_ARM
48
#include "arm-gen.c"
49
#endif
50
#ifdef TCC_TARGET_ARM64
51
#include "arm64-gen.c"
52
#endif
53
#ifdef TCC_TARGET_C67
54
#include "c67-gen.c"
55
#endif
56
#ifdef TCC_TARGET_X86_64
57
#include "x86_64-gen.c"
58
#endif
59
#ifdef CONFIG_TCC_ASM
60
#include "tccasm.c"
61
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
62
#include "i386-asm.c"
63
#endif
64
#endif
65
#ifdef TCC_TARGET_COFF
66
#include "tcccoff.c"
67
#endif
68
#if defined(TCC_TARGET_PE) || defined(TCC_TARGET_MEOS)
69
#include "tccpe.c"
70
#endif
71
#ifdef TCC_TARGET_MEOS
72
#include "tccmeos.c"
73
#endif
74
 
75
#endif /* ONE_SOURCE */
76
 
77
/********************************************************/
78
#ifndef CONFIG_TCC_ASM
79
ST_FUNC void asm_instr(void)
80
{
81
    tcc_error("inline asm() not supported");
82
}
83
ST_FUNC void asm_global_instr(void)
84
{
85
    tcc_error("inline asm() not supported");
86
}
87
#endif
88
 
89
/********************************************************/
90
#ifdef _WIN32
91
static char *normalize_slashes(char *path)
92
{
93
    char *p;
94
    for (p = path; *p; ++p)
95
        if (*p == '\\')
96
            *p = '/';
97
    return path;
98
}
99
 
100
static HMODULE tcc_module;
101
 
102
/* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
103
static void tcc_set_lib_path_w32(TCCState *s)
104
{
105
    char path[1024], *p;
106
    GetModuleFileNameA(tcc_module, path, sizeof path);
107
    p = tcc_basename(normalize_slashes(strlwr(path)));
108
    if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
109
        p -= 5;
110
    else if (p > path)
111
        p--;
112
    *p = 0;
113
    tcc_set_lib_path(s, path);
114
}
115
 
116
#ifdef TCC_TARGET_PE
117
static void tcc_add_systemdir(TCCState *s)
118
{
119
    char buf[1000];
120
    GetSystemDirectory(buf, sizeof buf);
121
    tcc_add_library_path(s, normalize_slashes(buf));
122
}
123
#endif
124
 
125
#ifndef CONFIG_TCC_STATIC
126
void dlclose(void *p)
127
{
128
    FreeLibrary((HMODULE)p);
129
}
130
#endif
131
 
132
#ifdef LIBTCC_AS_DLL
133
BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
134
{
135
    if (DLL_PROCESS_ATTACH == dwReason)
136
        tcc_module = hDll;
137
    return TRUE;
138
}
139
#endif
140
#endif
141
 
142
/********************************************************/
143
/* copy a string and truncate it. */
144
PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s)
145
{
146
    char *q, *q_end;
147
    int c;
148
 
149
    if (buf_size > 0) {
150
        q = buf;
151
        q_end = buf + buf_size - 1;
152
        while (q < q_end) {
153
            c = *s++;
154
            if (c == '\0')
155
                break;
156
            *q++ = c;
157
        }
158
        *q = '\0';
159
    }
160
    return buf;
161
}
162
 
163
/* strcat and truncate. */
164
PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
165
{
166
    int len;
167
    len = strlen(buf);
168
    if (len < buf_size)
169
        pstrcpy(buf + len, buf_size - len, s);
170
    return buf;
171
}
172
 
173
PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num)
174
{
175
    memcpy(out, in, num);
176
    out[num] = '\0';
177
    return out;
178
}
179
 
180
/* extract the basename of a file */
181
PUB_FUNC char *tcc_basename(const char *name)
182
{
183
    char *p = strchr(name, 0);
184
    while (p > name && !IS_DIRSEP(p[-1]))
185
        --p;
186
    return p;
187
}
188
 
189
/* extract extension part of a file
190
 *
191
 * (if no extension, return pointer to end-of-string)
192
 */
193
PUB_FUNC char *tcc_fileextension (const char *name)
194
{
195
    char *b = tcc_basename(name);
196
    char *e = strrchr(b, '.');
197
    return e ? e : strchr(b, 0);
198
}
199
 
200
/********************************************************/
201
/* memory management */
202
 
203
#undef free
204
#undef malloc
205
#undef realloc
206
 
207
#ifndef MEM_DEBUG
208
 
209
PUB_FUNC void tcc_free(void *ptr)
210
{
211
    free(ptr);
212
}
213
 
214
PUB_FUNC void *tcc_malloc(unsigned long size)
215
{
216
    void *ptr;
217
    ptr = malloc(size);
218
    if (!ptr && size)
219
        tcc_error("memory full (malloc)");
220
    return ptr;
221
}
222
 
223
PUB_FUNC void *tcc_mallocz(unsigned long size)
224
{
225
    void *ptr;
226
    ptr = tcc_malloc(size);
227
    memset(ptr, 0, size);
228
    return ptr;
229
}
230
 
231
PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
232
{
233
    void *ptr1;
234
    ptr1 = realloc(ptr, size);
235
    if (!ptr1 && size)
236
        tcc_error("memory full (realloc)");
237
    return ptr1;
238
}
239
 
240
PUB_FUNC char *tcc_strdup(const char *str)
241
{
242
    char *ptr;
243
    ptr = tcc_malloc(strlen(str) + 1);
244
    strcpy(ptr, str);
245
    return ptr;
246
}
247
 
248
PUB_FUNC void tcc_memstats(int bench)
249
{
250
}
251
 
252
#else
253
 
254
#define MEM_DEBUG_MAGIC1 0xFEEDDEB1
255
#define MEM_DEBUG_MAGIC2 0xFEEDDEB2
256
#define MEM_DEBUG_FILE_LEN 15
257
 
258
struct mem_debug_header {
259
    size_t      magic1;
260
    size_t      size;
261
    struct mem_debug_header *prev;
262
    struct mem_debug_header *next;
263
    size_t      line_num;
264
    char        file_name[MEM_DEBUG_FILE_LEN + 1];
265
    size_t      magic2;
266
};
267
 
268
typedef struct mem_debug_header mem_debug_header_t;
269
 
270
static mem_debug_header_t *mem_debug_chain;
271
static size_t mem_cur_size;
272
static size_t mem_max_size;
273
 
274
PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line)
275
{
276
    void *ptr;
277
    int ofs;
278
 
279
    mem_debug_header_t *header;
280
 
281
    ptr = malloc(sizeof(mem_debug_header_t) + size);
282
    if (!ptr)
283
        tcc_error("memory full (malloc)");
284
 
285
    mem_cur_size += size;
286
    if (mem_cur_size > mem_max_size)
287
        mem_max_size = mem_cur_size;
288
 
289
    header = (mem_debug_header_t *)ptr;
290
 
291
    header->magic1 = MEM_DEBUG_MAGIC1;
292
    header->magic2 = MEM_DEBUG_MAGIC2;
293
    header->size = size;
294
    header->line_num = line;
295
 
296
    ofs = strlen(file) - MEM_DEBUG_FILE_LEN;
297
    strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN);
298
    header->file_name[MEM_DEBUG_FILE_LEN] = 0;
299
 
300
    header->next = mem_debug_chain;
301
    header->prev = NULL;
302
 
303
    if (header->next)
304
        header->next->prev = header;
305
 
306
    mem_debug_chain = header;
307
 
308
    ptr = (char *)ptr + sizeof(mem_debug_header_t);
309
    return ptr;
310
}
311
 
312
PUB_FUNC void tcc_free_debug(void *ptr)
313
{
314
    mem_debug_header_t *header;
315
 
316
    if (!ptr)
317
        return;
318
 
319
    ptr = (char *)ptr - sizeof(mem_debug_header_t);
320
    header = (mem_debug_header_t *)ptr;
321
    if (header->magic1 != MEM_DEBUG_MAGIC1 ||
322
        header->magic2 != MEM_DEBUG_MAGIC2 ||
323
        header->size == (size_t)-1 )
324
    {
325
        tcc_error("tcc_free check failed");
326
    }
327
 
328
    mem_cur_size -= header->size;
329
    header->size = (size_t)-1;
6441 siemargl 330
 
6429 siemargl 331
    if (header->next)
332
        header->next->prev = header->prev;
333
 
334
    if (header->prev)
335
        header->prev->next = header->next;
336
 
337
    if (header == mem_debug_chain)
338
        mem_debug_chain = header->next;
339
 
340
    free(ptr);
341
}
342
 
343
 
344
PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line)
345
{
346
    void *ptr;
347
    ptr = tcc_malloc_debug(size,file,line);
348
    memset(ptr, 0, size);
349
    return ptr;
350
}
351
 
352
PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line)
353
{
354
    mem_debug_header_t *header;
355
    int mem_debug_chain_update = 0;
356
 
357
    if (!ptr) {
358
        ptr = tcc_malloc_debug(size, file, line);
359
        return ptr;
360
    }
361
 
362
    ptr = (char *)ptr - sizeof(mem_debug_header_t);
363
    header = (mem_debug_header_t *)ptr;
364
    if (header->magic1 != MEM_DEBUG_MAGIC1 ||
365
        header->magic2 != MEM_DEBUG_MAGIC2 ||
366
        header->size == (size_t)-1 )
367
    {
368
        check_error:
369
            tcc_error("tcc_realloc check failed");
370
    }
371
 
372
    mem_debug_chain_update = (header == mem_debug_chain);
373
 
374
    mem_cur_size -= header->size;
375
    ptr = realloc(ptr, sizeof(mem_debug_header_t) + size);
376
    if (!ptr)
377
        tcc_error("memory full (realloc)");
378
 
379
    header = (mem_debug_header_t *)ptr;
380
    if (header->magic1 != MEM_DEBUG_MAGIC1 ||
381
        header->magic2 != MEM_DEBUG_MAGIC2)
382
    {
383
        goto check_error;
384
    }
385
 
386
    mem_cur_size += size;
387
    if (mem_cur_size > mem_max_size)
388
        mem_max_size = mem_cur_size;
389
 
390
    header->size = size;
391
    if (header->next)
392
        header->next->prev = header;
393
 
394
    if (header->prev)
395
        header->prev->next = header;
396
 
397
    if (mem_debug_chain_update)
398
        mem_debug_chain = header;
399
 
400
    ptr = (char *)ptr + sizeof(mem_debug_header_t);
401
    return ptr;
402
}
403
 
404
PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line)
405
{
406
    char *ptr;
407
    ptr = tcc_malloc_debug(strlen(str) + 1, file, line);
408
    strcpy(ptr, str);
409
    return ptr;
410
}
411
 
412
PUB_FUNC void tcc_memstats(int bench)
413
{
414
    if (mem_cur_size) {
415
        mem_debug_header_t *header = mem_debug_chain;
416
 
417
        fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
418
            mem_cur_size, mem_max_size);
419
 
420
        while (header) {
421
            fprintf(stderr, "  file %s, line %u: %u bytes\n",
422
                header->file_name, header->line_num, header->size);
423
            header = header->next;
424
        }
425
    }
426
    else if (bench)
427
        fprintf(stderr, "mem_max_size= %d bytes\n", mem_max_size);
428
}
429
 
430
#undef MEM_DEBUG_MAGIC1
431
#undef MEM_DEBUG_MAGIC2
432
#undef MEM_DEBUG_FILE_LEN
433
 
434
#endif
435
 
436
#define free(p) use_tcc_free(p)
437
#define malloc(s) use_tcc_malloc(s)
438
#define realloc(p, s) use_tcc_realloc(p, s)
439
 
440
/********************************************************/
441
/* dynarrays */
442
 
443
ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data)
444
{
445
    int nb, nb_alloc;
446
    void **pp;
447
 
448
    nb = *nb_ptr;
449
    pp = *ptab;
450
    /* every power of two we double array size */
451
    if ((nb & (nb - 1)) == 0) {
452
        if (!nb)
453
            nb_alloc = 1;
454
        else
455
            nb_alloc = nb * 2;
456
        pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
457
        *ptab = pp;
458
    }
459
    pp[nb++] = data;
460
    *nb_ptr = nb;
461
}
462
 
463
ST_FUNC void dynarray_reset(void *pp, int *n)
464
{
465
    void **p;
466
    for (p = *(void***)pp; *n; ++p, --*n)
467
        if (*p)
468
            tcc_free(*p);
469
    tcc_free(*(void**)pp);
470
    *(void**)pp = NULL;
471
}
472
 
473
static void tcc_split_path(TCCState *s, void ***p_ary, int *p_nb_ary, const char *in)
474
{
475
    const char *p;
476
    do {
477
        int c;
478
        CString str;
479
 
480
        cstr_new(&str);
481
        for (p = in; c = *p, c != '\0' && c != PATHSEP; ++p) {
482
            if (c == '{' && p[1] && p[2] == '}') {
483
                c = p[1], p += 2;
484
                if (c == 'B')
485
                    cstr_cat(&str, s->tcc_lib_path, -1);
486
            } else {
487
                cstr_ccat(&str, c);
488
            }
489
        }
490
        cstr_ccat(&str, '\0');
491
        dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
492
        cstr_free(&str);
493
        in = p+1;
494
    } while (*p);
495
}
496
 
497
/********************************************************/
498
 
499
ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
500
{
501
    Section *sec;
502
 
503
    sec = tcc_mallocz(sizeof(Section) + strlen(name));
504
    strcpy(sec->name, name);
505
    sec->sh_type = sh_type;
506
    sec->sh_flags = sh_flags;
507
    switch(sh_type) {
508
    case SHT_HASH:
509
    case SHT_REL:
510
    case SHT_RELA:
511
    case SHT_DYNSYM:
512
    case SHT_SYMTAB:
513
    case SHT_DYNAMIC:
514
        sec->sh_addralign = 4;
515
        break;
516
    case SHT_STRTAB:
517
        sec->sh_addralign = 1;
518
        break;
519
    default:
520
        sec->sh_addralign =  PTR_SIZE; /* gcc/pcc default aligment */
521
        break;
522
    }
523
 
524
    if (sh_flags & SHF_PRIVATE) {
525
        dynarray_add((void ***)&s1->priv_sections, &s1->nb_priv_sections, sec);
526
    } else {
527
        sec->sh_num = s1->nb_sections;
528
        dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
529
    }
530
 
531
    return sec;
532
}
533
 
534
static void free_section(Section *s)
535
{
536
    tcc_free(s->data);
537
}
538
 
539
/* realloc section and set its content to zero */
540
ST_FUNC void section_realloc(Section *sec, unsigned long new_size)
541
{
542
    unsigned long size;
543
    unsigned char *data;
544
 
545
    size = sec->data_allocated;
546
    if (size == 0)
547
        size = 1;
548
    while (size < new_size)
549
        size = size * 2;
550
    data = tcc_realloc(sec->data, size);
551
    memset(data + sec->data_allocated, 0, size - sec->data_allocated);
552
    sec->data = data;
553
    sec->data_allocated = size;
554
}
555
 
556
/* reserve at least 'size' bytes in section 'sec' from
557
   sec->data_offset. */
558
ST_FUNC void *section_ptr_add(Section *sec, addr_t size)
559
{
560
    size_t offset, offset1;
561
 
562
    offset = sec->data_offset;
563
    offset1 = offset + size;
564
    if (offset1 > sec->data_allocated)
565
        section_realloc(sec, offset1);
566
    sec->data_offset = offset1;
567
    return sec->data + offset;
568
}
569
 
570
/* reserve at least 'size' bytes from section start */
571
ST_FUNC void section_reserve(Section *sec, unsigned long size)
572
{
573
    if (size > sec->data_allocated)
574
        section_realloc(sec, size);
575
    if (size > sec->data_offset)
576
        sec->data_offset = size;
577
}
578
 
579
/* return a reference to a section, and create it if it does not
580
   exists */
581
ST_FUNC Section *find_section(TCCState *s1, const char *name)
582
{
583
    Section *sec;
584
    int i;
585
    for(i = 1; i < s1->nb_sections; i++) {
586
        sec = s1->sections[i];
587
        if (!strcmp(name, sec->name))
588
            return sec;
589
    }
590
    /* sections are created as PROGBITS */
591
    return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
592
}
593
 
594
/* update sym->c so that it points to an external symbol in section
595
   'section' with value 'value' */
596
ST_FUNC void put_extern_sym2(Sym *sym, Section *section,
597
                            addr_t value, unsigned long size,
598
                            int can_add_underscore)
599
{
600
    int sym_type, sym_bind, sh_num, info, other;
601
    ElfW(Sym) *esym;
602
    const char *name;
603
    char buf1[256];
604
 
605
#ifdef CONFIG_TCC_BCHECK
606
    char buf[32];
607
#endif
608
 
609
    if (section == NULL)
610
        sh_num = SHN_UNDEF;
611
    else if (section == SECTION_ABS)
612
        sh_num = SHN_ABS;
613
    else
614
        sh_num = section->sh_num;
615
 
616
    if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
617
        sym_type = STT_FUNC;
618
    } else if ((sym->type.t & VT_BTYPE) == VT_VOID) {
619
        sym_type = STT_NOTYPE;
620
    } else {
621
        sym_type = STT_OBJECT;
622
    }
623
 
624
    if (sym->type.t & VT_STATIC)
625
        sym_bind = STB_LOCAL;
626
    else {
627
        if (sym->type.t & VT_WEAK)
628
            sym_bind = STB_WEAK;
629
        else
630
            sym_bind = STB_GLOBAL;
631
    }
632
 
633
    if (!sym->c) {
634
        name = get_tok_str(sym->v, NULL);
635
#ifdef CONFIG_TCC_BCHECK
636
        if (tcc_state->do_bounds_check) {
637
            /* XXX: avoid doing that for statics ? */
638
            /* if bound checking is activated, we change some function
639
               names by adding the "__bound" prefix */
640
            switch(sym->v) {
641
#ifdef TCC_TARGET_PE
642
            /* XXX: we rely only on malloc hooks */
643
            case TOK_malloc:
644
            case TOK_free:
645
            case TOK_realloc:
646
            case TOK_memalign:
647
            case TOK_calloc:
648
#endif
649
            case TOK_memcpy:
650
            case TOK_memmove:
651
            case TOK_memset:
652
            case TOK_strlen:
653
            case TOK_strcpy:
654
            case TOK_alloca:
655
                strcpy(buf, "__bound_");
656
                strcat(buf, name);
657
                name = buf;
658
                break;
659
            }
660
        }
661
#endif
662
        other = 0;
663
 
664
#ifdef TCC_TARGET_PE
665
        if (sym->type.t & VT_EXPORT)
666
            other |= ST_PE_EXPORT;
667
        if (sym_type == STT_FUNC && sym->type.ref) {
668
            Sym *ref = sym->type.ref;
669
            if (ref->a.func_export)
670
                other |= ST_PE_EXPORT;
671
            if (ref->a.func_call == FUNC_STDCALL && can_add_underscore) {
672
                sprintf(buf1, "_%s@%d", name, ref->a.func_args * PTR_SIZE);
673
                name = buf1;
674
                other |= ST_PE_STDCALL;
675
                can_add_underscore = 0;
676
            }
677
        } else {
678
            if (find_elf_sym(tcc_state->dynsymtab_section, name))
679
                other |= ST_PE_IMPORT;
680
            if (sym->type.t & VT_IMPORT)
681
                other |= ST_PE_IMPORT;
682
        }
683
#else
684
        if (! (sym->type.t & VT_STATIC))
685
	    other = (sym->type.t & VT_VIS_MASK) >> VT_VIS_SHIFT;
686
#endif
687
        if (tcc_state->leading_underscore && can_add_underscore) {
688
            buf1[0] = '_';
689
            pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
690
            name = buf1;
691
        }
692
        if (sym->asm_label) {
693
            name = get_tok_str(sym->asm_label, NULL);
694
        }
695
        info = ELFW(ST_INFO)(sym_bind, sym_type);
696
        sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
697
    } else {
698
        esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
699
        esym->st_value = value;
700
        esym->st_size = size;
701
        esym->st_shndx = sh_num;
702
    }
703
}
704
 
705
ST_FUNC void put_extern_sym(Sym *sym, Section *section,
706
                           addr_t value, unsigned long size)
707
{
708
    put_extern_sym2(sym, section, value, size, 1);
709
}
710
 
711
/* add a new relocation entry to symbol 'sym' in section 's' */
712
ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type,
713
                     addr_t addend)
714
{
715
    int c = 0;
716
    if (sym) {
717
        if (0 == sym->c)
718
            put_extern_sym(sym, NULL, 0, 0);
719
        c = sym->c;
720
    }
721
    /* now we can add ELF relocation info */
722
    put_elf_reloca(symtab_section, s, offset, type, c, addend);
723
}
724
 
725
ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
726
{
727
    greloca(s, sym, offset, type, 0);
728
}
729
 
730
/********************************************************/
731
 
732
static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
733
{
734
    int len;
735
    len = strlen(buf);
736
    vsnprintf(buf + len, buf_size - len, fmt, ap);
737
}
738
 
739
static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
740
{
741
    va_list ap;
742
    va_start(ap, fmt);
743
    strcat_vprintf(buf, buf_size, fmt, ap);
744
    va_end(ap);
745
}
746
 
747
static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
748
{
749
    char buf[2048];
750
    BufferedFile **pf, *f;
751
 
752
    buf[0] = '\0';
753
    /* use upper file if inline ":asm:" or token ":paste:" */
754
    for (f = file; f && f->filename[0] == ':'; f = f->prev)
755
     ;
756
    if (f) {
757
        for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
758
            strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
759
                (*pf)->filename, (*pf)->line_num);
760
        if (f->line_num > 0) {
761
            strcat_printf(buf, sizeof(buf), "%s:%d: ",
762
                f->filename, f->line_num);
763
        } else {
764
            strcat_printf(buf, sizeof(buf), "%s: ",
765
                f->filename);
766
        }
767
    } else {
768
        strcat_printf(buf, sizeof(buf), "tcc: ");
769
    }
770
    if (is_warning)
771
        strcat_printf(buf, sizeof(buf), "warning: ");
772
    else
773
        strcat_printf(buf, sizeof(buf), "error: ");
774
    strcat_vprintf(buf, sizeof(buf), fmt, ap);
775
 
776
    if (!s1->error_func) {
777
        /* default case: stderr */
778
        if (s1->ppfp) /* print a newline during tcc -E */
779
            fprintf(s1->ppfp, "\n"), fflush(s1->ppfp);
6574 siemargl 780
#ifndef TCC_TARGET_MEOS
6429 siemargl 781
        fprintf(stderr, "%s\n", buf);
782
        fflush(stderr); /* print error/warning now (win32) */
6574 siemargl 783
#else
784
        fprintf(stdout, "%s\n", buf);
785
        fflush(stdout); /* print error/warning now (win32) */
786
#endif
6429 siemargl 787
    } else {
788
        s1->error_func(s1->error_opaque, buf);
789
    }
790
    if (!is_warning || s1->warn_error)
791
        s1->nb_errors++;
792
}
793
 
794
LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque,
795
                        void (*error_func)(void *opaque, const char *msg))
796
{
797
    s->error_opaque = error_opaque;
798
    s->error_func = error_func;
799
}
800
 
801
/* error without aborting current compilation */
802
PUB_FUNC void tcc_error_noabort(const char *fmt, ...)
803
{
804
    TCCState *s1 = tcc_state;
805
    va_list ap;
806
 
807
    va_start(ap, fmt);
808
    error1(s1, 0, fmt, ap);
809
    va_end(ap);
810
}
811
 
812
PUB_FUNC void tcc_error(const char *fmt, ...)
813
{
814
    TCCState *s1 = tcc_state;
815
    va_list ap;
816
 
817
    va_start(ap, fmt);
818
    error1(s1, 0, fmt, ap);
819
    va_end(ap);
820
    /* better than nothing: in some cases, we accept to handle errors */
821
    if (s1->error_set_jmp_enabled) {
822
        longjmp(s1->error_jmp_buf, 1);
823
    } else {
824
        /* XXX: eliminate this someday */
825
        exit(1);
826
    }
827
}
828
 
829
PUB_FUNC void tcc_warning(const char *fmt, ...)
830
{
831
    TCCState *s1 = tcc_state;
832
    va_list ap;
833
 
834
    if (s1->warn_none)
835
        return;
836
 
837
    va_start(ap, fmt);
838
    error1(s1, 1, fmt, ap);
839
    va_end(ap);
840
}
841
 
842
/********************************************************/
843
/* I/O layer */
844
 
845
ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
846
{
847
    BufferedFile *bf;
848
    int buflen = initlen ? initlen : IO_BUF_SIZE;
849
 
850
    bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
851
    bf->buf_ptr = bf->buffer;
852
    bf->buf_end = bf->buffer + initlen;
853
    bf->buf_end[0] = CH_EOB; /* put eob symbol */
854
    pstrcpy(bf->filename, sizeof(bf->filename), filename);
855
#ifdef _WIN32
856
    normalize_slashes(bf->filename);
857
#endif
858
    bf->line_num = 1;
859
    bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
860
    bf->fd = -1;
861
    bf->prev = file;
862
    file = bf;
863
}
864
 
865
ST_FUNC void tcc_close(void)
866
{
867
    BufferedFile *bf = file;
868
    if (bf->fd > 0) {
869
        close(bf->fd);
870
        total_lines += bf->line_num;
871
    }
872
    file = bf->prev;
873
    tcc_free(bf);
874
}
875
 
876
ST_FUNC int tcc_open(TCCState *s1, const char *filename)
877
{
878
    int fd;
879
    if (strcmp(filename, "-") == 0)
880
        fd = 0, filename = "";
881
    else
882
        fd = open(filename, O_RDONLY | O_BINARY);
883
    if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
884
        printf("%s %*s%s\n", fd < 0 ? "nf":"->",
885
               (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
886
    if (fd < 0)
887
        return -1;
888
 
889
    tcc_open_bf(s1, filename, 0);
890
    file->fd = fd;
891
    return fd;
892
}
893
 
894
/* compile the C file opened in 'file'. Return non zero if errors. */
895
static int tcc_compile(TCCState *s1)
896
{
897
    Sym *define_start;
898
    char buf[512];
899
    volatile int section_sym;
900
 
901
#ifdef INC_DEBUG
902
    printf("%s: **** new file\n", file->filename);
903
#endif
904
    preprocess_init(s1);
905
 
906
    cur_text_section = NULL;
907
    funcname = "";
908
    anon_sym = SYM_FIRST_ANOM;
909
 
910
    /* file info: full path + filename */
911
    section_sym = 0; /* avoid warning */
912
    if (s1->do_debug) {
913
        section_sym = put_elf_sym(symtab_section, 0, 0,
914
                                  ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
915
                                  text_section->sh_num, NULL);
916
        getcwd(buf, sizeof(buf));
917
#ifdef _WIN32
918
        normalize_slashes(buf);
919
#endif
920
        pstrcat(buf, sizeof(buf), "/");
921
        put_stabs_r(buf, N_SO, 0, 0,
922
                    text_section->data_offset, text_section, section_sym);
923
        put_stabs_r(file->filename, N_SO, 0, 0,
924
                    text_section->data_offset, text_section, section_sym);
925
    }
926
    /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
927
       symbols can be safely used */
928
    put_elf_sym(symtab_section, 0, 0,
929
                ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
930
                SHN_ABS, file->filename);
931
 
932
    /* define some often used types */
933
    int_type.t = VT_INT;
934
 
935
    char_pointer_type.t = VT_BYTE;
936
    mk_pointer(&char_pointer_type);
937
 
938
#if PTR_SIZE == 4
939
    size_type.t = VT_INT;
940
#else
941
    size_type.t = VT_LLONG;
942
#endif
943
 
944
    func_old_type.t = VT_FUNC;
945
    func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
946
#ifdef TCC_TARGET_ARM
947
    arm_init(s1);
948
#endif
949
 
950
#if 0
951
    /* define 'void *alloca(unsigned int)' builtin function */
952
    {
953
        Sym *s1;
954
 
955
        p = anon_sym++;
956
        sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
957
        s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
958
        s1->next = NULL;
959
        sym->next = s1;
960
        sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
961
    }
962
#endif
963
 
964
    define_start = define_stack;
965
    nocode_wanted = 1;
966
 
967
    if (setjmp(s1->error_jmp_buf) == 0) {
968
        s1->nb_errors = 0;
969
        s1->error_set_jmp_enabled = 1;
970
 
971
        ch = file->buf_ptr[0];
972
        tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
973
        parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
974
        next();
975
        decl(VT_CONST);
976
        if (tok != TOK_EOF)
977
            expect("declaration");
978
        check_vstack();
979
 
980
        /* end of translation unit info */
981
        if (s1->do_debug) {
982
            put_stabs_r(NULL, N_SO, 0, 0,
983
                        text_section->data_offset, text_section, section_sym);
984
        }
985
    }
986
 
987
    s1->error_set_jmp_enabled = 0;
988
 
989
    /* reset define stack, but leave -Dsymbols (may be incorrect if
990
       they are undefined) */
991
    free_defines(define_start);
992
 
993
    gen_inline_functions();
994
 
995
    sym_pop(&global_stack, NULL);
996
    sym_pop(&local_stack, NULL);
997
 
998
    return s1->nb_errors != 0 ? -1 : 0;
999
}
1000
 
1001
LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
1002
{
1003
    int len, ret;
1004
 
1005
    len = strlen(str);
1006
    tcc_open_bf(s, "", len);
1007
    memcpy(file->buffer, str, len);
1008
    ret = tcc_compile(s);
1009
    tcc_close();
1010
    return ret;
1011
}
1012
 
1013
/* define a preprocessor symbol. A value can also be provided with the '=' operator */
1014
LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
1015
{
1016
    int len1, len2;
1017
    /* default value */
1018
    if (!value)
1019
        value = "1";
1020
    len1 = strlen(sym);
1021
    len2 = strlen(value);
1022
 
1023
    /* init file structure */
1024
    tcc_open_bf(s1, "", len1 + len2 + 1);
1025
    memcpy(file->buffer, sym, len1);
1026
    file->buffer[len1] = ' ';
1027
    memcpy(file->buffer + len1 + 1, value, len2);
1028
 
1029
    /* parse with define parser */
1030
    ch = file->buf_ptr[0];
1031
    next_nomacro();
1032
    parse_define();
1033
 
1034
    tcc_close();
1035
}
1036
 
1037
/* undefine a preprocessor symbol */
1038
LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
1039
{
1040
    TokenSym *ts;
1041
    Sym *s;
1042
    ts = tok_alloc(sym, strlen(sym));
1043
    s = define_find(ts->tok);
1044
    /* undefine symbol by putting an invalid name */
1045
    if (s)
1046
        define_undef(s);
1047
}
1048
 
1049
/* cleanup all static data used during compilation */
1050
static void tcc_cleanup(void)
1051
{
1052
    if (NULL == tcc_state)
1053
        return;
1054
    tcc_state = NULL;
1055
 
1056
    preprocess_delete();
1057
 
1058
    /* free sym_pools */
1059
    dynarray_reset(&sym_pools, &nb_sym_pools);
1060
    /* reset symbol stack */
1061
    sym_free_first = NULL;
1062
}
1063
 
1064
LIBTCCAPI TCCState *tcc_new(void)
1065
{
1066
    TCCState *s;
1067
    char buffer[100];
1068
    int a,b,c;
1069
 
1070
    tcc_cleanup();
1071
 
1072
    s = tcc_mallocz(sizeof(TCCState));
1073
    if (!s)
1074
        return NULL;
1075
    tcc_state = s;
1076
#ifdef _WIN32
1077
    tcc_set_lib_path_w32(s);
1078
#else
1079
    tcc_set_lib_path(s, CONFIG_TCCDIR);
1080
#endif
1081
    s->output_type = 0;
1082
    preprocess_new();
1083
    s->include_stack_ptr = s->include_stack;
1084
 
1085
    /* we add dummy defines for some special macros to speed up tests
1086
       and to have working defined() */
1087
    define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
1088
    define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
1089
    define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
1090
    define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
1091
 
1092
    /* define __TINYC__ 92X  */
1093
    sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
1094
    sprintf(buffer, "%d", a*10000 + b*100 + c);
1095
    tcc_define_symbol(s, "__TINYC__", buffer);
1096
 
1097
    /* standard defines */
1098
    tcc_define_symbol(s, "__STDC__", NULL);
1099
    tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
1100
    tcc_define_symbol(s, "__STDC_HOSTED__", NULL);
1101
 
1102
    /* target defines */
1103
#if defined(TCC_TARGET_I386)
1104
    tcc_define_symbol(s, "__i386__", NULL);
1105
    tcc_define_symbol(s, "__i386", NULL);
1106
    tcc_define_symbol(s, "i386", NULL);
1107
#elif defined(TCC_TARGET_X86_64)
1108
    tcc_define_symbol(s, "__x86_64__", NULL);
1109
#elif defined(TCC_TARGET_ARM)
1110
    tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
1111
    tcc_define_symbol(s, "__arm_elf__", NULL);
1112
    tcc_define_symbol(s, "__arm_elf", NULL);
1113
    tcc_define_symbol(s, "arm_elf", NULL);
1114
    tcc_define_symbol(s, "__arm__", NULL);
1115
    tcc_define_symbol(s, "__arm", NULL);
1116
    tcc_define_symbol(s, "arm", NULL);
1117
    tcc_define_symbol(s, "__APCS_32__", NULL);
1118
    tcc_define_symbol(s, "__ARMEL__", NULL);
1119
#if defined(TCC_ARM_EABI)
1120
    tcc_define_symbol(s, "__ARM_EABI__", NULL);
1121
#endif
1122
#if defined(TCC_ARM_HARDFLOAT)
1123
    s->float_abi = ARM_HARD_FLOAT;
1124
    tcc_define_symbol(s, "__ARM_PCS_VFP", NULL);
1125
#else
1126
    s->float_abi = ARM_SOFTFP_FLOAT;
1127
#endif
1128
#elif defined(TCC_TARGET_ARM64)
1129
    tcc_define_symbol(s, "__aarch64__", NULL);
1130
#endif
1131
 
1132
#ifdef TCC_TARGET_PE
1133
    tcc_define_symbol(s, "_WIN32", NULL);
1134
# ifdef TCC_TARGET_X86_64
1135
    tcc_define_symbol(s, "_WIN64", NULL);
1136
# endif
1137
#else
1138
    tcc_define_symbol(s, "__unix__", NULL);
1139
    tcc_define_symbol(s, "__unix", NULL);
1140
    tcc_define_symbol(s, "unix", NULL);
1141
# if defined(__linux__)
1142
    tcc_define_symbol(s, "__linux__", NULL);
1143
    tcc_define_symbol(s, "__linux", NULL);
1144
# endif
1145
# if defined(__FreeBSD__)
1146
#  define str(s) #s
1147
    tcc_define_symbol(s, "__FreeBSD__", str( __FreeBSD__));
1148
#  undef str
1149
# endif
1150
# if defined(__FreeBSD_kernel__)
1151
    tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
1152
# endif
1153
#endif
1154
# if defined(__NetBSD__)
1155
#  define str(s) #s
1156
    tcc_define_symbol(s, "__NetBSD__", str( __NetBSD__));
1157
#  undef str
1158
# endif
1159
 
1160
    /* TinyCC & gcc defines */
1161
#if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
1162
    tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
1163
    tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
1164
#else
1165
    tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
1166
    tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
1167
#endif
1168
 
1169
#ifdef TCC_TARGET_PE
1170
    tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
1171
    tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short");
1172
#else
1173
    tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
1174
    /* wint_t is unsigned int by default, but (signed) int on BSDs
1175
       and unsigned short on windows.  Other OSes might have still
1176
       other conventions, sigh.  */
1177
#if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__NetBSD__)
1178
    tcc_define_symbol(s, "__WINT_TYPE__", "int");
1179
#else
1180
    tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int");
1181
#endif
1182
#endif
1183
 
1184
#ifndef TCC_TARGET_PE
1185
    /* glibc defines */
1186
    tcc_define_symbol(s, "__REDIRECT(name, proto, alias)", "name proto __asm__ (#alias)");
1187
    tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)", "name proto __asm__ (#alias) __THROW");
1188
    /* paths for crt objects */
1189
    tcc_split_path(s, (void ***)&s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
1190
#endif
1191
 
1192
    /* no section zero */
1193
    dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
1194
 
1195
    /* create standard sections */
1196
    text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
1197
    data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
1198
    bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
1199
 
1200
    /* symbols are always generated for linking stage */
1201
    symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
1202
                                ".strtab",
1203
                                ".hashtab", SHF_PRIVATE);
1204
    strtab_section = symtab_section->link;
1205
    s->symtab = symtab_section;
1206
 
1207
    /* private symbol table for dynamic symbols */
1208
    s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
1209
                                      ".dynstrtab",
1210
                                      ".dynhashtab", SHF_PRIVATE);
1211
    s->alacarte_link = 1;
1212
    s->nocommon = 1;
1213
    s->warn_implicit_function_declaration = 1;
1214
 
1215
#ifdef CHAR_IS_UNSIGNED
1216
    s->char_is_unsigned = 1;
1217
#endif
1218
    /* enable this if you want symbols with leading underscore on windows: */
1219
#if 0 /* def TCC_TARGET_PE */
1220
    s->leading_underscore = 1;
1221
#endif
1222
#if 0 /* TCC_TARGET_MEOS */
1223
    s->leading_underscore = 1;
1224
#endif
1225
#ifdef TCC_TARGET_I386
1226
    s->seg_size = 32;
1227
#endif
1228
#ifdef TCC_IS_NATIVE
1229
    s->runtime_main = "main";
1230
#endif
1231
    return s;
1232
}
1233
 
1234
LIBTCCAPI void tcc_delete(TCCState *s1)
1235
{
1236
    int i;
1237
    int bench = s1->do_bench;
1238
 
1239
    tcc_cleanup();
1240
 
1241
    /* close a preprocessor output */
1242
    if (s1->ppfp && s1->ppfp != stdout)
1243
        fclose(s1->ppfp);
1244
    if (s1->dffp && s1->dffp != s1->ppfp)
1245
        fclose(s1->dffp);
1246
 
1247
    /* free all sections */
1248
    for(i = 1; i < s1->nb_sections; i++)
1249
        free_section(s1->sections[i]);
1250
    dynarray_reset(&s1->sections, &s1->nb_sections);
1251
 
1252
    for(i = 0; i < s1->nb_priv_sections; i++)
1253
        free_section(s1->priv_sections[i]);
1254
    dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
1255
 
1256
    /* free any loaded DLLs */
1257
#ifdef TCC_IS_NATIVE
1258
    for ( i = 0; i < s1->nb_loaded_dlls; i++) {
1259
        DLLReference *ref = s1->loaded_dlls[i];
1260
        if ( ref->handle )
1261
            dlclose(ref->handle);
1262
    }
1263
#endif
1264
 
1265
    /* free loaded dlls array */
1266
    dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
1267
 
1268
    /* free library paths */
1269
    dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
1270
    dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
1271
 
1272
    /* free include paths */
1273
    dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
1274
    dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
1275
    dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
1276
 
1277
    tcc_free(s1->tcc_lib_path);
1278
    tcc_free(s1->soname);
1279
    tcc_free(s1->rpath);
1280
    tcc_free(s1->init_symbol);
1281
    tcc_free(s1->fini_symbol);
1282
    tcc_free(s1->outfile);
1283
    tcc_free(s1->deps_outfile);
1284
    dynarray_reset(&s1->files, &s1->nb_files);
1285
    dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
1286
    dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
1287
 
1288
#ifdef TCC_IS_NATIVE
1289
# ifdef HAVE_SELINUX
1290
    munmap (s1->write_mem, s1->mem_size);
1291
    munmap (s1->runtime_mem, s1->mem_size);
1292
# else
1293
    tcc_free(s1->runtime_mem);
1294
# endif
1295
#endif
1296
 
1297
    tcc_free(s1->sym_attrs);
1298
    tcc_free(s1);
1299
    tcc_memstats(bench);
1300
}
1301
 
1302
LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
1303
{
1304
    tcc_split_path(s, (void ***)&s->include_paths, &s->nb_include_paths, pathname);
1305
    return 0;
1306
}
1307
 
1308
LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
1309
{
1310
    tcc_split_path(s, (void ***)&s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
1311
    return 0;
1312
}
1313
 
1314
ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, int filetype)
1315
{
1316
    ElfW(Ehdr) ehdr;
1317
    int fd, ret, size;
1318
 
1319
    parse_flags = 0;
1320
#ifdef CONFIG_TCC_ASM
1321
    /* if .S file, define __ASSEMBLER__ like gcc does */
1322
    if ((filetype == TCC_FILETYPE_ASM) || (filetype == TCC_FILETYPE_ASM_PP)) {
1323
        tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
1324
        parse_flags = PARSE_FLAG_ASM_FILE;
1325
    }
1326
#endif
1327
 
1328
    /* open the file */
1329
    ret = tcc_open(s1, filename);
1330
    if (ret < 0) {
1331
        if (flags & AFF_PRINT_ERROR)
1332
            tcc_error_noabort("file '%s' not found", filename);
1333
        return ret;
1334
    }
1335
 
1336
    /* update target deps */
1337
    dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1338
            tcc_strdup(filename));
1339
 
1340
    if (flags & AFF_PREPROCESS) {
1341
        ret = tcc_preprocess(s1);
1342
        goto the_end;
1343
    }
1344
 
1345
    if (filetype == TCC_FILETYPE_C) {
1346
        /* C file assumed */
1347
        ret = tcc_compile(s1);
1348
        goto the_end;
1349
    }
1350
 
1351
#ifdef CONFIG_TCC_ASM
1352
    if (filetype == TCC_FILETYPE_ASM_PP) {
1353
        /* non preprocessed assembler */
1354
        ret = tcc_assemble(s1, 1);
1355
        goto the_end;
1356
    }
1357
 
1358
    if (filetype == TCC_FILETYPE_ASM) {
1359
        /* preprocessed assembler */
1360
        ret = tcc_assemble(s1, 0);
1361
        goto the_end;
1362
    }
1363
#endif
1364
 
1365
    fd = file->fd;
1366
    /* assume executable format: auto guess file type */
1367
    size = read(fd, &ehdr, sizeof(ehdr));
1368
    lseek(fd, 0, SEEK_SET);
1369
    if (size <= 0) {
1370
        tcc_error_noabort("could not read header");
1371
        goto the_end;
1372
    }
1373
 
1374
    if (size == sizeof(ehdr) &&
1375
        ehdr.e_ident[0] == ELFMAG0 &&
1376
        ehdr.e_ident[1] == ELFMAG1 &&
1377
        ehdr.e_ident[2] == ELFMAG2 &&
1378
        ehdr.e_ident[3] == ELFMAG3) {
1379
 
1380
        /* do not display line number if error */
1381
        file->line_num = 0;
1382
        if (ehdr.e_type == ET_REL) {
1383
            ret = tcc_load_object_file(s1, fd, 0);
1384
            goto the_end;
1385
 
1386
        }
1387
#if !defined(TCC_TARGET_PE) && !defined(TCC_TARGET_MEOS)
1388
        if (ehdr.e_type == ET_DYN) {
1389
            if (s1->output_type == TCC_OUTPUT_MEMORY) {
1390
#ifdef TCC_IS_NATIVE
1391
                void *h;
1392
                h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
1393
                if (h)
1394
#endif
1395
                    ret = 0;
1396
            } else {
1397
                ret = tcc_load_dll(s1, fd, filename,
1398
                                   (flags & AFF_REFERENCED_DLL) != 0);
1399
            }
1400
            goto the_end;
1401
        }
1402
#endif
1403
        tcc_error_noabort("unrecognized ELF file");
1404
        goto the_end;
1405
    }
1406
 
1407
    if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
1408
        file->line_num = 0; /* do not display line number if error */
1409
        ret = tcc_load_archive(s1, fd);
1410
        goto the_end;
1411
    }
1412
 
1413
#ifdef TCC_TARGET_COFF
1414
    if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
1415
        ret = tcc_load_coff(s1, fd);
1416
        goto the_end;
1417
    }
1418
#endif
1419
 
1420
#if defined(TCC_TARGET_PE) ||  defined(TCC_TARGET_MEOS)
1421
    ret = pe_load_file(s1, filename, fd);
1422
#else
1423
    /* as GNU ld, consider it is an ld script if not recognized */
1424
    ret = tcc_load_ldscript(s1);
1425
#endif
1426
    if (ret < 0)
1427
        tcc_error_noabort("unrecognized file type");
1428
 
1429
the_end:
1430
    tcc_close();
1431
    return ret;
1432
}
1433
 
1434
LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename, int filetype)
1435
{
1436
    if (s->output_type == TCC_OUTPUT_PREPROCESS)
1437
        return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | AFF_PREPROCESS, filetype);
1438
    else
1439
        return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR, filetype);
1440
}
1441
 
1442
LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
1443
{
1444
    tcc_split_path(s, (void ***)&s->library_paths, &s->nb_library_paths, pathname);
1445
    return 0;
1446
}
1447
 
1448
static int tcc_add_library_internal(TCCState *s, const char *fmt,
1449
    const char *filename, int flags, char **paths, int nb_paths)
1450
{
1451
    char buf[1024];
1452
    int i;
1453
 
1454
    for(i = 0; i < nb_paths; i++) {
1455
        snprintf(buf, sizeof(buf), fmt, paths[i], filename);
1456
//printf("added lib [%s]\n", buf);
1457
        if (tcc_add_file_internal(s, buf, flags, TCC_FILETYPE_BINARY) == 0)
1458
            return 0;
1459
    }
1460
    return -1;
1461
}
1462
 
1463
#if !defined(TCC_TARGET_PE) && !defined(TCC_TARGET_MEOS)
1464
/* find and load a dll. Return non zero if not found */
1465
/* XXX: add '-rpath' option support ? */
1466
ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
1467
{
1468
    return tcc_add_library_internal(s, "%s/%s", filename, flags,
1469
        s->library_paths, s->nb_library_paths);
1470
}
1471
#endif
1472
 
1473
ST_FUNC int tcc_add_crt(TCCState *s, const char *filename)
1474
{
1475
    if (-1 == tcc_add_library_internal(s, "%s/%s",
1476
        filename, 0, s->crt_paths, s->nb_crt_paths))
1477
        tcc_error_noabort("file '%s' not found", filename);
1478
    return 0;
1479
}
1480
 
1481
/* the library name is the same as the argument of the '-l' option */
1482
LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
1483
{
1484
#if defined(TCC_TARGET_PE) || defined(TCC_TARGET_MEOS)
1485
    const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
1486
    const char **pp = s->static_link ? libs + 4 : libs;
1487
#else
1488
    const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
1489
    const char **pp = s->static_link ? libs + 1 : libs;
1490
#endif
1491
    while (*pp) {
1492
        if (0 == tcc_add_library_internal(s, *pp,
1493
            libraryname, 0, s->library_paths, s->nb_library_paths))
1494
            return 0;
1495
        ++pp;
1496
    }
1497
    return -1;
1498
}
1499
 
1500
PUB_FUNC int tcc_add_library_err(TCCState *s, const char *libname)
1501
{
1502
    int ret = tcc_add_library(s, libname);
1503
    if (ret < 0)
1504
        tcc_error_noabort("cannot find library 'lib%s'", libname);
1505
    return ret;
1506
}
1507
 
1508
/* habdle #pragma comment(lib,) */
1509
ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
1510
{
1511
    int i;
1512
    for (i = 0; i < s1->nb_pragma_libs; i++)
1513
        tcc_add_library_err(s1, s1->pragma_libs[i]);
1514
}
1515
 
1516
LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
1517
{
1518
#if defined(TCC_TARGET_PE) || defined(TCC_TARGET_MEOS)
1519
    /* On x86_64 'val' might not be reachable with a 32bit offset.
1520
       So it is handled here as if it were in a DLL. */
1521
    pe_putimport(s, 0, name, (uintptr_t)val);
1522
#else
1523
    add_elf_sym(symtab_section, (uintptr_t)val, 0,
1524
        ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
1525
        SHN_ABS, name);
1526
#endif
1527
    return 0;
1528
}
1529
 
1530
 
1531
/* Windows stat* ( https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx ):
1532
 * - st_gid, st_ino, st_uid: only valid on "unix" file systems (not FAT, NTFS, etc)
1533
 * - st_atime, st_ctime: not valid on FAT, valid on NTFS.
1534
 * - Other fields should be reasonably compatible (and S_ISDIR should work).
1535
 *
1536
 * BY_HANDLE_FILE_INFORMATION ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa363788%28v=vs.85%29.aspx ):
1537
 * - File index (combined nFileIndexHigh and nFileIndexLow) _may_ change when the file is opened.
1538
 *   - But on NTFS: it's guaranteed to be the same value until the file is deleted.
1539
 * - On windows server 2012 there's a 128b file id, and the 64b one via
1540
 *   nFileIndex* is not guaranteed to be unique.
1541
 *
1542
 * - MS Docs suggest to that volume number with the file index could be used to
1543
 *   check if two handles refer to the same file.
1544
 */
1545
#ifndef _WIN32
1546
typedef struct stat                file_info_t;
1547
#else
1548
typedef BY_HANDLE_FILE_INFORMATION file_info_t;
1549
#endif
1550
 
1551
int get_file_info(const char *fname, file_info_t *out_info)
1552
{
1553
#ifndef _WIN32
1554
    return stat(fname, out_info);
1555
#else
1556
    int rv = 1;
1557
    HANDLE h = CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
1558
                          FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, NULL);
1559
 
1560
    if (h != INVALID_HANDLE_VALUE) {
1561
        rv = !GetFileInformationByHandle(h, out_info);
1562
        CloseHandle(h);
1563
    }
1564
    return rv;
1565
#endif
1566
}
1567
 
1568
int is_dir(file_info_t *info)
1569
{
1570
#ifndef _WIN32
1571
    return S_ISDIR(info->st_mode);
1572
#else
1573
    return (info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ==
1574
           FILE_ATTRIBUTE_DIRECTORY;
1575
#endif
1576
}
1577
 
1578
int is_same_file(const file_info_t *fi1, const file_info_t *fi2)
1579
{
1580
#ifndef _WIN32
1581
    return fi1->st_dev == fi2->st_dev &&
1582
           fi1->st_ino == fi2->st_ino;
1583
#else
1584
    return fi1->dwVolumeSerialNumber == fi2->dwVolumeSerialNumber &&
1585
           fi1->nFileIndexHigh       == fi2->nFileIndexHigh &&
1586
           fi1->nFileIndexLow        == fi2->nFileIndexLow;
1587
#endif
1588
}
1589
 
1590
static void
1591
tcc_normalize_inc_dirs_aux(file_info_t *stats, size_t *pnum, char **path)
1592
{
1593
    size_t i, num = *pnum;
1594
    if (get_file_info(*path, &stats[num]) || !is_dir(&stats[num]))
1595
        goto remove;
1596
    for (i = 0; i < num; i++)
1597
        if (is_same_file(&stats[i], &stats[num]))
1598
            goto remove;
1599
    *pnum = num + 1;
1600
    return;
1601
 remove:
1602
    tcc_free(*path);
1603
    *path = 0;
1604
}
1605
 
1606
/* Remove non-existent and duplicate directories from include paths. */
1607
ST_FUNC void tcc_normalize_inc_dirs(TCCState *s)
1608
{
1609
    file_info_t *stats =
1610
        tcc_malloc(((size_t)s->nb_sysinclude_paths + s->nb_include_paths) *
1611
                   sizeof(*stats));
1612
    size_t i, num = 0;
1613
    for (i = 0; i < s->nb_sysinclude_paths; i++)
1614
        tcc_normalize_inc_dirs_aux(stats, &num, &s->sysinclude_paths[i]);
1615
    for (i = 0; i < s->nb_include_paths; i++)
1616
        tcc_normalize_inc_dirs_aux(stats, &num, &s->include_paths[i]);
1617
    tcc_free(stats);
1618
}
1619
 
1620
LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
1621
{
1622
    s->output_type = output_type;
1623
 
1624
    if (s->output_type == TCC_OUTPUT_PREPROCESS) {
1625
        if (!s->outfile) {
1626
            s->ppfp = stdout;
1627
        } else {
1628
            s->ppfp = fopen(s->outfile, "w");
1629
            if (!s->ppfp)
1630
                tcc_error("could not write '%s'", s->outfile);
1631
        }
1632
        s->dffp = s->ppfp;
1633
        if (s->dflag == 'M')
1634
            s->ppfp = NULL;
1635
    }
1636
    if (s->option_C && !s->ppfp)
1637
        s->option_C = 0;
1638
 
1639
    if (!s->nostdinc) {
1640
        /* default include paths */
1641
        /* -isystem paths have already been handled */
1642
        tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
1643
    }
1644
 
1645
    /* if bound checking, then add corresponding sections */
1646
#ifdef CONFIG_TCC_BCHECK
1647
    if (s->do_bounds_check) {
1648
        /* define symbol */
1649
        tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
1650
        /* create bounds sections */
1651
        bounds_section = new_section(s, ".bounds",
1652
                                     SHT_PROGBITS, SHF_ALLOC);
1653
        lbounds_section = new_section(s, ".lbounds",
1654
                                      SHT_PROGBITS, SHF_ALLOC);
1655
    }
1656
#endif
1657
 
1658
    if (s->char_is_unsigned) {
1659
        tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
1660
    }
1661
 
1662
    /* add debug sections */
1663
    if (s->do_debug) {
1664
        /* stab symbols */
1665
        stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
1666
        stab_section->sh_entsize = sizeof(Stab_Sym);
1667
        stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
1668
        put_elf_str(stabstr_section, "");
1669
        stab_section->link = stabstr_section;
1670
        /* put first entry */
1671
        put_stabs("", 0, 0, 0, 0);
1672
    }
1673
 
1674
    tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
1675
#ifdef TCC_TARGET_PE
1676
# ifdef _WIN32
1677
    tcc_add_systemdir(s);
1678
# endif
1679
#elif defined(TCC_TARGET_MEOS)
1680
    if (s->output_type != TCC_OUTPUT_OBJ && !s->nostdlib)
1681
    {
1682
        tcc_add_crt(s,"start.o");
1683
//        tcc_add_library(s,"ck"); // adding libck.a dont work, because need to be added last
1684
    }
1685
#else
1686
    /* add libc crt1/crti objects */
1687
    if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
1688
        !s->nostdlib) {
1689
        if (output_type != TCC_OUTPUT_DLL)
1690
            tcc_add_crt(s, "crt1.o");
1691
        tcc_add_crt(s, "crti.o");
1692
    }
1693
#endif
1694
 
1695
#ifdef CONFIG_TCC_BCHECK
1696
    if (s->do_bounds_check && (output_type == TCC_OUTPUT_EXE))
1697
    {
1698
        /* force a bcheck.o linking */
1699
        addr_t func = TOK___bound_init;
1700
        Sym *sym = external_global_sym(func, &func_old_type, 0);
1701
        if (!sym->c)
1702
            put_extern_sym(sym, NULL, 0, 0);
1703
    }
1704
#endif
1705
 
1706
    if (s->normalize_inc_dirs)
1707
        tcc_normalize_inc_dirs(s);
1708
    if (s->output_type == TCC_OUTPUT_PREPROCESS)
1709
        print_defines();
1710
 
1711
    return 0;
1712
}
1713
 
1714
LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
1715
{
1716
    tcc_free(s->tcc_lib_path);
1717
    s->tcc_lib_path = tcc_strdup(path);
1718
}
1719
 
1720
#define WD_ALL    0x0001 /* warning is activated when using -Wall */
1721
#define FD_INVERT 0x0002 /* invert value before storing */
1722
 
1723
typedef struct FlagDef {
1724
    uint16_t offset;
1725
    uint16_t flags;
1726
    const char *name;
1727
} FlagDef;
1728
 
1729
static const FlagDef warning_defs[] = {
1730
    { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
1731
    { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
1732
    { offsetof(TCCState, warn_error), 0, "error" },
1733
    { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
1734
      "implicit-function-declaration" },
1735
};
1736
 
1737
ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
1738
                    const char *name, int value)
1739
{
1740
    int i;
1741
    const FlagDef *p;
1742
    const char *r;
1743
 
1744
    r = name;
1745
    if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
1746
        r += 3;
1747
        value = !value;
1748
    }
1749
    for(i = 0, p = flags; i < nb_flags; i++, p++) {
1750
        if (!strcmp(r, p->name))
1751
            goto found;
1752
    }
1753
    return -1;
1754
 found:
1755
    if (p->flags & FD_INVERT)
1756
        value = !value;
1757
    *(int *)((uint8_t *)s + p->offset) = value;
1758
    return 0;
1759
}
1760
 
1761
/* set/reset a warning */
1762
static int tcc_set_warning(TCCState *s, const char *warning_name, int value)
1763
{
1764
    int i;
1765
    const FlagDef *p;
1766
 
1767
    if (!strcmp(warning_name, "all")) {
1768
        for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
1769
            if (p->flags & WD_ALL)
1770
                *(int *)((uint8_t *)s + p->offset) = 1;
1771
        }
1772
		s->warn_unsupported = 1;  // siemargl. was unused flag about compiler features
1773
        return 0;
1774
    } else {
1775
        return set_flag(s, warning_defs, countof(warning_defs),
1776
                        warning_name, value);
1777
    }
1778
}
1779
 
1780
static const FlagDef flag_defs[] = {
1781
    { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
1782
    { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
1783
    { offsetof(TCCState, nocommon), FD_INVERT, "common" },
1784
    { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
1785
    { offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
1786
    { offsetof(TCCState, old_struct_init_code), 0, "old-struct-init-code" },
1787
    { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
1788
    { offsetof(TCCState, normalize_inc_dirs), 0, "normalize-inc-dirs" },
1789
};
1790
 
1791
/* set/reset a flag */
1792
static int tcc_set_flag(TCCState *s, const char *flag_name, int value)
1793
{
1794
    return set_flag(s, flag_defs, countof(flag_defs),
1795
                    flag_name, value);
1796
}
1797
 
1798
 
1799
static int strstart(const char *val, const char **str)
1800
{
1801
    const char *p, *q;
1802
    p = *str;
1803
    q = val;
1804
    while (*q) {
1805
        if (*p != *q)
1806
            return 0;
1807
        p++;
1808
        q++;
1809
    }
1810
    *str = p;
1811
    return 1;
1812
}
1813
 
1814
/* Like strstart, but automatically takes into account that ld options can
1815
 *
1816
 * - start with double or single dash (e.g. '--soname' or '-soname')
1817
 * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
1818
 *   or '-Wl,-soname=x.so')
1819
 *
1820
 * you provide `val` always in 'option[=]' form (no leading -)
1821
 */
1822
static int link_option(const char *str, const char *val, const char **ptr)
1823
{
1824
    const char *p, *q;
1825
 
1826
    /* there should be 1 or 2 dashes */
1827
    if (*str++ != '-')
1828
        return 0;
1829
    if (*str == '-')
1830
        str++;
1831
 
1832
    /* then str & val should match (potentialy up to '=') */
1833
    p = str;
1834
    q = val;
1835
 
1836
    while (*q != '\0' && *q != '=') {
1837
        if (*p != *q)
1838
            return 0;
1839
        p++;
1840
        q++;
1841
    }
1842
 
1843
    /* '=' near eos means ',' or '=' is ok */
1844
    if (*q == '=') {
1845
        if (*p != ',' && *p != '=')
1846
            return 0;
1847
        p++;
1848
        q++;
1849
    }
1850
 
1851
    if (ptr)
1852
        *ptr = p;
1853
    return 1;
1854
}
1855
 
1856
static const char *skip_linker_arg(const char **str)
1857
{
1858
    const char *s1 = *str;
1859
    const char *s2 = strchr(s1, ',');
1860
    *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
1861
    return s2;
1862
}
1863
 
1864
static char *copy_linker_arg(const char *p)
1865
{
1866
    const char *q = p;
1867
    skip_linker_arg(&q);
1868
    return pstrncpy(tcc_malloc(q - p + 1), p, q - p);
1869
}
1870
 
1871
/* set linker options */
1872
static int tcc_set_linker(TCCState *s, const char *option)
6441 siemargl 1873
{
6429 siemargl 1874
    while (option && *option) {
1875
 
1876
        const char *p = option;
1877
        char *end = NULL;
1878
        int ignoring = 0;
1879
 
1880
        if (link_option(option, "Bsymbolic", &p)) {
1881
            s->symbolic = 1;
1882
        } else if (link_option(option, "nostdlib", &p)) {
1883
            s->nostdlib = 1;
1884
        } else if (link_option(option, "fini=", &p)) {
1885
            s->fini_symbol = copy_linker_arg(p);
1886
            ignoring = 1;
1887
        } else if (link_option(option, "image-base=", &p)
1888
                || link_option(option, "Ttext=", &p)) {
1889
            s->text_addr = strtoull(p, &end, 16);
1890
            s->has_text_addr = 1;
1891
        } else if (link_option(option, "init=", &p)) {
1892
            s->init_symbol = copy_linker_arg(p);
1893
            ignoring = 1;
1894
        } else if (link_option(option, "oformat=", &p)) {
1895
#if defined(TCC_TARGET_PE)
1896
            if (strstart("pe-", &p)) {
1897
#elif defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1898
            if (strstart("elf64-", &p)) {
1899
#else
1900
            if (strstart("elf32-", &p)) {
1901
#endif
1902
                s->output_format = TCC_OUTPUT_FORMAT_ELF;
1903
            } else if (!strcmp(p, "binary")) {
1904
                s->output_format = TCC_OUTPUT_FORMAT_BINARY;
1905
#ifdef TCC_TARGET_COFF
1906
            } else if (!strcmp(p, "coff")) {
1907
                s->output_format = TCC_OUTPUT_FORMAT_COFF;
1908
#endif
1909
            } else
1910
                goto err;
1911
 
1912
        } else if (link_option(option, "as-needed", &p)) {
1913
            ignoring = 1;
1914
        } else if (link_option(option, "O", &p)) {
1915
            ignoring = 1;
1916
        } else if (link_option(option, "rpath=", &p)) {
1917
            s->rpath = copy_linker_arg(p);
1918
        } else if (link_option(option, "section-alignment=", &p)) {
1919
            s->section_align = strtoul(p, &end, 16);
1920
        } else if (link_option(option, "soname=", &p)) {
1921
            s->soname = copy_linker_arg(p);
1922
#ifdef TCC_TARGET_PE
1923
        } else if (link_option(option, "file-alignment=", &p)) {
1924
            s->pe_file_align = strtoul(p, &end, 16);
1925
        } else if (link_option(option, "stack=", &p)) {
1926
            s->pe_stack_size = strtoul(p, &end, 10);
1927
        } else if (link_option(option, "subsystem=", &p)) {
1928
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1929
            if (!strcmp(p, "native")) {
1930
                s->pe_subsystem = 1;
1931
            } else if (!strcmp(p, "console")) {
1932
                s->pe_subsystem = 3;
1933
            } else if (!strcmp(p, "gui")) {
1934
                s->pe_subsystem = 2;
1935
            } else if (!strcmp(p, "posix")) {
1936
                s->pe_subsystem = 7;
1937
            } else if (!strcmp(p, "efiapp")) {
1938
                s->pe_subsystem = 10;
1939
            } else if (!strcmp(p, "efiboot")) {
1940
                s->pe_subsystem = 11;
1941
            } else if (!strcmp(p, "efiruntime")) {
1942
                s->pe_subsystem = 12;
1943
            } else if (!strcmp(p, "efirom")) {
1944
                s->pe_subsystem = 13;
1945
#elif defined(TCC_TARGET_ARM)
1946
            if (!strcmp(p, "wince")) {
1947
                s->pe_subsystem = 9;
1948
#endif
1949
            } else
1950
                goto err;
1951
#endif
1952
        } else
1953
            goto err;
1954
 
1955
        if (ignoring && s->warn_unsupported) err: {
1956
            char buf[100], *e;
1957
            pstrcpy(buf, sizeof buf, e = copy_linker_arg(option)), tcc_free(e);
1958
            if (ignoring)
1959
                tcc_warning("unsupported linker option '%s'", buf);
1960
            else
1961
                tcc_error("unsupported linker option '%s'", buf);
1962
        }
1963
        option = skip_linker_arg(&p);
1964
    }
1965
    return 0;
1966
}
1967
 
1968
typedef struct TCCOption {
1969
    const char *name;
1970
    uint16_t index;
1971
    uint16_t flags;
1972
} TCCOption;
1973
 
1974
enum {
1975
    TCC_OPTION_HELP,
1976
    TCC_OPTION_I,
1977
    TCC_OPTION_D,
1978
    TCC_OPTION_U,
1979
    TCC_OPTION_P,
1980
    TCC_OPTION_L,
1981
    TCC_OPTION_B,
1982
    TCC_OPTION_l,
1983
    TCC_OPTION_bench,
1984
    TCC_OPTION_bt,
1985
    TCC_OPTION_b,
1986
    TCC_OPTION_g,
1987
    TCC_OPTION_c,
1988
    TCC_OPTION_C,
1989
    TCC_OPTION_dumpversion,
1990
    TCC_OPTION_d,
1991
    TCC_OPTION_float_abi,
1992
    TCC_OPTION_static,
1993
    TCC_OPTION_std,
1994
    TCC_OPTION_shared,
1995
    TCC_OPTION_soname,
1996
    TCC_OPTION_o,
1997
    TCC_OPTION_r,
1998
    TCC_OPTION_s,
1999
    TCC_OPTION_traditional,
2000
    TCC_OPTION_Wl,
2001
    TCC_OPTION_W,
2002
    TCC_OPTION_O,
2003
    TCC_OPTION_m,
2004
    TCC_OPTION_f,
2005
    TCC_OPTION_isystem,
2006
    TCC_OPTION_iwithprefix,
2007
    TCC_OPTION_nostdinc,
2008
    TCC_OPTION_nostdlib,
2009
    TCC_OPTION_print_search_dirs,
2010
    TCC_OPTION_rdynamic,
2011
    TCC_OPTION_pedantic,
2012
    TCC_OPTION_pthread,
2013
    TCC_OPTION_run,
2014
    TCC_OPTION_v,
2015
    TCC_OPTION_w,
2016
    TCC_OPTION_pipe,
2017
    TCC_OPTION_E,
2018
    TCC_OPTION_MD,
2019
    TCC_OPTION_MF,
6441 siemargl 2020
    TCC_OPTION_x,
2021
    TCC_OPTION_stack
6429 siemargl 2022
};
2023
 
2024
#define TCC_OPTION_HAS_ARG 0x0001
2025
#define TCC_OPTION_NOSEP   0x0002 /* cannot have space before option and arg */
2026
 
2027
static const TCCOption tcc_options[] = {
2028
    { "h", TCC_OPTION_HELP, 0 },
2029
    { "-help", TCC_OPTION_HELP, 0 },
2030
    { "?", TCC_OPTION_HELP, 0 },
2031
    { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
2032
    { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
2033
    { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
2034
    { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2035
    { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
2036
    { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
2037
    { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2038
    { "bench", TCC_OPTION_bench, 0 },
2039
#ifdef CONFIG_TCC_BACKTRACE
2040
    { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
2041
#endif
2042
#ifdef CONFIG_TCC_BCHECK
2043
    { "b", TCC_OPTION_b, 0 },
2044
#endif
2045
    { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2046
    { "c", TCC_OPTION_c, 0 },
2047
    { "C", TCC_OPTION_C, 0 },
2048
    { "dumpversion", TCC_OPTION_dumpversion, 0},
2049
    { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2050
#ifdef TCC_TARGET_ARM
2051
    { "mfloat-abi", TCC_OPTION_float_abi, TCC_OPTION_HAS_ARG },
2052
#endif
2053
    { "static", TCC_OPTION_static, 0 },
2054
    { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2055
    { "shared", TCC_OPTION_shared, 0 },
2056
    { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
2057
    { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
2058
    { "pedantic", TCC_OPTION_pedantic, 0},
2059
    { "pthread", TCC_OPTION_pthread, 0},
2060
    { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2061
    { "rdynamic", TCC_OPTION_rdynamic, 0 },
2062
    { "r", TCC_OPTION_r, 0 },
2063
    { "s", TCC_OPTION_s, 0 },
2064
    { "traditional", TCC_OPTION_traditional, 0 },
2065
    { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2066
    { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2067
    { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2068
    { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
2069
    { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2070
    { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
2071
    { "iwithprefix", TCC_OPTION_iwithprefix, TCC_OPTION_HAS_ARG },
2072
    { "nostdinc", TCC_OPTION_nostdinc, 0 },
2073
    { "nostdlib", TCC_OPTION_nostdlib, 0 },
2074
    { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
2075
    { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2076
    { "w", TCC_OPTION_w, 0 },
2077
    { "pipe", TCC_OPTION_pipe, 0},
2078
    { "E", TCC_OPTION_E, 0},
2079
    { "MD", TCC_OPTION_MD, 0},
2080
    { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
2081
    { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
6441 siemargl 2082
    { "stack", TCC_OPTION_stack, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP},
6429 siemargl 2083
    { NULL, 0, 0 },
2084
};
2085
 
2086
static void parse_option_D(TCCState *s1, const char *optarg)
2087
{
2088
    char *sym = tcc_strdup(optarg);
2089
    char *value = strchr(sym, '=');
2090
    if (value)
2091
        *value++ = '\0';
2092
    tcc_define_symbol(s1, sym, value);
2093
    tcc_free(sym);
2094
}
2095
 
2096
static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
2097
{
2098
    int len = strlen(filename);
2099
    char *p = tcc_malloc(len + 2);
2100
    if (filetype) {
2101
        *p = filetype;
2102
    }
2103
    else {
2104
        /* use a file extension to detect a filetype */
2105
        const char *ext = tcc_fileextension(filename);
2106
        if (ext[0]) {
2107
            ext++;
2108
            if (!strcmp(ext, "S"))
2109
                *p = TCC_FILETYPE_ASM_PP;
2110
            else
2111
            if (!strcmp(ext, "s"))
2112
                *p = TCC_FILETYPE_ASM;
2113
            else
2114
            if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
2115
                *p = TCC_FILETYPE_C;
2116
            else
2117
                *p = TCC_FILETYPE_BINARY;
2118
        }
2119
        else {
2120
            *p = TCC_FILETYPE_C;
2121
        }
2122
    }
2123
    strcpy(p+1, filename);
2124
    dynarray_add((void ***)&s->files, &s->nb_files, p);
2125
}
2126
 
2127
ST_FUNC int tcc_parse_args1(TCCState *s, int argc, char **argv)
2128
{
2129
    const TCCOption *popt;
2130
    const char *optarg, *r;
2131
    int optind = 0;
2132
    ParseArgsState *pas = s->parse_args_state;
2133
 
6460 siemargl 2134
/*
2135
#ifdef TCC_TARGET_MEOS
2136
// siemargl testing
2137
	s->output_format = TCC_OUTPUT_FORMAT_COFF;
2138
#endif
2139
*/
6429 siemargl 2140
    while (optind < argc) {
2141
 
2142
        r = argv[optind++];
2143
        if (r[0] != '-' || r[1] == '\0') {
2144
            /* handle list files */
2145
            if (r[0] == '@' && r[1]) {
2146
                char buf[sizeof file->filename], *p;
2147
                char **argv = NULL;
2148
                int argc = 0;
2149
                FILE *fp;
2150
 
2151
                fp = fopen(r + 1, "rb");
2152
                if (fp == NULL)
2153
                    tcc_error("list file '%s' not found", r + 1);
2154
                while (fgets(buf, sizeof buf, fp)) {
2155
                    p = trimfront(trimback(buf, strchr(buf, 0)));
2156
                    if (0 == *p || ';' == *p)
2157
                        continue;
2158
                    dynarray_add((void ***)&argv, &argc, tcc_strdup(p));
2159
                }
2160
                fclose(fp);
2161
                tcc_parse_args1(s, argc, argv);
2162
                dynarray_reset(&argv, &argc);
2163
            } else {
2164
                args_parser_add_file(s, r, pas->filetype);
2165
                if (pas->run) {
2166
                    optind--;
2167
                    /* argv[0] will be this file */
2168
                    break;
2169
                }
2170
            }
2171
            continue;
2172
        }
2173
 
2174
        /* find option in table */
2175
        for(popt = tcc_options; ; ++popt) {
2176
            const char *p1 = popt->name;
2177
            const char *r1 = r + 1;
2178
            if (p1 == NULL)
2179
                tcc_error("invalid option -- '%s'", r);
2180
            if (!strstart(p1, &r1))
2181
                continue;
2182
            optarg = r1;
2183
            if (popt->flags & TCC_OPTION_HAS_ARG) {
2184
                if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
2185
                    if (optind >= argc)
2186
                        tcc_error("argument to '%s' is missing", r);
2187
                    optarg = argv[optind++];
2188
                }
2189
            } else if (*r1 != '\0')
2190
                continue;
2191
            break;
2192
        }
2193
 
2194
        switch(popt->index) {
2195
        case TCC_OPTION_HELP:
2196
            return 0;
2197
        case TCC_OPTION_I:
2198
            tcc_add_include_path(s, optarg);
2199
            break;
2200
        case TCC_OPTION_D:
2201
            parse_option_D(s, optarg);
2202
            break;
2203
        case TCC_OPTION_U:
2204
            tcc_undefine_symbol(s, optarg);
2205
            break;
2206
        case TCC_OPTION_L:
2207
            tcc_add_library_path(s, optarg);
2208
            break;
2209
        case TCC_OPTION_B:
2210
            /* set tcc utilities path (mainly for tcc development) */
2211
            tcc_set_lib_path(s, optarg);
2212
            break;
2213
        case TCC_OPTION_l:
2214
            args_parser_add_file(s, r, TCC_FILETYPE_BINARY);
2215
            s->nb_libraries++;
2216
            break;
2217
        case TCC_OPTION_pthread:
2218
            parse_option_D(s, "_REENTRANT");
2219
            pas->pthread = 1;
2220
            break;
2221
        case TCC_OPTION_bench:
2222
            s->do_bench = 1;
2223
            break;
2224
#ifdef CONFIG_TCC_BACKTRACE
2225
        case TCC_OPTION_bt:
2226
            tcc_set_num_callers(atoi(optarg));
2227
            break;
2228
#endif
2229
#ifdef CONFIG_TCC_BCHECK
2230
        case TCC_OPTION_b:
2231
            s->do_bounds_check = 1;
2232
            s->do_debug = 1;
2233
            break;
2234
#endif
2235
        case TCC_OPTION_g:
2236
            s->do_debug = 1;
2237
            break;
2238
        case TCC_OPTION_c:
2239
            if (s->output_type)
2240
                tcc_warning("-c: some compiler action already specified (%d)", s->output_type);
2241
            s->output_type = TCC_OUTPUT_OBJ;
2242
            break;
2243
        case TCC_OPTION_C:
2244
            s->option_C = 1;
2245
            break;
2246
        case TCC_OPTION_d:
2247
            if (*optarg == 'D' || *optarg == 'M')
2248
                s->dflag = *optarg;
2249
            else {
2250
                if (s->warn_unsupported)
2251
                    goto unsupported_option;
2252
                tcc_error("invalid option -- '%s'", r);
2253
            }
2254
            break;
2255
#ifdef TCC_TARGET_ARM
2256
        case TCC_OPTION_float_abi:
2257
            /* tcc doesn't support soft float yet */
2258
            if (!strcmp(optarg, "softfp")) {
2259
                s->float_abi = ARM_SOFTFP_FLOAT;
2260
                tcc_undefine_symbol(s, "__ARM_PCS_VFP");
2261
            } else if (!strcmp(optarg, "hard"))
2262
                s->float_abi = ARM_HARD_FLOAT;
2263
            else
2264
                tcc_error("unsupported float abi '%s'", optarg);
2265
            break;
2266
#endif
2267
        case TCC_OPTION_static:
2268
            s->static_link = 1;
2269
            break;
2270
        case TCC_OPTION_std:
2271
    	    /* silently ignore, a current purpose:
2272
    	       allow to use a tcc as a reference compiler for "make test" */
2273
            break;
2274
        case TCC_OPTION_shared:
2275
    	    if (s->output_type)
2276
                tcc_warning("-shared: some compiler action already specified (%d)", s->output_type);
2277
            s->output_type = TCC_OUTPUT_DLL;
2278
            break;
2279
        case TCC_OPTION_soname:
2280
            s->soname = tcc_strdup(optarg);
2281
            break;
2282
        case TCC_OPTION_m:
2283
            s->option_m = tcc_strdup(optarg);
2284
            break;
2285
        case TCC_OPTION_o:
2286
            if (s->outfile) {
2287
                tcc_warning("multiple -o option");
2288
                tcc_free(s->outfile);
2289
            }
2290
            s->outfile = tcc_strdup(optarg);
2291
            break;
2292
        case TCC_OPTION_r:
2293
            /* generate a .o merging several output files */
2294
    	    if (s->output_type)
2295
                tcc_warning("-r: some compiler action already specified (%d)", s->output_type);
2296
            s->option_r = 1;
2297
            s->output_type = TCC_OUTPUT_OBJ;
2298
            break;
2299
        case TCC_OPTION_isystem:
2300
            tcc_add_sysinclude_path(s, optarg);
2301
            break;
2302
        case TCC_OPTION_iwithprefix:
2303
            if (1) {
2304
                char buf[1024];
2305
                int buf_size = sizeof(buf)-1;
2306
                char *p = &buf[0];
2307
 
2308
                char *sysroot = "{B}/";
2309
                int len = strlen(sysroot);
2310
                if (len > buf_size)
2311
                    len = buf_size;
2312
                strncpy(p, sysroot, len);
2313
                p += len;
2314
                buf_size -= len;
2315
 
2316
                len = strlen(optarg);
2317
                if (len > buf_size)
2318
                    len = buf_size;
2319
                strncpy(p, optarg, len+1);
2320
                tcc_add_sysinclude_path(s, buf);
2321
            }
2322
            break;
2323
        case TCC_OPTION_nostdinc:
2324
            s->nostdinc = 1;
2325
            break;
2326
        case TCC_OPTION_nostdlib:
2327
            s->nostdlib = 1;
2328
            break;
2329
        case TCC_OPTION_print_search_dirs:
2330
            s->print_search_dirs = 1;
2331
            break;
2332
        case TCC_OPTION_run:
2333
    	    if (s->output_type)
2334
                tcc_warning("-run: some compiler action already specified (%d)", s->output_type);
2335
            s->output_type = TCC_OUTPUT_MEMORY;
2336
            tcc_set_options(s, optarg);
2337
            pas->run = 1;
2338
            break;
2339
        case TCC_OPTION_v:
2340
            do ++s->verbose; while (*optarg++ == 'v');
2341
            break;
2342
        case TCC_OPTION_f:
2343
            if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
2344
                goto unsupported_option;
2345
            break;
2346
        case TCC_OPTION_W:
2347
            if (tcc_set_warning(s, optarg, 1) < 0 &&
2348
                s->warn_unsupported)
2349
                goto unsupported_option;
2350
            break;
2351
        case TCC_OPTION_w:
2352
            s->warn_none = 1;
2353
            break;
2354
        case TCC_OPTION_rdynamic:
2355
            s->rdynamic = 1;
2356
            break;
2357
        case TCC_OPTION_Wl:
2358
            if (pas->linker_arg.size)
2359
                --pas->linker_arg.size, cstr_ccat(&pas->linker_arg, ',');
2360
            cstr_cat(&pas->linker_arg, optarg, 0);
2361
            break;
2362
        case TCC_OPTION_E:
2363
    	    if (s->output_type)
2364
                tcc_warning("-E: some compiler action already specified (%d)", s->output_type);
2365
            s->output_type = TCC_OUTPUT_PREPROCESS;
2366
            break;
2367
        case TCC_OPTION_P:
2368
            s->Pflag = atoi(optarg) + 1;
2369
            break;
2370
        case TCC_OPTION_MD:
2371
            s->gen_deps = 1;
2372
            break;
2373
        case TCC_OPTION_MF:
2374
            s->deps_outfile = tcc_strdup(optarg);
2375
            break;
2376
        case TCC_OPTION_dumpversion:
2377
            printf ("%s\n", TCC_VERSION);
2378
            exit(0);
2379
        case TCC_OPTION_s:
2380
            s->do_strip = 1;
2381
            break;
2382
        case TCC_OPTION_traditional:
2383
            break;
2384
        case TCC_OPTION_x:
2385
            if (*optarg == 'c')
2386
                pas->filetype = TCC_FILETYPE_C;
2387
            else
2388
            if (*optarg == 'a')
2389
                pas->filetype = TCC_FILETYPE_ASM_PP;
2390
            else
2391
            if (*optarg == 'n')
2392
                pas->filetype = 0;
2393
            else
2394
                tcc_warning("unsupported language '%s'", optarg);
2395
            break;
2396
        case TCC_OPTION_O:
2397
            if (1) {
2398
                int opt = atoi(optarg);
2399
                char *sym = "__OPTIMIZE__";
2400
                if (opt)
2401
                    tcc_define_symbol(s, sym, 0);
2402
                else
2403
                    tcc_undefine_symbol(s, sym);
2404
            }
2405
            break;
2406
        case TCC_OPTION_pedantic:
2407
        case TCC_OPTION_pipe:
2408
            /* ignored */
2409
            break;
6441 siemargl 2410
        case TCC_OPTION_stack:
2411
#ifdef TCC_TARGET_MEOS
2412
            s->pe_stack_size = strtoul(optarg+1, NULL, 10);
2413
#endif
2414
            break;
6429 siemargl 2415
        default:
2416
            if (s->warn_unsupported) {
2417
            unsupported_option:
2418
                tcc_warning("unsupported option '%s'", r);
2419
            }
2420
            break;
2421
        }
2422
    }
2423
    return optind;
2424
}
2425
 
2426
PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv)
2427
{
2428
    ParseArgsState *pas;
2429
    int ret, is_allocated = 0;
2430
 
2431
    if (!s->parse_args_state) {
2432
        s->parse_args_state = tcc_mallocz(sizeof(ParseArgsState));
2433
        cstr_new(&s->parse_args_state->linker_arg);
2434
        is_allocated = 1;
2435
    }
2436
    pas = s->parse_args_state;
2437
 
2438
    ret = tcc_parse_args1(s, argc, argv);
2439
 
2440
    if (s->output_type == 0)
2441
        s->output_type = TCC_OUTPUT_EXE;
2442
 
2443
    if (pas->pthread && s->output_type != TCC_OUTPUT_OBJ)
2444
        tcc_set_options(s, "-lpthread");
2445
 
2446
    if (s->output_type == TCC_OUTPUT_EXE)
2447
        tcc_set_linker(s, (const char *)pas->linker_arg.data);
2448
 
2449
    if (is_allocated) {
2450
        cstr_free(&pas->linker_arg);
2451
        tcc_free(pas);
2452
        s->parse_args_state = NULL;
2453
    }
2454
    return ret;
2455
}
2456
 
2457
LIBTCCAPI int tcc_set_options(TCCState *s, const char *str)
2458
{
2459
    const char *s1;
2460
    char **argv, *arg;
2461
    int argc, len;
2462
    int ret;
2463
 
2464
    argc = 0, argv = NULL;
2465
    for(;;) {
2466
        while (is_space(*str))
2467
            str++;
2468
        if (*str == '\0')
2469
            break;
2470
        s1 = str;
2471
        while (*str != '\0' && !is_space(*str))
2472
            str++;
2473
        len = str - s1;
2474
        arg = tcc_malloc(len + 1);
2475
        pstrncpy(arg, s1, len);
2476
        dynarray_add((void ***)&argv, &argc, arg);
2477
    }
2478
    ret = tcc_parse_args(s, argc, argv);
2479
    dynarray_reset(&argv, &argc);
2480
    return ret;
2481
}
2482
 
2483
PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time)
2484
{
2485
    double tt;
2486
    tt = (double)total_time / 1000000.0;
2487
    if (tt < 0.001)
2488
        tt = 0.001;
2489
    if (total_bytes < 1)
2490
        total_bytes = 1;
6574 siemargl 2491
    fprintf(stdout, "%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
6429 siemargl 2492
           tok_ident - TOK_IDENT, total_lines, total_bytes,
2493
           tt, (int)(total_lines / tt),
2494
           total_bytes / tt / 1000000.0);
2495
}
2496
 
2497
PUB_FUNC void tcc_set_environment(TCCState *s)
2498
{
2499
    char * path;
2500
 
2501
    path = getenv("C_INCLUDE_PATH");
2502
    if(path != NULL) {
2503
        tcc_add_include_path(s, path);
2504
    }
2505
    path = getenv("CPATH");
2506
    if(path != NULL) {
2507
        tcc_add_include_path(s, path);
2508
    }
2509
    path = getenv("LIBRARY_PATH");
2510
    if(path != NULL) {
2511
        tcc_add_library_path(s, path);
2512
    }
2513
}