data.h 15 KB

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