data.h 15 KB

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