gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Relocatable code

#69696 - HyperHacker - Wed Feb 01, 2006 8:48 am

I want to write a 'modular' program for DS. The idea is to be able to just load up a program into any random place in memory, and run it. This means the programs have to be relocatable, and not depend on being placed in a specific part of memory. How can I make such programs using DevkitPro?

Also is there a way to define a specific memory range for certain variables? Normally when you create variables, the compiler starts with the beginning of unused memory and puts them there. (Say your program takes up 0x02000000-0x020000FF, then if you define a byte variable it goes at 0x02000100, then a word variable will go at 0x02000102, etc.) How can I tell it to start putting them in a different part of memory? (Eg create some variables there, then add some statement that says to put the next one at 0x03000000, then 0x03000002, etc.)

#69716 - tepples - Wed Feb 01, 2006 2:47 pm

Use section attributes. To learn how, see "Q: How do I put large (> 16 KB) arrays into a program without it crashing?" in the beginners' FAQ.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#69755 - HyperHacker - Wed Feb 01, 2006 6:17 pm

Ah, thanks. Will that make functions relocatable too, or do I have to play with linker settings or change the code?

#69770 - sajiimori - Wed Feb 01, 2006 7:08 pm

I haven't done it yet myself, but the topic was being discussed here:
http://forum.gbadev.org/viewtopic.php?t=8051

#69811 - HyperHacker - Thu Feb 02, 2006 3:42 am

Yes, that's exactly what I want to do. Sounds complicated though. :-( There's no way to just tell the compiler/linker/whichever that I just want raw code (no headers or whatnot, I can make my own) that only uses relative addressing?

What I'm trying to do is make a simple multi-tasking OS for the DS. I can work out how to do the actual task switching, it's loading the programs into memory that's a problem, since they'll always expect to be placed at a specific address.

[edit] arm-elf-as has an option to generate PIC code... whatever that means. <_<

#69819 - sajiimori - Thu Feb 02, 2006 5:05 am

Yeah, I mentioned the PIC option in the other thread. I reckon that's what restricts the code to only use relative addressing. Get a raw binary the usual way, with objcopy or whatever else.

Most of the work is setting up an interface between a module and outside code.

I'd be curious about your results.

#69869 - Sausage Boy - Thu Feb 02, 2006 4:51 pm

I've been thinking about doing something like that for a while, a dynamic os thing would be so much cooler than statically compiled like WinDS etc... I didn't have much success, and then my ds broke, but I'm glad I'm not the only one who thought of this.
_________________
"no offense, but this is the gayest game ever"