View Single Post
gcc function order
Old
  (#1)
Lazy
Member
 
Lazy's Avatar
 
Status: Offline
Posts: 236
Join Date: Jan 2004
Location: Toronto, Ontario, Canada
Default gcc function order - 27-11-2005

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.
  
Reply With Quote