Apply patch from Daniel Schürmann: https://sourceforge.net/p/boomerang/bugs/78/
[boomerang.git] / boomerang / loader / DOS4GWBinaryFile.h
blob3ce0ef8548a9bd1f54734b0e46554936bd50d18c
1 /*
2 * Copyright (C) 2000, The University of Queensland
3 * Copyright (C) 2001, Sun Microsystems, Inc
5 * See the file "LICENSE.TERMS" for information on usage and
6 * redistribution of this file, and for a DISCLAIMER OF ALL
7 * WARRANTIES.
9 */
11 /* File: DOS4GWBinaryFile.h
12 * Desc: This file contains the definition of the class DOS4GWBinaryFile.
15 #ifndef __DOS4GWBINARYFILE_H__
16 #define __DOS4GWBINARYFILE_H_
18 #include "BinaryFile.h"
19 #include <string>
21 /* $Revision$
22 * This file contains the definition of the DOS4GWBinaryFile class, and some
23 * other definitions specific to the exe version of the BinaryFile object
25 /* At present, this loader supports the OS2 file format (also known as
26 * the Linear eXecutable format) as much as I've found necessary to
27 * inspect old DOS4GW apps. This loader could also be used for decompiling
28 * Win9x VxD files or, of course, OS2 binaries, but you're probably better off
29 * making a specific loader for each of these.
30 * 24 Jan 05 - Trent: created.
33 // Given a little endian value x, load its value assuming little endian order
34 // Note: must be able to take address of x
35 // Note: Unlike the LH macro in BinaryFile.h, the paraeter is not a pointer
36 #define LMMH(x) ((unsigned)((Byte *)(&x))[0] + ((unsigned)((Byte *)(&x))[1] << 8) + \
37 ((unsigned)((Byte *)(&x))[2] << 16) + ((unsigned)((Byte *)(&x))[3] << 24))
38 // With this one, x IS a pounsigneder
39 #define LMMH2(x) ((unsigned)((Byte *)(x))[0] + ((unsigned)((Byte *)(x))[1] << 8) + \
40 ((unsigned)((Byte *)(x))[2] << 16) + ((unsigned)((Byte *)(x))[3] << 24))
41 #define LMMHw(x) ((unsigned)((Byte *)(&x))[0] + ((unsigned)((Byte *)(&x))[1] << 8))
43 typedef struct /* exe file header, just the signature really */
45 Byte sigLo; /* .EXE signature: 0x4D 0x5A */
46 Byte sigHi;
48 Header;
50 //#ifdef WIN32
51 #pragma pack(1)
52 //#endif
54 typedef struct
56 Byte sigLo;
57 Byte sigHi;
58 Byte byteord;
59 Byte wordord;
60 DWord formatlvl;
61 SWord cputype;
62 SWord ostype;
63 DWord modulever;
64 DWord moduleflags;
65 DWord modulenumpages;
66 DWord eipobjectnum;
67 DWord eip;
68 DWord espobjectnum;
69 DWord esp;
70 DWord pagesize;
71 DWord pageoffsetshift;
72 DWord fixupsectionsize;
73 DWord fixupsectionchksum;
74 DWord loadersectionsize;
75 DWord loadersectionchksum;
76 DWord objtbloffset;
77 DWord numobjsinmodule;
78 DWord objpagetbloffset;
79 DWord objiterpagesoffset;
80 DWord resourcetbloffset;
81 DWord numresourcetblentries;
82 DWord residentnametbloffset;
83 DWord entrytbloffset;
84 DWord moduledirectivesoffset;
85 DWord nummoduledirectives;
86 DWord fixuppagetbloffset;
87 DWord fixuprecordtbloffset;
88 DWord importtbloffset;
89 DWord numimportmoduleentries;
90 DWord importproctbloffset;
91 DWord perpagechksumoffset;
92 DWord datapagesoffset;
93 DWord numpreloadpages;
94 DWord nonresnametbloffset;
95 DWord nonresnametbllen;
96 DWord nonresnametblchksum;
97 DWord autodsobjectnum;
98 DWord debuginfooffset;
99 DWord debuginfolen;
100 DWord numinstancepreload;
101 DWord numinstancedemand;
102 DWord heapsize;
104 LXHeader;
106 typedef struct
108 DWord VirtualSize;
109 DWord RelocBaseAddr;
110 DWord ObjectFlags;
111 DWord PageTblIdx;
112 DWord NumPageTblEntries;
113 DWord Reserved1;
115 LXObject;
117 typedef struct
119 DWord pagedataoffset;
120 SWord datasize;
121 SWord flags;
123 LXPage;
125 // this is correct for internal fixups only
127 typedef struct
129 unsigned char src;
130 unsigned char flags;
131 short srcoff;
132 // unsigned char object; // these are now variable length
133 // unsigned short trgoff;
135 LXFixup;
137 //#ifdef WIN32
138 #pragma pack(4)
139 //#endif
141 class DOS4GWBinaryFile : public BinaryFile
143 public:
144 DOS4GWBinaryFile(); // Default constructor
145 virtual ~DOS4GWBinaryFile(); // Destructor
146 virtual bool Open(const char* sName); // Open the file for r/w; ???
147 virtual void Close(); // Close file opened with Open()
148 virtual void UnLoad(); // Unload the image
149 virtual LOAD_FMT GetFormat() const; // Get format (i.e.
150 // LOADFMT_DOS4GW)
151 virtual MACHINE GetMachine() const; // Get machine (i.e.
152 // MACHINE_Pentium)
154 virtual const char *getFilename() const
156 return m_pFileName;
158 virtual bool isLibrary() const;
159 virtual std::list<const char *> getDependencyList();
160 virtual ADDRESS getImageBase();
161 virtual size_t getImageSize();
163 virtual std::list<SectionInfo*>& GetEntryPoints(const char* pEntry = "main");
164 virtual ADDRESS GetMainEntryPoint();
165 virtual ADDRESS GetEntryPoint();
166 ptrdiff_t getDelta();
167 virtual const char* SymbolByAddress(ADDRESS dwAddr); // Get sym from addr
168 virtual ADDRESS GetAddressByName(const char* name,
169 bool bNoTypeOK = false); // Find addr given name
170 virtual void AddSymbol(ADDRESS uNative, const char *pName);
173 // -- -- -- -- -- -- -- -- --
175 // Internal information
176 // Dump headers, etc
177 virtual bool DisplayDetails(const char* fileName, FILE* f = stdout);
179 protected:
181 int dos4gwRead2(short *ps) const; // Read 2 bytes from native addr
182 int dos4gwRead4(int *pi) const; // Read 4 bytes from native addr
184 public:
186 virtual int readNative1(ADDRESS a); // Read 1 bytes from native addr
187 virtual int readNative2(ADDRESS a); // Read 2 bytes from native addr
188 virtual int readNative4(ADDRESS a); // Read 4 bytes from native addr
189 virtual QWord readNative8(ADDRESS a); // Read 8 bytes from native addr
190 virtual float readNativeFloat4(ADDRESS a); // Read 4 bytes as float
191 virtual double readNativeFloat8(ADDRESS a); // Read 8 bytes as float
193 virtual bool IsDynamicLinkedProcPointer(ADDRESS uNative);
194 virtual bool IsDynamicLinkedProc(ADDRESS uNative);
195 virtual const char *GetDynamicProcName(ADDRESS uNative);
197 virtual std::map<ADDRESS, std::string> &getSymbols()
199 return dlprocptrs;
202 protected:
203 virtual bool RealLoad(const char* sName); // Load the file; pure virtual
205 private:
207 bool PostLoad(void* handle); // Called after archive member loaded
209 Header* m_pHeader; // Pointer to header
210 LXHeader* m_pLXHeader; // Pointer to lx header
211 LXObject* m_pLXObjects; // Pointer to lx objects
212 LXPage* m_pLXPages; // Pointer to lx pages
213 size_t m_cbImage; // Size of image
214 //int m_cReloc; // Number of relocation entries
215 //DWord* m_pRelocTable; // The relocation table
216 unsigned char *base; // Beginning of the loaded image
217 // Map from address of dynamic pointers to library procedure names:
218 std::map<ADDRESS, std::string> dlprocptrs;
219 const char *m_pFileName;
223 //#ifdef WIN32
224 #pragma pack()
225 //#endif
226 #endif // ifndef __DOS4GWBINARYFILE_H__