Browse Source

Add support for DWARF 2 in ld-tigcc.

git-svn-id: file:///var/svn/tigccpp/trunk/tigcc/ld-tigcc@207 9552661e-59e3-4036-b4f2-dbe53926924f
kevinkofler 19 years ago
parent
commit
fa6f284525
3 changed files with 106 additions and 9 deletions
  1. 19 5
      data.h
  2. 60 4
      export/exp_dbg.c
  3. 27 0
      import/imp_coff.c

+ 19 - 5
data.h

@@ -51,11 +51,25 @@ struct GLOBAL_IMPORT;
 typedef enum {PT_NATIVE = 1, PT_NOSTUB = 2, PT_KERNEL = 0, PT_NOSTUB_DLL = 3, PT_FLASH_OS = 4, PT_FARGO = 5} ProgramTypes;
 
 // Debugging Information Section Type Enumeration
-// DI_NONE:       Not debugging information (same as FALSE)
-// DI_STAB:       Stabs symbol table
-// DI_STABSTR:    Stabs string table
-// DI_LAST:       Last debugging information type, for iterating purposes
-typedef enum {DI_NONE = FALSE, DI_STAB = 1, DI_STABSTR = 2, DI_LAST = 2} DebuggingInfoTypes;
+// DI_NONE:           Not debugging information (same as FALSE)
+// DI_STAB:           Stabs symbol table
+// DI_STABSTR:        Stabs string table
+// DI_DEBUG_ABBREV:   DWARF 2 .debug_abbrev section
+// DI_DEBUG_ARANGES:  DWARF 2 .debug_aranges section
+// DI_DEBUG_FRAME:    DWARF 2 .debug_frame section
+// DI_DEBUG_INFO:     DWARF 2 .debug_info section
+// DI_DEBUG_LINE:     DWARF 2 .debug_line section
+// DI_DEBUG_LOC:      DWARF 2 .debug_loc section
+// DI_DEBUG_MACINFO:  DWARF 2 .debug_macinfo section
+// DI_DEBUG_PUBNAMES: DWARF 2 .debug_pubnames section
+// DI_DEBUG_STR:      DWARF 2 .debug_str section
+// DI_EH_FRAME:       DWARF 2 .eh_frame section (used only for debugging in TIGCC)
+// DI_LAST:           Last debugging information type, for iterating purposes
+typedef enum {DI_NONE = FALSE, DI_STAB = 1, DI_STABSTR = 2, DI_DEBUG_ABBREV = 3,
+              DI_DEBUG_ARANGES = 4, DI_DEBUG_FRAME = 5, DI_DEBUG_INFO = 6,
+              DI_DEBUG_LINE = 7, DI_DEBUG_LOC = 8, DI_DEBUG_MACINFO = 9,
+              DI_DEBUG_PUBNAMES = 10, DI_DEBUG_STR = 11, DI_EH_FRAME = 12,
+              DI_LAST = 12} DebuggingInfoTypes;
 
 typedef I4 VERSION;
 

+ 60 - 4
export/exp_dbg.c

@@ -73,12 +73,13 @@ static void ApplyIfNonNull(void (*f)(const SECTION *, void *),
 
 static void MapToAllSections(const PROGRAM *Program, void (*f)(const SECTION *, void *), void *UserData)
 {
+	COUNT i;
 	SectionID = 0;
 	ApplyIfNonNull (f, Program->MainSection, UserData);
 	ApplyIfNonNull (f, Program->DataSection, UserData);
 	ApplyIfNonNull (f, Program->BSSSection, UserData);
-	ApplyIfNonNull (f, Program->DebuggingInfoSection[DI_STAB-1], UserData);
-	ApplyIfNonNull (f, Program->DebuggingInfoSection[DI_STABSTR-1], UserData);
+	for (i = 0; i < DI_LAST; i++)
+		ApplyIfNonNull (f, Program->DebuggingInfoSection[i], UserData);
 }
 
 static void CountSectionCOFFSize (const SECTION *Section, void *UserData)
@@ -165,6 +166,8 @@ static void CountSymbols (const SECTION *Section, void *UserData)
 {
 	const SYMBOL *Symbol;
 
+	(*(COUNT *)UserData)++; // section symbol
+
 	for_each (Symbol, Section->Symbols)
 	{
 		(*(COUNT *)UserData)++;
@@ -193,9 +196,14 @@ static void CountSymbolTableOffset (const SECTION *Section, void *UserData)
 static void WriteSectionHeader (const SECTION *Section, void *UserData)
 {
 	static const char SectionNames[MAX_NAMED_SECTION][COFF_SECTION_NAME_LEN] =
-	                  {".text", ".data", ".bss", ".stab", ".stabstr"};
+	                  {".text", ".data", ".bss", ".stab", ".stabstr",
+	                   ".debug_a"/*bbrev*/, ".debug_a"/*ranges*/,
+	                   ".debug_f"/*rame*/, ".debug_i"/*nfo*/, ".debug_l"/*ine*/,
+	                   ".debug_l"/*oc*/, ".debug_m"/*acinfo*/,
+	                   ".debug_p"/*ubnames*/, ".debug_s"/*tr*/, ".eh_fram"/*e*/};
 	static const I4 SectionFlags[MAX_NAMED_SECTION] =
-	                {COFF_SECTION_TEXT, COFF_SECTION_DATA, COFF_SECTION_BSS, 0, 0};
+	                {COFF_SECTION_TEXT, COFF_SECTION_DATA, COFF_SECTION_BSS, 0,
+	                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 	SIZE Size = Section->Size;
 	const RELOC *Reloc;
 	COUNT RelocCount = 0;
@@ -264,6 +272,8 @@ static void FindCOFFSymbolNumber (const SECTION *Section, void *UserData)
 
 	if (((COFFSymbolNumberStruct *)UserData)->Number) return;
 
+	((COFFSymbolNumberStruct *)UserData)->Current++; // section symbol
+
 	for_each (Symbol, Section->Symbols)
 	{
 		if (((COFFSymbolNumberStruct *)UserData)->Symbol == Symbol)
@@ -349,12 +359,47 @@ static void PatchRelocOffsetsOut (const SECTION *Section, void *UserData ATTRIBU
 	}
 }
 
+static const char *SectionFullNames[MAX_NAMED_SECTION] =
+                  {".text", ".data", ".bss", ".stab", ".stabstr",
+                   ".debug_abbrev", ".debug_aranges",
+                   ".debug_frame", ".debug_info", ".debug_line",
+                   ".debug_loc", ".debug_macinfo",
+                   ".debug_pubnames", ".debug_str", ".eh_frame"};
+
 static void WriteSymbolTable (const SECTION *Section, void *UserData)
 {
 	const SYMBOL *Symbol;
 	COUNT COFFSectionNumber = ++(((SectionOffsets *)UserData)->COFFSectionNumber);
 	OFFSET VAddr = VAddrs[COFFSectionNumber];
 
+	// Create a new section symbol with exactly the intended name to get around
+	// the 8 char section name limit.
+	{
+		COFF_SYMBOL COFFSymbol;
+		const char *SectionName = SectionFullNames[SectionID];
+		COUNT NameLen = strlen (SectionName);
+
+		if (NameLen > 8)
+		{
+			// String table entry
+			WriteTI4(*(TI4*)(COFFSymbol.Name.Name), 0);
+			WriteTI4(COFFSymbol.Name.StringRef.StringOffset, ((SectionOffsets *)UserData)->FileOffset);
+			((SectionOffsets *)UserData)->FileOffset += NameLen + 1;
+		}
+		else
+		{
+			memset (COFFSymbol.Name.Name, 0, 8);
+			strncpy (COFFSymbol.Name.Name, SectionName, 8);
+		}
+		WriteTI4 (COFFSymbol.Value, VAddr);
+		WriteTI2 (COFFSymbol.Section, COFFSectionNumber);
+		WriteTI2 (COFFSymbol.Type, 0);
+		WriteTI1 (COFFSymbol.Class, COFF_SYMBOL_LABEL);
+		WriteTI1 (COFFSymbol.AuxSymbolCount, 0);
+
+		ExportWrite (((SectionOffsets *)UserData)->File, &COFFSymbol, sizeof (COFF_SYMBOL), 1);
+	}
+
 	for_each (Symbol, Section->Symbols)
 	{
 		COFF_SYMBOL COFFSymbol;
@@ -386,6 +431,17 @@ static void WriteStringTable (const SECTION *Section, void *UserData)
 {
 	const SYMBOL *Symbol;
 
+	// Section symbol
+	{
+		COUNT NameLen = strlen (SectionFullNames[SectionID]);
+
+		if (NameLen > 8)
+		{
+			// String table entry
+			ExportWrite ((EXP_FILE *)UserData, SectionFullNames[SectionID], NameLen + 1, 1);
+		}
+	}
+
 	for_each (Symbol, Section->Symbols)
 	{
 		COUNT NameLen = strlen (Symbol->Name);

+ 27 - 0
import/imp_coff.c

@@ -175,6 +175,26 @@ BOOLEAN ImportCOFFFile (PROGRAM *Program, const I1 *File, SIZE FileSize, const c
 					Section->DebuggingInfoType = DI_STAB;
 				else if (!strncmp (SectionName, ".stabstr", 8))
 					Section->DebuggingInfoType = DI_STABSTR;
+				else if (!strncmp (SectionName, ".debug_a", 8))
+					Section->DebuggingInfoType = DI_DEBUG_ABBREV;
+					/* (This might also be .debug_aranges, we need to wait for
+					    the section symbol to disambiguate.) */
+				else if (!strncmp (SectionName, ".debug_f", 8))
+					Section->DebuggingInfoType = DI_DEBUG_FRAME;
+				else if (!strncmp (SectionName, ".debug_i", 8))
+					Section->DebuggingInfoType = DI_DEBUG_INFO;
+				else if (!strncmp (SectionName, ".debug_l", 8))
+					Section->DebuggingInfoType = DI_DEBUG_LINE;
+					/* (This might also be .debug_loc, we need to wait for
+					    the section symbol to disambiguate.) */
+				else if (!strncmp (SectionName, ".debug_m", 8))
+					Section->DebuggingInfoType = DI_DEBUG_MACINFO;
+				else if (!strncmp (SectionName, ".debug_p", 8))
+					Section->DebuggingInfoType = DI_DEBUG_PUBNAMES;
+				else if (!strncmp (SectionName, ".debug_s", 8))
+					Section->DebuggingInfoType = DI_DEBUG_STR;
+				else if (!strncmp (SectionName, ".eh_fram", 8))
+					Section->DebuggingInfoType = DI_EH_FRAME;
 #endif
 				Section->CanCutRanges  = AllRelocs;
 				Section->FileName      = FileName;
@@ -277,6 +297,13 @@ BOOLEAN ImportCOFFFile (PROGRAM *Program, const I1 *File, SIZE FileSize, const c
 									SymInfo[CurCOFFSymbolNumber].Symbol = Section->SectionSymbol;
 									// Rename the section symbol to the full section name as stored in the symbol.
 									CreateSectionSymbol (Section, SymName);
+#ifdef DEBUGGING_INFO_SUPPORT
+									// Disambiguate DWARF 2 section names with conflicting 8 char abbreviations.
+									if (!strcmp (SymName, ".debug_aranges"))
+										Section->DebuggingInfoType = DI_DEBUG_ARANGES;
+									else if (!strcmp (SymName, ".debug_loc"))
+										Section->DebuggingInfoType = DI_DEBUG_LOC;
+#endif
 								}
 								else
 								{