3

I have some C code that is implemented in a ROM. I'd like to link an application against that code, so I can save some space (as my application won't have to include the ROM code as it's already in the ROM). My question is about how to do this in an efficient way. I'm using the GCC toolchain for ARM (arm-none-eabi-*)

The long explanation:

I'll quote Oracle's definition of a stub object:

A stub object is a shared object, built entirely from mapfiles, that supplies the same linking interface as the real object, while containing no code or data.

As a normal part of the linking process, we can link against libraries, either static or dynamic. As I'm using an embedded target with no MMU, I'm using static libraries. When linking against static libraries, the library code is copied to my final application image. In my problem here, I have code that is implemented in a ROM. I have the final binary and the map file of this ROM. What I want is to call this ROM code from my application, so I don't have to include it again.

So, let's say that I have the following code in ROM

int some_rom_function(void)
{
    // do something here, doesn't really matter what
}

I go to the map file and see that some_rom_function lives at 0x1234:

 .text.some_rom_function
                0x00001234       0x94 ./source/mysource.o
                0x00001234                some_rom_function

My current solution is to use a header with inline functions, like so:

#define SOME_ROM_FUNCTION_ADDR    0x00001234

static inline int some_rom_function(void)
{
    return (*((int (**)())SOME_ROM_FUNCTION_ADDR))();
}

So that in my application code I can just do

int main( void )
{
    some_rom_function();
}

My question is: is there a better way to do this, especially for a large application? Do I really have to manually go through every single function?

Can the binutils applications (ld, objcopy, etc, etc) be used to achieve the same result, assuming that I have the final binary (non-relocatable code)?

Caveats: 1. I do own the original ROM code. So I have access to the source, the final binary, and the map file.

  1. The ROM has already been implemented. This means that it literally is written in stone (silicon in my case), so I can't change anything about the ROM itself.

Thanks!

Leonardo
  • 1,533
  • 17
  • 28
  • Are you asking how to get the function address or what? – Eugene Sh. Dec 11 '19 at 21:35
  • I can see the function address from the map file. I want to create a stub library I can link against my application and I'm asking if objcopy can help me with that. – Leonardo Dec 11 '19 at 21:36
  • It might be possible to do as a part of the "ROM code" build. Is that an option? – Eugene Sh. Dec 11 '19 at 21:37
  • I'm sorry, what do you mean by "'ROM code' build"? – Leonardo Dec 11 '19 at 21:38
  • Can you recompile your ROM code (i.e. as a library)? – LegendofPedro Dec 11 '19 at 21:39
  • You have some functions in ROM, right? Means that you have some code compiling into the binary sitting in the ROM. Do you own it? Can you change its build process? – Eugene Sh. Dec 11 '19 at 21:40
  • @LegendofPedro, If I turn the ROM into a library, my final application image will have the implementation of the ROM functions. I'm trying to avoid that to save application space. – Leonardo Dec 11 '19 at 21:46
  • @EugeneSh. I can recompile the ROM, but I can't change it as it is already implemented in silicon. – Leonardo Dec 11 '19 at 21:47
  • Then I don't think you can do any better than some script to parse either the linker map file or objdump or some other tool output. – Eugene Sh. Dec 11 '19 at 21:52
  • @EugeneSh.OK, thanks! – Leonardo Dec 11 '19 at 21:54
  • use sed to convert function declarations from headers into typedefs, i.e.: typedef int (*some_rom_function_t)(void). and another sed to convert "0x00001234 some_function" into static variables - pointers to functions: "some_function_t some_function =0x00001234;" the rest is usual stuff - just call function as usual. – Maxim Sagaydachny Dec 11 '19 at 22:03
  • @MaximSagaydachny OK, but that is essentially what I'm doing. Can I use objcopy to create a library from the ROM (ELF) image? – Leonardo Dec 11 '19 at 22:09
  • 1
    Could you [use a linker script](https://sourceware.org/binutils/docs/ld/REGION_005fALIAS.html#REGION_005fALIAS) to put your library into a dummy section (hex file) but with the `ORIGIN` of your ROM addresses? – LegendofPedro Dec 11 '19 at 23:50
  • I'd try a way @LegendofPedro kind of points to: Since your ROM is "written in stone" you could write a linker script that includes definitions of all function entry points by their name and with their fixed addresses. Additionally you write a header file that declares these functions, as usual, but you don't need an implementation file. Unfortunately I don't have more time right now to prepare an answer. :-( Would you mind to provide the (minimalized) linker script that you use now for your application(s)? And perhaps some [example]? – the busybee Dec 12 '19 at 07:09

0 Answers0