This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

export control for Darwin


The attached patch adds exported symbol control on Darwin to
libstdc++.

The Darwin linker doesn't have linker scripts, in fact it doesn't even
have wildcards in the export list.  So, I wrote a perl script which
takes the output of 'nm -P' on the .o files, and kind-of parses the
linker script, and spits out a list of the actual symbols that the
linker script would match.  I think this approach is better than
having a fixed export list for Darwin which we'd have to maintain.

What I'm not so sure about, though, is:
- requiring perl, although note that perl is always available on Darwin
  native, and for cross-builds from a machine which didn't have it
  you could always say --disable-symvers; and
- not changing the name of --enable-symvers.

Any opinions?

I've tested it on powerpc-darwin, and am now testing on x86-linux to
check that I didn't break that.  I'll commit in a day or two if there
are no objections.

-- 
- Geoffrey Keating <geoffk@apple.com>

===File ~/patches/libstdc++-darwinexport-0.patch============
2005-07-25  Geoffrey Keating  <geoffk@apple.com>

	* acinclude.m4 (GLIBCXX_ENABLE_SYMVERS): Handle darwin-export,
	and make it the default for Darwin.
	* scripts/make-exports.pl: New.
	* src/Makefile.am: Update for changes to GLIBCXX_ENABLE_SYMVERS.
	Handle darwin-export.
	* aclocal.m4: Regenerate.
	* configure: Regenerate.
	* include/Makefile.in: Regenerate.
	* libmath/Makefile.in: Regenerate.
	* libsupc++/Makefile.in: Regenerate.
	* po/Makefile.in: Regenerate.
	* src/Makefile.in: Regenerate.
	* testsuite/Makefile.in: Regenerate.

Index: acinclude.m4
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/acinclude.m4,v
retrieving revision 1.320
diff -u -p -u -p -r1.320 acinclude.m4
--- acinclude.m4	28 Jun 2005 19:51:38 -0000	1.320
+++ acinclude.m4	26 Jul 2005 00:40:51 -0000
@@ -1671,16 +1671,30 @@ AC_DEFUN([GLIBCXX_ENABLE_SYMVERS], [
 
 GLIBCXX_ENABLE(symvers,$1,[=STYLE],
   [enables symbol versioning of the shared library],
-  [permit yes|no|gnu])
+  [permit yes|no|gnu|darwin-export])
 
 # If we never went through the GLIBCXX_CHECK_LINKER_FEATURES macro, then we
 # don't know enough about $LD to do tricks...
 AC_REQUIRE([GLIBCXX_CHECK_LINKER_FEATURES])
-# FIXME  The following test is too strict, in theory.
-if test $enable_shared = no ||
-        test "x$LD" = x ||
-        test x$glibcxx_gnu_ld_version = x; then
-  enable_symvers=no
+
+# Turn a 'yes' into a suitable default.
+if test x$enable_symvers = xyes ; then
+  if test $enable_shared = no ||
+     test "x$LD" = x ; then
+    enable_symvers=no
+  elif $with_gnu_ld == yes ; then
+    enable_symvers=gnu
+  else
+    case ${target_os} in
+      darwin*)
+	enable_symvers=darwin-export ;;
+      *)
+      AC_MSG_WARN([=== You have requested some kind of symbol versioning, but])
+      AC_MSG_WARN([=== you are not using a supported linker.])
+      AC_MSG_WARN([=== Symbol versioning will be disabled.])
+	enable_symvers=no ;;
+    esac
+  fi
 fi
 
 # Check to see if libgcc_s exists, indicating that shared libgcc is possible.
@@ -1710,39 +1724,34 @@ changequote([,])dnl
   AC_MSG_RESULT($glibcxx_shared_libgcc)
 fi
 
-# For GNU ld, we need at least this version.  The format is described in
-# GLIBCXX_CHECK_LINKER_FEATURES above.
-glibcxx_min_gnu_ld_version=21400
-
-# Check to see if unspecified "yes" value can win, given results above.
-# Change "yes" into either "no" or a style name.
-if test $enable_symvers = yes; then
-  if test $with_gnu_ld = yes &&
-     test $glibcxx_shared_libgcc = yes;
-  then
-    if test $glibcxx_gnu_ld_version -ge $glibcxx_min_gnu_ld_version ; then
-      enable_symvers=gnu
-    else
-      # The right tools, the right setup, but too old.  Fallbacks?
-      AC_MSG_WARN(=== Linker version $glibcxx_gnu_ld_version is too old for)
-      AC_MSG_WARN(=== full symbol versioning support in this release of GCC.)
-      AC_MSG_WARN(=== You would need to upgrade your binutils to version)
-      AC_MSG_WARN(=== $glibcxx_min_gnu_ld_version or later and rebuild GCC.)
-      if test $glibcxx_gnu_ld_version -ge 21200 ; then
-        # Globbing fix is present, proper block support is not.
-        AC_MSG_WARN([=== Symbol versioning will be disabled.])
-        enable_symvers=no
-      else
-        # 2.11 or older.
-        AC_MSG_WARN([=== Symbol versioning will be disabled.])
-        enable_symvers=no
-      fi
-    fi
-  else
-    # just fail for now
+# If no shared libgcc, can't win.
+if test $glibcxx_shared_libgcc != yes &&
+   test $enable_symvers != no ; then
     AC_MSG_WARN([=== You have requested some kind of symbol versioning, but])
-    AC_MSG_WARN([=== either you are not using a supported linker, or you are])
-    AC_MSG_WARN([=== not building a shared libgcc_s (which is required).])
+    AC_MSG_WARN([=== you are not building a shared libgcc_s.])
+    AC_MSG_WARN([=== Symbol versioning will be disabled.])
+    enable_symvers=no
+  enable_symvers=no
+fi
+
+# Check to see if 'gnu' can win.
+if test $enable_symvers = gnu; then
+  # For GNU ld, we need at least this version.  The format is described in
+  # GLIBCXX_CHECK_LINKER_FEATURES above.
+  glibcxx_min_gnu_ld_version=21400
+
+  if test $with_gnu_ld != yes ; then
+    # just fail for now
+    AC_MSG_WARN([=== You have requested GNU symbol versioning, but])
+    AC_MSG_WARN([=== you are not using the GNU linker.])
+    AC_MSG_WARN([=== Symbol versioning will be disabled.])
+    enable_symvers=no
+  elif test $glibcxx_gnu_ld_version -lt $glibcxx_min_gnu_ld_version ; then
+    # The right tools, the right setup, but too old.  Fallbacks?
+    AC_MSG_WARN(=== Linker version $glibcxx_gnu_ld_version is too old for)
+    AC_MSG_WARN(=== full symbol versioning support in this release of GCC.)
+    AC_MSG_WARN(=== You would need to upgrade your binutils to version)
+    AC_MSG_WARN(=== $glibcxx_min_gnu_ld_version or later and rebuild GCC.)
     AC_MSG_WARN([=== Symbol versioning will be disabled.])
     enable_symvers=no
   fi
@@ -1756,7 +1765,10 @@ case $enable_symvers in
   gnu)
     SYMVER_MAP=config/linker-map.gnu
     AC_DEFINE(_GLIBCXX_SYMVER, 1, 
-              [Define to use symbol versioning in the shared library.])
+              [Define to use GNU symbol versioning in the shared library.])
+    ;;
+  darwin-export)
+    SYMVER_MAP=config/linker-map.gnu
     ;;
 esac
 
@@ -1787,7 +1799,9 @@ AC_MSG_RESULT([$glibcxx_ptrdiff_t_is_i])
 
 AC_SUBST(SYMVER_MAP)
 AC_SUBST(port_specific_symbol_files)
-GLIBCXX_CONDITIONAL(GLIBCXX_BUILD_VERSIONED_SHLIB, test $enable_symvers != no)
+GLIBCXX_CONDITIONAL(ENABLE_SYMVERS_GNU, test $enable_symvers == gnu)
+GLIBCXX_CONDITIONAL(ENABLE_SYMVERS_DARWIN_EXPORT, dnl
+  test $enable_symvers == darwin-export)
 AC_MSG_NOTICE(versioning on shared library symbols is $enable_symvers)
 ])
 
Index: scripts/make_exports.pl
===================================================================
RCS file: scripts/make_exports.pl
diff -N scripts/make_exports.pl
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ scripts/make_exports.pl	26 Jul 2005 00:40:52 -0000
@@ -0,0 +1,144 @@
+#!/usr/bin/perl -w
+
+# This script takes two arguments, a version script and a dynamic library
+# (in that order), and prints a list of symbols to be exported from the
+# library.
+# It expects a 'nm' with the POSIX '-P' option, but everyone has one of
+# those, right?  It also expects that symbol names have a leading underscore,
+# which is somewhat less likely.
+
+use File::Glob ':glob';
+use FileHandle;
+use IPC::Open2;
+
+# The glob patterns that are to be applied to the demangled name
+my @cxx_globs = ();
+# The glob patterns that apply directly to the name in the .o files
+my @globs = ();
+# The patterns for local variables (usually just '*').
+my @ignored = ();
+
+##########
+# Fill in the various glob arrays.
+
+# The next pattern will go into this array.
+my $glob = \@globs;
+my $symvers = shift;
+
+open F,$symvers or die $!;
+
+while (<F>) {
+    chomp;
+    # Lines of the form '} SOME_VERSION_NAME_1.0;'
+    if (/^[ \t]*\}[ \tA-Z0-9_.a-z]*;[ \t]*$/) {
+      $glob = \@globs;
+      next;
+    }
+    # Comment and blank lines
+    next if (/^[ \t]*\#/);
+    next if (/^[ \t]*$/);
+    # Lines of the form 'SOME_VERSION_NAME_1.1 {'
+    next if (/^[A-Z0-9_. \t]*{$/);
+    # Ignore 'global:'
+    next if (/^[ \t]*global:$/);
+    # After 'local:', globs should be ignored, they won't be exported.
+    if (/^[ \t]*local:$/) {
+	$glob = \@ignored;
+	next;
+    }
+    # After 'extern "C++"', globs are C++ patterns
+    if (/^[ \t]*extern \"C\+\+\"[ \t]*$/) {
+	$glob = \@cxx_globs;
+	next;
+    }
+    # Catch globs.  Note that '{}' is not allowed in globs by this script,
+    # so only '*' and '[]' are available.
+    if (/^[ \t]*([^ \t;{}#]+);?[ \t]*$/) {
+	my $ptn = $1;
+	# Turn the glob into a regex by replacing '*' with '.*'.
+	$ptn =~ s/\*/\.\*/g;
+	push @$glob,$ptn;
+	next;
+    }
+    # Important sanity check.  This script can't handle lots of formats
+    # that GNU ld can, so be sure to error out if one is seen!
+    die "strange line `$_'";
+}
+close F;
+
+# Make 'if (1)' for debugging.
+if (0) {
+    print "cxx:\n";
+    (printf "%s\n",$_) foreach (@cxx_globs);
+    print "globs:\n";
+    (printf "%s\n", $_) foreach (@globs);
+    print "ignored:\n";
+    (printf "%s\n", $_) foreach (@ignored);
+}
+
+##########
+# Combine the arrays into single regular expressions
+# This cuts the time required from about 30 seconds to about 0.5 seconds.
+
+my $glob_regex = '^_(' . (join '|',@globs) . ')$';
+my $cxx_regex = (join '|',@cxx_globs);
+
+##########
+# Get all the symbols from the library, match them, and add them to a hash.
+
+my %export_hash = ();
+
+# Process each symbol.
+print STDERR 'nm -P '.(join ' ',@ARGV).'|';
+open NM,'nm -P '.(join ' ',@ARGV).'|' or die $!;
+# Talk to c++filt through a pair of file descriptors.
+open2(*FILTIN, *FILTOUT, "c++filt") or die $!;
+NAME: while (<NM>) {
+    my $i;
+    chomp;
+
+    # nm prints out stuff at the start, ignore it.
+    next if (/^$/);
+    next if (/:$/);
+    # Ignore undefined and local symbols.
+    next if (/^([^ ]+) [Ua-z] /);
+
+    # $sym is the name of the symbol, $noeh_sym is the same thing with
+    # any '.eh' suffix removed.
+    die "unknown nm output $_" if (! /^([^ ]+) [A-Z] /);
+    my $sym = $1;
+    my $noeh_sym = $sym;
+    $noeh_sym =~ s/\.eh$//;
+
+    # Maybe it matches one of the patterns based on the symbol in the .o file.
+    if ($noeh_sym =~ /$glob_regex/) {
+        $export_hash{$sym} = 1;
+	next NAME;
+    }
+
+    # No?  Well, maybe its demangled form matches one of those patterns.
+    printf FILTOUT "%s\n",$noeh_sym;
+    my $dem = <FILTIN>;
+    chomp $dem;
+    if ($dem =~ /$cxx_regex/) {
+        $export_hash{$sym} = 2;
+	next NAME;
+    }
+
+    # No?  Well, then ignore it.
+}
+close NM or die "nm error";
+close FILTOUT or die "c++filt error";
+close FILTIN or die "c++filt error";
+
+##########
+# Print out the export file
+
+# Print information about generating this file
+print "# This is a generated file.\n";
+print "# It was generated by:\n";
+printf "# %s %s %s\n", $0, $symvers, (join ' ',@ARGV);
+
+foreach my $i (keys %export_hash) {
+  printf "%s\n",$i or die;
+}
Index: src/Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/src/Makefile.am,v
retrieving revision 1.149
diff -u -p -u -p -r1.149 Makefile.am
--- src/Makefile.am	17 Jun 2005 07:33:53 -0000	1.149
+++ src/Makefile.am	26 Jul 2005 00:40:52 -0000
@@ -28,7 +28,7 @@ include $(top_srcdir)/fragment.am
 toolexeclib_LTLIBRARIES = libstdc++.la
 
 # Symbol versioning for shared libraries.
-if GLIBCXX_BUILD_VERSIONED_SHLIB
+if ENABLE_SYMVERS_GNU
 version_arg = -Wl,--version-script=libstdc++-symbol.ver
 version_dep = libstdc++-symbol.ver
 libstdc++-symbol.ver:  ${glibcxx_srcdir}/$(SYMVER_MAP)
@@ -40,9 +40,23 @@ libstdc++-symbol.ver:  ${glibcxx_srcdir}
 	  rm tmp.top tmp.bottom; \
 	fi
 else
+if ENABLE_SYMVERS_DARWIN_EXPORT
+version_arg = -Wl,-exported_symbols_list,libstdc++-symbol.explist
+version_dep = libstdc++-symbol.explist
+libstdc++-symbol.explist : ${glibcxx_srcdir}/$(SYMVER_MAP) \
+		${glibcxx_srcdir}/scripts/make_exports.pl \
+		$(libstdc___la_OBJECTS) $(libstdc___la_LIBADD)
+	perl ${glibcxx_srcdir}/scripts/make_exports.pl \
+	  ${glibcxx_srcdir}/$(SYMVER_MAP) \
+	  $(libstdc___la_OBJECTS:%.lo=.libs/%.o) \
+	 `echo $(libstdc___la_LIBADD) | \
+	    sed 's,/\([^/.]*\)\.la,/.libs/\1.a,g'` \
+	 > $@ || (rm -f $@ ; exit 1)
+else
 version_arg =
 version_dep =
 endif
+endif
 
 
 # Source files linked in via configuration/make substitution for a
============================================================


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]