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.

Compile error "dangerous relocation: unaligned address"

I am just beginning a new project using mspgcc-20120406-p20120911.  I have the code below, which I believe to be properly structured.

typedef void (*cbFunction_t)(void);
cbFunction_t callbackFunc = 0;

I get the compiler error "dangerous relocation: unaligned address" on the following lines where I try to assign a value to the function pointer:

void registerISRCallback (cbFunction_t callbackFunc)
{
	callbackFunc= callbackFunc;
}


void unregisterISRCallback (void)
{
	callbackFunc= NULL;
}

I have tried the exact same code in CCS v5.4 and it compiles fine.  Am I missing a #pragma or other that solve this issue in mspgcc?  I would appreciate any help on this.  Thanks.

  • CLINT STEVENSON1 said:
    void registerISRCallback (cbFunction_t callbackFunc)
    { callbackFunc= callbackFunc; }

    This looks a bit weird. Your local variable/parameter has the same name as the global one. Don't you get a shadowing warning?

    Also, is this really a compiler error? Or is it a linker error? The term 'relocation' doesn't sound like from the compiler. And is it an error? 'dangerous' rather sounds like a warning. I'd expect an 'invalid' or 'illegal' on an error.

  • I actually renamed the callback for the purpose of the forum, but I get the same error either way.  In examining the build console I see "ld returned 1 exit status", so it is a linker error.  

    Another strange is that if I put the same code in main.c, I don't get this error.  I only get the error when this code is located in a separate source file.  Like I mentioned, I duplicated this project (exact same code) in CCSv5 and it built just fine.  I'm not sure what I'm doing wrong, but it seems silly to avoid using callback functions for no other reason than being tied to mspgcc.  

  • I think I found it.  In "Project > Properties", I added the workspace path to the assembler include paths, recompiled and it builds successfully now.  

  • Ok, so the assembler include path wasn't the problem...  Prior to my last post, before poking around in the project settings, I had also commented out a static uint8_t variable foo in a third and separate source file from where I get the linker error on the callback function pointer.  I have since removed the workspace path from the assembler include paths (it is now empty again) and honed in on how this variable is declared.  What I found is that I only get this linker error when the variable is declared as both 1) static and 2) an 8-bit width.  

    I changed the declaration to volatile, which it needs to be anyway, and now I get no linker errors.  

  • CLINT STEVENSON1 said:
    I only get this linker error when the variable is declared as both 1) static and 2) an 8-bit width.  

    Strange. It looks like the linker fails to apply proper padding, so byte-sized foo causes the function pointer to be placed on an uneven address. Normally, this shouldn't happen for global variables (auto-alignment).

    Do you use large code model? Maybe the 20 (32) bit function pointers don't get auto-aligned (linker bug) and then the linker notices that there is a word/dword-sized variable on an unaligned address.

    You should post this in the CCS compiler forum.

  • I haven't specified large code model, so unless mspgcc uses this by default, then no.  Thanks for the insight.  I'll post this to the CCS compiler forum as you suggested.  

    Here is the post in the CCS compiler forum for anyone who may be following this:

  • Didn't you say that you were using mspgcc-20120406-p20120911? Or, was it CCS?

  • Yes, I'm using using mspgcc-20120406-p20120911.  Jens is the guru and suggested I post the Q in the CCS forum...

  • CLINT STEVENSON1 said:
    Jens is the guru and suggested I post the Q in the CCS forum...

    I'm not 100% fault proof :)

    I remembered that you spoke about CCS in your original post - but you were just telling that you tried in CCS and it worked. So my fault.

    However, MSPGCC has been taken over by TI/RedHat recently, so the CCS forum might still be the place to ask. Even if just for a pointer where to ask next.

**Attention** This is a public forum