kernel.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* kernel.c: Routines to handle automatic insertion of section contents in kernel format
  2. Copyright (C) 2003 Sebastian Reichelt, Kevin Kofler
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #include "kernel.h"
  15. #include "../integers.h"
  16. #include "../manip.h"
  17. #include "../special.h"
  18. #include "model/list.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. // Get the size needed to emit a reloc table in kernel format for the
  22. // items enumerated in the list model specified by Model. SourceSection
  23. // may be NULL.
  24. SIZE GetKernelFormatRelocSize (LIST_MODEL *Model, PROGRAM *Program, SECTION *SourceSection, void *UserData)
  25. {
  26. // We simply need 2 bytes per item, and 2 additional bytes at the end.
  27. return (2 * (GetItemCount (Model, Program, SourceSection, UserData)) + 2);
  28. }
  29. // Emit a reloc table in kernel format for the items enumerated in the
  30. // list model specified by Model. SourceSection may be NULL.
  31. BOOLEAN EmitKernelFormatRelocs (LIST_MODEL *Model, PROGRAM *Program, SECTION *SourceSection, void *UserData, SECTION *Section, I1 **NewData)
  32. {
  33. SECTION *CurSection;
  34. void *NextItem = NULL;
  35. OFFSET Offset;
  36. // For each section (or just for SourceSection)...
  37. for (CurSection = (SourceSection ? : GetFirst (Program->Sections)); CurSection; CurSection = GetNext (CurSection))
  38. {
  39. // For each item in the list...
  40. do {
  41. // Output the target offset, and get the reloc's location.
  42. Offset = Model (CurSection, &NextItem, UserData, FALSE, TRUE);
  43. // A return value < 0 indicates that there were no more items.
  44. if (Offset >= 0)
  45. {
  46. // Create a new reloc at this location, pointing to the
  47. // location of the list item.
  48. if (!(CreateProgramRelativeReloc (Section, NewData, CurSection, Offset, 2)))
  49. return FALSE;
  50. }
  51. } while (NextItem);
  52. // If we only want the items of one section, we have finished.
  53. if (SourceSection)
  54. break;
  55. }
  56. // Output the two zero bytes.
  57. *NewData += 2;
  58. return TRUE;
  59. }
  60. // Append relocation entries in the format required by kernels. If
  61. // TargetSection is NULL, insert all relocation entries that point to
  62. // unhandled sections. Otherwise, insert all relocation entries pointing
  63. // to this section.
  64. // Warning: Inserting relocs is special: Since the relocs are changed
  65. // during the process, they can be inserted only once.
  66. BOOLEAN InsertKernelRelocs (SECTION *Section, SECTION *TargetSection)
  67. {
  68. PROGRAM *Program = Section->Parent;
  69. // Initialize user data for list model.
  70. RELOC_USER_DATA UserData = {TargetSection};
  71. // If a target section is specified, it is referenced now.
  72. if (TargetSection)
  73. TargetSection->Referenced = TRUE;
  74. else
  75. // Do code optimizations now, since this might reduce the number
  76. // of relocs. If a target section was specified, this is pointless,
  77. // as relocs into a separate section can never be optimized away.
  78. FixCode (Program);
  79. {
  80. // Allocate space for the relocs.
  81. I1 *NewData = AllocateSpaceInSection (Section, GetKernelFormatRelocSize ((LIST_MODEL *) RelocListModel, Program, NULL, &UserData));
  82. // Apply the format documented in _kernel_program_header.s.
  83. if (NewData)
  84. {
  85. // Insert the relocs.
  86. if (!(EmitKernelFormatRelocs ((LIST_MODEL *) RelocListModel, Program, NULL, &UserData, Section, &NewData)))
  87. return FALSE;
  88. }
  89. else
  90. return FALSE;
  91. }
  92. // If a target section was specified, and its Handled flag was not
  93. // set yet, now setting it is probably correct.
  94. if (TargetSection)
  95. TargetSection->Handled = TRUE;
  96. return TRUE;
  97. }
  98. // Append relocation entries to TargetSection in the format required by
  99. // kernels. If TargetSection is NULL, AlwaysTerminate specifies whether
  100. // to insert the two terminating zero bytes anyway.
  101. // Warning: Inserting relocs is special: Since the relocs are changed
  102. // during the process, they can be inserted only once.
  103. BOOLEAN InsertKernelSectionRefs (SECTION *Section, SECTION *TargetSection, BOOLEAN AlwaysTerminate)
  104. {
  105. // If the target section exists, this is the same format as for kernels.
  106. // However, if none exists, we need to output the terminating zero
  107. // bytes anyway. However, we cannot use InsertKernelRelocs in this
  108. // case because the TargetSection parameter will be NULL.
  109. if (TargetSection)
  110. return InsertKernelRelocs (Section, TargetSection);
  111. else if (AlwaysTerminate)
  112. // Insert empty relocation table.
  113. return (AllocateSpaceInSection (Section, 2) != NULL);
  114. else
  115. return TRUE;
  116. }
  117. // Append ROM calls in the format required by kernels.
  118. BOOLEAN InsertKernelROMCalls (SECTION *Section)
  119. {
  120. BOOLEAN Result = TRUE;
  121. PROGRAM *Program = Section->Parent;
  122. // Initialize user data for list model.
  123. ROM_CALL_USER_DATA UserData = {NULL, 0, -1};
  124. // Allocate space for dynamic user data.
  125. if (!(UserData.ROMFunctions = calloc (Program->HighestROMCall + 1, sizeof (ROM_CALL_FUNCTION_DATA))))
  126. {
  127. Error (NULL, "Out of memory while inserting ROM calls.");
  128. return FALSE;
  129. }
  130. {
  131. // Go through all ROM calls and increment the appropriate counters.
  132. COUNT ROMRelocCount = GetItemCount ((LIST_MODEL *) ROMCallListModel, Program, NULL, &UserData);
  133. // If no ROM calls are used, do not output anything.
  134. if (ROMRelocCount > 0)
  135. {
  136. // Allocate space for the ROM call numbers and references.
  137. I1 *NewData = AllocateSpaceInSection (Section, (4 * UserData.ROMFunctionCount) + (2 * ROMRelocCount) + 2);
  138. // Apply the format documented in _kernel_program_header.s.
  139. if (NewData)
  140. {
  141. // Output the number of functions used.
  142. WriteTI2 (*((TI2 *) NewData), UserData.ROMFunctionCount - 1);
  143. NewData += 2;
  144. for (UserData.CurFunction = 0; UserData.CurFunction <= Program->HighestROMCall; UserData.CurFunction++)
  145. {
  146. if (UserData.ROMFunctions[UserData.CurFunction].RelocCount)
  147. {
  148. // Output the current function number.
  149. WriteTI2 (*((TI2 *) NewData), UserData.CurFunction);
  150. NewData += 2;
  151. // Emit all ROM calls using this function.
  152. if (!(EmitKernelFormatRelocs ((LIST_MODEL *) ROMCallListModel, Program, NULL, &UserData, Section, &NewData)))
  153. {
  154. Result = FALSE;
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. else
  161. Result = FALSE;
  162. }
  163. }
  164. // Free the extra information.
  165. free (UserData.ROMFunctions);
  166. return Result;
  167. }
  168. // Append RAM calls in the format required by kernels.
  169. BOOLEAN InsertKernelRAMCalls (SECTION *Section)
  170. {
  171. BOOLEAN Result = TRUE;
  172. PROGRAM *Program = Section->Parent;
  173. // Initialize user data for list model.
  174. RAM_CALL_USER_DATA UserData = {{{NULL, 0}, {NULL, 0}, {NULL, 0}, {NULL, 0}}, 0, RT_ALL_TYPES, -1};
  175. // Allocate space for dynamic user data.
  176. RAM_CALL_TYPE_DATA *CurType = UserData.RAMTypes;
  177. RAM_CALL_TYPE LoopType;
  178. for (LoopType = 0; LoopType < RAM_CALL_TYPE_COUNT; LoopType++, CurType++)
  179. {
  180. if (!(CurType->Functions = calloc (Program->HighestRAMCall + 1, sizeof (RAM_CALL_FUNCTION_DATA))))
  181. {
  182. Error (NULL, "Out of memory while inserting RAM calls.");
  183. Result = FALSE;
  184. break;
  185. }
  186. }
  187. if (Result)
  188. {
  189. // Go through all RAM calls and increment the appropriate counters.
  190. COUNT RAMRelocCount = GetItemCount ((LIST_MODEL *) RAMCallListModel, Program, NULL, &UserData);
  191. // If no RAM calls are used, do not output anything.
  192. if (RAMRelocCount)
  193. {
  194. // Allocate space for the RAM call numbers and references.
  195. I1 *NewData = AllocateSpaceInSection (Section, (4 * UserData.RAMFunctionCount) + (2 * RAMRelocCount) + 2);
  196. // Apply the format documented in _kernel_program_header.s.
  197. if (NewData)
  198. {
  199. // Output the number of functions used.
  200. WriteTI2 (*((TI2 *) NewData), UserData.RAMFunctionCount - 1);
  201. NewData += 2;
  202. // For each possible RAM call type...
  203. CurType = UserData.RAMTypes;
  204. for (UserData.CurType = 0; UserData.CurType < RAM_CALL_TYPE_COUNT; UserData.CurType++, CurType++)
  205. {
  206. // Check if the type has been used.
  207. if (CurType->FunctionCount)
  208. {
  209. // Gather information about the type.
  210. SIZE TypeSize = ((UserData.CurType == RT_RAM_CALL_2) || (UserData.CurType == RT_EXTRA_RAM_2) ? 2 : 4);
  211. BOOLEAN TypeExtraRAM = (UserData.CurType == RT_EXTRA_RAM_4) || (UserData.CurType == RT_EXTRA_RAM_2);
  212. I2 TypeFlags = 0;
  213. if (TypeSize < 4)
  214. TypeFlags |= (1 << 15);
  215. if (TypeExtraRAM)
  216. TypeFlags |= (1 << 14);
  217. // For each possible RAM function number...
  218. for (UserData.CurFunction = 0; UserData.CurFunction <= Program->HighestRAMCall; UserData.CurFunction++)
  219. {
  220. // Check if a RAM function of this type and number has been used.
  221. if (CurType->Functions[UserData.CurFunction].RelocCount)
  222. {
  223. // Output the type and number.
  224. if (NewData - Section->Data + 2 <= Section->Size)
  225. WriteTI2 (*((TI2 *) NewData), UserData.CurFunction | TypeFlags);
  226. NewData += 2;
  227. // Emit all RAM calls using this function.
  228. if (!(EmitKernelFormatRelocs ((LIST_MODEL *) RAMCallListModel, Program, NULL, &UserData, Section, &NewData)))
  229. {
  230. Result = FALSE;
  231. break;
  232. }
  233. }
  234. }
  235. }
  236. if (!Result)
  237. break;
  238. }
  239. }
  240. else
  241. Result = FALSE;
  242. }
  243. }
  244. // Free the extra information.
  245. for (UserData.CurType = 0; UserData.CurType < RAM_CALL_TYPE_COUNT; UserData.CurType++)
  246. {
  247. RAM_CALL_FUNCTION_DATA *Functions = UserData.RAMTypes[UserData.CurType].Functions;
  248. if (Functions)
  249. free (Functions);
  250. }
  251. return Result;
  252. }
  253. // Append library calls in the format required by kernels.
  254. BOOLEAN InsertKernelLibraries (SECTION *Section)
  255. {
  256. BOOLEAN Result = TRUE;
  257. PROGRAM *Program = Section->Parent;
  258. // Do not output anything if no libraries are referenced.
  259. if (!(IsEmpty (Program->Libraries)))
  260. {
  261. // Initialize user data for list model.
  262. LIB_CALL_USER_DATA UserData = {0, NULL, 0, 0, NULL, -1, -1};
  263. if (InitializeLibCallUserData (&UserData, Program))
  264. {
  265. // Go through all library calls and increment the appropriate counters.
  266. COUNT LibRelocCount = GetItemCount ((LIST_MODEL *) LibCallListModel, Program, NULL, &UserData);
  267. // Do not output anything if no libraries are referenced.
  268. if (LibRelocCount)
  269. {
  270. // Allocate space for the libraries and relocs.
  271. I1 *NewData = AllocateSpaceInSection (Section, (12 * UserData.UsedLibCount) + (4 * UserData.LibFunctionCount) + (2 * LibRelocCount));
  272. // Apply the format documented in _kernel_program_header.s.
  273. if (NewData)
  274. {
  275. // For each referenced library...
  276. LIB_DATA *CurLib = UserData.Libs;
  277. for (UserData.CurLib = 0; UserData.CurLib < UserData.LibCount; UserData.CurLib++, CurLib++)
  278. {
  279. if (CurLib->FunctionCount)
  280. {
  281. // Output its name.
  282. if (strlen (CurLib->Lib->Name) > 8)
  283. Warning (Section->FileName, "Library name `%s' too long; cutting off at 8th character.", CurLib->Lib->Name);
  284. strncpy (NewData, CurLib->Lib->Name, 8);
  285. WriteTI1 (*((TI1 *) (NewData + 9)), CurLib->Lib->Version);
  286. NewData += 10;
  287. }
  288. }
  289. // For each referenced library...
  290. CurLib = UserData.Libs;
  291. for (UserData.CurLib = 0; UserData.CurLib < UserData.LibCount; UserData.CurLib++, CurLib++)
  292. {
  293. if (CurLib->FunctionCount)
  294. {
  295. // Output the number of exports used.
  296. WriteTI2 (*((TI2 *) NewData), CurLib->FunctionCount - 1);
  297. NewData += 2;
  298. // For each referenced export...
  299. for (UserData.CurFunction = 0; UserData.CurFunction <= CurLib->Lib->Highest; UserData.CurFunction++)
  300. {
  301. if (CurLib->Functions[UserData.CurFunction].RelocCount)
  302. {
  303. // Output the export number.
  304. if (NewData - Section->Data + 2 <= Section->Size)
  305. WriteTI2 (*((TI2 *) NewData), UserData.CurFunction);
  306. NewData += 2;
  307. // Emit all library calls using this export.
  308. if (!(EmitKernelFormatRelocs ((LIST_MODEL *) LibCallListModel, Program, NULL, &UserData, Section, &NewData)))
  309. {
  310. Result = FALSE;
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. else
  319. Result = FALSE;
  320. }
  321. }
  322. else
  323. Result = FALSE;
  324. // Free the extra information.
  325. FinalizeLibCallUserData (&UserData);
  326. }
  327. return Result;
  328. }
  329. #ifdef FARGO_SUPPORT
  330. // Append library calls in the format required by Fargo v0.2.0.
  331. BOOLEAN InsertFargo020Libraries (SECTION *Section)
  332. {
  333. BOOLEAN Result = TRUE;
  334. PROGRAM *Program = Section->Parent;
  335. // Do not output anything if no libraries are referenced.
  336. if (!(IsEmpty (Program->Libraries)))
  337. {
  338. // Initialize user data for list model.
  339. LIB_CALL_USER_DATA UserData = {0, NULL, 0, 0, NULL, -1, -1};
  340. if (InitializeLibCallUserData (&UserData, Program))
  341. {
  342. // Go through all library calls and increment the appropriate counters.
  343. COUNT LibRelocCount = GetItemCount ((LIST_MODEL *) LibCallListModel, Program, NULL, &UserData);
  344. // Do not output anything if no libraries are referenced.
  345. if (LibRelocCount)
  346. {
  347. // Calculate the space needed for the library references.
  348. SIZE RelocSize = (4 * UserData.UsedLibCount) + (4 * UserData.LibFunctionCount) + (2 * LibRelocCount) + 2;
  349. // Calculate the space needed for the library names.
  350. SIZE SizeToAllocate = RelocSize;
  351. LIB_DATA *CurLib = UserData.Libs;
  352. for (UserData.CurLib = 0; UserData.CurLib < UserData.LibCount; UserData.CurLib++, CurLib++)
  353. {
  354. if (CurLib->FunctionCount)
  355. SizeToAllocate += strlen (CurLib->Lib->Name) + 1;
  356. }
  357. // Pad to an even size.
  358. if (SizeToAllocate & 1)
  359. SizeToAllocate++;
  360. // Allocate space for the libraries and relocs.
  361. {
  362. I1 *NewData = AllocateSpaceInSection (Section, SizeToAllocate);
  363. // Apply the format documented in _fargo_program_header.s.
  364. if (NewData)
  365. {
  366. I1 *LibName = NewData + RelocSize;
  367. // For each referenced library...
  368. CurLib = UserData.Libs;
  369. for (UserData.CurLib = 0; UserData.CurLib < UserData.LibCount; UserData.CurLib++, CurLib++)
  370. {
  371. if (CurLib->FunctionCount)
  372. {
  373. // Output its name.
  374. strcpy (LibName, CurLib->Lib->Name);
  375. // Create a new reloc at the current location, pointing to
  376. // the location of the library name.
  377. {
  378. RELOC *Reloc = CreateProgramRelativeReloc (Section, &NewData, Section, LibName - Section->Data, 2);
  379. if (!Reloc)
  380. {
  381. Result = FALSE;
  382. break;
  383. }
  384. }
  385. LibName += strlen (CurLib->Lib->Name) + 1;
  386. // Output the references to it.
  387. {
  388. // For each referenced export...
  389. LIB_FUNCTION_DATA *CurFunction = CurLib->Functions;
  390. for (UserData.CurFunction = 0; UserData.CurFunction <= CurLib->Lib->Highest; UserData.CurFunction++, CurFunction++)
  391. {
  392. if (CurFunction->RelocCount)
  393. {
  394. // Output its number.
  395. if (NewData - Section->Data + 2 <= Section->Size)
  396. WriteTI2 (*((TI2 *) NewData), UserData.CurFunction + 1);
  397. NewData += 2;
  398. // Emit all library calls using this export.
  399. if (!(EmitKernelFormatRelocs ((LIST_MODEL *) LibCallListModel, Program, NULL, &UserData, Section, &NewData)))
  400. {
  401. Result = FALSE;
  402. break;
  403. }
  404. }
  405. }
  406. if (!Result)
  407. break;
  408. }
  409. NewData += 2;
  410. }
  411. }
  412. }
  413. else
  414. Result = FALSE;
  415. }
  416. }
  417. }
  418. else
  419. Result = FALSE;
  420. // Free the extra information.
  421. FinalizeLibCallUserData (&UserData);
  422. }
  423. return Result;
  424. }
  425. #endif /* FARGO_SUPPORT */
  426. // Append exported symbols in the format required by kernels. If
  427. // TrailingZeroBytes is FALSE, use the Fargo v0.2.x format without
  428. // trailing zero bytes.
  429. BOOLEAN InsertKernelExports (SECTION *Section, BOOLEAN TrailingZeroBytes)
  430. {
  431. PROGRAM *Program = Section->Parent;
  432. // Export Counter.
  433. COUNT ExportCount = 0;
  434. // Loop Variables.
  435. SECTION *CurSection;
  436. SYMBOL *CurSymbol;
  437. // Get the number of exports.
  438. // The number of exports is equal to the highest export number + 1.
  439. for_each (CurSection, Program->Sections)
  440. {
  441. for_each (CurSymbol, CurSection->Symbols)
  442. {
  443. if (CurSymbol->Exported)
  444. {
  445. OFFSET ExportNumber = GetExportNumber (CurSymbol->Name);
  446. if ((ExportNumber >= 0) && (ExportCount < ExportNumber + 1))
  447. ExportCount = ExportNumber + 1;
  448. }
  449. }
  450. }
  451. if (ExportCount || TrailingZeroBytes)
  452. {
  453. // Allocate space for the exports.
  454. I1 *NewData = AllocateSpaceInSection (Section, (2 * ExportCount) + (TrailingZeroBytes ? 2 : 0));
  455. // Apply the format documented in _kernel_program_header.s.
  456. if (NewData)
  457. {
  458. // For each section...
  459. for_each (CurSection, Program->Sections)
  460. {
  461. // For each symbol...
  462. for_each (CurSymbol, CurSection->Symbols)
  463. {
  464. // Check if it is exported.
  465. if (CurSymbol->Exported)
  466. {
  467. OFFSET ExportNumber = GetExportNumber (CurSymbol->Name);
  468. if (ExportNumber >= 0)
  469. {
  470. // Create a new reloc at the appropriate location,
  471. // pointing to the exported symbol.
  472. RELOC *Reloc = calloc (1, sizeof (RELOC));
  473. if (Reloc)
  474. {
  475. Reloc->Parent = Section;
  476. Reloc->Location = NewData - Section->Data + 2 * ExportNumber;
  477. Reloc->Size = 2;
  478. Reloc->Target.Symbol = CurSymbol;
  479. Reloc->Target.SymbolName = CurSymbol->Name;
  480. SetRelocProgramRelative (Reloc);
  481. InsertReloc (Section, Reloc);
  482. CurSection->Referenced = TRUE;
  483. }
  484. else
  485. {
  486. Error (NULL, "Out of memory while inserting exports.");
  487. return FALSE;
  488. }
  489. }
  490. }
  491. }
  492. }
  493. }
  494. else
  495. return FALSE;
  496. }
  497. return TRUE;
  498. }