Index: src/InitTweak/FixGlobalInit.cc
===================================================================
--- src/InitTweak/FixGlobalInit.cc	(revision 64071c2ad8eac1915b692988a8dc0d8e9d7cb8da)
+++ src/InitTweak/FixGlobalInit.cc	(revision 7baed7d624d6fc245ee9ff6f08edb3226adfc288)
@@ -22,4 +22,5 @@
 #include "SynTree/Initializer.h"
 #include "SynTree/Visitor.h"
+#include "SynTree/Attribute.h"
 #include <algorithm>
 
@@ -116,7 +117,20 @@
 	GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
 		std::string fixedName = globalFunctionName( name );
-		initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Constructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) );
-
-		destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Destructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) );
+		std::list< Expression * > ctorParameters;
+		std::list< Expression * > dtorParameters;
+		if ( inLibrary ) {
+			// Constructor/destructor attributes take a single parameter which
+			// is the priority, with lower numbers meaning higher priority.
+			// Functions specified with priority are guaranteed to run before
+			// functions without a priority. To ensure that constructors and destructors
+			// for library code are run before constructors and destructors for user code,
+			// specify a priority when building the library. Priorities 0-100 are reserved by gcc.
+			ctorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
+			dtorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
+		}
+		initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
+		initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
+		destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
+		destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
 	}
 
