xref: /linux/tools/objtool/Makefile (revision 956b9cbd7f156c8672dac94a00de3c6a0939c692)
1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
2442f04c3SJosh Poimboeufinclude ../scripts/Makefile.include
3630e7a29SArnaldo Carvalho de Meloinclude ../scripts/Makefile.arch
4442f04c3SJosh Poimboeuf
50d83da43SJosh Poimboeufifeq ($(SRCARCH),x86)
60d83da43SJosh Poimboeuf	BUILD_ORC    := y
70d83da43SJosh Poimboeuf	ARCH_HAS_KLP := y
80d83da43SJosh Poimboeufendif
90d83da43SJosh Poimboeuf
100d83da43SJosh Poimboeufifeq ($(SRCARCH),loongarch)
110d83da43SJosh Poimboeuf	BUILD_ORC	   := y
120d83da43SJosh Poimboeufendif
130d83da43SJosh Poimboeuf
140d83da43SJosh Poimboeufifeq ($(ARCH_HAS_KLP),y)
15ee0b48faSJosh Poimboeuf	HAVE_XXHASH = $(shell printf "$(pound)include <xxhash.h>\nXXH3_state_t *state;int main() {}" | \
160d83da43SJosh Poimboeuf		      $(HOSTCC) -xc - -o /dev/null -lxxhash 2> /dev/null && echo y || echo n)
170d83da43SJosh Poimboeuf	ifeq ($(HAVE_XXHASH),y)
18dd590d4dSJosh Poimboeuf		BUILD_KLP	 := y
190d83da43SJosh Poimboeuf		LIBXXHASH_CFLAGS := $(shell $(HOSTPKG_CONFIG) libxxhash --cflags 2>/dev/null) \
200d83da43SJosh Poimboeuf				    -DBUILD_KLP
210d83da43SJosh Poimboeuf		LIBXXHASH_LIBS   := $(shell $(HOSTPKG_CONFIG) libxxhash --libs 2>/dev/null || echo -lxxhash)
220d83da43SJosh Poimboeuf	endif
230d83da43SJosh Poimboeufendif
240d83da43SJosh Poimboeuf
25dd590d4dSJosh Poimboeufexport BUILD_ORC BUILD_KLP
260d83da43SJosh Poimboeuf
27442f04c3SJosh Poimboeufifeq ($(srctree),)
28e19b7ceeSUwe Kleine-Königsrctree := $(patsubst %/,%,$(dir $(CURDIR)))
29442f04c3SJosh Poimboeufsrctree := $(patsubst %/,%,$(dir $(srctree)))
30442f04c3SJosh Poimboeufendif
31442f04c3SJosh Poimboeuf
32*68b4fe32SJosh PoimboeufRM ?= rm -f
33*68b4fe32SJosh Poimboeuf
34bdb8bf7dSIan RogersLIBSUBCMD_DIR = $(srctree)/tools/lib/subcmd/
35bdb8bf7dSIan Rogersifneq ($(OUTPUT),)
36bdb8bf7dSIan Rogers  LIBSUBCMD_OUTPUT = $(abspath $(OUTPUT))/libsubcmd
37bdb8bf7dSIan Rogerselse
38bdb8bf7dSIan Rogers  LIBSUBCMD_OUTPUT = $(CURDIR)/libsubcmd
39bdb8bf7dSIan Rogersendif
40bdb8bf7dSIan RogersLIBSUBCMD = $(LIBSUBCMD_OUTPUT)/libsubcmd.a
41442f04c3SJosh Poimboeuf
42442f04c3SJosh PoimboeufOBJTOOL    := $(OUTPUT)objtool
43442f04c3SJosh PoimboeufOBJTOOL_IN := $(OBJTOOL)-in.o
44442f04c3SJosh Poimboeuf
45d5ea4fecSChun-Tse ShaoLIBELF_FLAGS := $(shell $(HOSTPKG_CONFIG) libelf --cflags 2>/dev/null)
46d5ea4fecSChun-Tse ShaoLIBELF_LIBS  := $(shell $(HOSTPKG_CONFIG) libelf --libs 2>/dev/null || echo -lelf)
47056d28d1SRolf Eike Beer
48442f04c3SJosh Poimboeufall: $(OBJTOOL)
49442f04c3SJosh Poimboeuf
5031eca25fSJosh PoimboeufWARNINGS := -Werror -Wall -Wextra -Wmissing-prototypes			\
5131eca25fSJosh Poimboeuf	    -Wmissing-declarations -Wwrite-strings			\
5231eca25fSJosh Poimboeuf	    -Wno-implicit-fallthrough -Wno-sign-compare			\
5331eca25fSJosh Poimboeuf	    -Wno-unused-parameter
5431eca25fSJosh Poimboeuf
556a77cff8SJosh PoimboeufINCLUDES := -I$(srctree)/tools/include \
5632b50485SHONG Yifan	    -I$(srctree)/tools/include/uapi \
576a77cff8SJosh Poimboeuf	    -I$(srctree)/tools/arch/$(HOSTARCH)/include/uapi \
586f8ca676SJulien Thierry	    -I$(srctree)/tools/arch/$(SRCARCH)/include	\
597786032eSVasily Gorbik	    -I$(srctree)/tools/objtool/include \
60bdb8bf7dSIan Rogers	    -I$(srctree)/tools/objtool/arch/$(SRCARCH)/include \
61bdb8bf7dSIan Rogers	    -I$(LIBSUBCMD_OUTPUT)/include
6231eca25fSJosh Poimboeuf
630d83da43SJosh PoimboeufOBJTOOL_CFLAGS  := -std=gnu11 -fomit-frame-pointer -O2 -g $(WARNINGS)	\
640d83da43SJosh Poimboeuf		   $(INCLUDES) $(LIBELF_FLAGS) $(LIBXXHASH_CFLAGS) $(HOSTCFLAGS)
6531eca25fSJosh Poimboeuf
660d83da43SJosh PoimboeufOBJTOOL_LDFLAGS := $(LIBSUBCMD) $(LIBELF_LIBS) $(LIBXXHASH_LIBS) $(HOSTLDFLAGS)
67442f04c3SJosh Poimboeuf
682e51f262SJan Beulich# Allow old libelf to be used:
69bf71940fSDavid Engrafelfshdr := $(shell echo '$(pound)include <libelf.h>' | $(HOSTCC) $(OBJTOOL_CFLAGS) -x c -E - 2>/dev/null | grep elf_getshdr)
70cd955bddSIan RogersOBJTOOL_CFLAGS += $(if $(elfshdr),,-DLIBELF_USE_DEPRECATED)
71cd955bddSIan Rogers
72cd955bddSIan Rogers# Always want host compilation.
73cd955bddSIan RogersHOST_OVERRIDES := CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)"
742e51f262SJan Beulich
7559953303SAlexandre Chartre#
7659953303SAlexandre Chartre# To support disassembly, objtool needs libopcodes which is provided
77436326bcSSasha Levin# with libbfd (binutils-dev or binutils-devel package).
7859953303SAlexandre Chartre#
79436326bcSSasha Levin# We check using HOSTCC directly rather than the shared feature framework
80436326bcSSasha Levin# because objtool is a host tool that links against host libraries.
81436326bcSSasha Levin#
823f2de814SSasha Levin# When using shared libraries, -lopcodes is sufficient as dependencies are
833f2de814SSasha Levin# resolved automatically. With static libraries, we must explicitly link
843f2de814SSasha Levin# against libopcodes' dependencies: libbfd, libiberty, and sometimes libz.
853f2de814SSasha Levin# Try each combination and use the first one that succeeds.
863f2de814SSasha Levin#
873f2de814SSasha LevinLIBOPCODES_LIBS := $(shell \
883f2de814SSasha Levin	for libs in "-lopcodes" \
893f2de814SSasha Levin		    "-lopcodes -lbfd" \
903f2de814SSasha Levin		    "-lopcodes -lbfd -liberty" \
913f2de814SSasha Levin		    "-lopcodes -lbfd -liberty -lz"; do \
923f2de814SSasha Levin		echo 'extern void disassemble_init_for_target(void *);' \
933f2de814SSasha Levin		     'int main(void) { disassemble_init_for_target(0); return 0; }' | \
943f2de814SSasha Levin			$(HOSTCC) -xc - -o /dev/null $$libs 2>/dev/null && \
953f2de814SSasha Levin			echo "$$libs" && break; \
963f2de814SSasha Levin	done)
9759953303SAlexandre Chartre
98436326bcSSasha Levin# Styled disassembler support requires binutils >= 2.39
99436326bcSSasha LevinHAVE_DISASM_STYLED := $(shell echo '$(pound)include <dis-asm.h>' | \
100436326bcSSasha Levin			$(HOSTCC) -E -xc - 2>/dev/null | grep -q disassembler_style && echo y)
10159953303SAlexandre Chartre
10259953303SAlexandre ChartreBUILD_DISAS := n
10359953303SAlexandre Chartre
1043f2de814SSasha Levinifneq ($(LIBOPCODES_LIBS),)
10559953303SAlexandre Chartre	BUILD_DISAS := y
106436326bcSSasha Levin	OBJTOOL_CFLAGS += -DDISAS -DPACKAGE='"objtool"'
1073f2de814SSasha Levin	OBJTOOL_LDFLAGS += $(LIBOPCODES_LIBS)
108436326bcSSasha Levinifeq ($(HAVE_DISASM_STYLED),y)
109436326bcSSasha Levin	OBJTOOL_CFLAGS += -DDISASM_INIT_STYLED
110436326bcSSasha Levinendif
11159953303SAlexandre Chartreendif
11259953303SAlexandre Chartre
11359953303SAlexandre Chartreexport BUILD_DISAS
11459953303SAlexandre Chartre
115442f04c3SJosh PoimboeufAWK = awk
116bdb8bf7dSIan RogersMKDIR = mkdir
1170decf1f8SMatt Helsley
11860cbdf5dSJosh Poimboeufexport srctree OUTPUT CFLAGS SRCARCH AWK
119442f04c3SJosh Poimboeufinclude $(srctree)/tools/build/Makefile.include
120442f04c3SJosh Poimboeuf
121cd955bddSIan Rogers$(OBJTOOL_IN): fixdep $(LIBSUBCMD) FORCE
1228c4526caSIan Rogers	$(Q)$(CONFIG_SHELL) ./sync-check.sh
123cd955bddSIan Rogers	$(Q)$(MAKE) $(build)=objtool $(HOST_OVERRIDES) CFLAGS="$(OBJTOOL_CFLAGS)" \
124cd955bddSIan Rogers		LDFLAGS="$(OBJTOOL_LDFLAGS)"
125cd955bddSIan Rogers
126442f04c3SJosh Poimboeuf
127442f04c3SJosh Poimboeuf$(OBJTOOL): $(LIBSUBCMD) $(OBJTOOL_IN)
128cd955bddSIan Rogers	$(QUIET_LINK)$(HOSTCC) $(OBJTOOL_IN) $(OBJTOOL_LDFLAGS) -o $@
129442f04c3SJosh Poimboeuf
130442f04c3SJosh Poimboeuf
131bdb8bf7dSIan Rogers$(LIBSUBCMD_OUTPUT):
1328c4526caSIan Rogers	$(Q)$(MKDIR) -p $@
133442f04c3SJosh Poimboeuf
134bdb8bf7dSIan Rogers$(LIBSUBCMD): fixdep $(LIBSUBCMD_OUTPUT) FORCE
1358c4526caSIan Rogers	$(Q)$(MAKE) -C $(LIBSUBCMD_DIR) O=$(LIBSUBCMD_OUTPUT) \
136bdb8bf7dSIan Rogers		DESTDIR=$(LIBSUBCMD_OUTPUT) prefix= subdir= \
137cd955bddSIan Rogers		$(HOST_OVERRIDES) EXTRA_CFLAGS="$(OBJTOOL_CFLAGS)" \
138bdb8bf7dSIan Rogers		$@ install_headers
139bdb8bf7dSIan Rogers
140bdb8bf7dSIan Rogers$(LIBSUBCMD)-clean:
141bdb8bf7dSIan Rogers	$(call QUIET_CLEAN, libsubcmd)
142bdb8bf7dSIan Rogers	$(Q)$(RM) -r -- $(LIBSUBCMD_OUTPUT)
143bdb8bf7dSIan Rogers
144bdb8bf7dSIan Rogersclean: $(LIBSUBCMD)-clean
145442f04c3SJosh Poimboeuf	$(call QUIET_CLEAN, objtool) $(RM) $(OBJTOOL)
146442f04c3SJosh Poimboeuf	$(Q)find $(OUTPUT) -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
1478308fd00SAlexandre Chartre	$(Q)$(RM) $(OUTPUT)arch/x86/lib/cpu-feature-names.c $(OUTPUT)fixdep
148bdb8bf7dSIan Rogers	$(Q)$(RM) $(OUTPUT)arch/x86/lib/inat-tables.c $(OUTPUT)fixdep
14959953303SAlexandre Chartre	$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.objtool
15059953303SAlexandre Chartre	$(Q)$(RM) -r -- $(OUTPUT)feature
151442f04c3SJosh Poimboeuf
152442f04c3SJosh PoimboeufFORCE:
153442f04c3SJosh Poimboeuf
154442f04c3SJosh Poimboeuf.PHONY: clean FORCE
155