コード例 #1
0
ファイル: ildasm_main.c プロジェクト: bencz/DotGnu
/*
 * Dump the contents of a file.
 */
static int dumpFile(const char *filename, FILE *stream,
					int closeStream, ILContext *context,
					int dumpSections, int flags)
{
	int loadError;
	ILImage *image;
	int imageType;

	/* Attempt to load the image into memory */
	loadError = ILImageLoad(stream, filename, context, &image,
							IL_LOADFLAG_FORCE_32BIT |
							IL_LOADFLAG_PRE_VALIDATE |
							((flags & ILDASM_RESOLVE_ALL)
								? 0 : IL_LOADFLAG_NO_RESOLVE) |
							(dumpSections ? IL_LOADFLAG_NO_METADATA : 0));
	if(loadError != 0)
	{
		if(closeStream)
		{
			fclose(stream);
		}
		fprintf(stderr, "%s: %s\n", filename, ILImageLoadError(loadError));
		return 1;
	}

	/* Close the input stream, because we don't need it any more */
	if(closeStream)
	{
		fclose(stream);
	}

	/* Dump interesting information about the image in general */
	fprintf(outstream, "// Input: %s\n", filename);
	imageType = ILImageType(image);
	fprintf(outstream, "// Image type: %s\n",
		    (imageType == IL_IMAGETYPE_DLL ? "DLL" :
				(imageType == IL_IMAGETYPE_EXE ? "EXE" :
					(imageType == IL_IMAGETYPE_JAVA ? "Java" :
						(imageType == IL_IMAGETYPE_OBJ ? "OBJ" : "Unknown")))));
	fprintf(outstream, "// Native code present: %s\n",
		    (ILImageHadNative(image) ? "Yes" : "No"));
	fprintf(outstream, "// 32-bit only: %s\n",
		    (ILImageIs32Bit(image) ? "Yes" : "No"));
	fprintf(outstream, "// Length of IL data: %lu\n", ILImageLength(image));
	fprintf(outstream, "\n");

	/* How should we dump the image? */
	if(dumpSections)
	{
		dumpSectionData(image, (flags & ILDASM_REAL_OFFSETS) != 0);
	}
	else
	{
		dumpAssembly(image, flags);
	}

	/* Clean up and exit */
	ILImageDestroy(image);
	return 0;
}
コード例 #2
0
/*
 * Load an IL image from an input stream and verify all of its methods.
 */
static int verify(const char *filename, ILContext *context, int allowUnsafe)
{
	ILImage *image;
	ILMethod *method;
	ILMethodCode code;
	int result;
	unsigned char *start;

	/* Attempt to load the image into memory */
	if(ILImageLoadFromFile(filename, context, &image,
						   IL_LOADFLAG_FORCE_32BIT, 1) != 0)
	{
		return 0;
	}

	/* Scan the entire MethodDef table and verify everything we find */
	method = 0;
	while((method = (ILMethod *)ILImageNextToken
				(image, IL_META_TOKEN_METHOD_DEF, (void *)method)) != 0)
	{
		/* Skip this method if it does not have IL bytecode */
		if(!ILMethod_RVA(method))
		{
			continue;
		}

		/* Get the IL bytecode for the method */
		if(!ILMethodGetCode(method, &code))
		{
			printError(image, method, "malformed code");
			continue;
		}

		/* Verify the method */
		result = _ILVerify(&_ILNullCoder, &start, method,
						   &code, allowUnsafe, 0);
		if(!result)
		{
			printError(image, method, "could not verify code");
		}
	}

	/* Clean up and exit */
	ILImageDestroy(image);
	return 0;
}