Add -Wl,-I and -Wl,-dynamic-linker options

Compatible with ld.bfd, gold, lld and mold. Also, document existing
behaviour of LD_SO environment variable
This commit is contained in:
remph
2025-02-06 21:24:22 +00:00
parent f8bd136d19
commit ec60d28e0f
5 changed files with 18 additions and 1 deletions

View File

@@ -1428,6 +1428,13 @@ static int tcc_set_linker(TCCState *s, const char *option)
s->rdynamic = 1;
} else if (link_option(option, "rpath=", &p)) {
copy_linker_arg(&s->rpath, p, ':');
} else if (link_option(option, "dynamic-linker=", &p)
|| link_option(option, "I=", &p)) {
copy_linker_arg(&s->elfint, p, 0);
} else if (strncmp("-I/", option, 3) == 0
|| strncmp("-I.", option, 3) == 0) {
p = option;
copy_linker_arg(&s->elfint, option + 2, 0);
} else if (link_option(option, "enable-new-dtags", &p)) {
s->enable_new_dtags = 1;
} else if (link_option(option, "section-alignment=", &p)) {

View File

@@ -360,6 +360,11 @@ Generate an object file combining all input files.
@item -Wl,-rpath=path
Put custom search path for dynamic libraries into executable.
@item -Wl,-Ipath
@item -Wl,--dynamic-linker=path
Set the ELF interpreter (dynamic linker). This defaults to the value of the
environment variable @env{LD_SO} if set, or a compiled-in default.
@item -Wl,--enable-new-dtags
When putting a custom search path for dynamic libraries into the executable,
create the new ELF dynamic tag DT_RUNPATH instead of the old legacy DT_RPATH.

2
tcc.c
View File

@@ -152,6 +152,8 @@ static const char help2[] =
" -soname= set DT_SONAME elf tag\n"
#if defined(TCC_TARGET_MACHO)
" -install_name= set DT_SONAME elf tag (soname macOS alias)\n"
#else
" -Ipath, -dynamic-linker=path set ELF interpreter to path"
#endif
" -Bsymbolic set DT_SYMBOLIC elf tag\n"
" -oformat=[elf32/64-* binary] set executable output format\n"

1
tcc.h
View File

@@ -802,6 +802,7 @@ struct TCCState {
char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
char *soname; /* as specified on the command line (-soname) */
char *rpath; /* as specified on the command line (-Wl,-rpath=) */
char *elfint; /* -Wl,-I on command line, LD_SO in environment, or DEFAULT_ELFINTERP(this) */
char *elf_entryname; /* "_start" unless set */
char *init_symbol; /* symbols to call at load-time (not used currently) */
char *fini_symbol; /* symbols to call at unload-time (not used currently) */

View File

@@ -2850,7 +2850,9 @@ static int elf_output_file(TCCState *s1, const char *filename)
if (file_type & TCC_OUTPUT_EXE) {
char *ptr;
/* allow override the dynamic loader */
const char *elfint = getenv("LD_SO");
const char *elfint = s1->elfint;
if (elfint == NULL)
elfint = getenv("LD_SO");
if (elfint == NULL)
elfint = DEFAULT_ELFINTERP(s1);
/* add interpreter section only if executable */