Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22085

How to place multiple const objects from multiple translation units sorted in one memory block?

$
0
0

I am developing firmware for some devices and each of them contain configuration objects which values are allowed to be changed by an external application. Here is example of such object:

struct TObjectParams
{
   ObjectId       ID;            // enum
   void*          Data;          // pointer to configuration data
   unsigned short Length;        // size of data

   TAccessControl AccessControl; //object access control
};

Each instance of TObjectParams is const and cannot be changed during runtime. Firmware consists of multiple modules and each of them comes with its own configuration objects. It is done so, because different devices can contain different modules and forcing each device to contain all off the objects would be waste of resources. My goal is to group all the objects into one place (array or single section) and have them sorted there by ID number (sorting will significantly reduce search complexity from O(n) to O(log n).

My first idea was to use constexpr to register object (in object's constructor) and during compile time create constexpr array with all objects. But as far as I know it would work only if all of the objects were in the same translation unit.

My second idea is to use __attribute__((used, section("objects_section"))) and now I am ensured that linker won't remove any defined object and all of them would be in same section. The problem is sorting. Is there any way to force linker to sort these objects? Or maybe it is possible by editig .elf file? I know that I can easily dump objects_section, sort it (by own app) and update through objcopy but it will totally mess debugging experience and direct call of any object because updating section content won't update symbols addresses (or maybe I am wrong).

Do you know how to not only update section content but also symbols addresses? Or maybe you know better approach to such a problem? I am open to all suggestions.


Viewing all articles
Browse latest Browse all 22085

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>