0

I am writing am embedded program for a Cortex M0, compiled with GCC. For power reasons, the main program loop is kept in RAM and the flash is turned off after setup.

We have a section declared in the Linker script called ramfunc which puts the specified functions into ram. Our "ram functions" have the follow attributes on their prototypes:

__attribute__((section(".ramfunc")));

However, I am now trying to include the SEGGER JLink RTT library into the program. SEGGER provides a header file, SEGGER_RTT.h, which is #included where we use it. However, because the Linker isn't moving the library functions into ramfunc, the program crashes when they're called after the flash is powered off.

From what I understand there are two ways to move functions in the ramfunc section:

  • using the __attribute__ macro.
  • specifying the object file and section in the linker script.

I want to do it using the attribute macro, since I don't have much experience with linker scripts, and I don't know if it's good to have it in there when production builds won't use the library.

To try this, I added this line to a header file and included it where we use this function:

unsigned SEGGER_RTT_WriteString(unsigned BufferIndex, const char* s) __attribute__((section(".ramfunc")));

However, the program still crashes when the WriteString function is called.

I suspect perhaps the reason this isn't working is because the SEGGER_RTT.c file is #including the header file that doesn't have these attributes set, but I'm not sure.

danielhep
  • 346
  • 1
  • 7
  • 1
    So modify linker script to put the section of this object file in ram. – KamilCuk Jun 30 '20 at 19:45
  • As someone who hasn't done much in linker scripts, can you explain how I would do this? I have the .ramfunc section, what would the line look like to add the library there? The library is SEGGER_RTT.h, so the object file is that with a .o. I tried this to no avail: ```SEGGER_RTT.*(.text*)``` @KamilCuk – danielhep Jun 30 '20 at 20:08
  • `The library is SEGGER_RTT.h` no, the library where `strlen` is from is most probably `libc.a` from newlib. Look at [this](https://stackoverflow.com/questions/42295298/place-segments-of-external-static-library-to-specific-locations). And `SEGGER_RTT.h` is the _header_, not a transaction unit, not a .c file that is compiled into an object file, from which the symbol comes from. Find the section in your linker script and put the object there. It would be easier if you would be specific - please specify what system are you compiling for, what compiler, compiler options and version, etc. – KamilCuk Jun 30 '20 at 20:28
  • Please create an [MCVE]. I guess you could also do `objcopy` the static library to change the section the symbols are located. The `__attribute__((section` has to be visible for function _definition_ - providing it for function _declaration_ will not change much. – KamilCuk Jun 30 '20 at 20:30
  • Thank you @KamilCuk, I rewrote my entire question to hopefully provide enough information and clarity. I will take a look at your links too and see if that helps. My ignorance when it comes to C vocabulary is definitely my downfall here! – danielhep Jun 30 '20 at 21:35
  • Most IDEs provide a flash build with a correct linker script in place. Is this Segger "Embedded Studio" aka Crossworks? – Lundin Jul 01 '20 at 06:53
  • Likely you're going to have to use elf tools to figure out what sections they put things in and put those in RAM in your linker script, or use elf tools to *change* what section they put things in. Have you considered contacting their support? They're the ones who insisted on providing the functionality as a binary library rather than as source... Or perhaps use some other (preferably source-level) solution providing this sort of functionality. – Chris Stratton Jul 01 '20 at 14:00

0 Answers0