LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-01-2004, 06:51 AM   #1
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
Want to execute program with out main


Hi!
I have a doubt, That is Can we write a C program with out main?
I think its possible. If possible I think we have to configure the loader such that it loads PC with the address of our newmain at the end of loading.
If that is right method, Can anybody explain me how to change or configure that loader (on linux)?
 
Old 03-01-2004, 07:57 AM   #2
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
I'm pretty sure you can, but why? If nothing else, just call whatever you want to be your entry point from main.

Code:
int main(int argc, char **argv)
{
    return some_other_function(argc, argv);
}
 
Old 03-01-2004, 08:37 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
From 'info ld'

Quote:
`-e ENTRY'
`--entry=ENTRY'
Use ENTRY as the explicit symbol for beginning execution of your
program, rather than the default entry point. If there is no
symbol named ENTRY, the linker will try to parse ENTRY as a number,
and use that as the entry address (the number will be interpreted
in base 10; you may use a leading `0x' for base 16, or a leading
`0' for base 8). *Note Entry Point::, for a discussion of defaults
and other ways of specifying the entry point.
 
Old 03-01-2004, 09:14 AM   #4
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
There's some problems with even compiling a C program without a mail.

[shadow@alien tmp]$ gcc test.c
/usr/lib/gcc-lib/i386-redhat-linux/3.3.2/../../../crt1.o(.text+0x18): In function `_start':
: undefined reference to `main'
collect2: ld returned 1 exit status

crt1.o is the startup code for the program, its automaticly linked with your binary when you.. erh, link your binary =)

running "nm" will on crt.o will show the following symbols (and missing ones).

00000000 D __data_start
00000000 W data_start
00000000 R _fp_hw
00000004 R _IO_stdin_used
U __libc_csu_fini
U __libc_csu_init
U __libc_start_main
U main
00000000 T _start

Im not totaly sure who actulay calls _start() and __libc_start_main(). But I do know that _start calls your main() in your program. So if you not intrested in replaying crt.o its hard to do a (executable) program thats missing the main symbol.

You can tell GCC not to include crt.o when compiling (using the flag: -mno-crt0)

Why do you want to skip main?
 
Old 03-01-2004, 10:03 AM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Hmmm... according to the info for LD, you can use the following is used to determine what the starting point is.

Quote:
File: ld.info, Node: Entry Point, Next: File Commands, Up: Simple Commands

Setting the Entry Point
-----------------------

The first instruction to execute in a program is called the "entry
point". You can use the `ENTRY' linker script command to set the entry
point. The argument is a symbol name:
ENTRY(SYMBOL)

There are several ways to set the entry point. The linker will set
the entry point by trying each of the following methods in order, and
stopping when one of them succeeds:
* the `-e' ENTRY command-line option;

* the `ENTRY(SYMBOL)' command in a linker script;

* the value of the symbol `start', if defined;

* the address of the first byte of the `.text' section, if present;

* The address `0'.
However, when I try the following:
Code:
// blah.c
#include <stdio.h>

int MyMain()
{
    printf("Hello\n");

    return 0;
}

$ gcc -o blah -eMyMain blah.c
/usr/lib/gcc-lib/i486-slackware-linux/3.2.3/../../../crt1.o(.text+0x18): In func
tion `_start':
: undefined reference to `main'
collect2: ld returned 1 exit status
Or I can get it to compile like so:
Code:
$ gcc -o blah.o -c blah.c
$ ld -o blah blah.o -lc -eMyMain
$ ./blah
-bash: ./blah: No such file or directory

$ ls -l blah
-rwxr-xr-x    1 deiussum users          2040 Mar  1 09:56 blah*
I tried the -mno-crt0 option and got this:

Code:
$ gcc -o blah -eMyMain blah.c -mno-crt0
cc1: invalid option `no-crt0'
So, I'm not sure anymore. VC++ has an option like /entryPoint:SomeEntryPoint, but with Windows programming you often have either a main() or a WinMain() or a DLLMain()...
 
  


Reply



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
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM
how can i remove the program in the main menu? Txlyo Red Hat 6 11-17-2003 04:13 AM
What is the main used program for coding Java? Tyir Programming 2 10-20-2003 08:10 PM
How does one add a program to the main panel of RH8? gsmonk Linux - Distributions 3 03-10-2003 03:58 PM
execute a program athenerx Programming 3 05-23-2002 04:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:13 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration