@@ -77,19 +77,32 @@ HOST_OVERRIDES := CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)"
7777# We check using HOSTCC directly rather than the shared feature framework
7878# because objtool is a host tool that links against host libraries.
7979#
80- HAVE_LIBOPCODES := $(shell echo 'int main(void) { return 0; }' | \
81- $(HOSTCC ) -xc - -o /dev/null -lopcodes 2>/dev/null && echo y)
80+ # When using shared libraries, -lopcodes is sufficient as dependencies are
81+ # resolved automatically. With static libraries, we must explicitly link
82+ # against libopcodes' dependencies: libbfd, libiberty, and sometimes libz.
83+ # Try each combination and use the first one that succeeds.
84+ #
85+ LIBOPCODES_LIBS := $(shell \
86+ for libs in "-lopcodes" \
87+ "-lopcodes -lbfd" \
88+ "-lopcodes -lbfd -liberty" \
89+ "-lopcodes -lbfd -liberty -lz"; do \
90+ echo 'extern void disassemble_init_for_target(void * ) ;' \
91+ 'int main(void) { disassemble_init_for_target(0); return 0; }' | \
92+ $(HOSTCC ) -xc - -o /dev/null $$libs 2>/dev/null && \
93+ echo "$$libs" && break; \
94+ done)
8295
8396# Styled disassembler support requires binutils >= 2.39
8497HAVE_DISASM_STYLED := $(shell echo '$(pound ) include <dis-asm.h>' | \
8598 $(HOSTCC ) -E -xc - 2>/dev/null | grep -q disassembler_style && echo y)
8699
87100BUILD_DISAS := n
88101
89- ifeq ($(HAVE_LIBOPCODES ) ,y )
102+ ifneq ($(LIBOPCODES_LIBS ) , )
90103 BUILD_DISAS := y
91104 OBJTOOL_CFLAGS += -DDISAS -DPACKAGE='"objtool"'
92- OBJTOOL_LDFLAGS += -lopcodes
105+ OBJTOOL_LDFLAGS += $(LIBOPCODES_LIBS)
93106ifeq ($(HAVE_DISASM_STYLED ) ,y)
94107 OBJTOOL_CFLAGS += -DDISASM_INIT_STYLED
95108endif
0 commit comments