Browse Source

Early RemoveUnusedSections pass for external data variables, in order to get rid of unused data sections.

git-svn-id: file:///var/svn/tigccpp/trunk/tigcc/ld-tigcc@166 9552661e-59e3-4036-b4f2-dbe53926924f
kevinkofler 19 years ago
parent
commit
3b762c4946
3 changed files with 67 additions and 0 deletions
  1. 38 0
      gcunused.c
  2. 15 0
      gcunused.h
  3. 14 0
      main.c

+ 38 - 0
gcunused.c

@@ -116,3 +116,41 @@ void RemoveUnusedSections (PROGRAM *Program)
 	}
 }
 
+// The following 2 functions are currently needed only for external data
+// variable support.
+#ifdef DATA_VAR_SUPPORT
+
+// Mark the section containing __main as Referenced. This is a kludge
+// compensating for the fact that the startup section referencing __main has not
+// been imported at that stage.
+void MarkMainSection (PROGRAM *Program)
+{
+	SECTION *Section;
+		
+	// For each section...
+	for_each (Section, Program->Sections)
+	{
+		SYMBOL *Symbol;
+			
+		// For each symbol...
+		for_each (Symbol, Section->Symbols)
+		{
+			// If the name matches, we have found the right symbol.
+			if (Symbol->Exported && (!(strcmp ("__main", Symbol->Name))))
+				MarkSection (Section);
+		}
+	}
+}
+
+// Clear the Referenced flag of all sections, in order to be able to run a
+// second RemoveUnusedSections pass at a later point.
+void ResetReferencedFlags (PROGRAM *Program)
+{
+	SECTION *Section;
+
+	for_each (Section, Program->Sections)
+		Section->Referenced = FALSE;
+}
+
+#endif /* DATA_VAR_SUPPORT */
+

+ 15 - 0
gcunused.h

@@ -26,4 +26,19 @@
 // Remove all unused sections.
 void RemoveUnusedSections (PROGRAM *Program);
 
+// The following 2 functions are currently needed only for external data
+// variable support.
+#ifdef DATA_VAR_SUPPORT
+
+// Mark the section containing __main as Referenced. This is a kludge
+// compensating for the fact that the startup section referencing __main has not
+// been imported at that stage.
+void MarkMainSection (PROGRAM *Program);
+
+// Clear the Referenced flag of all sections, in order to be able to run a
+// second RemoveUnusedSections pass at a later point.
+void ResetReferencedFlags (PROGRAM *Program);
+
+#endif /* DATA_VAR_SUPPORT */
+
 #endif

+ 14 - 0
main.c

@@ -242,6 +242,20 @@ int main (int ArgCount, const char **Args)
 			// sections.
 			if (DatVarInfo->Name)
 			{
+				if (OptInfo->RemoveUnused && (!(Program.Frozen)))
+				{
+					// Mark the section containing __main as referenced.
+					MarkMainSection (&Program);
+					// Remove unreferenced sections now, before constant merging
+					// and section merging make it impossible.
+					RemoveUnusedSections (&Program);
+					// Reset the Referenced flags so we can do another GC pass
+					// when the imports are done.
+					ResetReferencedFlags (&Program);
+					
+					DoSpecialDump (1, "(early-cut)");
+				}
+				
 				if (OptInfo->MergeConstants && (!(Program.Frozen)))
 				{
 					// Merge constants now, as we can't do it anymore after