Setting Target Memory Location for Compiled Library

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Setting Target Memory Location for Compiled Library

Jump to solution
858 Views
jimfell
Contributor III

I've compiled the Freescale USB stack (v4.0.2) as a library, and I'd like to specify its location in memory on the target, so that it can be used by both the main application code and our bootloader.  Is there a way to do this without adding `#pragma define_section` to all of the source files in the stack and `__declspec (section_name)` to every function and variable declarations in the USB stack source?  I'm using CodeWarrior 10.2.  Thanks.

Labels (1)
0 Kudos
1 Solution
706 Views
jimfell
Contributor III

My apologies for not posting this sooner, but I was able to resolve the problem on my own using function pointers.  For example, let's say function MyFunc needs to be addressed at 0x00000300:

typedef void (*fpMyFunc_t)(void);

extern const fpMyFunc_t fpMyFunc @ 0x00000300; // Additional function pointers can be added at offsets of 4 bytes

Function MyFunc is located somewhere in source, let's say it's in a file called file.c.  The following then needs to be added to the linker command file:

FORCE_ACTIVE { _MyFunc } # prevents linker from deadstripping the function, now that it's not directly called

SECTIONS {

...

  .text_MyFunc : # A separate section is needed for each function to be addressed

  {

    OBJECT (_MyFunc, file_c.obj)

    . = ALIGN (0x4);

  } >> code

...

  .my_vectors :

  {

   WRITEW(ADDR(.text_MyFunc));

    # Additional function pointers may be assigned here 

  } >> myfunctionvectors # defined in MEMORY section of LCF

}

Now, at link time the address of MyFunc will be assigned to the function pointer fpMyFunc, which effectively acts as an alias for MyFunc without having to change the source file in which MyFunc is actually located.  I know that there may be a couple other ways to do this, but this implementation suits our needs.

View solution in original post

0 Kudos
2 Replies
707 Views
jimfell
Contributor III

My apologies for not posting this sooner, but I was able to resolve the problem on my own using function pointers.  For example, let's say function MyFunc needs to be addressed at 0x00000300:

typedef void (*fpMyFunc_t)(void);

extern const fpMyFunc_t fpMyFunc @ 0x00000300; // Additional function pointers can be added at offsets of 4 bytes

Function MyFunc is located somewhere in source, let's say it's in a file called file.c.  The following then needs to be added to the linker command file:

FORCE_ACTIVE { _MyFunc } # prevents linker from deadstripping the function, now that it's not directly called

SECTIONS {

...

  .text_MyFunc : # A separate section is needed for each function to be addressed

  {

    OBJECT (_MyFunc, file_c.obj)

    . = ALIGN (0x4);

  } >> code

...

  .my_vectors :

  {

   WRITEW(ADDR(.text_MyFunc));

    # Additional function pointers may be assigned here 

  } >> myfunctionvectors # defined in MEMORY section of LCF

}

Now, at link time the address of MyFunc will be assigned to the function pointer fpMyFunc, which effectively acts as an alias for MyFunc without having to change the source file in which MyFunc is actually located.  I know that there may be a couple other ways to do this, but this implementation suits our needs.

0 Kudos
706 Views
dataScout
NXP Employee
NXP Employee


Hello,

Which target processor are you using?

Regards

0 Kudos