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.

Linking .lib file



How to link .lib file generated by other project to specific memory location.

-- Rahul

  • If you can define a Memory Section for all the objects, at ones, in a specific library I don’t know.

    You can create a separate Memory Section for the objects in a library. But in each of the library object files you need to add a pragma to the specific code section.
  • The linker maps compiler segments into linker sections. While you can give the linker new rules to map differently, you need to specify the segment to which code belongs at compile time. If your lib has been compiled to put its code into the default code segment, then it is linked there together with any code that was compiled into the code segment. AFAIK the linker cannot distinguish between segments from your project code and form library code. Same segment name, same destination section.
    So to get what you want, you need to compile the library so that its code goes into a specific, unique segment, and add a rule to every project linker script that uses this lib. This rule can then map the code to standard text section, or to a specific section at a specific address. However, it won’t link at all if you don’t add a specific rule to each project that ever uses the lib.
  • Thanks Michael,

    I did compile .lib with specific rules in linker so that nothing will get messed up at main project. But I'm not getting how to mention .lib generated by other project in .cmd(linker command file) file of the main project.

    I'm sing CCSv5.5

    any help?

    -Rahul

  • Usually, a library is wither used as source code (which I prefer), or as a collection of compiler-generated objects. In both cases, the library is generated before the linker is invoked. Only dynamically linked libraries are handled by a linker, as they are more or less independent executables, just not being executed by the OS but by other programs.
    So your linker command settings in the library project are don't care.
    TO put your library code into a certain location, you need to give all or each single library function a pragma that tells the compiler to put it into a specific segment.
    Like "#pragma CODE_SEGMENT(function_name, segment_name)" (details: see your compiler manual)
    In the target project, you can add mapping commands to the linker script that tells the linker where to map these segments (as they won't be mapped automatically to the normal code locations anymore)
  • Rahul Udagatti said:
    But I'm not getting how to mention .lib generated by other project in .cmd(linker command file) file of the main project.

    Look at section 8.5.4.5 Specifying Library or Archive Members as Input to Output Sections in the MSP430 Assembly Language Tools User's Guide. It shows how sections from a specific library file can be placed in their own sections.


    E.g. I took an example project which linked the standard run time library rts430x_lc_rd_eabi.lib and added a custom memory region in the linker command file:

        CUSTOM_FLASH            : origin = 0xA400, length = 0x2000
    

    In the sections entry in the linker command file the .text from the rts430x_lc_rd_eabi.lib library was forced to the CUSTOM_FLASH memory, while .text from other objects was left in the FLASH or FLASH2 memory regions:

        .text       : {}>> FLASH2 | FLASH       /* CODE                              */
        .text:_isr  : {} > FLASH                /* ISR CODE SPACE                    */
        .text       : {-l=rts430x_lc_rd_eabi.lib(.text)} > CUSTOM_FLASH
    

    Inspecting the linker map file confirmed that the .text from the rts430x_lc_rd_eabi.lib library had been placed in it's own memory region, and only by having to modify the linker command file (i.e. didn't have to modify how the library was built).

  • Good to know that the linker mapping rules can not only map segments to sections but also filter by source file. I didn't know that. This of course makes mapping of library code easier, since it isn't necessary to compile the lib with specific segment info then.

**Attention** This is a public forum