2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 include $(topsrcdir
)/config
/config.mk
11 ifneq (1_1
,$(MOZ_MEMORY
)_
$(or
$(MOZ_NATIVE_JEMALLOC
),$(FORCE_SHARED_LIB
)))
13 ifneq (,$(filter OS2 WINNT
,$(OS_ARCH
)))
14 SDK_LIBRARY
= $(IMPORT_LIBRARY
)
16 ifdef FORCE_SHARED_LIB
17 SDK_LIBRARY
= $(SHARED_LIBRARY
)
19 SDK_LIBRARY
= $(REAL_LIBRARY
)
25 MOZ_GLUE_LDFLAGS
= # Don't link against ourselves
27 ifneq (,$(ZLIB_IN_MOZGLUE
)$(MOZ_LINKER
))
29 EXTRA_DSO_LDOPTS
+= $(MOZ_ZLIB_LIBS
)
31 SHARED_LIBRARY_LIBS
+= $(MOZ_ZLIB_LIBS
)
35 ifeq (WINNT
,$(OS_TARGET
))
38 mozglue.def
: mozglue.def.in
39 $(call py_action
,preprocessor
,$(if
$(MOZ_REPLACE_MALLOC
),-DMOZ_REPLACE_MALLOC
) $(ACDEFINES
) $< -o
$@
)
41 GARBAGE
+= mozglue.def
43 ifneq (,$(filter -DEFAULTLIB
:mozcrt
,$(MOZ_GLUE_LDFLAGS
)))
44 # Don't install the import library if we use mozcrt
45 NO_INSTALL_IMPORT_LIBRARY
= 1
57 ifeq (Darwin_1
,$(OS_TARGET
)_
$(MOZ_REPLACE_MALLOC
))
59 -Wl
,-U
,_replace_init \
60 -Wl
,-U
,_replace_malloc \
61 -Wl
,-U
,_replace_posix_memalign \
62 -Wl
,-U
,_replace_aligned_alloc \
63 -Wl
,-U
,_replace_calloc \
64 -Wl
,-U
,_replace_realloc \
65 -Wl
,-U
,_replace_free \
66 -Wl
,-U
,_replace_memalign \
67 -Wl
,-U
,_replace_valloc \
68 -Wl
,-U
,_replace_malloc_usable_size \
69 -Wl
,-U
,_replace_malloc_good_size \
70 -Wl
,-U
,_replace_jemalloc_stats \
71 -Wl
,-U
,_replace_jemalloc_purge_freed_pages \
72 -Wl
,-U
,_replace_jemalloc_free_dirty_pages \
75 ifneq ($(MOZ_REPLACE_MALLOC_LINKAGE
),compiler support
)
76 EXTRA_DSO_LDOPTS
+= -flat_namespace
78 ifeq ($(MOZ_REPLACE_MALLOC_LINKAGE
),dummy library
)
79 EXTRA_DSO_LDOPTS
+= -Wl
,-weak_library
,$(DEPTH
)/memory
/replace
/dummy
/$(DLL_PREFIX
)replace_malloc
$(DLL_SUFFIX
)
83 ifeq (android
, $(MOZ_WIDGET_TOOLKIT
))
84 # To properly wrap jemalloc's pthread_atfork call.
85 EXTRA_DSO_LDOPTS
+= -Wl
,--wrap
=pthread_atfork
89 ifeq (arm
, $(TARGET_CPU
))
90 EXTRA_DSO_LDOPTS
+= -Wl
,-version-script
,$(srcdir)/arm-eabi-filter
95 ifeq (Android
, $(OS_TARGET
))
96 WRAP_LDFLAGS
:= $(filter -Wl
%,$(WRAP_LDFLAGS
))
99 include $(topsrcdir
)/config
/rules.mk
102 ifeq (WINNT
,$(OS_TARGET
))
103 # Roll our own custom logic here for the import library
105 ###############################################################################
107 # Linking Mozilla itself to jemalloc is not particularly difficult. To do this
108 # we avoid linking directly to the Microsoft-provided CRT import libraries.
109 # Instead, we link to our own import library which we generate here. To
110 # replace the CRT's malloc/free/other memory management symbols we export
111 # our own versions out of jemalloc.dll. We then take the import library that
112 # the compiler generates for jemalloc.dll and combine it with the MS CRT import
113 # libraries. We put our library on the command line first, and the CRT symbols
114 # are discarded in favor of our versions!
116 # Unfortunately that was too easy. The CRT import library is not a standard
117 # import library that contains a list of symbols and whatnot. It also includes
118 # object files that are linked into generated programs. One of these,
119 # crtdll.obj is (as one might expect) linked into all DLLs that link against
120 # the CRT. This file does things like run static C++ constructors when the
121 # DLL is attached, call DllMain, etc.
123 # In the CRT source all malloc/free calls are made to malloc_crt and free_crt.
124 # In debug builds these are both defined to malloc_dbg and free_dbg. In opt
125 # builds malloc_crt is an actual function, implemented and exposed from the
126 # CRT. free_crt is, however, defined to be just plain old free. This works
127 # fine inside the CRT where malloc_crt and free operate on the same heap.
128 # Outside the CRT malloc_crt is in the CRT's heap, but free is in jemalloc's
129 # heap. This causes much pain at shutdown :-(
131 # The obvious solution here is to override malloc_crt too. Unfortunately,
132 # that doesn't work because the CRT expects to be able to call msize on this
133 # piece of memory deep inside the CRT, which will fail because it'll call the
134 # CRT's msize on a pointer in jemalloc's heap.
136 # Our solution to this is quite devious. We take apart the CRT's import lib
137 # and remove the problematic object file. We then poke at the object file's
138 # symbol table and replace '__imp__free' (which means grab free from some
139 # other DLL) with '__imp__frex'. Then we define our own dummy no-op function
140 # in jemalloc.dll and export it as frex. Then we put the CRT import lib
141 # back together with the patched crtdll.obj, glue it to the end of jemalloc's
142 # import library and link the rest of Mozilla to that.
144 # The result? A binary that uses jemalloc, doesn't crash, and leaks a tiny
145 # amount of memory (32 words per DLL in the 2010 CRT) at shutdown.
147 ###############################################################################
150 $(INSTALL
) $(IFLAGS2
) mozcrt.lib
$(DIST
)/lib
152 # And finally combine that with the jemalloc import library to get an import
153 # library that has our malloc/free/etc and the CRT's everything else
154 mozcrt.lib
: $(IMPORT_LIBRARY
) msvc_modified.lib
157 # Put the fixed object file back in
158 msvc_modified.lib
: msvc_removed.lib crtdll_fixed.obj
161 # Fix the object file
162 crtdll_fixed.obj
: crtdll.obj
163 $(PYTHON
) $(srcdir)/fixcrt.py
165 # Find the path of crtdll.obj
166 CRTDLL_FULLPATH
=$(subst \
,\\,$(shell lib
-list msvc_combined.lib | grep crtdll
\\.obj
))
168 # Remove the broken object file, only after we have extracted it
169 msvc_removed.lib
: msvc_combined.lib crtdll.obj
170 lib
-OUT
:$@ msvc_combined.lib
-REMOVE
:$(CRTDLL_FULLPATH
)
172 # Extract the broken object file out of the combined library
173 crtdll.obj
: msvc_combined.lib
174 lib
-OUT
:$@
$^
-EXTRACT
:$(CRTDLL_FULLPATH
)
176 # Grab both CRT libraries and combine them into one library to simplify things
178 lib
-OUT
:$@
$(WIN32_CRT_LIBS
)