English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 21 December 2018, 21:01   #1
mcgeezer
Registered User

 
Join Date: Oct 2017
Location: Sunderland, England
Posts: 1,488
Making relocatable code - vasm

Hi all,

I've made use of the basereg directive in vasm today and it is really useful as it results in much cleaner/faster code.

To improve things further I'm looking at making my project completely PC relative and have been wondering the best way to reference memory pointers.

Consider the following snippet.

Code:
START: moveq #0,d0
           lea DATA(pc),a0
           rts

DATA:  dc.l   START
The pointer stored in DATA to START obviously breaks relocating the program, but is there a way in VASM to perhaps create offsets in the longword pointers instead of direct addresses?

The idea is that I need to load my program from a bootstrap that has allocated memory.

I hope this makes sense... I perhaps do not have some of the right terminology but I hope you know what I mean.

Geezer
mcgeezer is offline  
Old 21 December 2018, 22:44   #2
ross
Per aspera ad astra

ross's Avatar
 
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 49
Posts: 2,183
Hi Geezer, it's not exactly what you ask for, but you can view it as way to not complicate your life .
The easiest thing (and that does not even require the modification of the assembled executable or coding style) is to create a parser for standard Amiga hunk files.
So: load the binary to an absolute address and parse/relocate using the VASM's generated HUNK_RELOC32.
You can even turns the Amiga executable into something more compressed and personal, but you'd lose the advantage of using LoadSeg() for standard system loader cases.
ross is offline  
Old 21 December 2018, 22:56   #3
jotd
This cat is no more
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 48
Posts: 3,275
I can share some hunk relocation source code too. It's not trivial. You can also use relative pointers:

Code:
START: moveq #0,d0
           lea DATA(pc),a0
           move.l   (a0),a1
           add.l      a0,a1
           rts

DATA:  dc.l   START-DATA
if close enough you can use word-sized relative pointers, like whdload does
jotd is offline  
Old 21 December 2018, 23:02   #4
mcgeezer
Registered User

 
Join Date: Oct 2017
Location: Sunderland, England
Posts: 1,488
So basically I simply allocate memory from the bootstrap, load in my (almost pc relative game code) and then fix-up all of the addresses using HUNK_RELOC32.

I take it HUNK_RELOC32 contains a list of all of the base offset addresses which I then just need to adjust.

Sound about right?
mcgeezer is offline  
Old 21 December 2018, 23:07   #5
jotd
This cat is no more
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 48
Posts: 3,275
if you only use RELOC32 that should work. There are more annoying hunk types, like BSS, which require you to move the code around. Wepl published the source for his RELOC program. I have some in-place relocate code too, which isn't perfect, but works.

Basically, load the executable where you allocated the memory, move back the data by $20 bytes, locate the reloc table and apply relocations.
jotd is offline  
Old 22 December 2018, 00:08   #6
Galahad/FLT
Going nowhere

Galahad/FLT's Avatar
 
Join Date: Oct 2001
Location: United Kingdom
Age: 46
Posts: 7,315
I gave a basic relocater to JOTD a while ago, its also how the Starquake cracktro is done.
Galahad/FLT is offline  
Old 22 December 2018, 00:36   #7
alpine9000
Registered User

 
Join Date: Mar 2016
Location: Australia
Posts: 729
vlink has another option for relocation. It’s simpler than hunk and you can always relocate in place.

Check out this post from phx: http://eab.abime.net/showpost.php?p=1149568&postcount=4
alpine9000 is online now  
Old 22 December 2018, 00:48   #8
phx
Natteravn

phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 1,479
If you want a more compact solution, do it like I did in my games (Solid Gold, Sqrxz, Trap Runner). Use vlink with the -brawseg option to write separate files for each segment (you define segments, and the sections which belong to them in a simple linker script). Usually you want to define a segment for Chip-RAM and one for the rest (program, data), which you load independently.

But the best feature is: by adding -q to -brawseg vlink will write some simple reloc tables into additional files. The format is: first longword defines number of relocs, followed by 32-bit offsets to relocate. Look into loader.asm from Solid Gold and the Makefile. Feel free to contact me (email) if you need any help.

Thanks, alpine9000, for the link. I was about to write it all again.
phx is offline  
Old 22 December 2018, 00:56   #9
mcgeezer
Registered User

 
Join Date: Oct 2017
Location: Sunderland, England
Posts: 1,488
Quote:
Originally Posted by phx View Post
If you want a more compact solution, do it like I did in my games (Solid Gold, Sqrxz, Trap Runner). Use vlink with the -brawseg option to write separate files for each segment (you define segments, and the sections which belong to them in a simple linker script). Usually you want to define a segment for Chip-RAM and one for the rest (program, data), which you load independently.

But the best feature is: by adding -q to -brawseg vlink will write some simple reloc tables into additional files. The format is: first longword defines number of relocs, followed by 32-bit offsets to relocate. Look into loader.asm from Solid Gold and the Makefile. Feel free to contact me (email) if you need any help.

Thanks, alpine9000, for the link. I was about to write it all again.
Thanks for this phx... it looks very promising and straight forward, I'll look into those options asap.

Great help as usual by all here!
mcgeezer is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
vasm dead code removal dalton Coders. Asm / Hardware 17 01 October 2013 15:31
Help linking VASM object code clenched Coders. Asm / Hardware 2 24 May 2013 23:32
BootDisk, a tool for making bootable disks from binary code Leffmann Coders. General 1 12 March 2011 17:41
Can making a backup kill a game? Some kind of protection code? fitzsteve support.Games 12 19 March 2010 03:59
Advice on disassembling non-relocatable code pmc Coders. General 15 15 May 2009 15:35

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 On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 03:13.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Page generated in 0.07146 seconds with 15 queries