Bug 1077148 part 1 - Define mozcrt in moz.build. r=gps

--HG--
rename : mozglue/build/Makefile.in => mozglue/crt/Makefile.in
rename : mozglue/build/fixcrt.py => mozglue/crt/fixcrt.py
This commit is contained in:
Mike Hommey 2014-10-30 13:05:50 +09:00
parent 86815ec7c9
commit c5ba34275a
10 changed files with 135 additions and 97 deletions

View File

@ -10,6 +10,12 @@ CXX_WRAPPER =
default::
# When MOZ_CRT is set, we get mozcrt from moz.build instead of
# MOZ_GLUE_LDFLAGS
ifdef MOZ_CRT
MOZ_GLUE_LDFLAGS =
endif
include $(topsrcdir)/config/makefiles/functions.mk
NSS_LIBS = \
@ -108,11 +114,6 @@ NSPR_LIB_DIR := $(subst -L,,$(subst -L$(DIST),-L$(ABS_DIST),$(NSPR_LIB_DIR)))
else
NSPR_LIB_DIR = $(ABS_DIST)/lib
endif
# Can't pass this in DEFAULT_GMAKE_FLAGS because that overrides
# definitions in NSS, so just export it into the sub-make's environment.
ifeq (WINNT_1,$(OS_TARGET)_$(MOZ_MEMORY))
export DLLFLAGS
endif
# To get debug symbols from NSS
export MOZ_DEBUG_SYMBOLS
@ -357,6 +358,18 @@ endif # MOZ_FOLD_LIBS
include $(topsrcdir)/config/rules.mk
# Can't pass this in DEFAULT_GMAKE_FLAGS because that overrides
# definitions in NSS, so just export it into the sub-make's environment.
ifeq (WINNT_1,$(OS_TARGET)_$(MOZ_MEMORY))
ifdef MOZ_CRT
# OS_LIBS comes from having mozcrt as a dependency in moz.build.
DLLFLAGS := $(OS_LIBS)
else
DLLFLAGS := -LIBPATH:$(ABS_DIST)/lib -DEFAULTLIB:mozglue
endif
export DLLFLAGS
endif
ifdef MOZ_FOLD_LIBS
# Force the linker to include everything from the static libraries.
EXPAND_LIBS_EXEC += --extract

View File

@ -36,6 +36,12 @@ elif CONFIG['MOZ_FOLD_LIBS']:
if CONFIG['OS_ARCH'] == 'Linux' and CONFIG['GCC_USE_GNU_LD']:
LD_VERSION_SCRIPT = 'nss3.def'
# mozcrt needs at least one reference in a moz.build for the frontend
# not to raise an error. Here, it allows to avoid setting DLLFLAGS in
# configure.in.
if CONFIG['MOZ_CRT']:
USE_LIBS += ['mozcrt']
else:
Library('nss')
USE_LIBS += [

View File

@ -7240,11 +7240,8 @@ else
WIN32_CRTDLL_FULLPATH=`lib -nologo -list $WIN32_CRT_LIBS | grep crtdll\\.obj`
lib -NOLOGO -OUT:crtdll.obj $WIN32_CRT_LIBS -EXTRACT:$WIN32_CRTDLL_FULLPATH
if grep -q '__imp__\{0,1\}free' crtdll.obj; then
MOZ_CRT=1
MOZ_GLUE_LDFLAGS='-LIBPATH:$(DIST)/lib -NODEFAULTLIB:msvcrt -NODEFAULTLIB:msvcprt -DEFAULTLIB:mozcrt'
dnl Also pass this to NSPR/NSS
DLLFLAGS="$DLLFLAGS $MOZ_GLUE_LDFLAGS"
else
DLLFLAGS="$DLLFLAGS -LIBPATH:\$(DIST)/lib -DEFAULTLIB:mozglue"
fi
rm crtdll.obj
;;
@ -7256,11 +7253,11 @@ fi # MOZ_MEMORY
AC_SUBST(MOZ_MEMORY)
AC_SUBST(MOZ_JEMALLOC3)
AC_SUBST(MOZ_NATIVE_JEMALLOC)
AC_SUBST(MOZ_CRT)
export MOZ_CRT
AC_SUBST(MOZ_GLUE_LDFLAGS)
AC_SUBST(MOZ_GLUE_PROGRAM_LDFLAGS)
AC_SUBST_LIST(WIN32_CRT_LIBS)
dnl Need to set this for make because NSS doesn't have configure
AC_SUBST(DLLFLAGS)
dnl We need to wrap dlopen and related functions on Android because we use
dnl our own linker.

View File

@ -3121,6 +3121,7 @@ if test "$MOZ_MEMORY"; then
esac
fi
AC_SUBST(MOZ_MEMORY)
AC_SUBST(MOZ_CRT)
AC_SUBST(MOZ_GLUE_LDFLAGS)
AC_SUBST(MOZ_GLUE_PROGRAM_LDFLAGS)

View File

@ -16,7 +16,7 @@ mozglue.def: mozglue.def.in $(GLOBAL_DEPS)
GARBAGE += mozglue.def
ifneq (,$(filter -DEFAULTLIB:mozcrt,$(MOZ_GLUE_LDFLAGS)))
ifdef MOZ_CRT
# Don't install the import library if we use mozcrt
NO_INSTALL_IMPORT_LIBRARY = 1
endif
@ -35,86 +35,3 @@ endif
ifeq (Android, $(OS_TARGET))
WRAP_LDFLAGS := $(filter -Wl%,$(WRAP_LDFLAGS))
endif
include $(topsrcdir)/config/rules.mk
ifdef MOZ_MEMORY
ifeq (WINNT,$(OS_TARGET))
# Roll our own custom logic here for the import library
###############################################################################
#
# Linking Mozilla itself to jemalloc is not particularly difficult. To do this
# we avoid linking directly to the Microsoft-provided CRT import libraries.
# Instead, we link to our own import library which we generate here. To
# replace the CRT's malloc/free/other memory management symbols we export
# our own versions out of jemalloc.dll. We then take the import library that
# the compiler generates for jemalloc.dll and combine it with the MS CRT import
# libraries. We put our library on the command line first, and the CRT symbols
# are discarded in favor of our versions!
#
# Unfortunately that was too easy. The CRT import library is not a standard
# import library that contains a list of symbols and whatnot. It also includes
# object files that are linked into generated programs. One of these,
# crtdll.obj is (as one might expect) linked into all DLLs that link against
# the CRT. This file does things like run static C++ constructors when the
# DLL is attached, call DllMain, etc.
#
# In the CRT source all malloc/free calls are made to malloc_crt and free_crt.
# In debug builds these are both defined to malloc_dbg and free_dbg. In opt
# builds malloc_crt is an actual function, implemented and exposed from the
# CRT. free_crt is, however, defined to be just plain old free. This works
# fine inside the CRT where malloc_crt and free operate on the same heap.
# Outside the CRT malloc_crt is in the CRT's heap, but free is in jemalloc's
# heap. This causes much pain at shutdown :-(
#
# The obvious solution here is to override malloc_crt too. Unfortunately,
# that doesn't work because the CRT expects to be able to call msize on this
# piece of memory deep inside the CRT, which will fail because it'll call the
# CRT's msize on a pointer in jemalloc's heap.
#
# Our solution to this is quite devious. We take apart the CRT's import lib
# and remove the problematic object file. We then poke at the object file's
# symbol table and replace '__imp__free' (which means grab free from some
# other DLL) with '__imp__frex'. Then we define our own dummy no-op function
# in jemalloc.dll and export it as frex. Then we put the CRT import lib
# back together with the patched crtdll.obj, glue it to the end of jemalloc's
# import library and link the rest of Mozilla to that.
#
# The result? A binary that uses jemalloc, doesn't crash, and leaks a tiny
# amount of memory (32 words per DLL in the 2010 CRT) at shutdown.
#
###############################################################################
target:: mozcrt.lib
$(INSTALL) $(IFLAGS2) mozcrt.lib $(DIST)/lib
# And finally combine that with the jemalloc import library to get an import
# library that has our malloc/free/etc and the CRT's everything else
mozcrt.lib: $(IMPORT_LIBRARY) msvc_modified.lib
lib -OUT:$@ $^
# Put the fixed object file back in
msvc_modified.lib: msvc_removed.lib crtdll_fixed.obj
lib -OUT:$@ $^
# Fix the object file
crtdll_fixed.obj: crtdll.obj
$(PYTHON) $(srcdir)/fixcrt.py
# Find the path of crtdll.obj
CRTDLL_FULLPATH=$(subst \,\\,$(shell lib -list msvc_combined.lib | grep crtdll\\.obj))
# Remove the broken object file, only after we have extracted it
msvc_removed.lib: msvc_combined.lib crtdll.obj
lib -OUT:$@ msvc_combined.lib -REMOVE:$(CRTDLL_FULLPATH)
# Extract the broken object file out of the combined library
crtdll.obj: msvc_combined.lib
lib -OUT:$@ $^ -EXTRACT:$(CRTDLL_FULLPATH)
# Grab both CRT libraries and combine them into one library to simplify things
msvc_combined.lib:
lib -OUT:$@ $(WIN32_CRT_LIBS)
endif
endif # MOZ_MEMORY

81
mozglue/crt/Makefile.in Normal file
View File

@ -0,0 +1,81 @@
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Roll our own custom logic here for the import library
###############################################################################
#
# Linking Mozilla itself to jemalloc is not particularly difficult. To do this
# we avoid linking directly to the Microsoft-provided CRT import libraries.
# Instead, we link to our own import library which we generate here. To
# replace the CRT's malloc/free/other memory management symbols we export
# our own versions out of jemalloc.dll. We then take the import library that
# the compiler generates for jemalloc.dll and combine it with the MS CRT import
# libraries. We put our library on the command line first, and the CRT symbols
# are discarded in favor of our versions!
#
# Unfortunately that was too easy. The CRT import library is not a standard
# import library that contains a list of symbols and whatnot. It also includes
# object files that are linked into generated programs. One of these,
# crtdll.obj is (as one might expect) linked into all DLLs that link against
# the CRT. This file does things like run static C++ constructors when the
# DLL is attached, call DllMain, etc.
#
# In the CRT source all malloc/free calls are made to malloc_crt and free_crt.
# In debug builds these are both defined to malloc_dbg and free_dbg. In opt
# builds malloc_crt is an actual function, implemented and exposed from the
# CRT. free_crt is, however, defined to be just plain old free. This works
# fine inside the CRT where malloc_crt and free operate on the same heap.
# Outside the CRT malloc_crt is in the CRT's heap, but free is in jemalloc's
# heap. This causes much pain at shutdown :-(
#
# The obvious solution here is to override malloc_crt too. Unfortunately,
# that doesn't work because the CRT expects to be able to call msize on this
# piece of memory deep inside the CRT, which will fail because it'll call the
# CRT's msize on a pointer in jemalloc's heap.
#
# Our solution to this is quite devious. We take apart the CRT's import lib
# and remove the problematic object file. We then poke at the object file's
# symbol table and replace '__imp__free' (which means grab free from some
# other DLL) with '__imp__frex'. Then we define our own dummy no-op function
# in jemalloc.dll and export it as frex. Then we put the CRT import lib
# back together with the patched crtdll.obj, glue it to the end of jemalloc's
# import library and link the rest of Mozilla to that.
#
# The result? A binary that uses jemalloc, doesn't crash, and leaks a tiny
# amount of memory (32 words per DLL in the 2010 CRT) at shutdown.
#
###############################################################################
target:: mozcrt.lib
$(INSTALL) $(IFLAGS2) mozcrt.lib $(DIST)/lib
# And finally combine that with the jemalloc import library to get an import
# library that has our malloc/free/etc and the CRT's everything else
mozcrt.lib: ../build/mozglue.lib msvc_modified.lib
lib -OUT:$@ $^
# Put the fixed object file back in
msvc_modified.lib: msvc_removed.lib crtdll_fixed.obj
lib -OUT:$@ $^
# Fix the object file
crtdll_fixed.obj: crtdll.obj
$(PYTHON) $(srcdir)/fixcrt.py
# Find the path of crtdll.obj
CRTDLL_FULLPATH=$(subst \,\\,$(shell lib -list msvc_combined.lib | grep crtdll\\.obj))
# Remove the broken object file, only after we have extracted it
msvc_removed.lib: msvc_combined.lib crtdll.obj
lib -OUT:$@ msvc_combined.lib -REMOVE:$(CRTDLL_FULLPATH)
# Extract the broken object file out of the combined library
crtdll.obj: msvc_combined.lib
lib -OUT:$@ $^ -EXTRACT:$(CRTDLL_FULLPATH)
# Grab both CRT libraries and combine them into one library to simplify things
msvc_combined.lib:
lib -OUT:$@ $(WIN32_CRT_LIBS)

16
mozglue/crt/moz.build Normal file
View File

@ -0,0 +1,16 @@
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Library('mozcrt')
STATIC_LIBRARY_NAME = 'mozcrt_s'
OS_LIBS += [
'-NODEFAULTLIB:msvcrt',
'-NODEFAULTLIB:msvcprt',
'-LIBPATH:%s/mozglue/crt' % TOPOBJDIR,
'-DEFAULTLIB:mozcrt',
]

View File

@ -12,4 +12,7 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'android':
DIRS += ['build']
if CONFIG['MOZ_CRT']:
DIRS += ['crt']
TEST_DIRS += ['tests']

View File

@ -1158,11 +1158,15 @@ INSTALL_TARGETS += %(prefix)s
# handled in moz.build world, assume any program or shared library
# we build depends on it.
if obj.KIND == 'target' and not isinstance(obj, StaticLibrary) and \
build_target != 'mozglue/build/target' and \
build_target not in ('mozglue/build/target',
'mozglue/crt/target') and \
not obj.config.substs.get('JS_STANDALONE') and \
(not isinstance(obj, SharedLibrary) or
obj.basename != 'clang-plugin'):
self._compile_graph[build_target].add('mozglue/build/target')
if obj.config.substs.get('MOZ_CRT'):
self._compile_graph[build_target].add('mozglue/crt/target')
else:
self._compile_graph[build_target].add('mozglue/build/target')
if obj.config.substs.get('MOZ_MEMORY'):
self._compile_graph[build_target].add('memory/build/target')