Skip to content

Commit 43158ea

Browse files
committed
tccelf: consider DSO symbols for PROVIDE
An input shared object can be the only place where a conventional boundary name is referenced or defined. In that case the regular symbol table has no entry, but native ELF linkers still synthesize the executable definition so that it participates in dynamic symbol resolution. Consult the input dynamic symbol table before treating a plain boundary name as unreferenced. Exercise the case with a DSO-defined end symbol whose address must be interposed by the executable's final boundary.
1 parent 384614a commit 43158ea

5 files changed

Lines changed: 37 additions & 5 deletions

File tree

tccelf.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,9 +1586,13 @@ static void provide_linker_sym(TCCState *s1, const char *name,
15861586
{
15871587
int sym_index = find_elf_sym(symtab_section, name);
15881588

1589-
if (!sym_index
1590-
|| ((ElfW(Sym) *)symtab_section->data)[sym_index].st_shndx != SHN_UNDEF)
1589+
if (sym_index) {
1590+
if (((ElfW(Sym) *)symtab_section->data)[sym_index].st_shndx != SHN_UNDEF)
1591+
return;
1592+
} else if (!s1->dynsymtab_section
1593+
|| !find_elf_sym(s1->dynsymtab_section, name)) {
15911594
return;
1595+
}
15921596
set_linker_sym(s1, name, sec, offs);
15931597
}
15941598

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
char end = 7;
2+
3+
char *get_dso_end(void)
4+
{
5+
return &end;
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
3+
extern char _end[];
4+
extern char *get_dso_end(void);
5+
6+
int main(void)
7+
{
8+
int errors = get_dso_end() != _end;
9+
10+
printf("DSO linker symbol errors: %d\n", errors);
11+
return errors;
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DSO linker symbol errors: 0

tests/tests2/Makefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ ifeq ($(CONFIG_bcheck),no)
3838
endif
3939
ifeq ($(CONFIG_dll),no)
4040
SKIP += 113_btdll.test 147_def_library.test # no shared lib support yet
41-
SKIP += 149_end_copy_reloc.test
41+
SKIP += 149_end_copy_reloc.test 151_dso_linker_symbol.test
4242
endif
4343
ifeq (-$(findstring gcc,$(CC))-,--)
4444
SKIP += $(patsubst %.expect,%.test,$(GEN-ALWAYS))
@@ -56,7 +56,7 @@ ifeq (-$(CONFIG_WIN32)-,-yes-)
5656
SKIP += 124_atomic_counter.test # No pthread support
5757
SKIP += 144_tls.test 146_tls_extern.test # TLS runtime not supported
5858
SKIP += 148_linker_symbols.test 149_end_copy_reloc.test # ELF linker tests
59-
SKIP += 150_linker_boundaries.test
59+
SKIP += 150_linker_boundaries.test 151_dso_linker_symbol.test
6060
endif
6161
ifneq (,$(filter OpenBSD FreeBSD NetBSD,$(TARGETOS)))
6262
SKIP += 106_versym.test # no pthread_condattr_setpshared
@@ -70,7 +70,7 @@ endif
7070
ifeq ($(CONFIG_OSX),yes)
7171
SKIP += 144_tls.test 146_tls_extern.test # TLS runtime not supported on Mach-O
7272
SKIP += 148_linker_symbols.test 149_end_copy_reloc.test # ELF linker tests
73-
SKIP += 150_linker_boundaries.test
73+
SKIP += 150_linker_boundaries.test 151_dso_linker_symbol.test
7474
endif
7575
ifeq ($(CONFIG_pie),yes)
7676
SKIP += 149_end_copy_reloc.test # PIE executables do not use copy relocations
@@ -183,6 +183,15 @@ GEN-ALWAYS =
183183
./$(basename $@).exe \
184184
)
185185

186+
# A linker symbol needed only by a DSO must be provided by the executable.
187+
151_dso_linker_symbol.test: NORUN = true
188+
151_dso_linker_symbol.test: T1 = ( \
189+
$(TCC) -shared $(subst 151,151+,$1) -o 151_dso_linker_symbol$(DLLSUF) && \
190+
$(TCC) $1 ./151_dso_linker_symbol$(DLLSUF) -Wl,-rpath=. \
191+
-o $(basename $@).exe && \
192+
./$(basename $@).exe \
193+
)
194+
186195
112_backtrace.test: FLAGS += -dt -b
187196
112_backtrace.test 113_btdll.test 126_bound_global.test: FILTER += \
188197
-e 's;[0-9A-Fa-fx]\{5,\};........;g' \

0 commit comments

Comments
 (0)