Skip to content

Commit

Permalink
Fix -Wl,-nostdlib behaviour for compatibility with other compilers
Browse files Browse the repository at this point in the history
TCC treats -nostdlib and -Wl,-nostdlib as equivalent, but on other compilers
which call a discrete linker, -nostdlib behaves as on tcc (not adding
startfiles/libc/endfiles) by modifying the ld command line, but -Wl,-nostdlib
adds -nostdlib to the ld cmdline, which stops the linker searching the default
directories for libraries.
  • Loading branch information
el-remph committed Feb 16, 2025
1 parent ec60d28 commit 1747b45
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
5 changes: 3 additions & 2 deletions libtcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,8 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
return 0;
}

tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);
if (!s->nostdlib_paths)
tcc_add_library_path(s, CONFIG_TCC_LIBPATHS);

#ifdef TCC_TARGET_PE
# ifdef TCC_IS_NATIVE
Expand Down Expand Up @@ -1383,7 +1384,7 @@ static int tcc_set_linker(TCCState *s, const char *option)
if (link_option(option, "Bsymbolic", &p)) {
s->symbolic = 1;
} else if (link_option(option, "nostdlib", &p)) {
s->nostdlib = 1;
s->nostdlib_paths = 1;
} else if (link_option(option, "e=", &p)
|| link_option(option, "entry=", &p)) {
copy_linker_arg(&s->elf_entryname, p, 0);
Expand Down
11 changes: 8 additions & 3 deletions tcc-doc.texi
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,6 @@ Link your program with dynamic library libxxx.so or static library
libxxx.a. The library is searched in the paths specified by the
@option{-L} option and @env{LIBRARY_PATH} variable.

@item -nostdlib
Don't implicitly link with libc, the C runtime files, and libtcc1.

@item -Bdir
Set the path where the tcc internal libraries (and include files) can be
found (default is @file{PREFIX/lib/tcc}).
Expand All @@ -357,6 +354,14 @@ opened with @code{dlopen()} needs to access executable symbols.
@item -r
Generate an object file combining all input files.

@item -nostdlib
Don't implicitly link with libc, the C runtime files, and libtcc1.

@item -Wl,-nostdlib
Don't search the default paths for libraries (@file{/usr/local/lib},
@file{/usr/lib} and @file{/lib}). Only the paths specified with @option{-L}
and @env{LIBRARY_PATH} are searched.

@item -Wl,-rpath=path
Put custom search path for dynamic libraries into executable.

Expand Down
3 changes: 2 additions & 1 deletion tcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ static const char help2[] =
" -On same as -D__OPTIMIZE__ for n > 0\n"
" -Wp,-opt same as -opt\n"
" -include file include 'file' above each input file\n"
" -nostdlib do not link with standard crt/libs\n"
" -isystem dir add 'dir' to system include path\n"
" -static link to static libraries (not recommended)\n"
" -dumpversion print version\n"
Expand Down Expand Up @@ -132,7 +133,7 @@ static const char help2[] =
" no-sse disable floats on x86_64\n"
#endif
"-Wl,... linker options:\n"
" -nostdlib do not link with standard crt/libs\n"
" -nostdlib do not search standard library paths\n"
" -[no-]whole-archive load lib(s) fully/only as needed\n"
" -export-all-symbols same as -rdynamic\n"
" -export-dynamic same as -rdynamic\n"
Expand Down
1 change: 1 addition & 0 deletions tcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ struct TCCState {
unsigned char verbose; /* if true, display some information during compilation */
unsigned char nostdinc; /* if true, no standard headers are added */
unsigned char nostdlib; /* if true, no standard libraries are added */
unsigned char nostdlib_paths; /* if true, the default paths are not searched for libraries */
unsigned char nocommon; /* if true, do not use common symbols for .bss data */
unsigned char static_link; /* if true, static linking is performed */
unsigned char rdynamic; /* if true, all symbols are exported */
Expand Down

0 comments on commit 1747b45

Please sign in to comment.