Skip to content

Commit f10ab13

Browse files
author
grischka
committed
linker options & tcc_load_ldscript() reworked
cleanup libtcc.c:tcc_set_linker() cleanup tccelf.c:tcc_load_ldscript() Also - tccrun.c, tccelf.c:relocate_syms(): with tcc -run -nostdlib, do resolve but only from explicitly on the command-line given libraries. - tccgen.c: optimize UMOD x % c -> x & (c-1) for c = 2^n - tcc-doc.texi: cleanup - tcc.h, tccpp.c, libtcc.c: add 'size' arg to pstrncpy() Also reorder functions in libtcc.c a bit. 9 files changed, 556 insertions(+), 617 deletions(-)
1 parent b338126 commit f10ab13

File tree

9 files changed

+556
-617
lines changed

9 files changed

+556
-617
lines changed

libtcc.c

+418-465
Large diffs are not rendered by default.

tcc-doc.texi

+12-10
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,6 @@ Show included files. As sole argument, print search dirs. -vvv shows tries too
185185
@item -bench
186186
Display compilation statistics.
187187

188-
@item -pthread
189-
Preprocess with @option{-D_REENTRANT}, link with @option{-lpthread}.
190-
191-
@item -On
192-
Pretend to optimise: set @option{-D__OPTIMIZE__} to the numeric value of n.
193-
Note that TCC performs no additional optimisation.
194-
195-
@item -dt
196-
With @option{-run}/@option{-E}: auto-define 'test_...' macros
197-
198188
@end table
199189

200190
Preprocessor options:
@@ -441,6 +431,12 @@ gnu11; @code{199901} otherwise.
441431
@item -x[c|a|b|n]
442432
Specify content of next input file: respectively C, assembly, binary, or none.
443433

434+
@item -O[n]
435+
Same as @option{-D__OPTIMIZE__} except for -O0.
436+
437+
@item -pthread
438+
Preprocess with @option{-D_REENTRANT}, link with @option{-lpthread}.
439+
444440
@item -M
445441
Just output makefile fragment with dependencies
446442

@@ -456,13 +452,19 @@ Like -MD except mention only user header files, not system header files.
456452
@item -MF depfile
457453
Use @file{depfile} as output for -MD.
458454

455+
@item -MP
456+
Mention all dependencies as targets too.
457+
459458
@item -print-search-dirs
460459
Print the configured installation directory and a list of library
461460
and include directories tcc will search.
462461

463462
@item -dumpversion
464463
Print version.
465464

465+
@item -dt
466+
With @option{-run}/@option{-E}: auto-define 'test_...' macros
467+
466468
@end table
467469

468470
Target specific options:

tcc.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static const char help2[] =
158158
#endif
159159
" -Bsymbolic set DT_SYMBOLIC elf tag\n"
160160
" -oformat=[elf32/64-* binary] set executable output format\n"
161-
" -init= -fini= -Map= -as-needed -O (ignored)\n"
161+
" -init= -fini= -Map= -as-needed -O -z= (ignored)\n"
162162
"Predefined macros:\n"
163163
" tcc -E -dM - < /dev/null\n"
164164
#endif
@@ -221,7 +221,7 @@ static void print_search_dirs(TCCState *s)
221221
print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
222222
print_dirs("libraries", s->library_paths, s->nb_library_paths);
223223
printf("libtcc1:\n %s/%s\n", s->library_paths[0], CONFIG_TCC_CROSSPREFIX TCC_LIBTCC1);
224-
#if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
224+
#ifdef TCC_TARGET_UNIX
225225
print_dirs("crt", s->crt_paths, s->nb_crt_paths);
226226
printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
227227
#endif
@@ -253,7 +253,9 @@ static char *default_outputfile(TCCState *s, const char *first_file)
253253

254254
if (first_file && strcmp(first_file, "-"))
255255
name = tcc_basename(first_file);
256-
snprintf(buf, sizeof(buf), "%s", name);
256+
if (strlen(name) + 4 >= sizeof buf)
257+
name = "a";
258+
strcpy(buf, name);
257259
ext = tcc_fileextension(buf);
258260
#ifdef TCC_TARGET_PE
259261
if (s->output_type == TCC_OUTPUT_DLL)
@@ -293,7 +295,7 @@ int main(int argc0, char **argv0)
293295
redo:
294296
argc = argc0, argv = argv0;
295297
s = s1 = tcc_new();
296-
opt = tcc_parse_args(s, &argc, &argv, 1);
298+
opt = tcc_parse_args(s, &argc, &argv);
297299
if (opt < 0)
298300
return 1;
299301

tcc.h

+12-11
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ extern long double strtold (const char *__nptr, char **__endptr);
217217

218218
#if defined TCC_TARGET_PE || defined TCC_TARGET_MACHO
219219
# define ELF_OBJ_ONLY /* create elf .o but native executables */
220+
#else
221+
# define TCC_TARGET_UNIX 1
220222
#endif
221223

222224
/* No ten-byte long doubles on window and macos except in
@@ -919,8 +921,6 @@ struct TCCState {
919921
/* debug state */
920922
struct _tccdbg *dState;
921923

922-
/* Is there a new undefined sym since last new_undef_sym() */
923-
int new_undef_sym;
924924
/* extra attributes (eg. GOT/PLT value) for symtab symbols */
925925
struct sym_attr *sym_attrs;
926926
int nb_sym_attrs;
@@ -988,7 +988,7 @@ struct TCCState {
988988
unsigned int total_output[4];
989989

990990
/* used by tcc_load_ldscript */
991-
int fd, cc;
991+
unsigned char *ld_p; /* text pointer */
992992

993993
/* for warnings/errors for object files */
994994
const char *current_filename;
@@ -1001,7 +1001,9 @@ struct TCCState {
10011001
char *deps_outfile; /* option -MF */
10021002
int argc;
10031003
char **argv;
1004-
CString linker_arg; /* collect -Wl options */
1004+
/* -Wl options */
1005+
char **link_argv;
1006+
int link_argc, link_optind;
10051007
};
10061008

10071009
struct filespec {
@@ -1195,7 +1197,7 @@ ST_DATA int g_debug;
11951197
/* public functions currently used by the tcc main function */
11961198
ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s);
11971199
ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s);
1198-
ST_FUNC char *pstrncpy(char *out, const char *in, size_t num);
1200+
ST_FUNC char *pstrncpy(char *out, size_t buf_size, const char *s, size_t num);
11991201
PUB_FUNC char *tcc_basename(const char *name);
12001202
PUB_FUNC char *tcc_fileextension (const char *name);
12011203

@@ -1265,7 +1267,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
12651267
#define AFF_TYPE_ASM 2
12661268
#define AFF_TYPE_ASMPP 4
12671269
#define AFF_TYPE_LIB 8
1268-
#define AFF_TYPE_MASK (15 | AFF_TYPE_BIN)
1270+
#define AFF_TYPE_MASK (7 | AFF_TYPE_BIN)
12691271
/* values from tcc_object_type(...) */
12701272
#define AFF_BINTYPE_REL 1
12711273
#define AFF_BINTYPE_DYN 2
@@ -1274,6 +1276,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
12741276

12751277
/* return value of tcc_add_file_internal(): 0, -1, or FILE_NOT_FOUND */
12761278
#define FILE_NOT_FOUND -2
1279+
#define FILE_NOT_RECOGNIZED -3 /* unrecognized file type */
12771280

12781281
#ifndef ELF_OBJ_ONLY
12791282
ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
@@ -1289,7 +1292,7 @@ ST_FUNC void tcc_add_btstub(TCCState *s1);
12891292
ST_FUNC void tcc_add_pragma_libs(TCCState *s1);
12901293
PUB_FUNC int tcc_add_library_err(TCCState *s, const char *f);
12911294
PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time);
1292-
PUB_FUNC int tcc_parse_args(TCCState *s, int *argc, char ***argv, int optind);
1295+
PUB_FUNC int tcc_parse_args(TCCState *s, int *argc, char ***argv);
12931296
#ifdef _WIN32
12941297
ST_FUNC char *normalize_slashes(char *path);
12951298
#endif
@@ -1571,7 +1574,7 @@ ST_FUNC void tcc_add_runtime(TCCState *s1);
15711574

15721575
/* ------------ xxx-link.c ------------ */
15731576

1574-
#if !defined ELF_OBJ_ONLY || defined TCC_TARGET_MACHO
1577+
#ifndef TCC_TARGET_PE
15751578
ST_FUNC int code_reloc (int reloc_type);
15761579
ST_FUNC int gotplt_entry_type (int reloc_type);
15771580
/* Whether to generate a GOT/PLT entry and when. NO_GOTPLT_ENTRY is first so
@@ -1583,13 +1586,11 @@ enum gotplt_entry {
15831586
ALWAYS_GOTPLT_ENTRY /* always generate (eg. PLTOFF relocs) */
15841587
};
15851588
#define NEED_RELOC_TYPE
1586-
15871589
#if !defined TCC_TARGET_MACHO || defined TCC_IS_NATIVE
15881590
ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr);
15891591
ST_FUNC void relocate_plt(TCCState *s1);
15901592
ST_FUNC void build_got_entries(TCCState *s1, int got_sym); /* in tccelf.c */
15911593
#define NEED_BUILD_GOT
1592-
15931594
#endif
15941595
#endif
15951596

@@ -1809,7 +1810,7 @@ ST_FUNC int macho_load_dll(TCCState *s1, int fd, const char *filename, int lev);
18091810
ST_FUNC int macho_load_tbd(TCCState *s1, int fd, const char *filename, int lev);
18101811
#ifdef TCC_IS_NATIVE
18111812
ST_FUNC void tcc_add_macos_sdkpath(TCCState* s);
1812-
ST_FUNC const char* macho_tbd_soname(const char* filename);
1813+
ST_FUNC char* macho_tbd_soname(int fd);
18131814
#endif
18141815
#endif
18151816
/* ------------ tccrun.c ----------------- */

0 commit comments

Comments
 (0)