History

Fix `tcc -run` on Windows

waterlens committed 1 year ago

Commit 52a9a541 parent a06c6085 files · +34 −15Browse files

Unified · Split

5 files
  1. libtcc.c +13 −5
  2. tcc.h +2 −2
  3. tccelf.c +13 −4
  4. tccmacho.c +1 −1
  5. tccpe.c +5 −3
libtcc.c+13−5
@@ -78,6 +78,10 @@ ST_DATA int nb_stk_data; · 77 unmodified lines
78/* option -d<num> (for general development purposes) */78/* option -d<num> (for general development purposes) */
79ST_DATA int g_debug;79ST_DATA int g_debug;
80 80
81#define DEBUG_LIBTCC 0
82#undef dprintf
83#define dprintf if (DEBUG_LIBTCC) printf
84
81/********************************************************/85/********************************************************/
82#ifdef _WIN3286#ifdef _WIN32
83ST_FUNC char *normalize_slashes(char *path)87ST_FUNC char *normalize_slashes(char *path)
@@ -963,15 +967,19 @@ LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname) · 879 unmodified lines
963}967}
964 968
965/* add/update a 'DLLReference', Just find if level == -1 */969/* add/update a 'DLLReference', Just find if level == -1 */
966ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname, int level)970ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllpath, int level)
967{971{
968 DLLReference *ref = NULL;972 DLLReference *ref = NULL;
969 int i;973 int i;
970 for (i = 0; i < s1->nb_loaded_dlls; i++)974 const char *dllname = tcc_basename(dllpath);
971 if (0 == strcmp(s1->loaded_dlls[i]->name, dllname)) {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)) {
972 ref = s1->loaded_dlls[i];979 ref = s1->loaded_dlls[i];
973 break;980 break;
974 }981 }
982 }
975 if (level == -1)983 if (level == -1)
976 return ref;984 return ref;
977 if (ref) {985 if (ref) {
@@ -980,8 +988,8 @@ ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname, int leve · 2 unmodified lines
980 ref->found = 1;988 ref->found = 1;
981 return ref;989 return ref;
982 }990 }
983 ref = tcc_mallocz(sizeof(DLLReference) + strlen(dllname));991 ref = tcc_mallocz(sizeof(DLLReference) + strlen(dllpath));
984 strcpy(ref->name, dllname);992 strcpy(ref->path, dllpath);
985 dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, ref);993 dynarray_add(&s1->loaded_dlls, &s1->nb_loaded_dlls, ref);
986 ref->level = level;994 ref->level = level;
987 ref->index = s1->nb_loaded_dlls;995 ref->index = s1->nb_loaded_dlls;
tcc.h+2−2
@@ -583,7 +583,7 @@ typedef struct DLLReference { · 582 unmodified lines
583 int level;583 int level;
584 void *handle;584 void *handle;
585 unsigned char found, index;585 unsigned char found, index;
586 char name[1];586 char path[1];
587} DLLReference;587} DLLReference;
588 588
589/* -------------------------------------------------- */589/* -------------------------------------------------- */
@@ -1293,7 +1293,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int *argc, char ***argv, int optind); · 703 unmodified lines
1293#ifdef _WIN321293#ifdef _WIN32
1294ST_FUNC char *normalize_slashes(char *path);1294ST_FUNC char *normalize_slashes(char *path);
1295#endif1295#endif
1296ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname, int level);1296ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllpath, int level);
1297ST_FUNC char *tcc_load_text(int fd);1297ST_FUNC char *tcc_load_text(int fd);
1298/* for #pragma once */1298/* for #pragma once */
1299ST_FUNC int normalized_PATHCMP(const char *f1, const char *f2);1299ST_FUNC int normalized_PATHCMP(const char *f1, const char *f2);
tccelf.c+13−4
@@ -1056,9 +1056,17 @@ ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve) · 1055 unmodified lines
1056 name = (char *) s1->symtab->link->data + sym->st_name;1056 name = (char *) s1->symtab->link->data + sym->st_name;
1057 /* Use ld.so to resolve symbol for us (for tcc -run) */1057 /* Use ld.so to resolve symbol for us (for tcc -run) */
1058 if (do_resolve) {1058 if (do_resolve) {
1059#if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE1059 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
1060 /* dlsym() needs the undecorated name. */1068 /* dlsym() needs the undecorated name. */
1061 void *addr = dlsym(RTLD_DEFAULT, &name[s1->leading_underscore]);1069 addr = dlsym(RTLD_DEFAULT, &name[s1->leading_underscore]);
1062#if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD || TARGETOS_ANDROID1070#if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD || TARGETOS_ANDROID
1063 if (addr == NULL) {1071 if (addr == NULL) {
1064 int i;1072 int i;
@@ -1066,6 +1074,7 @@ ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve) · 1 unmodified line
1066 if ((addr = dlsym(s1->loaded_dlls[i]->handle, name)))1074 if ((addr = dlsym(s1->loaded_dlls[i]->handle, name)))
1067 break;1075 break;
1068 }1076 }
1077#endif
1069#endif1078#endif
1070 if (addr) {1079 if (addr) {
1071 sym->st_value = (addr_t) addr;1080 sym->st_value = (addr_t) addr;
@@ -2901,7 +2910,7 @@ static int elf_output_file(TCCState *s1, const char *filename) · 1829 unmodified lines
2901 for(i = 0; i < s1->nb_loaded_dlls; i++) {2910 for(i = 0; i < s1->nb_loaded_dlls; i++) {
2902 DLLReference *dllref = s1->loaded_dlls[i];2911 DLLReference *dllref = s1->loaded_dlls[i];
2903 if (dllref->level == 0)2912 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)));
2905 }2914 }
2906 2915
2907 if (s1->rpath)2916 if (s1->rpath)
@@ -3723,7 +3732,7 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 815 unmodified lines
3723 soname = dynstr + dt->d_un.d_val;3732 soname = dynstr + dt->d_un.d_val;
3724 3733
3725 /* if the dll is already loaded, do not load it */3734 /* 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)
3727 goto ret_success;3736 goto ret_success;
3728 3737
3729 if (v.nb_versyms != nb_syms)3738 if (v.nb_versyms != nb_syms)
tccmacho.c+1−1
@@ -1770,7 +1770,7 @@ static void collect_sections(TCCState *s1, struct macho *mo, const char *filenam · 1769 unmodified lines
1770 for(i = 0; i < s1->nb_loaded_dlls; i++) {1770 for(i = 0; i < s1->nb_loaded_dlls; i++) {
1771 DLLReference *dllref = s1->loaded_dlls[i];1771 DLLReference *dllref = s1->loaded_dlls[i];
1772 if (dllref->level == 0)1772 if (dllref->level == 0)
1773 add_dylib(mo, dllref->name);1773 add_dylib(mo, tcc_basename(dllref->path));
1774 }1774 }
1775 1775
1776 if (s1->rpath) {1776 if (s1->rpath) {
tccpe.c+5−3
@@ -848,7 +848,9 @@ static void pe_build_imports(struct pe_info *pe) · 847 unmodified lines
848 848
849 dllindex = p->dll_index;849 dllindex = p->dll_index;
850 if (dllindex)850 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);
852 else854 else
853 name = "", dllref = NULL;855 name = "", dllref = NULL;
854 856
@@ -884,7 +886,7 @@ static void pe_build_imports(struct pe_info *pe) · 29 unmodified lines
884 if (pe->type == PE_RUN) {886 if (pe->type == PE_RUN) {
885 if (dllref) {887 if (dllref) {
886 if ( !dllref->handle )888 if ( !dllref->handle )
887 dllref->handle = LoadLibraryA(dllref->name);889 dllref->handle = LoadLibraryA(dllref->path);
888 v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name);890 v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name);
889 }891 }
890 if (!v)892 if (!v)
@@ -1736,7 +1738,7 @@ quit: · 845 unmodified lines
1736static int pe_load_dll(TCCState *s1, int fd, const char *filename)1738static int pe_load_dll(TCCState *s1, int fd, const char *filename)
1737{1739{
1738 char *p, *q;1740 char *p, *q;
1739 DLLReference *ref = tcc_add_dllref(s1, tcc_basename(filename), 0);1741 DLLReference *ref = tcc_add_dllref(s1, filename, 0);
1740 if (ref->found)1742 if (ref->found)
1741 return 0;1743 return 0;
1742 if (get_dllexports(fd, &p))1744 if (get_dllexports(fd, &p))

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t