obj2ti.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Written by Julien Muchembled.
  2. // Fixed by Romain Lievin for Linux.
  3. // Copyright (c) 2001. All rights reserved.
  4. #if defined(WIN32) || defined(__WIN32__)
  5. # include <windows.h>
  6. #endif
  7. #include <malloc.h>
  8. #include <stdio.h>
  9. #include "str.h"
  10. #include <stdlib.h>
  11. #define _alloca __builtin_alloca
  12. #if !defined(WIN32) && !defined(__WIN32__)
  13. # define _MAX_PATH 1024 //PATH_MAX //unistd.h
  14. # define LOBYTE(w) (unsigned char)(w)
  15. # define HIBYTE(w) (unsigned char)((w) >> 8)
  16. typedef unsigned long DWORD;
  17. #endif
  18. #include "stubs.h"
  19. #include "m68kdefs.h"
  20. #ifndef STYP_TEXT
  21. #define STYP_TEXT 0x20
  22. #endif
  23. #ifndef STYP_DATA
  24. #define STYP_DATA 0x40
  25. #endif
  26. #ifndef STYP_BSS
  27. #define STYP_BSS 0x80
  28. #endif
  29. // Taken from the source of a68k.
  30. #define HunkUnit 999
  31. #define HunkName 1000
  32. #define HunkCode 1001
  33. #define HunkData 1002
  34. #define HunkBSS 1003
  35. #define HunkR32 1004
  36. #define HunkR16 1005
  37. #define HunkR8 1006
  38. #define HunkExt 1007
  39. #define HunkSym 1008
  40. #define HunkDbg 1009
  41. #define HunkEnd 1010
  42. #define TypeEnd 0
  43. #define TypeExport 1
  44. #define TypeImport32 0x81
  45. #define TypeImport16 0x83
  46. #define TypeImport8 0x84
  47. #define TypeImport16Alt 0x8A
  48. enum ObjErr {
  49. OBJERR_OK,
  50. OBJERR_Open,
  51. OBJERR_Read,
  52. OBJERR_Mem,
  53. OBJERR_Unknown,
  54. OBJERR_NbScns,
  55. OBJERR_Size,
  56. OBJERR_HunkType,
  57. OBJERR_R32Hunk,
  58. OBJERR_ExtHunk,
  59. OBJERR_ExtType,
  60. OBJERR_OptHdr,
  61. OBJERR_ScnType,
  62. OBJERR_RlcType,
  63. OBJERR_nbErrTypes
  64. };
  65. enum TI68kErr {
  66. TI68kERR_OK,
  67. TI68kERR_NoOutput=OBJERR_nbErrTypes,
  68. TI68kERR_Unknown,
  69. TI68kERR_BadExport,
  70. TI68kERR_Conflict,
  71. TI68kERR_DoorsOSUndef,
  72. TI68kERR_NostubUndef,
  73. TI68kERR_prosit,
  74. TI68kERR_TooLarge,
  75. TI68kERR_nbErrTypes
  76. };
  77. char *szErr[TI68kERR_nbErrTypes-1]={
  78. "Error in opening object file",
  79. "Error in reading object file",
  80. "Not enough memory",
  81. "Unknown format",
  82. "Bad number of sections",
  83. "Unexpected end of file",
  84. "Unsupported hunk type",
  85. "Unexpected or bad Reloc32 hunk",
  86. "Unexpected Ext. hunk",
  87. "Unsupported Ext. type",
  88. "Optional header is unsupported",
  89. "Unsupported section type",
  90. "Unsupported relocation type",
  91. "No target (_ti89, _ti92plus, ...) is defined",
  92. "Program format is unsupported or not specified",
  93. "Exported label is not valid",
  94. NULL, NULL, NULL,
  95. "Prosit format is unsupported"
  96. "Variable too large"
  97. };
  98. enum TI68kType {
  99. TI68k_none,
  100. TI68k_68kP,
  101. TI68k_68kL,
  102. TI68k_nostub,
  103. TI68k_nostub_dll,
  104. TI68k_prosit,
  105. TI68k_ext,
  106. };
  107. char *szTI68kType[]={
  108. "_main",
  109. "_library",
  110. "_nostub",
  111. "_nostub_dll",
  112. "_prosit",
  113. "_extfile"
  114. };
  115. #define Delete(mem) do{if (mem) {delete mem; mem = NULL;}}while(0)
  116. #define DeleteCast(type,mem) do{if ((type)mem) {delete (type)mem; mem = NULL;}}while(0)
  117. typedef struct {
  118. char *name;
  119. unsigned long nrlcs, *rlc;
  120. } Import;
  121. typedef struct {
  122. char *name;
  123. unsigned long value;
  124. } Export;
  125. class Object
  126. {
  127. private:
  128. long Size;
  129. union {
  130. void *o0;
  131. unsigned char *o8;
  132. unsigned long *o32;
  133. FILHDR *filhdr;
  134. };
  135. enum {
  136. OBJFMT_None,
  137. OBJFMT_Amiga,
  138. OBJFMT_COFF
  139. } ObjFmt;
  140. protected:
  141. bool bBadExport;
  142. struct {
  143. unsigned long size, *R32[3], nR32[3], nImp16, nImp32, nExports;
  144. union {
  145. void *o0;
  146. unsigned char *o8;
  147. unsigned long *o32;
  148. };
  149. Import *Imp16, *Imp32;
  150. Export *Exports;
  151. bool DelDat;
  152. } Hunk[3];
  153. int CodeHunk, DataHunk, BSSHunk;
  154. unsigned int nscns;
  155. public:
  156. Object () {
  157. Size = 0; o0 = NULL; ObjFmt = OBJFMT_None;
  158. memset (Hunk, 0, sizeof (Hunk)); nscns = 0;
  159. CodeHunk = DataHunk = BSSHunk = -1;
  160. bBadExport = false;
  161. }
  162. virtual ~Object () {
  163. int i; for (i=0; i<3; i++) {
  164. unsigned long j;
  165. if (Hunk[i].DelDat) Delete (Hunk[i].o8);
  166. for (j=0; j<3; j++) Delete (Hunk[i].R32[j]);
  167. for (j=0; j<Hunk[i].nImp16; j++)
  168. Delete (Hunk[i].Imp16[j].rlc);
  169. if (Hunk[i].Imp32)
  170. for (j=0; j<Hunk[i].nImp32; j++)
  171. Delete (Hunk[i].Imp32[j].rlc);
  172. Delete (Hunk[i].Imp16);
  173. Delete (Hunk[i].Imp32);
  174. Delete (Hunk[i].Exports);
  175. }
  176. if (o0) free (o0), o0 = NULL;
  177. }
  178. void Merge (int &, int &);
  179. ObjErr Read (char *);
  180. private:
  181. void AllocExt (int, Import **, Import **, Export **);
  182. char *COFF_ReadName (SYMENT *, char *);
  183. void MergeImp (unsigned long, Import *, unsigned long &, Import * &, unsigned long);
  184. void Merge2 (unsigned long &, void * &, unsigned long &, void * &, unsigned long);
  185. #ifdef SUPPORT_AMIGA
  186. ObjErr ReadAmiga ();
  187. #endif
  188. ObjErr ReadCOFF ();
  189. };
  190. class TI68k : public Object
  191. {
  192. private:
  193. char **szErr;
  194. TI68kType Type;
  195. union {
  196. unsigned short Flags;
  197. struct {
  198. unsigned short _Output92p : 1;
  199. unsigned short _Output89 : 1;
  200. unsigned short _NoSaveScreen : 1;
  201. unsigned short _ReadOnly : 1;
  202. unsigned short Unused : 4;
  203. unsigned short _LibVersion : 8;
  204. } bFlags;
  205. };
  206. #define Output92p (bFlags._Output92p)
  207. #define Output89 (bFlags._Output89)
  208. #define NoSaveScreen (bFlags._NoSaveScreen)
  209. #define ReadOnly (bFlags._ReadOnly)
  210. #define LibVersion (bFlags._LibVersion)
  211. unsigned long size;
  212. unsigned char *dat;
  213. private:
  214. TI68kErr Conflict (TI68kType);
  215. TI68kErr DoSave (char *, char *, char *, unsigned long, unsigned short);
  216. TI68kErr MakeDoorsOS ();
  217. TI68kErr MakeNostub ();
  218. TI68kErr MakeExt ();
  219. bool NostubImp (unsigned long, Import *);
  220. bool ExtImp (unsigned long, Import *);
  221. void UndefRef (Import *);
  222. public:
  223. TI68k (char **szErr2, TI68kType Type = TI68k_none) {
  224. szErr = szErr2;
  225. this->Type = Type;
  226. Flags = 0;
  227. size = 0; dat = NULL;
  228. }
  229. virtual ~TI68k () {
  230. Delete (dat);
  231. if (szErr) Delete (szErr[TI68kERR_Conflict-1]);
  232. }
  233. TI68kErr Make ();
  234. TI68kErr Save (char *, bool, bool);
  235. unsigned long GetExeSize (void) { return size+2; }
  236. };