I have a project set up where I compile and link a shared library (libexample.so) with a linker script that looks like this:
SECTIONS
{
.modules : {
__MODULES_START = .;
KEEP(*(.mod*));
__MODULES_END = .;
}
...
}
I use these in my code to load modules compiled into the library.
extern uint32_t __MODULES_START;
extern uint32_t __MODULES_END;
unsigned int init_mods (void) {
void (*p)(void) = (void *)&__MODULES_START;
...
}
And when I compile the library in my Makefile
build/%.o: %.c
gcc -o $@ -c $< -fPIC -g -Os -Iinclude
bin/libexample.so: $(OBJS)
gcc -o $@ $^ -shared -fPIC -lc -T$(LINKER_SCRIPT)
It builds and links just fine, and it works when I try to link the library to another project that calls "init_mods".
build/%.o: %.c
gcc -o $@ -c $< -fPIE -g -Os -Iinclude -I../libexample/include
bin/untitled-program: $(OBJS)
gcc -o $@ $^ -fPIE -lc -lexample -Lbin '-Wl,-rpath,$$ORIGIN'
However, when I run the program where it can find the library, I receive the following linking error:
/bin/untitled-program: error while loading shared libraries: /blah/blah/libexample.so: unexpected PLT reloc type 0x08
When I readelf the shared library, I get the two definitions in my symbol table
Symbol table '.symtab' contains 223 entries:
Num: Value Size Type Bind Vis Ndx Name
...
154: 0000000000000050 0 NOTYPE GLOBAL DEFAULT 2 __MODULE_INIT_END
...
222: 0000000000000028 0 NOTYPE GLOBAL DEFAULT 2 __MODULE_INIT_START
So I'm wondering if my issue has to do with the NOTYPE, but I'm having trouble finding documentation on this.
To explain why I think my issue has to do with the linker script variables, when I run my program with linker debugging on, one of them is the last one to show up.
$ LD_DEBUG=all ./untitled-program
...
23856: symbol=__MODULE_END; lookup in file=./bin/untitled-program [0]
23856: symbol=__MODULE_END; lookup in file=/usr/lib/libc.so.6 [0]
23856: symbol=__MODULE_END; lookup in file=./bin/libexample.so [0]
23856: binding file ./bin/libexample.so [0] to ./bin/libexample.so [0]: normal symbol `__MODULE_END'
...
23856: symbol=__MODULE_START; lookup in file=./bin/untitled-program [0]
23856: symbol=__MODULE_START; lookup in file=/usr/lib/libc.so.6 [0]
23856: symbol=__MODULE_START; lookup in file=./bin/libexample.so [0]
23856: binding file ./bin/libexample.so [0] to ./bin/libexample.so [0]: normal symbol `__MODULE_START'
./bin/untitled-program: error while loading shared libraries: ./bin/libexample.so: unexpected PLT reloc type 0x08
But, that's the weird thing because it's able to bind one of the other linker script variables before it fails.
I have been working on this issue for too long, so I'm having trouble seeing the bigger picture. Maybe I'm thinking about this wrong and the issue is with another symbol. Any help or guidance would be appreciated!