473,573 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overriding a 'C' function in a static library (.a)

I have an executable which links to a static library (.a).
I want to provide a hook by overriding a function part of this static
library.
Eg: I have a function "int blkstart(int i)" in this static library.
I want to override this by having another function which is exactly
similar in signature including name.
When somebody call this function, control should automatically come to
my overriddent function. Inside this function, based on certain
conditions, I would like to call the original function in the static
library.

I tried using dlsym and dlopen APIs under SUN Solaris, but it didn't
work as I suppose it is not meant for static library.
Also I think this is the reason why it didn't work with static lib:

Since static lib (.a) doesn't have identity at runtime like dlls, only
the first available symbol was taken by linker during linking time and
ignores any duplicate symbols available in any other
static libs. At runtime, there is no way we can get the address of the
duplicate
function residing in the static library. Even running "nm <exe>" shows
only one
symbol which is the overloaded one (as in the make file, I have .o
file ahead of
-l<static-lib>). Basically at runtime, dlsym() can never resolve the
duplicate function address as to start with, it was part of a static
lib and linker ignores it as it has already resolved that symbol from
the overloaded function.
I would like to know if there is any other approach to fix this
problem. Note that I don't have freedom to convert this static lib to
dynamic one.
Nov 13 '05 #1
3 10928
mpatnam wrote:

I have an executable which links to a static library (.a).
I want to provide a hook by overriding a function part of this static
library.
The details of how to do this (or whether it can be done
at all) are specific to your platform, and really have nothing
to do with the C language. However:
Eg: I have a function "int blkstart(int i)" in this static library.
I want to override this by having another function which is exactly
similar in signature including name.
[...]
I would like to know if there is any other approach to fix this
problem. Note that I don't have freedom to convert this static lib to
dynamic one.


The C-and-nothing-but-C answer is to recompile all
of the calling program, adding

#define blkstart my_blkstart

at the beginning of each translation unit ("source file")
except for the file where my_blkstart is defined. Note
that this will work only for those sources you can recompile;
if the .a library itself contains un-recompilable functions
that call blkstart(), this technique will not catch those
calls.

--
Er*********@sun .com
Nov 13 '05 #2
On Wed, 12 Nov 2003 12:08:53 -0800, mpatnam wrote:
I have an executable which links to a static library (.a). I want to
provide a hook by overriding a function part of this static library.
Eg: I have a function "int blkstart(int i)" in this static library. I
want to override this by having another function which is exactly
similar in signature including name.
When somebody call this function, control should automatically come to
my overriddent function. Inside this function, based on certain
conditions, I would like to call the original function in the static
library.


Closest thing I can think of would be a macro. That is, do something
like:

#define blkstart mynewblkstart

then, in mynewblkstart.c :

#undef blkstart

int mynewblkstart(i nt i )
{
return blkstart( i );
}

You'll just have to ensure that anyone calling blkstart also has the
appropriate header included (to get the macro).
Nov 13 '05 #3
mk******@yahoo. com (mpatnam) wrote:
# I have an executable which links to a static library (.a).
# I want to provide a hook by overriding a function part of this static
# library.
# Eg: I have a function "int blkstart(int i)" in this static library.
# I want to override this by having another function which is exactly
# similar in signature including name.

Once an external symbol is defined in a running program, you'll have
little or no chance to redefine it.
An alternative scheme is to something like

plover.h
typedef int (*BlkStartFunc) (int);
void defineBlkStartF unc(BlkStartFun c);
plover.c
int default_blkstar t(int) {
your current blkstart routine goes here
};
static BlkStartFunc blkstart = default_blkstar t;
void defineBlkStartF unc(BlkStartFun c f) {
blkstart = f;
}

Thus if your client does nothing, they use your default blkstart. If they
want to override that, rather than depend on something tricky with the loader,
they just call defineBlkStartF unc with their own blkstart routine. Within
your code, you still call blkstart(x) the same as always.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I think that's kinda of personal; I don't think I should answer that.
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4300
by: mccoyn | last post by:
I'm porting an old project to use .NET. When I try to link in a static library (.lib) that has a single managed class in it. I get the following errors: LINK : error LNK2020: unresolved token (0600000E) PatInfo::.ctor LINK : error LNK2020: unresolved token (0600000F) PatInfo::Finalize LINK : fatal error LNK1120: 2 unresolved externals I've...
1
320
by: rahul | last post by:
Hi, I am working on developing a C++ application and caught with a strange issue: I am having 4 dynamic libraries for e.g. a.dll, b.dll, c.dll and d.dll. Each of these is linked with a static library called msg.lib. Now in all but one (d.dll) dynamic libraries there is a function called "LOGTOFILE". In d.dll this function is replaced...
1
6254
by: Vijai Kalyan | last post by:
I am not sure if this OT, but I will go ahead and post. If it has to be posted elsewhere let me know. -vijai. ------ I have a class declared as follows: #pragma once // a simple reference counter implementation
4
3542
by: newest newbie | last post by:
I'm new to using c#, but what I'm trying to do is create a library control containing a form which in turn contains a Windows application that I have defined in another library. I'm able to do it if my application which I put in the form is a c++ dll like so: private extern static void RunMyApp(IntPtr handle);
6
983
by: bauerwo | last post by:
Dear all I'm a newbie to C, C++, and Visual C++ .Net, so if there is a better place to ask this question, please let me know. Using Visual C++ .Net, I haven compiled and linked a static library "myLib.lib", providing the function "myFunc". I can access it without any problem in another C++ program "myMain" by writing extern double...
1
1128
by: Bonj | last post by:
Hello I have created what you would think would be the simplest of static libraries, and still I cannot get it to link correctly. In the static library (main.c) I have: long __cdecl getnum(long testinput) { return 42; }
15
2302
by: Notre Poubelle | last post by:
Hello, I have a large legacy MFC application. As is typical, there is an executable along with several MFC DLLs. One of these DLLs is created by staticly linking in many libraries resulting in one very large DLL that has the bulk of the code. I've downloaded the MFCWinFormsSample.EXE and have noticed that the main app could stay as a...
6
10346
by: =?Utf-8?B?R29yZG8=?= | last post by:
Hello everyone, I've been trying for some time now to move to C++/CLI, but I have several large legacy C++ static libraries I need to use. When I set up a simple solution with a C++/CLI Winforms app and a C++ native static library, they work well together _unless_ I have, it seems, any static variables defined in any function in the native...
6
5604
by: Even | last post by:
Hi all, As far as I know, relative address will be assigned by program at link time.Right? Ok, here we go. So if we use a static library, it will give the program an relative address(i mean the function address) at link time. Also the code of the library will load into the program. Go on. Now if we use a dynamic library, what it will...
0
8021
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8202
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7787
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8065
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5593
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3732
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1041
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.