.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   gcc function order (http://forums.bots-united.com/showthread.php?t=4707)

Lazy 27-11-2005 01:48

gcc function order
 
Is there a way to make sure a function is the first one to be compiled and first one written to the executable/object?
Since I'm not using any libraries passing -nostdlib to gcc will require _start instead of main.
This works fine except if _start is not the first function the whole thing messes up.
What happens is...
Compile->Link->copy code to raw binary

I did try a hack where I put a function at the beginning of the source file which called _start but after adding a few more functions that broke too.
My goal is the smallest possible binary size, which is why I'm working directly with hardware instead of using a high level library...
Code:

#include <gba_base.h>
#include <gba_video.h>

#define FRAMEBUFFER ( ( volatile u16* ) 0x06000000 )
#define RGB15( r, g, b ) ( ( r << 10 ) + ( g << 5 ) + ( b ) )
#define ru32 register u32
#define ru16 register u16

// HACKHACKHACK
// MAKE SURE THIS IS THE FIRST FUNCTION IN THE ONLY SOURCE FILE
void _start( void );
static void GCCStartHack( void ) {
        _start( );
}

// Draws a 1 pixel wide line at the given coordinates with the given height
static void line( ru32 iX, ru32 iY, ru32 iHeight, ru16 iColor ) {
        ru32 iTarget = iY + iHeight;

        for ( ; iY < iTarget; iY++ )
                FRAMEBUFFER[ iX + ( iY * 240 ) ] = iColor;
}

static void game_drawborder( void ) {
        line( 70, 10, 150, RGB15( 15, 15, 15 ) );
        line( 170, 10, 150, RGB15( 15, 15, 15 ) );
}

// DO NOT ADD ANY CODE BEFORE THIS FUNCTION
void _start( void ) {
        REG_DISPCNT = MODE_3 | BG2_ENABLE;        // Set framebuffer mode

        game_drawborder( );

// Enter infinite loop
main_loop:
        goto main_loop;
}

I haven't gotten far but that compiles to around 60 bytes, it'd be smaller but for some reason thumb code isn't working.
Here is my cheap makefile...
Code:

CC=arm-elf-gcc
OBJCOPY=arm-elf-objcopy
CFLAGS=-Os -fomit-frame-pointer -mcpu=arm7tdmi -I/home/lazy/dev/tools/devkitARM/libgba/include -c
LFLAGS=-mno-fpu -nostdlib

minitris.o:
        $(CC) $(CFLAGS) -o minitris.o minitris.c

minitris.elf: minitris.o
        $(CC) $(LFLAGS) -o minitris.elf minitris.o

minitris.gba: minitris.elf
        $(OBJCOPY) -O binary minitris.elf minitris.gba

all: clean minitris.gba

clean:
        rm -fv *.o
        rm -fv *.elf
        rm -fv *.gba

Any help is appreciated since I'm new to gcc and this won't work unless _start is at the beginning of the binary.

Whistler 27-11-2005 04:45

Re: gcc function order
 
not sure if a custom link script (like that one in Metamod-p) will work, but I have never done this so I don't know further.

Onno Kreuzinger 28-11-2005 21:15

Re: gcc function order
 
ouch, w/o any libc you will be very much on you own, no one does this for long :)

Prehaps give dietclib a try, it's commonly used in embedded and mirco linux systems (4 mb ram+).

--OK

Lazy 28-11-2005 21:29

Re: gcc function order
 
Actually, the only ram I have to work with is 256kb - and even that is cut down since in my case it is shared with the program itself.
http://en.wikipedia.org/wiki/Gba

I just want to see if I can code a simple game in less than 256 bytes. I know it has been done for x86 but I wanted to see if it's possible with C since all of the ones I looked at were done in assembly.

But it's not going anywhere since gcc seems to put _start wherever it wants which results in some other function getting called and the system not being initialized.

I'm going to try a few more things later tonight and see what happens.

Onno Kreuzinger 29-11-2005 22:23

Re: gcc function order
 
well, then good luck :)

Pierre-Marie Baty 30-11-2005 14:06

Re: gcc function order
 
You should ask the guys at the demo scene (start at www.pouet.net if they have a forum there). They know how to do heaps of things in 256 bytes.

Lazy 02-12-2005 01:08

Re: gcc function order
 
Bah.
Got too frustrated with it so I decided to port sdl sopwith to the nintendo ds.
Its working very well on real hardware so once I get the input and sound finished I can release it into the wild.
:)


All times are GMT +2. The time now is 19:27.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.