special.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. /* special.c: Routines to handle special characteristics of the linker
  2. Copyright (C) 2002-2004 Sebastian Reichelt
  3. Copyright (C) 2003-2004 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->Referenced = 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->Referenced = 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->Referenced = 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 (GetBuiltinValue (Program, SymName + (sizeof ("has_") - 1), SymNameLength - 1 - (sizeof ("has_") - 1), &NewValue, -1))
  569. HasValue = (NewValue || Program->ResolveAllBuiltins);
  570. }
  571. // Resolve "..._count".
  572. else if ((SymNameLength > ((SIZE) (sizeof ("_count") - 1))) && (!(strcmp (SymName + SymNameLength - (sizeof ("_count") - 1), "_count"))))
  573. {
  574. if (Program->ResolveAllBuiltins)
  575. {
  576. Program->Frozen = TRUE;
  577. if (GetBuiltinValue (Program, SymName, SymNameLength - (sizeof ("_count") - 1), &NewValue, 0))
  578. HasValue = TRUE;
  579. }
  580. }
  581. else if (SymNameMatches ("entry_point"))
  582. SetToEntryPoint = TRUE;
  583. else if (SymNameMatches ("entry_point_plus_0x8000"))
  584. {
  585. SetToEntryPoint = TRUE;
  586. NewValue = 0x8000;
  587. }
  588. else if (SymNameMatches ("constructors_start"))
  589. {
  590. if (!(NewSymbol = Program->Constructors.Start))
  591. return TRUE;
  592. }
  593. else if (SymNameMatches ("constructors_end"))
  594. {
  595. if (!(NewSymbol = Program->Constructors.End))
  596. return TRUE;
  597. }
  598. else if (SymNameMatches ("constructors_size"))
  599. {
  600. if (Program->Constructors.Start && Program->Constructors.End)
  601. {
  602. NewValue = Program->Constructors.End->Location - Program->Constructors.Start->Location;
  603. HasValue = TRUE;
  604. }
  605. else if (Program->ResolveAllBuiltins)
  606. HasValue = TRUE;
  607. }
  608. else if (SymNameMatches ("destructors_start"))
  609. {
  610. if (!(NewSymbol = Program->Destructors.Start))
  611. return TRUE;
  612. }
  613. else if (SymNameMatches ("destructors_end"))
  614. {
  615. if (!(NewSymbol = Program->Destructors.End))
  616. return TRUE;
  617. }
  618. else if (SymNameMatches ("destructors_size"))
  619. {
  620. if (Program->Destructors.Start && Program->Destructors.End)
  621. {
  622. NewValue = Program->Destructors.End->Location - Program->Destructors.Start->Location;
  623. HasValue = TRUE;
  624. }
  625. else if (Program->ResolveAllBuiltins)
  626. HasValue = TRUE;
  627. }
  628. else if (SymNameMatches ("data_start"))
  629. {
  630. if (Program->DataSection)
  631. NewSymbol = Program->DataSection->SectionSymbol;
  632. else
  633. return TRUE;
  634. }
  635. else if (SymNameMatches ("data_end"))
  636. {
  637. if (Program->DataSection)
  638. {
  639. NewSymbol = Program->DataSection->SectionSymbol;
  640. NewTargetOffset = Program->DataSection->Size;
  641. }
  642. else
  643. return TRUE;
  644. }
  645. else if (SymNameMatches ("data_size"))
  646. {
  647. if (Program->DataSection)
  648. {
  649. NewValue = Program->DataSection->Size;
  650. HasValue = TRUE;
  651. }
  652. else if (Program->ResolveAllBuiltins)
  653. HasValue = TRUE;
  654. }
  655. else if (SymNameMatches ("bss_start"))
  656. {
  657. if (Program->BSSSection)
  658. NewSymbol = Program->BSSSection->SectionSymbol;
  659. else
  660. return TRUE;
  661. }
  662. else if (SymNameMatches ("bss_end"))
  663. {
  664. if (Program->BSSSection)
  665. {
  666. NewSymbol = Program->BSSSection->SectionSymbol;
  667. NewTargetOffset = Program->BSSSection->Size;
  668. }
  669. else
  670. return TRUE;
  671. }
  672. else if (SymNameMatches ("bss_size"))
  673. {
  674. if (Program->BSSSection)
  675. {
  676. NewValue = Program->BSSSection->Size;
  677. HasValue = TRUE;
  678. }
  679. else if (Program->ResolveAllBuiltins)
  680. HasValue = TRUE;
  681. }
  682. else if (SymNameMatches ("file_version"))
  683. {
  684. if (Program->ResolveAllBuiltins || Program->Version)
  685. {
  686. NewValue = Program->Version;
  687. HasValue = TRUE;
  688. }
  689. }
  690. else if (SymNameMatches ("kernel_flags"))
  691. {
  692. if (Program->ResolveAllBuiltins)
  693. {
  694. NewValue = Program->KernelFlags;
  695. HasValue = TRUE;
  696. }
  697. }
  698. else if (SymNameMatches ("kernel_bss_table"))
  699. {
  700. if (Program->BSSSection)
  701. {
  702. strcpy ((char *) (Location->SymbolName), "__kernel_bss_table");
  703. if (TryAgain)
  704. *TryAgain = TRUE;
  705. // This prevents the section from being merged, and prevents
  706. // relocs to it from being emitted.
  707. Program->BSSSection->Handled = TRUE;
  708. return FALSE;
  709. }
  710. else if (Program->ResolveAllBuiltins)
  711. SetToEntryPoint = TRUE;
  712. }
  713. else if (SymNameMatches ("program_size"))
  714. {
  715. if (Program->ResolveAllBuiltins && Program->MainSection)
  716. {
  717. NewValue = Program->MainSection->Size;
  718. HasValue = TRUE;
  719. }
  720. else
  721. return TRUE;
  722. }
  723. else if (SymNameMatches ("kernel_export_table"))
  724. {
  725. BOOLEAN HasExports = FALSE;
  726. for_each (TempSection, Program->Sections)
  727. {
  728. SYMBOL *TempSymbol;
  729. for_each (TempSymbol, TempSection->Symbols)
  730. {
  731. if (TempSymbol->Exported)
  732. {
  733. OFFSET ExportNumber = GetExportNumber (TempSymbol->Name);
  734. if (ExportNumber >= 0)
  735. {
  736. HasExports = TRUE;
  737. TempSection->Referenced = TRUE;
  738. break;
  739. }
  740. }
  741. }
  742. }
  743. if (HasExports)
  744. {
  745. strcpy ((char *) (Location->SymbolName), "__kernel_export_table");
  746. if (TryAgain)
  747. *TryAgain = TRUE;
  748. return FALSE;
  749. }
  750. else if (Program->ResolveAllBuiltins)
  751. SetToEntryPoint = TRUE;
  752. }
  753. #ifdef DATA_VAR_SUPPORT
  754. else if (SymNameMatches ("data_var_name_end"))
  755. {
  756. // Point the reloc to the terminating zero byte of the name.
  757. if (Program->DataVarInfo->Name)
  758. {
  759. strcpy ((char *) (Location->SymbolName), "__data_var_name_start");
  760. Location->Offset += strlen (Program->DataVarInfo->Name) + 1;
  761. if (TryAgain)
  762. *TryAgain = TRUE;
  763. return FALSE;
  764. }
  765. }
  766. #endif /* DATA_VAR_SUPPORT */
  767. else
  768. return FALSE;
  769. if (!SetToEntryPoint)
  770. {
  771. if (HasValue)
  772. {
  773. // Point the location to the new value.
  774. Location->Symbol = NULL;
  775. FreeLocationSymbolName (Section, Location);
  776. Location->Offset += NewValue;
  777. }
  778. else if (NewSymbol)
  779. {
  780. // Point the location to the new symbol.
  781. Location->Symbol = NewSymbol;
  782. FreeLocationSymbolName (Section, Location);
  783. Location->Offset += NewTargetOffset;
  784. NewSymbol->Parent->Referenced = TRUE;
  785. }
  786. else
  787. Location->Builtin = TRUE;
  788. return TRUE;
  789. }
  790. }
  791. // If this is a reloc to a kernel-specific symbol, point it to the
  792. // entry point. The result of this is that the reloc's value
  793. // becomes 0 if the reloc was made up by something like
  794. // _exit-__kernel_entry_point.
  795. else if (SymNameMatches ("exit") || SymNameMatches ("comment") || SymNameMatches ("extraram") || SymNameMatches ("library"))
  796. SetToEntryPoint = TRUE;
  797. if (SetToEntryPoint)
  798. {
  799. if (Program->EntryPoint.Symbol)
  800. {
  801. Location->Symbol = Program->EntryPoint.Symbol;
  802. FreeLocationSymbolName (Section, Location);
  803. Location->Offset += Program->EntryPoint.Offset + NewValue;
  804. }
  805. return TRUE;
  806. }
  807. }
  808. return FALSE;
  809. }
  810. // Count the items for a specific built-in symbol, specified by SymName
  811. // and SymNameLength. If TrueValue is nonzero, items are not counted,
  812. // but NewValue is set to this value if at least one item was found. In
  813. // that case, if Program->ResolveAllBuiltins is false, NewValue may be
  814. // unchanged even though there are some items; you need to check back
  815. // later when Program->ResolveAllBuiltins is true.
  816. BOOLEAN GetBuiltinValue (PROGRAM *Program, const char *SymName, SIZE SymNameLength, OFFSET *NewValue, OFFSET TrueValue)
  817. {
  818. #define Count(n,op,code) \
  819. ({ \
  820. register OFFSET n__ = (n); \
  821. if (TrueValue) \
  822. { \
  823. if (n__) \
  824. { \
  825. *NewValue = TrueValue; \
  826. code; \
  827. } \
  828. } \
  829. else \
  830. *NewValue op n__; \
  831. })
  832. #define SetCounter(n) (Count ((n), =, (void) 0))
  833. #define IncreaseCounter(n) (Count ((n), +=, break))
  834. BOOLEAN SymNameMatches (const char *Name)
  835. {
  836. return (!(strncmp (SymName, Name, SymNameLength)));
  837. }
  838. SECTION *TempSection;
  839. RELOC *TempReloc;
  840. SYMBOL *TempSymbol;
  841. if (SymNameMatches ("constructor"))
  842. {
  843. if (Program->Constructors.Start && Program->Constructors.End)
  844. SetCounter ((Program->Constructors.End->Location - Program->Constructors.Start->Location) >> 2);
  845. }
  846. else if (SymNameMatches ("destructor"))
  847. {
  848. if (Program->Destructors.Start && Program->Destructors.End)
  849. SetCounter ((Program->Destructors.End->Location - Program->Destructors.Start->Location) >> 2);
  850. }
  851. else if (SymNameMatches ("reloc"))
  852. {
  853. // Count all absolute relocs.
  854. // Relative relocs will either be resolved completely or
  855. // produce errors.
  856. for_each (TempSection, Program->Sections)
  857. {
  858. IncreaseCounter (TempSection->Relocs.EmittedCount);
  859. // Since relocs may be removed, if ResolveAllBuiltins
  860. // is false, do not handle TrueValue.
  861. if ((!TrueValue) || Program->ResolveAllBuiltins)
  862. {
  863. for_each (TempReloc, TempSection->Relocs)
  864. if (!(TempReloc->Relative || TempReloc->Target.Builtin || (TempReloc->Target.Symbol && (TempReloc->Target.Symbol->Parent->Handled))))
  865. IncreaseCounter (1);
  866. }
  867. }
  868. }
  869. else if (SymNameMatches ("data_ref"))
  870. {
  871. if (Program->DataSection)
  872. {
  873. // Count all absolute relocs to the data section.
  874. for_each (TempSection, Program->Sections)
  875. for_each (TempReloc, TempSection->Relocs)
  876. if (TempReloc->Target.Symbol && (TempReloc->Target.Symbol->Parent == Program->DataSection) && (!(TempReloc->Relative || TempReloc->Target.Builtin)))
  877. IncreaseCounter (1);
  878. }
  879. }
  880. else if (SymNameMatches ("bss_ref"))
  881. {
  882. if (Program->BSSSection)
  883. {
  884. // Count all absolute relocs to the BSS section.
  885. for_each (TempSection, Program->Sections)
  886. for_each (TempReloc, TempSection->Relocs)
  887. if (TempReloc->Target.Symbol && (TempReloc->Target.Symbol->Parent == Program->BSSSection) && (!(TempReloc->Relative || TempReloc->Target.Builtin)))
  888. IncreaseCounter (1);
  889. }
  890. }
  891. else if (SymNameMatches ("rom_call"))
  892. {
  893. for_each (TempSection, Program->Sections)
  894. IncreaseCounter (CountItems (TempSection->ROMCalls, ROM_CALL));
  895. }
  896. else if (SymNameMatches ("ram_call"))
  897. {
  898. for_each (TempSection, Program->Sections)
  899. IncreaseCounter (CountItems (TempSection->RAMCalls, RAM_CALL));
  900. }
  901. else if (SymNameMatches ("lib"))
  902. SetCounter (CountItems (Program->Libraries, LIBRARY));
  903. else if (SymNameMatches ("referenced_lib"))
  904. SetCounter (Program->Libraries.ReferencedCount);
  905. else if (SymNameMatches ("export"))
  906. {
  907. // The number of exports is equal to the highest export number + 1.
  908. for_each (TempSection, Program->Sections)
  909. {
  910. for_each (TempSymbol, TempSection->Symbols)
  911. {
  912. if (TempSymbol->Exported)
  913. {
  914. OFFSET ExportNumber = GetExportNumber (TempSymbol->Name);
  915. if ((ExportNumber >= 0) && (*NewValue < ExportNumber + 1))
  916. Count (ExportNumber + 1, =, break);
  917. }
  918. }
  919. }
  920. }
  921. else if (SymNameMatches ("nostub_comment"))
  922. {
  923. for_each (TempSection, Program->Sections)
  924. {
  925. for_each (TempSymbol, TempSection->Symbols)
  926. {
  927. if (TempSymbol->Exported && (!(strncmp (TempSymbol->Name, SYMPF_NOSTUB_DATA, sizeof (SYMPF_NOSTUB_DATA) - 1))))
  928. {
  929. OFFSET ExportNumber = GetExportNumber (TempSymbol->Name + (sizeof (SYMPF_NOSTUB_DATA_START) - 1));
  930. if (ExportNumber >= 0)
  931. IncreaseCounter (1);
  932. }
  933. }
  934. }
  935. }
  936. else
  937. return FALSE;
  938. #undef IncreaseCounter
  939. #undef SetCounter
  940. #undef Count
  941. return TRUE;
  942. }
  943. // If the given symbol name belongs to a calculator-specific builtin
  944. // symbol, return a pointer to the part of it that holds the values.
  945. const char *GetCalcBuiltinValues (const char *SymName)
  946. {
  947. if (!(strncmp (SymName, SYMPF_BUILTIN_CALC_CONST, sizeof (SYMPF_BUILTIN_CALC_CONST) - 1)))
  948. {
  949. return (SymName + (sizeof (SYMPF_BUILTIN_CALC_CONST) - 1));
  950. }
  951. return NULL;
  952. }
  953. // Check if the given location points to a calculator-specific builtin
  954. // symbol.
  955. BOOLEAN IsCalcBuiltinLocation (const LOCATION *Location)
  956. {
  957. return (Location->SymbolName && (GetCalcBuiltinValues (Location->SymbolName) || (!(strcmp (Location->SymbolName, SYM_BUILTIN_HARDWARE_ID)))));
  958. }
  959. // Return whether the reloc can be resolved to a calculator-specific value.
  960. // ResolveSpecialSymbol or something related must have been called on the
  961. // reloc at least once.
  962. BOOLEAN IsPlainCalcBuiltin (const RELOC *Reloc)
  963. {
  964. // To improve speed, check whether the target and relation (if it exists)
  965. // are builtin symbols.
  966. if (Reloc->Target.Builtin && ((!(Reloc->Relation)) || Reloc->Relation->Builtin))
  967. {
  968. // Check the target.
  969. if (IsCalcBuiltinLocation (&(Reloc->Target)))
  970. {
  971. // If there is a relation, check it.
  972. if (Reloc->Relation)
  973. return IsCalcBuiltinLocation (Reloc->Relation);
  974. // Otherwise, the reloc may not be relative, since that would
  975. // mean that it cannot be resolved to a number.
  976. else
  977. return (!(Reloc->Relative));
  978. }
  979. }
  980. return FALSE;
  981. }
  982. // If the location can be resolved to a calculator-specific value, get the
  983. // value for the specified calculator.
  984. // If IsCalcBuiltinLocation returned a positive result for this reloc, this
  985. // function will not return a negative result.
  986. BOOLEAN GetCalcBuiltinLocationValue (const LOCATION *Location, ProgramCalcs DestCalc, IMAX *Value)
  987. {
  988. // Basic sanity checks.
  989. if (Value && Location->SymbolName)
  990. {
  991. // Special case: __ld_hardware_id
  992. if (!(strcmp (Location->SymbolName, SYM_BUILTIN_HARDWARE_ID)))
  993. {
  994. switch (DestCalc)
  995. {
  996. case CALC_TI89:
  997. *Value = 3;
  998. break;
  999. case CALC_TI89 | CALC_FLAG_TITANIUM:
  1000. *Value = 9;
  1001. break;
  1002. case CALC_TI92PLUS:
  1003. *Value = 1;
  1004. break;
  1005. case CALC_V200:
  1006. *Value = 8;
  1007. break;
  1008. default:
  1009. Warning (NULL, SYM_BUILTIN_HARDWARE_ID " not defined for this calculator.");
  1010. *Value = 0;
  1011. }
  1012. *Value += Location->Offset;
  1013. return TRUE;
  1014. }
  1015. else
  1016. {
  1017. // Get the part of the symbol name that holds the values,
  1018. // separated by '_'.
  1019. const char *Values = GetCalcBuiltinValues (Location->SymbolName);
  1020. if (Values)
  1021. {
  1022. // AND out the calculator flags.
  1023. DestCalc &= ~CALC_FLAG_TITANIUM;
  1024. // While we still have at least one value left...
  1025. while (Values)
  1026. {
  1027. // Get the end of the value string.
  1028. const char *ValueEnd = strchr (Values, '_');
  1029. SIZE ValueSize = ValueEnd ? (SIZE) (ValueEnd - Values) : (SIZE) (strlen (Values));
  1030. // If this is the value that belongs to the current calculator,
  1031. // extract the value and return.
  1032. if (DestCalc == 1)
  1033. {
  1034. char *EndPtr = NULL;
  1035. // Create a copy of the value, with a terminating zero byte.
  1036. char ValueStr[ValueSize+1];
  1037. strncpy (ValueStr, Values, ValueSize);
  1038. ValueStr [ValueSize] = 0;
  1039. // Convert this string into a number and return it.
  1040. *Value = strtoul (ValueStr, &EndPtr, 0);
  1041. if (EndPtr && *EndPtr)
  1042. Warning (NULL, "Invalid number `%s' in `%s'.", ValueStr, Location->SymbolName);
  1043. *Value += Location->Offset;
  1044. return TRUE;
  1045. }
  1046. // Advance to the next value.
  1047. Values = ValueEnd ? ValueEnd + 1 : NULL;
  1048. // Advance to the next calculator.
  1049. DestCalc >>= 1;
  1050. }
  1051. // No more values were found, but the function did not exit yet.
  1052. Warning (NULL, "Calculator constant `%s' contains too few values.", Location->SymbolName);
  1053. *Value = 0;
  1054. // We have to return a positive result anyway because
  1055. // IsCalcBuiltinLocation would as well.
  1056. return TRUE;
  1057. }
  1058. }
  1059. }
  1060. return FALSE;
  1061. }
  1062. // If the reloc can be resolved to a calculator-specific value, get the
  1063. // value for the specified calculator.
  1064. // The return value is the same as for IsPlainCalcBuiltin.
  1065. BOOLEAN GetCalcBuiltinValue (const RELOC *Reloc, ProgramCalcs DestCalc, IMAX *Value)
  1066. {
  1067. if (Value)
  1068. {
  1069. // Get the value for the target.
  1070. if (GetCalcBuiltinLocationValue (&(Reloc->Target), DestCalc, Value))
  1071. {
  1072. // If there is a relation, subtract its value.
  1073. if (Reloc->Relation)
  1074. {
  1075. IMAX RelationValue;
  1076. if (!(GetCalcBuiltinLocationValue (Reloc->Relation, DestCalc, &RelationValue)))
  1077. return FALSE;
  1078. *Value -= RelationValue;
  1079. }
  1080. // Otherwise, the reloc may not be relative
  1081. // (see IsPlainCalcBuiltin).
  1082. else if (Reloc->Relative)
  1083. return FALSE;
  1084. *Value += Reloc->FixedOffset;
  1085. return TRUE;
  1086. }
  1087. }
  1088. return FALSE;
  1089. }
  1090. // If required by some special symbol(s) at the end of the section,
  1091. // modify the contents of the section.
  1092. // This can be used to insert special items such as relocation entries.
  1093. // MergedSection specifies the (usually large) part of the program that
  1094. // has already been merged.
  1095. // If necessary, MergedSection is frozen automatically.
  1096. BOOLEAN HandleSectionContents (SECTION *Section, SECTION *MergedSection)
  1097. {
  1098. SYMBOL *Symbol;
  1099. // Search the labels at the end of the secion to find special ones.
  1100. for (Symbol = FindSymbolAtPos (Section, Section->Size, TRUE); Symbol; Symbol = GetNext (Symbol))
  1101. {
  1102. if (!(strncmp (Symbol->Name, SYMPF_BUILTIN_INSERT, sizeof (SYMPF_BUILTIN_INSERT) - 1)))
  1103. return (HandleInsertion (Section, Symbol->Location, Symbol->Name + sizeof (SYMPF_BUILTIN_INSERT) - 1, MergedSection, FALSE));
  1104. }
  1105. return TRUE;
  1106. }
  1107. // Insert contents for an insertion specified by SymbolName, and return
  1108. // a symbol from where the insertion took place.
  1109. // If necessary, Section is frozen automatically.
  1110. SYMBOL *HandleAutoInsertion (SECTION *Section, const char *SymbolName)
  1111. {
  1112. if (!(strncmp (SymbolName, SYMPF_BUILTIN_INSERT, sizeof (SYMPF_BUILTIN_INSERT) - 1)))
  1113. {
  1114. CreateSectionSegment (Section);
  1115. // All insertions except compressed ones should be aligned on a 2-byte boundary.
  1116. if ((!(strncmp (SymbolName, SYMPF_BUILTIN_INSERT_COMPRESSED, sizeof (SYMPF_BUILTIN_INSERT_COMPRESSED) - 1)))
  1117. || (!(strcmp (SymbolName, SYMPF_BUILTIN_INSERT "fargo021_relocs")))
  1118. || (!(strcmp (SymbolName, SYMPF_BUILTIN_INSERT "preos_compressed_tables")))
  1119. || PadSection (Section, 2))
  1120. {
  1121. // Create a new symbol at the end of the section.
  1122. SYMBOL *Result = calloc (1, sizeof (SYMBOL));
  1123. if (Result)
  1124. {
  1125. Result->Parent = Section;
  1126. Result->Location = Section->Size;
  1127. strncpy (Result->Name, SymbolName, MAX_SYM_LEN);
  1128. Result->Exported = TRUE;
  1129. Append (Section->Symbols, Result);
  1130. // Insert the data.
  1131. if (AppendInsertionData (Section, SymbolName + sizeof (SYMPF_BUILTIN_INSERT) - 1, Section, TRUE))
  1132. return Result;
  1133. }
  1134. else
  1135. Error (NULL, "Out of memory.");
  1136. }
  1137. }
  1138. return NULL;
  1139. }
  1140. // Handle an insertion by cutting the section off at the specified location
  1141. // and inserting the contents specified by the name, taking into account
  1142. // that MergedSection specifies the (usually large) part of the program
  1143. // that has already been merged.
  1144. BOOLEAN HandleInsertion (SECTION *Section, OFFSET Location, const char *Name, SECTION *MergedSection, BOOLEAN AlwaysTerminate)
  1145. {
  1146. if (Location == Section->Size)
  1147. return AppendInsertionData (Section, Name, MergedSection, AlwaysTerminate);
  1148. else
  1149. return TRUE;
  1150. }
  1151. // Append the data required by an insertion (specified by name) to the
  1152. // section specified by Section, taking into account that MergedSection
  1153. // specifies the (usually large) part of the program that has already
  1154. // been merged.
  1155. BOOLEAN AppendInsertionData (SECTION *Section, const char *Name, SECTION *MergedSection, BOOLEAN AlwaysTerminate)
  1156. {
  1157. BOOLEAN NameMatches (const char *InsertionName)
  1158. {
  1159. return (!(strcmp (Name, InsertionName)));
  1160. }
  1161. PROGRAM *Program = Section->Parent;
  1162. #ifdef DATA_VAR_SUPPORT
  1163. // Data variable name.
  1164. if (NameMatches ("data_var_name"))
  1165. return InsertDataVarName (Section);
  1166. else
  1167. #endif /* DATA_VAR_SUPPORT */
  1168. // Nostub-specific formats.
  1169. if (NameMatches ("nostub_comments"))
  1170. return InsertNostubComments (Section);
  1171. // Kernel-specific formats.
  1172. else if (NameMatches ("kernel_relocs"))
  1173. return InsertKernelRelocs (Section, NULL);
  1174. else if (NameMatches ("kernel_bss_refs"))
  1175. return InsertKernelSectionRefs (Section, Program->BSSSection, AlwaysTerminate);
  1176. else if (NameMatches ("kernel_data_refs"))
  1177. return InsertKernelSectionRefs (Section, Program->DataSection, AlwaysTerminate);
  1178. else if (NameMatches ("kernel_rom_calls"))
  1179. return InsertKernelROMCalls (Section);
  1180. else if (NameMatches ("kernel_ram_calls"))
  1181. return InsertKernelRAMCalls (Section);
  1182. else if (NameMatches ("kernel_libs"))
  1183. return InsertKernelLibraries (Section);
  1184. else if (NameMatches ("kernel_exports"))
  1185. return InsertKernelExports (Section, TRUE);
  1186. #ifdef FARGO_SUPPORT
  1187. // Fargo-specific formats.
  1188. else if (NameMatches ("fargo_exports"))
  1189. return InsertKernelExports (Section, FALSE);
  1190. else if (NameMatches ("fargo020_bss_refs"))
  1191. return InsertKernelSectionRefs (Section, Program->BSSSection, TRUE);
  1192. else if (NameMatches ("fargo020_libs"))
  1193. return InsertFargo020Libraries (Section);
  1194. #endif /* FARGO_SUPPORT */
  1195. // PreOS-specific formats.
  1196. else if (NameMatches ("preos_compressed_tables"))
  1197. return InsertPreOsCompressedTables (Section);
  1198. // Other compressed formats.
  1199. else
  1200. {
  1201. char *ReferenceName = malloc ((sizeof (SYMPF_BUILTIN) - 1) + strlen (Name) + sizeof ("_ref"));
  1202. if (ReferenceName)
  1203. {
  1204. // Build the reference symbol name: "__ld_" Name "_ref".
  1205. strcpy (ReferenceName, SYMPF_BUILTIN);
  1206. strcat (ReferenceName, Name);
  1207. strcat (ReferenceName, "_ref");
  1208. {
  1209. LOCATION Reference = {NULL, ReferenceName, 0, FALSE};
  1210. // Try to find a reference symbol. If none is found, use
  1211. // the program entry point.
  1212. Section->Relocs.UnresolvedCount++;
  1213. if (!(ResolveLocation (Program, Section, &Reference)))
  1214. {
  1215. FreeLocationSymbolName (Section, &Reference);
  1216. Reference = Program->EntryPoint;
  1217. }
  1218. #ifdef FARGO_SUPPORT
  1219. // Fargo-specific formats.
  1220. if (NameMatches ("fargo021_relocs"))
  1221. return InsertCompressedRelocs (Section, NULL, MergedSection, &Reference);
  1222. else if (NameMatches ("fargo021_bss_refs"))
  1223. return InsertFargo021SectionRefs (Section, Program->BSSSection, MergedSection, &Reference);
  1224. else if (NameMatches ("fargo021_libs"))
  1225. return InsertFargo021Libraries (Section, MergedSection, &Reference);
  1226. #endif /* FARGO_SUPPORT */
  1227. // Compressed relocation tables using our own format.
  1228. else if (NameMatches ("compressed_relocs"))
  1229. return InsertCompressedRelocs (Section, NULL, MergedSection, &Reference);
  1230. else if (NameMatches ("compressed_bss_refs"))
  1231. return InsertCompressedSectionRefs (Section, Program->BSSSection, MergedSection, &Reference);
  1232. else if (NameMatches ("compressed_data_refs"))
  1233. return InsertCompressedSectionRefs (Section, Program->DataSection, MergedSection, &Reference);
  1234. else if (NameMatches ("compressed_rom_calls"))
  1235. return InsertCompressedROMCalls (Section, MergedSection, &Reference);
  1236. else
  1237. Warning (GetFileName (Section, Section->Size), "Unrecognized insertion `%s'.", Name);
  1238. }
  1239. }
  1240. else
  1241. {
  1242. Error (NULL, "Out of memory.");
  1243. return FALSE;
  1244. }
  1245. }
  1246. return TRUE;
  1247. }