Skip to content

Commit 52a9a54

Browse files
committed
Fix tcc -run on Windows
1 parent a06c608 commit 52a9a54

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

Diff for: libtcc.c

+13-5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ ST_DATA int nb_stk_data;
7878
/* option -d<num> (for general development purposes) */
7979
ST_DATA int g_debug;
8080

81+
#define DEBUG_LIBTCC 0
82+
#undef dprintf
83+
#define dprintf if (DEBUG_LIBTCC) printf
84+
8185
/********************************************************/
8286
#ifdef _WIN32
8387
ST_FUNC char *normalize_slashes(char *path)
@@ -963,15 +967,19 @@ LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
963967
}
964968

965969
/* add/update a 'DLLReference', Just find if level == -1 */
966-
ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname, int level)
970+
ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllpath, int level)
967971
{
968972
DLLReference *ref = NULL;
969973
int i;
970-
for (i = 0; i < s1->nb_loaded_dlls; i++)
971-
if (0 == strcmp(s1->loaded_dlls[i]->name, dllname)) {
974+
const char *dllname = tcc_basename(dllpath);
975+
const char *name;
976+
for (i = 0; i < s1->nb_loaded_dlls; i++) {
977+
name = tcc_basename(s1->loaded_dlls[i]->path);
978+
if (0 == strcmp(name, dllname)) {
972979
ref = s1->loaded_dlls[i];
973980
break;
974981
}
982+
}
975983
if (level == -1)
976984
return ref;
977985
if (ref) {
@@ -980,8 +988,8 @@ ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname, int leve
980988
ref->found = 1;
981989
return ref;
982990
}
983-
ref = tcc_mallocz(sizeof(DLLReference) + strlen(dllname));
984-
strcpy(ref->name, dllname);
991+
ref = tcc_mallocz(sizeof(DLLReference) + strlen(dllpath));
992+
strcpy(ref->path, dllpath);
985993
dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, ref);
986994
ref->level = level;
987995
ref->index = s1->nb_loaded_dlls;

Diff for: tcc.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ typedef struct DLLReference {
583583
int level;
584584
void *handle;
585585
unsigned char found, index;
586-
char name[1];
586+
char path[1];
587587
} DLLReference;
588588

589589
/* -------------------------------------------------- */
@@ -1293,7 +1293,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int *argc, char ***argv, int optind);
12931293
#ifdef _WIN32
12941294
ST_FUNC char *normalize_slashes(char *path);
12951295
#endif
1296-
ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname, int level);
1296+
ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllpath, int level);
12971297
ST_FUNC char *tcc_load_text(int fd);
12981298
/* for #pragma once */
12991299
ST_FUNC int normalized_PATHCMP(const char *f1, const char *f2);

Diff for: tccelf.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -1056,16 +1056,25 @@ ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve)
10561056
name = (char *) s1->symtab->link->data + sym->st_name;
10571057
/* Use ld.so to resolve symbol for us (for tcc -run) */
10581058
if (do_resolve) {
1059-
#if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE
1059+
void *addr = NULL;
1060+
#if defined(TCC_IS_NATIVE)
1061+
#if defined(TCC_TARGET_PE)
1062+
int i;
1063+
for (i = 0; i < s1->nb_loaded_dlls; i++) {
1064+
if ((addr = GetProcAddress(s1->loaded_dlls[i]->handle, name)))
1065+
break;
1066+
}
1067+
#else
10601068
/* dlsym() needs the undecorated name. */
1061-
void *addr = dlsym(RTLD_DEFAULT, &name[s1->leading_underscore]);
1069+
addr = dlsym(RTLD_DEFAULT, &name[s1->leading_underscore]);
10621070
#if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD || TARGETOS_ANDROID
10631071
if (addr == NULL) {
10641072
int i;
10651073
for (i = 0; i < s1->nb_loaded_dlls; i++)
10661074
if ((addr = dlsym(s1->loaded_dlls[i]->handle, name)))
10671075
break;
10681076
}
1077+
#endif
10691078
#endif
10701079
if (addr) {
10711080
sym->st_value = (addr_t) addr;
@@ -2901,7 +2910,7 @@ static int elf_output_file(TCCState *s1, const char *filename)
29012910
for(i = 0; i < s1->nb_loaded_dlls; i++) {
29022911
DLLReference *dllref = s1->loaded_dlls[i];
29032912
if (dllref->level == 0)
2904-
put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, dllref->name));
2913+
put_dt(dynamic, DT_NEEDED, put_elf_str(dynstr, tcc_basename(dllref->path)));
29052914
}
29062915

29072916
if (s1->rpath)
@@ -3723,7 +3732,7 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
37233732
soname = dynstr + dt->d_un.d_val;
37243733

37253734
/* if the dll is already loaded, do not load it */
3726-
if (tcc_add_dllref(s1, soname, level)->found)
3735+
if (tcc_add_dllref(s1, filename, level)->found)
37273736
goto ret_success;
37283737

37293738
if (v.nb_versyms != nb_syms)

Diff for: tccmacho.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,7 @@ static void collect_sections(TCCState *s1, struct macho *mo, const char *filenam
17701770
for(i = 0; i < s1->nb_loaded_dlls; i++) {
17711771
DLLReference *dllref = s1->loaded_dlls[i];
17721772
if (dllref->level == 0)
1773-
add_dylib(mo, dllref->name);
1773+
add_dylib(mo, tcc_basename(dllref->path));
17741774
}
17751775

17761776
if (s1->rpath) {

Diff for: tccpe.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,9 @@ static void pe_build_imports(struct pe_info *pe)
848848

849849
dllindex = p->dll_index;
850850
if (dllindex)
851-
name = (dllref = pe->s1->loaded_dlls[dllindex-1])->name;
851+
name = tcc_basename(
852+
(dllref = pe->s1->loaded_dlls[dllindex-1])
853+
->path);
852854
else
853855
name = "", dllref = NULL;
854856

@@ -884,7 +886,7 @@ static void pe_build_imports(struct pe_info *pe)
884886
if (pe->type == PE_RUN) {
885887
if (dllref) {
886888
if ( !dllref->handle )
887-
dllref->handle = LoadLibraryA(dllref->name);
889+
dllref->handle = LoadLibraryA(dllref->path);
888890
v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name);
889891
}
890892
if (!v)
@@ -1736,7 +1738,7 @@ static int pe_load_def(TCCState *s1, int fd)
17361738
static int pe_load_dll(TCCState *s1, int fd, const char *filename)
17371739
{
17381740
char *p, *q;
1739-
DLLReference *ref = tcc_add_dllref(s1, tcc_basename(filename), 0);
1741+
DLLReference *ref = tcc_add_dllref(s1, filename, 0);
17401742
if (ref->found)
17411743
return 0;
17421744
if (get_dllexports(fd, &p))

0 commit comments

Comments
 (0)