[279] | 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 | #include "tccrun.c"
|
---|
| 42 | #ifdef TCC_TARGET_I386
|
---|
| 43 | #include "i386-gen.c"
|
---|
| 44 | #endif
|
---|
| 45 | #ifdef TCC_TARGET_ARM
|
---|
| 46 | #include "arm-gen.c"
|
---|
| 47 | #endif
|
---|
| 48 | #ifdef TCC_TARGET_C67
|
---|
| 49 | #include "c67-gen.c"
|
---|
| 50 | #endif
|
---|
| 51 | #ifdef TCC_TARGET_X86_64
|
---|
| 52 | #include "x86_64-gen.c"
|
---|
| 53 | #endif
|
---|
| 54 | #ifdef TCC_TARGET_IL
|
---|
| 55 | #include "il-gen.c"
|
---|
| 56 | #endif
|
---|
| 57 | #ifdef CONFIG_TCC_ASM
|
---|
| 58 | #include "tccasm.c"
|
---|
| 59 | #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
---|
| 60 | #include "i386-asm.c"
|
---|
| 61 | #endif
|
---|
| 62 | #endif
|
---|
| 63 | #ifdef TCC_TARGET_COFF
|
---|
| 64 | #include "tcccoff.c"
|
---|
| 65 | #endif
|
---|
| 66 | #ifdef TCC_TARGET_PE
|
---|
| 67 | #include "tccpe.c"
|
---|
| 68 | #endif
|
---|
| 69 | #endif /* ONE_SOURCE */
|
---|
| 70 |
|
---|
| 71 | /********************************************************/
|
---|
| 72 | #ifndef CONFIG_TCC_ASM
|
---|
| 73 | ST_FUNC void asm_instr(void)
|
---|
| 74 | {
|
---|
| 75 | tcc_error("inline asm() not supported");
|
---|
| 76 | }
|
---|
| 77 | ST_FUNC void asm_global_instr(void)
|
---|
| 78 | {
|
---|
| 79 | tcc_error("inline asm() not supported");
|
---|
| 80 | }
|
---|
| 81 | #endif
|
---|
| 82 |
|
---|
| 83 | /********************************************************/
|
---|
| 84 |
|
---|
| 85 | #ifdef _WIN32
|
---|
| 86 | char *normalize_slashes(char *path)
|
---|
| 87 | {
|
---|
| 88 | char *p;
|
---|
| 89 | for (p = path; *p; ++p)
|
---|
| 90 | if (*p == '\\')
|
---|
| 91 | *p = '/';
|
---|
| 92 | return path;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | #if 0
|
---|
| 96 | static HMODULE tcc_module;
|
---|
| 97 |
|
---|
| 98 | /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
|
---|
| 99 | static void tcc_set_lib_path_w32(TCCState *s)
|
---|
| 100 | {
|
---|
| 101 | char path[1024], *p;
|
---|
| 102 | GetModuleFileNameA(tcc_module, path, sizeof path);
|
---|
| 103 | p = tcc_basename(normalize_slashes(strlwr(path)));
|
---|
| 104 | if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
|
---|
| 105 | p -= 5;
|
---|
| 106 | else if (p > path)
|
---|
| 107 | p--;
|
---|
| 108 | *p = 0;
|
---|
| 109 | tcc_set_lib_path(s, path);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | #ifdef TCC_TARGET_PE
|
---|
| 113 | static void tcc_add_systemdir(TCCState *s)
|
---|
| 114 | {
|
---|
| 115 | char buf[1000];
|
---|
| 116 | GetSystemDirectory(buf, sizeof buf);
|
---|
| 117 | tcc_add_library_path(s, normalize_slashes(buf));
|
---|
| 118 | }
|
---|
| 119 | #endif
|
---|
| 120 |
|
---|
| 121 | #ifndef CONFIG_TCC_STATIC
|
---|
| 122 | void dlclose(void *p)
|
---|
| 123 | {
|
---|
| 124 | FreeLibrary((HMODULE)p);
|
---|
| 125 | }
|
---|
| 126 | #endif
|
---|
| 127 | #else
|
---|
| 128 | extern void tcc_set_lib_path_w32(TCCState *s);
|
---|
| 129 | extern void dlclose(void *p);
|
---|
| 130 | #endif
|
---|
| 131 |
|
---|
| 132 | #ifdef LIBTCC_AS_DLL
|
---|
| 133 | BOOL WINAPI DllMain (HANDLE 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 | #ifdef MEM_DEBUG
|
---|
| 208 | ST_DATA int mem_cur_size;
|
---|
| 209 | ST_DATA int mem_max_size;
|
---|
| 210 | unsigned malloc_usable_size(void*);
|
---|
| 211 | #endif
|
---|
| 212 |
|
---|
| 213 | PUB_FUNC void tcc_free(void *ptr)
|
---|
| 214 | {
|
---|
| 215 | #ifdef MEM_DEBUG
|
---|
| 216 | mem_cur_size -= malloc_usable_size(ptr);
|
---|
| 217 | #endif
|
---|
| 218 | free(ptr);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | PUB_FUNC void *tcc_malloc(unsigned long size)
|
---|
| 222 | {
|
---|
| 223 | void *ptr;
|
---|
| 224 | ptr = malloc(size);
|
---|
| 225 | if (!ptr && size)
|
---|
| 226 | tcc_error("memory full");
|
---|
| 227 | #ifdef MEM_DEBUG
|
---|
| 228 | mem_cur_size += malloc_usable_size(ptr);
|
---|
| 229 | if (mem_cur_size > mem_max_size)
|
---|
| 230 | mem_max_size = mem_cur_size;
|
---|
| 231 | #endif
|
---|
| 232 | return ptr;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | PUB_FUNC void *tcc_mallocz(unsigned long size)
|
---|
| 236 | {
|
---|
| 237 | void *ptr;
|
---|
| 238 | ptr = tcc_malloc(size);
|
---|
| 239 | memset(ptr, 0, size);
|
---|
| 240 | return ptr;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
|
---|
| 244 | {
|
---|
| 245 | void *ptr1;
|
---|
| 246 | #ifdef MEM_DEBUG
|
---|
| 247 | mem_cur_size -= malloc_usable_size(ptr);
|
---|
| 248 | #endif
|
---|
| 249 | ptr1 = realloc(ptr, size);
|
---|
| 250 | if (!ptr1 && size)
|
---|
| 251 | tcc_error("memory full");
|
---|
| 252 | #ifdef MEM_DEBUG
|
---|
| 253 | /* NOTE: count not correct if alloc error, but not critical */
|
---|
| 254 | mem_cur_size += malloc_usable_size(ptr1);
|
---|
| 255 | if (mem_cur_size > mem_max_size)
|
---|
| 256 | mem_max_size = mem_cur_size;
|
---|
| 257 | #endif
|
---|
| 258 | return ptr1;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | PUB_FUNC char *tcc_strdup(const char *str)
|
---|
| 262 | {
|
---|
| 263 | char *ptr;
|
---|
| 264 | ptr = tcc_malloc(strlen(str) + 1);
|
---|
| 265 | strcpy(ptr, str);
|
---|
| 266 | return ptr;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | PUB_FUNC void tcc_memstats(void)
|
---|
| 270 | {
|
---|
| 271 | #ifdef MEM_DEBUG
|
---|
| 272 | printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
|
---|
| 273 | #endif
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | #define free(p) use_tcc_free(p)
|
---|
| 277 | #define malloc(s) use_tcc_malloc(s)
|
---|
| 278 | #define realloc(p, s) use_tcc_realloc(p, s)
|
---|
| 279 |
|
---|
| 280 | /********************************************************/
|
---|
| 281 | /* dynarrays */
|
---|
| 282 |
|
---|
| 283 | ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data)
|
---|
| 284 | {
|
---|
| 285 | int nb, nb_alloc;
|
---|
| 286 | void **pp;
|
---|
| 287 |
|
---|
| 288 | nb = *nb_ptr;
|
---|
| 289 | pp = *ptab;
|
---|
| 290 | /* every power of two we double array size */
|
---|
| 291 | if ((nb & (nb - 1)) == 0) {
|
---|
| 292 | if (!nb)
|
---|
| 293 | nb_alloc = 1;
|
---|
| 294 | else
|
---|
| 295 | nb_alloc = nb * 2;
|
---|
| 296 | pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
|
---|
| 297 | *ptab = pp;
|
---|
| 298 | }
|
---|
| 299 | pp[nb++] = data;
|
---|
| 300 | *nb_ptr = nb;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | ST_FUNC void dynarray_reset(void *pp, int *n)
|
---|
| 304 | {
|
---|
| 305 | void **p;
|
---|
| 306 | for (p = *(void***)pp; *n; ++p, --*n)
|
---|
| 307 | if (*p)
|
---|
| 308 | tcc_free(*p);
|
---|
| 309 | tcc_free(*(void**)pp);
|
---|
| 310 | *(void**)pp = NULL;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | static void tcc_split_path(TCCState *s, void ***p_ary, int *p_nb_ary, const char *in)
|
---|
| 314 | {
|
---|
| 315 | const char *p;
|
---|
| 316 | do {
|
---|
| 317 | int c;
|
---|
| 318 | CString str;
|
---|
| 319 |
|
---|
| 320 | cstr_new(&str);
|
---|
| 321 | for (p = in; c = *p, c != '\0' && c != PATHSEP; ++p) {
|
---|
| 322 | if (c == '{' && p[1] && p[2] == '}') {
|
---|
| 323 | c = p[1], p += 2;
|
---|
| 324 | if (c == 'B')
|
---|
| 325 | cstr_cat(&str, s->tcc_lib_path);
|
---|
| 326 | } else {
|
---|
| 327 | cstr_ccat(&str, c);
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 | cstr_ccat(&str, '\0');
|
---|
| 331 | dynarray_add(p_ary, p_nb_ary, str.data);
|
---|
| 332 | in = p+1;
|
---|
| 333 | } while (*p);
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | /********************************************************/
|
---|
| 337 |
|
---|
| 338 | ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
|
---|
| 339 | {
|
---|
| 340 | Section *sec;
|
---|
| 341 |
|
---|
| 342 | sec = tcc_mallocz(sizeof(Section) + strlen(name));
|
---|
| 343 | strcpy(sec->name, name);
|
---|
| 344 | sec->sh_type = sh_type;
|
---|
| 345 | sec->sh_flags = sh_flags;
|
---|
| 346 | switch(sh_type) {
|
---|
| 347 | case SHT_HASH:
|
---|
| 348 | case SHT_REL:
|
---|
| 349 | case SHT_RELA:
|
---|
| 350 | case SHT_DYNSYM:
|
---|
| 351 | case SHT_SYMTAB:
|
---|
| 352 | case SHT_DYNAMIC:
|
---|
| 353 | sec->sh_addralign = 4;
|
---|
| 354 | break;
|
---|
| 355 | case SHT_STRTAB:
|
---|
| 356 | sec->sh_addralign = 1;
|
---|
| 357 | break;
|
---|
| 358 | default:
|
---|
| 359 | sec->sh_addralign = 32; /* default conservative alignment */
|
---|
| 360 | break;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | if (sh_flags & SHF_PRIVATE) {
|
---|
| 364 | dynarray_add((void ***)&s1->priv_sections, &s1->nb_priv_sections, sec);
|
---|
| 365 | } else {
|
---|
| 366 | sec->sh_num = s1->nb_sections;
|
---|
| 367 | dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | return sec;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | static void free_section(Section *s)
|
---|
| 374 | {
|
---|
| 375 | tcc_free(s->data);
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | /* realloc section and set its content to zero */
|
---|
| 379 | ST_FUNC void section_realloc(Section *sec, unsigned long new_size)
|
---|
| 380 | {
|
---|
| 381 | unsigned long size;
|
---|
| 382 | unsigned char *data;
|
---|
| 383 |
|
---|
| 384 | size = sec->data_allocated;
|
---|
| 385 | if (size == 0)
|
---|
| 386 | size = 1;
|
---|
| 387 | while (size < new_size)
|
---|
| 388 | size = size * 2;
|
---|
| 389 | data = tcc_realloc(sec->data, size);
|
---|
| 390 | memset(data + sec->data_allocated, 0, size - sec->data_allocated);
|
---|
| 391 | sec->data = data;
|
---|
| 392 | sec->data_allocated = size;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | /* reserve at least 'size' bytes in section 'sec' from
|
---|
| 396 | sec->data_offset. */
|
---|
| 397 | ST_FUNC void *section_ptr_add(Section *sec, unsigned long size)
|
---|
| 398 | {
|
---|
| 399 | unsigned long offset, offset1;
|
---|
| 400 |
|
---|
| 401 | offset = sec->data_offset;
|
---|
| 402 | offset1 = offset + size;
|
---|
| 403 | if (offset1 > sec->data_allocated)
|
---|
| 404 | section_realloc(sec, offset1);
|
---|
| 405 | sec->data_offset = offset1;
|
---|
| 406 | return sec->data + offset;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | /* reserve at least 'size' bytes from section start */
|
---|
| 410 | ST_FUNC void section_reserve(Section *sec, unsigned long size)
|
---|
| 411 | {
|
---|
| 412 | if (size > sec->data_allocated)
|
---|
| 413 | section_realloc(sec, size);
|
---|
| 414 | if (size > sec->data_offset)
|
---|
| 415 | sec->data_offset = size;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | /* return a reference to a section, and create it if it does not
|
---|
| 419 | exists */
|
---|
| 420 | ST_FUNC Section *find_section(TCCState *s1, const char *name)
|
---|
| 421 | {
|
---|
| 422 | Section *sec;
|
---|
| 423 | int i;
|
---|
| 424 | for(i = 1; i < s1->nb_sections; i++) {
|
---|
| 425 | sec = s1->sections[i];
|
---|
| 426 | if (!strcmp(name, sec->name))
|
---|
| 427 | return sec;
|
---|
| 428 | }
|
---|
| 429 | /* sections are created as PROGBITS */
|
---|
| 430 | return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | /* update sym->c so that it points to an external symbol in section
|
---|
| 434 | 'section' with value 'value' */
|
---|
| 435 | ST_FUNC void put_extern_sym2(Sym *sym, Section *section,
|
---|
| 436 | addr_t value, unsigned long size,
|
---|
| 437 | int can_add_underscore)
|
---|
| 438 | {
|
---|
| 439 | int sym_type, sym_bind, sh_num, info, other;
|
---|
| 440 | ElfW(Sym) *esym;
|
---|
| 441 | const char *name;
|
---|
| 442 | char buf1[256];
|
---|
| 443 |
|
---|
| 444 | if (section == NULL)
|
---|
| 445 | sh_num = SHN_UNDEF;
|
---|
| 446 | else if (section == SECTION_ABS)
|
---|
| 447 | sh_num = SHN_ABS;
|
---|
| 448 | else
|
---|
| 449 | sh_num = section->sh_num;
|
---|
| 450 |
|
---|
| 451 | if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
|
---|
| 452 | sym_type = STT_FUNC;
|
---|
| 453 | } else if ((sym->type.t & VT_BTYPE) == VT_VOID) {
|
---|
| 454 | sym_type = STT_NOTYPE;
|
---|
| 455 | } else {
|
---|
| 456 | sym_type = STT_OBJECT;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | if (sym->type.t & VT_STATIC)
|
---|
| 460 | sym_bind = STB_LOCAL;
|
---|
| 461 | else {
|
---|
| 462 | if (sym->type.t & VT_WEAK)
|
---|
| 463 | sym_bind = STB_WEAK;
|
---|
| 464 | else
|
---|
| 465 | sym_bind = STB_GLOBAL;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | if (!sym->c) {
|
---|
| 469 | name = get_tok_str(sym->v, NULL);
|
---|
| 470 | #ifdef CONFIG_TCC_BCHECK
|
---|
| 471 | if (tcc_state->do_bounds_check) {
|
---|
| 472 | char buf[32];
|
---|
| 473 |
|
---|
| 474 | /* XXX: avoid doing that for statics ? */
|
---|
| 475 | /* if bound checking is activated, we change some function
|
---|
| 476 | names by adding the "__bound" prefix */
|
---|
| 477 | switch(sym->v) {
|
---|
| 478 | #ifdef TCC_TARGET_PE
|
---|
| 479 | /* XXX: we rely only on malloc hooks */
|
---|
| 480 | case TOK_malloc:
|
---|
| 481 | case TOK_free:
|
---|
| 482 | case TOK_realloc:
|
---|
| 483 | case TOK_memalign:
|
---|
| 484 | case TOK_calloc:
|
---|
| 485 | #endif
|
---|
| 486 | case TOK_memcpy:
|
---|
| 487 | case TOK_memmove:
|
---|
| 488 | case TOK_memset:
|
---|
| 489 | case TOK_strlen:
|
---|
| 490 | case TOK_strcpy:
|
---|
| 491 | #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
---|
| 492 | case TOK_alloca:
|
---|
| 493 | #endif
|
---|
| 494 | strcpy(buf, "__bound_");
|
---|
| 495 | strcat(buf, name);
|
---|
| 496 | name = buf;
|
---|
| 497 | break;
|
---|
| 498 | }
|
---|
| 499 | }
|
---|
| 500 | #endif
|
---|
| 501 | other = 0;
|
---|
| 502 |
|
---|
| 503 | #ifdef TCC_TARGET_PE
|
---|
| 504 | if (sym->type.t & VT_EXPORT)
|
---|
| 505 | other |= 1;
|
---|
| 506 | if (sym_type == STT_FUNC && sym->type.ref) {
|
---|
| 507 | int attr = sym->type.ref->r;
|
---|
| 508 | if (FUNC_EXPORT(attr))
|
---|
| 509 | other |= 1;
|
---|
| 510 | if (FUNC_CALL(attr) == FUNC_STDCALL && can_add_underscore) {
|
---|
| 511 | sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr) * PTR_SIZE);
|
---|
| 512 | name = buf1;
|
---|
| 513 | other |= 2;
|
---|
| 514 | can_add_underscore = 0;
|
---|
| 515 | }
|
---|
| 516 | } else {
|
---|
| 517 | if (find_elf_sym(tcc_state->dynsymtab_section, name))
|
---|
| 518 | other |= 4;
|
---|
| 519 | if (sym->type.t & VT_IMPORT)
|
---|
| 520 | other |= 4;
|
---|
| 521 | }
|
---|
| 522 | #endif
|
---|
| 523 | if (tcc_state->leading_underscore && can_add_underscore) {
|
---|
| 524 | buf1[0] = '_';
|
---|
| 525 | pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
|
---|
| 526 | name = buf1;
|
---|
| 527 | }
|
---|
| 528 | if (sym->asm_label) {
|
---|
| 529 | name = sym->asm_label;
|
---|
| 530 | }
|
---|
| 531 | info = ELFW(ST_INFO)(sym_bind, sym_type);
|
---|
| 532 | sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
|
---|
| 533 | } else {
|
---|
| 534 | esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
|
---|
| 535 | esym->st_value = value;
|
---|
| 536 | esym->st_size = size;
|
---|
| 537 | esym->st_shndx = sh_num;
|
---|
| 538 | }
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 | ST_FUNC void put_extern_sym(Sym *sym, Section *section,
|
---|
| 542 | addr_t value, unsigned long size)
|
---|
| 543 | {
|
---|
| 544 | put_extern_sym2(sym, section, value, size, 1);
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | /* add a new relocation entry to symbol 'sym' in section 's' */
|
---|
| 548 | ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
|
---|
| 549 | {
|
---|
| 550 | int c = 0;
|
---|
| 551 | if (sym) {
|
---|
| 552 | if (0 == sym->c)
|
---|
| 553 | put_extern_sym(sym, NULL, 0, 0);
|
---|
| 554 | c = sym->c;
|
---|
| 555 | }
|
---|
| 556 | /* now we can add ELF relocation info */
|
---|
| 557 | put_elf_reloc(symtab_section, s, offset, type, c);
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | /********************************************************/
|
---|
| 561 |
|
---|
| 562 | static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
|
---|
| 563 | {
|
---|
| 564 | int len;
|
---|
| 565 | len = strlen(buf);
|
---|
| 566 | vsnprintf(buf + len, buf_size - len, fmt, ap);
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 | static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
|
---|
| 570 | {
|
---|
| 571 | va_list ap;
|
---|
| 572 | va_start(ap, fmt);
|
---|
| 573 | strcat_vprintf(buf, buf_size, fmt, ap);
|
---|
| 574 | va_end(ap);
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 | static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
|
---|
| 578 | {
|
---|
| 579 | char buf[2048];
|
---|
| 580 | BufferedFile **pf, *f;
|
---|
| 581 |
|
---|
| 582 | buf[0] = '\0';
|
---|
| 583 | /* use upper file if inline ":asm:" or token ":paste:" */
|
---|
| 584 | for (f = file; f && f->filename[0] == ':'; f = f->prev);
|
---|
| 585 | if (f) {
|
---|
| 586 | for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
|
---|
| 587 | strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
|
---|
| 588 | (*pf)->filename, (*pf)->line_num);
|
---|
| 589 | if (f->line_num > 0) {
|
---|
| 590 | strcat_printf(buf, sizeof(buf), "%s:%d: ",
|
---|
| 591 | f->filename, f->line_num);
|
---|
| 592 | } else {
|
---|
| 593 | strcat_printf(buf, sizeof(buf), "%s: ",
|
---|
| 594 | f->filename);
|
---|
| 595 | }
|
---|
| 596 | } else {
|
---|
| 597 | strcat_printf(buf, sizeof(buf), "tcc: ");
|
---|
| 598 | }
|
---|
| 599 | if (is_warning)
|
---|
| 600 | strcat_printf(buf, sizeof(buf), "warning: ");
|
---|
| 601 | else
|
---|
| 602 | strcat_printf(buf, sizeof(buf), "error: ");
|
---|
| 603 | strcat_vprintf(buf, sizeof(buf), fmt, ap);
|
---|
| 604 |
|
---|
| 605 | if (!s1->error_func) {
|
---|
| 606 | /* default case: stderr */
|
---|
| 607 | fprintf(stderr, "%s\n", buf);
|
---|
| 608 | } else {
|
---|
| 609 | s1->error_func(s1->error_opaque, buf);
|
---|
| 610 | }
|
---|
| 611 | if (!is_warning || s1->warn_error)
|
---|
| 612 | s1->nb_errors++;
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque,
|
---|
| 616 | void (*error_func)(void *opaque, const char *msg))
|
---|
| 617 | {
|
---|
| 618 | s->error_opaque = error_opaque;
|
---|
| 619 | s->error_func = error_func;
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | /* error without aborting current compilation */
|
---|
| 623 | PUB_FUNC void tcc_error_noabort(const char *fmt, ...)
|
---|
| 624 | {
|
---|
| 625 | TCCState *s1 = tcc_state;
|
---|
| 626 | va_list ap;
|
---|
| 627 |
|
---|
| 628 | va_start(ap, fmt);
|
---|
| 629 | error1(s1, 0, fmt, ap);
|
---|
| 630 | va_end(ap);
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 | PUB_FUNC void tcc_error(const char *fmt, ...)
|
---|
| 634 | {
|
---|
| 635 | TCCState *s1 = tcc_state;
|
---|
| 636 | va_list ap;
|
---|
| 637 |
|
---|
| 638 | va_start(ap, fmt);
|
---|
| 639 | error1(s1, 0, fmt, ap);
|
---|
| 640 | va_end(ap);
|
---|
| 641 | /* better than nothing: in some cases, we accept to handle errors */
|
---|
| 642 | if (s1->error_set_jmp_enabled) {
|
---|
| 643 | longjmp(s1->error_jmp_buf, 1);
|
---|
| 644 | } else {
|
---|
| 645 | /* XXX: eliminate this someday */
|
---|
| 646 | exit(1);
|
---|
| 647 | }
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | PUB_FUNC void tcc_warning(const char *fmt, ...)
|
---|
| 651 | {
|
---|
| 652 | TCCState *s1 = tcc_state;
|
---|
| 653 | va_list ap;
|
---|
| 654 |
|
---|
| 655 | if (s1->warn_none)
|
---|
| 656 | return;
|
---|
| 657 |
|
---|
| 658 | va_start(ap, fmt);
|
---|
| 659 | error1(s1, 1, fmt, ap);
|
---|
| 660 | va_end(ap);
|
---|
| 661 | }
|
---|
| 662 |
|
---|
| 663 | /********************************************************/
|
---|
| 664 | /* I/O layer */
|
---|
| 665 |
|
---|
| 666 | ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
|
---|
| 667 | {
|
---|
| 668 | BufferedFile *bf;
|
---|
| 669 | int buflen = initlen ? initlen : IO_BUF_SIZE;
|
---|
| 670 |
|
---|
| 671 | bf = tcc_malloc(sizeof(BufferedFile) + buflen);
|
---|
| 672 | bf->buf_ptr = bf->buffer;
|
---|
| 673 | bf->buf_end = bf->buffer + initlen;
|
---|
| 674 | bf->buf_end[0] = CH_EOB; /* put eob symbol */
|
---|
| 675 | pstrcpy(bf->filename, sizeof(bf->filename), filename);
|
---|
| 676 | #ifdef _WIN32
|
---|
| 677 | normalize_slashes(bf->filename);
|
---|
| 678 | #endif
|
---|
| 679 | bf->line_num = 1;
|
---|
| 680 | bf->ifndef_macro = 0;
|
---|
| 681 | bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
|
---|
| 682 | bf->fd = -1;
|
---|
| 683 | bf->prev = file;
|
---|
| 684 | file = bf;
|
---|
| 685 | }
|
---|
| 686 |
|
---|
| 687 | ST_FUNC void tcc_close(void)
|
---|
| 688 | {
|
---|
| 689 | BufferedFile *bf = file;
|
---|
| 690 | if (bf->fd > 0) {
|
---|
| 691 | close(bf->fd);
|
---|
| 692 | total_lines += bf->line_num;
|
---|
| 693 | }
|
---|
| 694 | file = bf->prev;
|
---|
| 695 | tcc_free(bf);
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | ST_FUNC int tcc_open(TCCState *s1, const char *filename)
|
---|
| 699 | {
|
---|
| 700 | int fd;
|
---|
| 701 | if (strcmp(filename, "-") == 0)
|
---|
| 702 | fd = 0, filename = "stdin";
|
---|
| 703 | else
|
---|
| 704 | fd = open(filename, O_RDONLY | O_BINARY);
|
---|
| 705 | if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
|
---|
| 706 | printf("%s %*s%s\n", fd < 0 ? "nf":"->",
|
---|
| 707 | (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
|
---|
| 708 | if (fd < 0)
|
---|
| 709 | return -1;
|
---|
| 710 |
|
---|
| 711 | tcc_open_bf(s1, filename, 0);
|
---|
| 712 | file->fd = fd;
|
---|
| 713 | return fd;
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | /* compile the C file opened in 'file'. Return non zero if errors. */
|
---|
| 717 | static int tcc_compile(TCCState *s1)
|
---|
| 718 | {
|
---|
| 719 | Sym *define_start;
|
---|
| 720 | SValue *pvtop;
|
---|
| 721 | char buf[512];
|
---|
| 722 | volatile int section_sym;
|
---|
| 723 |
|
---|
| 724 | #ifdef INC_DEBUG
|
---|
| 725 | printf("%s: **** new file\n", file->filename);
|
---|
| 726 | #endif
|
---|
| 727 | preprocess_init(s1);
|
---|
| 728 |
|
---|
| 729 | cur_text_section = NULL;
|
---|
| 730 | funcname = "";
|
---|
| 731 | anon_sym = SYM_FIRST_ANOM;
|
---|
| 732 |
|
---|
| 733 | /* file info: full path + filename */
|
---|
| 734 | section_sym = 0; /* avoid warning */
|
---|
| 735 | if (s1->do_debug) {
|
---|
| 736 | section_sym = put_elf_sym(symtab_section, 0, 0,
|
---|
| 737 | ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
|
---|
| 738 | text_section->sh_num, NULL);
|
---|
| 739 | getcwd(buf, sizeof(buf));
|
---|
| 740 | #ifdef _WIN32
|
---|
| 741 | normalize_slashes(buf);
|
---|
| 742 | #endif
|
---|
| 743 | pstrcat(buf, sizeof(buf), "/");
|
---|
| 744 | put_stabs_r(buf, N_SO, 0, 0,
|
---|
| 745 | text_section->data_offset, text_section, section_sym);
|
---|
| 746 | put_stabs_r(file->filename, N_SO, 0, 0,
|
---|
| 747 | text_section->data_offset, text_section, section_sym);
|
---|
| 748 | }
|
---|
| 749 | /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
|
---|
| 750 | symbols can be safely used */
|
---|
| 751 | put_elf_sym(symtab_section, 0, 0,
|
---|
| 752 | ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
|
---|
| 753 | SHN_ABS, file->filename);
|
---|
| 754 |
|
---|
| 755 | /* define some often used types */
|
---|
| 756 | int_type.t = VT_INT;
|
---|
| 757 |
|
---|
| 758 | char_pointer_type.t = VT_BYTE;
|
---|
| 759 | mk_pointer(&char_pointer_type);
|
---|
| 760 |
|
---|
| 761 | #if PTR_SIZE == 4
|
---|
| 762 | size_type.t = VT_INT;
|
---|
| 763 | #else
|
---|
| 764 | size_type.t = VT_LLONG;
|
---|
| 765 | #endif
|
---|
| 766 |
|
---|
| 767 | func_old_type.t = VT_FUNC;
|
---|
| 768 | func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
|
---|
| 769 | #ifdef TCC_TARGET_ARM
|
---|
| 770 | arm_init_types();
|
---|
| 771 | #endif
|
---|
| 772 |
|
---|
| 773 | #if 0
|
---|
| 774 | /* define 'void *alloca(unsigned int)' builtin function */
|
---|
| 775 | {
|
---|
| 776 | Sym *s1;
|
---|
| 777 |
|
---|
| 778 | p = anon_sym++;
|
---|
| 779 | sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
|
---|
| 780 | s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
|
---|
| 781 | s1->next = NULL;
|
---|
| 782 | sym->next = s1;
|
---|
| 783 | sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
|
---|
| 784 | }
|
---|
| 785 | #endif
|
---|
| 786 |
|
---|
| 787 | define_start = define_stack;
|
---|
| 788 | nocode_wanted = 1;
|
---|
| 789 |
|
---|
| 790 | if (setjmp(s1->error_jmp_buf) == 0) {
|
---|
| 791 | s1->nb_errors = 0;
|
---|
| 792 | s1->error_set_jmp_enabled = 1;
|
---|
| 793 |
|
---|
| 794 | ch = file->buf_ptr[0];
|
---|
| 795 | tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
|
---|
| 796 | parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
|
---|
| 797 | pvtop = vtop;
|
---|
| 798 | next();
|
---|
| 799 | decl(VT_CONST);
|
---|
| 800 | if (tok != TOK_EOF)
|
---|
| 801 | expect("declaration");
|
---|
| 802 | if (pvtop != vtop)
|
---|
| 803 | tcc_warning("internal compiler error: vstack leak? (%d)", vtop - pvtop);
|
---|
| 804 |
|
---|
| 805 | /* end of translation unit info */
|
---|
| 806 | if (s1->do_debug) {
|
---|
| 807 | put_stabs_r(NULL, N_SO, 0, 0,
|
---|
| 808 | text_section->data_offset, text_section, section_sym);
|
---|
| 809 | }
|
---|
| 810 | }
|
---|
| 811 |
|
---|
| 812 | s1->error_set_jmp_enabled = 0;
|
---|
| 813 |
|
---|
| 814 | /* reset define stack, but leave -Dsymbols (may be incorrect if
|
---|
| 815 | they are undefined) */
|
---|
| 816 | free_defines(define_start);
|
---|
| 817 |
|
---|
| 818 | gen_inline_functions();
|
---|
| 819 |
|
---|
| 820 | sym_pop(&global_stack, NULL);
|
---|
| 821 | sym_pop(&local_stack, NULL);
|
---|
| 822 |
|
---|
| 823 | return s1->nb_errors != 0 ? -1 : 0;
|
---|
| 824 | }
|
---|
| 825 |
|
---|
| 826 | LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
|
---|
| 827 | {
|
---|
| 828 | int len, ret;
|
---|
| 829 | len = strlen(str);
|
---|
| 830 |
|
---|
| 831 | tcc_open_bf(s, "<string>", len);
|
---|
| 832 | memcpy(file->buffer, str, len);
|
---|
| 833 | ret = tcc_compile(s);
|
---|
| 834 | tcc_close();
|
---|
| 835 | return ret;
|
---|
| 836 | }
|
---|
| 837 |
|
---|
| 838 | /* define a preprocessor symbol. A value can also be provided with the '=' operator */
|
---|
| 839 | LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
|
---|
| 840 | {
|
---|
| 841 | int len1, len2;
|
---|
| 842 | /* default value */
|
---|
| 843 | if (!value)
|
---|
| 844 | value = "1";
|
---|
| 845 | len1 = strlen(sym);
|
---|
| 846 | len2 = strlen(value);
|
---|
| 847 |
|
---|
| 848 | /* init file structure */
|
---|
| 849 | tcc_open_bf(s1, "<define>", len1 + len2 + 1);
|
---|
| 850 | memcpy(file->buffer, sym, len1);
|
---|
| 851 | file->buffer[len1] = ' ';
|
---|
| 852 | memcpy(file->buffer + len1 + 1, value, len2);
|
---|
| 853 |
|
---|
| 854 | /* parse with define parser */
|
---|
| 855 | ch = file->buf_ptr[0];
|
---|
| 856 | next_nomacro();
|
---|
| 857 | parse_define();
|
---|
| 858 |
|
---|
| 859 | tcc_close();
|
---|
| 860 | }
|
---|
| 861 |
|
---|
| 862 | /* undefine a preprocessor symbol */
|
---|
| 863 | LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
|
---|
| 864 | {
|
---|
| 865 | TokenSym *ts;
|
---|
| 866 | Sym *s;
|
---|
| 867 | ts = tok_alloc(sym, strlen(sym));
|
---|
| 868 | s = define_find(ts->tok);
|
---|
| 869 | /* undefine symbol by putting an invalid name */
|
---|
| 870 | if (s)
|
---|
| 871 | define_undef(s);
|
---|
| 872 | }
|
---|
| 873 |
|
---|
| 874 | /* cleanup all static data used during compilation */
|
---|
| 875 | static void tcc_cleanup(void)
|
---|
| 876 | {
|
---|
| 877 | int i, n;
|
---|
| 878 | if (NULL == tcc_state)
|
---|
| 879 | return;
|
---|
| 880 | tcc_state = NULL;
|
---|
| 881 |
|
---|
| 882 | /* free -D defines */
|
---|
| 883 | free_defines(NULL);
|
---|
| 884 |
|
---|
| 885 | /* free tokens */
|
---|
| 886 | n = tok_ident - TOK_IDENT;
|
---|
| 887 | for(i = 0; i < n; i++)
|
---|
| 888 | tcc_free(table_ident[i]);
|
---|
| 889 | tcc_free(table_ident);
|
---|
| 890 |
|
---|
| 891 | /* free sym_pools */
|
---|
| 892 | dynarray_reset(&sym_pools, &nb_sym_pools);
|
---|
| 893 | /* string buffer */
|
---|
| 894 | cstr_free(&tokcstr);
|
---|
| 895 | /* reset symbol stack */
|
---|
| 896 | sym_free_first = NULL;
|
---|
| 897 | /* cleanup from error/setjmp */
|
---|
| 898 | macro_ptr = NULL;
|
---|
| 899 | }
|
---|
| 900 |
|
---|
| 901 | LIBTCCAPI TCCState *tcc_new(void)
|
---|
| 902 | {
|
---|
| 903 | TCCState *s;
|
---|
| 904 | char buffer[100];
|
---|
| 905 | int a,b,c;
|
---|
| 906 |
|
---|
| 907 | tcc_cleanup();
|
---|
| 908 |
|
---|
| 909 | s = tcc_mallocz(sizeof(TCCState));
|
---|
| 910 | if (!s)
|
---|
| 911 | return NULL;
|
---|
| 912 | tcc_state = s;
|
---|
| 913 | #ifdef _WIN32
|
---|
| 914 | tcc_set_lib_path_w32(s);
|
---|
| 915 | #else
|
---|
| 916 | tcc_set_lib_path(s, CONFIG_TCCDIR);
|
---|
| 917 | #endif
|
---|
| 918 | s->output_type = TCC_OUTPUT_MEMORY;
|
---|
| 919 | preprocess_new();
|
---|
| 920 | s->include_stack_ptr = s->include_stack;
|
---|
| 921 |
|
---|
| 922 | /* we add dummy defines for some special macros to speed up tests
|
---|
| 923 | and to have working defined() */
|
---|
| 924 | define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
|
---|
| 925 | define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
|
---|
| 926 | define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
|
---|
| 927 | define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
|
---|
| 928 |
|
---|
| 929 | /* define __TINYC__ 92X */
|
---|
| 930 | sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
|
---|
| 931 | sprintf(buffer, "%d", a*10000 + b*100 + c);
|
---|
| 932 | tcc_define_symbol(s, "__TINYC__", buffer);
|
---|
| 933 |
|
---|
| 934 | /* standard defines */
|
---|
| 935 | tcc_define_symbol(s, "__STDC__", NULL);
|
---|
| 936 | tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
|
---|
| 937 |
|
---|
| 938 | /* target defines */
|
---|
| 939 | #if defined(TCC_TARGET_I386)
|
---|
| 940 | tcc_define_symbol(s, "__i386__", NULL);
|
---|
| 941 | tcc_define_symbol(s, "__i386", NULL);
|
---|
| 942 | tcc_define_symbol(s, "i386", NULL);
|
---|
| 943 | #elif defined(TCC_TARGET_X86_64)
|
---|
| 944 | tcc_define_symbol(s, "__x86_64__", NULL);
|
---|
| 945 | #elif defined(TCC_TARGET_ARM)
|
---|
| 946 | tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
|
---|
| 947 | tcc_define_symbol(s, "__arm_elf__", NULL);
|
---|
| 948 | tcc_define_symbol(s, "__arm_elf", NULL);
|
---|
| 949 | tcc_define_symbol(s, "arm_elf", NULL);
|
---|
| 950 | tcc_define_symbol(s, "__arm__", NULL);
|
---|
| 951 | tcc_define_symbol(s, "__arm", NULL);
|
---|
| 952 | tcc_define_symbol(s, "arm", NULL);
|
---|
| 953 | tcc_define_symbol(s, "__APCS_32__", NULL);
|
---|
| 954 | #endif
|
---|
| 955 |
|
---|
| 956 | #ifdef TCC_TARGET_PE
|
---|
| 957 | tcc_define_symbol(s, "_WIN32", NULL);
|
---|
| 958 | # ifdef TCC_TARGET_X86_64
|
---|
| 959 | tcc_define_symbol(s, "_WIN64", NULL);
|
---|
| 960 | # endif
|
---|
| 961 | #else
|
---|
| 962 | tcc_define_symbol(s, "__unix__", NULL);
|
---|
| 963 | tcc_define_symbol(s, "__unix", NULL);
|
---|
| 964 | tcc_define_symbol(s, "unix", NULL);
|
---|
| 965 | # if defined(__linux)
|
---|
| 966 | tcc_define_symbol(s, "__linux__", NULL);
|
---|
| 967 | tcc_define_symbol(s, "__linux", NULL);
|
---|
| 968 | # endif
|
---|
| 969 | # if defined(__FreeBSD__)
|
---|
| 970 | # define str(s) #s
|
---|
| 971 | tcc_define_symbol(s, "__FreeBSD__", str( __FreeBSD__));
|
---|
| 972 | # undef str
|
---|
| 973 | # endif
|
---|
| 974 | # if defined(__FreeBSD_kernel__)
|
---|
| 975 | tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
|
---|
| 976 | # endif
|
---|
| 977 | #endif
|
---|
| 978 |
|
---|
| 979 | /* TinyCC & gcc defines */
|
---|
| 980 | #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
|
---|
| 981 | tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
|
---|
| 982 | tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
|
---|
| 983 | #else
|
---|
| 984 | tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
|
---|
| 985 | tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
|
---|
| 986 | #endif
|
---|
| 987 |
|
---|
| 988 | #ifdef TCC_TARGET_PE
|
---|
| 989 | tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
|
---|
| 990 | #else
|
---|
| 991 | tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
|
---|
| 992 | #endif
|
---|
| 993 |
|
---|
| 994 | #ifndef TCC_TARGET_PE
|
---|
| 995 | /* glibc defines */
|
---|
| 996 | tcc_define_symbol(s, "__REDIRECT(name, proto, alias)", "name proto __asm__ (#alias)");
|
---|
| 997 | tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)", "name proto __asm__ (#alias) __THROW");
|
---|
| 998 | /* default library paths */
|
---|
| 999 | tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
|
---|
| 1000 | /* paths for crt objects */
|
---|
| 1001 | tcc_split_path(s, (void ***)&s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
|
---|
| 1002 | #endif
|
---|
| 1003 |
|
---|
| 1004 | /* no section zero */
|
---|
| 1005 | dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
|
---|
| 1006 |
|
---|
| 1007 | /* create standard sections */
|
---|
| 1008 | text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
|
---|
| 1009 | data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
|
---|
| 1010 | bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
|
---|
| 1011 |
|
---|
| 1012 | /* symbols are always generated for linking stage */
|
---|
| 1013 | symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
|
---|
| 1014 | ".strtab",
|
---|
| 1015 | ".hashtab", SHF_PRIVATE);
|
---|
| 1016 | strtab_section = symtab_section->link;
|
---|
| 1017 | s->symtab = symtab_section;
|
---|
| 1018 |
|
---|
| 1019 | /* private symbol table for dynamic symbols */
|
---|
| 1020 | s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
|
---|
| 1021 | ".dynstrtab",
|
---|
| 1022 | ".dynhashtab", SHF_PRIVATE);
|
---|
| 1023 | s->alacarte_link = 1;
|
---|
| 1024 | s->nocommon = 1;
|
---|
| 1025 | s->section_align = ELF_PAGE_SIZE;
|
---|
| 1026 |
|
---|
| 1027 | #ifdef CHAR_IS_UNSIGNED
|
---|
| 1028 | s->char_is_unsigned = 1;
|
---|
| 1029 | #endif
|
---|
| 1030 | /* enable this if you want symbols with leading underscore on windows: */
|
---|
| 1031 | #if 0 //def TCC_TARGET_PE
|
---|
| 1032 | s->leading_underscore = 1;
|
---|
| 1033 | #endif
|
---|
| 1034 | #ifdef TCC_TARGET_I386
|
---|
| 1035 | s->seg_size = 32;
|
---|
| 1036 | #endif
|
---|
| 1037 | return s;
|
---|
| 1038 | }
|
---|
| 1039 |
|
---|
| 1040 | LIBTCCAPI void tcc_delete(TCCState *s1)
|
---|
| 1041 | {
|
---|
| 1042 | int i;
|
---|
| 1043 |
|
---|
| 1044 | tcc_cleanup();
|
---|
| 1045 |
|
---|
| 1046 | /* free all sections */
|
---|
| 1047 | for(i = 1; i < s1->nb_sections; i++)
|
---|
| 1048 | free_section(s1->sections[i]);
|
---|
| 1049 | dynarray_reset(&s1->sections, &s1->nb_sections);
|
---|
| 1050 |
|
---|
| 1051 | for(i = 0; i < s1->nb_priv_sections; i++)
|
---|
| 1052 | free_section(s1->priv_sections[i]);
|
---|
| 1053 | dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
|
---|
| 1054 |
|
---|
| 1055 | /* free any loaded DLLs */
|
---|
| 1056 | for ( i = 0; i < s1->nb_loaded_dlls; i++) {
|
---|
| 1057 | DLLReference *ref = s1->loaded_dlls[i];
|
---|
| 1058 | if ( ref->handle )
|
---|
| 1059 | dlclose(ref->handle);
|
---|
| 1060 | }
|
---|
| 1061 |
|
---|
| 1062 | /* free loaded dlls array */
|
---|
| 1063 | dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
|
---|
| 1064 |
|
---|
| 1065 | /* free library paths */
|
---|
| 1066 | dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
|
---|
| 1067 | dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
|
---|
| 1068 |
|
---|
| 1069 | /* free include paths */
|
---|
| 1070 | dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
|
---|
| 1071 | dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
|
---|
| 1072 | dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
|
---|
| 1073 |
|
---|
| 1074 | tcc_free(s1->tcc_lib_path);
|
---|
| 1075 | tcc_free(s1->soname);
|
---|
| 1076 | tcc_free(s1->rpath);
|
---|
| 1077 | tcc_free(s1->init_symbol);
|
---|
| 1078 | tcc_free(s1->fini_symbol);
|
---|
| 1079 | tcc_free(s1->outfile);
|
---|
| 1080 | tcc_free(s1->deps_outfile);
|
---|
| 1081 | dynarray_reset(&s1->files, &s1->nb_files);
|
---|
| 1082 | dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
|
---|
| 1083 |
|
---|
| 1084 | #ifdef TCC_IS_NATIVE
|
---|
| 1085 | # ifdef HAVE_SELINUX
|
---|
| 1086 | munmap (s1->write_mem, s1->mem_size);
|
---|
| 1087 | munmap (s1->runtime_mem, s1->mem_size);
|
---|
| 1088 | # else
|
---|
| 1089 | tcc_free(s1->runtime_mem);
|
---|
| 1090 | # endif
|
---|
| 1091 | #endif
|
---|
| 1092 |
|
---|
| 1093 | tcc_free(s1);
|
---|
| 1094 | }
|
---|
| 1095 |
|
---|
| 1096 | LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
|
---|
| 1097 | {
|
---|
| 1098 | tcc_split_path(s, (void ***)&s->include_paths, &s->nb_include_paths, pathname);
|
---|
| 1099 | return 0;
|
---|
| 1100 | }
|
---|
| 1101 |
|
---|
| 1102 | LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
|
---|
| 1103 | {
|
---|
| 1104 | tcc_split_path(s, (void ***)&s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
|
---|
| 1105 | return 0;
|
---|
| 1106 | }
|
---|
| 1107 |
|
---|
| 1108 | ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
|
---|
| 1109 | {
|
---|
| 1110 | const char *ext;
|
---|
| 1111 | ElfW(Ehdr) ehdr;
|
---|
| 1112 | int fd, ret, size;
|
---|
| 1113 |
|
---|
| 1114 | /* find source file type with extension */
|
---|
| 1115 | ext = tcc_fileextension(filename);
|
---|
| 1116 | if (ext[0])
|
---|
| 1117 | ext++;
|
---|
| 1118 |
|
---|
| 1119 | #ifdef CONFIG_TCC_ASM
|
---|
| 1120 | /* if .S file, define __ASSEMBLER__ like gcc does */
|
---|
| 1121 | if (!strcmp(ext, "S"))
|
---|
| 1122 | tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
|
---|
| 1123 | #endif
|
---|
| 1124 |
|
---|
| 1125 | /* open the file */
|
---|
| 1126 | ret = tcc_open(s1, filename);
|
---|
| 1127 | if (ret < 0) {
|
---|
| 1128 | if (flags & AFF_PRINT_ERROR)
|
---|
| 1129 | tcc_error_noabort("file '%s' not found", filename);
|
---|
| 1130 | return ret;
|
---|
| 1131 | }
|
---|
| 1132 |
|
---|
| 1133 | /* update target deps */
|
---|
| 1134 | dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
|
---|
| 1135 | tcc_strdup(filename));
|
---|
| 1136 |
|
---|
| 1137 | if (flags & AFF_PREPROCESS) {
|
---|
| 1138 | ret = tcc_preprocess(s1);
|
---|
| 1139 | goto the_end;
|
---|
| 1140 | }
|
---|
| 1141 |
|
---|
| 1142 | if (!ext[0] || !PATHCMP(ext, "c")) {
|
---|
| 1143 | /* C file assumed */
|
---|
| 1144 | ret = tcc_compile(s1);
|
---|
| 1145 | goto the_end;
|
---|
| 1146 | }
|
---|
| 1147 |
|
---|
| 1148 | #ifdef CONFIG_TCC_ASM
|
---|
| 1149 | if (!strcmp(ext, "S")) {
|
---|
| 1150 | /* preprocessed assembler */
|
---|
| 1151 | ret = tcc_assemble(s1, 1);
|
---|
| 1152 | goto the_end;
|
---|
| 1153 | }
|
---|
| 1154 |
|
---|
| 1155 | if (!strcmp(ext, "s")) {
|
---|
| 1156 | /* non preprocessed assembler */
|
---|
| 1157 | ret = tcc_assemble(s1, 0);
|
---|
| 1158 | goto the_end;
|
---|
| 1159 | }
|
---|
| 1160 | #endif
|
---|
| 1161 |
|
---|
| 1162 | fd = file->fd;
|
---|
| 1163 | /* assume executable format: auto guess file type */
|
---|
| 1164 | size = read(fd, &ehdr, sizeof(ehdr));
|
---|
| 1165 | lseek(fd, 0, SEEK_SET);
|
---|
| 1166 | if (size <= 0) {
|
---|
| 1167 | tcc_error_noabort("could not read header");
|
---|
| 1168 | goto the_end;
|
---|
| 1169 | }
|
---|
| 1170 |
|
---|
| 1171 | if (size == sizeof(ehdr) &&
|
---|
| 1172 | ehdr.e_ident[0] == ELFMAG0 &&
|
---|
| 1173 | ehdr.e_ident[1] == ELFMAG1 &&
|
---|
| 1174 | ehdr.e_ident[2] == ELFMAG2 &&
|
---|
| 1175 | ehdr.e_ident[3] == ELFMAG3) {
|
---|
| 1176 |
|
---|
| 1177 | /* do not display line number if error */
|
---|
| 1178 | file->line_num = 0;
|
---|
| 1179 | if (ehdr.e_type == ET_REL) {
|
---|
| 1180 | ret = tcc_load_object_file(s1, fd, 0);
|
---|
| 1181 | goto the_end;
|
---|
| 1182 |
|
---|
| 1183 | }
|
---|
| 1184 | #ifndef TCC_TARGET_PE
|
---|
| 1185 | if (ehdr.e_type == ET_DYN) {
|
---|
| 1186 | if (s1->output_type == TCC_OUTPUT_MEMORY) {
|
---|
| 1187 | #ifdef TCC_IS_NATIVE
|
---|
| 1188 | void *h;
|
---|
| 1189 | h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
|
---|
| 1190 | if (h)
|
---|
| 1191 | #endif
|
---|
| 1192 | ret = 0;
|
---|
| 1193 | } else {
|
---|
| 1194 | ret = tcc_load_dll(s1, fd, filename,
|
---|
| 1195 | (flags & AFF_REFERENCED_DLL) != 0);
|
---|
| 1196 | }
|
---|
| 1197 | goto the_end;
|
---|
| 1198 | }
|
---|
| 1199 | #endif
|
---|
| 1200 | tcc_error_noabort("unrecognized ELF file");
|
---|
| 1201 | goto the_end;
|
---|
| 1202 | }
|
---|
| 1203 |
|
---|
| 1204 | if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
|
---|
| 1205 | file->line_num = 0; /* do not display line number if error */
|
---|
| 1206 | ret = tcc_load_archive(s1, fd);
|
---|
| 1207 | goto the_end;
|
---|
| 1208 | }
|
---|
| 1209 |
|
---|
| 1210 | #ifdef TCC_TARGET_COFF
|
---|
| 1211 | if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
|
---|
| 1212 | ret = tcc_load_coff(s1, fd);
|
---|
| 1213 | goto the_end;
|
---|
| 1214 | }
|
---|
| 1215 | #endif
|
---|
| 1216 |
|
---|
| 1217 | #ifdef TCC_TARGET_PE
|
---|
| 1218 | ret = pe_load_file(s1, filename, fd);
|
---|
| 1219 | #else
|
---|
| 1220 | /* as GNU ld, consider it is an ld script if not recognized */
|
---|
| 1221 | ret = tcc_load_ldscript(s1);
|
---|
| 1222 | #endif
|
---|
| 1223 | if (ret < 0)
|
---|
| 1224 | tcc_error_noabort("unrecognized file type");
|
---|
| 1225 |
|
---|
| 1226 | the_end:
|
---|
| 1227 | tcc_close();
|
---|
| 1228 | return ret;
|
---|
| 1229 | }
|
---|
| 1230 |
|
---|
| 1231 | LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
|
---|
| 1232 | {
|
---|
| 1233 | if (s->output_type == TCC_OUTPUT_PREPROCESS)
|
---|
| 1234 | return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | AFF_PREPROCESS);
|
---|
| 1235 | else
|
---|
| 1236 | return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
|
---|
| 1237 | }
|
---|
| 1238 |
|
---|
| 1239 | LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
|
---|
| 1240 | {
|
---|
| 1241 | tcc_split_path(s, (void ***)&s->library_paths, &s->nb_library_paths, pathname);
|
---|
| 1242 | return 0;
|
---|
| 1243 | }
|
---|
| 1244 |
|
---|
| 1245 | static int tcc_add_library_internal(TCCState *s, const char *fmt,
|
---|
| 1246 | const char *filename, int flags, char **paths, int nb_paths)
|
---|
| 1247 | {
|
---|
| 1248 | char buf[1024];
|
---|
| 1249 | int i;
|
---|
| 1250 |
|
---|
| 1251 | for(i = 0; i < nb_paths; i++) {
|
---|
| 1252 | snprintf(buf, sizeof(buf), fmt, paths[i], filename);
|
---|
| 1253 | if (tcc_add_file_internal(s, buf, flags) == 0)
|
---|
| 1254 | return 0;
|
---|
| 1255 | }
|
---|
| 1256 | return -1;
|
---|
| 1257 | }
|
---|
| 1258 |
|
---|
| 1259 | /* find and load a dll. Return non zero if not found */
|
---|
| 1260 | /* XXX: add '-rpath' option support ? */
|
---|
| 1261 | ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
|
---|
| 1262 | {
|
---|
| 1263 | return tcc_add_library_internal(s, "%s/%s", filename, flags,
|
---|
| 1264 | s->library_paths, s->nb_library_paths);
|
---|
| 1265 | }
|
---|
| 1266 |
|
---|
| 1267 | ST_FUNC int tcc_add_crt(TCCState *s, const char *filename)
|
---|
| 1268 | {
|
---|
| 1269 | if (-1 == tcc_add_library_internal(s, "%s/%s",
|
---|
| 1270 | filename, 0, s->crt_paths, s->nb_crt_paths))
|
---|
| 1271 | tcc_error_noabort("file '%s' not found", filename);
|
---|
| 1272 | return 0;
|
---|
| 1273 | }
|
---|
| 1274 |
|
---|
| 1275 | /* the library name is the same as the argument of the '-l' option */
|
---|
| 1276 | LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
|
---|
| 1277 | {
|
---|
| 1278 | #ifdef TCC_TARGET_PE
|
---|
| 1279 | const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
|
---|
| 1280 | const char **pp = s->static_link ? libs + 4 : libs;
|
---|
| 1281 | #else
|
---|
| 1282 | const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
|
---|
| 1283 | const char **pp = s->static_link ? libs + 1 : libs;
|
---|
| 1284 | #endif
|
---|
| 1285 | while (*pp) {
|
---|
| 1286 | if (0 == tcc_add_library_internal(s, *pp,
|
---|
| 1287 | libraryname, 0, s->library_paths, s->nb_library_paths))
|
---|
| 1288 | return 0;
|
---|
| 1289 | ++pp;
|
---|
| 1290 | }
|
---|
| 1291 | return -1;
|
---|
| 1292 | }
|
---|
| 1293 |
|
---|
| 1294 | LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
|
---|
| 1295 | {
|
---|
| 1296 | #ifdef TCC_TARGET_PE
|
---|
| 1297 | /* On x86_64 'val' might not be reachable with a 32bit offset.
|
---|
| 1298 | So it is handled here as if it were in a DLL. */
|
---|
| 1299 | pe_putimport(s, 0, name, (uintptr_t)val);
|
---|
| 1300 | #else
|
---|
| 1301 | /* XXX: Same problem on linux but currently "solved" elsewhere
|
---|
| 1302 | via the rather dirty 'runtime_plt_and_got' hack. */
|
---|
| 1303 | add_elf_sym(symtab_section, (uintptr_t)val, 0,
|
---|
| 1304 | ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
|
---|
| 1305 | SHN_ABS, name);
|
---|
| 1306 | #endif
|
---|
| 1307 | return 0;
|
---|
| 1308 | }
|
---|
| 1309 |
|
---|
| 1310 | LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
|
---|
| 1311 | {
|
---|
| 1312 | s->output_type = output_type;
|
---|
| 1313 |
|
---|
| 1314 | if (!s->nostdinc) {
|
---|
| 1315 | /* default include paths */
|
---|
| 1316 | /* -isystem paths have already been handled */
|
---|
| 1317 | tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
|
---|
| 1318 | }
|
---|
| 1319 |
|
---|
| 1320 | /* if bound checking, then add corresponding sections */
|
---|
| 1321 | #ifdef CONFIG_TCC_BCHECK
|
---|
| 1322 | if (s->do_bounds_check) {
|
---|
| 1323 | /* define symbol */
|
---|
| 1324 | tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
|
---|
| 1325 | /* create bounds sections */
|
---|
| 1326 | bounds_section = new_section(s, ".bounds",
|
---|
| 1327 | SHT_PROGBITS, SHF_ALLOC);
|
---|
| 1328 | lbounds_section = new_section(s, ".lbounds",
|
---|
| 1329 | SHT_PROGBITS, SHF_ALLOC);
|
---|
| 1330 | }
|
---|
| 1331 | #endif
|
---|
| 1332 |
|
---|
| 1333 | if (s->char_is_unsigned) {
|
---|
| 1334 | tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
|
---|
| 1335 | }
|
---|
| 1336 |
|
---|
| 1337 | /* add debug sections */
|
---|
| 1338 | if (s->do_debug) {
|
---|
| 1339 | /* stab symbols */
|
---|
| 1340 | stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
|
---|
| 1341 | stab_section->sh_entsize = sizeof(Stab_Sym);
|
---|
| 1342 | stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
|
---|
| 1343 | put_elf_str(stabstr_section, "");
|
---|
| 1344 | stab_section->link = stabstr_section;
|
---|
| 1345 | /* put first entry */
|
---|
| 1346 | put_stabs("", 0, 0, 0, 0);
|
---|
| 1347 | }
|
---|
| 1348 |
|
---|
| 1349 | #ifdef TCC_TARGET_PE
|
---|
| 1350 | tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
|
---|
| 1351 | # ifdef _WIN32
|
---|
| 1352 | tcc_add_systemdir(s);
|
---|
| 1353 | # endif
|
---|
| 1354 | #else
|
---|
| 1355 | /* add libc crt1/crti objects */
|
---|
| 1356 | if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
|
---|
| 1357 | !s->nostdlib) {
|
---|
| 1358 | if (output_type != TCC_OUTPUT_DLL)
|
---|
| 1359 | tcc_add_crt(s, "crt1.o");
|
---|
| 1360 | tcc_add_crt(s, "crti.o");
|
---|
| 1361 | }
|
---|
| 1362 | #endif
|
---|
| 1363 | return 0;
|
---|
| 1364 | }
|
---|
| 1365 |
|
---|
| 1366 | LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
|
---|
| 1367 | {
|
---|
| 1368 | tcc_free(s->tcc_lib_path);
|
---|
| 1369 | s->tcc_lib_path = tcc_strdup(path);
|
---|
| 1370 | }
|
---|
| 1371 |
|
---|
| 1372 | #define WD_ALL 0x0001 /* warning is activated when using -Wall */
|
---|
| 1373 | #define FD_INVERT 0x0002 /* invert value before storing */
|
---|
| 1374 |
|
---|
| 1375 | typedef struct FlagDef {
|
---|
| 1376 | uint16_t offset;
|
---|
| 1377 | uint16_t flags;
|
---|
| 1378 | const char *name;
|
---|
| 1379 | } FlagDef;
|
---|
| 1380 |
|
---|
| 1381 | static const FlagDef warning_defs[] = {
|
---|
| 1382 | { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
|
---|
| 1383 | { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
|
---|
| 1384 | { offsetof(TCCState, warn_error), 0, "error" },
|
---|
| 1385 | { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
|
---|
| 1386 | "implicit-function-declaration" },
|
---|
| 1387 | };
|
---|
| 1388 |
|
---|
| 1389 | ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
|
---|
| 1390 | const char *name, int value)
|
---|
| 1391 | {
|
---|
| 1392 | int i;
|
---|
| 1393 | const FlagDef *p;
|
---|
| 1394 | const char *r;
|
---|
| 1395 |
|
---|
| 1396 | r = name;
|
---|
| 1397 | if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
|
---|
| 1398 | r += 3;
|
---|
| 1399 | value = !value;
|
---|
| 1400 | }
|
---|
| 1401 | for(i = 0, p = flags; i < nb_flags; i++, p++) {
|
---|
| 1402 | if (!strcmp(r, p->name))
|
---|
| 1403 | goto found;
|
---|
| 1404 | }
|
---|
| 1405 | return -1;
|
---|
| 1406 | found:
|
---|
| 1407 | if (p->flags & FD_INVERT)
|
---|
| 1408 | value = !value;
|
---|
| 1409 | *(int *)((uint8_t *)s + p->offset) = value;
|
---|
| 1410 | return 0;
|
---|
| 1411 | }
|
---|
| 1412 |
|
---|
| 1413 | /* set/reset a warning */
|
---|
| 1414 | static int tcc_set_warning(TCCState *s, const char *warning_name, int value)
|
---|
| 1415 | {
|
---|
| 1416 | int i;
|
---|
| 1417 | const FlagDef *p;
|
---|
| 1418 |
|
---|
| 1419 | if (!strcmp(warning_name, "all")) {
|
---|
| 1420 | for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
|
---|
| 1421 | if (p->flags & WD_ALL)
|
---|
| 1422 | *(int *)((uint8_t *)s + p->offset) = 1;
|
---|
| 1423 | }
|
---|
| 1424 | return 0;
|
---|
| 1425 | } else {
|
---|
| 1426 | return set_flag(s, warning_defs, countof(warning_defs),
|
---|
| 1427 | warning_name, value);
|
---|
| 1428 | }
|
---|
| 1429 | }
|
---|
| 1430 |
|
---|
| 1431 | static const FlagDef flag_defs[] = {
|
---|
| 1432 | { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
|
---|
| 1433 | { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
|
---|
| 1434 | { offsetof(TCCState, nocommon), FD_INVERT, "common" },
|
---|
| 1435 | { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
|
---|
| 1436 | };
|
---|
| 1437 |
|
---|
| 1438 | /* set/reset a flag */
|
---|
| 1439 | static int tcc_set_flag(TCCState *s, const char *flag_name, int value)
|
---|
| 1440 | {
|
---|
| 1441 | return set_flag(s, flag_defs, countof(flag_defs),
|
---|
| 1442 | flag_name, value);
|
---|
| 1443 | }
|
---|
| 1444 |
|
---|
| 1445 |
|
---|
| 1446 | static int strstart(const char *val, const char **str)
|
---|
| 1447 | {
|
---|
| 1448 | const char *p, *q;
|
---|
| 1449 | p = *str;
|
---|
| 1450 | q = val;
|
---|
| 1451 | while (*q) {
|
---|
| 1452 | if (*p != *q)
|
---|
| 1453 | return 0;
|
---|
| 1454 | p++;
|
---|
| 1455 | q++;
|
---|
| 1456 | }
|
---|
| 1457 | *str = p;
|
---|
| 1458 | return 1;
|
---|
| 1459 | }
|
---|
| 1460 |
|
---|
| 1461 | /* Like strstart, but automatically takes into account that ld options can
|
---|
| 1462 | *
|
---|
| 1463 | * - start with double or single dash (e.g. '--soname' or '-soname')
|
---|
| 1464 | * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
|
---|
| 1465 | * or '-Wl,-soname=x.so')
|
---|
| 1466 | *
|
---|
| 1467 | * you provide `val` always in 'option[=]' form (no leading -)
|
---|
| 1468 | */
|
---|
| 1469 | static int link_option(const char *str, const char *val, const char **ptr)
|
---|
| 1470 | {
|
---|
| 1471 | const char *p, *q;
|
---|
| 1472 |
|
---|
| 1473 | /* there should be 1 or 2 dashes */
|
---|
| 1474 | if (*str++ != '-')
|
---|
| 1475 | return 0;
|
---|
| 1476 | if (*str == '-')
|
---|
| 1477 | str++;
|
---|
| 1478 |
|
---|
| 1479 | /* then str & val should match (potentialy up to '=') */
|
---|
| 1480 | p = str;
|
---|
| 1481 | q = val;
|
---|
| 1482 |
|
---|
| 1483 | while (*q != '\0' && *q != '=') {
|
---|
| 1484 | if (*p != *q)
|
---|
| 1485 | return 0;
|
---|
| 1486 | p++;
|
---|
| 1487 | q++;
|
---|
| 1488 | }
|
---|
| 1489 |
|
---|
| 1490 | /* '=' near eos means ',' or '=' is ok */
|
---|
| 1491 | if (*q == '=') {
|
---|
| 1492 | if (*p != ',' && *p != '=')
|
---|
| 1493 | return 0;
|
---|
| 1494 | p++;
|
---|
| 1495 | q++;
|
---|
| 1496 | }
|
---|
| 1497 |
|
---|
| 1498 | if (ptr)
|
---|
| 1499 | *ptr = p;
|
---|
| 1500 | return 1;
|
---|
| 1501 | }
|
---|
| 1502 |
|
---|
| 1503 | static const char *skip_linker_arg(const char **str)
|
---|
| 1504 | {
|
---|
| 1505 | const char *s1 = *str;
|
---|
| 1506 | const char *s2 = strchr(s1, ',');
|
---|
| 1507 | *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
|
---|
| 1508 | return s2;
|
---|
| 1509 | }
|
---|
| 1510 |
|
---|
| 1511 | static char *copy_linker_arg(const char *p)
|
---|
| 1512 | {
|
---|
| 1513 | const char *q = p;
|
---|
| 1514 | skip_linker_arg(&q);
|
---|
| 1515 | return pstrncpy(tcc_malloc(q - p + 1), p, q - p);
|
---|
| 1516 | }
|
---|
| 1517 |
|
---|
| 1518 | /* set linker options */
|
---|
| 1519 | static int tcc_set_linker(TCCState *s, const char *option)
|
---|
| 1520 | {
|
---|
| 1521 | while (option && *option) {
|
---|
| 1522 |
|
---|
| 1523 | const char *p = option;
|
---|
| 1524 | char *end = NULL;
|
---|
| 1525 | int ignoring = 0;
|
---|
| 1526 |
|
---|
| 1527 | if (link_option(option, "Bsymbolic", &p)) {
|
---|
| 1528 | s->symbolic = 1;
|
---|
| 1529 | } else if (link_option(option, "nostdlib", &p)) {
|
---|
| 1530 | s->nostdlib = 1;
|
---|
| 1531 | } else if (link_option(option, "fini=", &p)) {
|
---|
| 1532 | s->fini_symbol = copy_linker_arg(p);
|
---|
| 1533 | ignoring = 1;
|
---|
| 1534 | } else if (link_option(option, "image-base=", &p)
|
---|
| 1535 | || link_option(option, "Ttext=", &p)) {
|
---|
| 1536 | s->text_addr = strtoull(p, &end, 16);
|
---|
| 1537 | s->has_text_addr = 1;
|
---|
| 1538 | } else if (link_option(option, "init=", &p)) {
|
---|
| 1539 | s->init_symbol = copy_linker_arg(p);
|
---|
| 1540 | ignoring = 1;
|
---|
| 1541 | } else if (link_option(option, "oformat=", &p)) {
|
---|
| 1542 | #if defined(TCC_TARGET_PE)
|
---|
| 1543 | if (strstart("pe-", &p)) {
|
---|
| 1544 | #elif defined(TCC_TARGET_X86_64)
|
---|
| 1545 | if (strstart("elf64-", &p)) {
|
---|
| 1546 | #else
|
---|
| 1547 | if (strstart("elf32-", &p)) {
|
---|
| 1548 | #endif
|
---|
| 1549 | s->output_format = TCC_OUTPUT_FORMAT_ELF;
|
---|
| 1550 | } else if (!strcmp(p, "binary")) {
|
---|
| 1551 | s->output_format = TCC_OUTPUT_FORMAT_BINARY;
|
---|
| 1552 | #ifdef TCC_TARGET_COFF
|
---|
| 1553 | } else if (!strcmp(p, "coff")) {
|
---|
| 1554 | s->output_format = TCC_OUTPUT_FORMAT_COFF;
|
---|
| 1555 | #endif
|
---|
| 1556 | } else
|
---|
| 1557 | goto err;
|
---|
| 1558 |
|
---|
| 1559 | } else if (link_option(option, "rpath=", &p)) {
|
---|
| 1560 | s->rpath = copy_linker_arg(p);
|
---|
| 1561 | } else if (link_option(option, "section-alignment=", &p)) {
|
---|
| 1562 | s->section_align = strtoul(p, &end, 16);
|
---|
| 1563 | } else if (link_option(option, "soname=", &p)) {
|
---|
| 1564 | s->soname = copy_linker_arg(p);
|
---|
| 1565 | #ifdef TCC_TARGET_PE
|
---|
| 1566 | } else if (link_option(option, "file-alignment=", &p)) {
|
---|
| 1567 | s->pe_file_align = strtoul(p, &end, 16);
|
---|
| 1568 | } else if (link_option(option, "stack=", &p)) {
|
---|
| 1569 | s->pe_stack_size = strtoul(p, &end, 10);
|
---|
| 1570 | } else if (link_option(option, "subsystem=", &p)) {
|
---|
| 1571 | #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
|
---|
| 1572 | if (!strcmp(p, "native")) {
|
---|
| 1573 | s->pe_subsystem = 1;
|
---|
| 1574 | } else if (!strcmp(p, "console")) {
|
---|
| 1575 | s->pe_subsystem = 3;
|
---|
| 1576 | } else if (!strcmp(p, "gui")) {
|
---|
| 1577 | s->pe_subsystem = 2;
|
---|
| 1578 | } else if (!strcmp(p, "posix")) {
|
---|
| 1579 | s->pe_subsystem = 7;
|
---|
| 1580 | } else if (!strcmp(p, "efiapp")) {
|
---|
| 1581 | s->pe_subsystem = 10;
|
---|
| 1582 | } else if (!strcmp(p, "efiboot")) {
|
---|
| 1583 | s->pe_subsystem = 11;
|
---|
| 1584 | } else if (!strcmp(p, "efiruntime")) {
|
---|
| 1585 | s->pe_subsystem = 12;
|
---|
| 1586 | } else if (!strcmp(p, "efirom")) {
|
---|
| 1587 | s->pe_subsystem = 13;
|
---|
| 1588 | #elif defined(TCC_TARGET_ARM)
|
---|
| 1589 | if (!strcmp(p, "wince")) {
|
---|
| 1590 | s->pe_subsystem = 9;
|
---|
| 1591 | #endif
|
---|
| 1592 | } else
|
---|
| 1593 | goto err;
|
---|
| 1594 | #endif
|
---|
| 1595 | } else
|
---|
| 1596 | goto err;
|
---|
| 1597 |
|
---|
| 1598 | if (ignoring && s->warn_unsupported) err: {
|
---|
| 1599 | char buf[100], *e;
|
---|
| 1600 | pstrcpy(buf, sizeof buf, e = copy_linker_arg(option)), tcc_free(e);
|
---|
| 1601 | if (ignoring)
|
---|
| 1602 | tcc_warning("unsupported linker option '%s'", buf);
|
---|
| 1603 | else
|
---|
| 1604 | tcc_error("unsupported linker option '%s'", buf);
|
---|
| 1605 | }
|
---|
| 1606 | option = skip_linker_arg(&p);
|
---|
| 1607 | }
|
---|
| 1608 | return 0;
|
---|
| 1609 | }
|
---|
| 1610 |
|
---|
| 1611 | typedef struct TCCOption {
|
---|
| 1612 | const char *name;
|
---|
| 1613 | uint16_t index;
|
---|
| 1614 | uint16_t flags;
|
---|
| 1615 | } TCCOption;
|
---|
| 1616 |
|
---|
| 1617 | enum {
|
---|
| 1618 | TCC_OPTION_HELP,
|
---|
| 1619 | TCC_OPTION_I,
|
---|
| 1620 | TCC_OPTION_D,
|
---|
| 1621 | TCC_OPTION_U,
|
---|
| 1622 | TCC_OPTION_L,
|
---|
| 1623 | TCC_OPTION_B,
|
---|
| 1624 | TCC_OPTION_l,
|
---|
| 1625 | TCC_OPTION_bench,
|
---|
| 1626 | TCC_OPTION_bt,
|
---|
| 1627 | TCC_OPTION_b,
|
---|
| 1628 | TCC_OPTION_g,
|
---|
| 1629 | TCC_OPTION_c,
|
---|
| 1630 | TCC_OPTION_static,
|
---|
| 1631 | TCC_OPTION_shared,
|
---|
| 1632 | TCC_OPTION_soname,
|
---|
| 1633 | TCC_OPTION_o,
|
---|
| 1634 | TCC_OPTION_r,
|
---|
| 1635 | TCC_OPTION_s,
|
---|
| 1636 | TCC_OPTION_Wl,
|
---|
| 1637 | TCC_OPTION_W,
|
---|
| 1638 | TCC_OPTION_O,
|
---|
| 1639 | TCC_OPTION_m,
|
---|
| 1640 | TCC_OPTION_f,
|
---|
| 1641 | TCC_OPTION_isystem,
|
---|
| 1642 | TCC_OPTION_nostdinc,
|
---|
| 1643 | TCC_OPTION_nostdlib,
|
---|
| 1644 | TCC_OPTION_print_search_dirs,
|
---|
| 1645 | TCC_OPTION_rdynamic,
|
---|
| 1646 | TCC_OPTION_pedantic,
|
---|
| 1647 | TCC_OPTION_pthread,
|
---|
| 1648 | TCC_OPTION_run,
|
---|
| 1649 | TCC_OPTION_v,
|
---|
| 1650 | TCC_OPTION_w,
|
---|
| 1651 | TCC_OPTION_pipe,
|
---|
| 1652 | TCC_OPTION_E,
|
---|
| 1653 | TCC_OPTION_MD,
|
---|
| 1654 | TCC_OPTION_MF,
|
---|
| 1655 | TCC_OPTION_x,
|
---|
| 1656 | TCC_OPTION_dumpversion,
|
---|
| 1657 | };
|
---|
| 1658 |
|
---|
| 1659 | #define TCC_OPTION_HAS_ARG 0x0001
|
---|
| 1660 | #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
|
---|
| 1661 |
|
---|
| 1662 | static const TCCOption tcc_options[] = {
|
---|
| 1663 | { "h", TCC_OPTION_HELP, 0 },
|
---|
| 1664 | { "-help", TCC_OPTION_HELP, 0 },
|
---|
| 1665 | { "?", TCC_OPTION_HELP, 0 },
|
---|
| 1666 | { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
|
---|
| 1667 | { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
|
---|
| 1668 | { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
|
---|
| 1669 | { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
|
---|
| 1670 | { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
|
---|
| 1671 | { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
---|
| 1672 | { "bench", TCC_OPTION_bench, 0 },
|
---|
| 1673 | #ifdef CONFIG_TCC_BACKTRACE
|
---|
| 1674 | { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
|
---|
| 1675 | #endif
|
---|
| 1676 | #ifdef CONFIG_TCC_BCHECK
|
---|
| 1677 | { "b", TCC_OPTION_b, 0 },
|
---|
| 1678 | #endif
|
---|
| 1679 | { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
---|
| 1680 | { "c", TCC_OPTION_c, 0 },
|
---|
| 1681 | { "static", TCC_OPTION_static, 0 },
|
---|
| 1682 | { "shared", TCC_OPTION_shared, 0 },
|
---|
| 1683 | { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
|
---|
| 1684 | { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
|
---|
| 1685 | { "pedantic", TCC_OPTION_pedantic, 0},
|
---|
| 1686 | { "pthread", TCC_OPTION_pthread, 0},
|
---|
| 1687 | { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
---|
| 1688 | { "rdynamic", TCC_OPTION_rdynamic, 0 },
|
---|
| 1689 | { "r", TCC_OPTION_r, 0 },
|
---|
| 1690 | { "s", TCC_OPTION_s, 0 },
|
---|
| 1691 | { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
---|
| 1692 | { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
---|
| 1693 | { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
---|
| 1694 | { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
|
---|
| 1695 | { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
---|
| 1696 | { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
|
---|
| 1697 | { "nostdinc", TCC_OPTION_nostdinc, 0 },
|
---|
| 1698 | { "nostdlib", TCC_OPTION_nostdlib, 0 },
|
---|
| 1699 | { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
|
---|
| 1700 | { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
---|
| 1701 | { "w", TCC_OPTION_w, 0 },
|
---|
| 1702 | { "pipe", TCC_OPTION_pipe, 0},
|
---|
| 1703 | { "E", TCC_OPTION_E, 0},
|
---|
| 1704 | { "MD", TCC_OPTION_MD, 0},
|
---|
| 1705 | { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
|
---|
| 1706 | { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
|
---|
| 1707 | { "dumpversion", TCC_OPTION_dumpversion, 0},
|
---|
| 1708 | { NULL, 0, 0 },
|
---|
| 1709 | };
|
---|
| 1710 |
|
---|
| 1711 | static void parse_option_D(TCCState *s1, const char *optarg)
|
---|
| 1712 | {
|
---|
| 1713 | char *sym = tcc_strdup(optarg);
|
---|
| 1714 | char *value = strchr(sym, '=');
|
---|
| 1715 | if (value)
|
---|
| 1716 | *value++ = '\0';
|
---|
| 1717 | tcc_define_symbol(s1, sym, value);
|
---|
| 1718 | tcc_free(sym);
|
---|
| 1719 | }
|
---|
| 1720 |
|
---|
| 1721 | PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv)
|
---|
| 1722 | {
|
---|
| 1723 | const TCCOption *popt;
|
---|
| 1724 | const char *optarg, *r;
|
---|
| 1725 | int run = 0;
|
---|
| 1726 | int pthread = 0;
|
---|
| 1727 | int optind = 0;
|
---|
| 1728 |
|
---|
| 1729 | /* collect -Wl options for input such as "-Wl,-rpath -Wl,<path>" */
|
---|
| 1730 | CString linker_arg;
|
---|
| 1731 | cstr_new(&linker_arg);
|
---|
| 1732 |
|
---|
| 1733 | while (optind < argc) {
|
---|
| 1734 |
|
---|
| 1735 | r = argv[optind++];
|
---|
| 1736 | if (r[0] != '-' || r[1] == '\0') {
|
---|
| 1737 | /* add a new file */
|
---|
| 1738 | dynarray_add((void ***)&s->files, &s->nb_files, tcc_strdup(r));
|
---|
| 1739 | if (run) {
|
---|
| 1740 | optind--;
|
---|
| 1741 | /* argv[0] will be this file */
|
---|
| 1742 | break;
|
---|
| 1743 | }
|
---|
| 1744 | continue;
|
---|
| 1745 | }
|
---|
| 1746 |
|
---|
| 1747 | /* find option in table */
|
---|
| 1748 | for(popt = tcc_options; ; ++popt) {
|
---|
| 1749 | const char *p1 = popt->name;
|
---|
| 1750 | const char *r1 = r + 1;
|
---|
| 1751 | if (p1 == NULL)
|
---|
| 1752 | tcc_error("invalid option -- '%s'", r);
|
---|
| 1753 | if (!strstart(p1, &r1))
|
---|
| 1754 | continue;
|
---|
| 1755 | optarg = r1;
|
---|
| 1756 | if (popt->flags & TCC_OPTION_HAS_ARG) {
|
---|
| 1757 | if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
|
---|
| 1758 | if (optind >= argc)
|
---|
| 1759 | tcc_error("argument to '%s' is missing", r);
|
---|
| 1760 | optarg = argv[optind++];
|
---|
| 1761 | }
|
---|
| 1762 | } else if (*r1 != '\0')
|
---|
| 1763 | continue;
|
---|
| 1764 | break;
|
---|
| 1765 | }
|
---|
| 1766 |
|
---|
| 1767 | switch(popt->index) {
|
---|
| 1768 | case TCC_OPTION_HELP:
|
---|
| 1769 | return 0;
|
---|
| 1770 | case TCC_OPTION_I:
|
---|
| 1771 | if (tcc_add_include_path(s, optarg) < 0)
|
---|
| 1772 | tcc_error("too many include paths");
|
---|
| 1773 | break;
|
---|
| 1774 | case TCC_OPTION_D:
|
---|
| 1775 | parse_option_D(s, optarg);
|
---|
| 1776 | break;
|
---|
| 1777 | case TCC_OPTION_U:
|
---|
| 1778 | tcc_undefine_symbol(s, optarg);
|
---|
| 1779 | break;
|
---|
| 1780 | case TCC_OPTION_L:
|
---|
| 1781 | tcc_add_library_path(s, optarg);
|
---|
| 1782 | break;
|
---|
| 1783 | case TCC_OPTION_B:
|
---|
| 1784 | /* set tcc utilities path (mainly for tcc development) */
|
---|
| 1785 | tcc_set_lib_path(s, optarg);
|
---|
| 1786 | break;
|
---|
| 1787 | case TCC_OPTION_l:
|
---|
| 1788 | dynarray_add((void ***)&s->files, &s->nb_files, tcc_strdup(r));
|
---|
| 1789 | s->nb_libraries++;
|
---|
| 1790 | break;
|
---|
| 1791 | case TCC_OPTION_pthread:
|
---|
| 1792 | parse_option_D(s, "_REENTRANT");
|
---|
| 1793 | pthread = 1;
|
---|
| 1794 | break;
|
---|
| 1795 | case TCC_OPTION_bench:
|
---|
| 1796 | s->do_bench = 1;
|
---|
| 1797 | break;
|
---|
| 1798 | #ifdef CONFIG_TCC_BACKTRACE
|
---|
| 1799 | case TCC_OPTION_bt:
|
---|
| 1800 | tcc_set_num_callers(atoi(optarg));
|
---|
| 1801 | break;
|
---|
| 1802 | #endif
|
---|
| 1803 | #ifdef CONFIG_TCC_BCHECK
|
---|
| 1804 | case TCC_OPTION_b:
|
---|
| 1805 | s->do_bounds_check = 1;
|
---|
| 1806 | s->do_debug = 1;
|
---|
| 1807 | break;
|
---|
| 1808 | #endif
|
---|
| 1809 | case TCC_OPTION_g:
|
---|
| 1810 | s->do_debug = 1;
|
---|
| 1811 | break;
|
---|
| 1812 | case TCC_OPTION_c:
|
---|
| 1813 | s->output_type = TCC_OUTPUT_OBJ;
|
---|
| 1814 | break;
|
---|
| 1815 | case TCC_OPTION_static:
|
---|
| 1816 | s->static_link = 1;
|
---|
| 1817 | break;
|
---|
| 1818 | case TCC_OPTION_shared:
|
---|
| 1819 | s->output_type = TCC_OUTPUT_DLL;
|
---|
| 1820 | break;
|
---|
| 1821 | case TCC_OPTION_soname:
|
---|
| 1822 | s->soname = tcc_strdup(optarg);
|
---|
| 1823 | break;
|
---|
| 1824 | case TCC_OPTION_m:
|
---|
| 1825 | s->option_m = tcc_strdup(optarg);
|
---|
| 1826 | break;
|
---|
| 1827 | case TCC_OPTION_o:
|
---|
| 1828 | s->outfile = tcc_strdup(optarg);
|
---|
| 1829 | break;
|
---|
| 1830 | case TCC_OPTION_r:
|
---|
| 1831 | /* generate a .o merging several output files */
|
---|
| 1832 | s->option_r = 1;
|
---|
| 1833 | s->output_type = TCC_OUTPUT_OBJ;
|
---|
| 1834 | break;
|
---|
| 1835 | case TCC_OPTION_isystem:
|
---|
| 1836 | tcc_add_sysinclude_path(s, optarg);
|
---|
| 1837 | break;
|
---|
| 1838 | case TCC_OPTION_nostdinc:
|
---|
| 1839 | s->nostdinc = 1;
|
---|
| 1840 | break;
|
---|
| 1841 | case TCC_OPTION_nostdlib:
|
---|
| 1842 | s->nostdlib = 1;
|
---|
| 1843 | break;
|
---|
| 1844 | case TCC_OPTION_print_search_dirs:
|
---|
| 1845 | s->print_search_dirs = 1;
|
---|
| 1846 | break;
|
---|
| 1847 | case TCC_OPTION_run:
|
---|
| 1848 | s->output_type = TCC_OUTPUT_MEMORY;
|
---|
| 1849 | tcc_set_options(s, optarg);
|
---|
| 1850 | run = 1;
|
---|
| 1851 | break;
|
---|
| 1852 | case TCC_OPTION_v:
|
---|
| 1853 | do ++s->verbose; while (*optarg++ == 'v');
|
---|
| 1854 | break;
|
---|
| 1855 | case TCC_OPTION_f:
|
---|
| 1856 | if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
|
---|
| 1857 | goto unsupported_option;
|
---|
| 1858 | break;
|
---|
| 1859 | case TCC_OPTION_W:
|
---|
| 1860 | if (tcc_set_warning(s, optarg, 1) < 0 &&
|
---|
| 1861 | s->warn_unsupported)
|
---|
| 1862 | goto unsupported_option;
|
---|
| 1863 | break;
|
---|
| 1864 | case TCC_OPTION_w:
|
---|
| 1865 | s->warn_none = 1;
|
---|
| 1866 | break;
|
---|
| 1867 | case TCC_OPTION_rdynamic:
|
---|
| 1868 | s->rdynamic = 1;
|
---|
| 1869 | break;
|
---|
| 1870 | case TCC_OPTION_Wl:
|
---|
| 1871 | if (linker_arg.size)
|
---|
| 1872 | --linker_arg.size, cstr_ccat(&linker_arg, ',');
|
---|
| 1873 | cstr_cat(&linker_arg, optarg);
|
---|
| 1874 | cstr_ccat(&linker_arg, '\0');
|
---|
| 1875 | break;
|
---|
| 1876 | case TCC_OPTION_E:
|
---|
| 1877 | s->output_type = TCC_OUTPUT_PREPROCESS;
|
---|
| 1878 | break;
|
---|
| 1879 | case TCC_OPTION_MD:
|
---|
| 1880 | s->gen_deps = 1;
|
---|
| 1881 | break;
|
---|
| 1882 | case TCC_OPTION_MF:
|
---|
| 1883 | s->deps_outfile = tcc_strdup(optarg);
|
---|
| 1884 | break;
|
---|
| 1885 | case TCC_OPTION_dumpversion:
|
---|
| 1886 | printf ("%s\n", TCC_VERSION);
|
---|
| 1887 | exit(0);
|
---|
| 1888 | case TCC_OPTION_O:
|
---|
| 1889 | case TCC_OPTION_pedantic:
|
---|
| 1890 | case TCC_OPTION_pipe:
|
---|
| 1891 | case TCC_OPTION_s:
|
---|
| 1892 | case TCC_OPTION_x:
|
---|
| 1893 | /* ignored */
|
---|
| 1894 | break;
|
---|
| 1895 | default:
|
---|
| 1896 | if (s->warn_unsupported) {
|
---|
| 1897 | unsupported_option:
|
---|
| 1898 | tcc_warning("unsupported option '%s'", r);
|
---|
| 1899 | }
|
---|
| 1900 | break;
|
---|
| 1901 | }
|
---|
| 1902 | }
|
---|
| 1903 |
|
---|
| 1904 | if (pthread && s->output_type != TCC_OUTPUT_OBJ)
|
---|
| 1905 | tcc_set_options(s, "-lpthread");
|
---|
| 1906 |
|
---|
| 1907 | tcc_set_linker(s, (const char *)linker_arg.data);
|
---|
| 1908 | cstr_free(&linker_arg);
|
---|
| 1909 |
|
---|
| 1910 | return optind;
|
---|
| 1911 | }
|
---|
| 1912 |
|
---|
| 1913 | LIBTCCAPI int tcc_set_options(TCCState *s, const char *str)
|
---|
| 1914 | {
|
---|
| 1915 | const char *s1;
|
---|
| 1916 | char **argv, *arg;
|
---|
| 1917 | int argc, len;
|
---|
| 1918 | int ret;
|
---|
| 1919 |
|
---|
| 1920 | argc = 0, argv = NULL;
|
---|
| 1921 | for(;;) {
|
---|
| 1922 | while (is_space(*str))
|
---|
| 1923 | str++;
|
---|
| 1924 | if (*str == '\0')
|
---|
| 1925 | break;
|
---|
| 1926 | s1 = str;
|
---|
| 1927 | while (*str != '\0' && !is_space(*str))
|
---|
| 1928 | str++;
|
---|
| 1929 | len = str - s1;
|
---|
| 1930 | arg = tcc_malloc(len + 1);
|
---|
| 1931 | pstrncpy(arg, s1, len);
|
---|
| 1932 | dynarray_add((void ***)&argv, &argc, arg);
|
---|
| 1933 | }
|
---|
| 1934 | ret = tcc_parse_args(s, argc, argv);
|
---|
| 1935 | dynarray_reset(&argv, &argc);
|
---|
| 1936 | return ret;
|
---|
| 1937 | }
|
---|
| 1938 |
|
---|
| 1939 | PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time)
|
---|
| 1940 | {
|
---|
| 1941 | double tt;
|
---|
| 1942 | tt = (double)total_time / 1000000.0;
|
---|
| 1943 | if (tt < 0.001)
|
---|
| 1944 | tt = 0.001;
|
---|
| 1945 | if (total_bytes < 1)
|
---|
| 1946 | total_bytes = 1;
|
---|
| 1947 | printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
|
---|
| 1948 | tok_ident - TOK_IDENT, total_lines, total_bytes,
|
---|
| 1949 | tt, (int)(total_lines / tt),
|
---|
| 1950 | total_bytes / tt / 1000000.0);
|
---|
| 1951 | }
|
---|