Skip to content

Commit 2a1c724

Browse files
committed
Document the case of cross-compiled LLVM
1 parent c497b30 commit 2a1c724

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/utils/hsdis/Makefile

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,25 +195,36 @@ LIBRARIES = $(TARGET_DIR)/bfd/libbfd.a \
195195
$(TARGET_DIR)/opcodes/libopcodes.a \
196196
$(TARGET_DIR)/libiberty/libiberty.a
197197
else
198-
LIBRARIES/i386 = $(patsubst -l%,%,$(shell $(LLVMDIR)/bin/llvm-config --libs x86 x86disassembler))
199-
LIBRARIES/amd64 = $(patsubst -l%,%,$(shell $(LLVMDIR)/bin/llvm-config --libs x86 x86disassembler))
200-
LIBRARIES/aarch64 = $(patsubst -l%,%,$(shell $(LLVMDIR)/bin/llvm-config --libs aarch64 aarch64disassembler))
198+
199+
# llvm-config may not be available when you've cross-compiled LLVM (build != host). That is the
200+
# case when you want to cross-compile hsdis as well. To work around that limitation, you can
201+
# simply specify the LLVM libraries and system libraries by hand. To do so, set the following
202+
# variables to the values outputed when running llvm-config on the target platform:
203+
# - LIBRARIES/$(ARCH) to the output of `llvm-config --libs {ARCH} {ARCH}disassembler`
204+
# - SYSTEM_LIBRARIES to the output of `llvm-config --system-libs`
205+
LLVM_CONFIG := $(if $(shell $(LLVMDIR)/bin/llvm-config --version),$(LLVMDIR)/bin/llvm-config)
206+
207+
ifneq ($(LLVM_CONFIG),)
208+
LIBRARIES/i386 = $(patsubst -l%,%,$(shell $(LLVM_CONFIG) --libs x86 x86disassembler))
209+
LIBRARIES/amd64 = $(patsubst -l%,%,$(shell $(LLVM_CONFIG) --libs x86 x86disassembler))
210+
LIBRARIES/aarch64 = $(patsubst -l%,%,$(shell $(LLVM_CONFIG) --libs aarch64 aarch64disassembler))
211+
endif
201212

202213
ifeq ($(LIBRARIES/$(ARCH)),)
203-
$(error "unknown LLVM libraries for ARCH=$(ARCH))
214+
$(error "Unknown LLVM libraries for ARCH=$(ARCH))
204215
endif
205216

206217
LIBRARIES=$(addprefix $(LLVMDIR)/lib/,$(patsubst %,$(ARC_PRE)%$(ARC_EXT),$(LIBRARIES/$(ARCH))))
218+
219+
ifneq ($(LLVM_CONFIG),)
220+
SYSTEM_LIBRARIES=$(shell $(LLVM_CONFIG) --system-libs)
221+
endif
207222
endif
208223

209224
ifneq ($(MINGW),)
210225
LIBRARIES += $(TARGET_DIR)/zlib/libz.a
211226
endif
212227

213-
ifneq ($(LLVM),)
214-
DLDFLAGS+=$(shell $(LLVMDIR)/bin/llvm-config --system-libs)
215-
endif
216-
217228
DEMO_TARGET = $(TARGET_DIR)/hsdis-demo
218229
DEMO_SOURCE = hsdis-demo.c
219230

@@ -237,7 +248,7 @@ $(TARGET_DIR)/Makefile:
237248
endif
238249

239250
$(TARGET): $(SOURCE) $(LIBS) $(LIBRARIES) $(TARGET_DIR)
240-
$(CXX) $(OUTFLAGS) $(CPPFLAGS) $(CFLAGS) $(SOURCE) $(DLDFLAGS) $(LIBRARIES)
251+
$(CXX) $(OUTFLAGS) $(CPPFLAGS) $(CFLAGS) $(SOURCE) $(DLDFLAGS) $(LIBRARIES) $(SYSTEM_LIBRARIES)
241252

242253
$(DEMO_TARGET): $(DEMO_SOURCE) $(TARGET) $(TARGET_DIR)
243254
$(CC) $(OUTFLAGS) -DTARGET_DIR=\"$(TARGET_DIR)\" $(CPPFLAGS) -g $(CFLAGS/$(ARCH)) $(DEMO_SOURCE) $(LDFLAGS)

src/utils/hsdis/hsdis.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,8 @@ class hsdis_backend : public hsdis_backend_base {
602602
if (_arch_name[0] == '\0')
603603
_arch_name = native_target_triple();
604604

605-
static bool complained = false;
606-
607605
if (LLVMInitializeNativeTarget() != 0) {
606+
static bool complained = false;
608607
if (!complained)
609608
(*_printf_callback)(_printf_stream, "failed to initialize LLVM native target\n");
610609
complained = true;
@@ -613,6 +612,7 @@ class hsdis_backend : public hsdis_backend_base {
613612
return;
614613
}
615614
if (LLVMInitializeNativeAsmPrinter() != 0) {
615+
static bool complained = false;
616616
if (!complained)
617617
(*_printf_callback)(_printf_stream, "failed to initialize LLVM native asm printer\n");
618618
complained = true;
@@ -621,6 +621,7 @@ class hsdis_backend : public hsdis_backend_base {
621621
return;
622622
}
623623
if (LLVMInitializeNativeDisassembler() != 0) {
624+
static bool complained = false;
624625
if (!complained)
625626
(*_printf_callback)(_printf_stream, "failed to initialize LLVM native disassembler\n");
626627
complained = true;
@@ -629,6 +630,7 @@ class hsdis_backend : public hsdis_backend_base {
629630
return;
630631
}
631632
if ((_dcontext = LLVMCreateDisasm(_arch_name, NULL, 0, NULL, NULL)) == NULL) {
633+
static bool complained = false;
632634
const char* bad = _arch_name;
633635
if (bad == &_target_triple[0])
634636
print_help("bad target_triple=%s", bad);
@@ -657,6 +659,7 @@ class hsdis_backend : public hsdis_backend_base {
657659
(*_printf_callback)(_printf_stream, "\n");
658660
}
659661
(*_printf_callback)(_printf_stream, "hsdis output options:\n");
662+
(*_printf_callback)(_printf_stream, " target_triple=<triple> select disassembly target\n");
660663
(*_printf_callback)(_printf_stream, " help print this message\n");
661664
}
662665

@@ -669,7 +672,7 @@ class hsdis_backend : public hsdis_backend_base {
669672
char buf[128];
670673
size_t size = LLVMDisasmInstruction(_dcontext, (uint8_t*)p, (uint64_t)(end - start), (uint64_t)p, buf, sizeof(buf));
671674
if (size > 0) {
672-
_printf_callback(_printf_stream, "%s", buf);
675+
(*_printf_callback)(_printf_stream, "%s", buf);
673676
}
674677
return size;
675678
}

0 commit comments

Comments
 (0)