Browse Source

Break up unused section removal into its own module (gcunused.c, gcunused.h) out of manip.c, manip.h.
Make RemoveSectionIfUnused static in gcunused.c. Don't mention it in the documentation of FreeSection (manip.c). RemoveSectionIfUnused won't work properly anymore outside of RemoveUnusedSections because the referencing information won't have been computed. Nobody outside of RemoveUnusedSections was using it anyway.


git-svn-id: file:///var/svn/tigccpp/trunk/tigcc/ld-tigcc@154 9552661e-59e3-4036-b4f2-dbe53926924f

kevinkofler 19 years ago
parent
commit
a4631dbaf5
6 changed files with 118 additions and 72 deletions
  1. 2 2
      Makefile
  2. 83 0
      gcunused.c
  3. 29 0
      gcunused.h
  4. 1 0
      main.c
  5. 1 61
      manip.c
  6. 2 9
      manip.h

+ 2 - 2
Makefile

@@ -27,9 +27,9 @@ COMPILE_C = $(CC) -c $(CFLAGS) $(ARCHFLAGS) $(WARN_CFLAGS) $(DEFINES)
 
 HEADERS = *.h formats/*.h import/*.h export/*.h bincode/*.h insert/*.h insert/model/*.h int_def.inc
 AR_HEADERS = ar/*.h ar/import/*.h ar/export/*.h *.h formats/*.h
-OBJECTS = main.o integers.o int_arb.o manip.o constmrg.o reorder.o import/import.o import/imp_coff.o import/imp_amig.o import/imp_ar.o export/export.o export/exp_def.o export/exp_os.o export/exp_tios.o export/exp_ndll.o export/exp_farg.o export/exp_data.o bincode/fix_m68k.o bincode/fix_tios.o bincode/fix_emu.o bincode/cutrange.o insert/ins_def.o insert/model/list.o insert/kernel.o insert/comprrlc.o insert/other.o special.o dump.o
+OBJECTS = main.o integers.o int_arb.o manip.o constmrg.o gcunused.o reorder.o import/import.o import/imp_coff.o import/imp_amig.o import/imp_ar.o export/export.o export/exp_def.o export/exp_os.o export/exp_tios.o export/exp_ndll.o export/exp_farg.o export/exp_data.o bincode/fix_m68k.o bincode/fix_tios.o bincode/fix_emu.o bincode/cutrange.o insert/ins_def.o insert/model/list.o insert/kernel.o insert/comprrlc.o insert/other.o special.o dump.o
 AR_OBJECTS = ar/main.o ar/manip.o ar/import/import.o ar/import/imp_coff.o ar/import/imp_amig.o ar/export/exp_ar.o ar/dump.o integers.o
-DLL_OBJECTS = main.do integers.do int_arb.do manip.do constmrg.do reorder.do import/import.do import/imp_coff.do import/imp_amig.do import/imp_ar.do export/export.do export/exp_def.do export/exp_os.do export/exp_tios.do export/exp_ndll.do export/exp_farg.do export/exp_data.do bincode/fix_m68k.do bincode/fix_tios.do bincode/fix_emu.do bincode/cutrange.do insert/ins_def.do insert/model/list.do insert/kernel.do insert/comprrlc.do insert/other.do special.do
+DLL_OBJECTS = main.do integers.do int_arb.do manip.do constmrg.do gcunused.do reorder.do import/import.do import/imp_coff.do import/imp_amig.do import/imp_ar.do export/export.do export/exp_def.do export/exp_os.do export/exp_tios.do export/exp_ndll.do export/exp_farg.do export/exp_data.do bincode/fix_m68k.do bincode/fix_tios.do bincode/fix_emu.do bincode/cutrange.do insert/ins_def.do insert/model/list.do insert/kernel.do insert/comprrlc.do insert/other.do special.do
 DLL_AR_OBJECTS = ar/main.do ar/manip.do ar/import/import.do ar/import/imp_coff.do ar/import/imp_amig.do ar/export/exp_ar.do
 BACKUPS = *~ format/*~ import/*~ export/*~ bincode/*~ insert/*~ insert/model/*~ ar/*~ ar/import/*~ ar/export/*~
 

+ 83 - 0
gcunused.c

@@ -0,0 +1,83 @@
+/* gcunused.c: Routines to remove unused sections
+
+   Copyright (C) 2002-2004 Sebastian Reichelt
+   Copyright (C) 2003-2005 Kevin Kofler
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+#include "gcunused.h"
+
+#include "manip.h"
+#include "special.h"
+
+// Free a section if it is no longer referenced. Update the ReferencedLibCount
+// accordingly.
+static BOOLEAN RemoveSectionIfUnused (SECTION *Section)
+{
+	PROGRAM *Program = Section->Parent;
+	SECTION *OtherSection;
+	LIB_CALL *LibCall, *OtherSecLibCall;
+	
+	// Don't free the section if it is still referenced.
+	if (Section->Referenced)
+		return FALSE;
+
+	// If this section references any libraries, and if it was the last one to
+	// reference them, we need to mark the library as no longer referenced.
+	for_each (LibCall, Section->LibCalls)
+	{
+		LIBRARY *Library = LibCall->Library;
+		if (Library->Referenced)
+		{
+			for_each (OtherSection, Program->Sections)
+			{
+				// Not this section!
+				if (OtherSection == Section) continue;
+				for_each (OtherSecLibCall, OtherSection->LibCalls)
+				{
+					// If this library is still referenced, forget it.
+					if (OtherSecLibCall->Library == Library) goto NextLibCall;
+				}
+			}
+			// The library is no longer referenced after this section is removed.
+			Library->Referenced = FALSE;
+			Program->Libraries.ReferencedCount--;
+		}
+NextLibCall:;
+	}
+
+	// Now free the section.
+	FreeSection (Section);
+	
+	return TRUE;
+}
+
+// Remove all unused sections.
+void RemoveUnusedSections (PROGRAM *Program)
+{
+	SECTION *Section, *NextSection;
+	
+	// For each section...
+	for (Section = GetFirst (Program->Sections); Section; Section = NextSection)
+	{
+		// Get the next section now, since GetNext won't work once the section
+		// has been freed.
+		NextSection = GetNext (Section);
+		
+		// Remove the section if it is unused.
+		RemoveSectionIfUnused (Section);
+	}
+}
+

+ 29 - 0
gcunused.h

@@ -0,0 +1,29 @@
+/* gcunused.h: Routines to remove unused sections
+
+   Copyright (C) 2002-2003 Sebastian Reichelt
+   Copyright (C) 2003-2005 Kevin Kofler
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+#ifndef GCUNUSED_H
+#define GCUNUSED_H
+
+#include "generic.h"
+#include "data.h"
+
+// Remove all unused sections.
+void RemoveUnusedSections (PROGRAM *Program);
+
+#endif

+ 1 - 0
main.c

@@ -23,6 +23,7 @@
 #include "data.h"
 #include "manip.h"
 #include "constmrg.h"
+#include "gcunused.h"
 #include "reorder.h"
 #include "formats/ar.h"
 #include "import/import.h"

+ 1 - 61
manip.c

@@ -80,8 +80,7 @@ void FreeProgram (PROGRAM *Program)
 	memset (Program, 0, sizeof (PROGRAM));
 }
 
-// Free a section. The section is assumed not to be referenced. Use
-// RemoveSectionIfUnused instead if the section might still be referenced.
+// Free a section. The section is assumed not to be referenced.
 void FreeSection (SECTION *Section)
 {
 	PROGRAM *Program = Section->Parent;
@@ -170,48 +169,6 @@ void FreeLocationSymbolName (SECTION *Section, LOCATION *Location)
 	Location->SymbolName = (Location->Symbol ? Location->Symbol->Name : NULL);
 }
 
-// Free a section if it is no longer referenced. Update the ReferencedLibCount
-// accordingly.
-BOOLEAN RemoveSectionIfUnused (SECTION *Section)
-{
-	PROGRAM *Program = Section->Parent;
-	SECTION *OtherSection;
-	LIB_CALL *LibCall, *OtherSecLibCall;
-	
-	// Don't free the section if it is still referenced.
-	if (Section->Referenced)
-		return FALSE;
-
-	// If this section references any libraries, and if it was the last one to
-	// reference them, we need to mark the library as no longer referenced.
-	for_each (LibCall, Section->LibCalls)
-	{
-		LIBRARY *Library = LibCall->Library;
-		if (Library->Referenced)
-		{
-			for_each (OtherSection, Program->Sections)
-			{
-				// Not this section!
-				if (OtherSection == Section) continue;
-				for_each (OtherSecLibCall, OtherSection->LibCalls)
-				{
-					// If this library is still referenced, forget it.
-					if (OtherSecLibCall->Library == Library) goto NextLibCall;
-				}
-			}
-			// The library is no longer referenced after this section is removed.
-			Library->Referenced = FALSE;
-			Program->Libraries.ReferencedCount--;
-		}
-NextLibCall:;
-	}
-
-	// Now free the section.
-	FreeSection (Section);
-	
-	return TRUE;
-}
-
 // Create a section symbol for the given section, if none has been
 // created yet. If there already is one, set its name accordingly.
 // Return the section symbol.
@@ -814,23 +771,6 @@ void OptimizeRelocs (PROGRAM *Program)
 	}
 }
 
-// Remove all unused sections.
-void RemoveUnusedSections (PROGRAM *Program)
-{
-	SECTION *Section, *NextSection;
-	
-	// For each section...
-	for (Section = GetFirst (Program->Sections); Section; Section = NextSection)
-	{
-		// Get the next section now, since GetNext won't work once the section
-		// has been freed.
-		NextSection = GetNext (Section);
-		
-		// Remove the section if it is unused.
-		RemoveSectionIfUnused (Section);
-	}
-}
-
 // Merge section Src into Dest.
 SECTION *MergeSections (SECTION *Dest, SECTION *Src)
 {

+ 2 - 9
manip.h

@@ -1,6 +1,7 @@
 /* manip.h: Routines to manipulate the internal data
 
    Copyright (C) 2002-2003 Sebastian Reichelt
+   Copyright (C) 2003-2005 Kevin Kofler
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -24,8 +25,7 @@
 
 // Free the program tree.
 void FreeProgram (PROGRAM *Program);
-// Free a section. The section is assumed not to be referenced. Use
-// RemoveSectionIfUnused instead if the section might still be referenced.
+// Free a section. The section is assumed not to be referenced.
 void FreeSection (SECTION *Section);
 // Free a relocation entry.
 void FreeReloc (RELOC *Reloc);
@@ -37,10 +37,6 @@ void FreeRelocRelation (RELOC *Reloc);
 // Decrease the number of unresolved relocs in the section.
 void FreeLocationSymbolName (SECTION *Section, LOCATION *Location);
 
-// Free a section if it is no longer referenced. Update the ReferencedLibCount
-// accordingly.
-BOOLEAN RemoveSectionIfUnused (SECTION *Section);
-
 // Create a section symbol for the given section, if none has been
 // created yet. If there already is one, set its name accordingly.
 // Return the section symbol.
@@ -97,9 +93,6 @@ void OptimizeLocation (LOCATION *Location);
 // Optimize all relocs to have the least possible target and relation offset.
 void OptimizeRelocs (PROGRAM *Program);
 
-// Remove all unused sections.
-void RemoveUnusedSections (PROGRAM *Program);
-
 // Merge section Src into Dest.
 // Returns Dest if successful.
 SECTION *MergeSections (SECTION *Dest, SECTION *Src);