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.

C55 relocatable executable. Need "howto" help.

Hi!

I need to make an application in form of a relocatable executable binary file.
Main idea looks like this: there is a host program running DSP/BIOS. This host program at the run time allocates memory for the guest application, loads the binary executable from flash into allocated space and runs the guest application by calling the pointer to allocated memory as a function.
The guest application nether uses any "external" functions (like memory allocation) nor global variables. It shouldn't have initialization code. It only has it's code, global constants (available within it's own boundaries only) and "context" for storing intermediate data. It has a main function that processes initialization and other functionality.

Please help me to start. I have the code of the app but I don't know how to link it to an executable binary file.
What linker CMD file should like like? Maybe there is something that I can read?...

For linking I use "--relocatable" and "--absolute_exe" option. Then I call hex55.exe and get some binary file.
But it doesn't look like what I need.

A pseudocode for main function of the guest application:

//==============================================================================
//------------------------------------------------------------------------------
uint16_t main (uint16_t Command, void* DataBuffer, uint16_t BufWordlen)
{
     uint16_t ResultCode;
     //------------------------------------------------------
     switch (Command)
     {
     case 0x0001: // Initialize context
         if (DataPointer == NULL)
         {
              ResultCode = 0; // error
         } else
         {
              memset (DataBuffer, 0x0000, BufWordlen);
              ResultCode = 1; // success
         }
         break;
     case 0x0002: // Process data
         ... // some code
         ResultCode = 1; // success
         break;
     default:
         ResultCode = 0; // error, unknown command
         break;
     }
     //------------------------------------------------------
     return ResultCode;
}

 

A pseudocode for loading and running this guest application from host program:

//==============================================================================
//------------------------------------------------------------------------------

typedef uint16_t (*T_GuestMainFunc) (uint16_t, void*, uint16_t);
T_GuestMainFunc GuestMainFunc;
void* GuestContext = NULL;
uint16_t GResult = 0;

...

// allocate space for application's code
GuestMainFunc = (T_GuestMainFunc) malloc (8192);
if (GuestMainFunc == NULL) return 0;
// load the code from flash memory
SPI_ReadFromMemory ((void*) GuestMainFunc, ZERO_OFFSET, 8192 * sizeof(uint16_t));
// allocate space for application's context
GuestContext = malloc (1024); // allocate space for context
if (GuestContext == NULL)
{
     free ((void*) GuestMainFunc);
     return 0;
}
// run application
GResult = GuestMainFunc (0x0000, GuestContext, 1024);
...

Thanks in advance for your replies!

  • What you're describing requires position-independent code (PIC).  In PIC, all code and data references are PC-relative, so you can load the code at any point in memory and run it.  The C55x compiler does not support PIC.  You could in theory hand-craft such a program in assembly code, but remember you can't use absolute addresses anywhere.

    You can generate a relocatable executable, for which you don't need PIC, but then you need your loader to perform the relocation when the program is loaded, which is no easy task.  You'd have to understand all of the relocation types used in the program and rewrite relocation fields with the appropriate value.  I'm not aware of a comprehensive reference for C55x relocation types.

  • Thank you for the reply!

    I see, that I'll not be able to have exactly what I need. But maybe the loader that performs relocation at the run time is OK. I would like to know more about this problem and about the format of binary files produced this way.

     

  • The only TI documentation that exists on that topic is the C55x Assembly Language Tools User's Guide (SPRU280) and the Common Object File Format application note (SPRAAO8), but the information on relocations is pretty light.