michael@0: # michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: DIST_INSTALL = 1 michael@0: michael@0: # For FORCE_SHARED_LIB michael@0: include $(topsrcdir)/config/config.mk michael@0: michael@0: ifneq (1_1,$(MOZ_MEMORY)_$(or $(MOZ_NATIVE_JEMALLOC),$(FORCE_SHARED_LIB))) michael@0: michael@0: ifneq (,$(filter WINNT,$(OS_ARCH))) michael@0: SDK_LIBRARY = $(IMPORT_LIBRARY) michael@0: else michael@0: ifdef FORCE_SHARED_LIB michael@0: SDK_LIBRARY = $(SHARED_LIBRARY) michael@0: else michael@0: SDK_LIBRARY = $(REAL_LIBRARY) michael@0: endif michael@0: endif michael@0: michael@0: endif michael@0: michael@0: MOZ_GLUE_LDFLAGS = # Don't link against ourselves michael@0: michael@0: SHARED_LIBRARY_LIBS += $(call EXPAND_LIBNAME_PATH,mfbt,$(DEPTH)/mfbt) michael@0: michael@0: ifneq (,$(ZLIB_IN_MOZGLUE)$(MOZ_LINKER)) michael@0: ifdef MOZ_NATIVE_ZLIB michael@0: EXTRA_DSO_LDOPTS += $(MOZ_ZLIB_LIBS) michael@0: else michael@0: SHARED_LIBRARY_LIBS += $(MOZ_ZLIB_LIBS) michael@0: endif michael@0: endif michael@0: michael@0: ifeq (WINNT,$(OS_TARGET)) michael@0: mozglue.def: mozglue.def.in michael@0: $(call py_action,preprocessor,$(if $(MOZ_REPLACE_MALLOC),-DMOZ_REPLACE_MALLOC) $(ACDEFINES) $< -o $@) michael@0: michael@0: GARBAGE += mozglue.def michael@0: michael@0: ifneq (,$(filter -DEFAULTLIB:mozcrt,$(MOZ_GLUE_LDFLAGS))) michael@0: # Don't install the import library if we use mozcrt michael@0: NO_INSTALL_IMPORT_LIBRARY = 1 michael@0: endif michael@0: michael@0: michael@0: EXTRA_DSO_LDOPTS += \ michael@0: $(call EXPAND_LIBNAME,version) \ michael@0: $(NULL) michael@0: michael@0: endif michael@0: michael@0: ifeq (Darwin_1,$(OS_TARGET)_$(MOZ_REPLACE_MALLOC)) michael@0: EXTRA_DSO_LDOPTS += \ michael@0: -Wl,-U,_replace_init \ michael@0: -Wl,-U,_replace_malloc \ michael@0: -Wl,-U,_replace_posix_memalign \ michael@0: -Wl,-U,_replace_aligned_alloc \ michael@0: -Wl,-U,_replace_calloc \ michael@0: -Wl,-U,_replace_realloc \ michael@0: -Wl,-U,_replace_free \ michael@0: -Wl,-U,_replace_memalign \ michael@0: -Wl,-U,_replace_valloc \ michael@0: -Wl,-U,_replace_malloc_usable_size \ michael@0: -Wl,-U,_replace_malloc_good_size \ michael@0: -Wl,-U,_replace_jemalloc_stats \ michael@0: -Wl,-U,_replace_jemalloc_purge_freed_pages \ michael@0: -Wl,-U,_replace_jemalloc_free_dirty_pages \ michael@0: $(NULL) michael@0: michael@0: ifneq ($(MOZ_REPLACE_MALLOC_LINKAGE),compiler support) michael@0: EXTRA_DSO_LDOPTS += -flat_namespace michael@0: endif michael@0: ifeq ($(MOZ_REPLACE_MALLOC_LINKAGE),dummy library) michael@0: EXTRA_DSO_LDOPTS += -Wl,-weak_library,$(DEPTH)/memory/replace/dummy/$(DLL_PREFIX)replace_malloc$(DLL_SUFFIX) michael@0: endif michael@0: endif michael@0: michael@0: ifeq (android, $(MOZ_WIDGET_TOOLKIT)) michael@0: # To properly wrap jemalloc's pthread_atfork call. michael@0: EXTRA_DSO_LDOPTS += -Wl,--wrap=pthread_atfork michael@0: endif michael@0: michael@0: ifdef MOZ_LINKER michael@0: ifeq (arm, $(TARGET_CPU)) michael@0: EXTRA_DSO_LDOPTS += -Wl,-version-script,$(srcdir)/arm-eabi-filter michael@0: endif michael@0: michael@0: endif michael@0: michael@0: ifeq (Android, $(OS_TARGET)) michael@0: WRAP_LDFLAGS := $(filter -Wl%,$(WRAP_LDFLAGS)) michael@0: endif michael@0: michael@0: include $(topsrcdir)/config/rules.mk michael@0: michael@0: ifdef MOZ_MEMORY michael@0: ifeq (WINNT,$(OS_TARGET)) michael@0: # Roll our own custom logic here for the import library michael@0: michael@0: ############################################################################### michael@0: # michael@0: # Linking Mozilla itself to jemalloc is not particularly difficult. To do this michael@0: # we avoid linking directly to the Microsoft-provided CRT import libraries. michael@0: # Instead, we link to our own import library which we generate here. To michael@0: # replace the CRT's malloc/free/other memory management symbols we export michael@0: # our own versions out of jemalloc.dll. We then take the import library that michael@0: # the compiler generates for jemalloc.dll and combine it with the MS CRT import michael@0: # libraries. We put our library on the command line first, and the CRT symbols michael@0: # are discarded in favor of our versions! michael@0: # michael@0: # Unfortunately that was too easy. The CRT import library is not a standard michael@0: # import library that contains a list of symbols and whatnot. It also includes michael@0: # object files that are linked into generated programs. One of these, michael@0: # crtdll.obj is (as one might expect) linked into all DLLs that link against michael@0: # the CRT. This file does things like run static C++ constructors when the michael@0: # DLL is attached, call DllMain, etc. michael@0: # michael@0: # In the CRT source all malloc/free calls are made to malloc_crt and free_crt. michael@0: # In debug builds these are both defined to malloc_dbg and free_dbg. In opt michael@0: # builds malloc_crt is an actual function, implemented and exposed from the michael@0: # CRT. free_crt is, however, defined to be just plain old free. This works michael@0: # fine inside the CRT where malloc_crt and free operate on the same heap. michael@0: # Outside the CRT malloc_crt is in the CRT's heap, but free is in jemalloc's michael@0: # heap. This causes much pain at shutdown :-( michael@0: # michael@0: # The obvious solution here is to override malloc_crt too. Unfortunately, michael@0: # that doesn't work because the CRT expects to be able to call msize on this michael@0: # piece of memory deep inside the CRT, which will fail because it'll call the michael@0: # CRT's msize on a pointer in jemalloc's heap. michael@0: # michael@0: # Our solution to this is quite devious. We take apart the CRT's import lib michael@0: # and remove the problematic object file. We then poke at the object file's michael@0: # symbol table and replace '__imp__free' (which means grab free from some michael@0: # other DLL) with '__imp__frex'. Then we define our own dummy no-op function michael@0: # in jemalloc.dll and export it as frex. Then we put the CRT import lib michael@0: # back together with the patched crtdll.obj, glue it to the end of jemalloc's michael@0: # import library and link the rest of Mozilla to that. michael@0: # michael@0: # The result? A binary that uses jemalloc, doesn't crash, and leaks a tiny michael@0: # amount of memory (32 words per DLL in the 2010 CRT) at shutdown. michael@0: # michael@0: ############################################################################### michael@0: michael@0: libs:: mozcrt.lib michael@0: $(INSTALL) $(IFLAGS2) mozcrt.lib $(DIST)/lib michael@0: michael@0: # And finally combine that with the jemalloc import library to get an import michael@0: # library that has our malloc/free/etc and the CRT's everything else michael@0: mozcrt.lib: $(IMPORT_LIBRARY) msvc_modified.lib michael@0: lib -OUT:$@ $^ michael@0: michael@0: # Put the fixed object file back in michael@0: msvc_modified.lib: msvc_removed.lib crtdll_fixed.obj michael@0: lib -OUT:$@ $^ michael@0: michael@0: # Fix the object file michael@0: crtdll_fixed.obj: crtdll.obj michael@0: $(PYTHON) $(srcdir)/fixcrt.py michael@0: michael@0: # Find the path of crtdll.obj michael@0: CRTDLL_FULLPATH=$(subst \,\\,$(shell lib -list msvc_combined.lib | grep crtdll\\.obj)) michael@0: michael@0: # Remove the broken object file, only after we have extracted it michael@0: msvc_removed.lib: msvc_combined.lib crtdll.obj michael@0: lib -OUT:$@ msvc_combined.lib -REMOVE:$(CRTDLL_FULLPATH) michael@0: michael@0: # Extract the broken object file out of the combined library michael@0: crtdll.obj: msvc_combined.lib michael@0: lib -OUT:$@ $^ -EXTRACT:$(CRTDLL_FULLPATH) michael@0: michael@0: # Grab both CRT libraries and combine them into one library to simplify things michael@0: msvc_combined.lib: michael@0: lib -OUT:$@ $(WIN32_CRT_LIBS) michael@0: endif michael@0: endif # MOZ_MEMORY