[project] Lose the battle between tabs & spaces
I'm a tabs guy. I like tabs, it's an elegant way to represent indentation instead of brute-forcing it. But I have to admit that the world seems to be going towards spaces, and tooling tends not to play nice with tabs. So here we go, changing the whole repo to spaces since I'm getting tired of all the inconsistent formatting.
This commit is contained in:
committed by
Justin C. Miller
parent
d36b2d8057
commit
8f529046a9
@@ -9,36 +9,36 @@ namespace elf {
|
||||
inline const file_header * fh(const void *data) { return reinterpret_cast<const file_header*>(data); }
|
||||
|
||||
file::file(const void *data, size_t size) :
|
||||
m_programs(offset_ptr<program_header>(data, fh(data)->ph_offset), fh(data)->ph_entsize, fh(data)->ph_num),
|
||||
m_sections(offset_ptr<section_header>(data, fh(data)->sh_offset), fh(data)->sh_entsize, fh(data)->sh_num),
|
||||
m_data(data),
|
||||
m_size(size)
|
||||
m_programs(offset_ptr<program_header>(data, fh(data)->ph_offset), fh(data)->ph_entsize, fh(data)->ph_num),
|
||||
m_sections(offset_ptr<section_header>(data, fh(data)->sh_offset), fh(data)->sh_entsize, fh(data)->sh_num),
|
||||
m_data(data),
|
||||
m_size(size)
|
||||
{
|
||||
}
|
||||
|
||||
bool
|
||||
file::valid() const
|
||||
{
|
||||
if (m_size < sizeof(file_header))
|
||||
return false;
|
||||
if (m_size < sizeof(file_header))
|
||||
return false;
|
||||
|
||||
const file_header *fheader = header();
|
||||
const file_header *fheader = header();
|
||||
|
||||
return
|
||||
fheader->magic == expected_magic &&
|
||||
fheader->word_size == wordsize::bits64 &&
|
||||
fheader->endianness == encoding::lsb &&
|
||||
fheader->os_abi == osabi::sysV &&
|
||||
fheader->file_type == filetype::executable &&
|
||||
fheader->machine_type == machine::x64 &&
|
||||
fheader->ident_version == 1 &&
|
||||
fheader->version == 1;
|
||||
return
|
||||
fheader->magic == expected_magic &&
|
||||
fheader->word_size == wordsize::bits64 &&
|
||||
fheader->endianness == encoding::lsb &&
|
||||
fheader->os_abi == osabi::sysV &&
|
||||
fheader->file_type == filetype::executable &&
|
||||
fheader->machine_type == machine::x64 &&
|
||||
fheader->ident_version == 1 &&
|
||||
fheader->version == 1;
|
||||
}
|
||||
|
||||
uintptr_t
|
||||
file::entrypoint() const
|
||||
{
|
||||
return static_cast<uintptr_t>(header()->entrypoint);
|
||||
return static_cast<uintptr_t>(header()->entrypoint);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -14,62 +14,62 @@ template <typename T>
|
||||
class subheaders
|
||||
{
|
||||
public:
|
||||
using iterator = const_offset_iterator<T>;
|
||||
using iterator = const_offset_iterator<T>;
|
||||
|
||||
subheaders(const T *start, size_t size, unsigned count) :
|
||||
m_start(start), m_size(size), m_count(count) {}
|
||||
subheaders(const T *start, size_t size, unsigned count) :
|
||||
m_start(start), m_size(size), m_count(count) {}
|
||||
|
||||
inline size_t size() const { return m_size; }
|
||||
inline unsigned count() const { return m_count; }
|
||||
inline size_t size() const { return m_size; }
|
||||
inline unsigned count() const { return m_count; }
|
||||
|
||||
inline const T & operator [] (int i) const { return *offset_ptr<T>(m_start, m_size*i); }
|
||||
inline const iterator begin() const { return iterator(m_start, m_size); }
|
||||
inline const iterator end() const { return offset_ptr<T>(m_start, m_size*m_count); }
|
||||
inline const T & operator [] (int i) const { return *offset_ptr<T>(m_start, m_size*i); }
|
||||
inline const iterator begin() const { return iterator(m_start, m_size); }
|
||||
inline const iterator end() const { return offset_ptr<T>(m_start, m_size*m_count); }
|
||||
|
||||
private:
|
||||
const T *m_start;
|
||||
size_t m_size;
|
||||
unsigned m_count;
|
||||
const T *m_start;
|
||||
size_t m_size;
|
||||
unsigned m_count;
|
||||
};
|
||||
|
||||
/// Represents a full ELF file's data
|
||||
class file
|
||||
{
|
||||
public:
|
||||
/// Constructor: Create an elf object out of ELF data in memory
|
||||
/// \arg data The ELF data to read
|
||||
/// \arg size Size of the ELF data, in bytes
|
||||
file(const void *data, size_t size);
|
||||
/// Constructor: Create an elf object out of ELF data in memory
|
||||
/// \arg data The ELF data to read
|
||||
/// \arg size Size of the ELF data, in bytes
|
||||
file(const void *data, size_t size);
|
||||
|
||||
/// Check the validity of the ELF data
|
||||
/// \returns true for valid ELF data
|
||||
bool valid() const;
|
||||
/// Check the validity of the ELF data
|
||||
/// \returns true for valid ELF data
|
||||
bool valid() const;
|
||||
|
||||
/// Get the entrypoint address of the program image
|
||||
/// \returns A pointer to the entrypoint of the program
|
||||
uintptr_t entrypoint() const;
|
||||
/// Get the entrypoint address of the program image
|
||||
/// \returns A pointer to the entrypoint of the program
|
||||
uintptr_t entrypoint() const;
|
||||
|
||||
/// Get the base address of the program in memory
|
||||
inline uintptr_t base() const {
|
||||
return reinterpret_cast<uintptr_t>(m_data);
|
||||
}
|
||||
/// Get the base address of the program in memory
|
||||
inline uintptr_t base() const {
|
||||
return reinterpret_cast<uintptr_t>(m_data);
|
||||
}
|
||||
|
||||
/// Get the ELF program headers
|
||||
inline const subheaders<program_header> & programs() const { return m_programs; }
|
||||
/// Get the ELF program headers
|
||||
inline const subheaders<program_header> & programs() const { return m_programs; }
|
||||
|
||||
/// Get the ELF section headers
|
||||
inline const subheaders<section_header> & sections() const { return m_sections; }
|
||||
/// Get the ELF section headers
|
||||
inline const subheaders<section_header> & sections() const { return m_sections; }
|
||||
|
||||
inline const file_header * header() const {
|
||||
return reinterpret_cast<const file_header *>(m_data);
|
||||
}
|
||||
inline const file_header * header() const {
|
||||
return reinterpret_cast<const file_header *>(m_data);
|
||||
}
|
||||
|
||||
private:
|
||||
subheaders<program_header> m_programs;
|
||||
subheaders<section_header> m_sections;
|
||||
subheaders<program_header> m_programs;
|
||||
subheaders<section_header> m_sections;
|
||||
|
||||
const void *m_data;
|
||||
size_t m_size;
|
||||
const void *m_data;
|
||||
size_t m_size;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -11,45 +11,45 @@ enum class machine : uint16_t { none, x64 = 0x3e };
|
||||
|
||||
enum class filetype : uint16_t
|
||||
{
|
||||
none,
|
||||
relocatable,
|
||||
executable,
|
||||
shared,
|
||||
core
|
||||
none,
|
||||
relocatable,
|
||||
executable,
|
||||
shared,
|
||||
core
|
||||
};
|
||||
|
||||
|
||||
struct file_header
|
||||
{
|
||||
uint32_t magic;
|
||||
uint32_t magic;
|
||||
|
||||
wordsize word_size;
|
||||
encoding endianness;
|
||||
uint8_t ident_version;
|
||||
osabi os_abi;
|
||||
wordsize word_size;
|
||||
encoding endianness;
|
||||
uint8_t ident_version;
|
||||
osabi os_abi;
|
||||
|
||||
uint64_t reserved;
|
||||
uint64_t reserved;
|
||||
|
||||
filetype file_type;
|
||||
machine machine_type;
|
||||
filetype file_type;
|
||||
machine machine_type;
|
||||
|
||||
uint32_t version;
|
||||
uint32_t version;
|
||||
|
||||
uint64_t entrypoint;
|
||||
uint64_t ph_offset;
|
||||
uint64_t sh_offset;
|
||||
uint64_t entrypoint;
|
||||
uint64_t ph_offset;
|
||||
uint64_t sh_offset;
|
||||
|
||||
uint32_t flags;
|
||||
uint32_t flags;
|
||||
|
||||
uint16_t eh_size;
|
||||
uint16_t eh_size;
|
||||
|
||||
uint16_t ph_entsize;
|
||||
uint16_t ph_num;
|
||||
uint16_t ph_entsize;
|
||||
uint16_t ph_num;
|
||||
|
||||
uint16_t sh_entsize;
|
||||
uint16_t sh_num;
|
||||
uint16_t sh_entsize;
|
||||
uint16_t sh_num;
|
||||
|
||||
uint16_t sh_str_idx;
|
||||
uint16_t sh_str_idx;
|
||||
} __attribute__ ((packed));
|
||||
|
||||
|
||||
@@ -57,40 +57,40 @@ enum class segment_type : uint32_t { null, load, dynamic, interpreter, note };
|
||||
|
||||
struct program_header
|
||||
{
|
||||
segment_type type;
|
||||
uint32_t flags;
|
||||
uint64_t offset;
|
||||
segment_type type;
|
||||
uint32_t flags;
|
||||
uint64_t offset;
|
||||
|
||||
uint64_t vaddr;
|
||||
uint64_t paddr;
|
||||
uint64_t vaddr;
|
||||
uint64_t paddr;
|
||||
|
||||
uint64_t file_size;
|
||||
uint64_t mem_size;
|
||||
uint64_t file_size;
|
||||
uint64_t mem_size;
|
||||
|
||||
uint64_t align;
|
||||
uint64_t align;
|
||||
} __attribute__ ((packed));
|
||||
|
||||
|
||||
enum class section_type : uint32_t { null, progbits };
|
||||
enum class section_flags : uint64_t
|
||||
{
|
||||
write = 0x01,
|
||||
alloc = 0x02,
|
||||
exec = 0x04,
|
||||
write = 0x01,
|
||||
alloc = 0x02,
|
||||
exec = 0x04,
|
||||
};
|
||||
|
||||
struct section_header
|
||||
{
|
||||
uint32_t name_offset;
|
||||
section_type type;
|
||||
section_flags flags;
|
||||
uint64_t addr;
|
||||
uint64_t offset;
|
||||
uint64_t size;
|
||||
uint32_t link;
|
||||
uint32_t info;
|
||||
uint64_t align;
|
||||
uint64_t entry_size;
|
||||
uint32_t name_offset;
|
||||
section_type type;
|
||||
section_flags flags;
|
||||
uint64_t addr;
|
||||
uint64_t offset;
|
||||
uint64_t size;
|
||||
uint32_t link;
|
||||
uint32_t info;
|
||||
uint64_t align;
|
||||
uint64_t entry_size;
|
||||
} __attribute__ ((packed));
|
||||
|
||||
} // namespace elf
|
||||
|
||||
Reference in New Issue
Block a user