special.c 43 KB

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