Voilà corrigé:
diff -ur ld-tigcc/constmrg.c ld-tigcc.refcountfix/constmrg.c
--- ld-tigcc/constmrg.c Sun Jul 25 15:37:08 2004
+++ ld-tigcc.refcountfix/constmrg.c Tue Aug 3 00:07:06 2004
@@ -103,8 +103,6 @@
// Delete Constant2.
if (Constant2Length == Section2->Size)
{
- // The section is no longer referenced.
- Section2->Referenced = FALSE;
// Free the section.
FreeSection (Section2);
}
@@ -136,8 +134,6 @@
// Delete Constant1.
if (Constant1Length == Section1->Size)
{
- // The section is no longer referenced.
- Section1->Referenced = FALSE;
// Free the section.
FreeSection (Section1);
}
@@ -208,8 +204,6 @@
// Delete Constant1.
if (Constant1Length == Section1->Size)
{
- // The section is no longer referenced.
- Section1->Referenced = FALSE;
// Free the section.
FreeSection (Section1);
}
@@ -223,8 +217,6 @@
// Delete Constant2.
if (Constant2Length == Section2->Size)
{
- // The section is no longer referenced.
- Section2->Referenced = FALSE;
// Free the section.
FreeSection (Section2);
}
diff -ur ld-tigcc/manip.c ld-tigcc.refcountfix/manip.c
--- ld-tigcc/manip.c Sun Jul 25 15:36:32 2004
+++ ld-tigcc.refcountfix/manip.c Tue Aug 3 00:25:06 2004
@@ -73,7 +73,6 @@
for (Section = GetLast (Program->Sections); Section; Section = NextSection)
{
NextSection = GetPrev (Section);
- Section->Referenced = FALSE;
FreeSection (Section);
}
@@ -81,15 +80,12 @@
memset (Program, 0, sizeof (PROGRAM));
}
-// Free a section. Works only if it is not referenced.
-BOOLEAN FreeSection (SECTION *Section)
+// Free a section. The section is assumed not to be referenced. Use
+// RemoveSectionIfUnused instead if the section might still be referenced.
+void FreeSection (SECTION *Section)
{
PROGRAM *Program = Section->Parent;
- // Don't even try to free the section if it might be referenced.
- if (Section->Referenced)
- return FALSE;
-
// Need to free the data, if any.
if (Section->Data)
free (Section->Data);
@@ -132,8 +128,6 @@
Unlink (Program->Sections, Section);
free (Section);
-
- return TRUE;
}
// Free a relocation entry.
@@ -176,6 +170,48 @@
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.
@@ -741,7 +777,7 @@
NextSection = GetNext (Section);
// Remove the section if it is unused.
- FreeSection (Section);
+ RemoveSectionIfUnused (Section);
}
}
@@ -913,7 +949,6 @@
Dest->Relocs.EmittedCount += Src->Relocs.EmittedCount;
// Free the source section.
- Src->Referenced = FALSE;
FreeSection (Src);
return Dest;
diff -ur ld-tigcc/manip.h ld-tigcc.refcountfix/manip.h
--- ld-tigcc/manip.h Wed Apr 7 19:44:56 2004
+++ ld-tigcc.refcountfix/manip.h Tue Aug 3 00:17:28 2004
@@ -24,8 +24,9 @@
// Free the program tree.
void FreeProgram (PROGRAM *Program);
-// Free a section. Works only if it is not referenced.
-BOOLEAN FreeSection (SECTION *Section);
+// Free a section. The section is assumed not to be referenced. Use
+// RemoveSectionIfUnused instead if the section might still be referenced.
+void FreeSection (SECTION *Section);
// Free a relocation entry.
void FreeReloc (RELOC *Reloc);
// Free the relation reference of a relocation entry, and set it to
@@ -35,6 +36,10 @@
// If a symbol is already known, set the symbol name to its name.
// 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.