coff.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* This file is derived from the GAS 2.1.4 assembler control file.
  2. The GAS product is under the GNU General Public License, version 2 or later.
  3. As such, this file is also under that license.
  4. If the file format changes in the COFF object, this file should be
  5. subsequently updated to reflect the changes.
  6. The actual loader module only uses a few of these structures. The full
  7. set is documented here because I received the full set. If you wish
  8. more information about COFF, then O'Reilly has a very excellent book.
  9. */
  10. #define E_SYMNMLEN 8 /* Number of characters in a symbol name */
  11. #define E_FILNMLEN 14 /* Number of characters in a file name */
  12. #define E_DIMNUM 4 /* Number of array dimensions in auxiliary entry */
  13. /*
  14. * These defines are byte order independent. There is no alignment of fields
  15. * permitted in the structures. Therefore they are declared as characters
  16. * and the values loaded from the character positions. It also makes it
  17. * nice to have it "endian" independent.
  18. */
  19. /* Load a short int from the following tables with little-endian formats */
  20. #define COFF_SHORT_L(ps) ((short)(((unsigned short)((unsigned char)ps[1])<<8)|\
  21. ((unsigned short)((unsigned char)ps[0]))))
  22. /* Load a long int from the following tables with little-endian formats */
  23. #define COFF_LONG_L(ps) (((long)(((unsigned long)((unsigned char)ps[3])<<24) |\
  24. ((unsigned long)((unsigned char)ps[2])<<16) |\
  25. ((unsigned long)((unsigned char)ps[1])<<8) |\
  26. ((unsigned long)((unsigned char)ps[0])))))
  27. /* Load a short int from the following tables with big-endian formats */
  28. #define COFF_SHORT_H(ps) ((short)(((unsigned short)((unsigned char)ps[0])<<8)|\
  29. ((unsigned short)((unsigned char)ps[1]))))
  30. /* Load a long int from the following tables with big-endian formats */
  31. #define COFF_LONG_H(ps) (((long)(((unsigned long)((unsigned char)ps[0])<<24) |\
  32. ((unsigned long)((unsigned char)ps[1])<<16) |\
  33. ((unsigned long)((unsigned char)ps[2])<<8) |\
  34. ((unsigned long)((unsigned char)ps[3])))))
  35. /* These may be overridden later by brain dead implementations which generate
  36. a big-endian header with little-endian data. In that case, generate a
  37. replacement macro which tests a flag and uses either of the two above
  38. as appropriate. */
  39. #define COFF_LONG(v) COFF_LONG_L(v)
  40. #define COFF_SHORT(v) COFF_SHORT_L(v)
  41. /*** coff information for Intel 386/486. */
  42. /********************** FILE HEADER **********************/
  43. struct COFF_filehdr {
  44. char f_magic[2]; /* magic number */
  45. char f_nscns[2]; /* number of sections */
  46. char f_timdat[4]; /* time & date stamp */
  47. char f_symptr[4]; /* file pointer to symtab */
  48. char f_nsyms[4]; /* number of symtab entries */
  49. char f_opthdr[2]; /* sizeof(optional hdr) */
  50. char f_flags[2]; /* flags */
  51. };
  52. /*
  53. * Bits for f_flags:
  54. *
  55. * F_RELFLG relocation info stripped from file
  56. * F_EXEC file is executable (i.e. no unresolved external
  57. * references)
  58. * F_LNNO line numbers stripped from file
  59. * F_LSYMS local symbols stripped from file
  60. * F_MINMAL this is a minimal object file (".m") output of fextract
  61. * F_UPDATE this is a fully bound update file, output of ogen
  62. * F_SWABD this file has had its bytes swabbed (in names)
  63. * F_AR16WR this file has the byte ordering of an AR16WR
  64. * (e.g. 11/70) machine
  65. * F_AR32WR this file has the byte ordering of an AR32WR machine
  66. * (e.g. vax and iNTEL 386)
  67. * F_AR32W this file has the byte ordering of an AR32W machine
  68. * (e.g. 3b,maxi)
  69. * F_PATCH file contains "patch" list in optional header
  70. * F_NODF (minimal file only) no decision functions for
  71. * replaced functions
  72. */
  73. #define COFF_F_RELFLG 0000001
  74. #define COFF_F_EXEC 0000002
  75. #define COFF_F_LNNO 0000004
  76. #define COFF_F_LSYMS 0000010
  77. #define COFF_F_MINMAL 0000020
  78. #define COFF_F_UPDATE 0000040
  79. #define COFF_F_SWABD 0000100
  80. #define COFF_F_AR16WR 0000200
  81. #define COFF_F_AR32WR 0000400
  82. #define COFF_F_AR32W 0001000
  83. #define COFF_F_PATCH 0002000
  84. #define COFF_F_NODF 0002000
  85. #define COFF_I386MAGIC 0x14c /* Linux's system */
  86. #if 0 /* Perhaps, someday, these formats may be used. */
  87. #define COFF_I386PTXMAGIC 0x154
  88. #define COFF_I386AIXMAGIC 0x175 /* IBM's AIX system */
  89. #define COFF_I386BADMAG(x) ((COFF_SHORT((x).f_magic) != COFF_I386MAGIC) \
  90. && COFF_SHORT((x).f_magic) != COFF_I386PTXMAGIC \
  91. && COFF_SHORT((x).f_magic) != COFF_I386AIXMAGIC)
  92. #else
  93. #define COFF_I386BADMAG(x) (COFF_SHORT((x).f_magic) != COFF_I386MAGIC)
  94. #endif
  95. #define COFF_FILHDR struct COFF_filehdr
  96. #define COFF_FILHSZ sizeof(COFF_FILHDR)
  97. /********************** AOUT "OPTIONAL HEADER" **********************/
  98. /* Linux COFF must have this "optional" header. Standard COFF has no entry
  99. location for the "entry" point. They normally would start with the first
  100. location of the .text section. This is not a good idea for linux. So,
  101. the use of this "optional" header is not optional. It is required.
  102. Do not be tempted to assume that the size of the optional header is
  103. a constant and simply index the next byte by the size of this structure.
  104. Use the 'f_opthdr' field in the main coff header for the size of the
  105. structure actually written to the file!!
  106. */
  107. typedef struct
  108. {
  109. char magic[2]; /* type of file */
  110. char vstamp[2]; /* version stamp */
  111. char tsize[4]; /* text size in bytes, padded to FW bdry */
  112. char dsize[4]; /* initialized data " " */
  113. char bsize[4]; /* uninitialized data " " */
  114. char entry[4]; /* entry pt. */
  115. char text_start[4]; /* base of text used for this file */
  116. char data_start[4]; /* base of data used for this file */
  117. }
  118. COFF_AOUTHDR;
  119. #define COFF_AOUTSZ (sizeof(COFF_AOUTHDR))
  120. #define COFF_STMAGIC 0401
  121. #define COFF_OMAGIC 0404
  122. #define COFF_JMAGIC 0407 /* dirty text and data image, can't share */
  123. #define COFF_DMAGIC 0410 /* dirty text segment, data aligned */
  124. #define COFF_ZMAGIC 0413 /* The proper magic number for executables */
  125. #define COFF_SHMAGIC 0443 /* shared library header */
  126. /********************** SECTION HEADER **********************/
  127. struct COFF_scnhdr {
  128. char s_name[8]; /* section name */
  129. char s_paddr[4]; /* physical address, aliased s_nlib */
  130. char s_vaddr[4]; /* virtual address */
  131. char s_size[4]; /* section size */
  132. char s_scnptr[4]; /* file ptr to raw data for section */
  133. char s_relptr[4]; /* file ptr to relocation */
  134. char s_lnnoptr[4]; /* file ptr to line numbers */
  135. char s_nreloc[2]; /* number of relocation entries */
  136. char s_nlnno[2]; /* number of line number entries */
  137. char s_flags[4]; /* flags */
  138. };
  139. #define COFF_SCNHDR struct COFF_scnhdr
  140. #define COFF_SCNHSZ sizeof(COFF_SCNHDR)
  141. /*
  142. * names of "special" sections
  143. */
  144. #define COFF_TEXT ".text"
  145. #define COFF_DATA ".data"
  146. #define COFF_BSS ".bss"
  147. #define COFF_COMMENT ".comment"
  148. #define COFF_LIB ".lib"
  149. #define COFF_SECT_TEXT 0 /* Section for instruction code */
  150. #define COFF_SECT_DATA 1 /* Section for initialized globals */
  151. #define COFF_SECT_BSS 2 /* Section for un-initialized globals */
  152. #define COFF_SECT_REQD 3 /* Minimum number of sections for good file */
  153. #define COFF_STYP_REG 0x00 /* regular segment */
  154. #define COFF_STYP_DSECT 0x01 /* dummy segment */
  155. #define COFF_STYP_NOLOAD 0x02 /* no-load segment */
  156. #define COFF_STYP_GROUP 0x04 /* group segment */
  157. #define COFF_STYP_PAD 0x08 /* .pad segment */
  158. #define COFF_STYP_COPY 0x10 /* copy section */
  159. #define COFF_STYP_TEXT 0x20 /* .text segment */
  160. #define COFF_STYP_DATA 0x40 /* .data segment */
  161. #define COFF_STYP_BSS 0x80 /* .bss segment */
  162. #define COFF_STYP_INFO 0x200 /* .comment section */
  163. #define COFF_STYP_OVER 0x400 /* overlay section */
  164. #define COFF_STYP_LIB 0x800 /* library section */
  165. /*
  166. * Shared libraries have the following section header in the data field for
  167. * each library.
  168. */
  169. struct COFF_slib {
  170. char sl_entsz[4]; /* Size of this entry */
  171. char sl_pathndx[4]; /* size of the header field */
  172. };
  173. #define COFF_SLIBHD struct COFF_slib
  174. #define COFF_SLIBSZ sizeof(COFF_SLIBHD)
  175. /********************** LINE NUMBERS **********************/
  176. /* 1 line number entry for every "breakpointable" source line in a section.
  177. * Line numbers are grouped on a per function basis; first entry in a function
  178. * grouping will have l_lnno = 0 and in place of physical address will be the
  179. * symbol table index of the function name.
  180. */
  181. struct COFF_lineno {
  182. union {
  183. char l_symndx[4]; /* function name symbol index, iff l_lnno == 0*/
  184. char l_paddr[4]; /* (physical) address of line number */
  185. } l_addr;
  186. char l_lnno[2]; /* line number */
  187. };
  188. #define COFF_LINENO struct COFF_lineno
  189. #define COFF_LINESZ 6
  190. /********************** SYMBOLS **********************/
  191. #define COFF_E_SYMNMLEN 8 /* # characters in a short symbol name */
  192. #define COFF_E_FILNMLEN 14 /* # characters in a file name */
  193. #define COFF_E_DIMNUM 4 /* # array dimensions in auxiliary entry */
  194. /*
  195. * All symbols and sections have the following definition
  196. */
  197. struct COFF_syment
  198. {
  199. union {
  200. char e_name[E_SYMNMLEN]; /* Symbol name (first 8 characters) */
  201. struct {
  202. char e_zeroes[4]; /* Leading zeros */
  203. char e_offset[4]; /* Offset if this is a header section */
  204. } e;
  205. } e;
  206. char e_value[4]; /* Value (address) of the segment */
  207. char e_scnum[2]; /* Section number */
  208. char e_type[2]; /* Type of section */
  209. char e_sclass[1]; /* Loader class */
  210. char e_numaux[1]; /* Number of auxiliary entries which follow */
  211. };
  212. #define COFF_N_BTMASK (0xf) /* Mask for important class bits */
  213. #define COFF_N_TMASK (0x30) /* Mask for important type bits */
  214. #define COFF_N_BTSHFT (4) /* # bits to shift class field */
  215. #define COFF_N_TSHIFT (2) /* # bits to shift type field */
  216. /*
  217. * Auxiliary entries because the main table is too limiting.
  218. */
  219. union COFF_auxent {
  220. /*
  221. * Debugger information
  222. */
  223. struct {
  224. char x_tagndx[4]; /* str, un, or enum tag indx */
  225. union {
  226. struct {
  227. char x_lnno[2]; /* declaration line number */
  228. char x_size[2]; /* str/union/array size */
  229. } x_lnsz;
  230. char x_fsize[4]; /* size of function */
  231. } x_misc;
  232. union {
  233. struct { /* if ISFCN, tag, or .bb */
  234. char x_lnnoptr[4]; /* ptr to fcn line # */
  235. char x_endndx[4]; /* entry ndx past block end */
  236. } x_fcn;
  237. struct { /* if ISARY, up to 4 dimen. */
  238. char x_dimen[E_DIMNUM][2];
  239. } x_ary;
  240. } x_fcnary;
  241. char x_tvndx[2]; /* tv index */
  242. } x_sym;
  243. /*
  244. * Source file names (debugger information)
  245. */
  246. union {
  247. char x_fname[E_FILNMLEN];
  248. struct {
  249. char x_zeroes[4];
  250. char x_offset[4];
  251. } x_n;
  252. } x_file;
  253. /*
  254. * Section information
  255. */
  256. struct {
  257. char x_scnlen[4]; /* section length */
  258. char x_nreloc[2]; /* # relocation entries */
  259. char x_nlinno[2]; /* # line numbers */
  260. } x_scn;
  261. /*
  262. * Transfer vector (branch table)
  263. */
  264. struct {
  265. char x_tvfill[4]; /* tv fill value */
  266. char x_tvlen[2]; /* length of .tv */
  267. char x_tvran[2][2]; /* tv range */
  268. } x_tv; /* info about .tv section (in auxent of symbol .tv)) */
  269. };
  270. #define COFF_SYMENT struct COFF_syment
  271. #define COFF_SYMESZ 18
  272. #define COFF_AUXENT union COFF_auxent
  273. #define COFF_AUXESZ 18
  274. #define COFF_ETEXT "etext"
  275. /********************** RELOCATION DIRECTIVES **********************/
  276. struct COFF_reloc {
  277. char r_vaddr[4]; /* Virtual address of item */
  278. char r_symndx[4]; /* Symbol index in the symtab */
  279. char r_type[2]; /* Relocation type */
  280. };
  281. #define COFF_RELOC struct COFF_reloc
  282. #define COFF_RELSZ 10
  283. #define COFF_DEF_DATA_SECTION_ALIGNMENT 4
  284. #define COFF_DEF_BSS_SECTION_ALIGNMENT 4
  285. #define COFF_DEF_TEXT_SECTION_ALIGNMENT 4
  286. /* For new sections we haven't heard of before */
  287. #define COFF_DEF_SECTION_ALIGNMENT 4