Odin icon indicating copy to clipboard operation
Odin copied to clipboard

Odin links to libraries from foreign imports of packages, when symbols values aren't used.

Open flysand7 opened this issue 2 years ago • 0 comments

Context

  • odin version dev-2023-11:59675949
  • Linux bumbread-pc 6.5.9-arch2-1 #1 SMP PREEMPT_DYNAMIC Thu, 26 Oct 2023 00:52:20 +0000 x86_64 GNU/Linux

Expected Behavior

I'm making a small library for creating windows, one of the things I want to be able to do is to create wayland and x11 windows in the same application without needing to recompile the application. That is, the application should not have a rigid dependency on libX11.so. The setup is simple.

import "core:dynlib"
import "vendor:x11/xlib"

X11_State :: struct {
	library:      dynlib.Library,
	XOpenDisplay: type_of(xlib.XOpenDisplay)
}

My thinking is that since I'm applying the type_of operator to the proc name, that should make it count as "not used" and not link to foreign-imported library by x11/xlib package, since I only need the type of the variable, not it's value.

Current Behavior

We can inspect the shared object dependencies with readelf, to see that libX11.so is needed by our application:

$ readelf -d window.bin | grep NEED
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libX11.so.6]

We can also see we don't need any of the symbols from the library, if we run readelf -s on the executable.

Removing the type_of line removes the shared object dependency.

flysand7 avatar Nov 24 '23 23:11 flysand7