Skip to content

Commit

Permalink
Support clients defining weak symbols of opengx functions (devkitPro#70)
Browse files Browse the repository at this point in the history
With this change, the translation unit (TU) of functions.c gets brought
into the list of units used in the linking when gc_gl.c's TU is used, as
confirmed by nm:

    functions.o:
    ...
    00000000 B _ogx_functions_c
    00000000 T ogx_get_proc_address
    ...

    gc_gl.o:
    ...
    00000000 D _ogx_force_proctable
             U _ogx_functions_c
    00000000 T ogx_initialize
    ...

As the comment in the code explains it, this allows us to support the
scenario where we use opengx in a client library (such as SDL) without
forcing a dependency on it in the client application. More precisely,
this change is made to support the case where an application *does use*
opengx via libSDL, to make so that the weak symbol for
ogx_get_proc_address() defined in libSDL gets overridden by the real one
(since the application is unlikely to use this function directly, but
it's indirectly used when SDL_GL_GetProcAddress() is called).
  • Loading branch information
mardy authored Sep 28, 2024
1 parent 14a593b commit b5406c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ typedef struct {
void *address;
} ProcMap;

int _ogx_functions_c = 0; /* referenced by gc_gl.c, see the comment in there */

#define PROC(name) { #name, name }
static const ProcMap s_proc_map[] = {
//PROC(glAccum),
Expand Down
7 changes: 7 additions & 0 deletions src/gc_gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ typedef struct
char _ogx_log_level = 0;
static GXTexObj s_zbuffer_texture;
static uint8_t s_zbuffer_texels[2 * 32] ATTRIBUTE_ALIGN(32);
/* Force the inclusion of functions.c's TU in the build when GL functions are
* used. In this way, if a client library (such as SDL) defines weak symbols
* for the opengx functions it uses, a client application which actually uses
* opengx will link and use its real implementation; at the same time, a client
* which does not use OpenGL is not forced to link with opengx. */
extern int _ogx_functions_c;
void *_ogx_force_proctable = &_ogx_functions_c;

static void draw_arrays_general(DrawMode gxmode, int first, int count, int ne,
int color_provide, int texen);
Expand Down

0 comments on commit b5406c8

Please sign in to comment.