Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /**************************************************************************/
  2. /*  COFF.H                                                                */
  3. /*     COFF data structures and related definitions used by the linker    */
  4. /**************************************************************************/
  5.  
  6. /*------------------------------------------------------------------------*/
  7. /*  COFF FILE HEADER                                                      */
  8. /*------------------------------------------------------------------------*/
  9. struct filehdr {
  10.         unsigned short  f_magic;        /* magic number */
  11.         unsigned short  f_nscns;        /* number of sections */
  12.         long            f_timdat;       /* time & date stamp */
  13.         long            f_symptr;       /* file pointer to symtab */
  14.         long            f_nsyms;        /* number of symtab entries */
  15.         unsigned short  f_opthdr;       /* sizeof(optional hdr) */
  16.         unsigned short  f_flags;        /* flags */
  17.         unsigned short  f_TargetID;     /* for C6x = 0x0099 */
  18.         };
  19.  
  20. /*------------------------------------------------------------------------*/
  21. /*  File header flags                                                     */
  22. /*------------------------------------------------------------------------*/
  23. #define  F_RELFLG   0x01       /* relocation info stripped from file       */
  24. #define  F_EXEC     0x02       /* file is executable (no unresolved refs)  */
  25. #define  F_LNNO     0x04       /* line nunbers stripped from file          */
  26. #define  F_LSYMS    0x08       /* local symbols stripped from file         */
  27. #define  F_GSP10    0x10       /* 34010 version                            */
  28. #define  F_GSP20    0x20       /* 34020 version                            */
  29. #define  F_SWABD    0x40       /* bytes swabbed (in names)                 */
  30. #define  F_AR16WR   0x80       /* byte ordering of an AR16WR (PDP-11)      */
  31. #define  F_LITTLE   0x100      /* byte ordering of an AR32WR (vax)         */
  32. #define  F_BIG      0x200      /* byte ordering of an AR32W (3B, maxi)     */
  33. #define  F_PATCH    0x400      /* contains "patch" list in optional header */
  34. #define  F_NODF     0x400  
  35.  
  36. #define F_VERSION    (F_GSP10  | F_GSP20)  
  37. #define F_BYTE_ORDER (F_LITTLE | F_BIG)
  38. #define FILHDR  struct filehdr
  39.  
  40. //#define FILHSZ  sizeof(FILHDR)
  41. #define FILHSZ  22                // above rounds to align on 4 bytes which causes problems
  42.  
  43. #define COFF_C67_MAGIC 0x00c2
  44.  
  45. /*------------------------------------------------------------------------*/
  46. /*  Macros to recognize magic numbers                                     */
  47. /*------------------------------------------------------------------------*/
  48. #define ISMAGIC(x)      (((unsigned short)(x))==(unsigned short)magic)
  49. #define ISARCHIVE(x)    ((((unsigned short)(x))==(unsigned short)ARTYPE))
  50. #define BADMAGIC(x)     (((unsigned short)(x) & 0x8080) && !ISMAGIC(x))
  51.  
  52. /*------------------------------------------------------------------------*/
  53. /*  OPTIONAL FILE HEADER                                                  */
  54. /*------------------------------------------------------------------------*/
  55. typedef struct aouthdr {
  56.         short   magic;          /* see magic.h                          */
  57.         short   vstamp;         /* version stamp                        */
  58.         long    tsize;          /* text size in bytes, padded to FW bdry*/
  59.         long    dsize;          /* initialized data "  "                */
  60.         long    bsize;          /* uninitialized data "   "             */
  61.         long    entrypt;        /* entry pt.                            */
  62.         long    text_start;     /* base of text used for this file      */
  63.         long    data_start;     /* base of data used for this file      */
  64. } AOUTHDR;
  65.  
  66. #define AOUTSZ  sizeof(AOUTHDR)
  67.  
  68. /*----------------------------------------------------------------------*/
  69. /*      When a UNIX aout header is to be built in the optional header,  */
  70. /*      the following magic numbers can appear in that header:          */
  71. /*                                                                      */
  72. /*              AOUT1MAGIC : default : readonly sharable text segment   */
  73. /*              AOUT2MAGIC:          : writable text segment            */
  74. /*              PAGEMAGIC  :         : configured for paging            */
  75. /*----------------------------------------------------------------------*/
  76. #define AOUT1MAGIC 0410
  77. #define AOUT2MAGIC 0407
  78. #define PAGEMAGIC  0413
  79.  
  80. /*------------------------------------------------------------------------*/
  81. /*  COMMON ARCHIVE FILE STRUCTURES                                        */
  82. /*                                                                        */
  83. /*       ARCHIVE File Organization:                                       */
  84. /*       _______________________________________________                  */
  85. /*       |__________ARCHIVE_MAGIC_STRING_______________|                  */
  86. /*       |__________ARCHIVE_FILE_MEMBER_1______________|                  */
  87. /*       |                                             |                  */
  88. /*       |       Archive File Header "ar_hdr"          |                  */
  89. /*       |.............................................|                  */
  90. /*       |       Member Contents                       |                  */
  91. /*       |               1. External symbol directory  |                  */
  92. /*       |               2. Text file                  |                  */
  93. /*       |_____________________________________________|                  */
  94. /*       |________ARCHIVE_FILE_MEMBER_2________________|                  */
  95. /*       |               "ar_hdr"                      |                  */
  96. /*       |.............................................|                  */
  97. /*       |       Member Contents (.o or text file)     |                  */
  98. /*       |_____________________________________________|                  */
  99. /*       |       .               .               .     |                  */
  100. /*       |       .               .               .     |                  */
  101. /*       |       .               .               .     |                  */
  102. /*       |_____________________________________________|                  */
  103. /*       |________ARCHIVE_FILE_MEMBER_n________________|                  */
  104. /*       |               "ar_hdr"                      |                  */
  105. /*       |.............................................|                  */
  106. /*       |               Member Contents               |                  */
  107. /*       |_____________________________________________|                  */
  108. /*                                                                        */
  109. /*------------------------------------------------------------------------*/
  110.  
  111. #define COFF_ARMAG   "!<arch>\n"
  112. #define SARMAG  8
  113. #define ARFMAG  "`\n"
  114.  
  115. struct ar_hdr           /* archive file member header - printable ascii */
  116. {
  117.         char    ar_name[16];    /* file member name - `/' terminated */
  118.         char    ar_date[12];    /* file member date - decimal */
  119.         char    ar_uid[6];      /* file member user id - decimal */
  120.         char    ar_gid[6];      /* file member group id - decimal */
  121.         char    ar_mode[8];     /* file member mode - octal */
  122.         char    ar_size[10];    /* file member size - decimal */
  123.         char    ar_fmag[2];     /* ARFMAG - string to end header */
  124. };
  125.  
  126. /*------------------------------------------------------------------------*/
  127. /*  SECTION HEADER                                                        */
  128. /*------------------------------------------------------------------------*/
  129. struct scnhdr {
  130.         char            s_name[8];      /* section name */
  131.         long            s_paddr;        /* physical address */
  132.         long            s_vaddr;        /* virtual address */
  133.         long            s_size;         /* section size */
  134.         long            s_scnptr;       /* file ptr to raw data for section */
  135.         long            s_relptr;       /* file ptr to relocation */
  136.         long            s_lnnoptr;      /* file ptr to line numbers */
  137.         unsigned int    s_nreloc;       /* number of relocation entries */
  138.         unsigned int    s_nlnno;        /* number of line number entries */
  139.         unsigned int    s_flags;        /* flags */
  140.                 unsigned short  s_reserved;     /* reserved byte */
  141.                 unsigned short  s_page;         /* memory page id */
  142.         };
  143.  
  144. #define SCNHDR  struct scnhdr
  145. #define SCNHSZ  sizeof(SCNHDR)
  146.  
  147. /*------------------------------------------------------------------------*/
  148. /* Define constants for names of "special" sections                       */
  149. /*------------------------------------------------------------------------*/
  150. //#define _TEXT    ".text"
  151. #define _DATA    ".data"
  152. #define _BSS     ".bss"
  153. #define _CINIT   ".cinit"
  154. #define _TV      ".tv"
  155.  
  156. /*------------------------------------------------------------------------*/
  157. /* The low 4 bits of s_flags is used as a section "type"                  */
  158. /*------------------------------------------------------------------------*/
  159. #define STYP_REG    0x00  /* "regular" : allocated, relocated, loaded */
  160. #define STYP_DSECT  0x01  /* "dummy"   : not allocated, relocated, not loaded */
  161. #define STYP_NOLOAD 0x02  /* "noload"  : allocated, relocated, not loaded */
  162. #define STYP_GROUP  0x04  /* "grouped" : formed of input sections */
  163. #define STYP_PAD    0x08  /* "padding" : not allocated, not relocated, loaded */
  164. #define STYP_COPY   0x10  /* "copy"    : used for C init tables -
  165.                                                 not allocated, relocated,
  166.                                                 loaded;  reloc & lineno
  167.                                                 entries processed normally */
  168. #define STYP_TEXT   0x20   /* section contains text only */
  169. #define STYP_DATA   0x40   /* section contains data only */
  170. #define STYP_BSS    0x80   /* section contains bss only */
  171.  
  172. #define STYP_ALIGN  0x100  /* align flag passed by old version assemblers */
  173. #define ALIGN_MASK  0x0F00 /* part of s_flags that is used for align vals */
  174. #define ALIGNSIZE(x) (1 << ((x & ALIGN_MASK) >> 8))
  175.  
  176. /*------------------------------------------------------------------------*/
  177. /*  RELOCATION ENTRIES                                                    */
  178. /*------------------------------------------------------------------------*/
  179. struct reloc
  180. {
  181.    long            r_vaddr;        /* (virtual) address of reference */
  182.    short           r_symndx;       /* index into symbol table */
  183.    unsigned short  r_disp;         /* additional bits for address calculation */
  184.    unsigned short  r_type;         /* relocation type */
  185. };
  186.  
  187. #define RELOC   struct reloc
  188. #define RELSZ   10                 /* sizeof(RELOC) */
  189.  
  190. /*--------------------------------------------------------------------------*/
  191. /*   define all relocation types                                            */
  192. /*--------------------------------------------------------------------------*/
  193.  
  194. #define R_ABS           0         /* absolute address - no relocation       */
  195. #define R_DIR16         01        /* UNUSED                                 */
  196. #define R_REL16         02        /* UNUSED                                 */
  197. #define R_DIR24         04        /* UNUSED                                 */
  198. #define R_REL24         05        /* 24 bits, direct                        */
  199. #define R_DIR32         06        /* UNUSED                                 */
  200. #define R_RELBYTE      017        /* 8 bits, direct                         */
  201. #define R_RELWORD      020        /* 16 bits, direct                        */
  202. #define R_RELLONG      021        /* 32 bits, direct                        */
  203. #define R_PCRBYTE      022        /* 8 bits, PC-relative                    */
  204. #define R_PCRWORD      023        /* 16 bits, PC-relative                   */
  205. #define R_PCRLONG      024        /* 32 bits, PC-relative                   */
  206. #define R_OCRLONG      030        /* GSP: 32 bits, one's complement direct  */
  207. #define R_GSPPCR16     031        /* GSP: 16 bits, PC relative (in words)   */
  208. #define R_GSPOPR32     032        /* GSP: 32 bits, direct big-endian        */
  209. #define R_PARTLS16     040        /* Brahma: 16 bit offset of 24 bit address*/
  210. #define R_PARTMS8      041        /* Brahma: 8 bit page of 24 bit address   */
  211. #define R_PARTLS7      050        /* DSP: 7 bit offset of 16 bit address    */
  212. #define R_PARTMS9      051        /* DSP: 9 bit page of 16 bit address      */
  213. #define R_REL13        052        /* DSP: 13 bits, direct                   */
  214.  
  215. /*------------------------------------------------------------------------*/
  216. /*  LINE NUMBER ENTRIES                                                   */
  217. /*------------------------------------------------------------------------*/
  218. struct lineno
  219. {
  220.         union
  221.         {
  222.                 long    l_symndx ;      /* sym. table index of function name
  223.                                                 iff l_lnno == 0      */
  224.                 long    l_paddr ;       /* (physical) address of line number */
  225.         }               l_addr ;
  226.         unsigned short  l_lnno ;        /* line number */
  227. };
  228.  
  229. #define LINENO  struct lineno
  230. #define LINESZ  6       /* sizeof(LINENO) */
  231.  
  232. /*------------------------------------------------------------------------*/
  233. /*   STORAGE CLASSES                                                      */
  234. /*------------------------------------------------------------------------*/
  235. #define  C_EFCN          -1    /* physical end of function */
  236. #define  C_NULL          0
  237. #define  C_AUTO          1     /* automatic variable */
  238. #define  C_EXT           2     /* external symbol */
  239. #define  C_STAT          3     /* static */
  240. #define  C_REG           4     /* register variable */
  241. #define  C_EXTDEF        5     /* external definition */
  242. #define  C_LABEL         6     /* label */
  243. #define  C_ULABEL        7     /* undefined label */
  244. #define  C_MOS           8     /* member of structure */
  245. #define  C_ARG           9     /* function argument */
  246. #define  C_STRTAG        10    /* structure tag */
  247. #define  C_MOU           11    /* member of union */
  248. #define  C_UNTAG         12    /* union tag */
  249. #define  C_TPDEF         13    /* type definition */
  250. #define C_USTATIC        14    /* undefined static */
  251. #define  C_ENTAG         15    /* enumeration tag */
  252. #define  C_MOE           16    /* member of enumeration */
  253. #define  C_REGPARM       17    /* register parameter */
  254. #define  C_FIELD         18    /* bit field */
  255.  
  256. #define  C_BLOCK         100   /* ".bb" or ".eb" */
  257. #define  C_FCN           101   /* ".bf" or ".ef" */
  258. #define  C_EOS           102   /* end of structure */
  259. #define  C_FILE          103   /* file name */
  260. #define  C_LINE          104   /* dummy sclass for line number entry */
  261. #define  C_ALIAS         105   /* duplicate tag */
  262. #define  C_HIDDEN        106   /* special storage class for external */
  263.                                /* symbols in dmert public libraries  */
  264. /*------------------------------------------------------------------------*/
  265. /*  SYMBOL TABLE ENTRIES                                                  */
  266. /*------------------------------------------------------------------------*/
  267.  
  268. #define  SYMNMLEN   8      /*  Number of characters in a symbol name */
  269. #define  FILNMLEN   14     /*  Number of characters in a file name */
  270. #define  DIMNUM     4      /*  Number of array dimensions in auxiliary entry */
  271.  
  272.  
  273. struct syment
  274. {
  275.         union
  276.         {
  277.                 char            _n_name[SYMNMLEN];      /* old COFF version */
  278.                 struct
  279.                 {
  280.                         long    _n_zeroes;      /* new == 0 */
  281.                         long    _n_offset;      /* offset into string table */
  282.                 } _n_n;
  283.                 char            *_n_nptr[2];    /* allows for overlaying */
  284.         } _n;
  285.         long                    n_value;        /* value of symbol */
  286.         short                   n_scnum;        /* section number */
  287.         unsigned short          n_type;         /* type and derived type */
  288.         char                    n_sclass;       /* storage class */
  289.         char                    n_numaux;       /* number of aux. entries */
  290. };
  291.  
  292. #define n_name          _n._n_name
  293. #define n_nptr          _n._n_nptr[1]
  294. #define n_zeroes        _n._n_n._n_zeroes
  295. #define n_offset        _n._n_n._n_offset
  296.  
  297. /*------------------------------------------------------------------------*/
  298. /* Relocatable symbols have a section number of the                       */
  299. /* section in which they are defined.  Otherwise, section                 */
  300. /* numbers have the following meanings:                                   */
  301. /*------------------------------------------------------------------------*/
  302. #define  N_UNDEF  0                     /* undefined symbol */
  303. #define  N_ABS    -1                    /* value of symbol is absolute */
  304. #define  N_DEBUG  -2                    /* special debugging symbol  */
  305. #define  N_TV     (unsigned short)-3    /* needs transfer vector (preload) */
  306. #define  P_TV     (unsigned short)-4    /* needs transfer vector (postload) */
  307.  
  308. /*------------------------------------------------------------------------*/
  309. /* The fundamental type of a symbol packed into the low                   */
  310. /* 4 bits of the word.                                                    */
  311. /*------------------------------------------------------------------------*/
  312. #define  _EF    ".ef"
  313.  
  314. #define  T_NULL     0          /* no type info */
  315. #define  T_ARG      1          /* function argument (only used by compiler) */
  316. #define  T_CHAR     2          /* character */
  317. #define  T_SHORT    3          /* short integer */
  318. #define  T_INT      4          /* integer */
  319. #define  T_LONG     5          /* long integer */
  320. #define  T_FLOAT    6          /* floating point */
  321. #define  T_DOUBLE   7          /* double word */
  322. #define  T_STRUCT   8          /* structure  */
  323. #define  T_UNION    9          /* union  */
  324. #define  T_ENUM     10         /* enumeration  */
  325. #define  T_MOE      11         /* member of enumeration */
  326. #define  T_UCHAR    12         /* unsigned character */
  327. #define  T_USHORT   13         /* unsigned short */
  328. #define  T_UINT     14         /* unsigned integer */
  329. #define  T_ULONG    15         /* unsigned long */
  330.  
  331. /*------------------------------------------------------------------------*/
  332. /* derived types are:                                                     */
  333. /*------------------------------------------------------------------------*/
  334. #define  DT_NON      0          /* no derived type */
  335. #define  DT_PTR      1          /* pointer */
  336. #define  DT_FCN      2          /* function */
  337. #define  DT_ARY      3          /* array */
  338.  
  339. #define MKTYPE(basic, d1,d2,d3,d4,d5,d6) \
  340.        ((basic) | ((d1) <<  4) | ((d2) <<  6) | ((d3) <<  8) |\
  341.                   ((d4) << 10) | ((d5) << 12) | ((d6) << 14))
  342.  
  343. /*------------------------------------------------------------------------*/
  344. /* type packing constants and macros                                      */
  345. /*------------------------------------------------------------------------*/
  346. #define  N_BTMASK_COFF     017
  347. #define  N_TMASK_COFF      060
  348. #define  N_TMASK1_COFF     0300
  349. #define  N_TMASK2_COFF     0360
  350. #define  N_BTSHFT_COFF     4
  351. #define  N_TSHIFT_COFF     2
  352.  
  353. #define  BTYPE_COFF(x)  ((x) & N_BTMASK_COFF)  
  354. #define  ISINT(x)  (((x) >= T_CHAR && (x) <= T_LONG) ||   \
  355.                     ((x) >= T_UCHAR && (x) <= T_ULONG) || (x) == T_ENUM)
  356. #define  ISFLT_COFF(x)  ((x) == T_DOUBLE || (x) == T_FLOAT)
  357. #define  ISPTR_COFF(x)  (((x) & N_TMASK_COFF) == (DT_PTR << N_BTSHFT_COFF))
  358. #define  ISFCN_COFF(x)  (((x) & N_TMASK_COFF) == (DT_FCN << N_BTSHFT_COFF))
  359. #define  ISARY_COFF(x)  (((x) & N_TMASK_COFF) == (DT_ARY << N_BTSHFT_COFF))
  360. #define  ISTAG_COFF(x)  ((x)==C_STRTAG || (x)==C_UNTAG || (x)==C_ENTAG)
  361.  
  362. #define  INCREF_COFF(x) ((((x)&~N_BTMASK_COFF)<<N_TSHIFT_COFF)|(DT_PTR<<N_BTSHFT_COFF)|(x&N_BTMASK_COFF))
  363. #define  DECREF_COFF(x) ((((x)>>N_TSHIFT_COFF)&~N_BTMASK_COFF)|((x)&N_BTMASK_COFF))
  364.  
  365. /*------------------------------------------------------------------------*/
  366. /*  AUXILIARY SYMBOL ENTRY                                                */
  367. /*------------------------------------------------------------------------*/
  368. union auxent
  369. {
  370.         struct
  371.         {
  372.                 long            x_tagndx;       /* str, un, or enum tag indx */
  373.                 union
  374.                 {
  375.                         struct
  376.                         {
  377.                                 unsigned short  x_lnno; /* declaration line number */
  378.                                 unsigned short  x_size; /* str, union, array size */
  379.                         } x_lnsz;
  380.                         long    x_fsize;        /* size of function */
  381.                 } x_misc;
  382.                 union
  383.                 {
  384.                         struct                  /* if ISFCN, tag, or .bb */
  385.                         {
  386.                                 long    x_lnnoptr;      /* ptr to fcn line # */
  387.                                 long    x_endndx;       /* entry ndx past block end */
  388.                         }       x_fcn;
  389.                         struct                  /* if ISARY, up to 4 dimen. */
  390.                         {
  391.                                 unsigned short  x_dimen[DIMNUM];
  392.                         }       x_ary;
  393.                 }               x_fcnary;
  394.                 unsigned short  x_regcount;   /* number of registers used by func */
  395.         }       x_sym;
  396.         struct
  397.         {
  398.                 char    x_fname[FILNMLEN];
  399.         }       x_file;
  400.         struct
  401.         {
  402.                 long    x_scnlen;          /* section length */
  403.                 unsigned short  x_nreloc;  /* number of relocation entries */
  404.                 unsigned short  x_nlinno;  /* number of line numbers */
  405.         }       x_scn;
  406. };
  407.  
  408. #define SYMENT  struct syment
  409. #define SYMESZ  18      /* sizeof(SYMENT) */
  410.  
  411. #define AUXENT  union auxent
  412. #define AUXESZ  18      /* sizeof(AUXENT) */
  413.  
  414. /*------------------------------------------------------------------------*/
  415. /*  NAMES OF "SPECIAL" SYMBOLS                                            */
  416. /*------------------------------------------------------------------------*/
  417. #define _STEXT          ".text"
  418. #define _ETEXT          "etext"
  419. #define _SDATA          ".data"
  420. #define _EDATA          "edata"
  421. #define _SBSS           ".bss"
  422. #define _END            "end"
  423. #define _CINITPTR       "cinit"
  424.  
  425. /*--------------------------------------------------------------------------*/
  426. /*  ENTRY POINT SYMBOLS                                                     */
  427. /*--------------------------------------------------------------------------*/
  428. #define _START          "_start"
  429. #define _MAIN           "_main"
  430.     /*  _CSTART         "_c_int00"          (defined in params.h)  */
  431.  
  432.  
  433. #define _TVORIG         "_tvorig"
  434. #define _TORIGIN        "_torigin"
  435. #define _DORIGIN        "_dorigin"
  436.  
  437. #define _SORIGIN        "_sorigin"
  438.