History

release loaded dlls cleanly (Sam K)

grischka committed 18 years ago

Commit f9bf48d1 parent 96bd8f23 files · +24 −9Browse files

Unified · Split

3 files
  1. tcc.c +11 −2
  2. tccelf.c +1 −1
  3. tccpe.c +12 −6
tcc.c+11−2
@@ -222,6 +222,7 @@ typedef struct Section { · 221 unmodified lines
222222
223223 typedef struct DLLReference {
224224 int level;
225+ void *handle;
225226 char name[1];
226227 } DLLReference;
227228
@@ -9486,8 +9487,6 @@ static void preprocess_init(TCCState *s1) · 9258 unmodified lines
94869487 vtop = vstack - 1;
94879488 s1->pack_stack[0] = 0;
94889489 s1->pack_stack_ptr = s1->pack_stack;
9489−
9490− macro_ptr = NULL;
94919490 }
94929491
94939492 /* compile the C file opened in 'file'. Return non zero if errors. */
@@ -10095,6 +10094,8 @@ static void tcc_cleanup(void) · 601 unmodified lines
1009510094 cstr_free(&tokcstr);
1009610095 /* reset symbol stack */
1009710096 sym_free_first = NULL;
10097+ /* cleanup from error/setjmp */
10098+ macro_ptr = NULL;
1009810099 }
1009910100
1010010101 TCCState *tcc_new(void)
@@ -10227,6 +10228,14 @@ void tcc_delete(TCCState *s1) · 126 unmodified lines
1022710228 for(i = 1; i < s1->nb_sections; i++)
1022810229 free_section(s1->sections[i]);
1022910230 tcc_free(s1->sections);
10231+
10232+ /* free any loaded DLLs */
10233+ for ( i = 0; i < s1->nb_loaded_dlls; i++)
10234+ {
10235+ DLLReference *ref = s1->loaded_dlls[i];
10236+ if ( ref->handle )
10237+ dlclose(ref->handle);
10238+ }
1023010239
1023110240 /* free loaded dlls array */
1023210241 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
tccelf.c+1−1
@@ -2276,7 +2276,7 @@ static int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) · 2275 unmodified lines
22762276 // printf("loading dll '%s'\n", soname);
22772277
22782278 /* add the dll and its level */
2279− dllref = tcc_malloc(sizeof(DLLReference) + strlen(soname));
2279+ dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname));
22802280 dllref->level = level;
22812281 strcpy(dllref->name, soname);
22822282 dynarray_add((void ***)&s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
tccpe.c+12−6
@@ -465,6 +465,8 @@ ST_FN int pe_find_import(TCCState * s1, const char *symbol) · 464 unmodified lines
465465 # include <dlfcn.h>
466466 # define LoadLibrary(s) dlopen(s, RTLD_NOW)
467467 # define GetProcAddress(h,s) dlsym(h, s)
468+#else
469+# define dlclose(h) FreeLibrary(h)
468470 #endif
469471
470472 /* for the -run option: dynamically load symbol from dll */
@@ -472,16 +474,21 @@ void *resolve_sym(struct TCCState *s1, const char *symbol, int type) · 1 unmodified line
472474 {
473475 char buffer[100];
474476 int sym_index, dll_index;
475− void *hModule, *addr, **m;
477+ void *addr, **m;
478+ DLLReference *dllref;
476479
477480 sym_index = pe_find_import(s1, symbol);
478481 if (0 == sym_index)
479482 return NULL;
480483 dll_index = ((Elf32_Sym *)s1->dynsymtab_section->data + sym_index)->st_value;
481− hModule = LoadLibrary(s1->loaded_dlls[dll_index-1]->name);
482− addr = GetProcAddress(hModule, symbol);
484+ dllref = s1->loaded_dlls[dll_index-1];
485+ if ( !dllref->handle )
486+ {
487+ dllref->handle = LoadLibrary(dllref->name);
488+ }
489+ addr = GetProcAddress(dllref->handle, symbol);
483490 if (NULL == addr)
484− addr = GetProcAddress(hModule, get_alt_symbol(buffer, symbol));
491+ addr = GetProcAddress(dllref->handle, get_alt_symbol(buffer, symbol));
485492
486493 if (addr && STT_OBJECT == type) {
487494 /* need to return a pointer to the address for data objects */
@@ -1413,9 +1420,8 @@ PUB_FN int pe_load_def_file(TCCState *s1, int fd) · 925 unmodified lines
14131420 continue;
14141421
14151422 case 2:
1416− dllref = tcc_malloc(sizeof(DLLReference) + strlen(dllname));
1423+ dllref = tcc_mallocz(sizeof(DLLReference) + strlen(dllname));
14171424 strcpy(dllref->name, dllname);
1418− dllref->level = 0;
14191425 dynarray_add((void ***) &s1->loaded_dlls, &s1->nb_loaded_dlls, dllref);
14201426 ++state;
14211427