1

I need a symbol alias to be the alias for the function function as described in this question, but for IAR compiler/linker. I have tried answers for GCC, but those don't work, obviously:

// source code
void alias(void) __attribute__ ((alias ("function")));
Error[Pe130]: expected a "{" C:\project\test.c 61

void alias(void) asm("function");
Error[Pe065]: expected a ";" C:\project\test.c 61

#pragma weak alias=function
Error[e46]: Undefined external "alias" referred in ?ABS_ENTRY_MOD (  ) 

// linker options:
-Dalias=function
Fatal Error[e163]: The command line symbol "function" in -Dalias=function is not defined. 

Does anyone know how I can define a function alias in IAR/Xlink?

A little background: I'm responsible for a module which is used in several projects. In order to test it, I have developed test cases for testIDEA tool which validate assertions at certain breakpoints. However, some common functions have different names in different projects (e.g. the initialization function could be called init(), init_mcu(), startup() etc.) so every time my module is integrated in a new project, I have to modify my test cases to match the new function names. What I'd like to do instead is to define a common alias (e.g. test_init()) to whatever the init function is called, so that my test cases can always set a breakpoint using this common name. I expect this symbol to make it to the ELF file, where it can be seen by the debugger.

Community
  • 1
  • 1
Dmitry Grigoryev
  • 3,156
  • 1
  • 25
  • 53

2 Answers2

2

From the IAR C/C++ Development Guide (p. 258):

To make the definition of foo a weak definition, write:
#pragma weak foo
To make NMI_Handler a weak alias for Default_Handler, write:
#pragma weak NMI_Handler=Default_Handler
If NMI_Handler is not defined elsewhere in the program, all references to NMI_Handler will refer to Default_Handler.

So, try something like this:

void f1(void);
void f2(void);

#pragma weak f1=f2

void f2(void) {}
bzeaman
  • 1,128
  • 11
  • 28
  • Thanks, I'm upvoting your answer since it's clever and may help others, but it didn't help me. When I do `#pragma weak f1=f2`, `f1` simply doesn't appear in the ELF file. If I try to force it with `-gf1` linker option, I get the `Error [e46]: Undefined external "f1" referred in ?ABS_ENTRY_MOD ( )` – Dmitry Grigoryev Jun 21 '16 at 15:13
  • 1
    @DmitryGrigoryev Just a guess: Maybe you need to do `#pragma required=f1` after the `weak` pragma. – user694733 Jun 22 '16 at 09:17
2

After a close look at xlink options I've found the one which sort of suits my needs:

-e -enew=old [,old] …

Use -e to configure a program at link time by redirecting a function call from one function to another.

This can also be used for creating stub functions; i.e. when a system is not yet complete, undefined function calls can be directed to a dummy routine until the real function has been written.

I ended up with this code:

void alias(void) {
// copy the body of function()
}

// Linker parameters
-ealias=function

This redirects all calls to function() to alias(), regardless the actual name function has. Sure it's an ugly hack because I have to copy the code, but I get what I wanted: I can set a breakpoint to alias() by name.

Community
  • 1
  • 1
Dmitry Grigoryev
  • 3,156
  • 1
  • 25
  • 53