This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

how to use a memory section size in my code

Hi,

I would like to use symbols defined in a linker .cmd file in my code.

I know pragma directives for getting memory section start addresses but is there a way of getting their size?

Basically what I'd like to do is to define a section with a startAddress and a size in a command file and use them in my source code.

I know how to deal with startAddress  ( pragma SECTION directive), but  not with it's associated size.

Thanks

 Bruno

  • The pragma SECTION directive only places that C data item in the section you name.  There is no guarantee, in the general case, that it will be placed at the start of the section.

    Instead, use the RUN_START and RUN_SIZE operators in the link command file when you create the output section.  Those operators are documented in the C6000 assembly tools book.  It looks something like ...

      some_output_section { /* list of input sections, possibly none */ } run = RAM, RUN_START(start_sym), RUN_SIZE(size_sym)

    This presumes you are building for EABI.  If you are building for the older COFF ABI, then you need to prepend each symbol with an underscore.

    Back in C, you cannot view these symbols like some ordinary C variable.  They are not.  What's interesting about them is the address (you might call it the value) of the symbol, not the contents of the memory address.  That's why it is typical to write something like ...

    extern int start_sym; // extern matters, type does not
    #define START_SYM ((unsigned int) &start_sym) // type matters here

    Then you use START_SYM where needed.

    Hope this helps ...

    -George 

  • Hi George,
    thank you for your helpfull answer. I've coded this way and it works:

    Linker .cmd file:

    SECTIONS
    {
      ...
      Images_Struct_Space: {}
            run = IMAGES_STRUCT,
            RUN_START(_LK_ImageStruct_org),
            RUN_SIZE(_LK_ImageStruct_len)
     ...

    }


    Code:
        extern int LK_ImageStruct_org, LK_ImageStruct_len;
        const unsigned int IMAGES_STRUCT_SPACE_ADDRESS = ((unsigned int)&LK_ImageStruct_org);
        const unsigned int IMAGES_STRUCT_SPACE_SIZE = (unsigned int)&LK_ImageStruct_len;

    (thanks by the way for your pertinent remark about the underscore....)

    Bruno

  • Hi George,

    this subject leads to another point:

    Is there a way of getting the real size of an object which has not been loaded.

    For example if I comment out data relative to "Images_Struct_Space" in my code then RUN_SIZE returns NULL, which is not surprizing since no data has been loaded in this section. I'd like to know if there is a way of having RUN_SIZE (or LOAD_SIZE or whatever's possible)  set to the real size of an object but without loading it (I've tried with type=NOLOAD but it doesn't seem to work as I think).

    Thanks,

     Bruno

  • I must be missing something.  You commented out all the code which defines data that is in the section Images_Struct_Space.  The linker still creates the output section, but it has length 0.  Thus, RUN_SIZE assigns 0 to the value of the symbol.  Given these circumstances, I don't see how the linker could possibly know the real size of the object.  

    Thanks and regards,

    -George

  • I'm sorry I haven't been clear enough. To be more precise I have two different issues (which possibly end up to the same point):

    1- When running in debug mode and downloading several times the same code, there is a bunch of data that I do not have to download each time (i.e bitmaps, filters etc..).

    For time saving what I do is download the whole code once and then comment out all these constant data for all the next downloads. I understand doing this will end-up with 0 length sections from the linker point of view and I'm asking myself if there is a way to do the same thing but by saying the linker not to load these data instead of commenting them out in the code.

    2- I also have different areas of data which are just simple raugh memory buffers that I need for my own memory allocation code (which is not malloc from <stdlib.h> and does not use -heap option) . So my question is: is there a way of specifying a length for an empty (i.e no object) section  within the linker cmd file that can be passed to the code at link time (or even at load or run time).

    Hope my explanations are clear enough :-)

    Regards,

     Bruno

  • For #1, use the NOLOAD section type.  Read more about that here.

    For #2, you can create an uninitialized section in the linker command file.  Something like ...

    special_buffer
    {
       . += 0x1000;
    } run = RAM, RUN_START(_buffer_symbol)

    Hope this helps ...

    -George

  • #1: I've already tried it but I must have made a mistake somewhere... Will try it again asap. (thanks for the link anyway!)

    #2: Nice syntax... :-)    but what about the size (I guess in your example it's hard set to 0x1000) can we get something like _buffer_size that can be passed to the code?

    Thanks

     Bruno

  • Bruno Scherrer said:
    but what about the size (I guess in your example it's hard set to 0x1000) can we get something like _buffer_size that can be passed to the code?

    The linker supports a limited form of C preprocessing.  You could use that to wrap it in a #define.  But that's about it.

    Thanks and regards,

    -George

  • George,

    I'm sorry I had to switch on another subject in the meantime.

    I thinks things are clear enough for me.

    Thanks a lot for your help

    Regards,

     Bruno