libtcc.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  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 (f->line_num > 0) {
  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)
  552. {
  553. Sym *define_start;
  554. int filetype, is_asm;
  555. define_start = define_stack;
  556. filetype = s1->filetype;
  557. is_asm = filetype == AFF_TYPE_ASM || filetype == AFF_TYPE_ASMPP;
  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. return s1->nb_errors != 0 ? -1 : 0;
  582. }
  583. LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
  584. {
  585. int len, ret;
  586. len = strlen(str);
  587. tcc_open_bf(s, "<string>", len);
  588. memcpy(file->buffer, str, len);
  589. ret = tcc_compile(s);
  590. tcc_close();
  591. return ret;
  592. }
  593. /* define a preprocessor symbol. A value can also be provided with the '=' operator */
  594. LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
  595. {
  596. int len1, len2;
  597. /* default value */
  598. if (!value)
  599. value = "1";
  600. len1 = strlen(sym);
  601. len2 = strlen(value);
  602. /* init file structure */
  603. tcc_open_bf(s1, "<define>", len1 + len2 + 1);
  604. memcpy(file->buffer, sym, len1);
  605. file->buffer[len1] = ' ';
  606. memcpy(file->buffer + len1 + 1, value, len2);
  607. /* parse with define parser */
  608. next_nomacro();
  609. parse_define();
  610. tcc_close();
  611. }
  612. /* undefine a preprocessor symbol */
  613. LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
  614. {
  615. TokenSym *ts;
  616. Sym *s;
  617. ts = tok_alloc(sym, strlen(sym));
  618. s = define_find(ts->tok);
  619. /* undefine symbol by putting an invalid name */
  620. if (s)
  621. define_undef(s);
  622. }
  623. /* cleanup all static data used during compilation */
  624. static void tcc_cleanup(void)
  625. {
  626. if (NULL == tcc_state)
  627. return;
  628. while (file)
  629. tcc_close();
  630. tccpp_delete(tcc_state);
  631. tcc_state = NULL;
  632. /* free sym_pools */
  633. dynarray_reset(&sym_pools, &nb_sym_pools);
  634. /* reset symbol stack */
  635. sym_free_first = NULL;
  636. }
  637. LIBTCCAPI TCCState *tcc_new(void)
  638. {
  639. TCCState *s;
  640. tcc_cleanup();
  641. s = tcc_mallocz(sizeof(TCCState));
  642. if (!s)
  643. return NULL;
  644. tcc_state = s;
  645. ++nb_states;
  646. s->alacarte_link = 1;
  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. /* do not display line number if error */
  903. file->line_num = 0;
  904. #ifdef TCC_TARGET_MACHO
  905. if (0 == obj_type && 0 == strcmp(tcc_fileextension(filename), ".dylib"))
  906. obj_type = AFF_BINTYPE_DYN;
  907. #endif
  908. switch (obj_type) {
  909. case AFF_BINTYPE_REL:
  910. ret = tcc_load_object_file(s1, fd, 0);
  911. break;
  912. #ifndef TCC_TARGET_PE
  913. case AFF_BINTYPE_DYN:
  914. if (s1->output_type == TCC_OUTPUT_MEMORY) {
  915. ret = 0;
  916. #ifdef TCC_IS_NATIVE
  917. if (NULL == dlopen(filename, RTLD_GLOBAL | RTLD_LAZY))
  918. ret = -1;
  919. #endif
  920. } else {
  921. ret = tcc_load_dll(s1, fd, filename,
  922. (flags & AFF_REFERENCED_DLL) != 0);
  923. }
  924. break;
  925. #endif
  926. case AFF_BINTYPE_AR:
  927. ret = tcc_load_archive(s1, fd);
  928. break;
  929. #ifdef TCC_TARGET_COFF
  930. case AFF_BINTYPE_C67:
  931. ret = tcc_load_coff(s1, fd);
  932. break;
  933. #endif
  934. default:
  935. #ifdef TCC_TARGET_PE
  936. ret = pe_load_file(s1, filename, fd);
  937. #else
  938. /* as GNU ld, consider it is an ld script if not recognized */
  939. ret = tcc_load_ldscript(s1);
  940. #endif
  941. if (ret < 0)
  942. tcc_error_noabort("unrecognized file type");
  943. break;
  944. }
  945. } else {
  946. ret = tcc_compile(s1);
  947. }
  948. tcc_close();
  949. return ret;
  950. }
  951. LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
  952. {
  953. int filetype = s->filetype;
  954. int flags = AFF_PRINT_ERROR;
  955. if (filetype == 0) {
  956. /* use a file extension to detect a filetype */
  957. const char *ext = tcc_fileextension(filename);
  958. if (ext[0]) {
  959. ext++;
  960. if (!strcmp(ext, "S"))
  961. filetype = AFF_TYPE_ASMPP;
  962. else if (!strcmp(ext, "s"))
  963. filetype = AFF_TYPE_ASM;
  964. else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
  965. filetype = AFF_TYPE_C;
  966. else
  967. flags |= AFF_TYPE_BIN;
  968. } else {
  969. filetype = AFF_TYPE_C;
  970. }
  971. s->filetype = filetype;
  972. }
  973. return tcc_add_file_internal(s, filename, flags);
  974. }
  975. LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
  976. {
  977. tcc_split_path(s, &s->library_paths, &s->nb_library_paths, pathname);
  978. return 0;
  979. }
  980. static int tcc_add_library_internal(TCCState *s, const char *fmt,
  981. const char *filename, int flags, char **paths, int nb_paths)
  982. {
  983. char buf[1024];
  984. int i;
  985. for(i = 0; i < nb_paths; i++) {
  986. snprintf(buf, sizeof(buf), fmt, paths[i], filename);
  987. if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
  988. return 0;
  989. }
  990. return -1;
  991. }
  992. /* find and load a dll. Return non zero if not found */
  993. /* XXX: add '-rpath' option support ? */
  994. ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
  995. {
  996. return tcc_add_library_internal(s, "%s/%s", filename, flags,
  997. s->library_paths, s->nb_library_paths);
  998. }
  999. ST_FUNC int tcc_add_crt(TCCState *s, const char *filename)
  1000. {
  1001. if (-1 == tcc_add_library_internal(s, "%s/%s",
  1002. filename, 0, s->crt_paths, s->nb_crt_paths))
  1003. tcc_error_noabort("file '%s' not found", filename);
  1004. return 0;
  1005. }
  1006. /* the library name is the same as the argument of the '-l' option */
  1007. LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
  1008. {
  1009. #if defined TCC_TARGET_PE
  1010. const char *libs[] = { "%s/%s.def", "%s/lib%s.def", "%s/%s.dll", "%s/lib%s.dll", "%s/lib%s.a", NULL };
  1011. const char **pp = s->static_link ? libs + 4 : libs;
  1012. #elif defined TCC_TARGET_MACHO
  1013. const char *libs[] = { "%s/lib%s.dylib", "%s/lib%s.a", NULL };
  1014. const char **pp = s->static_link ? libs + 1 : libs;
  1015. #else
  1016. const char *libs[] = { "%s/lib%s.so", "%s/lib%s.a", NULL };
  1017. const char **pp = s->static_link ? libs + 1 : libs;
  1018. #endif
  1019. while (*pp) {
  1020. if (0 == tcc_add_library_internal(s, *pp,
  1021. libraryname, 0, s->library_paths, s->nb_library_paths))
  1022. return 0;
  1023. ++pp;
  1024. }
  1025. return -1;
  1026. }
  1027. PUB_FUNC int tcc_add_library_err(TCCState *s, const char *libname)
  1028. {
  1029. int ret = tcc_add_library(s, libname);
  1030. if (ret < 0)
  1031. tcc_error_noabort("library '%s' not found", libname);
  1032. return ret;
  1033. }
  1034. /* handle #pragma comment(lib,) */
  1035. ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
  1036. {
  1037. int i;
  1038. for (i = 0; i < s1->nb_pragma_libs; i++)
  1039. tcc_add_library_err(s1, s1->pragma_libs[i]);
  1040. }
  1041. LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
  1042. {
  1043. #ifdef TCC_TARGET_PE
  1044. /* On x86_64 'val' might not be reachable with a 32bit offset.
  1045. So it is handled here as if it were in a DLL. */
  1046. pe_putimport(s, 0, name, (uintptr_t)val);
  1047. #else
  1048. set_elf_sym(symtab_section, (uintptr_t)val, 0,
  1049. ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
  1050. SHN_ABS, name);
  1051. #endif
  1052. return 0;
  1053. }
  1054. LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path)
  1055. {
  1056. tcc_free(s->tcc_lib_path);
  1057. s->tcc_lib_path = tcc_strdup(path);
  1058. }
  1059. #define WD_ALL 0x0001 /* warning is activated when using -Wall */
  1060. #define FD_INVERT 0x0002 /* invert value before storing */
  1061. typedef struct FlagDef {
  1062. uint16_t offset;
  1063. uint16_t flags;
  1064. const char *name;
  1065. } FlagDef;
  1066. static int no_flag(const char **pp)
  1067. {
  1068. const char *p = *pp;
  1069. if (*p != 'n' || *++p != 'o' || *++p != '-')
  1070. return 0;
  1071. *pp = p + 1;
  1072. return 1;
  1073. }
  1074. ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, const char *name)
  1075. {
  1076. int value, ret;
  1077. const FlagDef *p;
  1078. const char *r;
  1079. value = 1;
  1080. r = name;
  1081. if (no_flag(&r))
  1082. value = 0;
  1083. for (ret = -1, p = flags; p->name; ++p) {
  1084. if (ret) {
  1085. if (strcmp(r, p->name))
  1086. continue;
  1087. } else {
  1088. if (0 == (p->flags & WD_ALL))
  1089. continue;
  1090. }
  1091. if (p->offset) {
  1092. *(int*)((char *)s + p->offset) =
  1093. p->flags & FD_INVERT ? !value : value;
  1094. if (ret)
  1095. return 0;
  1096. } else {
  1097. ret = 0;
  1098. }
  1099. }
  1100. return ret;
  1101. }
  1102. static int strstart(const char *val, const char **str)
  1103. {
  1104. const char *p, *q;
  1105. p = *str;
  1106. q = val;
  1107. while (*q) {
  1108. if (*p != *q)
  1109. return 0;
  1110. p++;
  1111. q++;
  1112. }
  1113. *str = p;
  1114. return 1;
  1115. }
  1116. /* Like strstart, but automatically takes into account that ld options can
  1117. *
  1118. * - start with double or single dash (e.g. '--soname' or '-soname')
  1119. * - arguments can be given as separate or after '=' (e.g. '-Wl,-soname,x.so'
  1120. * or '-Wl,-soname=x.so')
  1121. *
  1122. * you provide `val` always in 'option[=]' form (no leading -)
  1123. */
  1124. static int link_option(const char *str, const char *val, const char **ptr)
  1125. {
  1126. const char *p, *q;
  1127. int ret;
  1128. /* there should be 1 or 2 dashes */
  1129. if (*str++ != '-')
  1130. return 0;
  1131. if (*str == '-')
  1132. str++;
  1133. /* then str & val should match (potentially up to '=') */
  1134. p = str;
  1135. q = val;
  1136. ret = 1;
  1137. if (q[0] == '?') {
  1138. ++q;
  1139. if (no_flag(&p))
  1140. ret = -1;
  1141. }
  1142. while (*q != '\0' && *q != '=') {
  1143. if (*p != *q)
  1144. return 0;
  1145. p++;
  1146. q++;
  1147. }
  1148. /* '=' near eos means ',' or '=' is ok */
  1149. if (*q == '=') {
  1150. if (*p == 0)
  1151. *ptr = p;
  1152. if (*p != ',' && *p != '=')
  1153. return 0;
  1154. p++;
  1155. } else if (*p) {
  1156. return 0;
  1157. }
  1158. *ptr = p;
  1159. return ret;
  1160. }
  1161. static const char *skip_linker_arg(const char **str)
  1162. {
  1163. const char *s1 = *str;
  1164. const char *s2 = strchr(s1, ',');
  1165. *str = s2 ? s2++ : (s2 = s1 + strlen(s1));
  1166. return s2;
  1167. }
  1168. static void copy_linker_arg(char **pp, const char *s, int sep)
  1169. {
  1170. const char *q = s;
  1171. char *p = *pp;
  1172. int l = 0;
  1173. if (p && sep)
  1174. p[l = strlen(p)] = sep, ++l;
  1175. skip_linker_arg(&q);
  1176. pstrncpy(l + (*pp = tcc_realloc(p, q - s + l + 1)), s, q - s);
  1177. }
  1178. /* set linker options */
  1179. static int tcc_set_linker(TCCState *s, const char *option)
  1180. {
  1181. while (*option) {
  1182. const char *p = NULL;
  1183. char *end = NULL;
  1184. int ignoring = 0;
  1185. int ret;
  1186. if (link_option(option, "Bsymbolic", &p)) {
  1187. s->symbolic = 1;
  1188. } else if (link_option(option, "nostdlib", &p)) {
  1189. s->nostdlib = 1;
  1190. } else if (link_option(option, "fini=", &p)) {
  1191. copy_linker_arg(&s->fini_symbol, p, 0);
  1192. ignoring = 1;
  1193. } else if (link_option(option, "image-base=", &p)
  1194. || link_option(option, "Ttext=", &p)) {
  1195. s->text_addr = strtoull(p, &end, 16);
  1196. s->has_text_addr = 1;
  1197. } else if (link_option(option, "init=", &p)) {
  1198. copy_linker_arg(&s->init_symbol, p, 0);
  1199. ignoring = 1;
  1200. } else if (link_option(option, "oformat=", &p)) {
  1201. #if defined(TCC_TARGET_PE)
  1202. if (strstart("pe-", &p)) {
  1203. #elif PTR_SIZE == 8
  1204. if (strstart("elf64-", &p)) {
  1205. #else
  1206. if (strstart("elf32-", &p)) {
  1207. #endif
  1208. s->output_format = TCC_OUTPUT_FORMAT_ELF;
  1209. } else if (!strcmp(p, "binary")) {
  1210. s->output_format = TCC_OUTPUT_FORMAT_BINARY;
  1211. #ifdef TCC_TARGET_COFF
  1212. } else if (!strcmp(p, "coff")) {
  1213. s->output_format = TCC_OUTPUT_FORMAT_COFF;
  1214. #endif
  1215. } else
  1216. goto err;
  1217. } else if (link_option(option, "as-needed", &p)) {
  1218. ignoring = 1;
  1219. } else if (link_option(option, "O", &p)) {
  1220. ignoring = 1;
  1221. } else if (link_option(option, "export-all-symbols", &p)) {
  1222. s->rdynamic = 1;
  1223. } else if (link_option(option, "rpath=", &p)) {
  1224. copy_linker_arg(&s->rpath, p, ':');
  1225. } else if (link_option(option, "enable-new-dtags", &p)) {
  1226. s->enable_new_dtags = 1;
  1227. } else if (link_option(option, "section-alignment=", &p)) {
  1228. s->section_align = strtoul(p, &end, 16);
  1229. } else if (link_option(option, "soname=", &p)) {
  1230. copy_linker_arg(&s->soname, p, 0);
  1231. #ifdef TCC_TARGET_PE
  1232. } else if (link_option(option, "large-address-aware", &p)) {
  1233. s->pe_characteristics |= 0x20;
  1234. } else if (link_option(option, "file-alignment=", &p)) {
  1235. s->pe_file_align = strtoul(p, &end, 16);
  1236. } else if (link_option(option, "stack=", &p)) {
  1237. s->pe_stack_size = strtoul(p, &end, 10);
  1238. } else if (link_option(option, "subsystem=", &p)) {
  1239. #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
  1240. if (!strcmp(p, "native")) {
  1241. s->pe_subsystem = 1;
  1242. } else if (!strcmp(p, "console")) {
  1243. s->pe_subsystem = 3;
  1244. } else if (!strcmp(p, "gui") || !strcmp(p, "windows")) {
  1245. s->pe_subsystem = 2;
  1246. } else if (!strcmp(p, "posix")) {
  1247. s->pe_subsystem = 7;
  1248. } else if (!strcmp(p, "efiapp")) {
  1249. s->pe_subsystem = 10;
  1250. } else if (!strcmp(p, "efiboot")) {
  1251. s->pe_subsystem = 11;
  1252. } else if (!strcmp(p, "efiruntime")) {
  1253. s->pe_subsystem = 12;
  1254. } else if (!strcmp(p, "efirom")) {
  1255. s->pe_subsystem = 13;
  1256. #elif defined(TCC_TARGET_ARM)
  1257. if (!strcmp(p, "wince")) {
  1258. s->pe_subsystem = 9;
  1259. #endif
  1260. } else
  1261. goto err;
  1262. #endif
  1263. } else if (ret = link_option(option, "?whole-archive", &p), ret) {
  1264. s->alacarte_link = ret < 0;
  1265. } else if (p) {
  1266. return 0;
  1267. } else {
  1268. err:
  1269. tcc_error("unsupported linker option '%s'", option);
  1270. }
  1271. if (ignoring && s->warn_unsupported)
  1272. tcc_warning("unsupported linker option '%s'", option);
  1273. option = skip_linker_arg(&p);
  1274. }
  1275. return 1;
  1276. }
  1277. typedef struct TCCOption {
  1278. const char *name;
  1279. uint16_t index;
  1280. uint16_t flags;
  1281. } TCCOption;
  1282. enum {
  1283. TCC_OPTION_HELP,
  1284. TCC_OPTION_HELP2,
  1285. TCC_OPTION_v,
  1286. TCC_OPTION_I,
  1287. TCC_OPTION_D,
  1288. TCC_OPTION_U,
  1289. TCC_OPTION_P,
  1290. TCC_OPTION_L,
  1291. TCC_OPTION_B,
  1292. TCC_OPTION_l,
  1293. TCC_OPTION_bench,
  1294. TCC_OPTION_bt,
  1295. TCC_OPTION_b,
  1296. TCC_OPTION_g,
  1297. TCC_OPTION_c,
  1298. TCC_OPTION_dumpversion,
  1299. TCC_OPTION_d,
  1300. TCC_OPTION_static,
  1301. TCC_OPTION_std,
  1302. TCC_OPTION_shared,
  1303. TCC_OPTION_soname,
  1304. TCC_OPTION_o,
  1305. TCC_OPTION_r,
  1306. TCC_OPTION_s,
  1307. TCC_OPTION_traditional,
  1308. TCC_OPTION_Wl,
  1309. TCC_OPTION_Wp,
  1310. TCC_OPTION_W,
  1311. TCC_OPTION_O,
  1312. TCC_OPTION_mfloat_abi,
  1313. TCC_OPTION_m,
  1314. TCC_OPTION_f,
  1315. TCC_OPTION_isystem,
  1316. TCC_OPTION_iwithprefix,
  1317. TCC_OPTION_include,
  1318. TCC_OPTION_nostdinc,
  1319. TCC_OPTION_nostdlib,
  1320. TCC_OPTION_print_search_dirs,
  1321. TCC_OPTION_rdynamic,
  1322. TCC_OPTION_param,
  1323. TCC_OPTION_pedantic,
  1324. TCC_OPTION_pthread,
  1325. TCC_OPTION_run,
  1326. TCC_OPTION_w,
  1327. TCC_OPTION_pipe,
  1328. TCC_OPTION_E,
  1329. TCC_OPTION_MD,
  1330. TCC_OPTION_MF,
  1331. TCC_OPTION_x,
  1332. TCC_OPTION_ar,
  1333. TCC_OPTION_impdef
  1334. };
  1335. #define TCC_OPTION_HAS_ARG 0x0001
  1336. #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
  1337. static const TCCOption tcc_options[] = {
  1338. { "h", TCC_OPTION_HELP, 0 },
  1339. { "-help", TCC_OPTION_HELP, 0 },
  1340. { "?", TCC_OPTION_HELP, 0 },
  1341. { "hh", TCC_OPTION_HELP2, 0 },
  1342. { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1343. { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
  1344. { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
  1345. { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
  1346. { "P", TCC_OPTION_P, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1347. { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
  1348. { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
  1349. { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1350. { "bench", TCC_OPTION_bench, 0 },
  1351. #ifdef CONFIG_TCC_BACKTRACE
  1352. { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
  1353. #endif
  1354. #ifdef CONFIG_TCC_BCHECK
  1355. { "b", TCC_OPTION_b, 0 },
  1356. #endif
  1357. { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1358. { "c", TCC_OPTION_c, 0 },
  1359. { "dumpversion", TCC_OPTION_dumpversion, 0},
  1360. { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1361. { "static", TCC_OPTION_static, 0 },
  1362. { "std", TCC_OPTION_std, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1363. { "shared", TCC_OPTION_shared, 0 },
  1364. { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
  1365. { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
  1366. { "-param", TCC_OPTION_param, TCC_OPTION_HAS_ARG },
  1367. { "pedantic", TCC_OPTION_pedantic, 0},
  1368. { "pthread", TCC_OPTION_pthread, 0},
  1369. { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1370. { "rdynamic", TCC_OPTION_rdynamic, 0 },
  1371. { "r", TCC_OPTION_r, 0 },
  1372. { "s", TCC_OPTION_s, 0 },
  1373. { "traditional", TCC_OPTION_traditional, 0 },
  1374. { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1375. { "Wp,", TCC_OPTION_Wp, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1376. { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1377. { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1378. #ifdef TCC_TARGET_ARM
  1379. { "mfloat-abi", TCC_OPTION_mfloat_abi, TCC_OPTION_HAS_ARG },
  1380. #endif
  1381. { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1382. { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
  1383. { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
  1384. { "include", TCC_OPTION_include, TCC_OPTION_HAS_ARG },
  1385. { "nostdinc", TCC_OPTION_nostdinc, 0 },
  1386. { "nostdlib", TCC_OPTION_nostdlib, 0 },
  1387. { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
  1388. { "w", TCC_OPTION_w, 0 },
  1389. { "pipe", TCC_OPTION_pipe, 0},
  1390. { "E", TCC_OPTION_E, 0},
  1391. { "MD", TCC_OPTION_MD, 0},
  1392. { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
  1393. { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
  1394. { "ar", TCC_OPTION_ar, 0},
  1395. #ifdef TCC_TARGET_PE
  1396. { "impdef", TCC_OPTION_impdef, 0},
  1397. #endif
  1398. { NULL, 0, 0 },
  1399. };
  1400. static const FlagDef options_W[] = {
  1401. { 0, 0, "all" },
  1402. { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
  1403. { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
  1404. { offsetof(TCCState, warn_error), 0, "error" },
  1405. { offsetof(TCCState, warn_gcc_compat), 0, "gcc-compat" },
  1406. { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
  1407. "implicit-function-declaration" },
  1408. { 0, 0, NULL }
  1409. };
  1410. static const FlagDef options_f[] = {
  1411. { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
  1412. { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
  1413. { offsetof(TCCState, nocommon), FD_INVERT, "common" },
  1414. { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
  1415. { offsetof(TCCState, ms_extensions), 0, "ms-extensions" },
  1416. { offsetof(TCCState, dollars_in_identifiers), 0, "dollars-in-identifiers" },
  1417. { 0, 0, NULL }
  1418. };
  1419. static const FlagDef options_m[] = {
  1420. { offsetof(TCCState, ms_bitfields), 0, "ms-bitfields" },
  1421. #ifdef TCC_TARGET_X86_64
  1422. { offsetof(TCCState, nosse), FD_INVERT, "sse" },
  1423. #endif
  1424. { 0, 0, NULL }
  1425. };
  1426. static void parse_option_D(TCCState *s1, const char *optarg)
  1427. {
  1428. char *sym = tcc_strdup(optarg);
  1429. char *value = strchr(sym, '=');
  1430. if (value)
  1431. *value++ = '\0';
  1432. tcc_define_symbol(s1, sym, value);
  1433. tcc_free(sym);
  1434. }
  1435. static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
  1436. {
  1437. struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
  1438. f->type = filetype;
  1439. f->alacarte = s->alacarte_link;
  1440. strcpy(f->name, filename);
  1441. dynarray_add(&s->files, &s->nb_files, f);
  1442. }
  1443. static int args_parser_make_argv(const char *r, int *argc, char ***argv)
  1444. {
  1445. int ret = 0, q, c;
  1446. CString str;
  1447. for(;;) {
  1448. while (c = (unsigned char)*r, c && c <= ' ')
  1449. ++r;
  1450. if (c == 0)
  1451. break;
  1452. q = 0;
  1453. cstr_new(&str);
  1454. while (c = (unsigned char)*r, c) {
  1455. ++r;
  1456. if (c == '\\' && (*r == '"' || *r == '\\')) {
  1457. c = *r++;
  1458. } else if (c == '"') {
  1459. q = !q;
  1460. continue;
  1461. } else if (q == 0 && c <= ' ') {
  1462. break;
  1463. }
  1464. cstr_ccat(&str, c);
  1465. }
  1466. cstr_ccat(&str, 0);
  1467. //printf("<%s>\n", str.data), fflush(stdout);
  1468. dynarray_add(argv, argc, tcc_strdup(str.data));
  1469. cstr_free(&str);
  1470. ++ret;
  1471. }
  1472. return ret;
  1473. }
  1474. /* read list file */
  1475. static void args_parser_listfile(TCCState *s,
  1476. const char *filename, int optind, int *pargc, char ***pargv)
  1477. {
  1478. int fd, i;
  1479. size_t len;
  1480. char *p;
  1481. int argc = 0;
  1482. char **argv = NULL;
  1483. fd = open(filename, O_RDONLY | O_BINARY);
  1484. if (fd < 0)
  1485. tcc_error("listfile '%s' not found", filename);
  1486. len = lseek(fd, 0, SEEK_END);
  1487. p = tcc_malloc(len + 1), p[len] = 0;
  1488. lseek(fd, 0, SEEK_SET), read(fd, p, len), close(fd);
  1489. for (i = 0; i < *pargc; ++i)
  1490. if (i == optind)
  1491. args_parser_make_argv(p, &argc, &argv);
  1492. else
  1493. dynarray_add(&argv, &argc, tcc_strdup((*pargv)[i]));
  1494. tcc_free(p);
  1495. dynarray_reset(&s->argv, &s->argc);
  1496. *pargc = s->argc = argc, *pargv = s->argv = argv;
  1497. }
  1498. PUB_FUNC int tcc_parse_args(TCCState *s, int *pargc, char ***pargv, int optind)
  1499. {
  1500. const TCCOption *popt;
  1501. const char *optarg, *r;
  1502. const char *run = NULL;
  1503. int last_o = -1;
  1504. int x;
  1505. CString linker_arg; /* collect -Wl options */
  1506. int tool = 0, arg_start = 0, noaction = optind;
  1507. char **argv = *pargv;
  1508. int argc = *pargc;
  1509. cstr_new(&linker_arg);
  1510. while (optind < argc) {
  1511. r = argv[optind];
  1512. if (r[0] == '@' && r[1] != '\0') {
  1513. args_parser_listfile(s, r + 1, optind, &argc, &argv);
  1514. continue;
  1515. }
  1516. optind++;
  1517. if (tool) {
  1518. if (r[0] == '-' && r[1] == 'v' && r[2] == 0)
  1519. ++s->verbose;
  1520. continue;
  1521. }
  1522. reparse:
  1523. if (r[0] != '-' || r[1] == '\0') {
  1524. if (r[0] != '@') /* allow "tcc file(s) -run @ args ..." */
  1525. args_parser_add_file(s, r, s->filetype);
  1526. if (run) {
  1527. tcc_set_options(s, run);
  1528. arg_start = optind - 1;
  1529. break;
  1530. }
  1531. continue;
  1532. }
  1533. /* find option in table */
  1534. for(popt = tcc_options; ; ++popt) {
  1535. const char *p1 = popt->name;
  1536. const char *r1 = r + 1;
  1537. if (p1 == NULL)
  1538. tcc_error("invalid option -- '%s'", r);
  1539. if (!strstart(p1, &r1))
  1540. continue;
  1541. optarg = r1;
  1542. if (popt->flags & TCC_OPTION_HAS_ARG) {
  1543. if (*r1 == '\0' && !(popt->flags & TCC_OPTION_NOSEP)) {
  1544. if (optind >= argc)
  1545. arg_err:
  1546. tcc_error("argument to '%s' is missing", r);
  1547. optarg = argv[optind++];
  1548. }
  1549. } else if (*r1 != '\0')
  1550. continue;
  1551. break;
  1552. }
  1553. switch(popt->index) {
  1554. case TCC_OPTION_HELP:
  1555. return OPT_HELP;
  1556. case TCC_OPTION_HELP2:
  1557. return OPT_HELP2;
  1558. case TCC_OPTION_I:
  1559. tcc_add_include_path(s, optarg);
  1560. break;
  1561. case TCC_OPTION_D:
  1562. parse_option_D(s, optarg);
  1563. break;
  1564. case TCC_OPTION_U:
  1565. tcc_undefine_symbol(s, optarg);
  1566. break;
  1567. case TCC_OPTION_L:
  1568. tcc_add_library_path(s, optarg);
  1569. break;
  1570. case TCC_OPTION_B:
  1571. /* set tcc utilities path (mainly for tcc development) */
  1572. tcc_set_lib_path(s, optarg);
  1573. break;
  1574. case TCC_OPTION_l:
  1575. args_parser_add_file(s, optarg, AFF_TYPE_LIB);
  1576. s->nb_libraries++;
  1577. break;
  1578. case TCC_OPTION_pthread:
  1579. parse_option_D(s, "_REENTRANT");
  1580. s->option_pthread = 1;
  1581. break;
  1582. case TCC_OPTION_bench:
  1583. s->do_bench = 1;
  1584. break;
  1585. #ifdef CONFIG_TCC_BACKTRACE
  1586. case TCC_OPTION_bt:
  1587. tcc_set_num_callers(atoi(optarg));
  1588. break;
  1589. #endif
  1590. #ifdef CONFIG_TCC_BCHECK
  1591. case TCC_OPTION_b:
  1592. s->do_bounds_check = 1;
  1593. s->do_debug = 1;
  1594. break;
  1595. #endif
  1596. case TCC_OPTION_g:
  1597. s->do_debug = 1;
  1598. break;
  1599. case TCC_OPTION_c:
  1600. x = TCC_OUTPUT_OBJ;
  1601. set_output_type:
  1602. if (s->output_type)
  1603. tcc_warning("-%s: overriding compiler action already specified", popt->name);
  1604. s->output_type = x;
  1605. break;
  1606. case TCC_OPTION_d:
  1607. if (*optarg == 'D')
  1608. s->dflag = 3;
  1609. else if (*optarg == 'M')
  1610. s->dflag = 7;
  1611. else if (*optarg == 't')
  1612. s->dflag = 16;
  1613. else if (isnum(*optarg))
  1614. g_debug = atoi(optarg);
  1615. else
  1616. goto unsupported_option;
  1617. break;
  1618. case TCC_OPTION_static:
  1619. s->static_link = 1;
  1620. break;
  1621. case TCC_OPTION_std:
  1622. /* silently ignore, a current purpose:
  1623. allow to use a tcc as a reference compiler for "make test" */
  1624. break;
  1625. case TCC_OPTION_shared:
  1626. x = TCC_OUTPUT_DLL;
  1627. goto set_output_type;
  1628. case TCC_OPTION_soname:
  1629. s->soname = tcc_strdup(optarg);
  1630. break;
  1631. case TCC_OPTION_o:
  1632. if (s->outfile) {
  1633. tcc_warning("multiple -o option");
  1634. tcc_free(s->outfile);
  1635. }
  1636. s->outfile = tcc_strdup(optarg);
  1637. break;
  1638. case TCC_OPTION_r:
  1639. /* generate a .o merging several output files */
  1640. s->option_r = 1;
  1641. x = TCC_OUTPUT_OBJ;
  1642. goto set_output_type;
  1643. case TCC_OPTION_isystem:
  1644. tcc_add_sysinclude_path(s, optarg);
  1645. break;
  1646. case TCC_OPTION_include:
  1647. dynarray_add(&s->cmd_include_files,
  1648. &s->nb_cmd_include_files, tcc_strdup(optarg));
  1649. break;
  1650. case TCC_OPTION_nostdinc:
  1651. s->nostdinc = 1;
  1652. break;
  1653. case TCC_OPTION_nostdlib:
  1654. s->nostdlib = 1;
  1655. break;
  1656. case TCC_OPTION_run:
  1657. #ifndef TCC_IS_NATIVE
  1658. tcc_error("-run is not available in a cross compiler");
  1659. #endif
  1660. run = optarg;
  1661. x = TCC_OUTPUT_MEMORY;
  1662. goto set_output_type;
  1663. case TCC_OPTION_v:
  1664. do ++s->verbose; while (*optarg++ == 'v');
  1665. ++noaction;
  1666. break;
  1667. case TCC_OPTION_f:
  1668. if (set_flag(s, options_f, optarg) < 0)
  1669. goto unsupported_option;
  1670. break;
  1671. #ifdef TCC_TARGET_ARM
  1672. case TCC_OPTION_mfloat_abi:
  1673. /* tcc doesn't support soft float yet */
  1674. if (!strcmp(optarg, "softfp")) {
  1675. s->float_abi = ARM_SOFTFP_FLOAT;
  1676. tcc_undefine_symbol(s, "__ARM_PCS_VFP");
  1677. } else if (!strcmp(optarg, "hard"))
  1678. s->float_abi = ARM_HARD_FLOAT;
  1679. else
  1680. tcc_error("unsupported float abi '%s'", optarg);
  1681. break;
  1682. #endif
  1683. case TCC_OPTION_m:
  1684. if (set_flag(s, options_m, optarg) < 0) {
  1685. if (x = atoi(optarg), x != 32 && x != 64)
  1686. goto unsupported_option;
  1687. if (PTR_SIZE != x/8)
  1688. return x;
  1689. ++noaction;
  1690. }
  1691. break;
  1692. case TCC_OPTION_W:
  1693. if (set_flag(s, options_W, optarg) < 0)
  1694. goto unsupported_option;
  1695. break;
  1696. case TCC_OPTION_w:
  1697. s->warn_none = 1;
  1698. break;
  1699. case TCC_OPTION_rdynamic:
  1700. s->rdynamic = 1;
  1701. break;
  1702. case TCC_OPTION_Wl:
  1703. if (linker_arg.size)
  1704. --linker_arg.size, cstr_ccat(&linker_arg, ',');
  1705. cstr_cat(&linker_arg, optarg, 0);
  1706. if (tcc_set_linker(s, linker_arg.data))
  1707. cstr_free(&linker_arg);
  1708. break;
  1709. case TCC_OPTION_Wp:
  1710. r = optarg;
  1711. goto reparse;
  1712. case TCC_OPTION_E:
  1713. x = TCC_OUTPUT_PREPROCESS;
  1714. goto set_output_type;
  1715. case TCC_OPTION_P:
  1716. s->Pflag = atoi(optarg) + 1;
  1717. break;
  1718. case TCC_OPTION_MD:
  1719. s->gen_deps = 1;
  1720. break;
  1721. case TCC_OPTION_MF:
  1722. s->deps_outfile = tcc_strdup(optarg);
  1723. break;
  1724. case TCC_OPTION_dumpversion:
  1725. printf ("%s\n", TCC_VERSION);
  1726. exit(0);
  1727. break;
  1728. case TCC_OPTION_x:
  1729. if (*optarg == 'c')
  1730. s->filetype = AFF_TYPE_C;
  1731. else if (*optarg == 'a')
  1732. s->filetype = AFF_TYPE_ASMPP;
  1733. else if (*optarg == 'n')
  1734. s->filetype = AFF_TYPE_NONE;
  1735. else
  1736. tcc_warning("unsupported language '%s'", optarg);
  1737. break;
  1738. case TCC_OPTION_O:
  1739. last_o = atoi(optarg);
  1740. break;
  1741. case TCC_OPTION_print_search_dirs:
  1742. x = OPT_PRINT_DIRS;
  1743. goto extra_action;
  1744. case TCC_OPTION_impdef:
  1745. x = OPT_IMPDEF;
  1746. goto extra_action;
  1747. case TCC_OPTION_ar:
  1748. x = OPT_AR;
  1749. extra_action:
  1750. arg_start = optind - 1;
  1751. if (arg_start != noaction)
  1752. tcc_error("cannot parse %s here", r);
  1753. tool = x;
  1754. break;
  1755. case TCC_OPTION_traditional:
  1756. case TCC_OPTION_pedantic:
  1757. case TCC_OPTION_pipe:
  1758. case TCC_OPTION_s:
  1759. /* ignored */
  1760. break;
  1761. default:
  1762. unsupported_option:
  1763. if (s->warn_unsupported)
  1764. tcc_warning("unsupported option '%s'", r);
  1765. break;
  1766. }
  1767. }
  1768. if (last_o > 0)
  1769. tcc_define_symbol(s, "__OPTIMIZE__", NULL);
  1770. if (linker_arg.size) {
  1771. r = linker_arg.data;
  1772. goto arg_err;
  1773. }
  1774. *pargc = argc - arg_start;
  1775. *pargv = argv + arg_start;
  1776. if (tool)
  1777. return tool;
  1778. if (optind != noaction)
  1779. return 0;
  1780. if (s->verbose == 2)
  1781. return OPT_PRINT_DIRS;
  1782. if (s->verbose)
  1783. return OPT_V;
  1784. return OPT_HELP;
  1785. }
  1786. LIBTCCAPI void tcc_set_options(TCCState *s, const char *r)
  1787. {
  1788. char **argv = NULL;
  1789. int argc = 0;
  1790. args_parser_make_argv(r, &argc, &argv);
  1791. tcc_parse_args(s, &argc, &argv, 0);
  1792. dynarray_reset(&argv, &argc);
  1793. }
  1794. PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time)
  1795. {
  1796. if (total_time < 1)
  1797. total_time = 1;
  1798. if (total_bytes < 1)
  1799. total_bytes = 1;
  1800. fprintf(stderr, "* %d idents, %d lines, %d bytes\n"
  1801. "* %0.3f s, %u lines/s, %0.1f MB/s\n",
  1802. tok_ident - TOK_IDENT, total_lines, total_bytes,
  1803. (double)total_time/1000,
  1804. (unsigned)total_lines*1000/total_time,
  1805. (double)total_bytes/1000/total_time);
  1806. #ifdef MEM_DEBUG
  1807. fprintf(stderr, "* %d bytes memory used\n", mem_max_size);
  1808. #endif
  1809. }