lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Compare these cases (this is freebsd, clang, GNU ld):

% cc -c -fPIC func.c func1.c func2.c main.c myfunc2.c
% cc -shared -o libfunc.so func1.o func2.o       
% cc -Wl,-rpath,$PWD -o smain main.o myfunc2.o libfunc.so
% ./smain 
Hello from main
        Hello from func1
                Hello from myfunc2
        Back to func1
Back to main
                Hello from myfunc2
Back to main

So far so good; we overrode the library version of the function.

Alternative #1:

% cc -shared -Wl,-Bsymbolic -o libfunc.so func1.o func2.o
% ./smain 
Hello from main
        Hello from func1
                Hello from func2
        Back to func1
Back to main
                Hello from myfunc2
Back to main

Now we have two func2's, one visible to the library and one to the main
program.

Or we could do:

% printf '{ global: func1; local: func2; };\n' >libfunc.x
% cc -shared -Wl,--version-script,libfunc.x -o libfunc.so func1.o
func2.o
% ./smain
Hello from main
        Hello from func1
                Hello from func2
        Back to func1
Back to main
                Hello from myfunc2
Back to main

Again, two versions.

Let's do it without a version script (NOTE: this is what lua does on
platforms that support it, see LUAI_FUNC)

% printf '/void func2/s/^/__attribute__((visibility("hidden"))) /\nwq\n' | ed func2.c
% cc -c -fPIC func2.c
% cc -shared -o libfunc.so func1.o func2.o
% ./smain                                 
Hello from main
        Hello from func1
                Hello from func2
        Back to func1
Back to main
                Hello from myfunc2
Back to main

Two versions again.

The PostgreSQL project went through a more involved version of this mess
recently, to the extent that we now have a source file that exists for
no reason other than to detect mis-linking. (Our case is more complex
because it's to do with dynamic loading, but similar principles apply.)

-- 
Andrew.