coff.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* coff.h: Definitions for COFF object files
  2. Copyright (C) 2002-2003 Sebastian Reichelt
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #ifndef COFF_H
  15. #define COFF_H
  16. #include "../generic.h"
  17. #include "../integers.h"
  18. // *** File Mapping Definitions ***
  19. // File Header
  20. typedef struct ATTRIBUTE_PACKED {
  21. TI2 Machine;
  22. TI2 SectionCount;
  23. TI4 TimeStamp;
  24. TI4 PSymbols;
  25. TI4 SymbolCount;
  26. TI2 OptHdrSize;
  27. TI2 Flags;
  28. } COFF_HEADER;
  29. #define COFF_SECTION_NAME_LEN 8
  30. // Section Header
  31. typedef struct ATTRIBUTE_PACKED {
  32. char Name[COFF_SECTION_NAME_LEN];
  33. TI4 PhysicalAddress;
  34. TI4 VirtualAddress;
  35. TI4 Size;
  36. TI4 PData;
  37. TI4 PRelocs;
  38. TI4 PLines;
  39. TI2 RelocCount;
  40. TI2 LineCount;
  41. TI4 Flags;
  42. } COFF_SECTION;
  43. typedef COFF_SECTION ATTRIBUTE_PACKED COFF_SECTIONS[];
  44. // Section Flags
  45. #define COFF_SECTION_TEXT 0x20
  46. #define COFF_SECTION_DATA 0x40
  47. #define COFF_SECTION_BSS 0x80
  48. #ifdef COFF_TIGCC_EXTENSIONS
  49. #define COFF_SECTION_MERGEABLE 0x1000000
  50. #define COFF_SECTION_UNALIGNED 0x2000000
  51. #endif
  52. // Symbol Name
  53. // (either directly or in string table)
  54. typedef union ATTRIBUTE_PACKED {
  55. struct ATTRIBUTE_PACKED {
  56. ZI4 Zero; // Check this with IsZero.
  57. TI4 StringOffset; // If Zero is really zero, this is an offset into the string table.
  58. } StringRef;
  59. char Name[8]; // Otherwise, this is the name of the symbol.
  60. } SYM_NAME;
  61. // Symbol Entry
  62. typedef struct ATTRIBUTE_PACKED {
  63. SYM_NAME Name;
  64. TI4 Value;
  65. TI2 Section;
  66. TI2 Type;
  67. TI1 Class;
  68. TI1 AuxSymbolCount;
  69. } COFF_SYMBOL;
  70. typedef COFF_SYMBOL ATTRIBUTE_PACKED COFF_SYMBOLS[];
  71. #define COFF_SYMBOL_EXTERNAL 0x02
  72. // Reloc Entry
  73. typedef struct ATTRIBUTE_PACKED {
  74. TI4 Location;
  75. TI4 Symbol;
  76. TI2 Type;
  77. } COFF_RELOC;
  78. typedef COFF_RELOC COFF_RELOCS[] ATTRIBUTE_PACKED;
  79. #define COFF_RELOC_DIR2 0x01
  80. #define COFF_RELOC_DIR4 0x06
  81. #define COFF_RELOC_ABS1 0x0F
  82. #define COFF_RELOC_ABS2 0x10
  83. #define COFF_RELOC_ABS4 0x11
  84. #define COFF_RELOC_REL1 0x12
  85. #define COFF_RELOC_REL2 0x13
  86. #define COFF_RELOC_REL4 0x14
  87. #define COFF_RELOC_ABS4_NEG 0x45
  88. #ifdef COFF_TIGCC_EXTENSIONS
  89. #define COFF_RELOC_ABS2_NEG 0x7161
  90. #define COFF_RELOC_ABS1_NEG 0x7162
  91. #define COFF_RELOC_UNOPTIMIZABLE 0x8000
  92. #endif
  93. // Line Number Entry
  94. typedef struct ATTRIBUTE_PACKED {
  95. TI4 LineAddress;
  96. TI2 LineNumber;
  97. } COFF_LINE_NUM;
  98. typedef COFF_LINE_NUM ATTRIBUTE_PACKED COFF_LINE_NUMS[];
  99. // *** Helping Definitions ***
  100. typedef struct {
  101. FILE_PTR PSections;
  102. COUNT SectionCount;
  103. FILE_PTR PSymbols;
  104. COUNT SymbolCount;
  105. FILE_PTR PStrings;
  106. } COFF_INFO;
  107. #define CreateCoffInfo(Header,Info) \
  108. ({ \
  109. (Info).PSections = sizeof (Header) + ReadTI2 ((Header).OptHdrSize); \
  110. (Info).SectionCount = ReadTI2 ((Header).SectionCount); \
  111. (Info).PSymbols = ReadTI4 ((Header).PSymbols); \
  112. (Info).SymbolCount = ReadTI4 ((Header).SymbolCount); \
  113. (Info).PStrings = (Info).PSymbols + (Info).SymbolCount * sizeof (COFF_SYMBOL); \
  114. })
  115. // *** File Type Check ***
  116. // Check whether a file has the COFF format.
  117. #define IsCOFFFile(File,FileSize) (((FileSize) >= ((SIZE) (sizeof (COFF_HEADER)))) && ((File) [0] == 0x01) && ((File) [1] == 0x50))
  118. #endif