loadPecoffLib.h
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:13k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* loadPecoffLib.h - Pecoff object module header */
  2. /* Copyright 1998-2001 Wind River Systems, Inc. */
  3. /*
  4. modification history
  5. --------------------
  6. 01b,23oct01,pad  Added PE section flag extensions. Tidied up a bit.
  7. 01a,25mar98,cym  written based on loadCoffLib.h.
  8. */
  9. #ifndef __INCloadPecoffLibh
  10. #define __INCloadPecoffLibh
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #ifdef CPU
  15. #include "symLib.h"
  16. #include "moduleLib.h"
  17. #endif
  18. /******************************************************************************
  19.  *
  20.  * This file describes a PECOFF object file.
  21.  *
  22.  ******************************************************************************/
  23. /******************************* DOS EXE HEADER ****************************/
  24. struct dosheader {
  25.     unsigned short e_magic;
  26.     unsigned short e_cblp;
  27.     unsigned short e_cp;
  28.     unsigned short e_crlc;
  29.     unsigned short e_cparhdr;
  30.     unsigned short e_minalloc;
  31.     unsigned short e_maxalloc;
  32.     unsigned short e_ss;
  33.     unsigned short e_sp;
  34.     unsigned short e_csum;
  35.     unsigned short e_ip;
  36.     unsigned short e_cs;
  37.     unsigned short e_lfarlc;
  38.     unsigned short e_ovno;
  39.     unsigned short e_res[4];
  40.     unsigned short e_oemid;
  41.     unsigned short e_oeminfo;
  42.     unsigned short e_res2[10];
  43.     unsigned long  e_lfanew; /* Offset to the COFF image header */
  44.     };
  45. /******************************* FILE HEADER *******************************/
  46. struct filehdr {
  47. unsigned short f_magic; /* magic number */
  48. unsigned short f_nscns; /* number of sections */
  49. long f_timdat; /* time & date stamp */
  50. long f_symptr; /* file pointer to symtab */
  51. long f_nsyms; /* number of symtab entries */
  52. unsigned short f_opthdr; /* sizeof(optional hdr) */
  53. unsigned short f_flags; /* flags */
  54. };
  55. /* Bits for f_flags:
  56.  * F_RELFLG relocation info stripped from file
  57.  * F_EXEC file is executable (no unresolved external references)
  58.  * F_LNNO line numbers stripped from file
  59.  * F_LSYMS local symbols stripped from file
  60.  * F_AR32WR file has byte ordering of an AR32WR machine (e.g. vax)
  61.  * F_PIC file contains position-independent code
  62.  * F_PID file contains position-independent data
  63.  * F_LINKPID file is suitable for linking w/pos-indep code or data
  64.  */
  65. #define F_RELFLG 0x0001
  66. #define F_EXEC 0x0002
  67. #define F_LNNO 0x0004
  68. #define F_LSYMS 0x0008
  69. #define F_AR32WR 0x0010
  70. #define F_PIC 0x0040
  71. #define F_PID 0x0080
  72. #define F_LINKPID 0x0100
  73. #define F_CCINFO 0x0800
  74. /* Additional magic values from the PE format */
  75. #define IMAGE_NT_SIGNATURE 0x00004550
  76. #define IMAGE_DOS_SIGNATURE 0x5A4D
  77. #define IMAGE_FILE_MACHINE_I386 0x14c
  78. #define FILHDR struct filehdr
  79. #define FILHSZ sizeof(FILHDR)
  80. #undef  N_TXTOFF                /* undefined for a.out overlap */
  81. #define N_TXTOFF(f) 
  82.     ((f).f_opthdr == 0) ? 
  83.     (FILHSZ + (f).f_nscns * SCNHSZ ) : 
  84.     (FILHSZ + AOUTSZ + (f).f_nscns * SCNHSZ )
  85. /************************* AOUT "OPTIONAL HEADER" *************************/
  86. #define OMAGIC 0407
  87. #define NMAGIC 0410
  88. typedef struct aouthdr {
  89. short magic; /* type of file */
  90. short vstamp; /* version stamp */
  91. unsigned long tsize; /* text size in bytes, padded to FW bdry*/
  92. unsigned long dsize; /* initialized data "  " */
  93. unsigned long bsize; /* uninitialized data "   " */
  94. unsigned long entry; /* entry pt. */
  95. unsigned long text_start; /* base of text used for this file */
  96. unsigned long data_start; /* base of data used for this file */
  97. unsigned long   image_base; /* offset for the image (add above) */
  98. unsigned long   section_align;  /* ?? */
  99. unsigned long   file_align;     /* ?? */
  100. } PEOPTION;
  101. #define PEOPTSZ (sizeof(PEOPTION))
  102. /****************************** SECTION HEADER ******************************/
  103. struct scnhdr {
  104. char s_name[8]; /* section name */
  105. long s_paddr; /* physical address, aliased s_nlib */
  106. long s_vaddr; /* virtual address */
  107. long s_size; /* section size */
  108. long s_scnptr; /* file ptr to raw data for section */
  109. long s_relptr; /* file ptr to relocation */
  110. long s_lnnoptr; /* file ptr to line numbers */
  111. unsigned short s_nreloc; /* number of relocation entries */
  112. unsigned short s_nlnno; /* number of line number entries*/
  113. long s_flags; /* flags */
  114. };
  115. /*
  116.  * names of "special" sections
  117.  */
  118. #define _TEXT ".text"
  119. #define _DATA ".data"
  120. #define _BSS ".bss"
  121. /* s_flags "type" */
  122. /* TYPE    ALLOCATED? RELOCATED? LOADED? */
  123. /* ----    ---------- ---------- ------- */
  124. #define STYP_REG 0x0000 /* regular    yes        yes       yes   */
  125. #define STYP_DSECT 0x0001 /* dummy      no         yes       no    */
  126. #define STYP_NOLOAD 0x0002 /* noload     yes        yes       no    */
  127. #define STYP_GROUP 0x0004 /* grouped  <formed from input sections> */
  128. #define STYP_PAD 0x0008 /* padding    no         no        yes   */
  129. #define STYP_COPY 0x0010 /* copy       no         no        yes   */
  130. #define STYP_INFO 0x0200 /* comment    no         no        no    */
  131. #define STYP_TEXT 0x0020 /* section contains text */
  132. #define STYP_DATA 0x0040 /* section contains data */
  133. #define STYP_BSS 0x0080 /* section contains bss  */
  134. #define STYP_DRECTVE    0x0a00  /* section contains drectve */
  135. #define STYP_LIT 0x8000 /* section contains literal */
  136. #define STYP_DISCARDABLE 0x01000000 /* Section can be discarded ?? */
  137. /*
  138.  * Additional section types specific to the PE format. These are the types
  139.  * actually used by the GNU toolchain along with the STYP_{TEXT|DATA|BSS}
  140.  * types. Their definitions come from gnu/include/coff/pe.h
  141.  */
  142. #define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 /* section can be discarded */
  143. #define IMAGE_SCN_MEM_EXECUTE     0x20000000 /* section is executable */
  144. #define IMAGE_SCN_MEM_READ        0x40000000 /* section is readable */
  145. #define IMAGE_SCN_MEM_WRITE       0x80000000 /* section is writable */
  146. #define SCNHDR struct scnhdr
  147. #define SCNHSZ sizeof(SCNHDR)
  148. /******************************* LINE NUMBERS *******************************/
  149. /* 1 line number entry for every "breakpointable" source line in a section.
  150.  * Line numbers are grouped on a per function basis; first entry in a function
  151.  * grouping will have l_lnno = 0 and in place of physical address will be the
  152.  * symbol table index of the function name.
  153.  */
  154. struct lineno{
  155. union {
  156. long l_symndx; /* function name symbol index, iff l_lnno == 0*/
  157. long l_paddr; /* (physical) address of line number */
  158. } l_addr;
  159. unsigned short l_lnno; /* line number */
  160. char padding[2]; /* force alignment */
  161. };
  162. #define LINENO struct lineno
  163. #define LINESZ sizeof(LINENO)
  164. /********************************** SYMBOLS **********************************/
  165. #define SYMNMLEN 8 /* # characters in a symbol name */
  166. #define FILNMLEN 14 /* # characters in a file name */
  167. #define DIMNUM 4 /* # array dimensions in auxiliary entry */
  168. struct syment {
  169. union {
  170. char _n_name[SYMNMLEN]; /* old COFF version */
  171. struct {
  172. long _n_zeroes; /* new == 0 */
  173. long _n_offset; /* offset into string table */
  174. } _n_n;
  175. char *_n_nptr[2]; /* allows for overlaying */
  176. } _n;
  177. long n_value; /* value of symbol */
  178. short n_scnum; /* section number */
  179. unsigned short n_type; /* type and derived type */
  180. char n_sclass; /* storage class */
  181. char n_numaux; /* number of aux. entries */
  182. };
  183. #define SYMENT struct syment
  184. #define SYMESZ  sizeof(SYMENT) /* don't use sizeof since some compilers */
  185. /* may add padding in the structure      */
  186. #define n_name _n._n_name
  187. #define n_ptr _n._n_nptr[1]
  188. #define n_zeroes _n._n_n._n_zeroes
  189. #define n_offset _n._n_n._n_offset
  190. /*
  191.  * Relocatable symbols have number of the section in which they are defined,
  192.  * or one of the following:
  193.  */
  194. #define N_UNDEF ((short)0)  /* undefined symbol */
  195. #define N_ABS   ((short)-1) /* value of symbol is absolute */
  196. #define N_DEBUG ((short)-2) /* debugging symbol -- value is meaningless */
  197. #define N_TV    ((short)-3) /* indicates symbol needs preload transfer vector */
  198. #define P_TV    ((short)-4) /* indicates symbol needs postload transfer vector*/
  199. /*
  200.  * Symbol storage classes
  201.  */
  202. #define C_EFCN -1 /* physical end of function */
  203. #define C_NULL 0
  204. #define C_AUTO 1 /* automatic variable */
  205. #define C_EXT 2 /* external symbol */
  206. #define C_STAT 3 /* static */
  207. #define C_REG 4 /* register variable */
  208. #define C_EXTDEF 5 /* external definition */
  209. #define C_LABEL 6 /* label */
  210. #define C_ULABEL 7 /* undefined label */
  211. #define C_MOS 8 /* member of structure */
  212. #define C_ARG 9 /* function argument */
  213. #define C_STRTAG 10 /* structure tag */
  214. #define C_MOU 11 /* member of union */
  215. #define C_UNTAG 12 /* union tag */
  216. #define C_TPDEF 13 /* type definition */
  217. #define C_USTATIC 14 /* undefined static */
  218. #define C_ENTAG 15 /* enumeration tag */
  219. #define C_MOE 16 /* member of enumeration */
  220. #define C_REGPARM 17 /* register parameter */
  221. #define C_FIELD 18 /* bit field */
  222. #define C_AUTOARG 19 /* auto argument */
  223. #define C_BLOCK 100 /* ".bb" or ".eb" */
  224. #define C_FCN 101 /* ".bf" or ".ef" */
  225. #define C_EOS 102 /* end of structure */
  226. #define C_FILE 103 /* file name */
  227. #define C_LINE 104 /* line # reformatted as symbol table entry */
  228. #define C_ALIAS   105 /* duplicate tag */
  229. #define C_HIDDEN 106 /* ext symbol in dmert public lib */
  230. #define C_SCALL 107 /* Procedure reachable via system call */
  231. #define C_LEAFEXT 108 /* Global leaf procedure, "call" via BAL */
  232. #define C_LEAFSTAT 113 /* Static leaf procedure, "call" via BAL */
  233. /*
  234.  * Type of a symbol, in low 5 bits of n_type
  235.  */
  236. #define T_NULL 0
  237. #define T_VOID 1 /* function argument (only used by compiler) */
  238. #define T_CHAR 2 /* character */
  239. #define T_SHORT 3 /* short integer */
  240. #define T_INT 4 /* integer */
  241. #define T_LONG 5 /* long integer */
  242. #define T_FLOAT 6 /* floating point */
  243. #define T_DOUBLE 7 /* double word */
  244. #define T_STRUCT 8 /* structure  */
  245. #define T_UNION 9 /* union  */
  246. #define T_ENUM 10 /* enumeration  */
  247. #define T_MOE 11 /* member of enumeration*/
  248. #define T_UCHAR 12 /* unsigned character */
  249. #define T_USHORT 13 /* unsigned short */
  250. #define T_UINT 14 /* unsigned integer */
  251. #define T_ULONG 15 /* unsigned long */
  252. #define T_LNGDBL 16 /* long double */
  253. /*
  254.  * derived types, in n_type
  255.  */
  256. #define DT_NON 0 /* no derived type */
  257. #define DT_PTR 1 /* pointer */
  258. #define DT_FCN 2 /* function */
  259. #define DT_ARY 3 /* array */
  260. #define N_BTMASK 0x1f
  261. #define N_TMASK 0x60
  262. #define N_BTSHFT 5
  263. #define N_TSHIFT 2
  264. #define BTYPE(x) ((x) & N_BTMASK)
  265. #define ISPTR(x) (((x) & N_TMASK) == (DT_PTR << N_BTSHFT))
  266. #define ISFCN(x) (((x) & N_TMASK) == (DT_FCN << N_BTSHFT))
  267. #define ISARY(x) (((x) & N_TMASK) == (DT_ARY << N_BTSHFT))
  268. #define ISTAG(x) ((x)==C_STRTAG||(x)==C_UNTAG||(x)==C_ENTAG)
  269. #define DECREF(x) ((((x)>>N_TSHIFT)&~N_BTMASK)|((x)&N_BTMASK))
  270. union auxent {
  271. struct {
  272. long x_tagndx; /* str, un, or enum tag indx */
  273. union {
  274. struct {
  275.     unsigned short x_lnno; /* declaration line number */
  276.     unsigned short x_size; /* str/union/array size */
  277. } x_lnsz;
  278. long x_fsize; /* size of function */
  279. } x_misc;
  280. union {
  281. struct { /* if ISFCN, tag, or .bb */
  282.     long x_lnnoptr; /* ptr to fcn line # */
  283.     long x_endndx; /* entry ndx past block end */
  284. } x_fcn;
  285. struct { /* if ISARY, up to 4 dimen. */
  286.     unsigned short x_dimen[DIMNUM];
  287. } x_ary;
  288. } x_fcnary;
  289. unsigned short x_tvndx; /* tv index */
  290. } x_sym;
  291. union {
  292. char x_fname[FILNMLEN];
  293. struct {
  294. long x_zeroes;
  295. long x_offset;
  296. } x_n;
  297. } x_file;
  298. struct {
  299. long x_scnlen; /* section length */
  300. unsigned short x_nreloc; /* # relocation entries */
  301. unsigned short x_nlinno; /* # line numbers */
  302. } x_scn;
  303. struct {
  304. long x_tvfill; /* tv fill value */
  305. unsigned short x_tvlen; /* length of .tv */
  306. unsigned short x_tvran[2]; /* tv range */
  307. } x_tv; /* info about .tv section (in auxent of symbol .tv)) */
  308. };
  309. #define AUXENT union auxent
  310. #define AUXESZ sizeof(AUXENT)
  311. #define _ETEXT "_etext"
  312. /********************** RELOCATION DIRECTIVES **********************/
  313. struct reloc {
  314. long r_vaddr; /* Virtual address of reference */
  315. long r_symndx; /* Index into symbol table */
  316. unsigned short r_type; /* Relocation type */
  317. };
  318. #define IMAGE_REL_I386_DIR32 0x0006
  319. #define IMAGE_REL_I386_REL32 0x0014
  320. #define RELOC struct reloc
  321. #define RELSZ sizeof(RELOC)
  322. /* status codes */
  323. #define S_loadLib_FILE_READ_ERROR               (M_loadPecoffLib | 1)
  324. #define S_loadLib_REALLOC_ERROR                 (M_loadPecoffLib | 2)
  325. #define S_loadLib_JMPADDR_ERROR                 (M_loadPecoffLib | 3)
  326. #define S_loadLib_NO_REFLO_PAIR                 (M_loadPecoffLib | 4)
  327. #define S_loadLib_GPREL_REFERENCE               (M_loadPecoffLib | 5)
  328. #define S_loadLib_UNRECOGNIZED_RELOCENTRY       (M_loadPecoffLib | 6)
  329. #define S_loadLib_REFHALF_OVFL                  (M_loadPecoffLib | 7)
  330. #define S_loadLib_FILE_ENDIAN_ERROR             (M_loadPecoffLib | 8)
  331. #define S_loadLib_UNEXPECTED_SYM_CLASS          (M_loadPecoffLib | 9)
  332. #define S_loadLib_UNRECOGNIZED_SYM_CLASS        (M_loadPecoffLib | 10)
  333. #define S_loadLib_HDR_READ                      (M_loadPecoffLib | 11)
  334. #define S_loadLib_OPTHDR_READ                   (M_loadPecoffLib | 12)
  335. #define S_loadLib_SCNHDR_READ                   (M_loadPecoffLib | 13)
  336. #define S_loadLib_READ_SECTIONS                 (M_loadPecoffLib | 14)
  337. #define S_loadLib_LOAD_SECTIONS                 (M_loadPecoffLib | 15)
  338. #define S_loadLib_RELOC_READ                    (M_loadPecoffLib | 16)
  339. #define S_loadLib_SYMHDR_READ                   (M_loadPecoffLib | 17)
  340. #define S_loadLib_EXTSTR_READ                   (M_loadPecoffLib | 18)
  341. #define S_loadLib_EXTSYM_READ                   (M_loadPecoffLib | 19)
  342. /* function declarations */
  343. extern STATUS    loadPecoffInit (void);
  344. extern MODULE_ID ldPecoffModAtSym (int fd, int symFlag, char **ppText,
  345.    char **ppData, char **ppBss,
  346.    SYMTAB_ID symTbl);
  347. #ifdef __cplusplus
  348. }
  349. #endif
  350. #endif /* __INCloadPecoffLibh */