flat assembler
Message board for the users of flat assembler.

Index > Non-x86 architectures > FASMARM elf relocation

Author
Thread Post new topic Reply to topic
rrq



Joined: 17 May 2021
Posts: 9
rrq 24 Jul 2025, 01:18
How do I link a FASMARM (armv7) binary with Linux gcc object modules?

Ultimately I want/need a flat binary that has start address 0x0 to be using code from gcc compile C code and collated into a "standard gcc-ar" library. With "format elf", it's possible to use "gcc" and "objcopy" to get loading happen and a resulting flat binary, but all the "extrn" references remain resolved as 0x0.

I believe there was something in progress for this some year ago, but maybe that stalled? But maybe there something available.

Perhaps it needs my DIY (like eg a second compile run after some "nm" and "sed" scripting)...
Post 24 Jul 2025, 01:18
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20708
Location: In your JS exploiting you and your system
revolution 24 Jul 2025, 02:18
Relocations for ELF format in the current version are not working.

However ... I do have a branch that has working relocations for ARM32 and ARM64 but I can't release it at this time. I am negotiating with all the parties involved to try and get a new version with the new features made public. I haven't been given a timeline for the approval process though. Sad I realise this doesn't help for your situation, but perhaps next year (?) the answer will be more positive?
Post 24 Jul 2025, 02:18
View user's profile Send private message Visit poster's website Reply with quote
rrq



Joined: 17 May 2021
Posts: 9
rrq 24 Jul 2025, 02:48
Thanks. Fair enough. I do appreciate all your effort, and a clear answer is very good!
(I'll set up something for myself using "nm" and "sed" meanwhile)
Post 24 Jul 2025, 02:48
View user's profile Send private message Reply with quote
rrq



Joined: 17 May 2021
Posts: 9
rrq 30 Jul 2025, 14:49
Just in case someone is interested: this is a brief description of my DIY approach for linking fasmarm code with gcc code; more specifically for making a flat binary with fasmarm as main program and some support functions together with gcc-compiled object modules and static libraries with other functions.

The fasmarm code has "extrn" directives for calls to gcc C code and it has "public" directives to support calls into to it. However the "extrn" directives are wrapped into using a "LINK" macro so that the external labels get duly defined for the actual load addresses.
Code:
macro LINK reladdr, name {
  name = (0x#reladdr - 0x000100d8 ) ; relative default _start address
  extrn `name as _#name
}    

The load addresses (reladdr) are picked from the "nm" output of the linked executable and the Makefile is set up to verify this prior to extracting the flat binary from the executable. All in all the Makefile includes the following generic rules (in addition to the specific dependences):
Code:
.SUFFIXES: # Disable all standard generic rules

# Generic rule to compile C into .o (ELF 32-bit LSB relocatable)
## (the same as the standard rule)
%.o: %.c
        (COMPILE.c) $(OUTPUT_OPTION) $<

# Generic rule to compile fasmarm into .elf (ELF 32-bit LSB relocatable)
## with the additional dependency on $@-externals.S
%.elf: %.S %-externals.S
        fasmarm $(FASMARGS) $< $@

# Generic rule to link .elf and .o into .bin (ELF 32-bit LSB executable)
LINKARGS = -nostartfiles -nolibc -nostdlib -nodefaultlibs \
        -pie -static -static-pie -fno-plt -Wl,--no-wchar-size-warning
%.bin: %.elf
       gcc $(CFLAGS) $(LINKARGS) $^ -o $@ $(LIBS)

# Generic rule to obtain the flat binary (after confirming %-externals.S)
## with a shell command to report entry points in $@.bin
define NMLIST
nm -t x $@.bin  2> /dev/null | sed '/ T /!d;s/^//'
endef
## and an awk script to confirm $@-externals.S with that nm output
define AWKPROG
$$1 == "LINK" { LN = LN sprintf( "\nLINK %s, %s", MAP[$$3], $$3 ); \
                if ( MAP[$$3] != $$2 ) { ER = 1 } ; next } \
$$2=="T" { MAP[$$3]=$$1 ; next } \
END { if ( ER != 1 ) exit 0; \
      print "*** $@-external.S should be:" LN | "LC_ALL=C sort"; exit 1; }
endef
##
%: %.bin
        ( $(NMLIST) ; tr , ' ' < $@-externals.S ) | awk '$(AWKPROG)'
        objcopy -O binary -j .text -j .rodata $^ $@
    

Note that $(LIBS) may include object module libraries as per normal; the linking resolves all their references and only the references out of the fasmarm module(s) need the special attention via the "$@-externals.S" file.

To make it all work the fasmarm main program, myprog.S, has a lead-in for an armv7 elf object module even though the end product is a flat binary. The lead-in is as follows:
Code:
format elf
processor CPU32_ALL
use32
postpone { include 'myprog-externals.inc' }
section '.text' executable
public _start
_start:    

The Makefile will build and extract the flat binary if the LINK directives in $@-externals.S conform with the ELF executable. Otherwise it builds the ELF executable and then it fails before extraction, and prints how the LINK file should look.

EDIT: corrected LINK macro to use 0x000100d8 (had it as 0x000100b8 before, which was wrong).
Post 30 Jul 2025, 14:49
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.