/*
 * jmpstub.sx
 *
 * Generic trampoline, mapping any conventional function name to the entry
 * point for an implementation within the "__mingw_" pseudo-namespace.
 *
 * $Id$
 *
 * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
 * Copyright (C) 2013, 2014, 2017, MinGW.org Project
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 *
 * This file implements a generic trampoline stub, which remaps a specified
 * function, via a CPU "jmp" instruction, to an alternative entry point; it
 * may be compiled using a command such as:
 *
 *   $(CC) -c $(CPPFLAGS) -D FUNCTION=funcname -o funcname.o jmpstub.sx
 *
 * to create a free standing object module, providing a physically addressable
 * entry point for __CRT_ALIAS function, "funcname".  (Note that any version of
 * GCC predating GCC-4.3 may also require the "-x assembler-with-cpp" option).
 *
 * By default, the generated stub redirects calls to function "funcname()",
 * such that they invoke the equivalent library function, with entry point at
 * "__mingw_funcname()"; this default may be overriden, by specification of
 * an additional "-D REMAPPED=entryname" option, resulting in redirection
 * of "funcname()" calls to the "entryname()" function entry point.
 *
 * Alternatively, calls to "funcname()" may be redirected to a DLL entry point,
 * via its importable name within the DLL's export table, by specification of
 * the "-D DLLENTRY=entryname" option, instead of "-D REMAPPED=entryname".
 *
 */
#define __entry__(__suffix__)  __label__(_,__suffix__)
#define __label__(__prefix__,__suffix__)  __prefix__##__suffix__

#if defined LLENTRY && ! defined DLLENTRY
/* This is a convenience for users; it allows specification of a DLLENTRY
 * option as simply "-DLLENTRY=entryname", as an alternative to specifying
 * it fully, as "-D DLLENTRY=entryname".
 */
#define DLLENTRY  LLENTRY
#endif

.text
#ifdef DLLENTRY
 /* This represents the case of redirection to a function implementation
  * residing within a DLL...
  */
# define __dllentry__(__suffix__)  __label__(__imp__,__suffix__)
# define __redirect__  *__dllentry__(DLLENTRY)

#else
 /* ...whereas this redirection to an alternative static library function,
  * or to a DLL implementation accessed via an import library trampoline.
  */
# ifndef REMAPPED
  /* No explicit entry point redirection specified; use the default entry
   * point name, within the "__mingw_" pseudo-namespace.
   */
#  define __mingw__(__suffix__)  __label__(__mingw_,__suffix__)
#  define REMAPPED  __mingw__(FUNCTION)
# endif
# define __redirect__  __entry__(REMAPPED)
.def	__entry__(REMAPPED); .scl 2; .type 32; .endef
#endif

.global __entry__(FUNCTION)
.def	__entry__(FUNCTION); .scl 2; .type 32; .endef

__entry__(FUNCTION): jmp __redirect__

/* $RCSfile$: end of file */
