data.h 15 KB

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