[MLton-commit] r6702

Wesley Terpstra wesley at mlton.org
Tue Aug 19 05:41:22 PDT 2008


First, a bit of background:

When a C compiler sees this global variable:
	int foo = 4;
it puts it in the data section. It is an error if two files define the same
symbol in the data section. However, this global variable definition:
	int foo;
places the variable (on some platforms) into the common section. The idea is
that if two files both declare 'foo' in this way, they refer to the same 
variable. Generally, however, you want to write:
	extern int foo;
in all files except one which defines foo. The extern directive makes the
symbol undefined and it becomes an error to link without an object which
defines foo (as either common or data).

The common symbol concept is convenience for programmers who aren't careful
about where they define their symbols. It's also non-portable to define
	int foo;
in multiple files. It will work on *nix, but not everywhere.

A problem with exposing the common symbols of a MLton library is that a user
linking to it can accidentally access and modify these symbols if he has
variables of the same name. This could cause bad behaviour as MLton expects
to be the only actor modifying these variables. Also, if an application
links in two MLton libraries, many symbols will definitely overlap and
quite likely interfere with each other.

Therefore, when compiling to a library, "common" symbols must be eliminated
and replaced with actual definitions. This way each library has it's own
private copy of the symbol. This is the purpose of the '-d' ld option.

Unfortunately, it seems that 'ld -d' is broken on osx. After allocating
common symbols, the resulting object file no longer links. Since MLton is
quite careful about where it defines symbols (since it is portable), the 
whole common-symbol infrastructure for sloppy programmers is unnecessary.
In fact, if MLton ever depended on this behaviour, it might become a 
portability bug.

Therefore, a simple fix never outputs common symbols in MLton. It is then
unnecessary to allocate them at link time. That's what this patch changes.

An alternative to "-fno-common" would be to change all uses of:
	int foo;
	struct bar baz;
to:
	int foo = 0;
	struct bar baz = { };

However, the command-line option seems easier.


----------------------------------------------------------------------

U   mlton/trunk/bin/mlton-script
U   mlton/trunk/bin/static-library
U   mlton/trunk/runtime/Makefile

----------------------------------------------------------------------

Modified: mlton/trunk/bin/mlton-script
===================================================================
--- mlton/trunk/bin/mlton-script	2008-08-18 21:25:21 UTC (rev 6701)
+++ mlton/trunk/bin/mlton-script	2008-08-19 12:41:21 UTC (rev 6702)
@@ -84,9 +84,9 @@
         -ar-script "$lib/static-library"                         \
         -cc "$gcc"                                               \
         -cc-opt-quote "-I$lib/include"                           \
-        -cc-opt '-O1'                                            \
+        -cc-opt '-O1 -fno-common'                                \
         -cc-opt '-fno-strict-aliasing -fomit-frame-pointer -w'   \
-        -link-opt '-lm -lgmp'                            \
+        -link-opt '-lm -lgmp'                                    \
         -mlb-path-map "$lib/mlb-path-map"                        \
         -target-as-opt amd64 '-m64'                              \
         -target-cc-opt amd64 '-m64'                              \

Modified: mlton/trunk/bin/static-library
===================================================================
--- mlton/trunk/bin/static-library	2008-08-18 21:25:21 UTC (rev 6701)
+++ mlton/trunk/bin/static-library	2008-08-19 12:41:21 UTC (rev 6702)
@@ -33,7 +33,7 @@
 rm -f "${output}"
 
 if "$partialLink"; then
-  "${target}ld" -r -d -o "$output.o" "$@"
+  "${target}ld" -r -o "$output.o" "$@"
   # The osx linker already makes hidden symbols local, so we can skip this
   if [ "$(uname -s)" != "Darwin" ]; then
     "${target}objdump" -t "$output.o" \

Modified: mlton/trunk/runtime/Makefile
===================================================================
--- mlton/trunk/runtime/Makefile	2008-08-18 21:25:21 UTC (rev 6701)
+++ mlton/trunk/runtime/Makefile	2008-08-19 12:41:21 UTC (rev 6702)
@@ -30,7 +30,7 @@
 		sed 's/.*gcc version \([0-9][0-9]*\)\.\([0-9][0-9]*\).*/\2/')
 GCC_VERSION := $(GCC_MAJOR_VERSION).$(GCC_MINOR_VERSION)
 
-FLAGS :=
+FLAGS := -fno-common
 EXE :=
 OPTFLAGS := -O2 -fomit-frame-pointer
 DEBUGFLAGS := -O1 -fno-inline -fkeep-inline-functions -g2




More information about the MLton-commit mailing list