Fix -Wl,-nostdlib behaviour for compatibility with other compilers

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.
This commit is contained in:
remph 2025-02-07 10:54:13 +00:00
parent ec60d28e0f
commit 1747b45649
4 changed files with 14 additions and 6 deletions

View File

@ -924,7 +924,8 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
return 0; 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_TARGET_PE
# ifdef TCC_IS_NATIVE # ifdef TCC_IS_NATIVE
@ -1383,7 +1384,7 @@ static int tcc_set_linker(TCCState *s, const char *option)
if (link_option(option, "Bsymbolic", &p)) { if (link_option(option, "Bsymbolic", &p)) {
s->symbolic = 1; s->symbolic = 1;
} else if (link_option(option, "nostdlib", &p)) { } else if (link_option(option, "nostdlib", &p)) {
s->nostdlib = 1; s->nostdlib_paths = 1;
} else if (link_option(option, "e=", &p) } else if (link_option(option, "e=", &p)
|| link_option(option, "entry=", &p)) { || link_option(option, "entry=", &p)) {
copy_linker_arg(&s->elf_entryname, p, 0); copy_linker_arg(&s->elf_entryname, p, 0);

View File

@ -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 libxxx.a. The library is searched in the paths specified by the
@option{-L} option and @env{LIBRARY_PATH} variable. @option{-L} option and @env{LIBRARY_PATH} variable.
@item -nostdlib
Don't implicitly link with libc, the C runtime files, and libtcc1.
@item -Bdir @item -Bdir
Set the path where the tcc internal libraries (and include files) can be Set the path where the tcc internal libraries (and include files) can be
found (default is @file{PREFIX/lib/tcc}). found (default is @file{PREFIX/lib/tcc}).
@ -357,6 +354,14 @@ opened with @code{dlopen()} needs to access executable symbols.
@item -r @item -r
Generate an object file combining all input files. 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 @item -Wl,-rpath=path
Put custom search path for dynamic libraries into executable. Put custom search path for dynamic libraries into executable.

3
tcc.c
View File

@ -98,6 +98,7 @@ static const char help2[] =
" -On same as -D__OPTIMIZE__ for n > 0\n" " -On same as -D__OPTIMIZE__ for n > 0\n"
" -Wp,-opt same as -opt\n" " -Wp,-opt same as -opt\n"
" -include file include 'file' above each input file\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" " -isystem dir add 'dir' to system include path\n"
" -static link to static libraries (not recommended)\n" " -static link to static libraries (not recommended)\n"
" -dumpversion print version\n" " -dumpversion print version\n"
@ -132,7 +133,7 @@ static const char help2[] =
" no-sse disable floats on x86_64\n" " no-sse disable floats on x86_64\n"
#endif #endif
"-Wl,... linker options:\n" "-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" " -[no-]whole-archive load lib(s) fully/only as needed\n"
" -export-all-symbols same as -rdynamic\n" " -export-all-symbols same as -rdynamic\n"
" -export-dynamic same as -rdynamic\n" " -export-dynamic same as -rdynamic\n"

1
tcc.h
View File

@ -729,6 +729,7 @@ struct TCCState {
unsigned char verbose; /* if true, display some information during compilation */ unsigned char verbose; /* if true, display some information during compilation */
unsigned char nostdinc; /* if true, no standard headers are added */ unsigned char nostdinc; /* if true, no standard headers are added */
unsigned char nostdlib; /* if true, no standard libraries 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 nocommon; /* if true, do not use common symbols for .bss data */
unsigned char static_link; /* if true, static linking is performed */ unsigned char static_link; /* if true, static linking is performed */
unsigned char rdynamic; /* if true, all symbols are exported */ unsigned char rdynamic; /* if true, all symbols are exported */