2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2008-02-28 00:26:35 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2008-02-28 00:26:35 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2008-02-28 00:26:35 +03:00
|
|
|
#include "cmStandardIncludes.h" // to get CMAKE_USE_ELF_PARSER first
|
2016-04-29 16:40:20 +03:00
|
|
|
|
2008-02-28 00:26:35 +03:00
|
|
|
#include "cmELF.h"
|
|
|
|
|
2016-06-28 17:17:52 +03:00
|
|
|
#include <cm_auto_ptr.hxx>
|
2014-01-04 09:47:13 +04:00
|
|
|
#include <cmsys/FStream.hxx>
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Include the ELF format information system header.
|
2013-06-21 13:13:35 +04:00
|
|
|
#if defined(__OpenBSD__)
|
2016-05-16 17:34:04 +03:00
|
|
|
#include <elf_abi.h>
|
|
|
|
#include <stdint.h>
|
2014-03-03 18:47:08 +04:00
|
|
|
#elif defined(__HAIKU__)
|
2016-05-16 17:34:04 +03:00
|
|
|
#include <elf32.h>
|
|
|
|
#include <elf64.h>
|
|
|
|
typedef struct Elf32_Ehdr Elf32_Ehdr;
|
|
|
|
typedef struct Elf32_Shdr Elf32_Shdr;
|
|
|
|
typedef struct Elf32_Sym Elf32_Sym;
|
|
|
|
typedef struct Elf32_Rel Elf32_Rel;
|
|
|
|
typedef struct Elf32_Rela Elf32_Rela;
|
|
|
|
#define ELFMAG0 0x7F
|
|
|
|
#define ELFMAG1 'E'
|
|
|
|
#define ELFMAG2 'L'
|
|
|
|
#define ELFMAG3 'F'
|
|
|
|
#define ET_NONE 0
|
|
|
|
#define ET_REL 1
|
|
|
|
#define ET_EXEC 2
|
|
|
|
#define ET_DYN 3
|
|
|
|
#define ET_CORE 4
|
|
|
|
#define EM_386 3
|
|
|
|
#define EM_SPARC 2
|
|
|
|
#define EM_PPC 20
|
2013-06-21 13:13:35 +04:00
|
|
|
#else
|
2016-05-16 17:34:04 +03:00
|
|
|
#include <elf.h>
|
2013-06-21 13:13:35 +04:00
|
|
|
#endif
|
2008-02-28 16:43:10 +03:00
|
|
|
#if defined(__sun)
|
2016-05-16 17:34:04 +03:00
|
|
|
#include <sys/link.h> // For dynamic section information
|
2008-02-28 16:43:10 +03:00
|
|
|
#endif
|
2016-07-28 19:26:10 +03:00
|
|
|
#ifdef _SCO_DS
|
|
|
|
#include <link.h> // For DT_SONAME etc.
|
|
|
|
#endif
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Low-level byte swapping implementation.
|
2016-05-16 17:34:04 +03:00
|
|
|
template <size_t s>
|
|
|
|
struct cmELFByteSwapSize
|
|
|
|
{
|
|
|
|
};
|
2016-08-17 02:49:57 +03:00
|
|
|
void cmELFByteSwap(char* /*unused*/, cmELFByteSwapSize<1> const& /*unused*/)
|
2008-02-28 00:26:35 +03:00
|
|
|
{
|
|
|
|
}
|
2016-08-17 02:49:57 +03:00
|
|
|
void cmELFByteSwap(char* data, cmELFByteSwapSize<2> const& /*unused*/)
|
2008-02-28 00:26:35 +03:00
|
|
|
{
|
|
|
|
char one_byte;
|
2016-05-16 17:34:04 +03:00
|
|
|
one_byte = data[0];
|
|
|
|
data[0] = data[1];
|
|
|
|
data[1] = one_byte;
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
2016-08-17 02:49:57 +03:00
|
|
|
void cmELFByteSwap(char* data, cmELFByteSwapSize<4> const& /*unused*/)
|
2008-02-28 00:26:35 +03:00
|
|
|
{
|
|
|
|
char one_byte;
|
2016-05-16 17:34:04 +03:00
|
|
|
one_byte = data[0];
|
|
|
|
data[0] = data[3];
|
|
|
|
data[3] = one_byte;
|
|
|
|
one_byte = data[1];
|
|
|
|
data[1] = data[2];
|
|
|
|
data[2] = one_byte;
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
2016-08-17 02:49:57 +03:00
|
|
|
void cmELFByteSwap(char* data, cmELFByteSwapSize<8> const& /*unused*/)
|
2008-02-28 00:26:35 +03:00
|
|
|
{
|
|
|
|
char one_byte;
|
2016-05-16 17:34:04 +03:00
|
|
|
one_byte = data[0];
|
|
|
|
data[0] = data[7];
|
|
|
|
data[7] = one_byte;
|
|
|
|
one_byte = data[1];
|
|
|
|
data[1] = data[6];
|
|
|
|
data[6] = one_byte;
|
|
|
|
one_byte = data[2];
|
|
|
|
data[2] = data[5];
|
|
|
|
data[5] = one_byte;
|
|
|
|
one_byte = data[3];
|
|
|
|
data[3] = data[4];
|
|
|
|
data[4] = one_byte;
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Low-level byte swapping interface.
|
|
|
|
template <typename T>
|
|
|
|
void cmELFByteSwap(T& x)
|
|
|
|
{
|
|
|
|
cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>());
|
|
|
|
}
|
|
|
|
|
|
|
|
class cmELFInternal
|
|
|
|
{
|
|
|
|
public:
|
2008-02-29 19:12:59 +03:00
|
|
|
typedef cmELF::StringEntry StringEntry;
|
2016-05-16 17:34:04 +03:00
|
|
|
enum ByteOrderType
|
|
|
|
{
|
|
|
|
ByteOrderMSB,
|
|
|
|
ByteOrderLSB
|
|
|
|
};
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Construct and take ownership of the file stream object.
|
2016-06-28 17:17:52 +03:00
|
|
|
cmELFInternal(cmELF* external, CM_AUTO_PTR<cmsys::ifstream>& fin,
|
2016-05-16 17:34:04 +03:00
|
|
|
ByteOrderType order)
|
|
|
|
: External(external)
|
|
|
|
, Stream(*fin.release())
|
|
|
|
, ByteOrder(order)
|
|
|
|
, ELFType(cmELF::FileTypeInvalid)
|
|
|
|
{
|
|
|
|
// In most cases the processor-specific byte order will match that
|
|
|
|
// of the target execution environment. If we choose wrong here
|
|
|
|
// it is fixed when the header is read.
|
2015-12-17 23:03:42 +03:00
|
|
|
#if KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_LITTLE
|
2008-02-28 00:26:35 +03:00
|
|
|
this->NeedSwap = (this->ByteOrder == ByteOrderMSB);
|
2015-12-17 23:03:42 +03:00
|
|
|
#elif KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_BIG
|
2008-02-28 00:26:35 +03:00
|
|
|
this->NeedSwap = (this->ByteOrder == ByteOrderLSB);
|
2008-05-13 18:34:24 +04:00
|
|
|
#else
|
|
|
|
this->NeedSwap = false; // Final decision is at runtime anyway.
|
2008-02-28 00:26:35 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// We have not yet loaded the section info.
|
|
|
|
this->DynamicSectionIndex = -1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Destruct and delete the file stream object.
|
2016-05-16 17:34:04 +03:00
|
|
|
virtual ~cmELFInternal() { delete &this->Stream; }
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Forward to the per-class implementation.
|
|
|
|
virtual unsigned int GetNumberOfSections() const = 0;
|
2008-04-14 23:02:24 +04:00
|
|
|
virtual unsigned int GetDynamicEntryCount() = 0;
|
|
|
|
virtual unsigned long GetDynamicEntryPosition(int j) = 0;
|
2014-04-14 20:13:19 +04:00
|
|
|
virtual StringEntry const* GetDynamicSectionString(unsigned int tag) = 0;
|
2008-02-28 00:26:35 +03:00
|
|
|
virtual void PrintInfo(std::ostream& os) const = 0;
|
|
|
|
|
2008-04-14 23:02:24 +04:00
|
|
|
bool ReadBytes(unsigned long pos, unsigned long size, char* buf)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-04-14 23:02:24 +04:00
|
|
|
this->Stream.seekg(pos);
|
|
|
|
this->Stream.read(buf, size);
|
2016-06-02 00:29:53 +03:00
|
|
|
return !this->Stream.fail();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-14 23:02:24 +04:00
|
|
|
|
2008-02-29 19:12:59 +03:00
|
|
|
// Lookup the SONAME in the DYNAMIC section.
|
|
|
|
StringEntry const* GetSOName()
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-02-29 19:12:59 +03:00
|
|
|
return this->GetDynamicSectionString(DT_SONAME);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-29 19:12:59 +03:00
|
|
|
|
|
|
|
// Lookup the RPATH in the DYNAMIC section.
|
|
|
|
StringEntry const* GetRPath()
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-02-29 19:12:59 +03:00
|
|
|
return this->GetDynamicSectionString(DT_RPATH);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-29 19:12:59 +03:00
|
|
|
|
|
|
|
// Lookup the RUNPATH in the DYNAMIC section.
|
|
|
|
StringEntry const* GetRunPath()
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-02-29 19:12:59 +03:00
|
|
|
#if defined(DT_RUNPATH)
|
|
|
|
return this->GetDynamicSectionString(DT_RUNPATH);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-29 19:12:59 +03:00
|
|
|
|
2008-02-28 00:26:35 +03:00
|
|
|
// Return the recorded ELF type.
|
|
|
|
cmELF::FileType GetFileType() const { return this->ELFType; }
|
|
|
|
protected:
|
|
|
|
// Data common to all ELF class implementations.
|
|
|
|
|
|
|
|
// The external cmELF object.
|
|
|
|
cmELF* External;
|
|
|
|
|
|
|
|
// The stream from which to read.
|
|
|
|
std::istream& Stream;
|
|
|
|
|
|
|
|
// The byte order of the ELF file.
|
|
|
|
ByteOrderType ByteOrder;
|
|
|
|
|
|
|
|
// The ELF file type.
|
|
|
|
cmELF::FileType ELFType;
|
|
|
|
|
|
|
|
// Whether we need to byte-swap structures read from the stream.
|
|
|
|
bool NeedSwap;
|
|
|
|
|
|
|
|
// The section header index of the DYNAMIC section (-1 if none).
|
|
|
|
int DynamicSectionIndex;
|
|
|
|
|
|
|
|
// Helper methods for subclasses.
|
|
|
|
void SetErrorMessage(const char* msg)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-02-28 00:26:35 +03:00
|
|
|
this->External->ErrorMessage = msg;
|
|
|
|
this->ELFType = cmELF::FileTypeInvalid;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-29 19:12:59 +03:00
|
|
|
|
|
|
|
// Store string table entry states.
|
2014-04-14 20:13:19 +04:00
|
|
|
std::map<unsigned int, StringEntry> DynamicSectionStrings;
|
2008-02-28 00:26:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Configure the implementation template for 32-bit ELF files.
|
|
|
|
struct cmELFTypes32
|
|
|
|
{
|
|
|
|
typedef Elf32_Ehdr ELF_Ehdr;
|
|
|
|
typedef Elf32_Shdr ELF_Shdr;
|
2016-05-16 17:34:04 +03:00
|
|
|
typedef Elf32_Dyn ELF_Dyn;
|
2008-02-28 00:26:35 +03:00
|
|
|
typedef Elf32_Half ELF_Half;
|
2015-12-17 23:03:42 +03:00
|
|
|
typedef KWIML_INT_uint32_t tagtype;
|
2008-02-28 00:26:35 +03:00
|
|
|
static const char* GetName() { return "32-bit"; }
|
|
|
|
};
|
|
|
|
|
2014-03-09 05:35:00 +04:00
|
|
|
// Configure the implementation template for 64-bit ELF files.
|
2016-07-28 19:26:10 +03:00
|
|
|
#ifndef _SCO_DS
|
2008-02-28 00:26:35 +03:00
|
|
|
struct cmELFTypes64
|
|
|
|
{
|
|
|
|
typedef Elf64_Ehdr ELF_Ehdr;
|
|
|
|
typedef Elf64_Shdr ELF_Shdr;
|
2016-05-16 17:34:04 +03:00
|
|
|
typedef Elf64_Dyn ELF_Dyn;
|
2008-02-28 00:26:35 +03:00
|
|
|
typedef Elf64_Half ELF_Half;
|
2015-12-17 23:03:42 +03:00
|
|
|
typedef KWIML_INT_uint64_t tagtype;
|
2008-02-28 00:26:35 +03:00
|
|
|
static const char* GetName() { return "64-bit"; }
|
|
|
|
};
|
2016-07-28 19:26:10 +03:00
|
|
|
#endif
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Parser implementation template.
|
|
|
|
template <class Types>
|
2016-05-16 17:34:04 +03:00
|
|
|
class cmELFInternalImpl : public cmELFInternal
|
2008-02-28 00:26:35 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Copy the ELF file format types from our configuration parameter.
|
|
|
|
typedef typename Types::ELF_Ehdr ELF_Ehdr;
|
|
|
|
typedef typename Types::ELF_Shdr ELF_Shdr;
|
2016-05-16 17:34:04 +03:00
|
|
|
typedef typename Types::ELF_Dyn ELF_Dyn;
|
2008-02-28 00:26:35 +03:00
|
|
|
typedef typename Types::ELF_Half ELF_Half;
|
2014-04-14 20:13:19 +04:00
|
|
|
typedef typename Types::tagtype tagtype;
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Construct with a stream and byte swap indicator.
|
2016-06-28 17:17:52 +03:00
|
|
|
cmELFInternalImpl(cmELF* external, CM_AUTO_PTR<cmsys::ifstream>& fin,
|
2008-02-28 00:26:35 +03:00
|
|
|
ByteOrderType order);
|
|
|
|
|
|
|
|
// Return the number of sections as specified by the ELF header.
|
2016-06-23 02:02:39 +03:00
|
|
|
unsigned int GetNumberOfSections() const CM_OVERRIDE
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-02-28 00:26:35 +03:00
|
|
|
return static_cast<unsigned int>(this->ELFHeader.e_shnum);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
2008-04-14 23:02:24 +04:00
|
|
|
// Get the file position and size of a dynamic section entry.
|
2016-06-23 02:02:39 +03:00
|
|
|
unsigned int GetDynamicEntryCount() CM_OVERRIDE;
|
|
|
|
unsigned long GetDynamicEntryPosition(int j) CM_OVERRIDE;
|
2008-04-14 23:02:24 +04:00
|
|
|
|
2008-02-29 19:12:59 +03:00
|
|
|
// Lookup a string from the dynamic section with the given tag.
|
2016-06-23 02:02:39 +03:00
|
|
|
StringEntry const* GetDynamicSectionString(unsigned int tag) CM_OVERRIDE;
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Print information about the ELF file.
|
2016-06-23 02:02:39 +03:00
|
|
|
void PrintInfo(std::ostream& os) const CM_OVERRIDE
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-02-28 00:26:35 +03:00
|
|
|
os << "ELF " << Types::GetName();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->ByteOrder == ByteOrderMSB) {
|
2008-02-28 00:26:35 +03:00
|
|
|
os << " MSB";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (this->ByteOrder == ByteOrderLSB) {
|
2008-02-28 00:26:35 +03:00
|
|
|
os << " LSB";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
switch (this->ELFType) {
|
2008-02-28 16:32:05 +03:00
|
|
|
case cmELF::FileTypeInvalid:
|
|
|
|
os << " invalid file";
|
|
|
|
break;
|
2008-02-28 00:26:35 +03:00
|
|
|
case cmELF::FileTypeRelocatableObject:
|
|
|
|
os << " relocatable object";
|
|
|
|
break;
|
|
|
|
case cmELF::FileTypeExecutable:
|
|
|
|
os << " executable";
|
|
|
|
break;
|
|
|
|
case cmELF::FileTypeSharedLibrary:
|
|
|
|
os << " shared library";
|
|
|
|
break;
|
|
|
|
case cmELF::FileTypeCore:
|
|
|
|
os << " core file";
|
|
|
|
break;
|
2008-03-03 00:19:40 +03:00
|
|
|
case cmELF::FileTypeSpecificOS:
|
|
|
|
os << " os-specific type";
|
|
|
|
break;
|
|
|
|
case cmELF::FileTypeSpecificProc:
|
|
|
|
os << " processor-specific type";
|
|
|
|
break;
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
os << "\n";
|
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
void ByteSwap(ELF_Ehdr& elf_header)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-02-28 00:26:35 +03:00
|
|
|
cmELFByteSwap(elf_header.e_type);
|
|
|
|
cmELFByteSwap(elf_header.e_machine);
|
|
|
|
cmELFByteSwap(elf_header.e_version);
|
|
|
|
cmELFByteSwap(elf_header.e_entry);
|
|
|
|
cmELFByteSwap(elf_header.e_phoff);
|
|
|
|
cmELFByteSwap(elf_header.e_shoff);
|
|
|
|
cmELFByteSwap(elf_header.e_flags);
|
|
|
|
cmELFByteSwap(elf_header.e_ehsize);
|
|
|
|
cmELFByteSwap(elf_header.e_phentsize);
|
|
|
|
cmELFByteSwap(elf_header.e_phnum);
|
|
|
|
cmELFByteSwap(elf_header.e_shentsize);
|
|
|
|
cmELFByteSwap(elf_header.e_shnum);
|
|
|
|
cmELFByteSwap(elf_header.e_shstrndx);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
void ByteSwap(ELF_Shdr& sec_header)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-02-28 00:26:35 +03:00
|
|
|
cmELFByteSwap(sec_header.sh_name);
|
|
|
|
cmELFByteSwap(sec_header.sh_type);
|
|
|
|
cmELFByteSwap(sec_header.sh_flags);
|
|
|
|
cmELFByteSwap(sec_header.sh_addr);
|
|
|
|
cmELFByteSwap(sec_header.sh_offset);
|
|
|
|
cmELFByteSwap(sec_header.sh_size);
|
|
|
|
cmELFByteSwap(sec_header.sh_link);
|
|
|
|
cmELFByteSwap(sec_header.sh_info);
|
|
|
|
cmELFByteSwap(sec_header.sh_addralign);
|
|
|
|
cmELFByteSwap(sec_header.sh_entsize);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
void ByteSwap(ELF_Dyn& dyn)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-02-28 00:26:35 +03:00
|
|
|
cmELFByteSwap(dyn.d_tag);
|
2016-05-16 17:34:04 +03:00
|
|
|
switch (dyn.d_tag) {
|
|
|
|
case DT_NULL: /* dyn.d_un ignored */
|
|
|
|
break;
|
|
|
|
case DT_NEEDED:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
|
|
|
case DT_PLTRELSZ:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
|
|
|
case DT_PLTGOT:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
case DT_HASH:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
case DT_STRTAB:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
case DT_SYMTAB:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
case DT_RELA:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
case DT_RELASZ:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
|
|
|
case DT_RELAENT:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
|
|
|
case DT_STRSZ:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
|
|
|
case DT_SYMENT:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
|
|
|
case DT_INIT:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
case DT_FINI:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
case DT_SONAME:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
|
|
|
case DT_RPATH:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
|
|
|
case DT_SYMBOLIC: /* dyn.d_un ignored */
|
|
|
|
break;
|
|
|
|
case DT_REL:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
case DT_RELSZ:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
|
|
|
case DT_RELENT:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
|
|
|
case DT_PLTREL:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
|
|
|
case DT_DEBUG:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
case DT_TEXTREL: /* dyn.d_un ignored */
|
|
|
|
break;
|
|
|
|
case DT_JMPREL:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
2008-02-29 19:12:59 +03:00
|
|
|
#ifdef T_BIND_NOW
|
2016-05-16 17:34:04 +03:00
|
|
|
case T_BIND_NOW: /* dyn.d_un ignored */
|
|
|
|
break;
|
2008-02-29 19:12:59 +03:00
|
|
|
#endif
|
|
|
|
#ifdef DT_INIT_ARRAY
|
2016-05-16 17:34:04 +03:00
|
|
|
case DT_INIT_ARRAY:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
2008-02-29 19:12:59 +03:00
|
|
|
#endif
|
|
|
|
#ifdef DT_FINI_ARRAY
|
2016-05-16 17:34:04 +03:00
|
|
|
case DT_FINI_ARRAY:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
2008-02-29 19:12:59 +03:00
|
|
|
#endif
|
|
|
|
#ifdef DT_INIT_ARRAYSZ
|
2016-05-16 17:34:04 +03:00
|
|
|
case DT_INIT_ARRAYSZ:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
2008-02-29 19:12:59 +03:00
|
|
|
#endif
|
|
|
|
#ifdef DT_FINI_ARRAYSZ
|
2016-05-16 17:34:04 +03:00
|
|
|
case DT_FINI_ARRAYSZ:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
2008-02-29 19:12:59 +03:00
|
|
|
#endif
|
|
|
|
#ifdef DT_RUNPATH
|
2016-05-16 17:34:04 +03:00
|
|
|
case DT_RUNPATH:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
2008-02-29 19:12:59 +03:00
|
|
|
#endif
|
|
|
|
#ifdef DT_FLAGS
|
2016-05-16 17:34:04 +03:00
|
|
|
case DT_FLAGS:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
2008-02-29 19:12:59 +03:00
|
|
|
#endif
|
|
|
|
#ifdef DT_PREINIT_ARRAY
|
2016-05-16 17:34:04 +03:00
|
|
|
case DT_PREINIT_ARRAY:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_ptr);
|
|
|
|
break;
|
2008-02-29 19:12:59 +03:00
|
|
|
#endif
|
|
|
|
#ifdef DT_PREINIT_ARRAYSZ
|
2016-05-16 17:34:04 +03:00
|
|
|
case DT_PREINIT_ARRAYSZ:
|
|
|
|
cmELFByteSwap(dyn.d_un.d_val);
|
|
|
|
break;
|
2008-02-29 19:12:59 +03:00
|
|
|
#endif
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
2008-03-03 16:48:37 +03:00
|
|
|
bool FileTypeValid(ELF_Half et)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-03-03 16:48:37 +03:00
|
|
|
unsigned int eti = static_cast<unsigned int>(et);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (eti == ET_NONE || eti == ET_REL || eti == ET_EXEC || eti == ET_DYN ||
|
|
|
|
eti == ET_CORE) {
|
2008-03-03 16:48:37 +03:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-03-03 16:48:37 +03:00
|
|
|
#if defined(ET_LOOS) && defined(ET_HIOS)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (eti >= ET_LOOS && eti <= ET_HIOS) {
|
2008-03-03 16:48:37 +03:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-03-03 16:48:37 +03:00
|
|
|
#endif
|
|
|
|
#if defined(ET_LOPROC) && defined(ET_HIPROC)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (eti >= ET_LOPROC && eti <= ET_HIPROC) {
|
2008-03-03 16:48:37 +03:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-03-03 16:48:37 +03:00
|
|
|
#endif
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-03-03 16:48:37 +03:00
|
|
|
|
2008-02-28 00:26:35 +03:00
|
|
|
bool Read(ELF_Ehdr& x)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-03-03 00:19:40 +03:00
|
|
|
// Read the header from the file.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->Stream.read(reinterpret_cast<char*>(&x), sizeof(x))) {
|
2008-03-03 00:19:40 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-03-03 00:19:40 +03:00
|
|
|
|
|
|
|
// The byte order of ELF header fields may not match that of the
|
|
|
|
// processor-specific data. The header fields are ordered to
|
|
|
|
// match the target execution environment, so we may need to
|
|
|
|
// memorize the order of all platforms based on the e_machine
|
|
|
|
// value. As a heuristic, if the type is invalid but its
|
|
|
|
// swapped value is okay then flip our swap mode.
|
|
|
|
ELF_Half et = x.e_type;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->NeedSwap) {
|
2008-03-03 00:19:40 +03:00
|
|
|
cmELFByteSwap(et);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!this->FileTypeValid(et)) {
|
2008-03-03 00:19:40 +03:00
|
|
|
cmELFByteSwap(et);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->FileTypeValid(et)) {
|
2008-03-03 00:19:40 +03:00
|
|
|
// The previous byte order guess was wrong. Flip it.
|
|
|
|
this->NeedSwap = !this->NeedSwap;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-03-03 00:19:40 +03:00
|
|
|
|
|
|
|
// Fix the byte order of the header.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->NeedSwap) {
|
2008-02-28 00:26:35 +03:00
|
|
|
ByteSwap(x);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
bool Read(ELF_Shdr& x)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
if (this->Stream.read(reinterpret_cast<char*>(&x), sizeof(x)) &&
|
|
|
|
this->NeedSwap) {
|
2008-02-28 00:26:35 +03:00
|
|
|
ByteSwap(x);
|
|
|
|
}
|
2016-06-02 00:29:53 +03:00
|
|
|
return !this->Stream.fail();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
bool Read(ELF_Dyn& x)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
if (this->Stream.read(reinterpret_cast<char*>(&x), sizeof(x)) &&
|
|
|
|
this->NeedSwap) {
|
2008-02-28 00:26:35 +03:00
|
|
|
ByteSwap(x);
|
|
|
|
}
|
2016-06-02 00:29:53 +03:00
|
|
|
return !this->Stream.fail();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
bool LoadSectionHeader(ELF_Half i)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2008-02-28 00:26:35 +03:00
|
|
|
// Read the section header from the file.
|
|
|
|
this->Stream.seekg(this->ELFHeader.e_shoff +
|
|
|
|
this->ELFHeader.e_shentsize * i);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->Read(this->SectionHeaders[i])) {
|
2008-02-28 00:26:35 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Identify some important sections.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->SectionHeaders[i].sh_type == SHT_DYNAMIC) {
|
2008-02-28 00:26:35 +03:00
|
|
|
this->DynamicSectionIndex = i;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
bool LoadDynamicSection();
|
|
|
|
|
|
|
|
// Store the main ELF header.
|
|
|
|
ELF_Ehdr ELFHeader;
|
|
|
|
|
|
|
|
// Store all the section headers.
|
|
|
|
std::vector<ELF_Shdr> SectionHeaders;
|
|
|
|
|
|
|
|
// Store all entries of the DYNAMIC section.
|
|
|
|
std::vector<ELF_Dyn> DynamicSectionEntries;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Types>
|
2016-06-28 17:17:52 +03:00
|
|
|
cmELFInternalImpl<Types>::cmELFInternalImpl(cmELF* external,
|
|
|
|
CM_AUTO_PTR<cmsys::ifstream>& fin,
|
|
|
|
ByteOrderType order)
|
2016-05-16 17:34:04 +03:00
|
|
|
: cmELFInternal(external, fin, order)
|
2008-02-28 00:26:35 +03:00
|
|
|
{
|
|
|
|
// Read the main header.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->Read(this->ELFHeader)) {
|
2008-03-03 00:19:40 +03:00
|
|
|
this->SetErrorMessage("Failed to read main ELF header.");
|
2008-02-28 00:26:35 +03:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Determine the ELF file type.
|
2016-05-16 17:34:04 +03:00
|
|
|
switch (this->ELFHeader.e_type) {
|
2008-03-03 00:19:40 +03:00
|
|
|
case ET_NONE:
|
2008-03-03 16:48:37 +03:00
|
|
|
this->SetErrorMessage("ELF file type is NONE.");
|
2008-03-03 00:19:40 +03:00
|
|
|
return;
|
|
|
|
case ET_REL:
|
2008-02-28 00:26:35 +03:00
|
|
|
this->ELFType = cmELF::FileTypeRelocatableObject;
|
|
|
|
break;
|
2008-03-03 00:19:40 +03:00
|
|
|
case ET_EXEC:
|
2008-02-28 00:26:35 +03:00
|
|
|
this->ELFType = cmELF::FileTypeExecutable;
|
|
|
|
break;
|
2008-03-03 00:19:40 +03:00
|
|
|
case ET_DYN:
|
2008-02-28 00:26:35 +03:00
|
|
|
this->ELFType = cmELF::FileTypeSharedLibrary;
|
|
|
|
break;
|
2008-03-03 00:19:40 +03:00
|
|
|
case ET_CORE:
|
2008-02-28 00:26:35 +03:00
|
|
|
this->ELFType = cmELF::FileTypeCore;
|
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
default: {
|
2008-03-03 16:48:37 +03:00
|
|
|
unsigned int eti = static_cast<unsigned int>(this->ELFHeader.e_type);
|
|
|
|
#if defined(ET_LOOS) && defined(ET_HIOS)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (eti >= ET_LOOS && eti <= ET_HIOS) {
|
2008-03-03 00:19:40 +03:00
|
|
|
this->ELFType = cmELF::FileTypeSpecificOS;
|
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-03-03 16:48:37 +03:00
|
|
|
#endif
|
|
|
|
#if defined(ET_LOPROC) && defined(ET_HIPROC)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (eti >= ET_LOPROC && eti <= ET_HIPROC) {
|
2008-03-03 00:19:40 +03:00
|
|
|
this->ELFType = cmELF::FileTypeSpecificProc;
|
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-03-03 16:48:37 +03:00
|
|
|
#endif
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream e;
|
2008-03-03 16:48:37 +03:00
|
|
|
e << "Unknown ELF file type " << eti;
|
|
|
|
this->SetErrorMessage(e.str().c_str());
|
|
|
|
return;
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Load the section headers.
|
|
|
|
this->SectionHeaders.resize(this->ELFHeader.e_shnum);
|
2016-05-16 17:34:04 +03:00
|
|
|
for (ELF_Half i = 0; i < this->ELFHeader.e_shnum; ++i) {
|
|
|
|
if (!this->LoadSectionHeader(i)) {
|
2008-03-03 00:19:40 +03:00
|
|
|
this->SetErrorMessage("Failed to load section headers.");
|
2008-02-28 00:26:35 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Types>
|
|
|
|
bool cmELFInternalImpl<Types>::LoadDynamicSection()
|
|
|
|
{
|
|
|
|
// If there is no dynamic section we are done.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->DynamicSectionIndex < 0) {
|
2008-02-28 00:26:35 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// If the section was already loaded we are done.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->DynamicSectionEntries.empty()) {
|
2008-02-28 00:26:35 +03:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
2015-11-19 20:04:34 +03:00
|
|
|
// If there are no entries we are done.
|
2008-02-28 00:26:35 +03:00
|
|
|
ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
|
2016-05-16 17:34:04 +03:00
|
|
|
if (sec.sh_entsize == 0) {
|
2015-11-19 20:04:34 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-11-19 20:04:34 +03:00
|
|
|
|
|
|
|
// Allocate the dynamic section entries.
|
2009-10-06 16:33:05 +04:00
|
|
|
int n = static_cast<int>(sec.sh_size / sec.sh_entsize);
|
2008-02-28 00:26:35 +03:00
|
|
|
this->DynamicSectionEntries.resize(n);
|
|
|
|
|
|
|
|
// Read each entry.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (int j = 0; j < n; ++j) {
|
2008-02-28 00:26:35 +03:00
|
|
|
// Seek to the beginning of the section entry.
|
2016-05-16 17:34:04 +03:00
|
|
|
this->Stream.seekg(sec.sh_offset + sec.sh_entsize * j);
|
2008-02-28 00:26:35 +03:00
|
|
|
ELF_Dyn& dyn = this->DynamicSectionEntries[j];
|
|
|
|
|
|
|
|
// Try reading the entry.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->Read(dyn)) {
|
2008-02-28 00:26:35 +03:00
|
|
|
this->SetErrorMessage("Error reading entry from DYNAMIC section.");
|
|
|
|
this->DynamicSectionIndex = -1;
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-14 23:02:24 +04:00
|
|
|
template <class Types>
|
|
|
|
unsigned int cmELFInternalImpl<Types>::GetDynamicEntryCount()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->LoadDynamicSection()) {
|
2008-04-14 23:02:24 +04:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < this->DynamicSectionEntries.size(); ++i) {
|
|
|
|
if (this->DynamicSectionEntries[i].d_tag == DT_NULL) {
|
2008-04-14 23:02:24 +04:00
|
|
|
return i;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-06-25 16:48:59 +04:00
|
|
|
return static_cast<unsigned int>(this->DynamicSectionEntries.size());
|
2008-04-14 23:02:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Types>
|
|
|
|
unsigned long cmELFInternalImpl<Types>::GetDynamicEntryPosition(int j)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->LoadDynamicSection()) {
|
2008-04-14 23:02:24 +04:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (j < 0 || j >= static_cast<int>(this->DynamicSectionEntries.size())) {
|
2008-04-14 23:02:24 +04:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-14 23:02:24 +04:00
|
|
|
ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
|
2016-05-16 17:34:04 +03:00
|
|
|
return static_cast<unsigned long>(sec.sh_offset + sec.sh_entsize * j);
|
2008-04-14 23:02:24 +04:00
|
|
|
}
|
|
|
|
|
2008-02-28 00:26:35 +03:00
|
|
|
template <class Types>
|
2016-05-16 17:34:04 +03:00
|
|
|
cmELF::StringEntry const* cmELFInternalImpl<Types>::GetDynamicSectionString(
|
|
|
|
unsigned int tag)
|
2008-02-28 00:26:35 +03:00
|
|
|
{
|
|
|
|
// Short-circuit if already checked.
|
2014-04-14 20:13:19 +04:00
|
|
|
std::map<unsigned int, StringEntry>::iterator dssi =
|
2008-02-29 19:12:59 +03:00
|
|
|
this->DynamicSectionStrings.find(tag);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (dssi != this->DynamicSectionStrings.end()) {
|
|
|
|
if (dssi->second.Position > 0) {
|
2008-02-29 19:12:59 +03:00
|
|
|
return &dssi->second;
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
2016-06-27 23:44:16 +03:00
|
|
|
return CM_NULLPTR;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-29 19:12:59 +03:00
|
|
|
|
|
|
|
// Create an entry for this tag. Assume it is missing until found.
|
|
|
|
StringEntry& se = this->DynamicSectionStrings[tag];
|
|
|
|
se.Position = 0;
|
2008-03-01 20:50:42 +03:00
|
|
|
se.Size = 0;
|
2008-04-14 23:02:24 +04:00
|
|
|
se.IndexInSection = -1;
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Try reading the dynamic section.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->LoadDynamicSection()) {
|
2016-06-27 23:44:16 +03:00
|
|
|
return CM_NULLPTR;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Get the string table referenced by the DYNAMIC section.
|
|
|
|
ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
|
2016-05-16 17:34:04 +03:00
|
|
|
if (sec.sh_link >= this->SectionHeaders.size()) {
|
2008-02-28 00:26:35 +03:00
|
|
|
this->SetErrorMessage("Section DYNAMIC has invalid string table index.");
|
2016-06-27 23:44:16 +03:00
|
|
|
return CM_NULLPTR;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
ELF_Shdr const& strtab = this->SectionHeaders[sec.sh_link];
|
|
|
|
|
2008-02-29 19:12:59 +03:00
|
|
|
// Look for the requested entry.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (typename std::vector<ELF_Dyn>::iterator di =
|
|
|
|
this->DynamicSectionEntries.begin();
|
|
|
|
di != this->DynamicSectionEntries.end(); ++di) {
|
2008-02-28 00:26:35 +03:00
|
|
|
ELF_Dyn& dyn = *di;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (static_cast<tagtype>(dyn.d_tag) == static_cast<tagtype>(tag)) {
|
2008-03-01 20:50:42 +03:00
|
|
|
// We found the tag requested.
|
|
|
|
// Make sure the position given is within the string section.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (dyn.d_un.d_val >= strtab.sh_size) {
|
2008-03-01 20:50:42 +03:00
|
|
|
this->SetErrorMessage("Section DYNAMIC references string beyond "
|
|
|
|
"the end of its string section.");
|
2016-06-27 23:44:16 +03:00
|
|
|
return CM_NULLPTR;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-29 19:12:59 +03:00
|
|
|
|
2008-03-01 20:50:42 +03:00
|
|
|
// Seek to the position reported by the entry.
|
|
|
|
unsigned long first = static_cast<unsigned long>(dyn.d_un.d_val);
|
|
|
|
unsigned long last = first;
|
|
|
|
unsigned long end = static_cast<unsigned long>(strtab.sh_size);
|
|
|
|
this->Stream.seekg(strtab.sh_offset + first);
|
|
|
|
|
|
|
|
// Read the string. It may be followed by more than one NULL
|
|
|
|
// terminator. Count the total size of the region allocated to
|
|
|
|
// the string. This assumes that the next string in the table
|
|
|
|
// is non-empty, but the "chrpath" tool makes the same
|
|
|
|
// assumption.
|
|
|
|
bool terminated = false;
|
2008-02-28 00:26:35 +03:00
|
|
|
char c;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (last != end && this->Stream.get(c) && !(terminated && c)) {
|
2008-03-01 20:50:42 +03:00
|
|
|
++last;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (c) {
|
2008-03-01 20:50:42 +03:00
|
|
|
se.Value += c;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-03-01 20:50:42 +03:00
|
|
|
terminated = true;
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-29 19:12:59 +03:00
|
|
|
|
|
|
|
// Make sure the whole value was read.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->Stream) {
|
2008-02-29 19:12:59 +03:00
|
|
|
this->SetErrorMessage("Dynamic section specifies unreadable RPATH.");
|
|
|
|
se.Value = "";
|
2016-06-27 23:44:16 +03:00
|
|
|
return CM_NULLPTR;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-29 19:12:59 +03:00
|
|
|
|
|
|
|
// The value has been read successfully. Report it.
|
2008-03-01 20:50:42 +03:00
|
|
|
se.Position = static_cast<unsigned long>(strtab.sh_offset + first);
|
|
|
|
se.Size = last - first;
|
2010-09-10 22:42:09 +04:00
|
|
|
se.IndexInSection =
|
|
|
|
static_cast<int>(di - this->DynamicSectionEntries.begin());
|
2008-02-29 19:12:59 +03:00
|
|
|
return &se;
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-06-27 23:44:16 +03:00
|
|
|
return CM_NULLPTR;
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
// External class implementation.
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmELF::cmELF(const char* fname)
|
2016-06-27 23:44:16 +03:00
|
|
|
: Internal(CM_NULLPTR)
|
2008-02-28 00:26:35 +03:00
|
|
|
{
|
|
|
|
// Try to open the file.
|
2016-06-28 17:17:52 +03:00
|
|
|
CM_AUTO_PTR<cmsys::ifstream> fin(new cmsys::ifstream(fname));
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Quit now if the file could not be opened.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!fin.get() || !*fin) {
|
2008-02-28 00:26:35 +03:00
|
|
|
this->ErrorMessage = "Error opening input file.";
|
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Read the ELF identification block.
|
|
|
|
char ident[EI_NIDENT];
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!fin->read(ident, EI_NIDENT)) {
|
2008-02-28 00:26:35 +03:00
|
|
|
this->ErrorMessage = "Error reading ELF identification.";
|
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!fin->seekg(0)) {
|
2008-02-28 00:26:35 +03:00
|
|
|
this->ErrorMessage = "Error seeking to beginning of file.";
|
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Verify the ELF identification.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!(ident[EI_MAG0] == ELFMAG0 && ident[EI_MAG1] == ELFMAG1 &&
|
|
|
|
ident[EI_MAG2] == ELFMAG2 && ident[EI_MAG3] == ELFMAG3)) {
|
2008-02-28 00:26:35 +03:00
|
|
|
this->ErrorMessage = "File does not have a valid ELF identification.";
|
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Check the byte order in which the rest of the file is encoded.
|
|
|
|
cmELFInternal::ByteOrderType order;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (ident[EI_DATA] == ELFDATA2LSB) {
|
2008-02-28 00:26:35 +03:00
|
|
|
// File is LSB.
|
2016-05-16 17:34:04 +03:00
|
|
|
order = cmELFInternal::ByteOrderLSB;
|
|
|
|
} else if (ident[EI_DATA] == ELFDATA2MSB) {
|
2008-02-28 00:26:35 +03:00
|
|
|
// File is MSB.
|
2016-05-16 17:34:04 +03:00
|
|
|
order = cmELFInternal::ByteOrderMSB;
|
|
|
|
} else {
|
2008-02-28 00:26:35 +03:00
|
|
|
this->ErrorMessage = "ELF file is not LSB or MSB encoded.";
|
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
|
|
|
|
// Check the class of the file and construct the corresponding
|
|
|
|
// parser implementation.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (ident[EI_CLASS] == ELFCLASS32) {
|
2008-02-28 00:26:35 +03:00
|
|
|
// 32-bit ELF
|
|
|
|
this->Internal = new cmELFInternalImpl<cmELFTypes32>(this, fin, order);
|
2016-07-28 19:26:10 +03:00
|
|
|
}
|
|
|
|
#ifndef _SCO_DS
|
|
|
|
else if (ident[EI_CLASS] == ELFCLASS64) {
|
2008-02-28 00:26:35 +03:00
|
|
|
// 64-bit ELF
|
|
|
|
this->Internal = new cmELFInternalImpl<cmELFTypes64>(this, fin, order);
|
2016-07-28 19:26:10 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
2008-02-28 00:26:35 +03:00
|
|
|
this->ErrorMessage = "ELF file class is not 32-bit or 64-bit.";
|
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmELF::~cmELF()
|
|
|
|
{
|
|
|
|
delete this->Internal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmELF::Valid() const
|
|
|
|
{
|
|
|
|
return this->Internal && this->Internal->GetFileType() != FileTypeInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmELF::FileType cmELF::GetFileType() const
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Valid()) {
|
2008-02-28 00:26:35 +03:00
|
|
|
return this->Internal->GetFileType();
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-02-28 00:26:35 +03:00
|
|
|
return FileTypeInvalid;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int cmELF::GetNumberOfSections() const
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Valid()) {
|
2008-02-28 00:26:35 +03:00
|
|
|
return this->Internal->GetNumberOfSections();
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-02-28 00:26:35 +03:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
|
|
|
|
2008-04-14 23:02:24 +04:00
|
|
|
unsigned int cmELF::GetDynamicEntryCount() const
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Valid()) {
|
2008-04-14 23:02:24 +04:00
|
|
|
return this->Internal->GetDynamicEntryCount();
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-04-14 23:02:24 +04:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-14 23:02:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long cmELF::GetDynamicEntryPosition(int index) const
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Valid()) {
|
2008-04-14 23:02:24 +04:00
|
|
|
return this->Internal->GetDynamicEntryPosition(index);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-04-14 23:02:24 +04:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-14 23:02:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cmELF::ReadBytes(unsigned long pos, unsigned long size, char* buf) const
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Valid()) {
|
2008-04-14 23:02:24 +04:00
|
|
|
return this->Internal->ReadBytes(pos, size, buf);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-04-14 23:02:24 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-14 23:02:24 +04:00
|
|
|
}
|
|
|
|
|
2008-02-28 00:26:35 +03:00
|
|
|
bool cmELF::GetSOName(std::string& soname)
|
2008-02-29 19:12:59 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (StringEntry const* se = this->GetSOName()) {
|
2008-02-29 19:12:59 +03:00
|
|
|
soname = se->Value;
|
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-02-29 19:12:59 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-29 19:12:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmELF::StringEntry const* cmELF::GetSOName()
|
2008-02-28 00:26:35 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Valid() &&
|
|
|
|
this->Internal->GetFileType() == cmELF::FileTypeSharedLibrary) {
|
2008-02-29 19:12:59 +03:00
|
|
|
return this->Internal->GetSOName();
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2016-06-27 23:44:16 +03:00
|
|
|
return CM_NULLPTR;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-29 19:12:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmELF::StringEntry const* cmELF::GetRPath()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Valid() &&
|
|
|
|
(this->Internal->GetFileType() == cmELF::FileTypeExecutable ||
|
|
|
|
this->Internal->GetFileType() == cmELF::FileTypeSharedLibrary)) {
|
2008-02-29 19:12:59 +03:00
|
|
|
return this->Internal->GetRPath();
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2016-06-27 23:44:16 +03:00
|
|
|
return CM_NULLPTR;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-29 19:12:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmELF::StringEntry const* cmELF::GetRunPath()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Valid() &&
|
|
|
|
(this->Internal->GetFileType() == cmELF::FileTypeExecutable ||
|
|
|
|
this->Internal->GetFileType() == cmELF::FileTypeSharedLibrary)) {
|
2008-02-29 19:12:59 +03:00
|
|
|
return this->Internal->GetRunPath();
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2016-06-27 23:44:16 +03:00
|
|
|
return CM_NULLPTR;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmELF::PrintInfo(std::ostream& os) const
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Valid()) {
|
2008-02-28 00:26:35 +03:00
|
|
|
this->Internal->PrintInfo(os);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-02-28 00:26:35 +03:00
|
|
|
os << "Not a valid ELF file.\n";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
}
|