Link vector handler only if init is used in program?

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm using a debugging library (just a .c file) with my program that outputs over USB and uses a couple of interrupt vector handlers internally. Even if nothing is ever called there, not even its init function, its relatively large interrupt handlers get linked into my program, since the vector table references them.

I want to figure out how to tell GCC that an interrupt vector handler function should be linked into the executable only if another function in the file is called as well (e.g. init). Basically it'd tell the linker to not treat references to the vector handler as a signal to link it in, rather treat references to the library's init function as a signal to link the vector handler in.

I realize that I could set up something in the makefile, but that's less convenient than having the linker automatically determine that the library isn't actually used and not link any of it.

This is a generally useful thing for libraries, and works except for vector handlers. I was thinking that as a last resort I could have the handler call through a function pointer, and set that in the init function. It's ugly and adds some latency to the handler, though.

// lib.c

void init_usb( void )
{
    // ...
}

ISR(USB_COM_vect)
{
    // lots of code
}
// main.c

int main( void )
{
    init_usb();
    // ...
    return 0;
}

// Result: USB_COM_vect linked in
// main2.c

int main( void )
{
    // ...
    return 0;
}

// Result: nothing from lib.c linked in
Last Edited: Fri. Aug 30, 2013 - 06:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The vector table should, by default, only be interested in a bunch of "weak references" to a default "unimplemented interrupt" handler, so you should only get the large chunks of "real" ISRs if you link in a module that provides a non-weak definition of the vector (such as one that includes an ISR(), like your lib.c module does).

If your lib.c module's compiled object code were put into a linkable library, and you added that library to the list of libraries that the "main.c" application is linked against, I believe things would work exactly as you say you want.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or just don't build lib.c as part of the project if nothing in it is used. (sorry if that sounds a bit obvious!).