special.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /* special.c: Routines to handle special characteristics of the linker
  2. Copyright (C) 2002-2004 Sebastian Reichelt
  3. Copyright (C) 2003-2005 Kevin Kofler
  4. Copyright (C) 2004 Billy Charvet
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  16. #include "special.h"
  17. #include "integers.h"
  18. #include "manip.h"
  19. #include "insert/insert.h"
  20. #include <string.h>
  21. #include <stdlib.h>
  22. // Try to translate an external symbol into a special-feature symbol used by the linker.
  23. // If the symbol contains a number, it is written to the variable pointed to by Number.
  24. SpecialExternalSymbolTypes TranslateSpecialExternalSymbol (PROGRAM *Program, char *SymbolName, void **Reference, OFFSET *Number)
  25. {
  26. SpecialExternalSymbolTypes SymType = ST_NORMAL;
  27. char *SymNum = SymbolName, *Divider = NULL;
  28. int NumBase = 16;
  29. if (!(strncmp (SymbolName, "tiamsapi_", sizeof ("tiamsapi_") - 1)))
  30. {
  31. SymType = ST_ROM_CALL;
  32. SymNum += sizeof ("tiamsapi_") - 1;
  33. NumBase = 10;
  34. }
  35. if (!(strncmp (SymbolName, "_ROM_CALL_", sizeof ("_ROM_CALL_") - 1)))
  36. {
  37. SymType = ST_ROM_CALL;
  38. SymNum += sizeof ("_ROM_CALL_") - 1;
  39. }
  40. else if (!(strncmp (SymbolName, "_RAM_CALL_", sizeof ("_RAM_CALL_") - 1)))
  41. {
  42. SymType = ST_RAM_CALL;
  43. SymNum += sizeof ("_RAM_CALL_") - 1;
  44. }
  45. else if (!(strncmp (SymbolName, "_extraramaddr@", sizeof ("_extraramaddr@") - 1)))
  46. {
  47. SymType = ST_EXTRA_RAM;
  48. SymNum += sizeof ("_extraramaddr@") - 1;
  49. }
  50. else if (!(strncmp (SymbolName, "_extraramaddr__", sizeof ("_extraramaddr__") - 1)))
  51. {
  52. SymType = ST_EXTRA_RAM;
  53. SymNum += sizeof ("_extraramaddr__") - 1;
  54. }
  55. else if (((Divider = strchr (SymbolName, '@'))) || (((Divider = strstr (SymbolName, "__"))) && (SymbolName [0] != '_') && (SymbolName [0] != '.') && (strncmp (SymbolName, "L_", sizeof ("L_") - 1))))
  56. {
  57. if (*Divider == '@')
  58. SymNum = Divider + 1;
  59. else
  60. SymNum = Divider + 2;
  61. if (strlen (SymNum) == 4)
  62. SymType = ST_LIB_CALL;
  63. }
  64. if (SymType != ST_NORMAL)
  65. {
  66. // Get the number the symbol contains (in hex).
  67. if (Number)
  68. {
  69. char *Err;
  70. *Number = strtoul (SymNum, &Err, NumBase);
  71. // If the number has an error in it, revert to normal symbol.
  72. if (*Err)
  73. SymType = ST_NORMAL;
  74. }
  75. }
  76. if (SymType == ST_LIB_CALL)
  77. {
  78. // Cut SymbolName at the divider.
  79. *Divider = 0;
  80. // Get a reference to the library identified by SymbolName.
  81. if (Reference)
  82. {
  83. *Reference = GetLibrary (Program, SymbolName);
  84. if (!(*Reference))
  85. SymType = ST_NORMAL;
  86. }
  87. }
  88. return SymType;
  89. }
  90. // Handle the symbol if it is a special one.
  91. // Returns TRUE if it was special; in this case it should not be used.
  92. BOOLEAN HandleSpecialSymbol (PROGRAM *Program, const char *SymbolName)
  93. {
  94. BOOLEAN SymbolNameMatches (const char *Name)
  95. {
  96. return (!(strcmp (SymbolName, Name)));
  97. }
  98. const char *Divider;
  99. // All special symbols start with an underscore.
  100. if (SymbolName [0] == '_')
  101. {
  102. SymbolName++;
  103. // The most important special symbol: __ref_all_xxx.
  104. // It adds xxx to the global imports.
  105. if (!(strncmp (SymbolName, "_ref_all_", sizeof ("_ref_all_") - 1)))
  106. {
  107. if (!(Program->IgnoreGlobalImports))
  108. AddGlobalImport (Program, SymbolName + sizeof ("_ref_all_") - 1);
  109. }
  110. else if (SymbolNameMatches ("tigcc_native"))
  111. {
  112. // Kernel is the default type. Everything else must be defined explicitly
  113. // and should therefore take precedence over _tigcc_native.
  114. if (Program->Type == PT_KERNEL)
  115. Program->Type = PT_NATIVE;
  116. }
  117. else if (SymbolNameMatches ("nostub"))
  118. {
  119. SECTION *FirstSection = GetFirst (Program->Sections);
  120. if (FirstSection && (!(FirstSection->StartupNumber)))
  121. FirstSection->Essential = TRUE;
  122. Program->Type = PT_NOSTUB;
  123. }
  124. #ifdef NOSTUB_DLL_SUPPORT
  125. else if (SymbolNameMatches ("nostub_dll"))
  126. {
  127. SECTION *FirstSection = GetFirst (Program->Sections);
  128. if (FirstSection && (!(FirstSection->StartupNumber)))
  129. FirstSection->Essential = TRUE;
  130. Program->Type = PT_NOSTUB_DLL;
  131. Program->Library = TRUE;
  132. }
  133. #endif /* NOSTUB_DLL_SUPPORT */
  134. #ifdef FARGO_SUPPORT
  135. else if (SymbolNameMatches ("fargo"))
  136. Program->Type = PT_FARGO;
  137. #endif /* FARGO_SUPPORT */
  138. #ifdef FLASH_OS_SUPPORT
  139. else if (SymbolNameMatches ("flash_os"))
  140. Program->Type = PT_FLASH_OS;
  141. #endif /* FLASH_OS_SUPPORT */
  142. else if (SymbolNameMatches ("library"))
  143. {
  144. Program->Library = TRUE;
  145. #ifdef FARGO_SUPPORT
  146. // Under Fargo, _library is a normal symbol as well.
  147. if (Program->Type == PT_FARGO)
  148. return FALSE;
  149. #endif /* FARGO_SUPPORT */
  150. }
  151. else if (SymbolNameMatches ("ti92"))
  152. {
  153. Program->Calcs |= CALC_TI92;
  154. }
  155. else if (SymbolNameMatches ("ti89"))
  156. {
  157. Program->Calcs |= CALC_TI89;
  158. Program->KernelFlags |= 0x02;
  159. }
  160. else if (SymbolNameMatches ("ti89ti"))
  161. {
  162. Program->Calcs |= CALC_TI89 | CALC_FLAG_TITANIUM;
  163. Program->KernelFlags |= 0x42;
  164. }
  165. else if (SymbolNameMatches ("ti92plus"))
  166. {
  167. Program->Calcs |= CALC_TI92PLUS;
  168. Program->KernelFlags |= 0x01;
  169. }
  170. else if (SymbolNameMatches ("v200"))
  171. {
  172. Program->Calcs |= CALC_V200;
  173. Program->KernelFlags |= 0x20;
  174. }
  175. else if (!(strncmp (SymbolName, "flag_", sizeof ("flag_") - 1)))
  176. Program->KernelFlags |= (1 << strtoul (SymbolName + sizeof ("flag_") - 1, NULL, 10));
  177. else if (!(strncmp (SymbolName, "version", sizeof ("version") - 1)))
  178. Program->Version = strtoul (SymbolName + sizeof ("version") - 1, NULL, 16);
  179. else if (!(strcmp (SymbolName, "_ld_use_fline_jumps")))
  180. {
  181. if (Program->OptimizeInfo)
  182. Program->OptimizeInfo->UseFLineJumps = TRUE;
  183. }
  184. else if (!(strcmp (SymbolName, "_ld_use_4byte_fline_jumps")))
  185. {
  186. if (Program->OptimizeInfo)
  187. Program->OptimizeInfo->Use4ByteFLineJumps = TRUE;
  188. }
  189. else if (!(strcmp (SymbolName, SYM_IGNORE_GLOBAL_IMPORTS + 1)))
  190. Program->IgnoreGlobalImports = TRUE;
  191. else if (SymbolNameMatches (SYM_OMIT_BSS_INIT + 1) || SymbolNameMatches (SYM_ALL_RELOCS + 1))
  192. // These are mostly file-local special symbols; they are handled
  193. // by the importing function.
  194. ;
  195. else
  196. // Not a special symbol.
  197. return FALSE;
  198. // The symbol was handled here.
  199. return TRUE;
  200. }
  201. else if (((Divider = strstr (SymbolName, "@version"))) || (((Divider = strstr (SymbolName, "__version"))) && (SymbolName [0] != '_') && (SymbolName [0] != '.') && (strncmp (SymbolName, "L_", sizeof ("L_") - 1))))
  202. {
  203. // Required minimum version number for a library.
  204. const char *Version;
  205. VERSION VersionNumber;
  206. if (*Divider == '@')
  207. Version = Divider + sizeof ("@version") - 1;
  208. else
  209. Version = Divider + sizeof ("__version") - 1;
  210. VersionNumber = strtoul (Version, NULL, 16);
  211. if (VersionNumber)
  212. {
  213. if (Divider - SymbolName <= MAX_SYM_LEN)
  214. {
  215. char LibName[MAX_SYM_LEN+1];
  216. LIBRARY *Library;
  217. strncpy (LibName, SymbolName, Divider - SymbolName);
  218. LibName [Divider - SymbolName] = 0;
  219. Library = GetLibrary (Program, LibName);
  220. if (Library && (Library->Version < VersionNumber))
  221. Library->Version = VersionNumber;
  222. }
  223. return TRUE;
  224. }
  225. }
  226. return FALSE;
  227. }
  228. // Translate a section name into a startup section number.
  229. // Returns 0 if the name does not represent a startup section.
  230. OFFSET GetStartupSectionNumber (const char *SectionName, SIZE MaxLen)
  231. {
  232. // Check if it looks like a startup section.
  233. if ((MaxLen > (SIZE) (sizeof ("_st") - 1)) && (!(strncmp (SectionName, "_st", sizeof ("_st") - 1))))
  234. {
  235. // Copy the section name to a temporary null-terminated location.
  236. char SecName[MaxLen+1];
  237. memset (SecName, 0, MaxLen + 1);
  238. strncpy (SecName, SectionName, MaxLen);
  239. // Check whether it is a library startup section.
  240. if (SecName [sizeof ("_st") - 1] == 'l')
  241. {
  242. // Library startup sections are special:
  243. // They may always be included, even if the file is not executable.
  244. // Therefore they get a negative number to distinguish them.
  245. OFFSET RealNum = strtoul (SecName + sizeof ("_stl") - 1, NULL, 10);
  246. return (RealNum ? RealNum - 10000 : 0);
  247. }
  248. else
  249. return (strtoul (SecName + sizeof ("_st") - 1, NULL, 10));
  250. }
  251. else
  252. return 0;
  253. }
  254. // Translate a symbol name into the number of an exported function, if the
  255. // function is meant to be exported.
  256. // Returns -1 if it is not an exported function.
  257. OFFSET GetExportNumber (const char *SymbolName)
  258. {
  259. const char *Divider;
  260. if (((Divider = strchr (SymbolName, '@'))) || (((Divider = strstr (SymbolName, "__"))) && (SymbolName [0] != '_') && (SymbolName [0] != '.') && (strncmp (SymbolName, "L_", sizeof ("L_") - 1))))
  261. {
  262. OFFSET Number;
  263. char *Err;
  264. if (*Divider == '@')
  265. Divider++;
  266. else
  267. Divider += 2;
  268. Number = strtoul (Divider, &Err, 16);
  269. if (*Err)
  270. return (-1);
  271. return Number;
  272. }
  273. else
  274. return (-1);
  275. }
  276. // Add all imports with names defined by this program, if they are needed.
  277. BOOLEAN CreateSpecialGlobalImports (PROGRAM *Program)
  278. {
  279. BOOLEAN Result = TRUE;
  280. SECTION *TempSection;
  281. if (Program->Type == PT_KERNEL)
  282. {
  283. if (Program->Library)
  284. Result = Result && AddGlobalImport (Program, "__kernel_library_header");
  285. else
  286. Result = Result && AddGlobalImport (Program, "__kernel_program_header");
  287. }
  288. #ifdef FARGO_SUPPORT
  289. else if (Program->Type == PT_FARGO)
  290. {
  291. if (Program->Library)
  292. Result = Result && AddGlobalImport (Program, "__fargo_library_header");
  293. else
  294. Result = Result && AddGlobalImport (Program, "__fargo_program_header");
  295. }
  296. #endif /* FARGO_SUPPORT */
  297. #ifdef FLASH_OS_SUPPORT
  298. else if (Program->Type == PT_FLASH_OS)
  299. Result = Result && AddGlobalImport (Program, "__flash_os_header");
  300. #endif /* FLASH_OS_SUPPORT */
  301. if (Program->Constructors.Start)
  302. Result = Result && AddGlobalImport (Program, "__handle_constructors");
  303. if (Program->Destructors.Start)
  304. Result = Result && AddGlobalImport (Program, "__handle_destructors");
  305. if (Program->BSSSection && Program->BSSSection->Initialized)
  306. Result = Result && AddGlobalImport (Program, "__initialize_bss");
  307. // Handle BSS section if any.
  308. if (Program->BSSSection && (!(Program->BSSSection->Handled)))
  309. {
  310. GLOBAL_IMPORT *Import = CreateGlobalImport (Program, "__handle_bss");
  311. if (Import)
  312. {
  313. // This is the import that will mark the BSS section as handled
  314. // if it succeeds.
  315. Program->BSSImport = Import;
  316. // Try to resolve it against available archives.
  317. ResolveGlobalImport (Program, Import);
  318. // The BSS section is simply merged into the rest if it is not
  319. // handled.
  320. Import->Succeeded = TRUE;
  321. }
  322. else
  323. Result = FALSE;
  324. }
  325. // Handle absolute relocs if there are any.
  326. for_each (TempSection, Program->Sections)
  327. {
  328. BOOLEAN Done = FALSE;
  329. RELOC *TempReloc;
  330. for_each (TempReloc, TempSection->Relocs)
  331. {
  332. if ((!(TempReloc->Relative || TempReloc->Target.Builtin || (TempReloc->Target.Symbol && (TempReloc->Target.Symbol->Parent->Handled)))))
  333. {
  334. GLOBAL_IMPORT *Import = AddGlobalImport (Program, "__handle_relocs");
  335. if (Import)
  336. // Relocs are either handled by the export format, or a
  337. // warning is emitted.
  338. Import->Succeeded = TRUE;
  339. else
  340. Result = FALSE;
  341. Done = TRUE;
  342. break;
  343. }
  344. }
  345. if (Done)
  346. break;
  347. }
  348. // Handle ROM calls if there are any.
  349. for_each (TempSection, Program->Sections)
  350. {
  351. if (!(IsEmpty (TempSection->ROMCalls)))
  352. {
  353. GLOBAL_IMPORT *Import = AddGlobalImport (Program, "__handle_rom_calls");
  354. if (Import)
  355. // ROM calls are either handled by the export format, or a
  356. // warning is emitted.
  357. Import->Succeeded = TRUE;
  358. else
  359. Result = FALSE;
  360. break;
  361. }
  362. }
  363. // Handle RAM calls if there are any.
  364. for_each (TempSection, Program->Sections)
  365. {
  366. if (!(IsEmpty (TempSection->RAMCalls)))
  367. {
  368. GLOBAL_IMPORT *Import = AddGlobalImport (Program, "__handle_ram_calls");
  369. if (Import)
  370. // RAM calls are either handled by the export format, or a
  371. // warning is emitted.
  372. Import->Succeeded = TRUE;
  373. else
  374. Result = FALSE;
  375. break;
  376. }
  377. }
  378. // Handle libraries if any libraries are referenced.
  379. if (!(IsEmpty (Program->Libraries)))
  380. {
  381. GLOBAL_IMPORT *Import = AddGlobalImport (Program, "__handle_libraries");
  382. if (Import)
  383. // Libraries are either handled by the export format, or a
  384. // warning is emitted.
  385. Import->Succeeded = TRUE;
  386. else
  387. Result = FALSE;
  388. }
  389. // Handle _nostub comments if any.
  390. if ((Program->Type == PT_NATIVE) || (Program->Type == PT_NOSTUB))
  391. {
  392. BOOLEAN NostubComments = FALSE;
  393. for_each (TempSection, Program->Sections)
  394. {
  395. SYMBOL *TempSymbol;
  396. for_each (TempSymbol, TempSection->Symbols)
  397. {
  398. if (TempSymbol->Exported && (!(strncmp (TempSymbol->Name, "_nostub_data__", sizeof ("_nostub_data__") - 1))))
  399. {
  400. OFFSET ExportNumber = GetExportNumber (TempSymbol->Name + sizeof ("_nostub_") - 1);
  401. if (ExportNumber >= 0)
  402. {
  403. NostubComments = TRUE;
  404. TempSection->Essential = TRUE;
  405. break;
  406. }
  407. }
  408. }
  409. }
  410. if (NostubComments)
  411. Result = Result && AddGlobalImport (Program, "__nostub_comment_header");
  412. }
  413. #ifdef DATA_VAR_SUPPORT
  414. // Handle data variable if desired.
  415. if (Program->DataVarInfo->Name && Program->DataSection)
  416. {
  417. Result = Result && (AddGlobalImport (Program, "__handle_data_var"));
  418. if (Program->DataVarInfo->CreateCopy)
  419. {
  420. Result = Result && (AddGlobalImport (Program, "__data_var_create_copy"));
  421. if (Program->DataVarInfo->CopyOnlyIfArchived)
  422. Result = Result && (AddGlobalImport (Program, "__data_var_copy_if_archived"));
  423. }
  424. }
  425. #endif /* DATA_VAR_SUPPORT */
  426. return Result;
  427. }
  428. // If the reloc or its relation point to a special ld-exported symbol,
  429. // change it to the appropriate value if possible. FALSE is returned
  430. // only if it is not a special symbol location, or if there was an error.
  431. BOOLEAN ResolveSpecialSymbolReloc (RELOC *Reloc, BOOLEAN *TryAgain)
  432. {
  433. // At first, resolve the relation, because if this is a built-in
  434. // number, we cannot write it into the section contents directly,
  435. // but we have to subtract it from FixedOffset.
  436. BOOLEAN Result = ResolveSpecialSymbolRelocRelation (Reloc, TryAgain);
  437. // Now resolve the target.
  438. return (ResolveSpecialSymbolRelocTarget (Reloc, TryAgain) ? Result : FALSE);
  439. }
  440. // If the reloc points to a special ld-exported symbol, change it to the
  441. // appropriate value if possible. FALSE is returned only if it is not a
  442. // special symbol reloc, or if there was an error.
  443. BOOLEAN ResolveSpecialSymbolRelocTarget (RELOC *Reloc, BOOLEAN *TryAgain)
  444. {
  445. BOOLEAN Result = FALSE;
  446. SECTION *Section = Reloc->Parent;
  447. LOCATION *Location = &(Reloc->Target);
  448. // If the relation is an unresolved reference to a builtin number,
  449. // do not even try to resolve the target. This would only cause
  450. // trouble, since we would need to handle it immediately if it
  451. // resolved to a number. There will be another call to this anyway
  452. // after the relation has finally been resolved.
  453. // Usually, we would at least need to set the Target.Builtin flag.
  454. // However, currently all tests which check for Target.Builtin also
  455. // check for Relation or Relative in general. If this ever changes,
  456. // this code will break.
  457. if (Reloc->Relation && Reloc->Relation->Builtin)
  458. return TRUE;
  459. if (ResolveSpecialSymbolLocation (Section, Location, TryAgain))
  460. {
  461. // If it has resolved to a number, write it into the section
  462. // contents.
  463. if (!(Location->Symbol || Location->SymbolName))
  464. {
  465. if (Reloc->Relation)
  466. Warning (GetFileName (Section, Reloc->Location), "Ignoring invalid negative reference to `%s' at 0x%lX.", Reloc->Relation->SymbolName, (long) Reloc->Location);
  467. if ((Reloc->Location >= 0) && (Reloc->Location + Reloc->Size <= Section->Size))
  468. {
  469. // Add the target offset (to support things such as "__ld_xxx+1").
  470. OFFSET NewValue = Location->Offset + Reloc->FixedOffset;
  471. if (Reloc->Relative)
  472. Warning (GetFileName (Section, Reloc->Location), "Invalid relative reference to built-in number `%s' at 0x%lX; changing to absolute.", Reloc->Target.SymbolName, (long) Reloc->Location);
  473. // Check if the section contents at the reloc are zero.
  474. if (!(IsZeroDataRange (Section, Reloc->Location, Reloc->Location + Reloc->Size)))
  475. Warning (GetFileName (Section, Reloc->Location), "Builtin reloc at 0x%lX to `%s' on nonzero section contents. Overlapping with another?", (long) Reloc->Location, Reloc->Target.SymbolName);
  476. // Resolve the reloc by writing the value into the section.
  477. Result = AddTI (Section->Data + Reloc->Location, Reloc->Size, NewValue, TRUE, TRUE);
  478. if (!Result)
  479. Error (GetFileName (Section, Reloc->Location), "Number `%s' (=%ld) too large for size %ld reloc at 0x%lX.", Reloc->Target.SymbolName, (long) NewValue, (long) Reloc->Size, (long) Reloc->Location);
  480. }
  481. else
  482. {
  483. Warning (GetFileName (Section, Reloc->Location), "Removing reloc at 0x%lX to `%s' outside of section.", (long) Reloc->Location, Reloc->Target.SymbolName);
  484. Result = TRUE;
  485. }
  486. FreeReloc (Reloc);
  487. }
  488. else
  489. Result = TRUE;
  490. }
  491. return Result;
  492. }
  493. // If the reloc's relation points to a special ld-exported symbol,
  494. // change it to the appropriate value if possible. FALSE is returned
  495. // only if it is not a special symbol location, or if there was an error.
  496. BOOLEAN ResolveSpecialSymbolRelocRelation (RELOC *Reloc, BOOLEAN *TryAgain)
  497. {
  498. BOOLEAN Result = FALSE;
  499. SECTION *Section = Reloc->Parent;
  500. LOCATION *Location = Reloc->Relation;
  501. if (Reloc->Relation)
  502. {
  503. // Try to resolve the relation.
  504. if (ResolveSpecialSymbolLocation (Section, Location, TryAgain))
  505. {
  506. // Check if it has really been resolved.
  507. BOOLEAN Resolved = (Location->Symbol != NULL);
  508. // If it has been resolved to a number, subtract it from
  509. // FixedOffset.
  510. if (!(Location->Symbol || Location->SymbolName))
  511. {
  512. Reloc->FixedOffset -= Location->Offset;
  513. FreeRelocRelation (Reloc);
  514. Resolved = TRUE;
  515. }
  516. Result = TRUE;
  517. }
  518. }
  519. return Result;
  520. }
  521. // If the location points to a special ld-exported symbol, change it to the
  522. // appropriate value if possible. FALSE is returned only if it is not a
  523. // special symbol location, or if there was an error.
  524. // Warning: If the special symbol resolves to a number, Location->Symbol and
  525. // Location->SymbolName will both be NULL, and Location->Offset will contain
  526. // the number.
  527. BOOLEAN ResolveSpecialSymbolLocation (SECTION *Section, LOCATION *Location, BOOLEAN *TryAgain)
  528. {
  529. const char *SymName = Location->SymbolName;
  530. BOOLEAN SymNameMatches (const char *Name)
  531. {
  532. return (!(strcmp (SymName, Name)));
  533. }
  534. // Locations that are already resolved do not need any treatment.
  535. if (Location->Symbol)
  536. return FALSE;
  537. // All special symbols start with '_'.
  538. if (SymName && (SymName [0] == '_'))
  539. {
  540. PROGRAM *Program = Section->Parent;
  541. BOOLEAN SetToEntryPoint = FALSE;
  542. OFFSET NewValue = 0;
  543. SymName++;
  544. // All built-in symbols start with "__ld_".
  545. if (!(strncmp (SymName, SYMPF_BUILTIN + 1, sizeof (SYMPF_BUILTIN) - 2)))
  546. {
  547. SIZE SymNameLength;
  548. BOOLEAN HasValue = FALSE;
  549. SYMBOL *NewSymbol = NULL;
  550. OFFSET NewTargetOffset = 0;
  551. SECTION *TempSection;
  552. // Skip the "__ld_" prefix.
  553. SymName += sizeof (SYMPF_BUILTIN) - 2;
  554. SymNameLength = strlen (SymName);
  555. // Find (but do not resolve) calculator constants.
  556. if (IsCalcBuiltinLocation (Location))
  557. ;
  558. // Resolve references to insertions.
  559. else if (!(strncmp (SymName, SYMPF_INSERT, sizeof (SYMPF_INSERT) - 1)))
  560. {
  561. if ((!(TempSection = Program->MainSection))
  562. || (!(NewSymbol = HandleAutoInsertion (TempSection, Location->SymbolName))))
  563. return TRUE;
  564. }
  565. // Resolve "has_...s".
  566. else if ((!(strncmp (SymName, "has_", sizeof ("has_") - 1))) && (SymName [SymNameLength - 1] == 's'))
  567. {
  568. if (Program->ResolveAllBuiltins)
  569. {
  570. Program->Frozen = TRUE;
  571. if (GetBuiltinValue (Program, SymName + (sizeof ("has_") - 1), SymNameLength - 1 - (sizeof ("has_") - 1), &NewValue, -1))
  572. HasValue = TRUE;
  573. }
  574. }
  575. // Resolve "..._count".
  576. else if ((SymNameLength > ((SIZE) (sizeof ("_count") - 1))) && (!(strcmp (SymName + SymNameLength - (sizeof ("_count") - 1), "_count"))))
  577. {
  578. if (Program->ResolveAllBuiltins)
  579. {
  580. Program->Frozen = TRUE;
  581. if (GetBuiltinValue (Program, SymName, SymNameLength - (sizeof ("_count") - 1), &NewValue, 0))
  582. HasValue = TRUE;
  583. }
  584. }
  585. else if (SymNameMatches ("entry_point"))
  586. SetToEntryPoint = TRUE;
  587. else if (SymNameMatches ("entry_point_plus_0x8000"))
  588. {
  589. SetToEntryPoint = TRUE;
  590. NewValue = 0x8000;
  591. }
  592. else if (SymNameMatches ("constructors_start"))
  593. {
  594. if (!(NewSymbol = Program->Constructors.Start))
  595. return TRUE;
  596. }
  597. else if (SymNameMatches ("constructors_end"))
  598. {
  599. if (!(NewSymbol = Program->Constructors.End))
  600. return TRUE;
  601. }
  602. else if (SymNameMatches ("constructors_size"))
  603. {
  604. if (Program->Constructors.Start && Program->Constructors.End)
  605. {
  606. NewValue = Program->Constructors.End->Location - Program->Constructors.Start->Location;
  607. HasValue = TRUE;
  608. }
  609. else if (Program->ResolveAllBuiltins)
  610. HasValue = TRUE;
  611. }
  612. else if (SymNameMatches ("destructors_start"))
  613. {
  614. if (!(NewSymbol = Program->Destructors.Start))
  615. return TRUE;
  616. }
  617. else if (SymNameMatches ("destructors_end"))
  618. {
  619. if (!(NewSymbol = Program->Destructors.End))
  620. return TRUE;
  621. }
  622. else if (SymNameMatches ("destructors_size"))
  623. {
  624. if (Program->Destructors.Start && Program->Destructors.End)
  625. {
  626. NewValue = Program->Destructors.End->Location - Program->Destructors.Start->Location;
  627. HasValue = TRUE;
  628. }
  629. else if (Program->ResolveAllBuiltins)
  630. HasValue = TRUE;
  631. }
  632. else if (SymNameMatches ("data_start"))
  633. {
  634. if (Program->DataSection)
  635. NewSymbol = Program->DataSection->SectionSymbol;
  636. else
  637. return TRUE;
  638. }
  639. else if (SymNameMatches ("data_end"))
  640. {
  641. if (Program->DataSection)
  642. {
  643. NewSymbol = Program->DataSection->SectionSymbol;
  644. NewTargetOffset = Program->DataSection->Size;
  645. }
  646. else
  647. return TRUE;
  648. }
  649. else if (SymNameMatches ("data_size"))
  650. {
  651. if (Program->DataSection)
  652. {
  653. NewValue = Program->DataSection->Size;
  654. HasValue = TRUE;
  655. }
  656. else if (Program->ResolveAllBuiltins)
  657. HasValue = TRUE;
  658. }
  659. else if (SymNameMatches ("bss_start"))
  660. {
  661. if (Program->BSSSection)
  662. NewSymbol = Program->BSSSection->SectionSymbol;
  663. else
  664. return TRUE;
  665. }
  666. else if (SymNameMatches ("bss_end"))
  667. {
  668. if (Program->BSSSection)
  669. {
  670. NewSymbol = Program->BSSSection->SectionSymbol;
  671. NewTargetOffset = Program->BSSSection->Size;
  672. }
  673. else
  674. return TRUE;
  675. }
  676. else if (SymNameMatches ("bss_size"))
  677. {
  678. if (Program->BSSSection)
  679. {
  680. NewValue = Program->BSSSection->Size;
  681. HasValue = TRUE;
  682. }
  683. else if (Program->ResolveAllBuiltins)
  684. HasValue = TRUE;
  685. }
  686. else if (SymNameMatches ("file_version"))
  687. {
  688. if (Program->ResolveAllBuiltins || Program->Version)
  689. {
  690. NewValue = Program->Version;
  691. HasValue = TRUE;
  692. }
  693. }
  694. else if (SymNameMatches ("kernel_flags"))
  695. {
  696. if (Program->ResolveAllBuiltins)
  697. {
  698. NewValue = Program->KernelFlags;
  699. HasValue = TRUE;
  700. }
  701. }
  702. else if (SymNameMatches ("kernel_bss_table"))
  703. {
  704. if (Program->BSSSection)
  705. {
  706. strcpy ((char *) (Location->SymbolName), "__kernel_bss_table");
  707. if (TryAgain)
  708. *TryAgain = TRUE;
  709. // This prevents the section from being merged, and prevents
  710. // relocs to it from being emitted.
  711. Program->BSSSection->Handled = TRUE;
  712. return FALSE;
  713. }
  714. else if (Program->ResolveAllBuiltins)
  715. SetToEntryPoint = TRUE;
  716. }
  717. else if (SymNameMatches ("program_size"))
  718. {
  719. if (Program->ResolveAllBuiltins && Program->MainSection)
  720. {
  721. NewValue = Program->MainSection->Size;
  722. HasValue = TRUE;
  723. }
  724. else
  725. return TRUE;
  726. }
  727. else if (SymNameMatches ("kernel_export_table"))
  728. {
  729. BOOLEAN HasExports = FALSE;
  730. for_each (TempSection, Program->Sections)
  731. {
  732. SYMBOL *TempSymbol;
  733. for_each (TempSymbol, TempSection->Symbols)
  734. {
  735. if (TempSymbol->Exported)
  736. {
  737. OFFSET ExportNumber = GetExportNumber (TempSymbol->Name);
  738. if (ExportNumber >= 0)
  739. {
  740. HasExports = TRUE;
  741. TempSection->Essential = TRUE;
  742. break;
  743. }
  744. }
  745. }
  746. }
  747. if (HasExports)
  748. {
  749. strcpy ((char *) (Location->SymbolName), "__kernel_export_table");
  750. if (TryAgain)
  751. *TryAgain = TRUE;
  752. return FALSE;
  753. }
  754. else if (Program->ResolveAllBuiltins)
  755. SetToEntryPoint = TRUE;
  756. }
  757. #ifdef DATA_VAR_SUPPORT
  758. else if (SymNameMatches ("data_var_name_end"))
  759. {
  760. // Point the reloc to the terminating zero byte of the name.
  761. if (Program->DataVarInfo->Name)
  762. {
  763. strcpy ((char *) (Location->SymbolName), "__data_var_name_start");
  764. Location->Offset += strlen (Program->DataVarInfo->Name) + 1;
  765. if (TryAgain)
  766. *TryAgain = TRUE;
  767. return FALSE;
  768. }
  769. }
  770. #endif /* DATA_VAR_SUPPORT */
  771. else
  772. return FALSE;
  773. if (!SetToEntryPoint)
  774. {
  775. if (HasValue)
  776. {
  777. // Point the location to the new value.
  778. Location->Symbol = NULL;
  779. FreeLocationSymbolName (Section, Location);
  780. Location->Offset += NewValue;
  781. }
  782. else if (NewSymbol)
  783. {
  784. // Point the location to the new symbol.
  785. Location->Symbol = NewSymbol;
  786. FreeLocationSymbolName (Section, Location);
  787. Location->Offset += NewTargetOffset;
  788. }
  789. else
  790. Location->Builtin = TRUE;
  791. return TRUE;
  792. }
  793. }
  794. // If this is a reloc to a kernel-specific symbol, point it to the
  795. // entry point. The result of this is that the reloc's value
  796. // becomes 0 if the reloc was made up by something like
  797. // _exit-__kernel_entry_point.
  798. else if (SymNameMatches ("exit") || SymNameMatches ("comment") || SymNameMatches ("extraram") || SymNameMatches ("library"))
  799. SetToEntryPoint = TRUE;
  800. if (SetToEntryPoint)
  801. {
  802. if (Program->EntryPoint.Symbol)
  803. {
  804. Location->Symbol = Program->EntryPoint.Symbol;
  805. FreeLocationSymbolName (Section, Location);
  806. Location->Offset += Program->EntryPoint.Offset + NewValue;
  807. }
  808. return TRUE;
  809. }
  810. }
  811. return FALSE;
  812. }
  813. // Count the items for a specific built-in symbol, specified by SymName
  814. // and SymNameLength. If TrueValue is nonzero, items are not counted,
  815. // but NewValue is set to this value if at least one item was found. In
  816. // that case, if Program->ResolveAllBuiltins is false, NewValue may be
  817. // unchanged even though there are some items; you need to check back
  818. // later when Program->ResolveAllBuiltins is true.
  819. BOOLEAN GetBuiltinValue (PROGRAM *Program, const char *SymName, SIZE SymNameLength, OFFSET *NewValue, OFFSET TrueValue)
  820. {
  821. #define Count(n,op,code) \
  822. ({ \
  823. register OFFSET n__ = (n); \
  824. if (TrueValue) \
  825. { \
  826. if (n__) \
  827. { \
  828. *NewValue = TrueValue; \
  829. code; \
  830. } \
  831. } \
  832. else \
  833. *NewValue op n__; \
  834. })
  835. #define SetCounter(n) (Count ((n), =, (void) 0))
  836. #define IncreaseCounter(n) (Count ((n), +=, break))
  837. BOOLEAN SymNameMatches (const char *Name)
  838. {
  839. return (!(strncmp (SymName, Name, SymNameLength)));
  840. }
  841. SECTION *TempSection;
  842. RELOC *TempReloc;
  843. SYMBOL *TempSymbol;
  844. if (SymNameMatches ("constructor"))
  845. {
  846. if (Program->Constructors.Start && Program->Constructors.End)
  847. SetCounter ((Program->Constructors.End->Location - Program->Constructors.Start->Location) >> 2);
  848. }
  849. else if (SymNameMatches ("destructor"))
  850. {
  851. if (Program->Destructors.Start && Program->Destructors.End)
  852. SetCounter ((Program->Destructors.End->Location - Program->Destructors.Start->Location) >> 2);
  853. }
  854. else if (SymNameMatches ("reloc"))
  855. {
  856. // Count all absolute relocs.
  857. // Relative relocs will either be resolved completely or
  858. // produce errors.
  859. for_each (TempSection, Program->Sections)
  860. {
  861. IncreaseCounter (TempSection->Relocs.EmittedCount);
  862. // Since relocs may be removed, if ResolveAllBuiltins
  863. // is false, do not handle TrueValue.
  864. if ((!TrueValue) || Program->ResolveAllBuiltins)
  865. {
  866. for_each (TempReloc, TempSection->Relocs)
  867. if (!(TempReloc->Relative || TempReloc->Target.Builtin || (TempReloc->Target.Symbol && (TempReloc->Target.Symbol->Parent->Handled))))
  868. IncreaseCounter (1);
  869. }
  870. }
  871. }
  872. else if (SymNameMatches ("data_ref"))
  873. {
  874. if (Program->DataSection)
  875. {
  876. // Count all absolute relocs to the data section.
  877. for_each (TempSection, Program->Sections)
  878. for_each (TempReloc, TempSection->Relocs)
  879. if (TempReloc->Target.Symbol && (TempReloc->Target.Symbol->Parent == Program->DataSection) && (!(TempReloc->Relative || TempReloc->Target.Builtin)))
  880. IncreaseCounter (1);
  881. }
  882. }
  883. else if (SymNameMatches ("bss_ref"))
  884. {
  885. if (Program->BSSSection)
  886. {
  887. // Count all absolute relocs to the BSS section.
  888. for_each (TempSection, Program->Sections)
  889. for_each (TempReloc, TempSection->Relocs)
  890. if (TempReloc->Target.Symbol && (TempReloc->Target.Symbol->Parent == Program->BSSSection) && (!(TempReloc->Relative || TempReloc->Target.Builtin)))
  891. IncreaseCounter (1);
  892. }
  893. }
  894. else if (SymNameMatches ("rom_call"))
  895. {
  896. for_each (TempSection, Program->Sections)
  897. IncreaseCounter (CountItems (TempSection->ROMCalls, ROM_CALL));
  898. }
  899. else if (SymNameMatches ("ram_call"))
  900. {
  901. for_each (TempSection, Program->Sections)
  902. IncreaseCounter (CountItems (TempSection->RAMCalls, RAM_CALL));
  903. }
  904. else if (SymNameMatches ("lib"))
  905. SetCounter (CountItems (Program->Libraries, LIBRARY));
  906. else if (SymNameMatches ("referenced_lib"))
  907. SetCounter (Program->Libraries.ReferencedCount);
  908. else if (SymNameMatches ("export"))
  909. {
  910. // The number of exports is equal to the highest export number + 1.
  911. for_each (TempSection, Program->Sections)
  912. {
  913. for_each (TempSymbol, TempSection->Symbols)
  914. {
  915. if (TempSymbol->Exported)
  916. {
  917. OFFSET ExportNumber = GetExportNumber (TempSymbol->Name);
  918. if ((ExportNumber >= 0) && (*NewValue < ExportNumber + 1))
  919. Count (ExportNumber + 1, =, break);
  920. }
  921. }
  922. }
  923. }
  924. else if (SymNameMatches ("nostub_comment"))
  925. {
  926. for_each (TempSection, Program->Sections)
  927. {
  928. for_each (TempSymbol, TempSection->Symbols)
  929. {
  930. if (TempSymbol->Exported && (!(strncmp (TempSymbol->Name, SYMPF_NOSTUB_DATA, sizeof (SYMPF_NOSTUB_DATA) - 1))))
  931. {
  932. OFFSET ExportNumber = GetExportNumber (TempSymbol->Name + (sizeof (SYMPF_NOSTUB_DATA_START) - 1));
  933. if (ExportNumber >= 0)
  934. IncreaseCounter (1);
  935. }
  936. }
  937. }
  938. }
  939. else
  940. return FALSE;
  941. #undef IncreaseCounter
  942. #undef SetCounter
  943. #undef Count
  944. return TRUE;
  945. }
  946. // If the given symbol name belongs to a calculator-specific builtin
  947. // symbol, return a pointer to the part of it that holds the values.
  948. static const char *GetCalcBuiltinValues (const char *SymName)
  949. {
  950. if (!(strncmp (SymName, SYMPF_BUILTIN_CALC_CONST, sizeof (SYMPF_BUILTIN_CALC_CONST) - 1)))
  951. {
  952. return (SymName + (sizeof (SYMPF_BUILTIN_CALC_CONST) - 1));
  953. }
  954. return NULL;
  955. }
  956. // Check if the given location points to a calculator-specific builtin
  957. // symbol.
  958. BOOLEAN IsCalcBuiltinLocation (const LOCATION *Location)
  959. {
  960. return (Location->SymbolName && (GetCalcBuiltinValues (Location->SymbolName) || (!(strcmp (Location->SymbolName, SYM_BUILTIN_HARDWARE_ID)))));
  961. }
  962. // Return whether the reloc can be resolved to a calculator-specific value.
  963. // ResolveSpecialSymbol or something related must have been called on the
  964. // reloc at least once.
  965. BOOLEAN IsPlainCalcBuiltin (const RELOC *Reloc)
  966. {
  967. // To improve speed, check whether the target and relation (if it exists)
  968. // are builtin symbols.
  969. if (Reloc->Target.Builtin && ((!(Reloc->Relation)) || Reloc->Relation->Builtin))
  970. {
  971. // Check the target.
  972. if (IsCalcBuiltinLocation (&(Reloc->Target)))
  973. {
  974. // If there is a relation, check it.
  975. if (Reloc->Relation)
  976. return IsCalcBuiltinLocation (Reloc->Relation);
  977. // Otherwise, the reloc may not be relative, since that would
  978. // mean that it cannot be resolved to a number.
  979. else
  980. return (!(Reloc->Relative));
  981. }
  982. }
  983. return FALSE;
  984. }
  985. // If the location can be resolved to a calculator-specific value, get the
  986. // value for the specified calculator.
  987. // If IsCalcBuiltinLocation returned a positive result for this reloc, this
  988. // function will not return a negative result.
  989. BOOLEAN GetCalcBuiltinLocationValue (const LOCATION *Location, ProgramCalcs DestCalc, IMAX *Value)
  990. {
  991. // Basic sanity checks.
  992. if (Value && Location->SymbolName)
  993. {
  994. // Special case: __ld_hardware_id
  995. if (!(strcmp (Location->SymbolName, SYM_BUILTIN_HARDWARE_ID)))
  996. {
  997. switch (DestCalc)
  998. {
  999. case CALC_TI89:
  1000. *Value = 3;
  1001. break;
  1002. case CALC_TI89 | CALC_FLAG_TITANIUM:
  1003. *Value = 9;
  1004. break;
  1005. case CALC_TI92PLUS:
  1006. *Value = 1;
  1007. break;
  1008. case CALC_V200:
  1009. *Value = 8;
  1010. break;
  1011. default:
  1012. Warning (NULL, SYM_BUILTIN_HARDWARE_ID " not defined for this calculator.");
  1013. *Value = 0;
  1014. }
  1015. *Value += Location->Offset;
  1016. return TRUE;
  1017. }
  1018. else
  1019. {
  1020. // Get the part of the symbol name that holds the values,
  1021. // separated by '_'.
  1022. const char *Values = GetCalcBuiltinValues (Location->SymbolName);
  1023. if (Values)
  1024. {
  1025. // AND out the calculator flags.
  1026. DestCalc &= ~CALC_FLAG_TITANIUM;
  1027. // While we still have at least one value left...
  1028. while (Values)
  1029. {
  1030. // Get the end of the value string.
  1031. const char *ValueEnd = strchr (Values, '_');
  1032. SIZE ValueSize = ValueEnd ? (SIZE) (ValueEnd - Values) : (SIZE) (strlen (Values));
  1033. // If this is the value that belongs to the current calculator,
  1034. // extract the value and return.
  1035. if (DestCalc == 1)
  1036. {
  1037. char *EndPtr = NULL;
  1038. // Create a copy of the value, with a terminating zero byte.
  1039. char ValueStr[ValueSize+1];
  1040. strncpy (ValueStr, Values, ValueSize);
  1041. ValueStr [ValueSize] = 0;
  1042. // Convert this string into a number and return it.
  1043. *Value = strtoul (ValueStr, &EndPtr, 0);
  1044. if (EndPtr && *EndPtr)
  1045. Warning (NULL, "Invalid number `%s' in `%s'.", ValueStr, Location->SymbolName);
  1046. *Value += Location->Offset;
  1047. return TRUE;
  1048. }
  1049. // Advance to the next value.
  1050. Values = ValueEnd ? ValueEnd + 1 : NULL;
  1051. // Advance to the next calculator.
  1052. DestCalc >>= 1;
  1053. }
  1054. // No more values were found, but the function did not exit yet.
  1055. Warning (NULL, "Calculator constant `%s' contains too few values.", Location->SymbolName);
  1056. *Value = 0;
  1057. // We have to return a positive result anyway because
  1058. // IsCalcBuiltinLocation would as well.
  1059. return TRUE;
  1060. }
  1061. }
  1062. }
  1063. return FALSE;
  1064. }
  1065. // If the reloc can be resolved to a calculator-specific value, get the
  1066. // value for the specified calculator.
  1067. // The return value is the same as for IsPlainCalcBuiltin.
  1068. BOOLEAN GetCalcBuiltinValue (const RELOC *Reloc, ProgramCalcs DestCalc, IMAX *Value)
  1069. {
  1070. if (Value)
  1071. {
  1072. // Get the value for the target.
  1073. if (GetCalcBuiltinLocationValue (&(Reloc->Target), DestCalc, Value))
  1074. {
  1075. // If there is a relation, subtract its value.
  1076. if (Reloc->Relation)
  1077. {
  1078. IMAX RelationValue;
  1079. if (!(GetCalcBuiltinLocationValue (Reloc->Relation, DestCalc, &RelationValue)))
  1080. return FALSE;
  1081. *Value -= RelationValue;
  1082. }
  1083. // Otherwise, the reloc may not be relative
  1084. // (see IsPlainCalcBuiltin).
  1085. else if (Reloc->Relative)
  1086. return FALSE;
  1087. *Value += Reloc->FixedOffset;
  1088. return TRUE;
  1089. }
  1090. }
  1091. return FALSE;
  1092. }
  1093. // If required by some special symbol(s) at the end of the section,
  1094. // modify the contents of the section.
  1095. // This can be used to insert special items such as relocation entries.
  1096. // MergedSection specifies the (usually large) part of the program that
  1097. // has already been merged.
  1098. // If necessary, MergedSection is frozen automatically.
  1099. BOOLEAN HandleSectionContents (SECTION *Section, SECTION *MergedSection)
  1100. {
  1101. SYMBOL *Symbol;
  1102. // Search the labels at the end of the secion to find special ones.
  1103. for (Symbol = FindSymbolAtPos (Section, Section->Size, TRUE); Symbol; Symbol = GetNext (Symbol))
  1104. {
  1105. if (!(strncmp (Symbol->Name, SYMPF_BUILTIN_INSERT, sizeof (SYMPF_BUILTIN_INSERT) - 1)))
  1106. return (HandleInsertion (Section, Symbol->Location, Symbol->Name + sizeof (SYMPF_BUILTIN_INSERT) - 1, MergedSection, FALSE));
  1107. }
  1108. return TRUE;
  1109. }
  1110. // Insert contents for an insertion specified by SymbolName, and return
  1111. // a symbol from where the insertion took place.
  1112. // If necessary, Section is frozen automatically.
  1113. SYMBOL *HandleAutoInsertion (SECTION *Section, const char *SymbolName)
  1114. {
  1115. if (!(strncmp (SymbolName, SYMPF_BUILTIN_INSERT, sizeof (SYMPF_BUILTIN_INSERT) - 1)))
  1116. {
  1117. CreateSectionSegment (Section);
  1118. // All insertions except compressed ones should be aligned on a 2-byte boundary.
  1119. if ((!(strncmp (SymbolName, SYMPF_BUILTIN_INSERT_COMPRESSED, sizeof (SYMPF_BUILTIN_INSERT_COMPRESSED) - 1)))
  1120. || (!(strncmp (SymbolName, SYMPF_BUILTIN_INSERT_MLINK, sizeof (SYMPF_BUILTIN_INSERT_MLINK) - 1)))
  1121. || (!(strcmp (SymbolName, SYMPF_BUILTIN_INSERT "fargo021_relocs")))
  1122. || (!(strcmp (SymbolName, SYMPF_BUILTIN_INSERT "preos_compressed_tables")))
  1123. || PadSection (Section, 2))
  1124. {
  1125. // Create a new symbol at the end of the section.
  1126. SYMBOL *Result = calloc (1, sizeof (SYMBOL));
  1127. if (Result)
  1128. {
  1129. Result->Parent = Section;
  1130. Result->Location = Section->Size;
  1131. strncpy (Result->Name, SymbolName, MAX_SYM_LEN);
  1132. Result->Exported = TRUE;
  1133. Append (Section->Symbols, Result);
  1134. // Insert the data.
  1135. if (AppendInsertionData (Section, SymbolName + sizeof (SYMPF_BUILTIN_INSERT) - 1, Section, TRUE))
  1136. return Result;
  1137. }
  1138. else
  1139. Error (NULL, "Out of memory.");
  1140. }
  1141. }
  1142. return NULL;
  1143. }
  1144. // Handle an insertion by cutting the section off at the specified location
  1145. // and inserting the contents specified by the name, taking into account
  1146. // that MergedSection specifies the (usually large) part of the program
  1147. // that has already been merged.
  1148. BOOLEAN HandleInsertion (SECTION *Section, OFFSET Location, const char *Name, SECTION *MergedSection, BOOLEAN AlwaysTerminate)
  1149. {
  1150. if (Location == Section->Size)
  1151. return AppendInsertionData (Section, Name, MergedSection, AlwaysTerminate);
  1152. else
  1153. return TRUE;
  1154. }
  1155. // Append the data required by an insertion (specified by name) to the
  1156. // section specified by Section, taking into account that MergedSection
  1157. // specifies the (usually large) part of the program that has already
  1158. // been merged.
  1159. BOOLEAN AppendInsertionData (SECTION *Section, const char *Name, SECTION *MergedSection, BOOLEAN AlwaysTerminate)
  1160. {
  1161. BOOLEAN NameMatches (const char *InsertionName)
  1162. {
  1163. return (!(strcmp (Name, InsertionName)));
  1164. }
  1165. PROGRAM *Program = Section->Parent;
  1166. #ifdef DATA_VAR_SUPPORT
  1167. // Data variable name.
  1168. if (NameMatches ("data_var_name"))
  1169. return InsertDataVarName (Section);
  1170. else
  1171. #endif /* DATA_VAR_SUPPORT */
  1172. // Nostub-specific formats.
  1173. if (NameMatches ("nostub_comments"))
  1174. return InsertNostubComments (Section);
  1175. // Kernel-specific formats.
  1176. else if (NameMatches ("kernel_relocs"))
  1177. return InsertKernelRelocs (Section, NULL);
  1178. else if (NameMatches ("kernel_bss_refs"))
  1179. return InsertKernelSectionRefs (Section, Program->BSSSection, AlwaysTerminate);
  1180. else if (NameMatches ("kernel_data_refs"))
  1181. return InsertKernelSectionRefs (Section, Program->DataSection, AlwaysTerminate);
  1182. else if (NameMatches ("kernel_rom_calls"))
  1183. return InsertKernelROMCalls (Section);
  1184. else if (NameMatches ("kernel_ram_calls"))
  1185. return InsertKernelRAMCalls (Section);
  1186. else if (NameMatches ("kernel_libs"))
  1187. return InsertKernelLibraries (Section);
  1188. else if (NameMatches ("kernel_exports"))
  1189. return InsertKernelExports (Section, TRUE);
  1190. #ifdef FARGO_SUPPORT
  1191. // Fargo-specific formats.
  1192. else if (NameMatches ("fargo_exports"))
  1193. return InsertKernelExports (Section, FALSE);
  1194. else if (NameMatches ("fargo020_bss_refs"))
  1195. return InsertKernelSectionRefs (Section, Program->BSSSection, TRUE);
  1196. else if (NameMatches ("fargo020_libs"))
  1197. return InsertFargo020Libraries (Section);
  1198. #endif /* FARGO_SUPPORT */
  1199. // PreOS-specific formats.
  1200. else if (NameMatches ("preos_compressed_tables"))
  1201. return InsertPreOsCompressedTables (Section);
  1202. // Other compressed formats.
  1203. else
  1204. {
  1205. char *ReferenceName = malloc ((sizeof (SYMPF_BUILTIN) - 1) + strlen (Name) + sizeof ("_ref"));
  1206. if (ReferenceName)
  1207. {
  1208. // Build the reference symbol name: "__ld_" Name "_ref".
  1209. strcpy (ReferenceName, SYMPF_BUILTIN);
  1210. strcat (ReferenceName, Name);
  1211. strcat (ReferenceName, "_ref");
  1212. {
  1213. LOCATION Reference = {NULL, ReferenceName, 0, FALSE};
  1214. // Try to find a reference symbol. If none is found, use
  1215. // the program entry point.
  1216. Section->Relocs.UnresolvedCount++;
  1217. if (!(ResolveLocation (Program, Section, &Reference)))
  1218. {
  1219. FreeLocationSymbolName (Section, &Reference);
  1220. Reference = Program->EntryPoint;
  1221. }
  1222. #ifdef FARGO_SUPPORT
  1223. // Fargo-specific formats.
  1224. if (NameMatches ("fargo021_relocs"))
  1225. return InsertCompressedRelocs (Section, NULL, MergedSection, &Reference);
  1226. else if (NameMatches ("fargo021_bss_refs"))
  1227. return InsertFargo021SectionRefs (Section, Program->BSSSection, MergedSection, &Reference);
  1228. else if (NameMatches ("fargo021_libs"))
  1229. return InsertFargo021Libraries (Section, MergedSection, &Reference);
  1230. #endif /* FARGO_SUPPORT */
  1231. // Compressed relocation tables using our own format.
  1232. else if (NameMatches ("compressed_relocs"))
  1233. return InsertCompressedRelocs (Section, NULL, MergedSection, &Reference);
  1234. else if (NameMatches ("compressed_bss_refs"))
  1235. return InsertCompressedSectionRefs (Section, Program->BSSSection, MergedSection, &Reference);
  1236. else if (NameMatches ("compressed_data_refs"))
  1237. return InsertCompressedSectionRefs (Section, Program->DataSection, MergedSection, &Reference);
  1238. else if (NameMatches ("compressed_rom_calls"))
  1239. return InsertCompressedROMCalls (Section, MergedSection, &Reference);
  1240. // Compressed relocation tables using our own mlink-style format.
  1241. else if (NameMatches ("mlink_relocs"))
  1242. return InsertMlinkRelocs (Section, NULL, MergedSection, &Reference);
  1243. else if (NameMatches ("mlink_bss_refs"))
  1244. return InsertMlinkSectionRefs (Section, Program->BSSSection, MergedSection, &Reference);
  1245. else if (NameMatches ("mlink_data_refs"))
  1246. return InsertMlinkSectionRefs (Section, Program->DataSection, MergedSection, &Reference);
  1247. else if (NameMatches ("mlink_rom_calls"))
  1248. return InsertMlinkROMCalls (Section, MergedSection, &Reference);
  1249. else
  1250. Warning (GetFileName (Section, Section->Size), "Unrecognized insertion `%s'.", Name);
  1251. }
  1252. }
  1253. else
  1254. {
  1255. Error (NULL, "Out of memory.");
  1256. return FALSE;
  1257. }
  1258. }
  1259. return TRUE;
  1260. }