1

I'm trying to understand how I can provide aliases to missnamed symbols when linking libraries with gnu ld. If I understand correctly it should be sufficient to define an addition to the default link script as

PROVIDE(undefined_symbol = existing_symbol);

however my minimal non-working example below does not seem to recognize the symbol foo which should be defined in foo.o. Any ideas or hints what the correct link script syntax is would be very appreciated?

foo.c:

int foo(int a) {
  return a + 1;
}

foo.h:

int foo(int a);

main.c:

#include "foo.h"
#include <stdio.h>

int main(int argc, char *argv[]) {
  int b = foo_(1);   // calling foo_ instead of foo
  printf("%d\n", b);
  return 0;
}

script.ld:

PROVIDE(foo_ = foo);

compile and link:

gcc -c -o foo.o foo.c
ar rcs foo.a foo.o
gcc -I. -c -o main.o main.c
gcc main.o foo.a -o test script.ld

> /usr/bin/ld:script.ld:1: undefined symbol 'foo' referenced in expression


> nm foo.a
foo.o:
0000000000000000 T foo

> nm main.o
                 U foo_
                 U _GLOBAL_OFFSET_TABLE_
0000000000000000 T main
                 U printf
Patrick Kwok
  • 42
  • 10

0 Answers0