Search code examples
gcclinkerarmmicrocontrollerobject-files

Linking an Unused Object File Breaks the Program


As a newbie to gcc and MCUs world, I am seeing a strange behavior I hope someone can help me with. I can create and execute a simple application for my armv7e-m board (CC3220S_LAUNCHXL) without any problem. However when I link an object file which is not referenced anywhere and does not reference any additional code, the program stops getting executed on the board. There are no compile or link time errors; Just the program won't work.

My question is how linking an unused object file would break the program execution?


Solution

  • My question is how linking an unused object file would break the program execution?

    There are many ways this could happen. Example:

    #include <stdio.h>
    int main()
    {
      puts("Hello!");
      return 0;
    }
    
    gcc t.c && ./a.out
    Hello!
    

    So far, everything works as expected. Now for some breakage:

    // t1.c
    __attribute__((constructor))
    void fn() { abort(); }
    
    gcc -w -c t1.c && gcc t.c t1.o && ./a.out
    Aborted (core dumped)
    

    Voila: an unreferenced fn linked into the program causes it to die before reaching main.

    Just the program won't work.

    You will likely get a more useful answer if you provide details of what exactly "won't work" means, and do some debugging on your own.