data.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* data.h: Definitions for internal data handling
  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. #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. // Debugging Information Section Type Enumeration
  44. // DI_NONE: Not debugging information (same as FALSE)
  45. // DI_STAB: Stabs symbol table
  46. // DI_STABSTR: Stabs string table
  47. // DI_LAST: Last debugging information type, for iterating purposes
  48. typedef enum {DI_NONE = FALSE, DI_STAB = 1, DI_STABSTR = 2, DI_LAST = 2} DebuggingInfoTypes;
  49. typedef I4 VERSION;
  50. // Named Location in a Section
  51. typedef struct {
  52. struct SYMBOL *Symbol; // Pointer to symbol, may be NULL.
  53. const char *SymbolName; // Symbol name. Must free if Symbol is NULL. Otherwise points to Symbol->Name. May not be NULL.
  54. OFFSET Offset; // Offset in relation to the symbol. When optimizing, this is considered part of the address.
  55. BOOLEAN Builtin; // Specifies whether the location is to be replaced by a built-in number.
  56. } LOCATION;
  57. // Section Markers
  58. typedef struct {
  59. struct SYMBOL *Start, *End;
  60. } SECTION_MARKERS;
  61. // Complete Program
  62. typedef struct PROGRAM {
  63. ProgramTypes Type; // The target type of the program.
  64. BOOLEAN Library; // Specifies whether this is a library.
  65. unsigned int Calcs; // The calculators the program is designed for (constants defined by enum ProgramCalcs).
  66. VERSION Version; // Version number of the program or library.
  67. I2 KernelFlags; // Kernel program flags; only used in kernel mode.
  68. struct {
  69. LIST_HEADER(struct SECTION);
  70. } Sections; // Sections in the program.
  71. struct {
  72. LIST_HEADER(struct LIBRARY);
  73. COUNT ReferencedCount; // The number of referenced libraries.
  74. } Libraries; // Run-time libraries used by the program.
  75. struct {
  76. LIST_HEADER(struct ARCHIVE);
  77. } Archives; // Available archives which object files can be imported from.
  78. struct {
  79. LIST_HEADER(struct GLOBAL_IMPORT);
  80. BOOLEAN ResolveInversions; // Specifies whether negated symbols should be processed. Otherwise, they are delayed.
  81. } GlobalImports; // Global imports; see AddGlobalImport in manip.h.
  82. BOOLEAN
  83. ResolveAllBuiltins, // Specifies that all builtin symbols should be resolved now, e.g. to 0 or NULL in some cases.
  84. IgnoreGlobalImports, // Specifies that the __ref_all_... symbol has no effect.
  85. Frozen; // Specifies that no positions, numbers, and sizes may be changed any more.
  86. LOCATION EntryPoint; // Entry point of the program.
  87. struct SECTION
  88. *MainSection, // Pointer to the main section, as soon as it is known. Usually only this section has to be written to the file.
  89. *DataSection, // Pointer to the data section, if code and data are separated.
  90. *BSSSection; // Pointer to the BSS section, if the program contains one.
  91. #ifdef DEBUGGING_INFO_SUPPORT
  92. BOOLEAN HaveDebuggingInfo; // Flag set if any debugging information is present, so we don't emit a .dbg file if there is no debugging information anyway.
  93. struct SECTION
  94. *DebuggingInfoSection[DI_LAST]; // Pointers to the debugging information sections of each type.
  95. #endif
  96. struct GLOBAL_IMPORT
  97. *BSSImport; // Pointer to the global import which handles the BSS section.
  98. SECTION_MARKERS
  99. Constructors, // Beginning and end of constructor section.
  100. Destructors; // Beginning and end of destructor section.
  101. OFFSET
  102. HighestROMCall, // Holds the highest ROM call number used.
  103. HighestRAMCall; // Holds the highest RAM call number used.
  104. #ifdef DATA_VAR_SUPPORT
  105. DATA_VAR_INFO
  106. *DataVarInfo; // Data variable settings.
  107. #endif /* DATA_VAR_SUPPORT */
  108. OPTIMIZE_INFO
  109. *OptimizeInfo; // Optimization settings and results.
  110. } PROGRAM;
  111. // Section in a Program
  112. typedef struct SECTION {
  113. LIST_ITEM_HEADER(struct SECTION);
  114. PROGRAM *Parent;
  115. SIZE Size; // Size of the section, in bytes.
  116. I1 *Data; // Pointer to section data, NULL if no data. Need to free this at the end.
  117. BOOLEAN Initialized; // If Data is NULL, this indicates whether the section is initialized with zeroes; otherwise it is always true.
  118. BOOLEAN Code; // Contains code. If this is true, Initialized must be true as well.
  119. BOOLEAN Mergeable; // Specifies whether this section contains one or more constants which may be merged on a symbol basis.
  120. 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.
  121. // (The latter case is the only one currently implemented.)
  122. BOOLEAN Essential; // Specifies whether the section is essential (unremovable).
  123. BOOLEAN Referenced; // Specifies whether the section is either essential itself or referenced somewhere (in a reloc) from an essential section.
  124. // This flag is significant only during RemoveUnusedSections. Before (or if not removing unused sections), it is always FALSE, afterwards, it is always TRUE.
  125. OFFSET StartupNumber; // If nonzero, specifies the location of the section in respect to other startup sections.
  126. DebuggingInfoTypes DebuggingInfoType; // Nonzero if this is a debugging information section, the actual value indicates what type of debugging information.
  127. BOOLEAN Constructors; // Is a vector of constructor functions.
  128. BOOLEAN Destructors; // Is a vector of destructor functions.
  129. 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).
  130. BOOLEAN Frozen; // Aside from trivial changes (i.e. changes to binary data or additions at the end), this section must stay as it is.
  131. BOOLEAN Handled; // Specifies whether the section is already handled and doesn't need any further treatment. This implies Frozen.
  132. struct {
  133. LIST_HEADER(struct SEGMENT);
  134. } Segments; // Sorted list of segments. If empty, this means the section consists of one big segment.
  135. struct {
  136. LIST_HEADER(struct SYMBOL);
  137. } Symbols; // Sorted list of symbols (labels).
  138. struct {
  139. LIST_HEADER(struct RELOC);
  140. COUNT UnresolvedCount; // The number of unresolved relocs and reloc relations.
  141. COUNT EmittedCount; // The number of already emitted relocs (which have been removed or changed to program-relative ones).
  142. COUNT OptRefCount; // The number of optimizable references pointing from and to this section.
  143. BOOLEAN RelativeRefs; // Specifies whether there are relative references from or to this section.
  144. BOOLEAN StartupRefs; // Specifies whether this section references or is referenced by a startup section.
  145. } Relocs; // Sorted list of relocation entries.
  146. struct {
  147. LIST_HEADER(struct ROM_CALL);
  148. BOOLEAN Handled; // Specifies whether the program has requested information about ROM calls.
  149. } ROMCalls; // Sorted list of ROM (AMS) calls.
  150. struct {
  151. LIST_HEADER(struct RAM_CALL);
  152. BOOLEAN Handled; // Specifies whether the program has requested information about RAM calls.
  153. } RAMCalls; // Sorted list of RAM (kernel) calls.
  154. struct {
  155. LIST_HEADER(struct LIB_CALL);
  156. BOOLEAN Handled; // Specifies whether the program has requested information about library calls.
  157. } LibCalls; // Sorted list of library calls.
  158. struct SYMBOL
  159. *SectionSymbol; // Symbol pointing to the beginning of the section. May only be NULL on fatal errors.
  160. const char *FileName; // File where the section originated from. May be NULL.
  161. } SECTION;
  162. // Segment of a Section.
  163. // Identifies a range that belongs to a specific pre-merge section.
  164. typedef struct SEGMENT {
  165. LIST_ITEM_HEADER(struct SEGMENT);
  166. SECTION_MARKERS Location; // Symbols specifying the beginning and end of the segment.
  167. BOOLEAN Code; // The segment contains code.
  168. BOOLEAN CanCutRanges; // Range cutting is allowed (i.e. the segment does not contain any implicit relative relocs).
  169. const char *FileName; // File where the segment originated from. May be NULL.
  170. } SEGMENT;
  171. // Symbol (Label) in a Section
  172. typedef struct SYMBOL {
  173. LIST_ITEM_HEADER(struct SYMBOL);
  174. SECTION *Parent;
  175. OFFSET Location; // Location of the symbol inside the section data.
  176. char Name[MAX_SYM_LEN+1]; // Symbol name.
  177. BOOLEAN Exported; // Only exported symbols are possible targets for name resolution.
  178. } SYMBOL;
  179. // Relocation Entry in a Section
  180. typedef struct RELOC {
  181. LIST_ITEM_HEADER(struct RELOC);
  182. SECTION *Parent;
  183. OFFSET Location; // Location of the reloc inside the section data.
  184. SIZE Size; // Size of the reloc, in bytes.
  185. LOCATION Target; // The location of the target the reloc points to.
  186. 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.
  187. 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.
  188. OFFSET FixedOffset; // Fixed offset to add after relocation. When optimizing (i.e., removing code), this is NOT considered part of the address.
  189. BOOLEAN Unoptimizable;// If true, the reloc is not a source operand or branch target and can never be optimized.
  190. } RELOC;
  191. // ROM Call Reference in a Section
  192. typedef struct ROM_CALL {
  193. LIST_ITEM_HEADER(struct ROM_CALL);
  194. SECTION *Parent;
  195. OFFSET Location; // Location of the ROM call reference inside the section data.
  196. SIZE Size; // Size of the ROM call reference, in bytes.
  197. OFFSET Number; // Number of the ROM call.
  198. OFFSET FixedOffset; // Offset in relation to the destination.
  199. } ROM_CALL;
  200. // RAM Call Reference in a Section
  201. typedef struct RAM_CALL {
  202. LIST_ITEM_HEADER(struct RAM_CALL);
  203. SECTION *Parent;
  204. OFFSET Location; // Location of the RAM call reference inside the section data.
  205. SIZE Size; // Size of the RAM call reference, in bytes.
  206. OFFSET Number; // Number of the RAM call.
  207. OFFSET FixedOffset; // Offset in relation to the destination.
  208. BOOLEAN ExtraRAMAddr; // Specifies whether we mean a program-defined extra RAM table address.
  209. } RAM_CALL;
  210. // Run-Time Library Used by the Program.
  211. typedef struct LIBRARY {
  212. LIST_ITEM_HEADER(struct LIBRARY);
  213. PROGRAM *Parent;
  214. char Name[MAX_SYM_LEN+1]; // Library Name.
  215. VERSION Version; // Required minimum version number.
  216. OFFSET Highest; // Holds the highest function number imported from this library.
  217. BOOLEAN Referenced; // Library is actually used.
  218. } LIBRARY;
  219. // Library Call Reference in a Section
  220. typedef struct LIB_CALL {
  221. LIST_ITEM_HEADER(struct LIB_CALL);
  222. SECTION *Parent;
  223. OFFSET Location; // Location of the library call reference inside the section data.
  224. SIZE Size; // Size of the library call reference, in bytes.
  225. LIBRARY *Library; // Library the call references.
  226. OFFSET Number; // Number of the library export.
  227. OFFSET FixedOffset; // Offset in relation to the destination.
  228. } LIB_CALL;
  229. // Archive Available to the Program
  230. typedef struct ARCHIVE {
  231. LIST_ITEM_HEADER(struct ARCHIVE);
  232. PROGRAM *Parent;
  233. const I1 *Data; // Pointer to archive contents. Need to free this at the end.
  234. SIZE Size; // Size of the archive, in bytes.
  235. struct {
  236. LIST_HEADER(struct ARCHIVE_OBJECT);
  237. } ObjectFiles; // List of known object files.
  238. struct {
  239. LIST_HEADER(struct ARCHIVE_SYMBOL);
  240. } Symbols; // List of exported symbols.
  241. const char *FileName; // File name of the archive.
  242. } ARCHIVE;
  243. // Object File in an Archive
  244. typedef struct ARCHIVE_OBJECT {
  245. LIST_ITEM_HEADER(struct ARCHIVE_OBJECT);
  246. ARCHIVE *Parent;
  247. FILE_PTR FileOffset; // Offset of the object file inside the archive.
  248. BOOLEAN Imported; // Specifies whether the file has already been imported.
  249. } ARCHIVE_OBJECT;
  250. // Exported Symbol in an Archive
  251. typedef struct ARCHIVE_SYMBOL {
  252. LIST_ITEM_HEADER(struct ARCHIVE_SYMBOL);
  253. ARCHIVE *Parent;
  254. const char *Name; // Symbol name (pointer into Parent->Data).
  255. ARCHIVE_OBJECT *ObjectFile; // Object file this symbol belongs to.
  256. BOOLEAN ContainsInversion; // Specifies whether the symbol contains an inversion (NOT operator) for global imports.
  257. } ARCHIVE_SYMBOL;
  258. // Global Import
  259. typedef struct GLOBAL_IMPORT {
  260. LIST_ITEM_HEADER(struct GLOBAL_IMPORT);
  261. PROGRAM *Parent;
  262. char SymbolName[MAX_SYM_LEN+1]; // Symbol name.
  263. BOOLEAN Succeeded; // Specifies whether a symbol matching this import has been found.
  264. } GLOBAL_IMPORT;
  265. #endif