special.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. /* special.c: Routines to handle special characteristics of the linker
  2. Copyright (C) 2002-2004 Sebastian Reichelt
  3. Copyright (C) 2003-2005 Kevin Kofler
  4. Copyright (C) 2004 Billy Charvet
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  16. #include "special.h"
  17. #include "integers.h"
  18. #include "manip.h"
  19. #include "insert/insert.h"
  20. #include <string.h>
  21. #include <stdlib.h>
  22. // Try to translate an external symbol into a special-feature symbol used by the linker.
  23. // If the symbol contains a number, it is written to the variable pointed to by Number.
  24. SpecialExternalSymbolTypes TranslateSpecialExternalSymbol (PROGRAM *Program, char *SymbolName, void **Reference, OFFSET *Number)
  25. {
  26. SpecialExternalSymbolTypes SymType = ST_NORMAL;
  27. char *SymNum = SymbolName, *Divider = NULL;
  28. int NumBase = 16;
  29. if (!(strncmp (SymbolName, "tiamsapi_", sizeof ("tiamsapi_") - 1)))
  30. {
  31. SymType = ST_ROM_CALL;
  32. SymNum += sizeof ("tiamsapi_") - 1;
  33. NumBase = 10;
  34. }
  35. if (!(strncmp (SymbolName, "_ROM_CALL_", sizeof ("_ROM_CALL_") - 1)))
  36. {
  37. SymType = ST_ROM_CALL;
  38. SymNum += sizeof ("_ROM_CALL_") - 1;
  39. }
  40. else if (!(strncmp (SymbolName, "_RAM_CALL_", sizeof ("_RAM_CALL_") - 1)))
  41. {
  42. SymType = ST_RAM_CALL;
  43. SymNum += sizeof ("_RAM_CALL_") - 1;
  44. }
  45. else if (!(strncmp (SymbolName, "_extraramaddr@", sizeof ("_extraramaddr@") - 1)))
  46. {
  47. SymType = ST_EXTRA_RAM;
  48. SymNum += sizeof ("_extraramaddr@") - 1;
  49. }
  50. else if (!(strncmp (SymbolName, "_extraramaddr__", sizeof ("_extraramaddr__") - 1)))
  51. {
  52. SymType = ST_EXTRA_RAM;
  53. SymNum += sizeof ("_extraramaddr__") - 1;
  54. }
  55. else if (((Divider = strchr (SymbolName, '@'))) || (((Divider = strstr (SymbolName, "__"))) && (SymbolName [0] != '_') && (SymbolName [0] != '.') && (strncmp (SymbolName, "L_", sizeof ("L_") - 1))))
  56. {
  57. if (*Divider == '@')
  58. SymNum = Divider + 1;
  59. else
  60. SymNum = Divider + 2;
  61. if (strlen (SymNum) == 4)
  62. SymType = ST_LIB_CALL;
  63. }
  64. if (SymType != ST_NORMAL)
  65. {
  66. // Get the number the symbol contains (in hex).
  67. if (Number)
  68. {
  69. char *Err;
  70. *Number = strtoul (SymNum, &Err, NumBase);
  71. // If the number has an error in it, revert to normal symbol.
  72. if (*Err)
  73. SymType = ST_NORMAL;
  74. }
  75. }
  76. if (SymType == ST_LIB_CALL)
  77. {
  78. // Cut SymbolName at the divider.
  79. *Divider = 0;
  80. // Get a reference to the library identified by SymbolName.
  81. if (Reference)
  82. {
  83. *Reference = GetLibrary (Program, SymbolName);
  84. if (!(*Reference))
  85. SymType = ST_NORMAL;
  86. }
  87. }
  88. return SymType;
  89. }
  90. // Handle the symbol if it is a special one.
  91. // Returns TRUE if it was special; in this case it should not be used.
  92. BOOLEAN HandleSpecialSymbol (PROGRAM *Program, const char *SymbolName)
  93. {
  94. BOOLEAN SymbolNameMatches (const char *Name)
  95. {
  96. return (!(strcmp (SymbolName, Name)));
  97. }
  98. const char *Divider;
  99. // All special symbols start with an underscore.
  100. if (SymbolName [0] == '_')
  101. {
  102. SymbolName++;
  103. // The most important special symbol: __ref_all_xxx.
  104. // It adds xxx to the global imports.
  105. if (!(strncmp (SymbolName, "_ref_all_", sizeof ("_ref_all_") - 1)))
  106. {
  107. if (!(Program->IgnoreGlobalImports))
  108. AddGlobalImport (Program, SymbolName + sizeof ("_ref_all_") - 1);
  109. }
  110. else if (SymbolNameMatches ("tigcc_native"))
  111. {
  112. // Kernel is the default type. Everything else must be defined explicitly
  113. // and should therefore take precedence over _tigcc_native.
  114. if (Program->Type == PT_KERNEL)
  115. Program->Type = PT_NATIVE;
  116. }
  117. else if (SymbolNameMatches ("nostub"))
  118. {
  119. SECTION *FirstSection = GetFirst (Program->Sections);
  120. if (FirstSection && (!(FirstSection->StartupNumber)))
  121. FirstSection->Essential = TRUE;
  122. Program->Type = PT_NOSTUB;
  123. }
  124. #ifdef NOSTUB_DLL_SUPPORT
  125. else if (SymbolNameMatches ("nostub_dll"))
  126. {
  127. SECTION *FirstSection = GetFirst (Program->Sections);
  128. if (FirstSection && (!(FirstSection->StartupNumber)))
  129. FirstSection->Essential = TRUE;
  130. Program->Type = PT_NOSTUB_DLL;
  131. Program->Library = TRUE;
  132. }
  133. #endif /* NOSTUB_DLL_SUPPORT */
  134. #ifdef FARGO_SUPPORT
  135. else if (SymbolNameMatches ("fargo"))
  136. Program->Type = PT_FARGO;
  137. #endif /* FARGO_SUPPORT */
  138. #ifdef FLASH_OS_SUPPORT
  139. else if (SymbolNameMatches ("flash_os"))
  140. Program->Type = PT_FLASH_OS;
  141. #endif /* FLASH_OS_SUPPORT */
  142. else if (SymbolNameMatches ("library"))
  143. {
  144. Program->Library = TRUE;
  145. #ifdef FARGO_SUPPORT
  146. // Under Fargo, _library is a normal symbol as well.
  147. if (Program->Type == PT_FARGO)
  148. return FALSE;
  149. #endif /* FARGO_SUPPORT */
  150. }
  151. else if (SymbolNameMatches ("ti92"))
  152. {
  153. Program->Calcs |= CALC_TI92;
  154. }
  155. else if (SymbolNameMatches ("ti89"))
  156. {
  157. Program->Calcs |= CALC_TI89;
  158. Program->KernelFlags |= 0x02;
  159. }
  160. else if (SymbolNameMatches ("ti89ti"))
  161. {
  162. Program->Calcs |= CALC_TI89 | CALC_FLAG_TITANIUM;
  163. Program->KernelFlags |= 0x42;
  164. }
  165. else if (SymbolNameMatches ("ti92plus"))
  166. {
  167. Program->Calcs |= CALC_TI92PLUS;
  168. Program->KernelFlags |= 0x01;
  169. }
  170. else if (SymbolNameMatches ("v200"))
  171. {
  172. Program->Calcs |= CALC_V200;
  173. Program->KernelFlags |= 0x20;
  174. }
  175. else if (!(strncmp (SymbolName, "flag_", sizeof ("flag_") - 1)))
  176. Program->KernelFlags |= (1 << strtoul (SymbolName + sizeof ("flag_") - 1, NULL, 10));
  177. else if (!(strncmp (SymbolName, "version", sizeof ("version") - 1)))
  178. Program->Version = strtoul (SymbolName + sizeof ("version") - 1, NULL, 16);
  179. else if (!(strcmp (SymbolName, "_ld_use_fline_jumps")))
  180. {
  181. if (Program->OptimizeInfo)
  182. Program->OptimizeInfo->UseFLineJumps = TRUE;
  183. }
  184. else if (!(strcmp (SymbolName, "_ld_use_4byte_fline_jumps")))
  185. {
  186. if (Program->OptimizeInfo)
  187. Program->OptimizeInfo->Use4ByteFLineJumps = TRUE;
  188. }
  189. else if (!(strcmp (SymbolName, SYM_IGNORE_GLOBAL_IMPORTS + 1)))
  190. Program->IgnoreGlobalImports = TRUE;
  191. else if (SymbolNameMatches (SYM_OMIT_BSS_INIT + 1) || SymbolNameMatches (SYM_ALL_RELOCS + 1))
  192. // These are mostly file-local special symbols; they are handled
  193. // by the importing function.
  194. ;
  195. else
  196. // Not a special symbol.
  197. return FALSE;
  198. // The symbol was handled here.
  199. return TRUE;
  200. }
  201. else if (((Divider = strstr (SymbolName, "@version"))) || (((Divider = strstr (SymbolName, "__version"))) && (SymbolName [0] != '_') && (SymbolName [0] != '.') && (strncmp (SymbolName, "L_", sizeof ("L_") - 1))))
  202. {
  203. // Required minimum version number for a library.
  204. const char *Version;
  205. VERSION VersionNumber;
  206. if (*Divider == '@')
  207. Version = Divider + sizeof ("@version") - 1;
  208. else
  209. Version = Divider + sizeof ("__version") - 1;
  210. VersionNumber = strtoul (Version, NULL, 16);
  211. if (VersionNumber)
  212. {
  213. if (Divider - SymbolName <= MAX_SYM_LEN)
  214. {
  215. char LibName[MAX_SYM_LEN+1];
  216. LIBRARY *Library;
  217. strncpy (LibName, SymbolName, Divider - SymbolName);
  218. LibName [Divider - SymbolName] = 0;
  219. Library = GetLibrary (Program, LibName);
  220. if (Library && (Library->Version < VersionNumber))
  221. Library->Version = VersionNumber;
  222. }
  223. return TRUE;
  224. }
  225. }
  226. return FALSE;
  227. }
  228. // Translate a section name into a startup section number.
  229. // Returns 0 if the name does not represent a startup section.
  230. OFFSET GetStartupSectionNumber (const char *SectionName, SIZE MaxLen)
  231. {
  232. // Check if it looks like a startup section.
  233. if ((MaxLen > (SIZE) (sizeof ("_st") - 1)) && (!(strncmp (SectionName, "_st", sizeof ("_st") - 1))))
  234. {
  235. // Copy the section name to a temporary null-terminated location.
  236. char SecName[MaxLen+1];
  237. memset (SecName, 0, MaxLen + 1);
  238. strncpy (SecName, SectionName, MaxLen);
  239. // Check whether it is a library startup section.
  240. if (SecName [sizeof ("_st") - 1] == 'l')
  241. {
  242. // Library startup sections are special:
  243. // They may always be included, even if the file is not executable.
  244. // Therefore they get a negative number to distinguish them.
  245. OFFSET RealNum = strtoul (SecName + sizeof ("_stl") - 1, NULL, 10);
  246. return (RealNum ? RealNum - 10000 : 0);
  247. }
  248. else
  249. return (strtoul (SecName + sizeof ("_st") - 1, NULL, 10));
  250. }
  251. else
  252. return 0;
  253. }
  254. // Translate a symbol name into the number of an exported function, if the
  255. // function is meant to be exported.
  256. // Returns -1 if it is not an exported function.
  257. OFFSET GetExportNumber (const char *SymbolName)
  258. {
  259. const char *Divider;
  260. if (((Divider = strchr (SymbolName, '@'))) || (((Divider = strstr (SymbolName, "__"))) && (SymbolName [0] != '_') && (SymbolName [0] != '.') && (strncmp (SymbolName, "L_", sizeof ("L_") - 1))))
  261. {
  262. OFFSET Number;
  263. char *Err;
  264. if (*Divider == '@')
  265. Divider++;
  266. else
  267. Divider += 2;
  268. Number = strtoul (Divider, &Err, 16);
  269. if (*Err)
  270. return (-1);
  271. return Number;
  272. }
  273. else
  274. return (-1);
  275. }
  276. // Add all imports with names defined by this program, if they are needed.
  277. BOOLEAN CreateSpecialGlobalImports (PROGRAM *Program)
  278. {
  279. BOOLEAN Result = TRUE;
  280. SECTION *TempSection;
  281. if (Program->Type == PT_KERNEL)
  282. {
  283. if (Program->Library)
  284. Result = Result && AddGlobalImport (Program, "__kernel_library_header");
  285. else
  286. Result = Result && AddGlobalImport (Program, "__kernel_program_header");
  287. }
  288. #ifdef FARGO_SUPPORT
  289. else if (Program->Type == PT_FARGO)
  290. {
  291. if (Program->Library)
  292. Result = Result && AddGlobalImport (Program, "__fargo_library_header");
  293. else
  294. Result = Result && AddGlobalImport (Program, "__fargo_program_header");
  295. }
  296. #endif /* FARGO_SUPPORT */
  297. #ifdef FLASH_OS_SUPPORT
  298. else if (Program->Type == PT_FLASH_OS)
  299. Result = Result && AddGlobalImport (Program, "__flash_os_header");
  300. #endif /* FLASH_OS_SUPPORT */
  301. if (Program->Constructors.Start)
  302. Result = Result && AddGlobalImport (Program, "__handle_constructors");
  303. if (Program->Destructors.Start)
  304. Result = Result && AddGlobalImport (Program, "__handle_destructors");
  305. if (Program->BSSSection && Program->BSSSection->Initialized)
  306. Result = Result && AddGlobalImport (Program, "__initialize_bss");
  307. // Handle BSS section if any.
  308. if (Program->BSSSection && (!(Program->BSSSection->Handled)))
  309. {
  310. GLOBAL_IMPORT *Import = CreateGlobalImport (Program, "__handle_bss");
  311. if (Import)
  312. {
  313. // This is the import that will mark the BSS section as handled
  314. // if it succeeds.
  315. Program->BSSImport = Import;
  316. // Try to resolve it against available archives.
  317. ResolveGlobalImport (Program, Import);
  318. // The BSS section is simply merged into the rest if it is not
  319. // handled.
  320. Import->Succeeded = TRUE;
  321. }
  322. else
  323. Result = FALSE;
  324. }
  325. // Handle absolute relocs if there are any.
  326. for_each (TempSection, Program->Sections)
  327. {
  328. BOOLEAN Done = FALSE;
  329. RELOC *TempReloc;
  330. for_each (TempReloc, TempSection->Relocs)
  331. {
  332. if ((!(TempReloc->Relative || TempReloc->Target.Builtin || (TempReloc->Target.Symbol && (TempReloc->Target.Symbol->Parent->Handled)))))
  333. {
  334. GLOBAL_IMPORT *Import = AddGlobalImport (Program, "__handle_relocs");
  335. if (Import)
  336. // Relocs are either handled by the export format, or a
  337. // warning is emitted.
  338. Import->Succeeded = TRUE;
  339. else
  340. Result = FALSE;
  341. Done = TRUE;
  342. break;
  343. }
  344. }
  345. if (Done)
  346. break;
  347. }
  348. // Handle ROM calls if there are any.
  349. for_each (TempSection, Program->Sections)
  350. {
  351. if (!(IsEmpty (TempSection->ROMCalls)))
  352. {
  353. GLOBAL_IMPORT *Import = AddGlobalImport (Program, "__handle_rom_calls");
  354. if (Import)
  355. // ROM calls are either handled by the export format, or a
  356. // warning is emitted.
  357. Import->Succeeded = TRUE;
  358. else
  359. Result = FALSE;
  360. break;
  361. }
  362. }
  363. // Handle RAM calls if there are any.
  364. for_each (TempSection, Program->Sections)
  365. {
  366. if (!(IsEmpty (TempSection->RAMCalls)))
  367. {
  368. GLOBAL_IMPORT *Import = AddGlobalImport (Program, "__handle_ram_calls");
  369. if (Import)
  370. // RAM calls are either handled by the export format, or a
  371. // warning is emitted.
  372. Import->Succeeded = TRUE;
  373. else
  374. Result = FALSE;
  375. break;
  376. }
  377. }
  378. // Handle libraries if any libraries are referenced.
  379. if (!(IsEmpty (Program->Libraries)))
  380. {
  381. GLOBAL_IMPORT *Import = AddGlobalImport (Program, "__handle_libraries");
  382. if (Import)
  383. // Libraries are either handled by the export format, or a
  384. // warning is emitted.
  385. Import->Succeeded = TRUE;
  386. else
  387. Result = FALSE;
  388. }
  389. // Handle _nostub comments if any.
  390. if ((Program->Type == PT_NATIVE) || (Program->Type == PT_NOSTUB))
  391. {
  392. BOOLEAN NostubComments = FALSE;
  393. for_each (TempSection, Program->Sections)
  394. {
  395. SYMBOL *TempSymbol;
  396. for_each (TempSymbol, TempSection->Symbols)
  397. {
  398. if (TempSymbol->Exported && (!(strncmp (TempSymbol->Name, "_nostub_data__", sizeof ("_nostub_data__") - 1))))
  399. {
  400. OFFSET ExportNumber = GetExportNumber (TempSymbol->Name + sizeof ("_nostub_") - 1);
  401. if (ExportNumber >= 0)
  402. {
  403. NostubComments = TRUE;
  404. TempSection->Essential = TRUE;
  405. break;
  406. }
  407. }
  408. }
  409. }
  410. if (NostubComments)
  411. Result = Result && AddGlobalImport (Program, "__nostub_comment_header");
  412. }
  413. #ifdef DATA_VAR_SUPPORT
  414. // Handle data variable if desired.
  415. if (Program->DataVarInfo->Name && Program->DataSection)
  416. {
  417. Result = Result && (AddGlobalImport (Program, "__handle_data_var"));
  418. if (Program->DataVarInfo->CreateCopy)
  419. {
  420. Result = Result && (AddGlobalImport (Program, "__data_var_create_copy"));
  421. if (Program->DataVarInfo->CopyOnlyIfArchived)
  422. Result = Result && (AddGlobalImport (Program, "__data_var_copy_if_archived"));
  423. }
  424. }
  425. #endif /* DATA_VAR_SUPPORT */
  426. return Result;
  427. }
  428. // If the reloc or its relation point to a special ld-exported symbol,
  429. // change it to the appropriate value if possible. FALSE is returned
  430. // only if it is not a special symbol location, or if there was an error.
  431. BOOLEAN ResolveSpecialSymbolReloc (RELOC *Reloc, BOOLEAN *TryAgain)
  432. {
  433. // At first, resolve the relation, because if this is a built-in
  434. // number, we cannot write it into the section contents directly,
  435. // but we have to subtract it from FixedOffset.
  436. BOOLEAN Result = ResolveSpecialSymbolRelocRelation (Reloc, TryAgain);
  437. // Now resolve the target.
  438. return (ResolveSpecialSymbolRelocTarget (Reloc, TryAgain) ? Result : FALSE);
  439. }
  440. // If the reloc points to a special ld-exported symbol, change it to the
  441. // appropriate value if possible. FALSE is returned only if it is not a
  442. // special symbol reloc, or if there was an error.
  443. BOOLEAN ResolveSpecialSymbolRelocTarget (RELOC *Reloc, BOOLEAN *TryAgain)
  444. {
  445. BOOLEAN Result = FALSE;
  446. SECTION *Section = Reloc->Parent;
  447. LOCATION *Location = &(Reloc->Target);
  448. // If the relation is an unresolved reference to a builtin number,
  449. // do not even try to resolve the target. This would only cause
  450. // trouble, since we would need to handle it immediately if it
  451. // resolved to a number. There will be another call to this anyway
  452. // after the relation has finally been resolved.
  453. // Usually, we would at least need to set the Target.Builtin flag.
  454. // However, currently all tests which check for Target.Builtin also
  455. // check for Relation or Relative in general. If this ever changes,
  456. // this code will break.
  457. if (Reloc->Relation && Reloc->Relation->Builtin)
  458. return TRUE;
  459. if (ResolveSpecialSymbolLocation (Section, Location, TryAgain))
  460. {
  461. // If it has resolved to a number, write it into the section
  462. // contents.
  463. if (!(Location->Symbol || Location->SymbolName))
  464. {
  465. if (Reloc->Relation)
  466. Warning (GetFileName (Section, Reloc->Location), "Ignoring invalid negative reference to `%s' at 0x%lX.", Reloc->Relation->SymbolName, (long) Reloc->Location);
  467. if ((Reloc->Location >= 0) && (Reloc->Location + Reloc->Size <= Section->Size))
  468. {
  469. // Add the target offset (to support things such as "__ld_xxx+1").
  470. OFFSET NewValue = Location->Offset + Reloc->FixedOffset;
  471. if (Reloc->Relative)
  472. Warning (GetFileName (Section, Reloc->Location), "Invalid relative reference to built-in number `%s' at 0x%lX; changing to absolute.", Reloc->Target.SymbolName, (long) Reloc->Location);
  473. // Check if the section contents at the reloc are zero.
  474. if (!(IsZeroDataRange (Section, Reloc->Location, Reloc->Location + Reloc->Size)))
  475. Warning (GetFileName (Section, Reloc->Location), "Builtin reloc at 0x%lX to `%s' on nonzero section contents. Overlapping with another?", (long) Reloc->Location, Reloc->Target.SymbolName);
  476. // Resolve the reloc by writing the value into the section.
  477. Result = AddTI (Section->Data + Reloc->Location, Reloc->Size, NewValue, TRUE, TRUE);
  478. if (!Result)
  479. Error (GetFileName (Section, Reloc->Location), "Number `%s' (=%ld) too large for size %ld reloc at 0x%lX.", Reloc->Target.SymbolName, (long) NewValue, (long) Reloc->Size, (long) Reloc->Location);
  480. }
  481. else
  482. {
  483. Warning (GetFileName (Section, Reloc->Location), "Removing reloc at 0x%lX to `%s' outside of section.", (long) Reloc->Location, Reloc->Target.SymbolName);
  484. Result = TRUE;
  485. }
  486. FreeReloc (Reloc);
  487. }
  488. else
  489. Result = TRUE;
  490. }
  491. return Result;
  492. }
  493. // If the reloc's relation points to a special ld-exported symbol,
  494. // change it to the appropriate value if possible. FALSE is returned
  495. // only if it is not a special symbol location, or if there was an error.
  496. BOOLEAN ResolveSpecialSymbolRelocRelation (RELOC *Reloc, BOOLEAN *TryAgain)
  497. {
  498. BOOLEAN Result = FALSE;
  499. SECTION *Section = Reloc->Parent;
  500. LOCATION *Location = Reloc->Relation;
  501. if (Reloc->Relation)
  502. {
  503. // Try to resolve the relation.
  504. if (ResolveSpecialSymbolLocation (Section, Location, TryAgain))
  505. {
  506. // Check if it has really been resolved.
  507. BOOLEAN Resolved = (Location->Symbol != NULL);
  508. // If it has been resolved to a number, subtract it from
  509. // FixedOffset.
  510. if (!(Location->Symbol || Location->SymbolName))
  511. {
  512. Reloc->FixedOffset -= Location->Offset;
  513. FreeRelocRelation (Reloc);
  514. Resolved = TRUE;
  515. }
  516. Result = TRUE;
  517. }
  518. }
  519. return Result;
  520. }
  521. // If the location points to a special ld-exported symbol, change it to the
  522. // appropriate value if possible. FALSE is returned only if it is not a
  523. // special symbol location, or if there was an error.
  524. // Warning: If the special symbol resolves to a number, Location->Symbol and
  525. // Location->SymbolName will both be NULL, and Location->Offset will contain
  526. // the number.
  527. BOOLEAN ResolveSpecialSymbolLocation (SECTION *Section, LOCATION *Location, BOOLEAN *TryAgain)
  528. {
  529. const char *SymName = Location->SymbolName;
  530. BOOLEAN SymNameMatches (const char *Name)
  531. {
  532. return (!(strcmp (SymName, Name)));
  533. }
  534. // Locations that are already resolved do not need any treatment.
  535. if (Location->Symbol)
  536. return FALSE;
  537. // All special symbols start with '_'.
  538. if (SymName && (SymName [0] == '_'))
  539. {
  540. PROGRAM *Program = Section->Parent;
  541. BOOLEAN SetToEntryPoint = FALSE;
  542. OFFSET NewValue = 0;
  543. SymName++;
  544. // All built-in symbols start with "__ld_".
  545. if (!(strncmp (SymName, SYMPF_BUILTIN + 1, sizeof (SYMPF_BUILTIN) - 2)))
  546. {
  547. SIZE SymNameLength;
  548. BOOLEAN HasValue = FALSE;
  549. SYMBOL *NewSymbol = NULL;
  550. OFFSET NewTargetOffset = 0;
  551. SECTION *TempSection;
  552. // Skip the "__ld_" prefix.
  553. SymName += sizeof (SYMPF_BUILTIN) - 2;
  554. SymNameLength = strlen (SymName);
  555. // Find (but do not resolve) calculator constants.
  556. if (IsCalcBuiltinLocation (Location))
  557. ;
  558. // Resolve references to insertions.
  559. else if (!(strncmp (SymName, SYMPF_INSERT, sizeof (SYMPF_INSERT) - 1)))
  560. {
  561. if ((!(TempSection = Program->MainSection))
  562. || (!(NewSymbol = HandleAutoInsertion (TempSection, Location->SymbolName))))
  563. return TRUE;
  564. }
  565. // Resolve "has_...s".
  566. else if ((!(strncmp (SymName, "has_", sizeof ("has_") - 1))) && (SymName [SymNameLength - 1] == 's'))
  567. {
  568. if (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->Essential = 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. }
  785. else
  786. Location->Builtin = TRUE;
  787. return TRUE;
  788. }
  789. }
  790. // If this is a reloc to a kernel-specific symbol, point it to the
  791. // entry point. The result of this is that the reloc's value
  792. // becomes 0 if the reloc was made up by something like
  793. // _exit-__kernel_entry_point.
  794. else if (SymNameMatches ("exit") || SymNameMatches ("comment") || SymNameMatches ("extraram") || SymNameMatches ("library"))
  795. SetToEntryPoint = TRUE;
  796. if (SetToEntryPoint)
  797. {
  798. if (Program->EntryPoint.Symbol)
  799. {
  800. Location->Symbol = Program->EntryPoint.Symbol;
  801. FreeLocationSymbolName (Section, Location);
  802. Location->Offset += Program->EntryPoint.Offset + NewValue;
  803. }
  804. return TRUE;
  805. }
  806. }
  807. return FALSE;
  808. }
  809. // Count the items for a specific built-in symbol, specified by SymName
  810. // and SymNameLength. If TrueValue is nonzero, items are not counted,
  811. // but NewValue is set to this value if at least one item was found. In
  812. // that case, if Program->ResolveAllBuiltins is false, NewValue may be
  813. // unchanged even though there are some items; you need to check back
  814. // later when Program->ResolveAllBuiltins is true.
  815. BOOLEAN GetBuiltinValue (PROGRAM *Program, const char *SymName, SIZE SymNameLength, OFFSET *NewValue, OFFSET TrueValue)
  816. {
  817. #define Count(n,op,code) \
  818. ({ \
  819. register OFFSET n__ = (n); \
  820. if (TrueValue) \
  821. { \
  822. if (n__) \
  823. { \
  824. *NewValue = TrueValue; \
  825. code; \
  826. } \
  827. } \
  828. else \
  829. *NewValue op n__; \
  830. })
  831. #define SetCounter(n) (Count ((n), =, (void) 0))
  832. #define IncreaseCounter(n) (Count ((n), +=, break))
  833. BOOLEAN SymNameMatches (const char *Name)
  834. {
  835. return (!(strncmp (SymName, Name, SymNameLength)));
  836. }
  837. SECTION *TempSection;
  838. RELOC *TempReloc;
  839. SYMBOL *TempSymbol;
  840. if (SymNameMatches ("constructor"))
  841. {
  842. if (Program->Constructors.Start && Program->Constructors.End)
  843. SetCounter ((Program->Constructors.End->Location - Program->Constructors.Start->Location) >> 2);
  844. }
  845. else if (SymNameMatches ("destructor"))
  846. {
  847. if (Program->Destructors.Start && Program->Destructors.End)
  848. SetCounter ((Program->Destructors.End->Location - Program->Destructors.Start->Location) >> 2);
  849. }
  850. else if (SymNameMatches ("reloc"))
  851. {
  852. // Count all absolute relocs.
  853. // Relative relocs will either be resolved completely or
  854. // produce errors.
  855. for_each (TempSection, Program->Sections)
  856. {
  857. IncreaseCounter (TempSection->Relocs.EmittedCount);
  858. // Since relocs may be removed, if ResolveAllBuiltins
  859. // is false, do not handle TrueValue.
  860. if ((!TrueValue) || Program->ResolveAllBuiltins)
  861. {
  862. for_each (TempReloc, TempSection->Relocs)
  863. if (!(TempReloc->Relative || TempReloc->Target.Builtin || (TempReloc->Target.Symbol && (TempReloc->Target.Symbol->Parent->Handled))))
  864. IncreaseCounter (1);
  865. }
  866. }
  867. }
  868. else if (SymNameMatches ("data_ref"))
  869. {
  870. if (Program->DataSection)
  871. {
  872. // Count all absolute relocs to the data section.
  873. for_each (TempSection, Program->Sections)
  874. for_each (TempReloc, TempSection->Relocs)
  875. if (TempReloc->Target.Symbol && (TempReloc->Target.Symbol->Parent == Program->DataSection) && (!(TempReloc->Relative || TempReloc->Target.Builtin)))
  876. IncreaseCounter (1);
  877. }
  878. }
  879. else if (SymNameMatches ("bss_ref"))
  880. {
  881. if (Program->BSSSection)
  882. {
  883. // Count all absolute relocs to the BSS section.
  884. for_each (TempSection, Program->Sections)
  885. for_each (TempReloc, TempSection->Relocs)
  886. if (TempReloc->Target.Symbol && (TempReloc->Target.Symbol->Parent == Program->BSSSection) && (!(TempReloc->Relative || TempReloc->Target.Builtin)))
  887. IncreaseCounter (1);
  888. }
  889. }
  890. else if (SymNameMatches ("rom_call"))
  891. {
  892. for_each (TempSection, Program->Sections)
  893. IncreaseCounter (CountItems (TempSection->ROMCalls, ROM_CALL));
  894. }
  895. else if (SymNameMatches ("ram_call"))
  896. {
  897. for_each (TempSection, Program->Sections)
  898. IncreaseCounter (CountItems (TempSection->RAMCalls, RAM_CALL));
  899. }
  900. else if (SymNameMatches ("lib"))
  901. SetCounter (CountItems (Program->Libraries, LIBRARY));
  902. else if (SymNameMatches ("referenced_lib"))
  903. SetCounter (Program->Libraries.ReferencedCount);
  904. else if (SymNameMatches ("export"))
  905. {
  906. // The number of exports is equal to the highest export number + 1.
  907. for_each (TempSection, Program->Sections)
  908. {
  909. for_each (TempSymbol, TempSection->Symbols)
  910. {
  911. if (TempSymbol->Exported)
  912. {
  913. OFFSET ExportNumber = GetExportNumber (TempSymbol->Name);
  914. if ((ExportNumber >= 0) && (*NewValue < ExportNumber + 1))
  915. Count (ExportNumber + 1, =, break);
  916. }
  917. }
  918. }
  919. }
  920. else if (SymNameMatches ("nostub_comment"))
  921. {
  922. for_each (TempSection, Program->Sections)
  923. {
  924. for_each (TempSymbol, TempSection->Symbols)
  925. {
  926. if (TempSymbol->Exported && (!(strncmp (TempSymbol->Name, SYMPF_NOSTUB_DATA, sizeof (SYMPF_NOSTUB_DATA) - 1))))
  927. {
  928. OFFSET ExportNumber = GetExportNumber (TempSymbol->Name + (sizeof (SYMPF_NOSTUB_DATA_START) - 1));
  929. if (ExportNumber >= 0)
  930. IncreaseCounter (1);
  931. }
  932. }
  933. }
  934. }
  935. else
  936. return FALSE;
  937. #undef IncreaseCounter
  938. #undef SetCounter
  939. #undef Count
  940. return TRUE;
  941. }
  942. // If the given symbol name belongs to a calculator-specific builtin
  943. // symbol, return a pointer to the part of it that holds the values.
  944. static const char *GetCalcBuiltinValues (const char *SymName)
  945. {
  946. if (!(strncmp (SymName, SYMPF_BUILTIN_CALC_CONST, sizeof (SYMPF_BUILTIN_CALC_CONST) - 1)))
  947. {
  948. return (SymName + (sizeof (SYMPF_BUILTIN_CALC_CONST) - 1));
  949. }
  950. return NULL;
  951. }
  952. // Check if the given location points to a calculator-specific builtin
  953. // symbol.
  954. BOOLEAN IsCalcBuiltinLocation (const LOCATION *Location)
  955. {
  956. return (Location->SymbolName && (GetCalcBuiltinValues (Location->SymbolName) || (!(strcmp (Location->SymbolName, SYM_BUILTIN_HARDWARE_ID)))));
  957. }
  958. // Return whether the reloc can be resolved to a calculator-specific value.
  959. // ResolveSpecialSymbol or something related must have been called on the
  960. // reloc at least once.
  961. BOOLEAN IsPlainCalcBuiltin (const RELOC *Reloc)
  962. {
  963. // To improve speed, check whether the target and relation (if it exists)
  964. // are builtin symbols.
  965. if (Reloc->Target.Builtin && ((!(Reloc->Relation)) || Reloc->Relation->Builtin))
  966. {
  967. // Check the target.
  968. if (IsCalcBuiltinLocation (&(Reloc->Target)))
  969. {
  970. // If there is a relation, check it.
  971. if (Reloc->Relation)
  972. return IsCalcBuiltinLocation (Reloc->Relation);
  973. // Otherwise, the reloc may not be relative, since that would
  974. // mean that it cannot be resolved to a number.
  975. else
  976. return (!(Reloc->Relative));
  977. }
  978. }
  979. return FALSE;
  980. }
  981. // If the location can be resolved to a calculator-specific value, get the
  982. // value for the specified calculator.
  983. // If IsCalcBuiltinLocation returned a positive result for this reloc, this
  984. // function will not return a negative result.
  985. BOOLEAN GetCalcBuiltinLocationValue (const LOCATION *Location, ProgramCalcs DestCalc, IMAX *Value)
  986. {
  987. // Basic sanity checks.
  988. if (Value && Location->SymbolName)
  989. {
  990. // Special case: __ld_hardware_id
  991. if (!(strcmp (Location->SymbolName, SYM_BUILTIN_HARDWARE_ID)))
  992. {
  993. switch (DestCalc)
  994. {
  995. case CALC_TI89:
  996. *Value = 3;
  997. break;
  998. case CALC_TI89 | CALC_FLAG_TITANIUM:
  999. *Value = 9;
  1000. break;
  1001. case CALC_TI92PLUS:
  1002. *Value = 1;
  1003. break;
  1004. case CALC_V200:
  1005. *Value = 8;
  1006. break;
  1007. default:
  1008. Warning (NULL, SYM_BUILTIN_HARDWARE_ID " not defined for this calculator.");
  1009. *Value = 0;
  1010. }
  1011. *Value += Location->Offset;
  1012. return TRUE;
  1013. }
  1014. else
  1015. {
  1016. // Get the part of the symbol name that holds the values,
  1017. // separated by '_'.
  1018. const char *Values = GetCalcBuiltinValues (Location->SymbolName);
  1019. if (Values)
  1020. {
  1021. // AND out the calculator flags.
  1022. DestCalc &= ~CALC_FLAG_TITANIUM;
  1023. // While we still have at least one value left...
  1024. while (Values)
  1025. {
  1026. // Get the end of the value string.
  1027. const char *ValueEnd = strchr (Values, '_');
  1028. SIZE ValueSize = ValueEnd ? (SIZE) (ValueEnd - Values) : (SIZE) (strlen (Values));
  1029. // If this is the value that belongs to the current calculator,
  1030. // extract the value and return.
  1031. if (DestCalc == 1)
  1032. {
  1033. char *EndPtr = NULL;
  1034. // Create a copy of the value, with a terminating zero byte.
  1035. char ValueStr[ValueSize+1];
  1036. strncpy (ValueStr, Values, ValueSize);
  1037. ValueStr [ValueSize] = 0;
  1038. // Convert this string into a number and return it.
  1039. *Value = strtoul (ValueStr, &EndPtr, 0);
  1040. if (EndPtr && *EndPtr)
  1041. Warning (NULL, "Invalid number `%s' in `%s'.", ValueStr, Location->SymbolName);
  1042. *Value += Location->Offset;
  1043. return TRUE;
  1044. }
  1045. // Advance to the next value.
  1046. Values = ValueEnd ? ValueEnd + 1 : NULL;
  1047. // Advance to the next calculator.
  1048. DestCalc >>= 1;
  1049. }
  1050. // No more values were found, but the function did not exit yet.
  1051. Warning (NULL, "Calculator constant `%s' contains too few values.", Location->SymbolName);
  1052. *Value = 0;
  1053. // We have to return a positive result anyway because
  1054. // IsCalcBuiltinLocation would as well.
  1055. return TRUE;
  1056. }
  1057. }
  1058. }
  1059. return FALSE;
  1060. }
  1061. // If the reloc can be resolved to a calculator-specific value, get the
  1062. // value for the specified calculator.
  1063. // The return value is the same as for IsPlainCalcBuiltin.
  1064. BOOLEAN GetCalcBuiltinValue (const RELOC *Reloc, ProgramCalcs DestCalc, IMAX *Value)
  1065. {
  1066. if (Value)
  1067. {
  1068. // Get the value for the target.
  1069. if (GetCalcBuiltinLocationValue (&(Reloc->Target), DestCalc, Value))
  1070. {
  1071. // If there is a relation, subtract its value.
  1072. if (Reloc->Relation)
  1073. {
  1074. IMAX RelationValue;
  1075. if (!(GetCalcBuiltinLocationValue (Reloc->Relation, DestCalc, &RelationValue)))
  1076. return FALSE;
  1077. *Value -= RelationValue;
  1078. }
  1079. // Otherwise, the reloc may not be relative
  1080. // (see IsPlainCalcBuiltin).
  1081. else if (Reloc->Relative)
  1082. return FALSE;
  1083. *Value += Reloc->FixedOffset;
  1084. return TRUE;
  1085. }
  1086. }
  1087. return FALSE;
  1088. }
  1089. // If required by some special symbol(s) at the end of the section,
  1090. // modify the contents of the section.
  1091. // This can be used to insert special items such as relocation entries.
  1092. // MergedSection specifies the (usually large) part of the program that
  1093. // has already been merged.
  1094. // If necessary, MergedSection is frozen automatically.
  1095. BOOLEAN HandleSectionContents (SECTION *Section, SECTION *MergedSection)
  1096. {
  1097. SYMBOL *Symbol;
  1098. // Search the labels at the end of the secion to find special ones.
  1099. for (Symbol = FindSymbolAtPos (Section, Section->Size, TRUE); Symbol; Symbol = GetNext (Symbol))
  1100. {
  1101. if (!(strncmp (Symbol->Name, SYMPF_BUILTIN_INSERT, sizeof (SYMPF_BUILTIN_INSERT) - 1)))
  1102. return (HandleInsertion (Section, Symbol->Location, Symbol->Name + sizeof (SYMPF_BUILTIN_INSERT) - 1, MergedSection, FALSE));
  1103. }
  1104. return TRUE;
  1105. }
  1106. // Insert contents for an insertion specified by SymbolName, and return
  1107. // a symbol from where the insertion took place.
  1108. // If necessary, Section is frozen automatically.
  1109. SYMBOL *HandleAutoInsertion (SECTION *Section, const char *SymbolName)
  1110. {
  1111. if (!(strncmp (SymbolName, SYMPF_BUILTIN_INSERT, sizeof (SYMPF_BUILTIN_INSERT) - 1)))
  1112. {
  1113. CreateSectionSegment (Section);
  1114. // All insertions except compressed ones should be aligned on a 2-byte boundary.
  1115. if ((!(strncmp (SymbolName, SYMPF_BUILTIN_INSERT_COMPRESSED, sizeof (SYMPF_BUILTIN_INSERT_COMPRESSED) - 1)))
  1116. || (!(strncmp (SymbolName, SYMPF_BUILTIN_INSERT_MLINK, sizeof (SYMPF_BUILTIN_INSERT_MLINK) - 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. // Compressed relocation tables using our own mlink-style format.
  1237. else if (NameMatches ("mlink_relocs"))
  1238. return InsertMlinkRelocs (Section, NULL, MergedSection, &Reference);
  1239. else if (NameMatches ("mlink_bss_refs"))
  1240. return InsertMlinkSectionRefs (Section, Program->BSSSection, MergedSection, &Reference);
  1241. else if (NameMatches ("mlink_data_refs"))
  1242. return InsertMlinkSectionRefs (Section, Program->DataSection, MergedSection, &Reference);
  1243. else if (NameMatches ("mlink_rom_calls"))
  1244. return InsertMlinkROMCalls (Section, MergedSection, &Reference);
  1245. else
  1246. Warning (GetFileName (Section, Section->Size), "Unrecognized insertion `%s'.", Name);
  1247. }
  1248. }
  1249. else
  1250. {
  1251. Error (NULL, "Out of memory.");
  1252. return FALSE;
  1253. }
  1254. }
  1255. return TRUE;
  1256. }