elfdll.c 8.84 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Elf-dll loader functions
 *
 * Copyright 1999 Bertho A. Stultiens
 */

#include <string.h>
#include <ctype.h>
9
#include <stdlib.h>
10 11 12 13 14 15 16 17 18 19

#include "config.h"
#include "windef.h"
#include "global.h"
#include "process.h"
#include "module.h"
#include "neexe.h"
#include "heap.h"
#include "wine/winbase16.h"
#include "elfdll.h"
20
#include "debugtools.h"
21 22
#include "winerror.h"

23 24
DECLARE_DEBUG_CHANNEL(elfdll)

25
#if defined(HAVE_DL_API)
26 27 28 29
#include <dlfcn.h>

/*------------------ HACKS -----------------*/
extern DWORD fixup_imports(WINE_MODREF *wm);
30
extern void dump_exports(HMODULE hModule);
31 32
/*---------------- END HACKS ---------------*/

33 34
char *extra_ld_library_path = NULL;	/* The extra search-path set in wine.conf */

35 36 37 38 39 40 41 42 43
struct elfdll_image
{
	HMODULE		pe_module_start;
	DWORD		pe_module_size;
	NE_MODULE	*ne_module_start;
	DWORD		ne_module_size;
};


44 45 46
/****************************************************************************
 *	ELFDLL_dlopen
 *
47 48 49
 * Wrapper for dlopen to search the EXTRA_LD_LIBRARY_PATH from wine.conf
 * manually because libdl.so caches the environment and does not accept our
 * changes.
50 51 52 53 54
 */
void *ELFDLL_dlopen(const char *libname, int flags)
{
	char buffer[256];
	int namelen;
55 56
	void *handle;
	char *ldpath;
57

58 59 60 61
	/* First try the default path search of dlopen() */
	handle = dlopen(libname, flags);
	if(handle)
		return handle;
62

63
	/* Now try to construct searches through our extra search-path */
64
	namelen = strlen(libname);
65 66
	ldpath = extra_ld_library_path;
	while(ldpath && *ldpath)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	{
		int len;
		char *cptr;
		char *from;

		from = ldpath;
		cptr = strchr(ldpath, ':');
		if(!cptr)
		{
			len = strlen(ldpath);
			ldpath = NULL;
		}
		else
		{
			len = cptr - ldpath;
			ldpath = cptr + 1;
		}

		if(len + namelen + 1 >= sizeof(buffer))
		{
87
			ERR_(elfdll)("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
88 89 90 91 92 93 94 95 96 97 98 99
			return NULL;
		}

		strncpy(buffer, from, len);
		if(len)
		{
			buffer[len] = '/';
			strcpy(buffer + len + 1, libname);
		}
		else
			strcpy(buffer + len, libname);

100
		TRACE_(elfdll)("Trying dlopen('%s', %d)\n", buffer, flags);
101 102 103 104 105 106 107 108 109

		handle = dlopen(buffer, flags);
		if(handle)
			return handle;
	}
	return NULL;
}


110 111 112 113 114 115 116 117 118 119 120 121 122
/****************************************************************************
 *	get_sobasename	(internal)
 *
 */
static LPSTR get_sobasename(LPCSTR path, LPSTR name)
{
	char *cptr;

	/* Strip the path from the library name */
	if((cptr = strrchr(path, '/')))
	{
		char *cp = strrchr(cptr+1, '\\');
		if(cp && cp > cptr)
123
			cptr = cp;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	}
	else
		cptr = strrchr(path, '\\');

	if(!cptr)
		cptr = (char *)path;	/* No '/' nor '\\' in path */
	else
		cptr++;

	strcpy(name, cptr);
	cptr = strrchr(name, '.');
	if(cptr)
		*cptr = '\0';	/* Strip extension */

	/* Convert to lower case.
	 * This must be done manually because it is not sure that
	 * other modules are accessible.
	 */
	for(cptr = name; *cptr; cptr++)
		*cptr = tolower(*cptr);

	return name;
}


/****************************************************************************
 *	ELFDLL_CreateModref	(internal)
 *
 * INPUT
 *	hModule	- the header from the elf-dll's data-segment
 *	path	- requested path from original call
 *
 * OUTPUT
 *	A WINE_MODREF pointer to the new object
 *
 * BUGS
 *	- Does not handle errors due to dependencies correctly
 *	- path can be wrong
 */
#define RVA(base, va)	(((DWORD)base) + ((DWORD)va))

static WINE_MODREF *ELFDLL_CreateModref(HMODULE hModule, LPCSTR path)
{
	IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
	IMAGE_DATA_DIRECTORY *dir;
	IMAGE_IMPORT_DESCRIPTOR *pe_import = NULL;
	WINE_MODREF *wm;
	int len;
	HANDLE procheap = GetProcessHeap();

	wm = (WINE_MODREF *)HeapAlloc(procheap, HEAP_ZERO_MEMORY, sizeof(*wm));
	if(!wm)
		return NULL;

	wm->module = hModule;
	wm->type = MODULE32_PE;		/* FIXME */

	dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
	if(dir->Size)
		wm->binfmt.pe.pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(hModule, dir->VirtualAddress);

	dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
	if(dir->Size)
		pe_import = wm->binfmt.pe.pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(hModule, dir->VirtualAddress);

	dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
	if(dir->Size)
		wm->binfmt.pe.pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(hModule, dir->VirtualAddress);

	wm->modname = HEAP_strdupA(procheap, 0, (char *)RVA(hModule, wm->binfmt.pe.pe_export->Name));

	len = GetLongPathNameA(path, NULL, 0);
	wm->longname = (char *)HeapAlloc(procheap, 0, len+1);
	GetLongPathNameA(path, wm->longname, len+1);

	wm->shortname = HEAP_strdupA(procheap, 0, path);

	/* Link MODREF into process list */
	wm->next = PROCESS_Current()->modref_list;
	PROCESS_Current()->modref_list = wm;

	if(!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
	{
		if(PROCESS_Current()->exe_modref)
208
			FIXME_(elfdll)("overwriting old exe_modref... arrgh\n");
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
		PROCESS_Current()->exe_modref = wm;
	}

	/* Fixup Imports */
	if(pe_import && fixup_imports(wm)) 
	{
		/* Error in this module or its dependencies
		 * remove entry from modref chain
		 */
		WINE_MODREF **xwm;
		for(xwm = &(PROCESS_Current()->modref_list); *xwm; xwm = &((*xwm)->next))
		{
			if ( *xwm == wm )
			{
				*xwm = wm->next;
				break;
			}
		}
		if(wm == PROCESS_Current()->exe_modref)
228
			ERR_(elfdll)("Have to delete current exe_modref. Expect crash now\n");
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
		HeapFree(procheap, 0, wm->shortname);
		HeapFree(procheap, 0, wm->longname);
		HeapFree(procheap, 0, wm->modname);
		HeapFree(procheap, 0, wm);
		return NULL;

		/* FIXME: We should traverse back in the recursion
		 * with an error to unload everything that got loaded
		 * before this error occurred.
		 * Too dificult for now though and we don't care at the 
		 * moment. But, it *MUST* be implemented someday because
		 * we won't be able to map the elf-dll twice in this
		 * address-space which can cause some unexpected and
		 * weird problems later on.
		 */
	}

	return wm;
}


/***********************************************************************
 *           ELFDLL_CreateNEModule
 *
 * Create a dummy NE module for the win32 elf-dll based on the supplied
 * NE header in the elf-dll.
 */
static HMODULE16 ELFDLL_CreateNEModule(NE_MODULE *ne_image, DWORD size)
{
258
	NE_MODULE *pModule;
259 260 261 262 263 264
	HMODULE16 hModule = GLOBAL_CreateBlock(GMEM_MOVEABLE, ne_image, size, 0,
						FALSE, FALSE, FALSE, NULL);
	if(!hModule)
		return (HMODULE16)0;

	FarSetOwner16(hModule, hModule);
265 266 267
	pModule = (NE_MODULE *)GlobalLock16(hModule);
	pModule->self = hModule;
	NE_RegisterModule(pModule);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	return hModule;
}


/****************************************************************************
 *	ELFDLL_LoadLibraryExA	(internal)
 *
 * Implementation of elf-dll loading for PE modules
 */
WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags, DWORD *err)
{
	LPVOID dlhandle;
	struct elfdll_image *image;
	char name[129];
	char soname[129];
	HMODULE16 hmod16;
	WINE_MODREF *wm;

	get_sobasename(path, name);
	strcpy(soname, name);
	strcat(soname, ".so");

290 291
	/* Try to open the elf-dll */
	dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
292 293
	if(!dlhandle)
	{
294
		WARN_(elfdll)("Could not load %s (%s)\n", soname, dlerror());
295 296 297 298 299 300 301 302 303 304
		*err = ERROR_FILE_NOT_FOUND;
		return NULL;
	}

	/* Get the 'dllname_elfdll_image' variable */
	strcpy(soname, name);
	strcat(soname, "_elfdll_image");
	image = (struct elfdll_image *)dlsym(dlhandle, soname);
	if(!image) 
	{
305
		ERR_(elfdll)("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
306 307 308 309 310 311 312 313 314
		dlclose(dlhandle);
		*err = ERROR_BAD_FORMAT;
		return NULL;
	}

	/* Create a win16 dummy module */
	hmod16 = ELFDLL_CreateNEModule(image->ne_module_start, image->ne_module_size);
	if(!hmod16)
	{
315
		ERR_(elfdll)("Could not create win16 dummy module for %s\n", path);
316 317 318 319 320 321 322 323 324 325
		dlclose(dlhandle);
		*err = ERROR_OUTOFMEMORY;
		return NULL;
	}

	image->ne_module_start->module32 = image->pe_module_start;

	wm = ELFDLL_CreateModref(image->pe_module_start, path);
	if(!wm)
	{
326
		ERR_(elfdll)("Could not create WINE_MODREF for %s\n", path);
327 328 329 330 331 332
		GLOBAL_FreeBlock((HGLOBAL16)hmod16);
		dlclose(dlhandle);
		*err = ERROR_OUTOFMEMORY;
		return NULL;
	}

333 334
	dump_exports(image->pe_module_start);

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	*err = 0;
	return wm;
}


/****************************************************************************
 *	ELFDLL_UnloadLibrary	(internal)
 *
 * Unload an elf-dll completely from memory and deallocate the modref
 */
void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
{
}


/****************************************************************************
 *	ELFDLL_LoadModule16	(internal)
 *
 * Implementation of elf-dll loading for NE modules
 */
HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
{
	return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
}

#else

/*
 * No elfdlls possible 
 * Just put stubs in here.
 */

WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags, DWORD *err)
{
	*err = ERROR_FILE_NOT_FOUND;
	return NULL;
}

void ELFDLL_UnloadLibrary(WINE_MODREF *wm)
{
}

HINSTANCE16 ELFDLL_LoadModule16(LPCSTR libname, BOOL implicit)
{
	return (HINSTANCE16)ERROR_FILE_NOT_FOUND;
}

#endif