I'm building a new 64bit Debian Squeeze server, and Postgres 8.4 reports the well documented 'undefined __gxx_personality_v0' error when I try to restore a database.
The lib builds/installs fine.
However, the source is c, not c++ which seems to be where __gxx_personality_v0 belongs.
The code is from a 32bit Etch/postgres 8.1 environmnet.
I'm kind of stuck - but I bet the solution is very simple!
Test to see whether the issue is actually related to PostgreSQL, or if it is a problem with your library build. Here's a simple program you can use to dlopen() your library and resolve a symbol. Compile it with:
gcc dlopentest.c -o dlopentest -ldl
... and run it as:
./dlopentest /path/to/my/lib.so somesymbol
or for more info prefix LD_DEBUG=symbols (for other options LD_DEBUG=help) eg:
LD_DEBUG=symbols ./dlopentest /path/to/my/lib somesymbol
Which symbol to look for depends on your code and what it's for. You haven't provided enough information to say.
This test program won't work with any library that requires symbols from the postgresql executable in order to load and init, so if your code is (for example) a procedural language it won't load. Most simple modules load fine, though.
You should also examine the output of:
ldd /path/to/your/library.so
to see if it's linking to libstdc++ or anything else you don't expect.
Here's the test program:
// Compile with a c99 compiler; I don't use oldstyle declarations
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char * argv[]) {
if (argc != 3) {
printf("Usage: %s /path/to/lib/to/load symbol_to_resolve\n", argv[0]);
printf(" eg: %s libc.so.6 gettimeofday\n", argv[0]);
return 1;
}
char * err;
const char * const libname = argv[1];
const char * const symname = argv[2];
dlerror(); // clear dl error state before starting work
void * libhandle = dlopen(libname, RTLD_LAZY);
if ( (err = dlerror()) != NULL ) {
printf("Failed to load library: %s\n", err);
return 2;
}
void * symhandle = dlsym(libhandle, symname);
if ( (err = dlerror()) != NULL ) {
printf("Failed to load symbol: %s\n", err);
return 2;
}
printf("Successfully retrieved symbol %s from library %s\n", symname, libname);
// Optional since we're existing, but good for testing:
if ( (err = dlerror()) != NULL ) {
printf("Failed to close lib during exit: %s\n", err);
return 2;
}
return 0;
}
Related
I'm writing a package manager for the Termux terminal emulator on android using the APK format. The program is written in C and uses various arguments like 'sync', and 'remove'. However, the function I have written doesn't recognize the argument I have written for the name of the package to 'sync'. 'sync' is meant to download an apk from the fdroid repositories and open it using xdg-open (not yet implemented) using the name of the apk given in the arguments.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void syncapk(char * apkname);
int main(int argc, char **argv)
{
if(argc==1) {
printf("Displaying help screen.\n");
} else if(argc>=2) {
if(strncmp(argv[1], "sync\n", 4) == 0) {
syncapk(argv[2]);
} else if(strncmp(argv[1], "upgrade", 7) == 0) {
printf("Upgrading all packages!\n");
} else if(strncmp(argv[1], "remove", 6) == 0) {
printf("Removing package!\n");
}
}
return 0;
}
void syncapk(char * apkname) {
printf("Syncing package: %s!\n", apkname);
char * synccmd = "fdroidcl download %s", apkname;
system(synccmd);
}
GCC (my compiler) says that the argument (the name of the apk I wish to download from the repositories) is 'redeclared as a different symbol'. I am fairly new to writing programs in C so feel free to critique other things, not just the problem itself and whether I could take a different approach completely.
This doesn't do what you think it does:
char * synccmd = "fdroidcl download %s", apkname;
This is defining a variable of type char * named synccmd and a variable of type char named apkname. The latter conflicts with the parameter of the same name, hence the error.
If you want to build a formatted string, you need to use sprintf to do that:
char synccmd[100];
sprintf(synccmd, "fdroidcl download %s", apkname);
Hello.
First I'm sorry for my ita-english.
I want use gwan with aerospike but when run the servlet...problem.
I start with this example.c of aerospike. In file example.c I put gwan.h and this is the output ./gwan:
loading
hello.cs: to use .cs scripts, install C#..
hello.lua: to use .lua scripts, install Lua
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Linking example.c: undefined symbol: g_namespace
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To run G-WAN, you must fix the error(s) or remove this Servlet.
Inside example.c:
#include "gwan.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <aerospike/aerospike.h>
#include <aerospike/aerospike_key.h>
#include <aerospike/aerospike_query.h>
#include <aerospike/as_error.h>
#include <aerospike/as_key.h>
#include <aerospike/as_query.h>
#include <aerospike/as_record.h>
#include <aerospike/as_status.h>
#include <aerospike/as_val.h>
#include "example_utils.h"
const char TEST_INDEX_NAME[] = "test-bin-index";
bool query_cb(const as_val* p_val, void* udata);
void cleanup(aerospike* p_as);
bool insert_records(aerospike* p_as);
int
main(int argc, char* argv[])
{
if (! example_get_opts(argc, argv, EXAMPLE_MULTI_KEY_OPTS)) {
exit(-1);
}
aerospike as;
example_connect_to_aerospike(&as);
example_remove_test_records(&as);
example_remove_index(&as, TEST_INDEX_NAME);
if (! example_create_integer_index(&as, "test-bin", TEST_INDEX_NAME))
{
cleanup(&as);
exit(-1);
}
if (! insert_records(&as)) {
cleanup(&as);
exit(-1);
}
if (! example_read_test_records(&as)) {
cleanup(&as);
exit(-1);
}
as_error err;
as_query query;
as_query_init(&query, g_namespace, g_set);
as_query_where_inita(&query, 1);
as_query_where(&query, "test-bin", as_integer_equals(7));
LOG("executing query: where test-bin = 7");
if (aerospike_query_foreach(&as, &err, NULL, &query, query_cb, NULL)
!= AEROSPIKE_OK) {
LOG("aerospike_query_foreach() returned %d - %s", err.code,
err.message);
as_query_destroy(&query);
cleanup(&as);
exit(-1);
}
LOG("query executed");
as_query_destroy(&query);
cleanup(&as);
LOG("simple query example successfully completed");
return 0;
}
bool
query_cb(const as_val* p_val, void* udata)
{
if (! p_val) {
LOG("query callback returned null - query is complete");
return true;
}
as_record* p_rec = as_record_fromval(p_val);
if (! p_rec) {
LOG("query callback returned non-as_record object");
return true;
}
LOG("query callback returned record:");
example_dump_record(p_rec);
return true;
}
void
cleanup(aerospike* p_as)
{
example_remove_test_records(p_as);
example_remove_index(p_as, TEST_INDEX_NAME);
example_cleanup(p_as);
}
bool
insert_records(aerospike* p_as)
{
set
as_record rec;
as_record_inita(&rec, 1);
for (uint32_t i = 0; i < g_n_keys; i++) {
as_error err;
as_key key;
as_key_init_int64(&key, g_namespace, g_set, (int64_t)i);
as_record_set_int64(&rec, "test-bin", (int64_t)i);
if (aerospike_key_put(p_as, &err, NULL, &key, &rec) != AEROSPIKE_OK) {
LOG("aerospike_key_put() returned %d - %s", err.code, err.message);
return false;
}
}
LOG("insert succeeded");
return true;
}
how can connect aerospike with gwan?
Thank you
You need to #pragma link your aerospike library, and make sure all your required header files are in the right place. See G-WAN FAQ or read example code in the G-WAN tarball.
Also, in G-WAN the return code of the main function will be used as HTTP response code, so avoid return -1;.
undefined symbol: g_namespace
the error message is clear. As long as this variable is undefined your C servlet won't compile.
I don't know your library but this variable is probably defined in a library include file - or must be defined by the end user (you). Check the library documentation.
Detailed steps to run Aerospike C-client example with G-WAN,
Download and extract G-WAN server tar on your system
You can start the G-WAN server using ./gwan script present in extracted folder, e.g. ./gwan_linux64-bit/
Get Aerospike C-client from https://github.com/aerospike/aerospike-client-c, and install on your system
Copy example.c to ./gwan_linux64-bit/0.0.0.0_8080/#0.0.0.0/csp/
Make following changes to example.c,
Add following #pragma directive,
#pragma include "/home/user/aerospike-client-c/examples/utils/src/include/"
This will help search example_utils.h, which is necessary for all the example scripts in C-client.
Add following #pragma directive,
#pragma link "/home/user/aerospike-client-c/examples/utils/src/main/example_utils.c"
We shall have to link example_utils.c, as it has definitions of all util functions used in example scripts.
Make changes to the return values. Retun proper HTTP error codes.
Now, you are good to go. Run ./gwan server and access your webservice through browser, http://127.0.0.1:8080/?example.c
I created a dll of a simple function in Matlab using this command:
mcc -t -L C -W lib:testfunctionLib -T link:lib testfunction.m libmmfile.mlib
The simple function looks like:
function y = testfunction(x)
y = x + 10;
end
I need to call the dll via c-code. This is what i'm using to get the result of the computation with the dll-function into a textfile:
#include <windows.h>
#include <stdio.h>
int main()
{
int z = 1;
FILE *Testfile;
typedef int(*BinaryFunction_t) (int);
BinaryFunction_t AddNumbers;
int result;
BOOL fFreeResult;
HINSTANCE hinstLib = LoadLibraryA("testfunctionLib.dll");
if (hinstLib != NULL)
{
AddNumbers = (BinaryFunction_t)GetProcAddress(hinstLib, "testfunction");
if (AddNumbers != NULL)
result = (*AddNumbers) (z);
fFreeResult = FreeLibrary(hinstLib);
Testfile = fopen("Testfile.txt", "a");
fprintf(Testfile, "%i\n", result);
fclose(Testfile);
}
else
{
Testfile = fopen("Testfile.txt", "a");
fprintf(Testfile, "NOT");
fclose(Testfile);
}
}
I always get a 'NOT' in my textfile because the c-code can't extract the function out of the dll. Why doesn't this work? The c-code for getting the dll-function should be ok, i tested it with a dll created within visual studio.
I know that the library failed to load, but i don't receive any error message.. I also tried to build the dll by the Matlab Coder. I get i lot of different files (c,h,etc) and a single dll-file called testfunction. But including this dll is still not working..
I have a written code for Windows, and now am trying to port to Linux, but not sure if it is possible or not.
My Question: I am trying to callback to a function that is in the executable from the shared library. How do I do that?
This is the compilation output:
Output:
build/release-linux-ppc64/ioq3ded.ppc64
build/release-linux-ppc64/ioquake3.ppc64
build/release-linux-ppc64/baseq3/cgameppc64.so
build/release-linux-ppc64/baseq3/qagameppc64.so
build/release-linux-ppc64/baseq3/uippc64.so
build/release-linux-ppc64/missionpack/cgameppc64.so
build/release-linux-ppc64/missionpack/qagameppc64.so
build/release-linux-ppc64/missionpack/uippc64.so
make[2]: Entering directory `/r/home7/XXX/ioquake3'
make[2]: `build/release-linux-ppc64/ioq3ded.ppc64' is up to date.
make[2]: `build/release-linux-ppc64/ioquake3.ppc64' is up to date.
LD build/release-linux-ppc64/baseq3/cgameppc64.so
LD build/release-linux-ppc64/baseq3/qagameppc64.so
LD build/release-linux-ppc64/baseq3/uippc64.so
LD build/release-linux-ppc64/missionpack/cgameppc64.so
LD build/release-linux-ppc64/missionpack/qagameppc64.so
LD build/release-linux-ppc64/missionpack/uippc64.so
This is the command that I run to execute my program, that is why I figured "ioq3ded.ppc64" to be my EXECUTABLE.
./ioq3ded.ppc64 +set fs_game Mod +set sv_pure 0 +set vm_game 0 +set vm_cgame 0 +set vm_ui 0 +set dedicated 1 +exec something_117.cfg
This is the code for Windows:
//Called function
__declspec(dllexport) void UnLinkLinkroutingcache( void )
{
//code
}
//Callback location
#include<windows.h>
typedef void (* fUnLinkLinkroutingcache_t)( void );
void fUnLinkLinkroutingcache( fUnLinkLinkroutingcache_t pUnLinkLinkroutingcache )
{
pUnLinkLinkroutingcache(); return;
}
fUnLinkLinkroutingcache_t pUnLinkLinkroutingcache;
void callUnlinkLink(void)
{
HMODULE hLib;
//fUnLinkLinkroutingcache_t pUnLinkLinkroutingcache;
hLib = LoadLibrary(TEXT("ioquake3.exe"));
if (hLib == NULL)
{
//Module not found, permission denied, ...
return 0; //inform caller of error
}
pUnLinkLinkroutingcache = (fUnLinkLinkroutingcache_t)GetProcAddress(hLib, TEXT("UnLinkLinkroutingcache"));
if ( pUnLinkLinkroutingcache == NULL)
{
return 0;
}
fUnLinkLinkroutingcache(pUnLinkLinkroutingcache);
}
I tried to port this code to Linux, but I can't seem to load the exe.
//Called function
extern void UnLinkLinkroutingcache( void )
//Callback location
void callUnlinkLink(void)
{
void* handle;
void (*initializer)(void);
FILE *fp;//zgzg2020
fp = fopen("HereIsMe.txt", "a");
if( fp != NULL )
{
fprintf(fp, "%d", 1 );
fprintf(fp, "\n" );
}
fclose(fp);
handle = dlopen("./ioq3ded.ppc64", RTLD_LAZY);
if(handle == NULL) {
// report error ...
fp = fopen("ICantFindFile.txt", "a");
if( fp != NULL )
{
fprintf(fp, "%d", 1 );
fprintf(fp, "\n" );
}
fclose(fp);
exit(1);return;
} else {
initializer = dlsym(handle,"UnLinkLinkroutingcache");
if(initializer == NULL) {
// report error ...
fp = fopen("ICantFindFfunction.txt", "a");
if( fp != NULL )
{
fprintf(fp, "%d", 1 );
fprintf(fp, "\n" );
}
fclose(fp);
exit(1);return;
} else {
// cast initializer to its proper type and use
(*initializer)();
}
// use the result in a call to dlsym
}
}
Unfortunately, dlopen will not suffice for this purpose as many other posters have said. It's only capable of loading shared libraries. If you pass NULL instead of a shared library path, you can look up any symbols that the run-time linker has not stripped from the main executable image (there aren't many).
However, if you want to call functions within an executable compiled without -rdynamic, try LD_PRELOAD'ing your own shared library that (on startup) locates the real path to your executable file via /proc/self/exe, memory maps it, and then reads the symbol table on that mapping (or other executable headers) to locate and interpret symbols within the main executable.
From there, it's just a matter of defining a few function pointers in your shared library that match the prototype of the function you wish to call, assigning them the location of the symbol in the binary, and calling the function pointer as normal.
You can't dlopen executables in Linux -- the function only works on shared libraries (.so, roughly equivalent to .dll on Windows).
What are you trying to accomplish?
If ioq3ded.ppc64 is really a .so (doesn't look like it), have you checked what it exports?
Have you checked errno when you get a null return from dlopen (assuming it is a .so file here, as #duskwuff says, everything else would fail)?
The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program. Which does mean it has to be dynamic library.
I need to load two dynamic libraries and there is one function name confliction. So I use the the command "objcopy --redefine-sym add=new_add libmy_test.so libmy_test_new.so" to modify the symbol name.
But it still reports "Error: ./libmy_test_new.so: undefined symbol: new_add"
The following are my test codes.
void *lib_handle2 = dlopen("./libmy_test_new.so", RTLD_NOW);
if (NULL == lib_handle2) {
printf("Error: %s\n", dlerror());
goto err1;
}
fp_add f_add2 = dlsym(lib_handle2, "new_add");
if (NULL == f_add2) {
printf("Error: %s\n", dlerror());
goto err2;
}
According to this page, it seems it does not work with dynamic symbol. More explanation are available in the original thread. If you want to use both symbol, then you somehow need to relink one of the libraries. However if you want only one of the symbol, then linking order might help you.
Maybe the solution is creating a wrapper library, in which you dlsopen the two libs, create two new symbol, and assign them using dlsym with the correct handle.
void *lib_handle1 = dlopen("./lib1.so", RTLD_NOW);
void *lib_handle2 = dlopen("./lib2.so", RTLD_NOW);
fp_add f_add1 = dlsym((lib_handle1, "add");
fp_add f_add2 = dlsym(lib_handle2, "add");
Of course it does not solve the problem of call generated inside the libraries.