473,385 Members | 1,798 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,385 software developers and data experts.

Hijack main function

11
Hello everybody,

Recently I got to do some initialization work just before first executable statement of every user C application.

For that,
Can we hijack the main function and write a wrapper around main? using LD_PRELOAD ??

The solution should be a portable one.

thanks in advance.


--
Ravi.T
Oct 26 '07 #1
8 2805
oler1s
671 Expert 512MB
Hijack the main function? The main function is already being called by the CRT. Which in itself is implementation specific. So unless you want to disable the CRT and reimplement everything yourself...I think you can see where I'm going with this.

Instead of trying to come up with a solution yourself, why don't you describe what you want in the end, what constraints you have to work with, and perhaps we can suggest solutions.
Oct 26 '07 #2
ravit
11
Ok.

I have a function foo() which I have to execute before any statement in the user application. then give the control to the user application.

I can give user a library which he can link to his application while preparing executable. With that facility I have to make the application to run my function foo() before any executable statement.

Should be portable to all Unix-like platforms.


I think the problem definition is clear. If not I please ask details.


--
Ravi.T
Oct 26 '07 #3
oler1s
671 Expert 512MB
Actually, your explanation isn't clear, so I'm making a few educated guesses based on what you have said.

As I understand it, you have a library and API you want others to use. One of the conditions for using that API is that there is a function foo() that must with certainty be called before any other API calls.

--- STOP HERE if I made a wrong guess. If you are trying to do something more fundamental like replace something in the CRT, then it's a whole lot more complicated ---

In any case, the solution is to inform the user about what he has to do. This should be in your API instructions.

You can't force the foo function to be called first. It's, as you might imagine, someone else's code. And with a bit of thought, you can see how you can't randomly hijack other people's code.
Oct 26 '07 #4
ashitpro
542 Expert 512MB
Have a look at "Linux Module Programming"

Every module has two sections
Say you module is test.o

initialize()
{
//Do your pre-formating here
//Call the application or Your_main_function
}
cleanup()
{

}
Your_main_function()
{

}
module_init(initialize); //registering functions
module_cleanup(cleanup);


Once you load this module, It will start executing initialize section
Here solution resides.
You can load this module as: insmod test.o and remove by: rmmod test.o
Oct 26 '07 #5
ravit
11
Not specific to Linux.
May be ported on AIX,SOlaris or anything of like Unix.
Oct 26 '07 #6
Banfa
9,065 Expert Mod 8TB
I'm with oler1s on this.

have a clear defined API and make sure the people using your code/library are aware that they need to call the initialisation routine as the first thing in their main.
Oct 26 '07 #7
ashitpro
542 Expert 512MB
Not specific to Linux.
May be ported on AIX,SOlaris or anything of like Unix.
Loadable module support is applicable for all POSIX complaint environments.
All you need is to make your module versatile to handle different platforms.
Oct 26 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
You have to write a shell with your main() and its call to foo(). Then you start the C application.

Everyone has to use your shell.

That's what Bill Gates did. Initially his main() was inside Windows. It was called by DOS. Bill's main() called foo() and a bunch of ther stuff and then called WinMain to start the application.

The user wrote WinMain().

So there is a precedent.

Too bad you aren't using C++. Since global variables are required to be created before main() starts you could call foo() from inside the constrcutor of a global object. That code would run before main(). All you would have to do is declare this global object somewhere in the program.
Oct 27 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sims | last post by:
Hi. Is it possible to 'subclass' php the same way you can subclass c++ a simple example would be... // CODE <?
192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
6
by: Jeff | last post by:
Guys, I posted this here because I want to monitor HTTP Responses and URL's and I thought the Webclasses might have been the correct area Ok so I dont really want to Hijack IE6 but what I...
1
by: endlesschuck | last post by:
Hello, I have a repeater which simply has the following: <input type="checkbox" id='chkRptItem_<%# DataBinder.Eval(Container.DataItem, "ID") %>' name="chkRptItems" value='<%#...
8
by: Michal Nazarewicz | last post by:
Hi, What does returning 0 from main() mean according to C89/C90 standard? I've found that in C99 it means successful termination (7.20.4.3p5) however as I'm editing book on C at Polish Wikibooks...
28
by: ravi | last post by:
Hello everybody, I am writing a small application which does some work before the user main function starts execution. I am trying to #define the main function. But the problem is that,
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.