Skip to content

Commit 0c1cbd8

Browse files
committed
Hide Go runtime symbols to allow coexistence with other Charm gems
When multiple Charm gems (bubbletea, lipgloss, etc.) are loaded in the same Ruby process, each embeds its own Go runtime via c-archive static linking. Ruby loads .bundle files with RTLD_GLOBAL, causing ~1900 Go runtime symbols to clash in the global namespace, resulting in a segfault. Fix this by using -load_hidden (macOS) / --version-script (Linux) to make all Go runtime symbols local to the .bundle. Only Init_lipgloss is exported. The Go API symbols (lipgloss_*) remain accessible within the bundle since they're resolved at link time. Tested: bubbletea + lipgloss loaded together in Ruby 4.0 — no segfault.
1 parent 501a8af commit 0c1cbd8

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

ext/lipgloss/exported_symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_Init_lipgloss

ext/lipgloss/extconf.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,22 @@ def detect_platform
4242
ERROR
4343
end
4444

45+
go_lib_path = File.join(go_lib_dir, "liblipgloss.a")
46+
4547
$LDFLAGS << " -L#{go_lib_dir}"
4648
$INCFLAGS << " -I#{go_lib_dir}"
4749

48-
$LOCAL_LIBS << " #{go_lib_dir}/liblipgloss.a"
49-
5050
case RbConfig::CONFIG["host_os"]
5151
when /darwin/
52+
$LDFLAGS << " -Wl,-load_hidden,#{go_lib_path}"
53+
$LDFLAGS << " -Wl,-exported_symbols_list,#{File.join(__dir__, "exported_symbols.txt")}"
5254
$LDFLAGS << " -framework CoreFoundation -framework Security -framework SystemConfiguration"
5355
$LDFLAGS << " -lresolv"
5456
when /linux/
57+
$LOCAL_LIBS << " #{go_lib_path}"
58+
version_script = File.join(__dir__, "version_script.map")
59+
File.write(version_script, "{ global: Init_lipgloss; local: *; };")
60+
$LDFLAGS << " -Wl,--version-script,#{version_script}"
5561
$LDFLAGS << " -lpthread -lm -ldl"
5662
$LDFLAGS << " -lresolv" if find_library("resolv", "res_query")
5763
end

0 commit comments

Comments
 (0)