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