LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-12-2004, 06:23 AM   #1
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
About startup routine


Hi!
Can any body explain me how to change the _startup routine for the C programs. Is there any side effects if i write my own or is it just like an ordinary function.
Thanks in advance,
Bye,
Eshwar.
 
Old 05-12-2004, 11:35 AM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
i dont understand your question. _startup just sets up main's stack frame and whatnot. look at the assembly code.
 
Old 05-12-2004, 03:13 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
Just incase you don't know, if you pass -S to gcc it will spit out the asm code for you to view.
 
Old 05-13-2004, 01:33 AM   #4
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Original Poster
Rep: Reputation: 15
_startup routine calls main by default , From there onwards our program gets executed. What i want to do is i just want to change the _start up routine such that it calls my function ( other than main). Once i have seen a person doing it. He compiled the code with -start option for Gcc. He wrote a routine with the name same as startup routine. And with the compilation He made the system call his own start up routine instead of systems default.
If any one know it please let me know.
Bye,
Eshwar.
 
Old 05-13-2004, 07:37 AM   #5
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
you can do so by givin -e switch to the linker and using whatever function you want as the entry point
gcc -c mymain.c
ld -e mymain mymain.c -L<lib dir > <libs you want to link with>

or gcc -Wl,e and so on
 
Old 05-13-2004, 08:10 AM   #6
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Original Poster
Rep: Reputation: 15
Hi shir shir!
I am getting error, When i try to execute the a.out i got after linking.

[root@localhost eshwar]# cc -c link.c
[root@localhost eshwar]# ld -e name link.o -L/usr/lib -lc
[root@localhost eshwar]# ./a.out
bash: ./a.out: /usr/lib/libc.so.1: bad ELF interpreter: No such file or directory
[root@localhost eshwar]#


link.c is as follows
-------------------------
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>

void name()
{

link( "Project" , "Proj" );

if( errno == EPERM )
{
printf( "Cannot Link Directories\n" );
}

}

Can you explain me, Why am i getting that error?

Thanks in advance
Bye,
Eshwar.
 
Old 05-13-2004, 08:21 AM   #7
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Original Poster
Rep: Reputation: 15
And using other method ,

[root@localhost eshwar]# gcc -Wl,--entry=name link.c
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.text+0x18): In function `_start':
../sysdeps/i386/elf/start.S:77: undefined reference to `main'
collect2: ld returned 1 exit status

I am getting linker error.
 
Old 05-13-2004, 11:57 AM   #8
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
how bout add a main function then
 
Old 05-13-2004, 10:47 PM   #9
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Original Poster
Rep: Reputation: 15
I want to write program without main
 
Old 05-14-2004, 12:17 AM   #10
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
the only way i've ever written programs w/o main is pure asm programs, and i've always had to build those with 'as' and 'ld', not gcc. perhaps u should start reading 'info gcc'
 
Old 05-14-2004, 02:27 AM   #11
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
root@localhost eshwar]# gcc -Wl,--entry=name link.c
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.text+0x18): In function `_start':
../sysdeps/i386/elf/start.S:77: undefined reference to `main'
collect2: ld returned 1 exit status

i get a feeling that the linker is not even getting your Wl.--entry switches...
i'd have to dig deeper to see why this could be happening ....

[root@localhost eshwar]# cc -c link.c
[root@localhost eshwar]# ld -e name link.o -L/usr/lib -lc
[root@localhost eshwar]# ./a.out
bash: ./a.out: /usr/lib/libc.so.1: bad ELF interpreter: No such file or directory

this might be because your binary while is an ELF, it is having totally mucked up addresses, thus, even tho it is dependant on libc, it is not able to map into the address space of the process..i guess itf you look at the objdump -x <bin>..out puts for a normal binary and your modified binary, you'll get what i am trying to say...i am not an expert at this..so this is the best i can help...

question..why do you want to write a program with ure own entry function..
main is as good as any?
 
Old 05-14-2004, 03:12 AM   #12
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Original Poster
Rep: Reputation: 15
We were doing a project, In that project we have to do some initializations to the processes which wants to use this project. So we want to implement a start up routine which will do this initialization and later calls the main of the processes. We have found this as the better way of doing with out putting any restrictions on the application programs.
If anyone knows how to do it please let me know.
 
Old 05-14-2004, 04:50 AM   #13
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
i dunno but ..by doing this.. you are not going to have any other main cos this is going to be your main entry function..so this is not what you are looking for....
_start is going to publish this new entry function as your main entry function...so your process shall already be up and running...

so i think there could be a way of changing those processes into threads....if it is not too difficult and your project can make do with threads....
 
Old 05-15-2004, 07:10 AM   #14
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Original Poster
Rep: Reputation: 15
At last i got it , Thanks Friends.
 
Old 05-15-2004, 11:58 AM   #15
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Well! If you got it please post some code. I know that I wouldn't mind knowing how its done (though I don't think I will ever use it! ).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
encryption in CGI routine rblampain Linux - Security 3 08-12-2005 10:55 PM
Interrupt routine... Elric of Grans Programming 2 07-05-2005 07:11 PM
Please confirm backup routine LLS Linux - General 4 09-30-2003 11:30 AM
File under routine, mundane, etc. JasonSmead Programming 1 07-31-2003 03:40 PM
Shutdown routine with power button bfought Linux - Newbie 2 09-09-2002 06:23 PM


All times are GMT -5. The time now is 03:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration