-
Notifications
You must be signed in to change notification settings - Fork 387
Description
I am wrapping a GHDL simulation in a C program. I have successfully done it by building a main.c
file with a int main(int argc, char **argv)
function that calls a function defined in a different file (wrapper.c
). ghdl_main
is called from the latter. The build procedure is as follows:
$ ghdl -a -fPIC ../screen_ghdl.vhdl ../test.vhdl
$ ghdl --bind -fPIC test_screen
$ gcc -c -fPIC ../wrapper.c -o wrapper.o
$ gcc ../main.c -o ../bin/main -Wl,wrapper.o -Wl,`ghdl --list-link test_screen`
Now I'd like to build a shared library libwrapper.so
, and a program (shared.c
) that uses it:
$ gcc -g3 -fPIC -shared -o libwrapper.so -Wl,wrapper.o -Wl,`ghdl --list-link test_screen`
$ gcc -L$PWD -o ../bin/shared ../shared.c -lwrapper -ldl
However, the last command fails:
/tmp/ccHFxRna.o: In function `main':
.../hwd-ide/examples/ghdl-so/obj/../shared.c:25: undefined reference to `ghdl_mm'
collect2: error: ld returned 1 exit status
Indeed, the three functions defined in wrapper.c
are not listed with nm -D libwrapper.so
. But, they are found with objdump:
$ objdump -d libwrapper.so | grep "<ghdl_mm>"
000000000000572e <ghdl_mm>:
$ objdump -d libwrapper.so | grep "<get_p>"
0000000000005699 <get_p>:
613a: e8 5a f5 ff ff callq 5699 <get_p>
$ objdump -d libwrapper.so | grep "<get_m>"
00000000000056f7 <get_m>:
619f: e8 53 f5 ff ff callq 56f7 <get_m>
Context
- OS: Fedora 28
- Origin: built from sources (using ghdl/docker) by adding
OPT_FLAGS+=-fPIC
andLIB_CFLAGS+=-fPIC
to the makefile.
GHDL 0.36-dev (v0.35-271-g5cd3de46-dirty) [Dunoon edition]
Compiled with GNAT Version: 8.1.1 20180712 (Red Hat 8.1.1-5)
llvm code generator
How to reproduce?
I uploaded the code to https://github.com/1138-4EB/hwd-ide/tree/examples-vhpi/examples/VHPI/sharedobject. Executing runme.sh
reproduces the steps above:
|> Analyze VHDL sources (screen_ghdl.vhdl, test.vhdl)
|> Bind simulation unit
|> Build 'wrapper.o' for 'ghdl_mm'
|> Build 'main'
|> Run 'main'
> Call 'ghdl_mm'
>> ghdl_mm
>> ghdl_main
get_p(1): 14
get_p(0): 2002260
|> Build 'wrapper.so'
|> Build 'shared'
/tmp/ccHFxRna.o: In function `main':
...hwd-ide/examples/ghdl-so/obj/../shared.c:25: undefined reference to `ghdl_mm'
collect2: error: ld returned 1 exit status
NOTE: A workaround is applied to the example now. See next comment.