Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse code memory for data

Tags:

c

gcc

embedded

ld

I have some C-code that run on a system with limited amount of memory. The code execution have basically two phases, startup phase and main phase. The startup phase consist of code that generates some parameters used by the main phase. During the main phase data is generated.

Since the startup phase is run only once I would like to reuse the memory space used by the startup code for data storage in the main phase.

I have tested one way to handle this:

  • A custom linker script placing code and data associated with the startup phase in a .startup section. This section is placed on the same address as .bss that is the bss section used by the main phase.
    The startup code calls the entry point for the startup phase and when it returns, before calling main in the main phase, it clears the .bss section.
    Xrossref commands is used in linker script to help getting code and data into right place.

This works but it has it's quirks. To get the startup code and data in the .startup section I must list them with input section names given by gcc during compilation.

Now I would like to enable lto (link time optimization) and that breaks the above method since input section names are changed.

Thinking of testing a new approach:

  • Build startup code and main code as two separate programs. Each program is build and optimized separately and put together to one boot image.
    Advantage is that there is no risk that the main code calls a function accidentally placed in startup section (which does no longer exist when main code is executed). Another advantage is that I do only need to specify the entry point for each phase and the linker will do the rest of finding out code and data needed for that phase.
    The parameter data output from startup and used by main can be placed in a common bss section or on the stack.

Disadvantage is that I can not see how startup code and main code can share functions that are used in both phases. If the shared functions are small, trying to share them might be a bad idea since lto will be more restricted or end up inlineing two versions of the shared function anyway.

Does anyone know of a preferred method for this or have any comment on the new suggested approach?

like image 846
henrik Avatar asked Jun 11 '14 21:06

henrik


People also ask

How can code be reused?

Code reuse is the act of recycling or repurposing code parts to improve existing or to create new software. Write it once, use it multiple times, or even better, Low-code (where someone else or some other group writes the underlying code for you).

What is reusable code called?

In software development (and computer programming in general), code reuse, also called software reuse, is the use of existing software, or software knowledge, to build new software, following the reusability principles.

What is code reuse in OOP?

Code reuse is the use of existing software to build new software. It is one of the holy grails of modern software development. APIs provide a mechanism to enable code reuse. In the early years of software development, it was common for a company to have to write all of the code for any application they produced.

What is reusability in programming?

In computer science and software engineering, reusability is the use of existing assets in some form within the software product development process; these assets are products and by-products of the software development life cycle and include code, software components, test suites, designs and documentation.


1 Answers

The method I've seen (Rabbit processors) is using overlays. [There was some hardware involved, so I'm not sure the analogy is perfect.] Anyway, the notion [in my mind] is like an elevator. You have a small amount of program space (the elevator cage) which is safe and viable at all times. But if you want to access another 'floor', you go back to the elevator (PC is in that small address space) and then switch the settings to activate another bank of memory. Then you can exit the elevator and be on a new floor.

You have a slightly different issue (want to use less memory instead of not having enough address lines) but the same strategy could apply.

Start in the elevator. Run the first start up code, which sets certain data values, also presumably in a safe area. Retreat back to the elevator. Overwrite the startup code space. Now you can exit the elevator. But you will still need the addresses of the new space. These need to be stored somewhere. Perhaps you have two sets of function pointers, with overlapping addresses, one for use in the startup, and another for use in the main code. Just use the right functions at the right time.

like image 101
Jiminion Avatar answered Oct 08 '22 00:10

Jiminion