1# $FreeBSD$
2
3GCCDIR=	${.CURDIR}/../../../contrib/gcc
4GCCLIB=	${.CURDIR}/../../../contrib/gcclibs
5
6SHLIB_NAME=	libgcc_s.so.1
7SHLIBDIR?=	/lib
8
9.include <bsd.own.mk>
10#
11# libgcc is linked in last and thus cannot depend on ssp symbols coming
12# from earlier libraries. Disable stack protection for this library.
13#
14MK_SSP=	no
15
16.include "${.CURDIR}/../../usr.bin/cc/Makefile.tgt"
17
18.if ${TARGET_CPUARCH} == "arm" && ${MK_ARM_EABI} != "no"
19CFLAGS+=	-DTARGET_ARM_EABI
20.endif
21
22.PATH: ${GCCDIR}/config/${GCC_CPU} ${GCCDIR}/config ${GCCDIR}
23
24CFLAGS+=	-DIN_GCC -DIN_LIBGCC2 -D__GCC_FLOAT_NOT_NEEDED \
25		-DHAVE_GTHR_DEFAULT \
26		-I${GCCLIB}/include \
27		-I${GCCDIR}/config -I${GCCDIR} -I. \
28		-I${.CURDIR}/../../usr.bin/cc/cc_tools
29
30LDFLAGS+=	-nodefaultlibs
31DPADD+=		${LIBC}
32LDADD+=		-lc
33
34OBJS=		# added to below in various ways depending on TARGET_CPUARCH
35
36#---------------------------------------------------------------------------
37#
38# Library members defined in libgcc2.c.
39# When upgrading GCC, obtain the following list from mklibgcc.in
40#
41LIB2FUNCS= _muldi3 _negdi2 _lshrdi3 _ashldi3 _ashrdi3 \
42	_cmpdi2 _ucmpdi2 \
43	_enable_execute_stack _trampoline __main _absvsi2 _absvdi2 _addvsi3 \
44	_addvdi3 _subvsi3 _subvdi3 _mulvsi3 _mulvdi3 _negvsi2 _negvdi2 _ctors \
45	_ffssi2 _ffsdi2 _clz _clzsi2 _clzdi2 _ctzsi2 _ctzdi2 _popcount_tab \
46	_popcountsi2 _popcountdi2 _paritysi2 _paritydi2 _powisf2 _powidf2 \
47	_powixf2 _powitf2 _mulsc3 _muldc3 _mulxc3 _multc3 _divsc3 _divdc3 \
48	_divxc3 _divtc3 _bswapsi2 _bswapdi2
49.if ${COMPILER_TYPE} != "clang" || ${TARGET_CPUARCH} != "arm"
50LIB2FUNCS+= _clear_cache
51.endif
52
53# The floating-point conversion routines that involve a single-word integer.
54.for mode in sf df xf
55LIB2FUNCS+= _fixuns${mode}si
56.endfor
57
58# Likewise double-word routines.
59.if ${TARGET_CPUARCH} != "arm" || ${MK_ARM_EABI} == "no"
60# These are implemented in an ARM specific file but will not be filtered out
61.for mode in sf df xf tf
62LIB2FUNCS+= _fix${mode}di _fixuns${mode}di
63LIB2FUNCS+= _floatdi${mode} _floatundi${mode}
64.endfor
65.endif
66
67LIB2ADD = $(LIB2FUNCS_EXTRA)
68LIB2ADD_ST = $(LIB2FUNCS_STATIC_EXTRA)
69
70# Additional sources to handle exceptions; overridden by targets as needed.
71LIB2ADDEH = unwind-dw2.c unwind-dw2-fde-glibc.c unwind-sjlj.c gthr-gnat.c \
72	unwind-c.c
73LIB2ADDEHSTATIC = $(LIB2ADDEH)
74LIB2ADDEHSHARED = $(LIB2ADDEH)
75
76# List of extra C and assembler files to add to static and shared libgcc2.
77# Assembler files should have names ending in `.asm'.
78LIB2FUNCS_EXTRA =
79
80# List of extra C and assembler files to add to static libgcc2.
81# Assembler files should have names ending in `.asm'.
82LIB2FUNCS_STATIC_EXTRA =
83
84# Defined in libgcc2.c, included only in the static library.
85# KAN: Excluded _sf_to_tf and _df_to_tf as TPBIT_FUNCS are not
86# built on any of our platforms.
87LIB2FUNCS_ST = _eprintf __gcc_bcmp
88
89FPBIT_FUNCS = _pack_sf _unpack_sf _addsub_sf _mul_sf _div_sf \
90    _fpcmp_parts_sf _compare_sf _eq_sf _ne_sf _gt_sf _ge_sf \
91    _lt_sf _le_sf _unord_sf _si_to_sf _sf_to_si _negate_sf _make_sf \
92    _sf_to_df _thenan_sf _sf_to_usi _usi_to_sf
93
94DPBIT_FUNCS = _pack_df _unpack_df _addsub_df _mul_df _div_df \
95    _fpcmp_parts_df _compare_df _eq_df _ne_df _gt_df _ge_df \
96    _lt_df _le_df _unord_df _si_to_df _df_to_si _negate_df _make_df \
97    _df_to_sf _thenan_df _df_to_usi _usi_to_df
98
99TPBIT_FUNCS = _pack_tf _unpack_tf _addsub_tf _mul_tf _div_tf \
100    _fpcmp_parts_tf _compare_tf _eq_tf _ne_tf _gt_tf _ge_tf \
101    _lt_tf _le_tf _unord_tf _si_to_tf _tf_to_si _negate_tf _make_tf \
102    _tf_to_df _tf_to_sf _thenan_tf _tf_to_usi _usi_to_tf
103
104# These might cause a divide overflow trap and so are compiled with
105# unwinder info.
106LIB2_DIVMOD_FUNCS = _divdi3 _moddi3 _udivdi3 _umoddi3 _udiv_w_sdiv _udivmoddi4
107
108#-----------------------------------------------------------------------
109#
110#	Platform specific bits.
111#	When upgrading GCC, get the following definitions from config/<cpu>/t-*
112#
113.if ${TARGET_CPUARCH} == "arm"
114#	from config/arm/t-strongarm-elf
115CFLAGS+=	-Dinhibit_libc -fno-inline
116CFLAGS.clang+=	-fheinous-gnu-extensions
117
118LIB1ASMSRC =	lib1funcs.asm
119LIB1ASMFUNCS =  _dvmd_tls _bb_init_func
120.if ${MK_ARM_EABI} != "no"
121LIB2ADDEH =	unwind-arm.c libunwind.S pr-support.c unwind-c.c
122# Some compilers generate __aeabi_ functions libgcc_s is missing
123DPADD+=		${LIBGCC}
124LDADD+=		-lgcc
125.else
126LIB2FUNCS_EXTRA = floatunsidf.c floatunsisf.c
127.endif
128.endif
129
130.if ${TARGET_CPUARCH} == mips
131LIB2FUNCS_EXTRA = floatunsidf.c floatunsisf.c
132# ABIs other than o32 need this
133.if ${TARGET_ARCH} != "mips" && ${TARGET_ARCH} != "mipsel"
134LIB2FUNCS_EXTRA+= floatdidf.c fixunsdfsi.c
135LIB2FUNCS_EXTRA+= floatdisf.c floatundidf.c
136LIB2FUNCS_EXTRA+= fixsfdi.c floatundisf.c
137LIB2FUNCS_EXTRA+= fixdfdi.c fixunssfsi.c
138.endif
139.endif
140
141.if ${TARGET_CPUARCH} == "ia64"
142#	from config/ia64/t-ia64
143LIB1ASMSRC   = lib1funcs.asm
144LIB1ASMFUNCS = __divxf3 __divdf3 __divsf3 \
145	__divdi3 __moddi3 __udivdi3 __umoddi3 \
146	__divsi3 __modsi3 __udivsi3 __umodsi3 __save_stack_nonlocal \
147	__nonlocal_goto __restore_stack_nonlocal __trampoline \
148	_fixtfdi _fixunstfdi _floatditf
149LIB2ADDEH = unwind-ia64.c unwind-sjlj.c unwind-c.c
150.endif
151
152.if ${TARGET_ARCH} == "powerpc"
153#	from config/rs6000/t-ppccomm
154LIB2FUNCS_EXTRA = tramp.asm
155LIB2FUNCS_STATIC_EXTRA = eabi.asm
156.endif
157
158.if ${TARGET_ARCH} == "powerpc64"
159#	from config/rs6000/t-ppccomm
160LIB2FUNCS_EXTRA = tramp.asm
161.endif
162
163.if ${TARGET_CPUARCH} == "sparc64"
164#	from config/sparc/t-elf
165LIB1ASMSRC =   lb1spc.asm
166LIB1ASMFUNCS = _mulsi3 _divsi3 _modsi3
167.endif
168
169#-----------------------------------------------------------------------
170
171# Remove any objects from LIB2FUNCS and LIB2_DIVMOD_FUNCS that are
172# defined as optimized assembly code in LIB1ASMFUNCS.
173.if defined(LIB1ASMFUNCS)
174.for sym in ${LIB1ASMFUNCS}
175LIB2FUNCS:=	${LIB2FUNCS:S/${sym}//g}
176LIB2_DIVMOD_FUNCS:= ${LIB2_DIVMOD_FUNCS:S/${sym}//g}
177.endfor
178.endif
179
180COMMONHDRS=	tm.h tconfig.h options.h unwind.h gthr-default.h
181
182#-----------------------------------------------------------------------
183#
184# Helpful shortcuts for compiler invocations.
185#
186HIDE =  -fvisibility=hidden -DHIDE_EXPORTS
187CC_T =	${CC} -c ${CFLAGS} ${HIDE} -fPIC
188CC_P =	${CC} -c ${CFLAGS} ${HIDE} -p -fPIC
189CC_S =	${CC} -c ${CFLAGS} ${PICFLAG} -DSHARED
190
191#-----------------------------------------------------------------------
192#
193# Functions from libgcc2.c
194#
195STD_CFLAGS =
196DIV_CFLAGS =	-fexceptions -fnon-call-exceptions
197
198STD_FUNCS =	${LIB2FUNCS}
199DIV_FUNCS =	${LIB2_DIVMOD_FUNCS}
200
201STD_CFILE =	libgcc2.c
202DIV_CFILE =	libgcc2.c
203
204OBJ_GRPS =	STD DIV
205
206#-----------------------------------------------------------------------
207#
208# Floating point emulation functions
209#
210.if ${TARGET_CPUARCH} == "armNOT_YET" || \
211    ${TARGET_CPUARCH} == "powerpc" || ${TARGET_CPUARCH} == "sparc64"
212
213FPBIT_CFLAGS =	-DFINE_GRAINED_LIBRARIES -DFLOAT
214DPBIT_CFLAGS =	-DFINE_GRAINED_LIBRARIES
215
216FPBIT_CFILE =	config/fp-bit.c
217DPBIT_CFILE =	config/fp-bit.c
218
219OBJ_GRPS +=	FPBIT DPBIT
220.endif
221
222#-----------------------------------------------------------------------
223#
224# Generic build rules for object groups defined above
225#
226.for T in ${OBJ_GRPS}
227${T}_OBJS_T =	${${T}_FUNCS:S/$/.o/}
228${T}_OBJS_P =	${${T}_FUNCS:S/$/.po/}
229${T}_OBJS_S =	${${T}_FUNCS:S/$/.So/}
230OBJS +=		${${T}_FUNCS:S/$/.o/}
231
232${${T}_OBJS_T}: ${${T}_CFILE} ${COMMONHDRS}
233	${CC_T} ${${T}_CFLAGS} -DL${.PREFIX} -o ${.TARGET} ${.ALLSRC:M*.c}
234${${T}_OBJS_P}: ${${T}_CFILE} ${COMMONHDRS}
235	${CC_P} ${${T}_CFLAGS} -DL${.PREFIX} -o ${.TARGET} ${.ALLSRC:M*.c}
236${${T}_OBJS_S}: ${${T}_CFILE} ${COMMONHDRS}
237	${CC_S} ${${T}_CFLAGS} -DL${.PREFIX} -o ${.TARGET} ${.ALLSRC:M*.c}
238.endfor
239
240#-----------------------------------------------------------------------
241#
242# Extra objects coming from separate files
243#
244.if !empty(LIB2ADD)
245OBJS  +=	${LIB2ADD:R:S/$/.o/}
246SOBJS +=	${LIB2ADD:R:S/$/.So/}
247POBJS +=	${LIB2ADD:R:S/$/.po/}
248.endif
249
250#-----------------------------------------------------------------------
251#
252# Objects that should be in static library only.
253#
254SYMS_ST =	${LIB2FUNCS_ST}	${LIB2ADD_ST}
255STAT_OBJS_T = 	${SYMS_ST:S/$/.o/}
256STAT_OBJS_P = 	${SYMS_ST:S/$/.po/}
257STATICOBJS  =	${SYMS_ST:S/$/.o/}
258
259${STAT_OBJS_T}:	${STD_CFILE} ${COMMONHDRS}
260	${CC_T} -DL${.PREFIX} -o ${.TARGET} ${.ALLSRC:M*.c}
261${STAT_OBJS_P}:	${STD_CFILE} ${COMMONHDRS}
262	${CC_P} -DL${.PREFIX} -o ${.TARGET} ${.ALLSRC:M*.c}
263
264#-----------------------------------------------------------------------
265#
266# Assembler files.
267#
268.if defined(LIB1ASMSRC)
269ASM_T =		${LIB1ASMFUNCS:S/$/.o/}
270ASM_P =		${LIB1ASMFUNCS:S/$/.po/}
271ASM_S =		${LIB1ASMFUNCS:S/$/.So/}
272ASM_V =		${LIB1ASMFUNCS:S/$/.vis/}
273OBJS +=		${LIB1ASMFUNCS:S/$/.o/}
274
275${ASM_T}: ${LIB1ASMSRC} ${.PREFIX}.vis
276	${CC} -x assembler-with-cpp -c ${CFLAGS} -DL${.PREFIX} \
277	    -o ${.TARGET} -include ${.PREFIX}.vis ${.ALLSRC:N*.h:N*.vis}
278${ASM_P}: ${LIB1ASMSRC} ${.PREFIX}.vis
279	${CC} -x assembler-with-cpp -p -c ${CFLAGS} -DL${.PREFIX} \
280	    -o ${.TARGET} -include ${.PREFIX}.vis ${.ALLSRC:N*.h:N*.vis}
281${ASM_S}: ${LIB1ASMSRC}
282	${CC} -x assembler-with-cpp -c ${PICFLAG} ${CFLAGS} -DL${.PREFIX} \
283	    -o ${.TARGET} ${.ALLSRC:N*.h}
284${ASM_V}: ${LIB1ASMSRC}
285	${CC} -x assembler-with-cpp -c ${CFLAGS} -DL${.PREFIX} \
286	    -o ${.PREFIX}.vo ${.ALLSRC:N*.h}
287	( ${NM} -pg ${.PREFIX}.vo | \
288		awk 'NF == 3 && $$2 !~ /^[UN]$$/ { print "\t.hidden ", $$3 }'\
289	) > ${.TARGET}
290
291CLEANFILES += ${ASM_V} ${ASM_V:R:S/$/.vo/}
292.endif
293
294#-----------------------------------------------------------------------
295#
296# Exception handling / unwinding support.
297#
298EH_OBJS_T = ${LIB2ADDEHSTATIC:R:S/$/.o/}
299EH_OBJS_P = ${LIB2ADDEHSTATIC:R:S/$/.po/}
300EH_OBJS_S = ${LIB2ADDEHSHARED:R:S/$/.So/}
301EH_CFLAGS = -fexceptions -D__GLIBC__=3 -DElfW=__ElfN
302SOBJS    += ${EH_OBJS_S}
303
304.for _src in ${LIB2ADDEHSTATIC}
305${_src:R:S/$/.o/}: ${_src} ${COMMONHDRS}
306	${CC_T} ${EH_CFLAGS} -o ${.TARGET} ${.IMPSRC}
307${_src:R:S/$/.po/}: ${_src} ${COMMONHDRS}
308	${CC_P} ${EH_CFLAGS} -o ${.TARGET} ${.IMPSRC}
309.endfor
310.for _src in ${LIB2ADDEHSHARED}
311${_src:R:S/$/.So/}: ${_src} ${COMMONHDRS}
312	${CC_S} ${EH_CFLAGS} -o ${.TARGET} ${.IMPSRC}
313.endfor
314
315
316#-----------------------------------------------------------------------
317#
318# Generated headers
319#
320${COMMONHDRS}: ${.CURDIR}/../../usr.bin/cc/cc_tools/Makefile
321	${MAKE} -f ${.ALLSRC} MFILE=${.ALLSRC} GCCDIR=${GCCDIR} ${.TARGET}
322
323CLEANFILES += ${COMMONHDRS}
324CLEANFILES += cs-*.h option*
325
326#-----------------------------------------------------------------------
327#
328# Build symbol version map
329#
330SHLIB_MKMAP      = ${GCCDIR}/mkmap-symver.awk
331SHLIB_MKMAP_OPTS =
332SHLIB_MAPFILES   = ${GCCDIR}/libgcc-std.ver
333.if ${TARGET_CPUARCH} == "arm" && ${MK_ARM_EABI} != "no"
334SHLIB_MAPFILES  += ${GCCDIR}/config/arm/libgcc-bpabi.ver
335.endif
336VERSION_MAP      = libgcc.map
337
338libgcc.map: ${SHLIB_MKMAP} ${SHLIB_MAPFILES} ${SOBJS} ${OBJS:R:S/$/.So/}
339	(  ${NM} -pg ${SOBJS};echo %% ; \
340	  cat ${SHLIB_MAPFILES} \
341	    | sed -e '/^[   ]*#/d' \
342	          -e 's/^%\(if\|else\|elif\|endif\|define\)/#\1/' \
343	    | ${CC} ${CFLAGS} -E -xassembler-with-cpp -; \
344	) | awk -f ${SHLIB_MKMAP} ${SHLIB_MKMAP_OPTS} > ${.TARGET}
345
346CLEANFILES +=	libgcc.map
347
348#-----------------------------------------------------------------------
349#
350# Build additional static libgcc_eh[_p].a libraries.
351#
352libgcc_eh.a:	${EH_OBJS_T}
353	@${ECHO} building static gcc_eh library
354	@rm -f ${.TARGET}
355	@${AR} ${ARFLAGS} ${.TARGET} `lorder ${EH_OBJS_T} | tsort -q`
356	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
357
358all:	libgcc_eh.a
359
360.if ${MK_PROFILE} != "no"
361libgcc_eh_p.a:	${EH_OBJS_P}
362	@${ECHO} building profiled gcc_eh library
363	@rm -f ${.TARGET}
364	@${AR} ${ARFLAGS} ${.TARGET} `lorder ${EH_OBJS_P} | tsort -q`
365	${RANLIB} ${RANLIBFLAGS} ${.TARGET}
366all:	libgcc_eh_p.a
367.endif
368
369_libinstall: _lib-eh-install
370
371_lib-eh-install:
372.if ${MK_INSTALLLIB} != "no"
373	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
374		${_INSTALLFLAGS} libgcc_eh.a ${DESTDIR}${LIBDIR}
375.endif
376.if ${MK_PROFILE} != "no"
377	${INSTALL} -C -o ${LIBOWN} -g ${LIBGRP} -m ${LIBMODE} \
378		${_INSTALLFLAGS} libgcc_eh_p.a ${DESTDIR}${LIBDIR}
379.endif
380
381CLEANFILES+=	libgcc_eh.a libgcc_eh_p.a ${EH_OBJS_T} ${EH_OBJS_P}
382
383.include <bsd.lib.mk>
384
385.SUFFIXES: .vis .vo
386