/*
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 "ELF32.h"
#include "Util.h"
#include "Debugger.h"
#include "DebuggerCoreInterface.h"

#include <QVector>
#include <QFile>
#include <QDebug>
#include <cstring>
#include <link.h>

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

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

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

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

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

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

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
edb::address_t ELF32::debugPointer() {
	readHeader();
	const edb::address_t section_offset	= m_Header->e_phoff;
	const std::size_t count				= m_Header->e_phnum;
		
	Elf32_Phdr section_header;
	for(std::size_t i = 0; i < count; ++i) {
		if(edb::v1::debuggerBase->readBytes(m_Region.start + (section_offset + i * sizeof(Elf32_Phdr)), &section_header, sizeof(Elf32_Phdr))) {
			if(section_header.p_type == PT_DYNAMIC) {
				try {
					QVector<quint8> buf(section_header.p_memsz);
					if(edb::v1::debuggerBase->readBytes(section_header.p_vaddr, &buf[0], section_header.p_memsz)) {
						const Elf32_Dyn *dynamic = reinterpret_cast<Elf32_Dyn *>(&buf[0]);
						while(dynamic->d_tag != DT_NULL) {
							if(dynamic->d_tag == DT_DEBUG) {
								return dynamic->d_un.d_val;
							}
							++dynamic;
						}
					}
				} catch(const std::bad_alloc &) {
					qDebug() << "[ELF32::debugPointer] no more memory";
					return 0;
				}
			}
		}
	}
	
	return 0;
}
