Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
87 user(s) are online (73 user(s) are browsing Forums)

Members: 2
Guests: 85

Mr_byte, samo79, more...

Support us!

Headlines

 
  Register To Post  

(1) 2 »
Programming question about shared objects
Home away from home
Home away from home


See User information
The question comes from my problems comes when i add plugins support to LodePaint. LodePaint just use .so plugins , and in main code do such thinks like:
#include <dlfcn.h>

namespace
{
  
template<typename F>
  
void do_dlsym(FfunctionPointervoiddl, const charname//to avoid warning
 
"dereferencing type-punned pointer will break
 strict-aliasing rulesdereferencing type-punned 
pointer will break strict-aliasing rules" 
when using dlsym
  
{
    
//*(void **)(&functionPointer) = dlsym(dl, name); //this gives the warning
    
    
union F funcvoidobj; } alias//Wikipedia's way to avoid the warning!
    
alias.obj dlsym(dlname);
    
functionPointer alias.func;

  }
}

FilterPlugin::FilterPlugin(const std::stringdynamicLib
GlobalToolSettingssettings, const lpi::gui::IGUIDrawergeom)
AOperationFilterUtil(settingstruetrue)
isvalid(true)
dialog(0)
p_pluginFreeUnsignedChar(0)
p_pluginFreeFloat(0)
p_filterGetName(0)
p_filterExecuteRGBA8(0)
p_filterExecuteRGBA32F(0)
p_filterResizeRGBA8(0)
p_filterResizeRGBA32F(0)
p_filterExecuteGrey8(0)
p_filterExecuteGrey32F(0)
p_filterResizeGrey8(0)
p_filterResizeGrey32F(0)
p_filterGetParameterNameEnum(0)
p_filterGetParameterNameEnumChoice(0)
p_filterGetParameterNameInt(0)
p_filterGetParameterNameDouble(0)
p_filterGetParameterNameBool(0)
p_filterGetParameterNameColor(0)
p_filterGetParameterNameString(0)
{
  
dl dlopen(dynamicLib.c_str(), RTLD_LOCAL RTLD_LAZY);
  if(
dl)
  {
    
int (*p_getLodePaintPluginType)();
    
do_dlsym(p_getLodePaintPluginTypedl"getLodePaintPluginType");
    if(
p_getLodePaintPluginType && p_getLodePaintPluginType() == 1)
    {
      
//filter plugin identifier found!
    
}
    else
    {
      
isvalid false;
      
dlclose(dl);
      
dl 0;
    }
  }
  
  if(
dl)
  {
    
std::cout<<"Loading Filter Plugin: " << dynamicLib << std::endl;
    
    
do_dlsym(p_filterGetNamedl"filterGetName");
    if(
p_filterGetName)
    {
      
char name[80];
      
p_filterGetName(name80);
      
label name;
    }
    else
    {
      
std::cout << "filterGetName not found "<< dlerror()<<std::endl;
      
isvalid false;
    }

    
do_dlsym(p_pluginFreeUnsignedChardl"pluginFreeUnsignedChar");
    
do_dlsym(p_pluginFreeFloatdl"pluginFreeFloat");
    
    
do_dlsym(p_filterExecuteRGBA8dl"filterExecuteRGBA8");
    
do_dlsym(p_filterExecuteRGBA32Fdl"filterExecuteRGBA32F");
    
do_dlsym(p_filterResizeRGBA8dl"filterResizeRGBA8");
    
do_dlsym(p_filterResizeRGBA32Fdl"filterResizeRGBA32F");
    
do_dlsym(p_filterExecuteGrey8dl"filterExecuteGrey8");
    
do_dlsym(p_filterExecuteGrey32Fdl"filterExecuteGrey32F");
    
do_dlsym(p_filterResizeGrey8dl"filterResizeGrey8");
    
do_dlsym(p_filterResizeGrey32Fdl"filterResizeGrey32F");

    
    if(((
p_filterResizeRGBA8 || p_filterResizeGrey8) && 
!
p_pluginFreeUnsignedChar) || ((p_filterResizeRGBA32F ||
 
p_filterResizeGrey32F) && !p_pluginFreeFloat))
    {
      
std::cout << "filter doesn't include pluginFreeUnsignedChar or
 pluginFreeFloat function"
<<std::endl;
      
isvalid false;
    }

    
do_dlsym(p_filterGetParameterCountEnumdl"filterGetParameterCountEnum");
    
do_dlsym(p_filterGetParameterNameEnumdl"filterGetParameterNameEnum");
    
do_dlsym(p_filterGetParameterNameEnumChoicedl"filterGetParameterNameEnumChoice");
    
do_dlsym(p_filterGetParameterCountIntdl"filterGetParameterCountInt");
    
do_dlsym(p_filterGetParameterNameIntdl"filterGetParameterNameInt");
    
do_dlsym(p_filterGetParameterCountDoubledl"filterGetParameterCountDouble");
    
do_dlsym(p_filterGetParameterNameDoubledl"filterGetParameterNameDouble");
    
do_dlsym(p_filterGetParameterCountBooldl"filterGetParameterCountBool");
    
do_dlsym(p_filterGetParameterNameBooldl"filterGetParameterNameBool");
    
do_dlsym(p_filterGetParameterCountColordl"filterGetParameterCountColor");
    
do_dlsym(p_filterGetParameterNameColordl"filterGetParameterNameColor");
    
do_dlsym(p_filterGetParameterCountStringdl"filterGetParameterCountString");


    
do_dlsym(p_filterGetParameterNameStringdl"filterGetParameterNameString");

    
bool hasUChar p_filterExecuteRGBA8 != || p_filterResizeRGBA8 != ||
 
p_filterExecuteGrey8 != || p_filterResizeGrey8 != 0;
    
bool hasFloat p_filterExecuteRGBA32F != || p_filterResizeRGBA32F != ||
 
p_filterExecuteGrey32F != || p_filterResizeGrey32F != 0;
    
    if(!
hasUChar && !hasFloat)
    {
      
std::cout << "no filterExecuteRGBA8, filterExecuteRGBA32F, filterResizeRGBA8, 
filterResizeRGBA32F, filterExecuteGrey8, filterExecuteGrey32F, filterResizeGrey8, or 
filterResizeGrey32F  function in filter plugin" 
<< std::endl;
      
isvalid false;
    }
    
    
initGUI(geom);

  }
  else 
isvalid false;
  
  if(!
isvalid && dl != 0)
  {
    
dlclose(dl);
    
dl 0;
  }
}

FilterPlugin::~FilterPlugin()
{
  if(
dldlclose(dl);
  if(
dialogdelete dialog;
}


I compile plugins with such string:

Quote:

gcc plugin.c -shared -o plugin.so


The problem is: When i compile main program (lodepaint) with static linking (without -use-dyndl), then, when i choice plugin it crashes , on libc.so (can provide crash log if its need it).

If i compile main program with -use-dyndl, then everything works fine. But still i curios, why it crashes when main programm are static, and, should it be like that or not.

Maybe need some kind of flags passed to gcc for amiga-specific support or kind ?

For example for linux, the same program compiles statically, and plugins dinamically as should, and all works fine.

Can also provide little example codes, crashlogs and alt if anyone in interest.


Edited by Rigo on 2010/6/13 8:48:03
Edited by Rigo on 2010/6/13 8:49:36
Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: Programming question about shared objects
Amigans Defender
Amigans Defender


See User information
@kas1e
Check the "Shared object plugins" section in "SDK:AmigaOS 4.1 SDK.pdf" and see if that helps you out.

ExecSG Team Lead
Go to top
Re: Programming question about shared objects
Amigans Defender
Amigans Defender


See User information
@kas1e

Your plugins need to be compiled with -fPIC, although I don't think this is causing the problem.

Usually crashes like that are due to shared objects linking to static objects. If you are using shared objects at all, it is better to compile everything -use-dynld anyway.

Go to top
Re: Programming question about shared objects
Home away from home
Home away from home


See User information
@Chris
Yes, for now i compile its as -use-dynld, but still in interest why (techically) it not works when i call .so from static binary.

Quote:

Usually crashes like that are due to shared objects linking to static objects.


So, it's indeed have place. Why it happens ? Bug, or aos4 implementation of shared objects ?

@ssolie
Thanks, found something interesting for tests.


Edited by kas1e on 2010/6/13 9:04:01
Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: Programming question about shared objects
Not too shy to talk
Not too shy to talk


See User information
@kas1e

I have no experience these dl-functions but did you have a look at utilitybase ? These threads are interesting :

http://utilitybase.com/forum/index.ph ... thread&forum=2&topic=1562

http://utilitybase.com/forum/index.ph ... thread&forum=2&topic=1660

Go to top
Re: Programming question about shared objects
Amigans Defender
Amigans Defender


See User information
@corto

add the -fPIC flag to teh plugin file and however i think you should remove (rename) libstdc++.so and use the static version of library when use c++ files. I've some experience with a bug in libstdc++.so. If you use the static one the version disappear..
I must file a bugzilla entry

i'm really tired...
Go to top
Re: Programming question about shared objects
Amigans Defender
Amigans Defender


See User information
@kas1e

Well, shared objects can't be linked to static libraries, so when they try to call something in a static library, they crash as the function is in unallocated memory.

Maybe you need to link your plugins explicitly to libraries they need? If you aren't doing that, it may only be due to being called from an app which has the required shared objects loaded already, that causes them to find them. Link the main executable statically and the shared objects won't be in memory.

Go to top
Re: Programming question about shared objects
Home away from home
Home away from home


See User information
@Chris

Still its strange, becasue on linux it works fine. I mean usage of .so plugins from the main, statically linked, programm. For now i test it like that:

compilation of main programm:
Quote:

-lSDL -lGL -Wl.-export-dynamic -ldl -lauto


compilation of plugin:
Quote:

gcc exampleFilter.c -shared -fPIC -o exampleFilter.so


Then, when i try to run plugin from programm, i have such error:
Symbol info:
Instruction pointer 0x7EBC269C belongs to module "libc.so" (PowerPC
Symbol__NewlibCall 0x4 in section 9 offset 0x00000CC8

Stack trace
:
    
libc.so:__NewlibCall()+0x4 (section 9 0xcc8)
    
exampleFilter.so:filterExecuteRGBA8()+0x11C (section 6 0x7f4)
    
lodepaint:_ZN12FilterPlugin4doitER12TextureRGBA8()+0x564 (section 1 0x1e341c)
    
lodepaint:_ZN20AOperationFilterUtil17operateForPreviewER12TextureRGBA8()+0x24 (section 1 0x188df8)
    
lodepaint:_ZN23FilterDialogWithPreview13updatePreviewEv()+0x7C (section 1 0x247744)
    
lodepaint:_Z8doFilterR11MainProgramR16IOperationFilterR9Paintings()+0x53C (section 1 0x14f440)
    
lodepaint:_Z16handleFilterMenuRP16IOperationFilterRN3lpi3gui12MenuVerticalERSt6vectorIPS4_SaIS7
_EER7FiltersRNS2_6IInputER11MainProgramR9Paintings
()+0x188 (section 1 0x14f820)
    
lodepaint:_Z14runMainProgramRKSs()+0x4B40 (section 1 0x1543a8)
    
lodepaint:_Z6doMainiPPc()+0x4A0 (section 1 0x158408)
    
lodepaint:main()+0x100 (section 1 0x1589f0)
    
native kernel module newlib.library.kmod+0x00001f44
    native kernel module newlib
.library.kmod+0x00002b90
    native kernel module newlib
.library.kmod+0x00002d54
    lodepaint
:_start()+0x170 (section 1 0x170)
    
native kernel module dos.library.kmod+0x0001b524
    native kernel module kernel
+0x0003ef08
    native kernel module kernel
+0x0003ef88


If i add just -use-dynld for the main programm, all works fine. But still i in interst: it is possible somehow make it works when main programm static, or not.

From the PDF on which ssolie point me, i see that is possible, but strange why for me it crashes then (i passed all those -fPIC and -Wl.-export-dynamic flags)


Edited by Rigo on 2010/6/13 11:40:01
Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: Programming question about shared objects
Home away from home
Home away from home


See User information
Btw, did it make any sense, if .so plugins have stack cookie inside, or not ?

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: Programming question about shared objects
Amigans Defender
Amigans Defender


See User information
@kas1e

Quote:
If i add just -use-dynld for the main programm, all works fine. But still i in interst: it is possible somehow make it works when main programm static, or not.

From the PDF on which ssolie point me, i see that is possible, but strange why for me it crashes then (i passed all those -fPIC and -Wl.-export-dynamic flags)


Yes, technically calling shared objects from a static-linked binary is fine. My little libdl archive on Aminet has an example which can be compiled static or -use-dynld and will work both ways.

Check with readelf -d that your .so plugins list libc.so as a required object.

Go to top
Re: Programming question about shared objects
Home away from home
Home away from home


See User information
@Chris
I cheked my plugins, and one of them:
Quote:

0x00000001 (NEEDED) Shared library: [libpthread.so]
0x00000001 (NEEDED) Shared library: [libstdc++.so]
0x00000001 (NEEDED) Shared library: [libgcc.so]
0x00000001 (NEEDED) Shared library: [libc.so]


other one:
Quote:

0x00000001 (NEEDED) Shared library: [libz.so.1]
0x00000001 (NEEDED) Shared library: [libstdc++.so]
0x00000001 (NEEDED) Shared library: [libgcc.so]
0x00000001 (NEEDED) Shared library: [libc.so]


and another one:
Quote:

0x00000001 (NEEDED) Shared library: [libpthread.so]
0x00000001 (NEEDED) Shared library: [libgcc.so]
0x00000001 (NEEDED) Shared library: [libc.so]


So, its everythere.. And when i compile main programm as static, always have such error.

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: Programming question about shared objects
Home away from home
Home away from home


See User information
@Chris
And now more funny. I have for all plugins, such crashlog when close main programm:

Quote:

Symbol info:
Instruction pointer 0x7ED8C808 belongs to module "" (HUNK/Kickstart)

Stack trace:

native kernel module newlib.library.kmod+0x000165fc
native kernel module newlib.library.kmod+0x00001f48
native kernel module newlib.library.kmod+0x00002b90
native kernel module newlib.library.kmod+0x00002d54
lodepaint:_start()+0x170 (section 9 @ 0x170)
native kernel module dos.library.kmod+0x0001b524
native kernel module kernel+0x0003ef08
native kernel module kernel+0x0003ef88


I add stack cookie to each plugin, but still, error the same, and happenes when i close main programm. What can cause such problem ? (and error, always the same, for any plugin, on always the same addresses).

How can i trace where is problem ?

For one plugin that crash happenes when i only run/close main programm with loading plugin. Other plugin crashes when i load programm, open a file, then exit, and then have such crash.

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: Programming question about shared objects
Home away from home
Home away from home


See User information
Btw, can i somehow mix .a files (which was not compiled with the -fPIC option) for my .so plugin ?

For example i have libjpeg_plugin.so, which i compile like:
Quote:

gcc -fPIC -shared libjpeg_plugin.c -o libjpeg_plugin.so -ljpeg


So i use libjpeg.a, which was compiled without -fPIC option, do i need to build my own copy of jpeg.a , with -fPIC option ?

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: Programming question about shared objects
Amigans Defender
Amigans Defender


See User information
@kas1e

you can mix .a and .so files in your exe but you cannot mix an already linked lib with the same lib in the shared form.

F.e.
you could have that libpng.so is linked with libz.so

You cannot link libz.a to your project otherwise you will get into real troubles..

i'm really tired...
Go to top
Re: Programming question about shared objects
Home away from home
Home away from home


See User information
@afxgroup

Why i ask about, because looks like when main binary are dinamic, GR cant say you exactly where crash is happenes, and refer just to newlib.kmod only. But when its static - you can see where is crash in programm happenes. But for see that, you need main static binary and your shared plugin , which, you cant link with any .a looks like (so, pluging then make no sense in that terms, just becasue plugins usually done for all those 3d party lib.a which you add to your plugin). But when all is -use-dynld, then all is ok (except that you cant normally detect where is crashes happenes)

For now i just create a plugin without any references to any .a , and then trying to catch error, but, in generall, its bad that i cant see where is crash happenes when main binary is dinamic (or can ? )

I also try to use GDB, but it crashes like hell, and just cant load or attach from GR that main dinamic binary at all.

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: Programming question about shared objects
Amigans Defender
Amigans Defender


See User information
@kas1e

Quote:
So i use libjpeg.a, which was compiled without -fPIC option, do i need to build my own copy of jpeg.a , with -fPIC option ?


No, you should be linking against libjpeg.so. I have a shiny new build of it here with the includes, if you don't have it.

Go to top
Re: Programming question about shared objects
Just popping in
Just popping in


See User information
If you have built libjpeg.so you should upload it somewhere.
I could not find it anywhere. It has disappeared from os4depot. And Wesnoth is also linked against libjpeg.so ... :|

Go to top
Re: Programming question about shared objects
Home away from home
Home away from home


See User information
@phx

For plugins i use just static one , but i have in my SOBJ: also libjpeg.so which can be used for imho (if you need it can upload somethere)

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: Programming question about shared objects
Not too shy to talk
Not too shy to talk


See User information

Go to top
Re: Programming question about shared objects
Amigans Defender
Amigans Defender


See User information
@kas1e

1) you must compile all pluginsg with -fPIC
gcc -shared -fPIC myplugin.c -o myplugin.so

2) rename (delete..) all your libstdc++.so in the SDK: dir and use the static one. You will have bigger exe but that works correctly

3) if you are using pluginst that use internally OS4 functions and so they need IExec/IDOS and so on interfaces opened with -lauto on main exe you MUST use -Wl,--export-dynamic into the .exe

4) if a simply plugin crash the problem is not surely a stack but there si a problem somewhere. use always -gstabs when compile and use addr2line to track the last valid function that is called before newlib/elf and so on

5) when possible link also dependecies with your plugin. so if your plugine needs f.e. libpng compile it with

gcc -use-dynld -shared -fPIC myplugin.c -o myplugin.so -lpng12

Edit:

6) if you want to use dlopen() and other dl function you muste use -use-dynld otherwise the static version of libdl will crash everything..

maybe something else..

i'm really tired...
Go to top

  Register To Post
(1) 2 »

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project