/*
Copyright (C) 2006 - 2009 Evan Teran
                   eteran@alum.rit.edu

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "ELF64.h"
#include "Util.h"
#include "Debugger.h"
#include "DebuggerCoreInterface.h"
#include <QFile>
#include <cstring>
#include <link.h>

//------------------------------------------------------------------------------
// Name: ELF64(const MemRegion &region)
// Desc: constructor
//------------------------------------------------------------------------------
ELF64::ELF64(const MemRegion &region) : BinaryInfo(region), m_Header(0) {

}

//------------------------------------------------------------------------------
// Name: ~ELF64()
// Desc: deconstructor
//------------------------------------------------------------------------------
ELF64::~ELF64() {
	delete m_Header;
}

//------------------------------------------------------------------------------
// Name: validateHeader()
// Desc: returns true if this file matches this particular info class
//------------------------------------------------------------------------------
bool ELF64::validateHeader() {
	readHeader();
	if(std::memcmp(m_Header->e_ident, ELFMAG, SELFMAG) == 0) {
		return m_Header->e_ident[EI_CLASS] == ELFCLASS64;
	}
	return false;
}

//------------------------------------------------------------------------------
// Name: nativeBinary
// Desc: returns true if this binary is native to the arch EDB was built for
//------------------------------------------------------------------------------
bool ELF64::nativeBinary() {
#ifdef EDB_X86_64
	return true;
#else
	return false;
#endif
}

//------------------------------------------------------------------------------
// Name: entryPoint()
// Desc: returns the entry point if any of the binary
//------------------------------------------------------------------------------
edb::address_t ELF64::entryPoint() {
	readHeader();
	return m_Header->e_entry;
}

//------------------------------------------------------------------------------
// Name: readHeader()
// Desc: reads in enough of the file to get the header
//------------------------------------------------------------------------------
void ELF64::readHeader() {
	if(m_Header == 0) {
		m_Header = new Elf64_Ehdr;
		
		const bool ok = edb::v1::debuggerBase->readBytes(m_Region.start, m_Header, sizeof(Elf64_Ehdr));
		if(!ok) {
			std::memset(m_Header, 0, sizeof(Elf64_Ehdr));
		}
	}
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
edb::address_t ELF64::debugPointer() {
	return 0;
}
