473,383 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 10864
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(int 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 defineBlkStartFunc(BlkStartFunc);
plover.c
int default_blkstart(int) {
your current blkstart routine goes here
};
static BlkStartFunc blkstart = default_blkstart;
void defineBlkStartFunc(BlkStartFunc 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 defineBlkStartFunc 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
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...
1
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...
1
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...
4
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...
6
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...
1
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...
15
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...
6
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...
6
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.