Absolute code in C

C and PASCAL (or any other high-level languages) in here please

Moderators: exxos, simonsunnyboy, Mug UK, Zorro 2, Moderator Team

User avatar
Arne
Captain Atari
Captain Atari
Posts: 400
Joined: Thu Nov 01, 2007 10:01 am

Absolute code in C

Postby Arne » Sun Feb 05, 2017 12:26 pm

Hi guys,

is there any chance to write absolute code (i.e. placing a compiled C binary at an absolute address)? Preferably in Pure C?
I think I have to write my own PCSTART.S. Correct?
I want to place the binary into ROM space at $E00000/$FC0000.
Image

czietz
Hardware Guru
Hardware Guru
Posts: 480
Joined: Tue May 24, 2016 6:47 pm

Re: Absolute code in C

Postby czietz » Sun Feb 05, 2017 12:45 pm

In gcc this would be a job for the linker script. See EmuTOS (emutos.ld) for an example.

I don't think this is possible with the Pure C linker. But you can of course always take the resulting PRG file and relocate it to an address of your choice. As you surely are aware, you must place DATA and BSS sections in RAM, obviously. Also, in order for this to work you'd have to check that executables generated by Pure C a) don't rely on TEXT, DATA and BSS being contiguous and b) don't do write accesses to the TEXT section, e.g. because some variables are placed there.

I think I have to write my own PCSTART.S. Correct?


Yes, because the standard startup assumes a correctly filled TOS basepage which you won't have when your code is executed from ROM.

czietz
Hardware Guru
Hardware Guru
Posts: 480
Joined: Tue May 24, 2016 6:47 pm

Re: Absolute code in C

Postby czietz » Sun Feb 05, 2017 4:03 pm

I've given this some more thought. I think you want to do one of these two things:

a) Run your code instead of TOS, e.g. from TOS-ROMs or as a diagnostic cartridge that gets started immediately after reset. In this case you have to initialize the MMU before you can even use RAM. (Any meaningful C program will use RAM.) How you do this is of course machine dependent; I once reverse engineered it for ST and STe. Once RAM is usable, you of course don't have to deal with an OS. So you can relocate your TEXT section to ROM and the DATA and BSS sections to an address in RAM of your choice. You have to copy the contents of the DATA section to RAM upon startup. (This is assuming Pure C code doesn't write to the TEXT section and is fine with dis-contiguous memory layout of the sections.) Also of course you have to initialize all other hardware yourself, then. Finally you have to be very careful with library calls, ensuring that they don't call any TOS functions internally.

b) Run your code from a cartridge after GEMDOS initialization or from the desktop. In this case, you cannot relocate your program at compile time, because you don't know which RAM address you can use for your data. (You will have to Malloc it upon startup.) My solution for this is to copy the whole program into Malloc'ed RAM, relocate it and run it like GEMDOS would have done. You can find this solution (with German explanation) under http://www.chzsoft.de/site/hardware/div ... -cartridge. However, I'm presently not sure if I uploaded the latest version.

Maybe you can explain a little what exactly you're planning to do.

User avatar
Arne
Captain Atari
Captain Atari
Posts: 400
Joined: Thu Nov 01, 2007 10:01 am

Re: Absolute code in C

Postby Arne » Mon Feb 06, 2017 8:24 am

Hi Christian,
thanks for your in-depth thoughts :cheers:
I think it will either end in using GCC (I :evil: GCC!) or sticking to plain assembler. Guess the latter.

And now for something completely different: if you point me to those CADstar files I can try to convert them.
Image

czietz
Hardware Guru
Hardware Guru
Posts: 480
Joined: Tue May 24, 2016 6:47 pm

Re: Absolute code in C

Postby czietz » Mon Feb 06, 2017 10:34 am

Note that most of the stuff about relocation, initializing the hardware and being careful with library calls also applies to programs compiled with gcc.

User avatar
mfro
Atari Super Hero
Atari Super Hero
Posts: 685
Joined: Thu Aug 02, 2012 10:33 am
Location: SW Germany

Re: Absolute code in C

Postby mfro » Mon Feb 06, 2017 5:23 pm

Pure-C *can* generate ROMable code. Did not try this for years, but basically providing some suitable values for Text, Data and BSS in the respective linker options should do the trick.

czietz
Hardware Guru
Hardware Guru
Posts: 480
Joined: Tue May 24, 2016 6:47 pm

Re: Absolute code in C

Postby czietz » Mon Feb 06, 2017 5:44 pm

Arne wrote:And now for something completely different: if you point me to those CADstar files I can try to convert them.


Thanks, though this would be somewhat easier if I had a way to contact you outside the forum. Can you please send me a PM with your e-mail address? Then I'll send those files to you.

czietz
Hardware Guru
Hardware Guru
Posts: 480
Joined: Tue May 24, 2016 6:47 pm

Re: Absolute code in C

Postby czietz » Mon Feb 06, 2017 6:09 pm

mfro wrote:Pure-C *can* generate ROMable code. Did not try this for years, but basically providing some suitable values for Text, Data and BSS in the respective linker options should do the trick.


Ah, indeed, I had never noticed that until now. However it's not exactly well documented what the results are -- at least in the online help. (Sadly I left my printed Pure C manual behind when I moved some time ago.)

User avatar
mfro
Atari Super Hero
Atari Super Hero
Posts: 685
Joined: Thu Aug 02, 2012 10:33 am
Location: SW Germany

Re: Absolute code in C

Postby mfro » Mon Feb 06, 2017 6:26 pm

czietz wrote:... Sadly I left my printed Pure C manual behind when I moved some time ago...


I should still have one somewhere. Let's see if I can find it ;)


Social Media

     

Return to “C / PASCAL etc.”

Who is online

Users browsing this forum: No registered users and 1 guest