File: ELFXX.cpp

package info (click to toggle)
edb-debugger 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,124 kB
  • sloc: cpp: 46,241; xml: 4,998; ansic: 3,088; sh: 52; asm: 33; makefile: 5
file content (201 lines) | stat: -rw-r--r-- 5,126 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
Copyright (C) 2006 - 2015 Evan Teran
                          evan.teran@gmail.com

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, see <http://www.gnu.org/licenses/>.
*/

#include "ELFXX.h"
#include "IDebugger.h"
#include "IProcess.h"
#include "IRegion.h"
#include "MemoryRegions.h"
#include "Util.h"
#include "edb.h"
#include "libELF/elf_phdr.h"
#include "string_hash.h"

#include <QDebug>
#include <QFile>
#include <QVector>
#include <cstdint>
#include <cstring>

namespace BinaryInfoPlugin {

class ELFBinaryException : public std::exception {
	const char *what() const noexcept override = 0;
};

class InvalidArguments : public ELFBinaryException {
public:
	const char *what() const noexcept override {
		return "Invalid Arguments";
	}
};

class ReadFailure : public ELFBinaryException {
public:
	const char *what() const noexcept override {
		return "Read Failure";
	}
};

class InvalidELF : public ELFBinaryException {
public:
	const char *what() const noexcept override {
		return "Invalid ELF";
	}
};

class InvalidArchitecture : public ELFBinaryException {
public:
	const char *what() const noexcept override {
		return "Invalid Architecture";
	}
};

template <class ElfHeader>
ELFXX<ElfHeader>::ELFXX(const std::shared_ptr<IRegion> &region)
	: region_(region) {

	using phdr_type = typename ElfHeader::elf_phdr;

	if (!region_) {
		throw InvalidArguments();
	}

	IProcess *const process = edb::v1::debugger_core->process();
	if (!process) {
		throw ReadFailure();
	}

	if (!process->readBytes(region_->start(), &header_, sizeof(ElfHeader))) {
		throw ReadFailure();
	}

	validateHeader();

	headers_.push_back({region_->start(), header_.e_ehsize});
	headers_.push_back({region_->start() + header_.e_phoff, static_cast<size_t>(header_.e_phentsize * header_.e_phnum)});

	auto phdr_size = header_.e_phentsize;

	if (phdr_size < sizeof(phdr_type)) {
		qDebug() << QString::number(region_->start(), 16) << "program header size less than expected";
		baseAddress_ = region_->start();
		return;
	}

	phdr_type phdr;

	auto phdr_base        = region_->start() + header_.e_phoff;
	edb::address_t lowest = ULLONG_MAX;

	if (header_.e_type == ET_EXEC) {

		// iterate all of the program headers
		for (uint16_t entry = 0; entry < header_.e_phnum; ++entry) {

			if (!process->readBytes(phdr_base + (phdr_size * entry), &phdr, sizeof(phdr_type))) {
				qDebug() << "Failed to read program header";
				break;
			}

			if (phdr.p_type == PT_LOAD && phdr.p_vaddr < lowest) {
				lowest = phdr.p_vaddr;
				// NOTE(eteran): they are defined to be in ascending order of vaddr
				break;
			}
		}
	} else if (header_.e_type == ET_DYN) {

		const QString process_executable = edb::v1::debugger_core->process()->name();
		for (const std::shared_ptr<IRegion> &r : edb::v1::memory_regions().regions()) {
			if (r->executable() && r->name() == region->name()) {
				lowest = std::min(lowest, r->start());
			}
		}
	}

	if (lowest == ULLONG_MAX) {
		qDebug() << "binary base address not found. Assuming " << QString::number(region_->start(), 16);
		baseAddress_ = region->start();
	} else {
		baseAddress_ = lowest;
	}
}

template <class ElfHeader>
/**
 * @brief ELFXX<ElfHeader>::headerSize
 * @return the number of bytes in this executable's header
 */
size_t ELFXX<ElfHeader>::headerSize() const {
	size_t size = header_.e_ehsize;
	// Do the program headers immediately follow the ELF header?
	if (size == header_.e_phoff) {
		size += header_.e_phentsize * header_.e_phnum;
	}
	return size;
}

/**
 * @brief ELFXX<ElfHeader>::headers
 * @return a list of all headers in this binary
 */
template <class ElfHeader>
std::vector<IBinary::Header> ELFXX<ElfHeader>::headers() const {
	return headers_;
}

/**
 * @brief ELFXX<ElfHeader>::validateHeader
 *
 * ensures that the header that we read was valid
 */
template <class ElfHeader>
void ELFXX<ElfHeader>::validateHeader() {
	if (std::memcmp(header_.e_ident, ELFMAG, SELFMAG) != 0) {
		throw InvalidELF();
	}
	if (header_.e_ident[EI_CLASS] != ElfHeader::ELFCLASS) {
		throw InvalidArchitecture();
	}
}

/**
 * @brief ELFXX<ElfHeader>::entryPoint
 * @return the entry point if any of the binary
 */
template <class ElfHeader>
edb::address_t ELFXX<ElfHeader>::entryPoint() {
	return header_.e_entry + baseAddress_;
}

/**
 * @brief ELFXX<ElfHeader>::header
 * @return a copy of the file header or nullptr if the region wasn't a valid,
 * known binary type
 */
template <class ElfHeader>
const void *ELFXX<ElfHeader>::header() const {
	return &header_;
}

// explicit instantiations
template class ELFXX<elf32_header>;
template class ELFXX<elf64_header>;

}