tccelf.c: write section headers before sections
[tinycc.git] / tcc.c
blob838f41d91163b6f3fa9b0817ec34b230e27a7bb1
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
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.
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.
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
21 #include "tcc.h"
22 #if ONE_SOURCE
23 # include "libtcc.c"
24 #endif
25 #include "tcctools.c"
27 static const char help[] =
28 "Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
29 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
30 " tcc [options...] -run infile (or --) [arguments...]\n"
31 "General options:\n"
32 " -c compile only - generate an object file\n"
33 " -o outfile set output filename\n"
34 " -run run compiled source\n"
35 " -fflag set or reset (with 'no-' prefix) 'flag' (see tcc -hh)\n"
36 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see tcc -hh)\n"
37 " -w disable all warnings\n"
38 " -v --version show version\n"
39 " -vv show search paths or loaded files\n"
40 " -h -hh show this, show more help\n"
41 " -bench show compilation statistics\n"
42 " - use stdin pipe as infile\n"
43 " @listfile read arguments from listfile\n"
44 "Preprocessor options:\n"
45 " -Idir add include path 'dir'\n"
46 " -Dsym[=val] define 'sym' with value 'val'\n"
47 " -Usym undefine 'sym'\n"
48 " -E preprocess only\n"
49 "Linker options:\n"
50 " -Ldir add library path 'dir'\n"
51 " -llib link with dynamic or static library 'lib'\n"
52 " -r generate (relocatable) object file\n"
53 " -shared generate a shared library/dll\n"
54 " -rdynamic export all global symbols to dynamic linker\n"
55 " -soname set name for shared library to be used at runtime\n"
56 " -Wl,-opt[=val] set linker option (see tcc -hh)\n"
57 "Debugger options:\n"
58 " -g generate stab runtime debug info\n"
59 " -gdwarf[-x] generate dwarf runtime debug info\n"
60 #ifdef TCC_TARGET_PE
61 " -g.pdb create .pdb debug database\n"
62 #endif
63 #ifdef CONFIG_TCC_BCHECK
64 " -b compile with built-in memory and bounds checker (implies -g)\n"
65 #endif
66 #ifdef CONFIG_TCC_BACKTRACE
67 " -bt[N] link with backtrace (stack dump) support [show max N callers]\n"
68 #endif
69 "Misc. options:\n"
70 " -std=version define __STDC_VERSION__ according to version (c11/gnu11)\n"
71 " -x[c|a|b|n] specify type of the next infile (C,ASM,BIN,NONE)\n"
72 " -nostdinc do not use standard system include paths\n"
73 " -nostdlib do not link with standard crt and libraries\n"
74 " -Bdir set tcc's private include/library dir\n"
75 " -M[M]D generate make dependency file [ignore system files]\n"
76 " -M[M] as above but no other output\n"
77 " -MF file specify dependency file name\n"
78 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
79 " -m32/64 defer to i386/x86_64 cross compiler\n"
80 #endif
81 "Tools:\n"
82 " create library : tcc -ar [crstvx] lib [files]\n"
83 #ifdef TCC_TARGET_PE
84 " create def file : tcc -impdef lib.dll [-v] [-o lib.def]\n"
85 #endif
88 static const char help2[] =
89 "Tiny C Compiler "TCC_VERSION" - More Options\n"
90 "Special options:\n"
91 " -P -P1 with -E: no/alternative #line output\n"
92 " -dD -dM with -E: output #define directives\n"
93 " -pthread same as -D_REENTRANT and -lpthread\n"
94 " -On same as -D__OPTIMIZE__ for n > 0\n"
95 " -Wp,-opt same as -opt\n"
96 " -include file include 'file' above each input file\n"
97 " -isystem dir add 'dir' to system include path\n"
98 " -static link to static libraries (not recommended)\n"
99 " -dumpversion print version\n"
100 " -print-search-dirs print search paths\n"
101 " -dt with -run/-E: auto-define 'test_...' macros\n"
102 "Ignored options:\n"
103 " -arch -C --param -pedantic -pipe -s -traditional\n"
104 "-W[no-]... warnings:\n"
105 " all turn on some (*) warnings\n"
106 " error[=warning] stop after warning (any or specified)\n"
107 " write-strings strings are const\n"
108 " unsupported warn about ignored options, pragmas, etc.\n"
109 " implicit-function-declaration warn for missing prototype (*)\n"
110 " discarded-qualifiers warn when const is dropped (*)\n"
111 "-f[no-]... flags:\n"
112 " unsigned-char default char is unsigned\n"
113 " signed-char default char is signed\n"
114 " common use common section instead of bss\n"
115 " leading-underscore decorate extern symbols\n"
116 " ms-extensions allow anonymous struct in struct\n"
117 " dollars-in-identifiers allow '$' in C symbols\n"
118 " test-coverage create code coverage code\n"
119 "-m... target specific options:\n"
120 " ms-bitfields use MSVC bitfield layout\n"
121 #ifdef TCC_TARGET_ARM
122 " float-abi hard/softfp on arm\n"
123 #endif
124 #ifdef TCC_TARGET_X86_64
125 " no-sse disable floats on x86_64\n"
126 #endif
127 "-Wl,... linker options:\n"
128 " -nostdlib do not link with standard crt/libs\n"
129 " -[no-]whole-archive load lib(s) fully/only as needed\n"
130 " -export-all-symbols same as -rdynamic\n"
131 " -export-dynamic same as -rdynamic\n"
132 " -image-base= -Ttext= set base address of executable\n"
133 " -section-alignment= set section alignment in executable\n"
134 #ifdef TCC_TARGET_PE
135 " -file-alignment= set PE file alignment\n"
136 " -stack= set PE stack reserve\n"
137 " -large-address-aware set related PE option\n"
138 " -subsystem=[console/windows] set PE subsystem\n"
139 " -oformat=[pe-* binary] set executable output format\n"
140 "Predefined macros:\n"
141 " tcc -E -dM - < nul\n"
142 #else
143 " -rpath= set dynamic library search path\n"
144 " -enable-new-dtags set DT_RUNPATH instead of DT_RPATH\n"
145 " -soname= set DT_SONAME elf tag\n"
146 #if defined(TCC_TARGET_MACHO)
147 " -install_name= set DT_SONAME elf tag (soname macOS alias)\n"
148 #endif
149 " -Bsymbolic set DT_SYMBOLIC elf tag\n"
150 " -oformat=[elf32/64-* binary] set executable output format\n"
151 " -init= -fini= -Map= -as-needed -O (ignored)\n"
152 "Predefined macros:\n"
153 " tcc -E -dM - < /dev/null\n"
154 #endif
155 "See also the manual for more details.\n"
158 static const char version[] =
159 "tcc version "TCC_VERSION
160 #ifdef TCC_GITHASH
161 " "TCC_GITHASH
162 #endif
163 " ("
164 #ifdef TCC_TARGET_I386
165 "i386"
166 #elif defined TCC_TARGET_X86_64
167 "x86_64"
168 #elif defined TCC_TARGET_C67
169 "C67"
170 #elif defined TCC_TARGET_ARM
171 "ARM"
172 # ifdef TCC_ARM_EABI
173 " eabi"
174 # ifdef TCC_ARM_HARDFLOAT
175 "hf"
176 # endif
177 # endif
178 #elif defined TCC_TARGET_ARM64
179 "AArch64"
180 #elif defined TCC_TARGET_RISCV64
181 "riscv64"
182 #endif
183 #ifdef TCC_TARGET_PE
184 " Windows"
185 #elif defined(TCC_TARGET_MACHO)
186 " Darwin"
187 #elif TARGETOS_FreeBSD || TARGETOS_FreeBSD_kernel
188 " FreeBSD"
189 #elif TARGETOS_OpenBSD
190 " OpenBSD"
191 #elif TARGETOS_NetBSD
192 " NetBSD"
193 #else
194 " Linux"
195 #endif
196 ")\n"
199 static void print_dirs(const char *msg, char **paths, int nb_paths)
201 int i;
202 printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
203 for(i = 0; i < nb_paths; i++)
204 printf(" %s\n", paths[i]);
207 static void print_search_dirs(TCCState *s)
209 printf("install: %s\n", s->tcc_lib_path);
210 /* print_dirs("programs", NULL, 0); */
211 print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
212 print_dirs("libraries", s->library_paths, s->nb_library_paths);
213 printf("libtcc1:\n %s/%s\n", s->library_paths[0], CONFIG_TCC_CROSSPREFIX TCC_LIBTCC1);
214 #if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
215 print_dirs("crt", s->crt_paths, s->nb_crt_paths);
216 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
217 #endif
220 static void set_environment(TCCState *s)
222 char * path;
224 path = getenv("C_INCLUDE_PATH");
225 if(path != NULL) {
226 tcc_add_sysinclude_path(s, path);
228 path = getenv("CPATH");
229 if(path != NULL) {
230 tcc_add_include_path(s, path);
232 path = getenv("LIBRARY_PATH");
233 if(path != NULL) {
234 tcc_add_library_path(s, path);
238 static char *default_outputfile(TCCState *s, const char *first_file)
240 char buf[1024];
241 char *ext;
242 const char *name = "a";
244 if (first_file && strcmp(first_file, "-"))
245 name = tcc_basename(first_file);
246 snprintf(buf, sizeof(buf), "%s", name);
247 ext = tcc_fileextension(buf);
248 #ifdef TCC_TARGET_PE
249 if (s->output_type == TCC_OUTPUT_DLL)
250 strcpy(ext, ".dll");
251 else
252 if (s->output_type == TCC_OUTPUT_EXE)
253 strcpy(ext, ".exe");
254 else
255 #endif
256 if ((s->just_deps || s->output_type == TCC_OUTPUT_OBJ) && !s->option_r && *ext)
257 strcpy(ext, ".o");
258 else
259 strcpy(buf, "a.out");
260 return tcc_strdup(buf);
263 static unsigned getclock_ms(void)
265 #ifdef _WIN32
266 return GetTickCount();
267 #else
268 struct timeval tv;
269 gettimeofday(&tv, NULL);
270 return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
271 #endif
274 int main(int argc0, char **argv0)
276 TCCState *s, *s1;
277 int ret, opt, n = 0, t = 0, done;
278 unsigned start_time = 0, end_time = 0;
279 const char *first_file;
280 int argc; char **argv;
281 FILE *ppfp = stdout;
283 redo:
284 argc = argc0, argv = argv0;
285 s = s1 = tcc_new();
286 #ifdef CONFIG_TCC_SWITCHES /* predefined options */
287 tcc_set_options(s, CONFIG_TCC_SWITCHES);
288 #endif
289 opt = tcc_parse_args(s, &argc, &argv, 1);
290 if (opt < 0)
291 return 1;
293 if (n == 0) {
294 if (opt == OPT_HELP) {
295 fputs(help, stdout);
296 if (!s->verbose)
297 return 0;
298 ++opt;
300 if (opt == OPT_HELP2) {
301 fputs(help2, stdout);
302 return 0;
304 if (opt == OPT_M32 || opt == OPT_M64)
305 return tcc_tool_cross(s, argv, opt);
306 if (s->verbose)
307 printf("%s", version);
308 if (opt == OPT_AR)
309 return tcc_tool_ar(s, argc, argv);
310 #ifdef TCC_TARGET_PE
311 if (opt == OPT_IMPDEF)
312 return tcc_tool_impdef(s, argc, argv);
313 #endif
314 if (opt == OPT_V)
315 return 0;
316 if (opt == OPT_PRINT_DIRS) {
317 /* initialize search dirs */
318 set_environment(s);
319 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
320 print_search_dirs(s);
321 return 0;
324 if (s->nb_files == 0) {
325 tcc_error_noabort("no input files");
326 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
327 if (s->outfile && 0!=strcmp("-",s->outfile)) {
328 ppfp = fopen(s->outfile, "wb");
329 if (!ppfp)
330 tcc_error_noabort("could not write '%s'", s->outfile);
332 } else if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
333 if (s->nb_libraries)
334 tcc_error_noabort("cannot specify libraries with -c");
335 else if (s->nb_files > 1 && s->outfile)
336 tcc_error_noabort("cannot specify output file with -c many files");
338 if (s->nb_errors)
339 return 1;
340 if (s->do_bench)
341 start_time = getclock_ms();
344 set_environment(s);
345 if (s->output_type == 0)
346 s->output_type = TCC_OUTPUT_EXE;
347 tcc_set_output_type(s, s->output_type);
348 s->ppfp = ppfp;
350 if ((s->output_type == TCC_OUTPUT_MEMORY
351 || s->output_type == TCC_OUTPUT_PREPROCESS)
352 && (s->dflag & 16)) { /* -dt option */
353 if (t)
354 s->dflag |= 32;
355 s->run_test = ++t;
356 if (n)
357 --n;
360 /* compile or add each files or library */
361 first_file = NULL;
362 do {
363 struct filespec *f = s->files[n];
364 s->filetype = f->type;
365 if (f->type & AFF_TYPE_LIB) {
366 ret = tcc_add_library_err(s, f->name);
367 } else {
368 if (1 == s->verbose)
369 printf("-> %s\n", f->name);
370 if (!first_file)
371 first_file = f->name;
372 ret = tcc_add_file(s, f->name);
374 done = ret || ++n >= s->nb_files;
375 } while (!done && (s->output_type != TCC_OUTPUT_OBJ || s->option_r));
377 if (s->do_bench)
378 end_time = getclock_ms();
380 if (s->run_test) {
381 t = 0;
382 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
384 } else if (0 == ret) {
385 if (s->output_type == TCC_OUTPUT_MEMORY) {
386 #ifdef TCC_IS_NATIVE
387 ret = tcc_run(s, argc, argv);
388 #endif
389 } else {
390 if (!s->outfile)
391 s->outfile = default_outputfile(s, first_file);
392 if (!s->just_deps && tcc_output_file(s, s->outfile))
394 else if (s->gen_deps)
395 gen_makedeps(s, s->outfile, s->deps_outfile);
399 done = 1;
400 if (t)
401 done = 0; /* run more tests with -dt -run */
402 else if (s->nb_errors)
403 ret = 1;
404 else if (n < s->nb_files)
405 done = 0; /* compile more files with -c */
406 else if (s->do_bench)
407 tcc_print_stats(s, end_time - start_time);
408 tcc_delete(s);
409 if (!done)
410 goto redo;
411 if (ppfp && ppfp != stdout)
412 fclose(ppfp);
413 return ret;