exp_dbg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* exp_dbg.c: Routines to export debugging information
  2. Copyright (C) 2002-2004 Sebastian Reichelt
  3. Copyright (C) 2005 Kevin Kofler
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #ifdef DEBUGGING_INFO_SUPPORT
  16. /* The format exported by these routines is a COFF file containing both the
  17. actual program and the debugging information (which is stripped out of the
  18. on-calc executable). They are actually object files, but with a ".dbg"
  19. extension to distinguish them from ordinary object files. These files can be
  20. read in by GDB as integrated into TiEmu.
  21. Note that this means that this has to be plain vanilla COFF, NO TIGCC
  22. extensions such as unoptimizable relocs are allowed, unless you are willing
  23. to implement these in GDB/BFD as well. And I really mean IMPLEMENT, not
  24. ignore, cluttering this file with information not used by GDB is useless. As
  25. this format isn't used for linking anyway, the restrictions on the format are
  26. not a problem, and it is pointless to add things not used by GDB. Using
  27. debugging information files for linking purposes or any other non-debugging
  28. purpose is NOT supported. This is NOT a "relocatable linking" feature.
  29. Implementing actual relocatable linking (linking multiple object files into a
  30. single object file) starting from this code is probably possible, but this
  31. code needs to be separate, as relocatable linking SHOULD use TIGCC extensions
  32. in order not to lose information. You'll also have to special-case the
  33. relocatable linking all over the place for things like global imports,
  34. startup sections, merging sections and so on.
  35. -- Kevin Kofler */
  36. #include "exp_dbg.h"
  37. #include "../formats/coff.h"
  38. #include "../manip.h"
  39. #include "../special.h"
  40. #include <string.h>
  41. #define MAX_NAMED_SECTION (3+DI_LAST)
  42. typedef struct {
  43. OFFSET FileOffset;
  44. OFFSET VirtualOffset;
  45. EXP_FILE *File;
  46. COUNT COFFSectionNumber;
  47. } SectionOffsets;
  48. static OFFSET VAddrs[MAX_NAMED_SECTION+1];
  49. static COUNT SectionID;
  50. // These really ought to be written in LISP rather than C, but...
  51. static void ApplyIfNonNull(void (*f)(const SECTION *, void *),
  52. const SECTION *Section, void *UserData)
  53. {
  54. if (Section)
  55. f (Section, UserData);
  56. SectionID++;
  57. }
  58. static void MapToAllSections(const PROGRAM *Program, void (*f)(const SECTION *, void *), void *UserData)
  59. {
  60. SectionID = 0;
  61. ApplyIfNonNull (f, Program->MainSection, UserData);
  62. ApplyIfNonNull (f, Program->DataSection, UserData);
  63. ApplyIfNonNull (f, Program->BSSSection, UserData);
  64. ApplyIfNonNull (f, Program->DebuggingInfoSection[DI_STAB-1], UserData);
  65. ApplyIfNonNull (f, Program->DebuggingInfoSection[DI_STABSTR-1], UserData);
  66. }
  67. static void CountSectionCOFFSize (const SECTION *Section, void *UserData)
  68. {
  69. SIZE Size = sizeof(COFF_SECTION);
  70. const SYMBOL *Symbol;
  71. const RELOC *Reloc;
  72. // The accumulated size should always be at least sizeof(COFF_HEADER).
  73. // Everything else means there was an error.
  74. if (!*(SIZE *)UserData)
  75. return;
  76. Size += Section->Size;
  77. for_each (Symbol, Section->Symbols)
  78. {
  79. COUNT NameLen = strlen (Symbol->Name);
  80. // Symbol table entry
  81. Size += sizeof (COFF_SYMBOL);
  82. // String table entry
  83. if (NameLen > 8)
  84. Size += NameLen + 1;
  85. }
  86. for_each (Reloc, Section->Relocs)
  87. {
  88. // If this can be resolved to a calculator-dependent value, ignore the
  89. // reloc for now.
  90. if (IsPlainCalcBuiltin (Reloc))
  91. continue;
  92. // We can only emit 4-byte absolute relocs.
  93. if (Reloc->Relative || (Reloc->Size != 4))
  94. {
  95. Error (GetFileName (Section, Reloc->Location), "Cannot emit %ld byte %s reloc to `%s'.", (long) Reloc->Size, Reloc->Relative ? "relative" : "absolute", Reloc->Target.SymbolName);
  96. *(SIZE *)UserData = 0;
  97. return;
  98. }
  99. // Reloc table entry
  100. Size += sizeof (COFF_RELOC);
  101. }
  102. *(SIZE *)UserData += Size;
  103. }
  104. // Get the file size needed to export the debugging information file.
  105. // Returns 0 on failure.
  106. // Call this function once to receive necessary error messages.
  107. static SIZE GetDebuggingInfoFileSize (const PROGRAM *Program)
  108. {
  109. SIZE Size = sizeof(COFF_HEADER);
  110. MapToAllSections (Program, CountSectionCOFFSize, &Size);
  111. return Size;
  112. }
  113. static void CompareSections (const SECTION *Section, void *UserData)
  114. {
  115. if (*(const SECTION **)UserData == Section)
  116. *(const SECTION **)UserData = NULL;
  117. }
  118. static void CheckForInvalidSectionContents (const SECTION *Section, void *UserData ATTRIBUTE_UNUSED)
  119. {
  120. if (!(IsEmpty (Section->ROMCalls)))
  121. Warning (Section->FileName, "Can't emit debugging information for ROM calls.");
  122. if (!(IsEmpty (Section->RAMCalls)))
  123. Warning (Section->FileName, "Can't emit debugging information for RAM calls.");
  124. if (!(IsEmpty (Section->LibCalls)))
  125. Warning (Section->FileName, "Can't emit debugging information for library calls.");
  126. }
  127. static void AddOne (const SECTION *Section ATTRIBUTE_UNUSED, void *UserData)
  128. {
  129. (*(COUNT *)UserData)++;
  130. }
  131. static void CountSymbols (const SECTION *Section, void *UserData)
  132. {
  133. const SYMBOL *Symbol;
  134. for_each (Symbol, Section->Symbols)
  135. {
  136. (*(COUNT *)UserData)++;
  137. }
  138. }
  139. static void CountSymbolTableOffset (const SECTION *Section, void *UserData)
  140. {
  141. SIZE Size = Section->Size;
  142. const RELOC *Reloc;
  143. for_each (Reloc, Section->Relocs)
  144. {
  145. // If this can be resolved to a calculator-dependent value, ignore the
  146. // reloc for now.
  147. if (IsPlainCalcBuiltin (Reloc))
  148. continue;
  149. // Reloc table entry
  150. Size += sizeof (COFF_RELOC);
  151. }
  152. *(OFFSET *)UserData += Size;
  153. }
  154. static void WriteSectionHeader (const SECTION *Section, void *UserData)
  155. {
  156. static const char SectionNames[MAX_NAMED_SECTION][COFF_SECTION_NAME_LEN] =
  157. {".text", ".data", ".bss", ".stab", ".stabstr"};
  158. static const I4 SectionFlags[MAX_NAMED_SECTION] =
  159. {COFF_SECTION_TEXT, COFF_SECTION_DATA, COFF_SECTION_BSS, 0, 0};
  160. SIZE Size = Section->Size;
  161. const RELOC *Reloc;
  162. COUNT RelocCount = 0;
  163. COFF_SECTION COFFSectionHeader;
  164. VAddrs[++(((SectionOffsets *)UserData)->COFFSectionNumber)] =
  165. ((SectionOffsets *)UserData)->VirtualOffset;
  166. for_each (Reloc, Section->Relocs)
  167. {
  168. // If this can be resolved to a calculator-dependent value, ignore the
  169. // reloc for now.
  170. if (IsPlainCalcBuiltin (Reloc))
  171. continue;
  172. // Reloc table entry
  173. Size += sizeof (COFF_RELOC);
  174. RelocCount++;
  175. }
  176. memcpy (COFFSectionHeader.Name, SectionNames[SectionID], COFF_SECTION_NAME_LEN);
  177. WriteTI4 (COFFSectionHeader.PhysicalAddress, ((SectionOffsets *)UserData)->VirtualOffset);
  178. WriteTI4 (COFFSectionHeader.VirtualAddress, ((SectionOffsets *)UserData)->VirtualOffset);
  179. WriteTI4 (COFFSectionHeader.Size, Section->Size);
  180. WriteTI4 (COFFSectionHeader.PData, ((SectionOffsets *)UserData)->FileOffset);
  181. WriteTI4 (COFFSectionHeader.PRelocs, ((SectionOffsets *)UserData)->FileOffset + Section->Size);
  182. WriteTI4 (COFFSectionHeader.PLines, 0);
  183. WriteTI2 (COFFSectionHeader.RelocCount, RelocCount);
  184. WriteTI2 (COFFSectionHeader.LineCount, 0);
  185. WriteTI4 (COFFSectionHeader.Flags, SectionFlags[SectionID]);
  186. ExportWrite (((SectionOffsets *)UserData)->File, &COFFSectionHeader, sizeof (COFF_SECTION), 1);
  187. ((SectionOffsets *)UserData)->FileOffset += Size;
  188. ((SectionOffsets *)UserData)->VirtualOffset += Section->Size;
  189. }
  190. typedef struct {
  191. const SECTION *Section;
  192. COUNT Current;
  193. COUNT Number;
  194. } COFFSectionNumberStruct;
  195. static void FindCOFFSectionNumber (const SECTION *Section, void *UserData)
  196. {
  197. ((COFFSectionNumberStruct *)UserData)->Current++;
  198. if (((COFFSectionNumberStruct *)UserData)->Section == Section)
  199. ((COFFSectionNumberStruct *)UserData)->Number = ((COFFSectionNumberStruct *)UserData)->Current;
  200. }
  201. static COUNT GetCOFFSectionNumber (const SECTION *Section)
  202. {
  203. COFFSectionNumberStruct COFFSectionNumber = {Section, 0, 0};
  204. MapToAllSections (Section->Parent, FindCOFFSectionNumber, &COFFSectionNumber);
  205. return COFFSectionNumber.Number;
  206. }
  207. typedef struct {
  208. const SYMBOL *Symbol;
  209. COUNT Current;
  210. COUNT Number;
  211. } COFFSymbolNumberStruct;
  212. static void FindCOFFSymbolNumber (const SECTION *Section, void *UserData)
  213. {
  214. const SYMBOL *Symbol;
  215. if (((COFFSymbolNumberStruct *)UserData)->Number) return;
  216. for_each (Symbol, Section->Symbols)
  217. {
  218. if (((COFFSymbolNumberStruct *)UserData)->Symbol == Symbol)
  219. {
  220. ((COFFSymbolNumberStruct *)UserData)->Number = ((COFFSymbolNumberStruct *)UserData)->Current;
  221. return;
  222. }
  223. ((COFFSymbolNumberStruct *)UserData)->Current++;
  224. }
  225. }
  226. static COUNT GetCOFFSymbolNumber (const SYMBOL *Symbol)
  227. {
  228. COFFSymbolNumberStruct COFFSymbolNumber = {Symbol, 0, 0};
  229. MapToAllSections (Symbol->Parent->Parent, FindCOFFSymbolNumber, &COFFSymbolNumber);
  230. return COFFSymbolNumber.Number;
  231. }
  232. static void PatchRelocOffsetsIn (const SECTION *Section, void *UserData ATTRIBUTE_UNUSED)
  233. {
  234. const RELOC *Reloc;
  235. for_each (Reloc, Section->Relocs)
  236. {
  237. // If this can be resolved to a calculator-dependent value, ignore the
  238. // reloc for now.
  239. if (IsPlainCalcBuiltin (Reloc))
  240. continue;
  241. if (Section->Data)
  242. {
  243. OFFSET RelocVirtualOffset = GetLocationOffset (Section, &(Reloc->Target)) + Reloc->FixedOffset;
  244. RelocVirtualOffset += VAddrs[GetCOFFSectionNumber (Reloc->Target.Symbol->Parent)];
  245. WriteTI4 (*(TI4 *)(Section->Data + Reloc->Location), RelocVirtualOffset);
  246. }
  247. else
  248. Error (GetFileName (Section, Reloc->Location), "Reloc at %lx in BSS section.", (unsigned long) Reloc->Relation);
  249. }
  250. }
  251. static void WriteSectionContents (const SECTION *Section, void *UserData)
  252. {
  253. const RELOC *Reloc;
  254. // Write section data.
  255. ExportWrite (((SectionOffsets *)UserData)->File, Section->Data, Section->Size, 1);
  256. // Write reloc table.
  257. for_each (Reloc, Section->Relocs)
  258. {
  259. COFF_RELOC COFFReloc;
  260. // If this can be resolved to a calculator-dependent value, ignore the
  261. // reloc for now.
  262. if (IsPlainCalcBuiltin (Reloc))
  263. continue;
  264. WriteTI4 (COFFReloc.Location, ((SectionOffsets *)UserData)->VirtualOffset + Reloc->Location);
  265. WriteTI4 (COFFReloc.Symbol, GetCOFFSymbolNumber (Reloc->Target.Symbol));
  266. WriteTI2 (COFFReloc.Type, COFF_RELOC_ABS4);
  267. ExportWrite (((SectionOffsets *)UserData)->File, &COFFReloc, sizeof (COFF_RELOC), 1);
  268. }
  269. // Count VirtualAddress.
  270. ((SectionOffsets *)UserData)->VirtualOffset += Section->Size;
  271. }
  272. static void PatchRelocOffsetsOut (const SECTION *Section, void *UserData ATTRIBUTE_UNUSED)
  273. {
  274. const RELOC *Reloc;
  275. for_each (Reloc, Section->Relocs)
  276. {
  277. // If this can be resolved to a calculator-dependent value, ignore the
  278. // reloc for now.
  279. if (IsPlainCalcBuiltin (Reloc))
  280. continue;
  281. if (Section->Data)
  282. WriteTI4 (*(TI4 *)(Section->Data + Reloc->Location), 0);
  283. }
  284. }
  285. static void WriteSymbolTable (const SECTION *Section, void *UserData)
  286. {
  287. const SYMBOL *Symbol;
  288. COUNT COFFSectionNumber = ++(((SectionOffsets *)UserData)->COFFSectionNumber);
  289. OFFSET VAddr = VAddrs[COFFSectionNumber];
  290. for_each (Symbol, Section->Symbols)
  291. {
  292. COFF_SYMBOL COFFSymbol;
  293. COUNT NameLen = strlen (Symbol->Name);
  294. if (NameLen > 8)
  295. {
  296. // String table entry
  297. WriteTI4(*(TI4*)(COFFSymbol.Name.Name), 0);
  298. WriteTI4(COFFSymbol.Name.StringRef.StringOffset, ((SectionOffsets *)UserData)->FileOffset);
  299. ((SectionOffsets *)UserData)->FileOffset += NameLen + 1;
  300. }
  301. else
  302. {
  303. memset (COFFSymbol.Name.Name, 0, 8);
  304. strncpy (COFFSymbol.Name.Name, Symbol->Name, 8);
  305. }
  306. WriteTI4 (COFFSymbol.Value, VAddr + Symbol->Location);
  307. WriteTI2 (COFFSymbol.Section, COFFSectionNumber);
  308. WriteTI2 (COFFSymbol.Type, 0);
  309. WriteTI1 (COFFSymbol.Class, Symbol->Exported ? COFF_SYMBOL_EXTERNAL : COFF_SYMBOL_LABEL);
  310. WriteTI1 (COFFSymbol.AuxSymbolCount, 0);
  311. ExportWrite (((SectionOffsets *)UserData)->File, &COFFSymbol, sizeof (COFF_SYMBOL), 1);
  312. }
  313. }
  314. static void WriteStringTable (const SECTION *Section, void *UserData ATTRIBUTE_UNUSED)
  315. {
  316. const SYMBOL *Symbol;
  317. for_each (Symbol, Section->Symbols)
  318. {
  319. COUNT NameLen = strlen (Symbol->Name);
  320. if (NameLen > 8)
  321. {
  322. // String table entry
  323. ExportWrite (((SectionOffsets *)UserData)->File, Symbol->Name, NameLen + 1, 1);
  324. }
  325. }
  326. }
  327. // Export the internal data structures into a TIOS file.
  328. static BOOLEAN ExportDebuggingInfoFile (const PROGRAM *Program, EXP_FILE *File, SIZE FileSize ATTRIBUTE_UNUSED)
  329. {
  330. COFF_HEADER COFFHeader;
  331. COUNT SectionCount = 0;
  332. COUNT SymbolCount = 0;
  333. SectionOffsets CurrentSectionOffsets;
  334. OFFSET SymbolTableOffset;
  335. OFFSET StringTableOffset;
  336. const SECTION *Section;
  337. // Check if the file is valid
  338. for_each (Section, Program->Sections)
  339. {
  340. const SECTION *UserData = Section;
  341. MapToAllSections (Program, CompareSections, &UserData);
  342. if (UserData)
  343. Warning (Section->FileName, "Ignoring unmerged section %s",
  344. Section->SectionSymbol->Name);
  345. }
  346. MapToAllSections (Program, CheckForInvalidSectionContents, NULL);
  347. // Write the COFF header
  348. MapToAllSections (Program, AddOne, &SectionCount);
  349. MapToAllSections (Program, CountSymbols, &SymbolCount);
  350. SymbolTableOffset = sizeof (COFF_HEADER) + SectionCount * sizeof (COFF_SECTION);
  351. MapToAllSections (Program, CountSymbolTableOffset, &SymbolTableOffset);
  352. WriteTI2 (COFFHeader.Machine, COFF_MACHINE_M68K);
  353. WriteTI2 (COFFHeader.SectionCount, SectionCount);
  354. WriteTI4 (COFFHeader.TimeStamp, 0);
  355. WriteTI4 (COFFHeader.PSymbols, SymbolTableOffset);
  356. WriteTI4 (COFFHeader.SymbolCount, SymbolCount);
  357. WriteTI2 (COFFHeader.OptHdrSize, 0);
  358. WriteTI2 (COFFHeader.Flags, (COFF_FLAG_32BIT_BE | COFF_FLAG_NO_LINE_NUMBERS));
  359. ExportWrite (File, &COFFHeader, sizeof (COFF_HEADER), 1);
  360. StringTableOffset = SymbolTableOffset + SymbolCount * sizeof (COFF_SYMBOL);
  361. // Write the section headers
  362. CurrentSectionOffsets.FileOffset = sizeof (COFF_HEADER) + SectionCount * sizeof (COFF_SECTION);
  363. CurrentSectionOffsets.VirtualOffset = 0;
  364. CurrentSectionOffsets.File = File;
  365. CurrentSectionOffsets.COFFSectionNumber = 0;
  366. MapToAllSections (Program, WriteSectionHeader, &CurrentSectionOffsets);
  367. // Write the section contents
  368. MapToAllSections (Program, PatchRelocOffsetsIn, NULL);
  369. CurrentSectionOffsets.VirtualOffset = 0;
  370. MapToAllSections (Program, WriteSectionContents, &CurrentSectionOffsets);
  371. MapToAllSections (Program, PatchRelocOffsetsOut, NULL);
  372. // Write the symbol table
  373. CurrentSectionOffsets.FileOffset = StringTableOffset;
  374. CurrentSectionOffsets.COFFSectionNumber = 0;
  375. MapToAllSections (Program, WriteSymbolTable, &CurrentSectionOffsets);
  376. // Write the string table
  377. MapToAllSections (Program, WriteStringTable, NULL);
  378. return TRUE;;
  379. }
  380. // Export the debugging information to a .dbg file.
  381. // This function is a modified version of ExportProgramToFormat.
  382. BOOLEAN ExportDebuggingInfo (const PROGRAM *Program, OUTPUT_FILE_FUNCTION GetOutputFile, OUTPUT_FILE_FINALIZE_FUNCTION FinalizeOutputFile)
  383. {
  384. BOOLEAN Success;
  385. EXP_FILE File;
  386. SIZE Size;
  387. I4 EffectiveSize = 0;
  388. // Fill the output file struct with zeroes.
  389. memset (&File, 0, sizeof (File));
  390. // Get the file size needed to export the file.
  391. Size = GetDebuggingInfoFileSize (Program);
  392. // A size of 0 indicates an error.
  393. if (Size <= 0)
  394. return FALSE;
  395. // Create an output file with the specified format.
  396. if (!(GetOutputFile (&(File.File), Size, 0, FR_DEBUGGING_INFO, FF_GDB_COFF, 0, NULL, FALSE, &EffectiveSize)))
  397. return FALSE;
  398. // Export the program into the output file.
  399. Success = ExportDebuggingInfoFile (Program, &File, Size);
  400. // Finalize and close the output file.
  401. if (FinalizeOutputFile)
  402. FinalizeOutputFile (&(File.File));
  403. return Success;
  404. }
  405. #endif /* DEBUGGING_INFO_SUPPORT */