data.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* data.h: Definitions for internal data handling
  2. Copyright (C) 2002-2004 Sebastian Reichelt
  3. Copyright (C) 2003-2004 Kevin Kofler
  4. Copyright (C) 2004 Billy Charvet
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  16. #ifndef DATA_H
  17. #define DATA_H
  18. #include "generic.h"
  19. #include "lists.h"
  20. #include "intrface.h"
  21. #define MAX_SYM_LEN 255
  22. struct PROGRAM;
  23. struct SECTION;
  24. struct SEGMENT;
  25. struct SYMBOL;
  26. struct RELOC;
  27. struct ROM_CALL;
  28. struct RAM_CALL;
  29. struct LIB_CALL;
  30. struct LIBRARY;
  31. struct ARCHIVE;
  32. struct ARCHIVE_OBJECT;
  33. struct ARCHIVE_SYMBOL;
  34. struct GLOBAL_IMPORT;
  35. // Program Type Enumeration
  36. // PT_NATIVE: ld-tigcc native mode; requires the manual definition of at least one startup section; not for use with Fargo and nostub DLLs
  37. // PT_NOSTUB: Traditional nostub mode; execution starts at the beginning of the first section
  38. // PT_KERNEL: Traditional kernel mode; kernel stub is included automatically
  39. // PT_NOSTUB_DLL: Nostub DLL mode; uses specific output format
  40. // PT_FLASH_OS: Flash OS mode: uses specific output and file format
  41. // PT_FARGO: Fargo II mode; uses specific output format
  42. typedef enum {PT_NATIVE = 1, PT_NOSTUB = 2, PT_KERNEL = 0, PT_NOSTUB_DLL = 3, PT_FLASH_OS = 4, PT_FARGO = 5} ProgramTypes;
  43. typedef I4 VERSION;
  44. // Named Location in a Section
  45. typedef struct {
  46. struct SYMBOL *Symbol; // Pointer to symbol, may be NULL.
  47. const char *SymbolName; // Symbol name. Must free if Symbol is NULL. Otherwise points to Symbol->Name. May not be NULL.
  48. OFFSET Offset; // Offset in relation to the symbol. When optimizing, this is considered part of the address.
  49. BOOLEAN Builtin; // Specifies whether the location is to be replaced by a built-in number.
  50. } LOCATION;
  51. // Section Markers
  52. typedef struct {
  53. struct SYMBOL *Start, *End;
  54. } SECTION_MARKERS;
  55. // Complete Program
  56. typedef struct PROGRAM {
  57. ProgramTypes Type; // The target type of the program.
  58. BOOLEAN Library; // Specifies whether this is a library.
  59. unsigned int Calcs; // The calculators the program is designed for (constants defined by enum ProgramCalcs).
  60. VERSION Version; // Version number of the program or library.
  61. I2 KernelFlags; // Kernel program flags; only used in kernel mode.
  62. struct {
  63. LIST_HEADER(struct SECTION);
  64. } Sections; // Sections in the program.
  65. struct {
  66. LIST_HEADER(struct LIBRARY);
  67. COUNT ReferencedCount; // The number of referenced libraries.
  68. } Libraries; // Run-time libraries used by the program.
  69. struct {
  70. LIST_HEADER(struct ARCHIVE);
  71. } Archives; // Available archives which object files can be imported from.
  72. struct {
  73. LIST_HEADER(struct GLOBAL_IMPORT);
  74. BOOLEAN ResolveInversions; // Specifies whether negated symbols should be processed. Otherwise, they are delayed.
  75. } GlobalImports; // Global imports; see AddGlobalImport in manip.h.
  76. BOOLEAN
  77. ResolveAllBuiltins, // Specifies that all builtin symbols should be resolved now, e.g. to 0 or NULL in some cases.
  78. IgnoreGlobalImports, // Specifies that the __ref_all_... symbol has no effect.
  79. Frozen; // Specifies that no positions, numbers, and sizes may be changed any more.
  80. LOCATION EntryPoint; // Entry point of the program.
  81. struct SECTION
  82. *MainSection, // Pointer to the main section, as soon as it is known. Usually only this section has to be written to the file.
  83. *DataSection, // Pointer to the data section, if code and data are separated.
  84. *BSSSection; // Pointer to the BSS section, if the program contains one.
  85. struct GLOBAL_IMPORT
  86. *BSSImport; // Pointer to the global import which handles the BSS section.
  87. SECTION_MARKERS
  88. Constructors, // Beginning and end of constructor section.
  89. Destructors; // Beginning and end of destructor section.
  90. OFFSET
  91. HighestROMCall, // Holds the highest ROM call number used.
  92. HighestRAMCall; // Holds the highest RAM call number used.
  93. #ifdef DATA_VAR_SUPPORT
  94. DATA_VAR_INFO
  95. *DataVarInfo; // Data variable settings.
  96. #endif /* DATA_VAR_SUPPORT */
  97. OPTIMIZE_INFO
  98. *OptimizeInfo; // Optimization settings and results.
  99. } PROGRAM;
  100. // Section in a Program
  101. typedef struct SECTION {
  102. LIST_ITEM_HEADER(struct SECTION);
  103. PROGRAM *Parent;
  104. SIZE Size; // Size of the section, in bytes.
  105. I1 *Data; // Pointer to section data, NULL if no data. Need to free this at the end.
  106. BOOLEAN Initialized; // If Data is NULL, this indicates whether the section is initialized with zeroes; otherwise it is always true.
  107. BOOLEAN Code; // Contains code. If this is true, Initialized must be true as well.
  108. BOOLEAN Mergeable; // Specifies whether this section contains one or more constants which may be merged on a symbol basis.
  109. BOOLEAN Unaligned; // Specifies whether this section can be placed at an unaligned address. If the section is mergeable, it is also assumed that each symbol can be unaligned.
  110. // (The latter case is the only one currently implemented.)
  111. BOOLEAN Referenced; // Specifies whether the section is referenced somewhere (in a reloc).
  112. OFFSET StartupNumber; // If nonzero, specifies the location of the section in respect to other startup sections.
  113. BOOLEAN Constructors; // Is a vector of constructor functions.
  114. BOOLEAN Destructors; // Is a vector of destructor functions.
  115. BOOLEAN CanCutRanges; // Range cutting is allowed at least in parts of the section (i.e. the section or segments in it do not contain any implicit relative relocs).
  116. BOOLEAN Frozen; // Aside from trivial changes (i.e. changes to binary data or additions at the end), this section must stay as it is.
  117. BOOLEAN Handled; // Specifies whether the section is already handled and doesn't need any further treatment. This implies Frozen.
  118. struct {
  119. LIST_HEADER(struct SEGMENT);
  120. } Segments; // Sorted list of segments. If empty, this means the section consists of one big segment.
  121. struct {
  122. LIST_HEADER(struct SYMBOL);
  123. } Symbols; // Sorted list of symbols (labels).
  124. struct {
  125. LIST_HEADER(struct RELOC);
  126. COUNT UnresolvedCount; // The number of unresolved relocs and reloc relations.
  127. COUNT EmittedCount; // The number of already emitted relocs (which have been removed or changed to program-relative ones).
  128. COUNT OptRefCount; // The number of optimizable references pointing from and to this section.
  129. BOOLEAN RelativeRefs; // Specifies whether there are relative references from or to this section.
  130. BOOLEAN StartupRefs; // Specifies whether this section references or is referenced by a startup section.
  131. } Relocs; // Sorted list of relocation entries.
  132. struct {
  133. LIST_HEADER(struct ROM_CALL);
  134. BOOLEAN Handled; // Specifies whether the program has requested information about ROM calls.
  135. } ROMCalls; // Sorted list of ROM (AMS) calls.
  136. struct {
  137. LIST_HEADER(struct RAM_CALL);
  138. BOOLEAN Handled; // Specifies whether the program has requested information about RAM calls.
  139. } RAMCalls; // Sorted list of RAM (kernel) calls.
  140. struct {
  141. LIST_HEADER(struct LIB_CALL);
  142. BOOLEAN Handled; // Specifies whether the program has requested information about library calls.
  143. } LibCalls; // Sorted list of library calls.
  144. struct SYMBOL
  145. *SectionSymbol; // Symbol pointing to the beginning of the section. May only be NULL on fatal errors.
  146. const char *FileName; // File where the section originated from. May be NULL.
  147. } SECTION;
  148. // Segment of a Section.
  149. // Identifies a range that belongs to a specific pre-merge section.
  150. typedef struct SEGMENT {
  151. LIST_ITEM_HEADER(struct SEGMENT);
  152. SECTION_MARKERS Location; // Symbols specifying the beginning and end of the segment.
  153. BOOLEAN Code; // The segment contains code.
  154. BOOLEAN CanCutRanges; // Range cutting is allowed (i.e. the segment does not contain any implicit relative relocs).
  155. const char *FileName; // File where the segment originated from. May be NULL.
  156. } SEGMENT;
  157. // Symbol (Label) in a Section
  158. typedef struct SYMBOL {
  159. LIST_ITEM_HEADER(struct SYMBOL);
  160. SECTION *Parent;
  161. OFFSET Location; // Location of the symbol inside the section data.
  162. char Name[MAX_SYM_LEN+1]; // Symbol name.
  163. BOOLEAN Exported; // Only exported symbols are possible targets for name resolution.
  164. } SYMBOL;
  165. // Relocation Entry in a Section
  166. typedef struct RELOC {
  167. LIST_ITEM_HEADER(struct RELOC);
  168. SECTION *Parent;
  169. OFFSET Location; // Location of the reloc inside the section data.
  170. SIZE Size; // Size of the reloc, in bytes.
  171. LOCATION Target; // The location of the target the reloc points to.
  172. BOOLEAN Relative; // If true, the value to be inserted is relative to the location of the reloc. If false, it is the absolute address of the target.
  173. LOCATION *Relation; // If Relative is true, this specifies that the value is not relative to the location of the reloc, but to this location. It must be freed.
  174. OFFSET FixedOffset; // Fixed offset to add after relocation. When optimizing (i.e., removing code), this is NOT considered part of the address.
  175. BOOLEAN Unoptimizable;// If true, the reloc is not a source operand or branch target and can never be optimized.
  176. } RELOC;
  177. // ROM Call Reference in a Section
  178. typedef struct ROM_CALL {
  179. LIST_ITEM_HEADER(struct ROM_CALL);
  180. SECTION *Parent;
  181. OFFSET Location; // Location of the ROM call reference inside the section data.
  182. SIZE Size; // Size of the ROM call reference, in bytes.
  183. OFFSET Number; // Number of the ROM call.
  184. OFFSET FixedOffset; // Offset in relation to the destination.
  185. } ROM_CALL;
  186. // RAM Call Reference in a Section
  187. typedef struct RAM_CALL {
  188. LIST_ITEM_HEADER(struct RAM_CALL);
  189. SECTION *Parent;
  190. OFFSET Location; // Location of the RAM call reference inside the section data.
  191. SIZE Size; // Size of the RAM call reference, in bytes.
  192. OFFSET Number; // Number of the RAM call.
  193. OFFSET FixedOffset; // Offset in relation to the destination.
  194. BOOLEAN ExtraRAMAddr; // Specifies whether we mean a program-defined extra RAM table address.
  195. } RAM_CALL;
  196. // Run-Time Library Used by the Program.
  197. typedef struct LIBRARY {
  198. LIST_ITEM_HEADER(struct LIBRARY);
  199. PROGRAM *Parent;
  200. char Name[MAX_SYM_LEN+1]; // Library Name.
  201. VERSION Version; // Required minimum version number.
  202. OFFSET Highest; // Holds the highest function number imported from this library.
  203. BOOLEAN Referenced; // Library is actually used.
  204. } LIBRARY;
  205. // Library Call Reference in a Section
  206. typedef struct LIB_CALL {
  207. LIST_ITEM_HEADER(struct LIB_CALL);
  208. SECTION *Parent;
  209. OFFSET Location; // Location of the library call reference inside the section data.
  210. SIZE Size; // Size of the library call reference, in bytes.
  211. LIBRARY *Library; // Library the call references.
  212. OFFSET Number; // Number of the library export.
  213. OFFSET FixedOffset; // Offset in relation to the destination.
  214. } LIB_CALL;
  215. // Archive Available to the Program
  216. typedef struct ARCHIVE {
  217. LIST_ITEM_HEADER(struct ARCHIVE);
  218. PROGRAM *Parent;
  219. const I1 *Data; // Pointer to archive contents. Need to free this at the end.
  220. SIZE Size; // Size of the archive, in bytes.
  221. struct {
  222. LIST_HEADER(struct ARCHIVE_OBJECT);
  223. } ObjectFiles; // List of known object files.
  224. struct {
  225. LIST_HEADER(struct ARCHIVE_SYMBOL);
  226. } Symbols; // List of exported symbols.
  227. const char *FileName; // File name of the archive.
  228. } ARCHIVE;
  229. // Object File in an Archive
  230. typedef struct ARCHIVE_OBJECT {
  231. LIST_ITEM_HEADER(struct ARCHIVE_OBJECT);
  232. ARCHIVE *Parent;
  233. FILE_PTR FileOffset; // Offset of the object file inside the archive.
  234. BOOLEAN Imported; // Specifies whether the file has already been imported.
  235. } ARCHIVE_OBJECT;
  236. // Exported Symbol in an Archive
  237. typedef struct ARCHIVE_SYMBOL {
  238. LIST_ITEM_HEADER(struct ARCHIVE_SYMBOL);
  239. ARCHIVE *Parent;
  240. const char *Name; // Symbol name (pointer into Parent->Data).
  241. ARCHIVE_OBJECT *ObjectFile; // Object file this symbol belongs to.
  242. BOOLEAN ContainsInversion; // Specifies whether the symbol contains an inversion (NOT operator) for global imports.
  243. } ARCHIVE_SYMBOL;
  244. // Global Import
  245. typedef struct GLOBAL_IMPORT {
  246. LIST_ITEM_HEADER(struct GLOBAL_IMPORT);
  247. PROGRAM *Parent;
  248. char SymbolName[MAX_SYM_LEN+1]; // Symbol name.
  249. BOOLEAN Succeeded; // Specifies whether a symbol matching this import has been found.
  250. } GLOBAL_IMPORT;
  251. #endif