flat assembler
Message board for the users of flat assembler.

Index > Main > Offset for relocation table

Author
Thread Post new topic Reply to topic
Coldy



Joined: 16 Jan 2023
Posts: 3
Coldy 16 Jan 2023, 15:11
Hello!
I need build my own relocation table in binary format.
Is there a way (magic keyword or something) to get offsets in code to build a relocation table?
Any ideas are welocme. Tnx.
Post 16 Jan 2023, 15:11
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8267
Location: Kraków, Poland
Tomasz Grysztar 16 Jan 2023, 16:07
This is something that was requested several times for fasm 1, but I never found a satisfying way of implementing it there, so it became one of the things that led to the design of fasm g. The auxiliary documentation for fasmg has examples of collecting relocation information in the How are the labels processed? section. There is also a chapter about generating relocations from scratch in my fasmg-based PE tutorial. And for more advanced examples you may look at how various fasm-compatible output formatters are constructed.
Post 16 Jan 2023, 16:07
View user's profile Send private message Visit poster's website Reply with quote
Coldy



Joined: 16 Jan 2023
Posts: 3
Coldy 17 Jan 2023, 08:10
Thanks for the quick response! Yes, the question was how to do it in fasm. fasm g is a new tool for me, I don't know how compatible it is with the fasm codebase.
Post 17 Jan 2023, 08:10
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8267
Location: Kraków, Poland
Tomasz Grysztar 17 Jan 2023, 10:05
The example of adapting to assemble HeavyThing should give a rough idea about the level of compatibility. And a project like FASMW can be assembled by fasmg with literally no changes to source, just set up include variable to point to the compatibility headers. If your code does not use complex macro features etc. then generally fasmg should be able to handle it easily.
Post 17 Jan 2023, 10:05
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8267
Location: Kraków, Poland
Tomasz Grysztar 17 Jan 2023, 10:23
A very simple example of generating relocation information for x86 code with fasmg:
Code:
include 'cpu/p6.inc'
use32

format binary as 'bin'

element RELOCATABLE_BASE
org RELOCATABLE_BASE + $

virtual as 'rel' ; save relocations into separate file
  Relocations::
end virtual

macro dd? data&
        iterate unit, data
                match ?, unit
                        dd ?
                else if unit relativeto RELOCATABLE_BASE
                        virtual Relocations
                                dd $% ; record raw offset
                        end virtual
                        dd unit-RELOCATABLE_BASE
                else
                        dd unit
                end if
        end iterate
end macro

start:
        mov     eax,start
        mov     edx,[start]    
The "dd" macro is based on the one from the PE tutorial. For better performance a CALM-based variant would be advisable, like the one used by fasm-compatible PE formatter.

If you need ALIGN, you can take the universal implementation and set it up by telling it what is the guaranteed alignment of your relocatable base:
Code:
include 'align.inc'
align.assume RELOCATABLE_BASE, 16    
Post 17 Jan 2023, 10:23
View user's profile Send private message Visit poster's website Reply with quote
Coldy



Joined: 16 Jan 2023
Posts: 3
Coldy 18 Jan 2023, 12:53
Tomasz Grysztar wrote:
...
but I never found a satisfying way of implementing it there...


I see that in fasm can add a new rule for relocaton block (for example) like this
Code:
relocation relocation_offsets_table
; User defined relocation table here 
end relocation
    

relocation_offsets_table is an array of dword as offsets in the code where relocation need to be applied

Such a table is probably generates for the PE format, but no access to it in birary format
That would be enough
Post 18 Jan 2023, 12:53
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8267
Location: Kraków, Poland
Tomasz Grysztar 18 Jan 2023, 13:21
Since fasm also handles formats like COFF and ELF, I needed a design that would be able to handle all the options, including generation of all kinds of relocations relative to various bases. I was thinking about it a lot back in the day and these designs laid foundation for fasmg's linear polynomials, with ELEMENT directive and operators like ELEMENTOF/METADATAOF. But this is something that cannot be back-ported into fasm 1 in a usable way.

In short: fasmg was my answer to requests for this and several other features (like namespaces). I did deliver it all and more, and I did even back-port some of the easier features into fasm 1, but for many others it's not possible.

You could try tweaking fasm 1 to allow for some very narrow solution, like what you suggested, but I'm unlikely to work on anything like that - what I already designed and implemented is ways ahead.
Post 18 Jan 2023, 13:21
View user's profile Send private message Visit poster's website Reply with quote
sylware



Joined: 23 Oct 2020
Posts: 371
Location: Marseille/France
sylware 19 Jan 2023, 12:54
BTW, did you hear about the new ELF relocation for PIE programs?

I wonder if this new relocation is more planned obsolescence than anything else.
Post 19 Jan 2023, 12:54
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3892
Location: vpcmipstrm
bitRAKE 19 Jan 2023, 21:27
I did patch fasm to allow custom relocation table generation. Although it's not a long term solution, but rather a hack to show how one might do it with minimal effort. Embarassed

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 19 Jan 2023, 21:27
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.