libtcc.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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. #include "tcc.h"
  21. /********************************************************/
  22. /* global variables */
  23. /* use GNU C extensions */
  24. ST_DATA int gnu_ext = 1;
  25. /* use TinyCC extensions */
  26. ST_DATA int tcc_ext = 1;
  27. /* XXX: get rid of this ASAP */
  28. ST_DATA struct TCCState *tcc_state;
  29. static int nb_states;
  30. /********************************************************/
  31. #if ONE_SOURCE
  32. #include "tccpp.c"
  33. #include "tccgen.c"
  34. #include "tccelf.c"
  35. #include "tccrun.c"
  36. #ifdef TCC_TARGET_I386
  37. #include "i386-gen.c"
  38. #include "i386-link.c"
  39. #include "i386-asm.c"
  40. #endif
  41. #ifdef TCC_TARGET_ARM
  42. #include "arm-gen.c"
  43. #include "arm-link.c"
  44. #include "arm-asm.c"
  45. #endif
  46. #ifdef TCC_TARGET_ARM64
  47. #include "arm64-gen.c"
  48. #include "arm64-link.c"
  49. #endif
  50. #ifdef TCC_TARGET_C67
  51. #include "c67-gen.c"
  52. #include "c67-link.c"
  53. #include "tcccoff.c"
  54. #endif
  55. #ifdef TCC_TARGET_X86_64
  56. #include "x86_64-gen.c"
  57. #include "x86_64-link.c"
  58. #include "i386-asm.c"
  59. #endif
  60. #ifdef CONFIG_TCC_ASM
  61. #include "tccasm.c"
  62. #endif
  63. #ifdef TCC_TARGET_PE
  64. #include "tccpe.c"
  65. #endif
  66. #endif /* ONE_SOURCE */
  67. /********************************************************/
  68. #ifndef CONFIG_TCC_ASM
  69. ST_FUNC void asm_instr(void)
  70. {
  71. tcc_error("inline asm() not supported");
  72. }
  73. ST_FUNC void asm_global_instr(void)
  74. {
  75. tcc_error("inline asm() not supported");
  76. }
  77. #endif
  78. /********************************************************/
  79. #ifdef _WIN32
  80. ST_FUNC char *normalize_slashes(char *path)
  81. {
  82. char *p;
  83. for (p = path; *p; ++p)
  84. if (*p == '\\')
  85. *p = '/';
  86. return path;
  87. }
  88. static HMODULE tcc_module;
  89. /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */
  90. static void tcc_set_lib_path_w32(TCCState *s)
  91. {
  92. char path[1024], *p;
  93. GetModuleFileNameA(tcc_module, path, sizeof path);
  94. p = tcc_basename(normalize_slashes(strlwr(path)));
  95. if (p > path)
  96. --p;
  97. *p = 0;
  98. tcc_set_lib_path(s, path);
  99. }
  100. #ifdef TCC_TARGET_PE
  101. static void tcc_add_systemdir(TCCState *s)
  102. {
  103. char buf[1000];
  104. GetSystemDirectory(buf, sizeof buf);
  105. tcc_add_library_path(s, normalize_slashes(buf));
  106. }
  107. #endif
  108. #ifdef LIBTCC_AS_DLL
  109. BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
  110. {
  111. if (DLL_PROCESS_ATTACH == dwReason)
  112. tcc_module = hDll;
  113. return TRUE;
  114. }
  115. #endif
  116. #endif
  117. /********************************************************/
  118. /* copy a string and truncate it. */
  119. ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s)
  120. {
  121. char *q, *q_end;
  122. int c;
  123. if (buf_size > 0) {
  124. q = buf;
  125. q_end = buf + buf_size - 1;
  126. while (q < q_end) {
  127. c = *s++;
  128. if (c == '\0')
  129. break;
  130. *q++ = c;
  131. }
  132. *q = '\0';
  133. }
  134. return buf;
  135. }
  136. /* strcat and truncate. */
  137. ST_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
  138. {
  139. int len;
  140. len = strlen(buf);
  141. if (len < buf_size)
  142. pstrcpy(buf + len, buf_size - len, s);
  143. return buf;
  144. }
  145. ST_FUNC char *pstrncpy(char *out, const char *in, size_t num)
  146. {
  147. memcpy(out, in, num);
  148. out[num] = '\0';
  149. return out;
  150. }
  151. /* extract the basename of a file */
  152. PUB_FUNC char *tcc_basename(const char *name)
  153. {
  154. char *p = strchr(name, 0);
  155. while (p > name && !IS_DIRSEP(p[-1]))
  156. --p;
  157. return p;
  158. }
  159. /* extract extension part of a file
  160. *
  161. * (if no extension, return pointer to end-of-string)
  162. */
  163. PUB_FUNC char *tcc_fileextension (const char *name)
  164. {
  165. char *b = tcc_basename(name);
  166. char *e = strrchr(b, '.');
  167. return e ? e : strchr(b, 0);
  168. }
  169. /********************************************************/
  170. /* memory management */
  171. #undef free
  172. #undef malloc
  173. #undef realloc
  174. #ifndef MEM_DEBUG
  175. PUB_FUNC void tcc_free(void *ptr)
  176. {
  177. free(ptr);
  178. }
  179. PUB_FUNC void *tcc_malloc(unsigned long size)
  180. {
  181. void *ptr;
  182. ptr = malloc(size);
  183. if (!ptr && size)
  184. tcc_error("memory full (malloc)");
  185. return ptr;
  186. }
  187. PUB_FUNC void *tcc_mallocz(unsigned long size)
  188. {
  189. void *ptr;
  190. ptr = tcc_malloc(size);
  191. memset(ptr, 0, size);
  192. return ptr;
  193. }
  194. PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size)
  195. {
  196. void *ptr1;
  197. ptr1 = realloc(ptr, size);
  198. if (!ptr1 && size)
  199. tcc_error("memory full (realloc)");
  200. return ptr1;
  201. }
  202. PUB_FUNC char *tcc_strdup(const char *str)
  203. {
  204. char *ptr;
  205. ptr = tcc_malloc(strlen(str) + 1);
  206. strcpy(ptr, str);
  207. return ptr;
  208. }
  209. PUB_FUNC void tcc_memcheck(void)
  210. {
  211. }
  212. #else
  213. #define MEM_DEBUG_MAGIC1 0xFEEDDEB1
  214. #define MEM_DEBUG_MAGIC2 0xFEEDDEB2
  215. #define MEM_DEBUG_MAGIC3 0xFEEDDEB3
  216. #define MEM_DEBUG_FILE_LEN 40
  217. #define MEM_DEBUG_CHECK3(header) \
  218. ((mem_debug_header_t*)((char*)header + header->size))->magic3
  219. #define MEM_USER_PTR(header) \
  220. ((char *)header + offsetof(mem_debug_header_t, magic3))
  221. #define MEM_HEADER_PTR(ptr) \
  222. (mem_debug_header_t *)((char*)ptr - offsetof(mem_debug_header_t, magic3))
  223. struct mem_debug_header {
  224. unsigned magic1;
  225. unsigned size;
  226. struct mem_debug_header *prev;
  227. struct mem_debug_header *next;
  228. int line_num;
  229. char file_name[MEM_DEBUG_FILE_LEN + 1];
  230. unsigned magic2;
  231. ALIGNED(16) unsigned magic3;
  232. };
  233. typedef struct mem_debug_header mem_debug_header_t;
  234. static mem_debug_header_t *mem_debug_chain;
  235. static unsigned mem_cur_size;
  236. static unsigned mem_max_size;
  237. static mem_debug_header_t *malloc_check(void *ptr, const char *msg)
  238. {
  239. mem_debug_header_t * header = MEM_HEADER_PTR(ptr);
  240. if (header->magic1 != MEM_DEBUG_MAGIC1 ||
  241. header->magic2 != MEM_DEBUG_MAGIC2 ||
  242. MEM_DEBUG_CHECK3(header) != MEM_DEBUG_MAGIC3 ||
  243. header->size == (unsigned)-1) {
  244. fprintf(stderr, "%s check failed\n", msg);
  245. if (header->magic1 == MEM_DEBUG_MAGIC1)
  246. fprintf(stderr, "%s:%u: block allocated here.\n",
  247. header->file_name, header->line_num);
  248. exit(1);
  249. }
  250. return header;
  251. }
  252. PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line)
  253. {
  254. int ofs;
  255. mem_debug_header_t *header;
  256. header = malloc(sizeof(mem_debug_header_t) + size);
  257. if (!header)
  258. tcc_error("memory full (malloc)");
  259. header->magic1 = MEM_DEBUG_MAGIC1;
  260. header->magic2 = MEM_DEBUG_MAGIC2;
  261. header->size = size;
  262. MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
  263. header->line_num = line;
  264. ofs = strlen(file) - MEM_DEBUG_FILE_LEN;
  265. strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), MEM_DEBUG_FILE_LEN);
  266. header->file_name[MEM_DEBUG_FILE_LEN] = 0;
  267. header->next = mem_debug_chain;
  268. header->prev = NULL;
  269. if (header->next)
  270. header->next->prev = header;
  271. mem_debug_chain = header;
  272. mem_cur_size += size;
  273. if (mem_cur_size > mem_max_size)
  274. mem_max_size = mem_cur_size;
  275. return MEM_USER_PTR(header);
  276. }
  277. PUB_FUNC void tcc_free_debug(void *ptr)
  278. {
  279. mem_debug_header_t *header;
  280. if (!ptr)
  281. return;
  282. header = malloc_check(ptr, "tcc_free");
  283. mem_cur_size -= header->size;
  284. header->size = (unsigned)-1;
  285. if (header->next)
  286. header->next->prev = header->prev;
  287. if (header->prev)
  288. header->prev->next = header->next;
  289. if (header == mem_debug_chain)
  290. mem_debug_chain = header->next;
  291. free(header);
  292. }
  293. PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line)
  294. {
  295. void *ptr;
  296. ptr = tcc_malloc_debug(size,file,line);
  297. memset(ptr, 0, size);
  298. return ptr;
  299. }
  300. PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line)
  301. {
  302. mem_debug_header_t *header;
  303. int mem_debug_chain_update = 0;
  304. if (!ptr)
  305. return tcc_malloc_debug(size, file, line);
  306. header = malloc_check(ptr, "tcc_realloc");
  307. mem_cur_size -= header->size;
  308. mem_debug_chain_update = (header == mem_debug_chain);
  309. header = realloc(header, sizeof(mem_debug_header_t) + size);
  310. if (!header)
  311. tcc_error("memory full (realloc)");
  312. header->size = size;
  313. MEM_DEBUG_CHECK3(header) = MEM_DEBUG_MAGIC3;
  314. if (header->next)
  315. header->next->prev = header;
  316. if (header->prev)
  317. header->prev->next = header;
  318. if (mem_debug_chain_update)
  319. mem_debug_chain = header;
  320. mem_cur_size += size;
  321. if (mem_cur_size > mem_max_size)
  322. mem_max_size = mem_cur_size;
  323. return MEM_USER_PTR(header);
  324. }
  325. PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line)
  326. {
  327. char *ptr;
  328. ptr = tcc_malloc_debug(strlen(str) + 1, file, line);
  329. strcpy(ptr, str);
  330. return ptr;
  331. }
  332. PUB_FUNC void tcc_memcheck(void)
  333. {
  334. if (mem_cur_size) {
  335. mem_debug_header_t *header = mem_debug_chain;
  336. fprintf(stderr, "MEM_DEBUG: mem_leak= %d bytes, mem_max_size= %d bytes\n",
  337. mem_cur_size, mem_max_size);
  338. while (header) {
  339. fprintf(stderr, "%s:%u: error: %u bytes leaked\n",
  340. header->file_name, header->line_num, header->size);
  341. header = header->next;
  342. }
  343. #if MEM_DEBUG-0 == 2
  344. exit(2);
  345. #endif
  346. }
  347. }
  348. #endif /* MEM_DEBUG */
  349. #define free(p) use_tcc_free(p)
  350. #define malloc(s) use_tcc_malloc(s)
  351. #define realloc(p, s) use_tcc_realloc(p, s)
  352. /********************************************************/
  353. /* dynarrays */
  354. ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data)
  355. {
  356. int nb, nb_alloc;
  357. void **pp;
  358. nb = *nb_ptr;
  359. pp = *(void ***)ptab;
  360. /* every power of two we double array size */
  361. if ((nb & (nb - 1)) == 0) {
  362. if (!nb)
  363. nb_alloc = 1;
  364. else
  365. nb_alloc = nb * 2;
  366. pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
  367. *(void***)ptab = pp;
  368. }
  369. pp[nb++] = data;
  370. *nb_ptr = nb;
  371. }
  372. ST_FUNC void dynarray_reset(void *pp, int *n)
  373. {
  374. void **p;
  375. for (p = *(void***)pp; *n; ++p, --*n)
  376. if (*p)
  377. tcc_free(*p);
  378. tcc_free(*(void**)pp);
  379. *(void**)pp = NULL;
  380. }
  381. static void tcc_split_path(TCCState *s, void *p_ary, int *p_nb_ary, const char *in)
  382. {
  383. const char *p;
  384. do {
  385. int c;
  386. CString str;
  387. cstr_new(&str);
  388. for (p = in; c = *p, c != '\0' && c != PATHSEP[0]; ++p) {
  389. if (c == '{' && p[1] && p[2] == '}') {
  390. c = p[1], p += 2;
  391. if (c == 'B')
  392. cstr_cat(&str, s->tcc_lib_path, -1);
  393. } else {
  394. cstr_ccat(&str, c);
  395. }
  396. }
  397. if (str.size) {
  398. cstr_ccat(&str, '\0');
  399. dynarray_add(p_ary, p_nb_ary, tcc_strdup(str.data));
  400. }
  401. cstr_free(&str);
  402. in = p+1;
  403. } while (*p);
  404. }
  405. /********************************************************/
  406. static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
  407. {
  408. int len;
  409. len = strlen(buf);
  410. vsnprintf(buf + len, buf_size - len, fmt, ap);
  411. }
  412. static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
  413. {
  414. va_list ap;
  415. va_start(ap, fmt);
  416. strcat_vprintf(buf, buf_size, fmt, ap);
  417. va_end(ap);
  418. }
  419. static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
  420. {
  421. char buf[2048];
  422. BufferedFile **pf, *f;
  423. buf[0] = '\0';
  424. /* use upper file if inline ":asm:" or token ":paste:" */
  425. for (f = file; f && f->filename[0] == ':'; f = f->prev)
  426. ;
  427. if (f) {
  428. for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
  429. strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
  430. (*pf)->filename, (*pf)->line_num);
  431. if (s1->error_set_jmp_enabled) {
  432. strcat_printf(buf, sizeof(buf), "%s:%d: ",
  433. f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
  434. } else {
  435. strcat_printf(buf, sizeof(buf), "%s: ",
  436. f->filename);
  437. }
  438. } else {
  439. strcat_printf(buf, sizeof(buf), "tcc: ");
  440. }
  441. if (is_warning)
  442. strcat_printf(buf, sizeof(buf), "warning: ");
  443. else
  444. strcat_printf(buf, sizeof(buf), "error: ");
  445. strcat_vprintf(buf, sizeof(buf), fmt, ap);
  446. if (!s1->error_func) {
  447. /* default case: stderr */
  448. if (s1->output_type == TCC_OUTPUT_PREPROCESS && s1->ppfp == stdout)
  449. /* print a newline during tcc -E */
  450. printf("\n"), fflush(stdout);
  451. fflush(stdout); /* flush -v output */
  452. fprintf(stderr, "%s\n", buf);
  453. fflush(stderr); /* print error/warning now (win32) */
  454. } else {
  455. s1->error_func(s1->error_opaque, buf);
  456. }
  457. if (!is_warning || s1->warn_error)
  458. s1->nb_errors++;
  459. }
  460. LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque,
  461. void (*error_func)(void *opaque, const char *msg))
  462. {
  463. s->error_opaque = error_opaque;
  464. s->error_func = error_func;
  465. }
  466. /* error without aborting current compilation */
  467. PUB_FUNC void tcc_error_noabort(const char *fmt, ...)
  468. {
  469. TCCState *s1 = tcc_state;
  470. va_list ap;
  471. va_start(ap, fmt);
  472. error1(s1, 0, fmt, ap);
  473. va_end(ap);
  474. }
  475. PUB_FUNC void tcc_error(const char *fmt, ...)
  476. {
  477. TCCState *s1 = tcc_state;
  478. va_list ap;
  479. va_start(ap, fmt);
  480. error1(s1, 0, fmt, ap);
  481. va_end(ap);
  482. /* better than nothing: in some cases, we accept to handle errors */
  483. if (s1->error_set_jmp_enabled) {
  484. longjmp(s1->error_jmp_buf, 1);
  485. } else {
  486. /* XXX: eliminate this someday */
  487. exit(1);
  488. }
  489. }
  490. PUB_FUNC void tcc_warning(const char *fmt, ...)
  491. {
  492. TCCState *s1 = tcc_state;
  493. va_list ap;
  494. if (s1->warn_none)
  495. return;
  496. va_start(ap, fmt);
  497. error1(s1, 1, fmt, ap);
  498. va_end(ap);
  499. }
  500. /********************************************************/
  501. /* I/O layer */
  502. ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
  503. {
  504. BufferedFile *bf;
  505. int buflen = initlen ? initlen : IO_BUF_SIZE;
  506. bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
  507. bf->buf_ptr = bf->buffer;
  508. bf->buf_end = bf->buffer + initlen;
  509. bf->buf_end[0] = CH_EOB; /* put eob symbol */
  510. pstrcpy(bf->filename, sizeof(bf->filename), filename);
  511. bf->true_filename = bf->filename;
  512. bf->line_num = 1;
  513. bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
  514. bf->fd = -1;
  515. bf->prev = file;
  516. file = bf;
  517. tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
  518. }
  519. ST_FUNC void tcc_close(void)
  520. {
  521. BufferedFile *bf = file;
  522. if (bf->fd > 0) {
  523. close(bf->fd);
  524. total_lines += bf->line_num;
  525. }
  526. if (bf->true_filename != bf->filename)
  527. tcc_free(bf->true_filename);
  528. file = bf->prev;
  529. tcc_free(bf);
  530. }
  531. ST_FUNC int tcc_open(TCCState *s1, const char *filename)
  532. {
  533. int fd;
  534. if (strcmp(filename, "-") == 0)
  535. fd = 0, filename = "<stdin>";
  536. else
  537. fd = open(filename, O_RDONLY | O_BINARY);
  538. if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
  539. printf("%s %*s%s\n", fd < 0 ? "nf":"->",
  540. (int)(s1->include_stack_ptr - s1->include_stack), "", filename);
  541. if (fd < 0)
  542. return -1;
  543. tcc_open_bf(s1, filename, 0);
  544. #ifdef _WIN32
  545. normalize_slashes(file->filename);
  546. #endif
  547. file->fd = fd;
  548. return fd;
  549. }
  550. /* compile the file opened in 'file'. Return non zero if errors. */
  551. static int tcc_compile(TCCState *s1, int filetype)
  552. {
  553. Sym *define_start;
  554. int is_asm;
  555. define_start = define_stack;
  556. is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP));
  557. tccelf_begin_file(s1);
  558. if (setjmp(s1->error_jmp_buf) == 0) {
  559. s1->nb_errors = 0;
  560. s1->error_set_jmp_enabled = 1;
  561. preprocess_start(s1, is_asm);
  562. if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
  563. tcc_preprocess(s1);
  564. } else if (is_asm) {
  565. #ifdef CONFIG_TCC_ASM
  566. tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP));
  567. #else
  568. tcc_error_noabort("asm not supported");
  569. #endif
  570. } else {
  571. tccgen_compile(s1);
  572. }
  573. }
  574. s1->error_set_jmp_enabled = 0;
  575. preprocess_end(s1);
  576. free_inline_functions(s1);
  577. /* reset define stack, but keep -D and built-ins */
  578. free_defines(define_start);
  579. sym_pop(&global_stack, NULL, 0);
  580. sym_pop(&local_stack, NULL, 0);
  581. tccelf_end_file(s1);
  582. return s1->nb_errors != 0 ? -1 : 0;
  583. }
  584. LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
  585. {
  586. int len, ret;
  587. len = strlen(str);
  588. tcc_open_bf(s, "<string>", len);
  589. memcpy(file->buffer, str, len);
  590. ret = tcc_compile(s, s->filetype);
  591. tcc_close();
  592. return ret;
  593. }
  594. /* define a preprocessor symbol. A value can also be provided with the '=' operator */
  595. LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
  596. {
  597. int len1, len2;
  598. /* default value */
  599. if (!value)
  600. value = "1";
  601. len1 = strlen(sym);
  602. len2 = strlen(value);
  603. /* init file structure */
  604. tcc_open_bf(s1, "<define>", len1 + len2 + 1);
  605. memcpy(file->buffer, sym, len1);
  606. file->buffer[len1] = ' ';
  607. memcpy(file->buffer + len1 + 1, value, len2);
  608. /* parse with define parser */
  609. next_nomacro();
  610. parse_define();
  611. tcc_close();
  612. }
  613. /* undefine a preprocessor symbol */
  614. LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
  615. {
  616. TokenSym *ts;
  617. Sym *s;
  618. ts = tok_alloc(sym, strlen(sym));
  619. s = define_find(ts->tok);
  620. /* undefine symbol by putting an invalid name */
  621. if (s)
  622. define_undef(s);
  623. }
  624. /* cleanup all static data used during compilation */
  625. static void tcc_cleanup(void)
  626. {
  627. if (NULL == tcc_state)
  628. return;
  629. while (file)
  630. tcc_close();
  631. tccpp_delete(tcc_state);
  632. tcc_state = NULL;
  633. /* free sym_pools */
  634. dynarray_reset(&sym_pools, &nb_sym_pools);
  635. /* reset symbol stack */
  636. sym_free_first = NULL;
  637. }
  638. LIBTCCAPI TCCState *tcc_new(void)
  639. {
  640. TCCState *s;
  641. tcc_cleanup();
  642. s = tcc_mallocz(sizeof(TCCState));
  643. if (!s)
  644. return NULL;
  645. tcc_state = s;
  646. ++nb_states;
  647. s->nocommon = 1;
  648. s->warn_implicit_function_declaration = 1;
  649. s->ms_extensions = 1;
  650. #ifdef CHAR_IS_UNSIGNED
  651. s->char_is_unsigned = 1;
  652. #endif
  653. #ifdef TCC_TARGET_I386
  654. s->seg_size = 32;
  655. #endif
  656. /* enable this if you want symbols with leading underscore on windows: */
  657. #if 0 /* def TCC_TARGET_PE */
  658. s->leading_underscore = 1;
  659. #endif
  660. #ifdef _WIN32
  661. tcc_set_lib_path_w32(s);
  662. #else
  663. tcc_set_lib_path(s, CONFIG_TCCDIR);
  664. #endif
  665. tccelf_new(s);
  666. tccpp_new(s);
  667. /* we add dummy defines for some special macros to speed up tests
  668. and to have working defined() */
  669. define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
  670. define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
  671. define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
  672. define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
  673. define_push(TOK___COUNTER__, MACRO_OBJ, NULL, NULL);
  674. {
  675. /* define __TINYC__ 92X */
  676. char buffer[32]; int a,b,c;
  677. sscanf(TCC_VERSION, "%d.%d.%d", &a, &b, &c);
  678. sprintf(buffer, "%d", a*10000 + b*100 + c);
  679. tcc_define_symbol(s, "__TINYC__", buffer);
  680. }
  681. /* standard defines */
  682. tcc_define_symbol(s, "__STDC__", NULL);
  683. tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
  684. tcc_define_symbol(s, "__STDC_HOSTED__", NULL);
  685. /* target defines */
  686. #if defined(TCC_TARGET_I386)
  687. tcc_define_symbol(s, "__i386__", NULL);
  688. tcc_define_symbol(s, "__i386", NULL);
  689. tcc_define_symbol(s, "i386", NULL);
  690. #elif defined(TCC_TARGET_X86_64)
  691. tcc_define_symbol(s, "__x86_64__", NULL);
  692. #elif defined(TCC_TARGET_ARM)
  693. tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
  694. tcc_define_symbol(s, "__arm_elf__", NULL);
  695. tcc_define_symbol(s, "__arm_elf", NULL);
  696. tcc_define_symbol(s, "arm_elf", NULL);
  697. tcc_define_symbol(s, "__arm__", NULL);
  698. tcc_define_symbol(s, "__arm", NULL);
  699. tcc_define_symbol(s, "arm", NULL);
  700. tcc_define_symbol(s, "__APCS_32__", NULL);
  701. tcc_define_symbol(s, "__ARMEL__", NULL);
  702. #if defined(TCC_ARM_EABI)
  703. tcc_define_symbol(s, "__ARM_EABI__", NULL);
  704. #endif
  705. #if defined(TCC_ARM_HARDFLOAT)
  706. s->float_abi = ARM_HARD_FLOAT;
  707. tcc_define_symbol(s, "__ARM_PCS_VFP", NULL);
  708. #else
  709. s->float_abi = ARM_SOFTFP_FLOAT;
  710. #endif
  711. #elif defined(TCC_TARGET_ARM64)
  712. tcc_define_symbol(s, "__aarch64__", NULL);
  713. #elif defined TCC_TARGET_C67
  714. tcc_define_symbol(s, "__C67__", NULL);
  715. #endif
  716. #ifdef TCC_TARGET_PE
  717. tcc_define_symbol(s, "_WIN32", NULL);
  718. # ifdef TCC_TARGET_X86_64
  719. tcc_define_symbol(s, "_WIN64", NULL);
  720. # endif
  721. #else
  722. tcc_define_symbol(s, "__unix__", NULL);
  723. tcc_define_symbol(s, "__unix", NULL);
  724. tcc_define_symbol(s, "unix", NULL);
  725. # if defined(__linux__)
  726. tcc_define_symbol(s, "__linux__", NULL);
  727. tcc_define_symbol(s, "__linux", NULL);
  728. # endif
  729. # if defined(__FreeBSD__)
  730. tcc_define_symbol(s, "__FreeBSD__", "__FreeBSD__");
  731. /* No 'Thread Storage Local' on FreeBSD with tcc */
  732. tcc_define_symbol(s, "__NO_TLS", NULL);
  733. # endif
  734. # if defined(__FreeBSD_kernel__)
  735. tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
  736. # endif
  737. # if defined(__NetBSD__)
  738. tcc_define_symbol(s, "__NetBSD__", "__NetBSD__");
  739. # endif
  740. # if defined(__OpenBSD__)
  741. tcc_define_symbol(s, "__OpenBSD__", "__OpenBSD__");
  742. # endif
  743. #endif
  744. /* TinyCC & gcc defines */
  745. #if PTR_SIZE == 4
  746. /* 32bit systems. */
  747. tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
  748. tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
  749. tcc_define_symbol(s, "__ILP32__", NULL);
  750. #elif LONG_SIZE == 4
  751. /* 64bit Windows. */
  752. tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long long");
  753. tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long long");
  754. tcc_define_symbol(s, "__LLP64__", NULL);
  755. #else
  756. /* Other 64bit systems. */
  757. tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned long");
  758. tcc_define_symbol(s, "__PTRDIFF_TYPE__", "long");
  759. tcc_define_symbol(s, "__LP64__", NULL);
  760. #endif
  761. #ifdef TCC_TARGET_PE
  762. tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
  763. tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short");
  764. #else
  765. tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
  766. /* wint_t is unsigned int by default, but (signed) int on BSDs
  767. and unsigned short on windows. Other OSes might have still
  768. other conventions, sigh. */
  769. # if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) \
  770. || defined(__NetBSD__) || defined(__OpenBSD__)
  771. tcc_define_symbol(s, "__WINT_TYPE__", "int");
  772. # ifdef __FreeBSD__
  773. /* define __GNUC__ to have some useful stuff from sys/cdefs.h
  774. that are unconditionally used in FreeBSDs other system headers :/ */
  775. tcc_define_symbol(s, "__GNUC__", "2");
  776. tcc_define_symbol(s, "__GNUC_MINOR__", "7");
  777. tcc_define_symbol(s, "__builtin_alloca", "alloca");
  778. # endif
  779. # else
  780. tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int");
  781. /* glibc defines */
  782. tcc_define_symbol(s, "__REDIRECT(name, proto, alias)",
  783. "name proto __asm__ (#alias)");
  784. tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)",
  785. "name proto __asm__ (#alias) __THROW");
  786. # endif
  787. # if defined(TCC_MUSL)
  788. tcc_define_symbol(s, "__DEFINED_va_list", "");
  789. tcc_define_symbol(s, "__DEFINED___isoc_va_list", "");
  790. tcc_define_symbol(s, "__isoc_va_list", "void *");
  791. # endif /* TCC_MUSL */
  792. /* Some GCC builtins that are simple to express as macros. */
  793. tcc_define_symbol(s, "__builtin_extract_return_addr(x)", "x");
  794. #endif /* ndef TCC_TARGET_PE */
  795. return s;
  796. }
  797. LIBTCCAPI void tcc_delete(TCCState *s1)
  798. {
  799. tcc_cleanup();
  800. /* free sections */
  801. tccelf_delete(s1);
  802. /* free library paths */
  803. dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
  804. dynarray_reset(&s1->crt_paths, &s1->nb_crt_paths);
  805. /* free include paths */
  806. dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
  807. dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
  808. dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
  809. dynarray_reset(&s1->cmd_include_files, &s1->nb_cmd_include_files);
  810. tcc_free(s1->tcc_lib_path);
  811. tcc_free(s1->soname);
  812. tcc_free(s1->rpath);
  813. tcc_free(s1->init_symbol);
  814. tcc_free(s1->fini_symbol);
  815. tcc_free(s1->outfile);
  816. tcc_free(s1->deps_outfile);
  817. dynarray_reset(&s1->files, &s1->nb_files);
  818. dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
  819. dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
  820. dynarray_reset(&s1->argv, &s1->argc);
  821. #ifdef TCC_IS_NATIVE
  822. /* free runtime memory */
  823. tcc_run_free(s1);
  824. #endif
  825. tcc_free(s1);
  826. if (0 == --nb_states)
  827. tcc_memcheck();
  828. }
  829. LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
  830. {
  831. s->output_type = output_type;
  832. /* always elf for objects */
  833. if (output_type == TCC_OUTPUT_OBJ)
  834. s->output_format = TCC_OUTPUT_FORMAT_ELF;
  835. if (s->char_is_unsigned)
  836. tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
  837. if (!s->nostdinc) {
  838. /* default include paths */
  839. /* -isystem paths have already been handled */
  840. tcc_add_sysinclude_path(s, CONFIG_TCC_SYSINCLUDEPATHS);
  841. }
  842. #ifdef CONFIG_TCC_BCHECK
  843. if (s->do_bounds_check) {
  844. /* if bound checking, then add corresponding sections */
  845. tccelf_bounds_new(s);
  846. /* define symbol */
  847. tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
  848. }
  849. #endif
  850. if (s->do_debug) {
  851. /* add debug sections */
  852. tccelf_stab_new(s);
  853. }
  854. tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
  855. #ifdef TCC_TARGET_PE
  856. # ifdef _WIN32
  857. if (!s->nostdlib && output_type != TCC_OUTPUT_OBJ)
  858. tcc_add_systemdir(s);
  859. # endif
  860. #else
  861. /* paths for crt objects */
  862. tcc_split_path(s, &s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX);
  863. /* add libc crt1/crti objects */
  864. if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
  865. !s->nostdlib) {
  866. if (output_type != TCC_OUTPUT_DLL)
  867. tcc_add_crt(s, "crt1.o");
  868. tcc_add_crt(s, "crti.o");
  869. }
  870. #endif
  871. return 0;
  872. }
  873. LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname)
  874. {
  875. tcc_split_path(s, &s->include_paths, &s->nb_include_paths, pathname);
  876. return 0;
  877. }
  878. LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
  879. {
  880. tcc_split_path(s, &s->sysinclude_paths, &s->nb_sysinclude_paths, pathname);
  881. return 0;
  882. }
  883. ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
  884. {
  885. int ret;
  886. /* open the file */
  887. ret = tcc_open(s1, filename);
  888. if (ret < 0) {
  889. if (flags & AFF_PRINT_ERROR)
  890. tcc_error_noabort("file '%s' not found", filename);
  891. return ret;
  892. }
  893. /* update target deps */
  894. dynarray_add(&s1->target_deps, &s1->nb_target_deps,
  895. tcc_strdup(filename));
  896. if (flags & AFF_TYPE_BIN) {
  897. ElfW(Ehdr) ehdr;
  898. int fd, obj_type;
  899. fd = file->fd;
  900. obj_type = tcc_object_type(fd, &ehdr);
  901. lseek(fd, 0, SEEK_SET);
  902. #ifdef TCC_TARGET_MACHO
  903. if (0 == obj_type && 0 == strcmp(tcc_fileextension(filename), ".dylib"))
  904. obj_type = AFF_BINTYPE_DYN;
  905. #endif
  906. switch (obj_type) {
  907. case AFF_BINTYPE_REL:
  908. ret = tcc_load_object_file(s1, fd, 0);
  909. break;
  910. #ifndef TCC_TARGET_PE
  911. case AFF_BINTYPE_DYN:
  912. if (s1->output_type == TCC_OUTPUT_MEMORY) {
  913. ret = 0;
  914. #ifdef TCC_IS_NATIVE
  915. if (NULL == dlopen(filename, RTLD_GLOBAL | RTLD_LAZY))
  916. ret = -1;
  917. #endif
  918. } else {
  919. ret = tcc_load_dll(s1, fd, filename,
  920. (flags & AFF_REFERENCED_DLL) != 0);
  921. }
  922. break;
  923. #endif
  924. case AFF_BINTYPE_AR:
  925. ret = tcc_load_archive(s1, fd, !(flags & AFF_WHOLE_ARCHIVE));
  926. break;
  927. #ifdef TCC_TARGET_COFF
  928. case AFF_BINTYPE_C67:
  929. ret = tcc_load_coff(s1, fd);
  930. break;
  931. #endif
  932. default:
  933. #ifdef TCC_TARGET_PE
  934. ret = pe_load_file(s1, filename, fd);
  935. #else
  936. /* as GNU ld, consider it is an ld script if not recognized */
  937. ret = tcc_load_ldscript(s1);
  938. #endif
  939. if (ret < 0)
  940. tcc_error_noabort("unrecognized file type");
  941. break;
  942. }
  943. } else {
  944. ret = tcc_compile(s1, flags);
  945. }
  946. tcc_close();
  947. return ret;
  948. }
  949. LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
  950. {
  951. int filetype = s->filetype;
  952. if (0 == (filetype & AFF_TYPE_MASK)) {
  953. /* use a file extension to detect a filetype */
  954. const char *ext = tcc_fileextension(filename);
  955. if (ext[0]) {
  956. ext++;
  957. if (!strcmp(ext, "S"))
  958. filetype = AFF_TYPE_ASMPP;
  959. else if (!strcmp(ext, "s"))
  960. filetype = AFF_TYPE_ASM;
  961. else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
  962. filetype = AFF_TYPE_C;
  963. else
  964. filetype |= AFF_TYPE_BIN;
  965. } else {
  966. filetype = AFF_TYPE_C;
  967. }
  968. }
  969. return tcc_add_file_internal(s, filename, filetype | AFF_PRINT_ERROR);
  970. }
  971. LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
  972. {
  973. tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname);
  974. return 0;
  975. }
  976. static int tcc_add_library_internal(TCCState *s, const char *fmt,
  977. const char *filename, int flags, char **paths, int nb_paths)
  978. {
  979. char buf[1024];
  980. int i;
  981. for(i = 0; i < nb_paths; i++) {
  982. snprintf(buf, sizeof(buf), fmt, paths[i], filename);
  983. if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
  984. return 0;
  985. }
  986. return -1;
  987. }
  988. /* find and load a dll. Return non zero if not found */
  989. /* XXX: add '-rpath' option support ? */
  990. ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
  991. {
  992. return tcc_add_library_internal(s, "%s/%s", filename, flags,
  993. s->library_paths, s->nb_library_paths);
  994. }
  995. ST_FUNC int tcc_add_crt(TCCState *s, const char *filename)
  996. {
  997. if (-1 == tcc_add_library_internal(s, "%s/%s",
  998. filename, 0, s->crt_paths, s->nb_crt_paths))
  999. tcc_error_noabort("file '%s' not found", filename);
  1000. return 0;
  1001. }
  1002. /* the library name is the same as the argument of the '-l' option */
  1003. LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
  1004. {
  1005. #if defined TCC_TARGET_PE
  1006. const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
  1007. const char **pp = s->static_link ? libs + 4 : libs;
  1008. #elif defined TCC_TARGET_MACHO
  1009. const char *libs[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL };
  1010. const char **pp = s->static_link ? libs + 1 : libs;
  1011. #else
  1012. const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
  1013. const char **pp = s->static_link ? libs + 1 : libs;
  1014. #endif
  1015. int flags = s->filetype & AFF_WHOLE_ARCHIVE;
  1016. while (*pp) {
  1017. if (0 == tcc_add_library_internal(s, *pp,
  1018. libraryname, flags, s->library_paths, s->nb_library_paths))
  1019. return 0;
  1020. ++pp;
  1021. }
  1022. return -1;
  1023. }
  1024. PUB_FUNC int tcc_add_library_err(TCCState *s, const char *libname)
  1025. {
  1026. int ret = tcc_add_library(s, libname);
  1027. if (ret < 0)
  1028. tcc_error_noabort("library '%s' not found", libname);
  1029. return ret;
  1030. }
  1031. /* handle #pragma comment(lib,) */
  1032. ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
  1033. {
  1034. int i;
  1035. for (i = 0; i < s1->nb_pragma_libs; i++)
  1036. tcc_add_library_err(s1, s1->pragma_libs[i]);
  1037. }
  1038. LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
  1039. {
  1040. #ifdef TCC_TARGET_PE
  1041. /* On x86_64 'val' might not be reachable with a 32bit offset.
  1042. So it is handled here as if it were in a DLL. */
  1043. pe_putimport(s, 0, name, (uintptr_t)val);
  1044. #else
  1045. set_elf_sym(symtab_section, (uintptr_t)val, 0,
  1046. ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
  1047. SHN_ABS, name);
  1048. #endif
  1049. return 0;
  1050. }
  1051. LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
  1052. {
  1053. tcc_free(s->tcc_lib_path);
  1054. s->tcc_lib_path = tcc_strdup(path);
  1055. }
  1056. #define WD_ALL 0x0001 /* warning is activated when using -Wall */
  1057. #define FD_INVERT 0x0002 /* invert value before storing */
  1058. typedef struct FlagDef {
  1059. uint16_t offset;
  1060. uint16_t flags;
  1061. const char *name;
  1062. } FlagDef;
  1063. static int no_flag(const char **pp)
  1064. {
  1065. const char *p = *pp;
  1066. if (*p != 'n' || *++p != 'o' || *++p != '-')
  1067. return 0;
  1068. *pp = p + 1;
  1069. return 1;
  1070. }
  1071. ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, const char *name)
  1072. {
  1073. int value, ret;
  1074. const FlagDef *p;
  1075. const char *r;
  1076. value = 1;
  1077. r = name;
  1078. if (no_flag(&r))
  1079. value = 0;
  1080. for (ret = -1, p = flags; p->name; ++p) {
  1081. if (ret) {
  1082. if (strcmp(r, p->name))
  1083. continue;
  1084. } else {
  1085. if (0 == (p->flags & WD_ALL))
  1086. continue;
  1087. }
  1088. if (p->offset) {
  1089. *(int*)((char *)s + p->offset) =
  1090. p->flags & FD_INVERT ? !value : value;
  1091. if (ret)
  1092. return 0;
  1093. } else {
  1094. ret = 0;
  1095. }
  1096. }
  1097. return ret;
  1098. }
  1099. static int strstart(const char *val, const char **str)
  1100. {
  1101. const char *p, *q;
  1102. p = *str;
  1103. q = val;
  1104. while (*q) {
  1105. if (*p != *q)
  1106. return 0;
  1107. p++;
  1108. q++;
  1109. }
  1110. *str = p;
  1111. return 1;
  1112. }
  1113. /* Like strstart, but automatically takes into account that ld options can
  1114. *
  1115. * - start with double or single dash (e.g. '--soname' or '-soname')
  1116. * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
  1117. * or '-Wl,-soname=x.so')
  1118. *
  1119. * you provide `val` always in 'option[=]' form (no leading -)
  1120. */
  1121. static int link_option(const char *str, const char *val, const char **ptr)
  1122. {
  1123. const char *p, *q;
  1124. int ret;
  1125. /* there should be 1 or 2 dashes */
  1126. if (*str++ != '-')
  1127. return 0;
  1128. if (*str == '-')
  1129. str++;
  1130. /* then str & val should match (potentially up to '=') */
  1131. p = str;
  1132. q = val;
  1133. ret = 1;
  1134. if (q[0] == '?') {
  1135. ++q;
  1136. if (no_flag(&p))
  1137. ret = -1;
  1138. }
  1139. while (*q != '\0' && *q != '=') {
  1140. if (*p != *q)
  1141. return 0;
  1142. p++;
  1143. q++;
  1144. }
  1145. /* '=' near eos means ',' or '=' is ok */
  1146. if (*q == '=') {
  1147. if (*p == 0)
  1148. *ptr = p;
  1149. if (*p != ',' && *p != '=')
  1150. return 0;
  1151. p++;
  1152. } else if (*p) {
  1153. return 0;
  1154. }
  1155. *ptr = p;
  1156. return ret;
  1157. }
  1158. static const char *skip_linker_arg(const char **str)
  1159. {
  1160. const char *s1 = *str;
  1161. const char *s2 = strchr(s1, ',');
  1162. *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
  1163. return s2;
  1164. }
  1165. static void copy_linker_arg(char **pp, const char *s, int sep)
  1166. {
  1167. const char *q = s;
  1168. char *p = *pp;
  1169. int l = 0;
  1170. if (p && sep)
  1171. p[l = strlen(p)] = sep, ++l;
  1172. skip_linker_arg(&q);
  1173. pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s);
  1174. }
  1175. /* set linker options */
  1176. static int tcc_set_linker(TCCState *s, const char *option)
  1177. {
  1178. while (*option) {
  1179. const char *p = NULL;
  1180. char *end = NULL;
  1181. int ignoring = 0;
  1182. int ret;
  1183. if (link_option(option, "Bsymbolic", &p)) {
  1184. s->symbolic = 1;
  1185. } else if (link_option(option, "nostdlib", &p)) {
  1186. s->nostdlib = 1;
  1187. } else if (link_option(option, "fini=", &p)) {
  1188. copy_linker_arg(&s->fini_symbol, p, 0);
  1189. ignoring = 1;
  1190. } else if (link_option(option, "image-base=", &p)
  1191. || link_option(option, "Ttext=", &p)) {
  1192. s->text_addr = strtoull(p, &end, 16);
  1193. s->has_text_addr = 1;
  1194. } else if (link_option(option, "init=", &p)) {
  1195. copy_linker_arg(&s->init_symbol, p, 0);
  1196. ignoring = 1;
  1197. } else if (link_option(option, "oformat=", &p)) {
  1198. #if defined(TCC_TARGET_PE)
  1199. if (strstart("pe-", &p)) {
  1200. #elif PTR_SIZE == 8
  1201. if (strstart("elf64-", &p)) {
  1202. #else
  1203. if (strstart("elf32-", &p)) {
  1204. #endif
  1205. s->output_format = TCC_OUTPUT_FORMAT_ELF;
  1206. } else if (!strcmp(p, "binary")) {
  1207. s->output_format = TCC_OUTPUT_FORMAT_BINARY;
  1208. #ifdef TCC_TARGET_COFF
  1209. } else if (!strcmp(p, "coff")) {
  1210. s->output_format = TCC_OUTPUT_FORMAT_COFF;
  1211. #endif
  1212. } else
  1213. goto err;
  1214. } else if (link_option(option, "as-needed", &p)) {
  1215. ignoring = 1;
  1216. } else if (link_option(option, "O", &p)) {
  1217. ignoring = 1;
  1218. } else if (link_option(option, "export-all-symbols", &p)) {
  1219. s->rdynamic = 1;
  1220. } else if (link_option(option, "export-dynamic", &p)) {
  1221. s->rdynamic = 1;
  1222. } else if (link_option(option, "rpath=", &p)) {
  1223. copy_linker_arg(&s->rpath, p, ':');
  1224. } else if (link_option(option, "enable-new-dtags", &p)) {
  1225. s->enable_new_dtags = 1;
  1226. } else if (link_option(option, "section-alignment=", &p)) {
  1227. s->section_align = strtoul(p, &end, 16);
  1228. } else if (link_option(option, "soname=", &p)) {
  1229. copy_linker_arg(&s->soname, p, 0);
  1230. #ifdef TCC_TARGET_PE
  1231. } else if (link_option(option, "large-address-aware", &p)) {
  1232. s->pe_characteristics |= 0x20;
  1233. } else if (link_option(option, "file-alignment=", &p)) {
  1234. s->pe_file_align = strtoul(p, &end, 16);
  1235. } else if (link_option(option, "stack=", &p)) {
  1236. s->pe_stack_size = strtoul(p, &end, 10);
  1237. } else if (link_option(option, "subsystem=", &p)) {
  1238. #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
  1239. if (!strcmp(p, "native")) {
  1240. s->pe_subsystem = 1;
  1241. } else if (!strcmp(p, "console")) {
  1242. s->pe_subsystem = 3;
  1243. } else if (!strcmp(p, "gui") || !strcmp(p, "windows")) {
  1244. s->pe_subsystem = 2;
  1245. } else if (!strcmp(p, "posix")) {
  1246. s->pe_subsystem = 7;
  1247. } else if (!strcmp(p, "efiapp")) {
  1248. s->pe_subsystem = 10;
  1249. } else if (!strcmp(p, "efiboot")) {
  1250. s->pe_subsystem = 11;
  1251. } else if (!strcmp(p, "efiruntime")) {
  1252. s->pe_subsystem = 12;
  1253. } else if (!strcmp(p, "efirom")) {
  1254. s->pe_subsystem = 13;
  1255. #elif defined(TCC_TARGET_ARM)
  1256. if (!strcmp(p, "wince")) {
  1257. s->pe_subsystem = 9;
  1258. #endif
  1259. } else
  1260. goto err;
  1261. #endif
  1262. } else if (ret = link_option(option, "?whole-archive", &p), ret) {
  1263. if (ret > 0)
  1264. s->filetype |= AFF_WHOLE_ARCHIVE;
  1265. else
  1266. s->filetype &= ~AFF_WHOLE_ARCHIVE;
  1267. } else if (p) {
  1268. return 0;
  1269. } else {
  1270. err:
  1271. tcc_error("unsupported linker option '%s'", option);
  1272. }
  1273. if (ignoring && s->warn_unsupported)
  1274. tcc_warning("unsupported linker option '%s'", option);
  1275. option = skip_linker_arg(&p);
  1276. }
  1277. return 1;
  1278. }
  1279. typedef struct TCCOption {
  1280. const char *name;
  1281. uint16_t index;
  1282. uint16_t flags;
  1283. } TCCOption;
  1284. enum {
  1285. TCC_OPTION_HELP,
  1286. TCC_OPTION_HELP2,
  1287. TCC_OPTION_v,
  1288. TCC_OPTION_I,
  1289. TCC_OPTION_D,
  1290. TCC_OPTION_U,
  1291. TCC_OPTION_P,
  1292. TCC_OPTION_L,
  1293. TCC_OPTION_B,
  1294. TCC_OPTION_l,
  1295. TCC_OPTION_bench,
  1296. TCC_OPTION_bt,
  1297. TCC_OPTION_b,
  1298. TCC_OPTION_g,
  1299. TCC_OPTION_c,
  1300. TCC_OPTION_dumpversion,
  1301. TCC_OPTION_d,
  1302. TCC_OPTION_static,
  1303. TCC_OPTION_std,
  1304. TCC_OPTION_shared,
  1305. TCC_OPTION_soname,
  1306. TCC_OPTION_o,
  1307. TCC_OPTION_r,
  1308. TCC_OPTION_s,
  1309. TCC_OPTION_traditional,
  1310. TCC_OPTION_Wl,
  1311. TCC_OPTION_Wp,
  1312. TCC_OPTION_W,
  1313. TCC_OPTION_O,
  1314. TCC_OPTION_mfloat_abi,
  1315. TCC_OPTION_m,
  1316. TCC_OPTION_f,
  1317. TCC_OPTION_isystem,
  1318. TCC_OPTION_iwithprefix,
  1319. TCC_OPTION_include,
  1320. TCC_OPTION_nostdinc,
  1321. TCC_OPTION_nostdlib,
  1322. TCC_OPTION_print_search_dirs,
  1323. TCC_OPTION_rdynamic,
  1324. TCC_OPTION_param,
  1325. TCC_OPTION_pedantic,
  1326. TCC_OPTION_pthread,
  1327. TCC_OPTION_run,
  1328. TCC_OPTION_w,
  1329. TCC_OPTION_pipe,
  1330. TCC_OPTION_E,
  1331. TCC_OPTION_MD,
  1332. TCC_OPTION_MF,
  1333. TCC_OPTION_x,
  1334. TCC_OPTION_ar,
  1335. TCC_OPTION_impdef
  1336. };
  1337. #define TCC_OPTION_HAS_ARG 0x0001
  1338. #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
  1339. static const TCCOption tcc_options[] = {
  1340. { "h", TCC_OPTION_HELP, 0 },
  1341. { "-help", TCC_OPTION_HELP, 0 },
  1342. { "?", TCC_OPTION_HELP, 0 },
  1343. { "hh", TCC_OPTION_HELP2, 0 },
  1344. { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1345. { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
  1346. { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
  1347. { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
  1348. { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1349. { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
  1350. { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
  1351. { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1352. { "bench", TCC_OPTION_bench, 0 },
  1353. #ifdef CONFIG_TCC_BACKTRACE
  1354. { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
  1355. #endif
  1356. #ifdef CONFIG_TCC_BCHECK
  1357. { "b", TCC_OPTION_b, 0 },
  1358. #endif
  1359. { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1360. { "c", TCC_OPTION_c, 0 },
  1361. { "dumpversion", TCC_OPTION_dumpversion, 0},
  1362. { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1363. { "static", TCC_OPTION_static, 0 },
  1364. { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1365. { "shared", TCC_OPTION_shared, 0 },
  1366. { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
  1367. { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
  1368. { "-param", TCC_OPTION_param, TCC_OPTION_HAS_ARG },
  1369. { "pedantic", TCC_OPTION_pedantic, 0},
  1370. { "pthread", TCC_OPTION_pthread, 0},
  1371. { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1372. { "rdynamic", TCC_OPTION_rdynamic, 0 },
  1373. { "r", TCC_OPTION_r, 0 },
  1374. { "s", TCC_OPTION_s, 0 },
  1375. { "traditional", TCC_OPTION_traditional, 0 },
  1376. { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1377. { "Wp,", TCC_OPTION_Wp, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1378. { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1379. { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1380. #ifdef TCC_TARGET_ARM
  1381. { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG },
  1382. #endif
  1383. { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1384. { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1385. { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
  1386. { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG },
  1387. { "nostdinc", TCC_OPTION_nostdinc, 0 },
  1388. { "nostdlib", TCC_OPTION_nostdlib, 0 },
  1389. { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
  1390. { "w", TCC_OPTION_w, 0 },
  1391. { "pipe", TCC_OPTION_pipe, 0},
  1392. { "E", TCC_OPTION_E, 0},
  1393. { "MD", TCC_OPTION_MD, 0},
  1394. { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
  1395. { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
  1396. { "ar", TCC_OPTION_ar, 0},
  1397. #ifdef TCC_TARGET_PE
  1398. { "impdef", TCC_OPTION_impdef, 0},
  1399. #endif
  1400. { NULL, 0, 0 },
  1401. };
  1402. static const FlagDef options_W[] = {
  1403. { 0, 0, "all" },
  1404. { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
  1405. { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
  1406. { offsetof(TCCState, warn_error), 0, "error" },
  1407. { offsetof(TCCState, warn_gcc_compat), 0, "gcc-compat" },
  1408. { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
  1409. "implicit-function-declaration" },
  1410. { 0, 0, NULL }
  1411. };
  1412. static const FlagDef options_f[] = {
  1413. { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
  1414. { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
  1415. { offsetof(TCCState, nocommon), FD_INVERT, "common" },
  1416. { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
  1417. { offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
  1418. { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
  1419. { 0, 0, NULL }
  1420. };
  1421. static const FlagDef options_m[] = {
  1422. { offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" },
  1423. #ifdef TCC_TARGET_X86_64
  1424. { offsetof(TCCState, nosse), FD_INVERT, "sse" },
  1425. #endif
  1426. { 0, 0, NULL }
  1427. };
  1428. static void parse_option_D(TCCState *s1, const char *optarg)
  1429. {
  1430. char *sym = tcc_strdup(optarg);
  1431. char *value = strchr(sym, '=');
  1432. if (value)
  1433. *value++ = '\0';
  1434. tcc_define_symbol(s1, sym, value);
  1435. tcc_free(sym);
  1436. }
  1437. static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
  1438. {
  1439. struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
  1440. f->type = filetype;
  1441. strcpy(f->name, filename);
  1442. dynarray_add(&s->files, &s->nb_files, f);
  1443. }
  1444. static int args_parser_make_argv(const char *r, int *argc, char ***argv)
  1445. {
  1446. int ret = 0, q, c;
  1447. CString str;
  1448. for(;;) {
  1449. while (c = (unsigned char)*r, c && c <= ' ')
  1450. ++r;
  1451. if (c == 0)
  1452. break;
  1453. q = 0;
  1454. cstr_new(&str);
  1455. while (c = (unsigned char)*r, c) {
  1456. ++r;
  1457. if (c == '\\' && (*r == '"' || *r == '\\')) {
  1458. c = *r++;
  1459. } else if (c == '"') {
  1460. q = !q;
  1461. continue;
  1462. } else if (q == 0 && c <= ' ') {
  1463. break;
  1464. }
  1465. cstr_ccat(&str, c);
  1466. }
  1467. cstr_ccat(&str, 0);
  1468. //printf("<%s>\n", str.data), fflush(stdout);
  1469. dynarray_add(argv, argc, tcc_strdup(str.data));
  1470. cstr_free(&str);
  1471. ++ret;
  1472. }
  1473. return ret;
  1474. }
  1475. /* read list file */
  1476. static void args_parser_listfile(TCCState *s,
  1477. const char *filename, int optind, int *pargc, char ***pargv)
  1478. {
  1479. int fd, i;
  1480. size_t len;
  1481. char *p;
  1482. int argc = 0;
  1483. char **argv = NULL;
  1484. fd = open(filename, O_RDONLY | O_BINARY);
  1485. if (fd < 0)
  1486. tcc_error("listfile '%s' not found", filename);
  1487. len = lseek(fd, 0, SEEK_END);
  1488. p = tcc_malloc(len + 1), p[len] = 0;
  1489. lseek(fd, 0, SEEK_SET), read(fd, p, len), close(fd);
  1490. for (i = 0; i < *pargc; ++i)
  1491. if (i == optind)
  1492. args_parser_make_argv(p, &argc, &argv);
  1493. else
  1494. dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i]));
  1495. tcc_free(p);
  1496. dynarray_reset(&s->argv, &s->argc);
  1497. *pargc = s->argc = argc, *pargv = s->argv = argv;
  1498. }
  1499. PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
  1500. {
  1501. const TCCOption *popt;
  1502. const char *optarg, *r;
  1503. const char *run = NULL;
  1504. int last_o = -1;
  1505. int x;
  1506. CString linker_arg; /* collect -Wl options */
  1507. int tool = 0, arg_start = 0, noaction = optind;
  1508. char **argv = *pargv;
  1509. int argc = *pargc;
  1510. cstr_new(&linker_arg);
  1511. while (optind < argc) {
  1512. r = argv[optind];
  1513. if (r[0] == '@' && r[1] != '\0') {
  1514. args_parser_listfile(s, r + 1, optind, &argc, &argv);
  1515. continue;
  1516. }
  1517. optind++;
  1518. if (tool) {
  1519. if (r[0] == '-' && r[1] == 'v' && r[2] == 0)
  1520. ++s->verbose;
  1521. continue;
  1522. }
  1523. reparse:
  1524. if (r[0] != '-' || r[1] == '\0') {
  1525. if (r[0] != '@') /* allow "tcc file(s) -run @ args ..." */
  1526. args_parser_add_file(s, r, s->filetype);
  1527. if (run) {
  1528. tcc_set_options(s, run);
  1529. arg_start = optind - 1;
  1530. break;
  1531. }
  1532. continue;
  1533. }
  1534. /* find option in table */
  1535. for(popt = tcc_options; ; ++popt) {
  1536. const char *p1 = popt->name;
  1537. const char *r1 = r + 1;
  1538. if (p1 == NULL)
  1539. tcc_error("invalid option -- '%s'", r);
  1540. if (!strstart(p1, &r1))
  1541. continue;
  1542. optarg = r1;
  1543. if (popt->flags & TCC_OPTION_HAS_ARG) {
  1544. if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
  1545. if (optind >= argc)
  1546. arg_err:
  1547. tcc_error("argument to '%s' is missing", r);
  1548. optarg = argv[optind++];
  1549. }
  1550. } else if (*r1 != '\0')
  1551. continue;
  1552. break;
  1553. }
  1554. switch(popt->index) {
  1555. case TCC_OPTION_HELP:
  1556. return OPT_HELP;
  1557. case TCC_OPTION_HELP2:
  1558. return OPT_HELP2;
  1559. case TCC_OPTION_I:
  1560. tcc_add_include_path(s, optarg);
  1561. break;
  1562. case TCC_OPTION_D:
  1563. parse_option_D(s, optarg);
  1564. break;
  1565. case TCC_OPTION_U:
  1566. tcc_undefine_symbol(s, optarg);
  1567. break;
  1568. case TCC_OPTION_L:
  1569. tcc_add_library_path(s, optarg);
  1570. break;
  1571. case TCC_OPTION_B:
  1572. /* set tcc utilities path (mainly for tcc development) */
  1573. tcc_set_lib_path(s, optarg);
  1574. break;
  1575. case TCC_OPTION_l:
  1576. args_parser_add_file(s, optarg, AFF_TYPE_LIB | (s->filetype & ~AFF_TYPE_MASK));
  1577. s->nb_libraries++;
  1578. break;
  1579. case TCC_OPTION_pthread:
  1580. parse_option_D(s, "_REENTRANT");
  1581. s->option_pthread = 1;
  1582. break;
  1583. case TCC_OPTION_bench:
  1584. s->do_bench = 1;
  1585. break;
  1586. #ifdef CONFIG_TCC_BACKTRACE
  1587. case TCC_OPTION_bt:
  1588. tcc_set_num_callers(atoi(optarg));
  1589. break;
  1590. #endif
  1591. #ifdef CONFIG_TCC_BCHECK
  1592. case TCC_OPTION_b:
  1593. s->do_bounds_check = 1;
  1594. s->do_debug = 1;
  1595. break;
  1596. #endif
  1597. case TCC_OPTION_g:
  1598. s->do_debug = 1;
  1599. break;
  1600. case TCC_OPTION_c:
  1601. x = TCC_OUTPUT_OBJ;
  1602. set_output_type:
  1603. if (s->output_type)
  1604. tcc_warning("-%s: overriding compiler action already specified", popt->name);
  1605. s->output_type = x;
  1606. break;
  1607. case TCC_OPTION_d:
  1608. if (*optarg == 'D')
  1609. s->dflag = 3;
  1610. else if (*optarg == 'M')
  1611. s->dflag = 7;
  1612. else if (*optarg == 't')
  1613. s->dflag = 16;
  1614. else if (isnum(*optarg))
  1615. g_debug = atoi(optarg);
  1616. else
  1617. goto unsupported_option;
  1618. break;
  1619. case TCC_OPTION_static:
  1620. s->static_link = 1;
  1621. break;
  1622. case TCC_OPTION_std:
  1623. /* silently ignore, a current purpose:
  1624. allow to use a tcc as a reference compiler for "make test" */
  1625. break;
  1626. case TCC_OPTION_shared:
  1627. x = TCC_OUTPUT_DLL;
  1628. goto set_output_type;
  1629. case TCC_OPTION_soname:
  1630. s->soname = tcc_strdup(optarg);
  1631. break;
  1632. case TCC_OPTION_o:
  1633. if (s->outfile) {
  1634. tcc_warning("multiple -o option");
  1635. tcc_free(s->outfile);
  1636. }
  1637. s->outfile = tcc_strdup(optarg);
  1638. break;
  1639. case TCC_OPTION_r:
  1640. /* generate a .o merging several output files */
  1641. s->option_r = 1;
  1642. x = TCC_OUTPUT_OBJ;
  1643. goto set_output_type;
  1644. case TCC_OPTION_isystem:
  1645. tcc_add_sysinclude_path(s, optarg);
  1646. break;
  1647. case TCC_OPTION_include:
  1648. dynarray_add(&s->cmd_include_files,
  1649. &s->nb_cmd_include_files, tcc_strdup(optarg));
  1650. break;
  1651. case TCC_OPTION_nostdinc:
  1652. s->nostdinc = 1;
  1653. break;
  1654. case TCC_OPTION_nostdlib:
  1655. s->nostdlib = 1;
  1656. break;
  1657. case TCC_OPTION_run:
  1658. #ifndef TCC_IS_NATIVE
  1659. tcc_error("-run is not available in a cross compiler");
  1660. #endif
  1661. run = optarg;
  1662. x = TCC_OUTPUT_MEMORY;
  1663. goto set_output_type;
  1664. case TCC_OPTION_v:
  1665. do ++s->verbose; while (*optarg++ == 'v');
  1666. ++noaction;
  1667. break;
  1668. case TCC_OPTION_f:
  1669. if (set_flag(s, options_f, optarg) < 0)
  1670. goto unsupported_option;
  1671. break;
  1672. #ifdef TCC_TARGET_ARM
  1673. case TCC_OPTION_mfloat_abi:
  1674. /* tcc doesn't support soft float yet */
  1675. if (!strcmp(optarg, "softfp")) {
  1676. s->float_abi = ARM_SOFTFP_FLOAT;
  1677. tcc_undefine_symbol(s, "__ARM_PCS_VFP");
  1678. } else if (!strcmp(optarg, "hard"))
  1679. s->float_abi = ARM_HARD_FLOAT;
  1680. else
  1681. tcc_error("unsupported float abi '%s'", optarg);
  1682. break;
  1683. #endif
  1684. case TCC_OPTION_m:
  1685. if (set_flag(s, options_m, optarg) < 0) {
  1686. if (x = atoi(optarg), x != 32 && x != 64)
  1687. goto unsupported_option;
  1688. if (PTR_SIZE != x/8)
  1689. return x;
  1690. ++noaction;
  1691. }
  1692. break;
  1693. case TCC_OPTION_W:
  1694. if (set_flag(s, options_W, optarg) < 0)
  1695. goto unsupported_option;
  1696. break;
  1697. case TCC_OPTION_w:
  1698. s->warn_none = 1;
  1699. break;
  1700. case TCC_OPTION_rdynamic:
  1701. s->rdynamic = 1;
  1702. break;
  1703. case TCC_OPTION_Wl:
  1704. if (linker_arg.size)
  1705. --linker_arg.size, cstr_ccat(&linker_arg, ',');
  1706. cstr_cat(&linker_arg, optarg, 0);
  1707. if (tcc_set_linker(s, linker_arg.data))
  1708. cstr_free(&linker_arg);
  1709. break;
  1710. case TCC_OPTION_Wp:
  1711. r = optarg;
  1712. goto reparse;
  1713. case TCC_OPTION_E:
  1714. x = TCC_OUTPUT_PREPROCESS;
  1715. goto set_output_type;
  1716. case TCC_OPTION_P:
  1717. s->Pflag = atoi(optarg) + 1;
  1718. break;
  1719. case TCC_OPTION_MD:
  1720. s->gen_deps = 1;
  1721. break;
  1722. case TCC_OPTION_MF:
  1723. s->deps_outfile = tcc_strdup(optarg);
  1724. break;
  1725. case TCC_OPTION_dumpversion:
  1726. printf ("%s\n", TCC_VERSION);
  1727. exit(0);
  1728. break;
  1729. case TCC_OPTION_x:
  1730. x = 0;
  1731. if (*optarg == 'c')
  1732. x = AFF_TYPE_C;
  1733. else if (*optarg == 'a')
  1734. x = AFF_TYPE_ASMPP;
  1735. else if (*optarg == 'b')
  1736. x = AFF_TYPE_BIN;
  1737. else if (*optarg == 'n')
  1738. x = AFF_TYPE_NONE;
  1739. else
  1740. tcc_warning("unsupported language '%s'", optarg);
  1741. s->filetype = x | (s->filetype & ~AFF_TYPE_MASK);
  1742. break;
  1743. case TCC_OPTION_O:
  1744. last_o = atoi(optarg);
  1745. break;
  1746. case TCC_OPTION_print_search_dirs:
  1747. x = OPT_PRINT_DIRS;
  1748. goto extra_action;
  1749. case TCC_OPTION_impdef:
  1750. x = OPT_IMPDEF;
  1751. goto extra_action;
  1752. case TCC_OPTION_ar:
  1753. x = OPT_AR;
  1754. extra_action:
  1755. arg_start = optind - 1;
  1756. if (arg_start != noaction)
  1757. tcc_error("cannot parse %s here", r);
  1758. tool = x;
  1759. break;
  1760. case TCC_OPTION_traditional:
  1761. case TCC_OPTION_pedantic:
  1762. case TCC_OPTION_pipe:
  1763. case TCC_OPTION_s:
  1764. /* ignored */
  1765. break;
  1766. default:
  1767. unsupported_option:
  1768. if (s->warn_unsupported)
  1769. tcc_warning("unsupported option '%s'", r);
  1770. break;
  1771. }
  1772. }
  1773. if (last_o > 0)
  1774. tcc_define_symbol(s, "__OPTIMIZE__", NULL);
  1775. if (linker_arg.size) {
  1776. r = linker_arg.data;
  1777. goto arg_err;
  1778. }
  1779. *pargc = argc - arg_start;
  1780. *pargv = argv + arg_start;
  1781. if (tool)
  1782. return tool;
  1783. if (optind != noaction)
  1784. return 0;
  1785. if (s->verbose == 2)
  1786. return OPT_PRINT_DIRS;
  1787. if (s->verbose)
  1788. return OPT_V;
  1789. return OPT_HELP;
  1790. }
  1791. LIBTCCAPI void tcc_set_options(TCCState *s, const char *r)
  1792. {
  1793. char **argv = NULL;
  1794. int argc = 0;
  1795. args_parser_make_argv(r, &argc, &argv);
  1796. tcc_parse_args(s, &argc, &argv, 0);
  1797. dynarray_reset(&argv, &argc);
  1798. }
  1799. PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time)
  1800. {
  1801. if (total_time < 1)
  1802. total_time = 1;
  1803. if (total_bytes < 1)
  1804. total_bytes = 1;
  1805. fprintf(stderr, "* %d idents, %d lines, %d bytes\n"
  1806. "* %0.3f s, %u lines/s, %0.1f MB/s\n",
  1807. tok_ident - TOK_IDENT, total_lines, total_bytes,
  1808. (double)total_time/1000,
  1809. (unsigned)total_lines*1000/total_time,
  1810. (double)total_bytes/1000/total_time);
  1811. #ifdef MEM_DEBUG
  1812. fprintf(stderr, "* %d bytes memory used\n", mem_max_size);
  1813. #endif
  1814. }