data.h 15 KB

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