2 * Copyright (C) 1997-2001, The University of Queensland
3 * Copyright (C) 2001, Sun Microsystems, Inc
4 * Copyright (C) 2002, Trent Waddington
6 * See the file "LICENSE.TERMS" for information on usage and
7 * redistribution of this file, and for a DISCLAIMER OF ALL
12 /* File: BinaryFile.cpp
13 * Desc: This file contains the implementation of the class BinaryFile
15 * This file implements the abstract BinaryFile class.
16 * All classes derived from this class must implement the Load()
22 * 21 Apr 02 - Mike: mods for boomerang
23 * 03 Jun 02 - Trent: if WIN32, no dynamic linking
24 * 14 Jun 02 - Mike: Fixed a bug where Windows programs chose the Exe loader
27 /*==============================================================================
29 *============================================================================*/
31 #if defined(_MSC_VER) && _MSC_VER <= 1200
32 #pragma warning(disable:4786)
35 #include "BinaryFile.h"
40 BinaryFile::BinaryFile(bool bArch
/*= false*/)
42 m_bArchive
= bArch
; // Remember whether an archive member
43 m_iNumSections
= 0; // No sections yet
44 m_pSections
= 0; // No section data yet
47 // This struct used to be initialised with a memset, but now that overwrites the virtual table (if compiled under gcc
48 // and possibly others)
50 SectionInfo::SectionInfo() :
51 pSectionName(NULL
), uNativeAddr(0), uHostAddr(0), uSectionSize(0), uSectionEntrySize(0), uType(0),
52 bCode(false), bData(false), bBss(0), bReadOnly(0)
56 SectionInfo::~SectionInfo()
60 int BinaryFile::GetNumSections() const
62 return m_iNumSections
;
65 PSectionInfo
BinaryFile::GetSectionInfo(int idx
) const
67 return m_pSections
+ idx
;
70 int BinaryFile::GetSectionIndexByName(const char* sName
)
72 for (int i
= 0; i
< m_iNumSections
; i
++)
74 if (strcmp(m_pSections
[i
].pSectionName
, sName
) == 0)
82 PSectionInfo
BinaryFile::GetSectionInfoByAddr(ADDRESS uEntry
) const
85 for (int i
= 0; i
< m_iNumSections
; i
++)
87 pSect
= &m_pSections
[i
];
88 if ((uEntry
>= pSect
->uNativeAddr
) &&
89 (uEntry
< pSect
->uNativeAddr
+ pSect
->uSectionSize
))
91 // We have the right section
95 // Failed to find the address
99 PSectionInfo
BinaryFile::GetSectionInfoByName(const char* sName
)
101 int i
= GetSectionIndexByName(sName
);
102 if (i
== -1) return 0;
103 return &m_pSections
[i
];
107 ///////////////////////
108 // Trivial functions //
109 // Overridden if reqd//
110 ///////////////////////
112 const char* BinaryFile::SymbolByAddress(ADDRESS uNative
)
114 return 0; // Overridden by subclasses that support syms
117 ADDRESS
BinaryFile::GetAddressByName(const char* pName
, bool bNoTypeOK
)
122 int BinaryFile::GetSizeByName(const char* pName
, bool bNoTypeOK
)
127 bool BinaryFile::IsDynamicLinkedProc(ADDRESS uNative
)
132 bool BinaryFile::IsStaticLinkedLibProc(ADDRESS uNative
)
137 bool BinaryFile::IsDynamicLinkedProcPointer(ADDRESS uNative
)
142 ADDRESS
BinaryFile::IsJumpToAnotherAddr(ADDRESS uNative
)
147 const char *BinaryFile::GetDynamicProcName(ADDRESS uNative
)
152 bool BinaryFile::DisplayDetails(const char* fileName
, FILE* f
/* = stdout */)
154 return false; // Should always be overridden
155 // Should display file header, program
156 // headers and section headers, as well
157 // as contents of each of the sections.
160 // Specific to BinaryFile objects that implement a "global pointer"
161 // Gets a pair of unsigned integers representing the address of %agp, and
162 // a machine specific value (GLOBALOFFSET)
163 // This is a stub routine that should be overridden if required
165 std::pair
<unsigned, unsigned> BinaryFile::GetGlobalPointerInfo()
167 return std::pair
<unsigned, unsigned>(0, 0);
170 // Get a pointer to a new map of dynamic global data items.
171 // If the derived class doesn't implement this function, return an empty map
172 // Caller should delete the returned map
174 std::map
<ADDRESS
, const char*>* BinaryFile::GetDynamicGlobalMap()
176 return new std::map
<ADDRESS
, const char*>;
179 // Get an array of exported function stub addresses. Normally overridden.
181 ADDRESS
* BinaryFile::GetImportStubs(int& numExports
)
187 void BinaryFile::getTextLimits()
189 int n
= GetNumSections();
190 limitTextLow
= 0xFFFFFFFF;
193 for (int i
= 0; i
< n
; i
++)
195 SectionInfo
* pSect
= GetSectionInfo(i
);
198 // The .plt section is an anomaly. It's code, but we never want to
199 // decode it, and in Sparc ELF files, it's actually in the data
200 // segment (so it can be modified). For now, we make this ugly
202 if (strcmp(".plt", pSect
->pSectionName
) == 0)
204 if (pSect
->uNativeAddr
< limitTextLow
)
205 limitTextLow
= pSect
->uNativeAddr
;
206 ADDRESS hiAddress
= pSect
->uNativeAddr
+ pSect
->uSectionSize
;
207 if (hiAddress
> limitTextHigh
)
208 limitTextHigh
= hiAddress
;
210 textDelta
= pSect
->uHostAddr
- (unsigned char *)pSect
->uNativeAddr
;
213 if (textDelta
!= pSect
->uHostAddr
- (unsigned char *)pSect
->uNativeAddr
)
214 std::cerr
<< "warning: textDelta different for section " << pSect
->pSectionName
<<