ELF: Run clang-format on everything.

This commit is contained in:
Andreas Kling 2019-06-07 17:12:30 +02:00
parent e42c3b4fd7
commit 9145917bf0
Notes: sideshowbarker 2024-07-19 13:41:06 +09:00
5 changed files with 551 additions and 547 deletions

View file

@ -14,12 +14,18 @@ ELFImage::~ELFImage()
static const char* object_file_type_to_string(Elf32_Half type)
{
switch (type) {
case ET_NONE: return "None";
case ET_REL: return "Relocatable";
case ET_EXEC: return "Executable";
case ET_DYN: return "Shared object";
case ET_CORE: return "Core";
default: return "(?)";
case ET_NONE:
return "None";
case ET_REL:
return "Relocatable";
case ET_EXEC:
return "Executable";
case ET_DYN:
return "Shared object";
case ET_CORE:
return "Core";
default:
return "(?)";
}
}

View file

@ -1,9 +1,9 @@
#pragma once
#include <AK/OwnPtr.h>
#include <AK/HashMap.h>
#include <AK/AKString.h>
#include <AK/ELF/exec_elf.h>
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
class ELFImage {
public:
@ -27,7 +27,7 @@ public:
{
}
~Symbol() { }
~Symbol() {}
const char* name() const { return m_image.table_string(m_sym.st_name); }
unsigned section_index() const { return m_sym.st_shndx; }
@ -51,7 +51,7 @@ public:
, m_program_header_index(program_header_index)
{
}
~ProgramHeader() { }
~ProgramHeader() {}
unsigned index() const { return m_program_header_index; }
dword type() const { return m_program_header.p_type; }
@ -65,6 +65,7 @@ public:
bool is_writable() const { return flags() & PF_W; }
bool is_executable() const { return flags() & PF_X; }
const char* raw_data() const { return m_image.raw_data(m_program_header.p_offset); }
private:
const ELFImage& m_image;
const Elf32_Phdr& m_program_header;
@ -79,7 +80,7 @@ public:
, m_section_index(sectionIndex)
{
}
~Section() { }
~Section() {}
const char* name() const { return m_image.section_header_table_string(m_section_header.sh_name); }
unsigned type() const { return m_section_header.sh_type; }
@ -109,10 +110,14 @@ public:
const Section section(unsigned) const;
const ProgramHeader program_header(unsigned const) const;
template<typename F> void for_each_section(F) const;
template<typename F> void for_each_section_of_type(unsigned, F) const;
template<typename F> void for_each_symbol(F) const;
template<typename F> void for_each_program_header(F) const;
template<typename F>
void for_each_section(F) const;
template<typename F>
void for_each_section_of_type(unsigned, F) const;
template<typename F>
void for_each_symbol(F) const;
template<typename F>
void for_each_program_header(F) const;
bool is_executable() const { return header().e_type == ET_EXEC; }
bool is_relocatable() const { return header().e_type == ET_REL; }
@ -158,7 +163,7 @@ template<typename F>
inline void ELFImage::for_each_symbol(F func) const
{
for (unsigned i = 0; i < symbol_count(); ++i) {
if (func(symbol(i)) == IterationDecision::Abort)
if (func(symbol(i)) == IterationDecision::Break)
break;
}
}

View file

@ -1,6 +1,6 @@
#include "ELFLoader.h"
#include <AK/kstdio.h>
#include <AK/QuickSort.h>
#include <AK/kstdio.h>
//#define ELFLOADER_DEBUG
@ -30,7 +30,7 @@ bool ELFLoader::load()
bool ELFLoader::layout()
{
bool failed = false;
m_image.for_each_program_header([&] (const ELFImage::ProgramHeader& program_header) {
m_image.for_each_program_header([&](const ELFImage::ProgramHeader& program_header) {
if (program_header.type() != PT_LOAD)
return;
#ifdef ELFLOADER_DEBUG
@ -43,8 +43,7 @@ bool ELFLoader::layout()
program_header.alignment(),
program_header.is_readable(),
program_header.is_writable(),
String::format("elf-alloc-%s%s", program_header.is_readable() ? "r" : "", program_header.is_writable() ? "w" : "")
);
String::format("elf-alloc-%s%s", program_header.is_readable() ? "r" : "", program_header.is_writable() ? "w" : ""));
memcpy(program_header.vaddr().as_ptr(), program_header.raw_data(), program_header.size_in_image());
} else {
map_section_hook(
@ -55,8 +54,7 @@ bool ELFLoader::layout()
program_header.is_readable(),
program_header.is_writable(),
program_header.is_executable(),
String::format("elf-map-%s%s%s", program_header.is_readable() ? "r" : "", program_header.is_writable() ? "w" : "", program_header.is_executable() ? "x" : "")
);
String::format("elf-map-%s%s%s", program_header.is_readable() ? "r" : "", program_header.is_writable() ? "w" : "", program_header.is_executable() ? "x" : ""));
}
});
return !failed;
@ -65,7 +63,7 @@ bool ELFLoader::layout()
char* ELFLoader::symbol_ptr(const char* name)
{
char* found_ptr = nullptr;
m_image.for_each_symbol([&] (const ELFImage::Symbol symbol) {
m_image.for_each_symbol([&](const ELFImage::Symbol symbol) {
if (symbol.type() != STT_FUNC)
return IterationDecision::Continue;
if (strcmp(symbol.name(), name))
@ -74,7 +72,7 @@ char* ELFLoader::symbol_ptr(const char* name)
found_ptr = (char*)symbol.value();
else
ASSERT_NOT_REACHED();
return IterationDecision::Abort;
return IterationDecision::Break;
});
return found_ptr;
}
@ -83,11 +81,11 @@ String ELFLoader::symbolicate(dword address) const
{
if (m_sorted_symbols.is_empty()) {
m_sorted_symbols.ensure_capacity(m_image.symbol_count());
m_image.for_each_symbol([this] (auto& symbol) {
m_image.for_each_symbol([this](auto& symbol) {
m_sorted_symbols.append({ symbol.value(), symbol.name() });
return IterationDecision::Continue;
});
quick_sort(m_sorted_symbols.begin(), m_sorted_symbols.end(), [] (auto& a, auto& b) {
quick_sort(m_sorted_symbols.begin(), m_sorted_symbols.end(), [](auto& a, auto& b) {
return a.address < b.address;
});
}

View file

@ -5,7 +5,7 @@
#include <AK/OwnPtr.h>
#include <AK/Vector.h>
#if defined(KERNEL)
#include <Kernel/VirtualAddress.h>
# include <Kernel/VirtualAddress.h>
#endif
#include <AK/ELF/ELFImage.h>
@ -34,7 +34,7 @@ private:
char* area_for_section_name(const char*);
struct PtrAndSize {
PtrAndSize() { }
PtrAndSize() {}
PtrAndSize(char* p, unsigned s)
: ptr(p)
, size(s)
@ -52,4 +52,3 @@ private:
};
mutable Vector<SortedSymbol> m_sorted_symbols;
};

View file

@ -117,10 +117,7 @@ typedef uint16_t Elf64_Quarter;
#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
/* e_ident */
#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
(ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
(ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
(ehdr).e_ident[EI_MAG3] == ELFMAG3)
#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && (ehdr).e_ident[EI_MAG1] == ELFMAG1 && (ehdr).e_ident[EI_MAG2] == ELFMAG2 && (ehdr).e_ident[EI_MAG3] == ELFMAG3)
/* ELF Header */
typedef struct elfhdr {
@ -311,7 +308,6 @@ typedef struct {
#define ELF_TEXT ".text" /* code */
#define ELF_OPENBSDRANDOMDATA ".openbsd.randomdata" /* constant randomdata */
/* Section Attribute Flags - sh_flags */
#define SHF_WRITE 0x1 /* Writable */
#define SHF_ALLOC 0x2 /* occupies memory */
@ -352,12 +348,12 @@ typedef struct {
/* Extract symbol info - st_info */
#define ELF32_ST_BIND(x) ((x) >> 4)
#define ELF32_ST_TYPE(x) (((unsigned int) x) & 0xf)
#define ELF32_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf))
#define ELF32_ST_TYPE(x) (((unsigned int)x) & 0xf)
#define ELF32_ST_INFO(b, t) (((b) << 4) + ((t)&0xf))
#define ELF64_ST_BIND(x) ((x) >> 4)
#define ELF64_ST_TYPE(x) (((unsigned int) x) & 0xf)
#define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf))
#define ELF64_ST_TYPE(x) (((unsigned int)x) & 0xf)
#define ELF64_ST_INFO(b, t) (((b) << 4) + ((t)&0xf))
/* Symbol Binding - ELF32_ST_BIND - st_info */
#define STB_LOCAL 0 /* Local symbol */
@ -378,7 +374,7 @@ typedef struct {
#define STT_HIPROC 15 /* specific symbol types */
/* Extract symbol visibility - st_other */
#define ELF_ST_VISIBILITY(v) ((v) & 0x3)
#define ELF_ST_VISIBILITY(v) ((v)&0x3)
#define ELF32_ST_VISIBILITY ELF_ST_VISIBILITY
#define ELF64_ST_VISIBILITY ELF_ST_VISIBILITY
@ -402,8 +398,8 @@ typedef struct {
/* Extract relocation info - r_info */
#define ELF32_R_SYM(i) ((i) >> 8)
#define ELF32_R_TYPE(i) ((unsigned char) (i))
#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
#define ELF32_R_TYPE(i) ((unsigned char)(i))
#define ELF32_R_INFO(s, t) (((s) << 8) + (unsigned char)(t))
typedef struct {
Elf64_Xword r_offset; /* where to do it */
@ -417,8 +413,8 @@ typedef struct {
} Elf64_Rela;
#define ELF64_R_SYM(info) ((info) >> 32)
#define ELF64_R_TYPE(info) ((info) & 0xFFFFFFFF)
#define ELF64_R_INFO(s,t) (((s) << 32) + (uint32_t)(t))
#define ELF64_R_TYPE(info) ((info)&0xFFFFFFFF)
#define ELF64_R_INFO(s, t) (((s) << 32) + (uint32_t)(t))
#if defined(__mips64__) && defined(__MIPSEL__)
/*
@ -426,12 +422,12 @@ typedef struct {
* than the regular ELF ABI: the r_info field is split into several
* pieces (see gnu/usr.bin/binutils-2.17/include/elf/mips.h for details).
*/
#undef ELF64_R_SYM
#undef ELF64_R_TYPE
#undef ELF64_R_INFO
#define ELF64_R_TYPE(info) ((uint64_t)swap32((info) >> 32))
#define ELF64_R_SYM(info) ((info) & 0xFFFFFFFF)
#define ELF64_R_INFO(s,t) (((uint64_t)swap32(t) << 32) + (uint32_t)(s))
# undef ELF64_R_SYM
# undef ELF64_R_TYPE
# undef ELF64_R_INFO
# define ELF64_R_TYPE(info) ((uint64_t)swap32((info) >> 32))
# define ELF64_R_SYM(info) ((info)&0xFFFFFFFF)
# define ELF64_R_INFO(s, t) (((uint64_t)swap32(t) << 32) + (uint32_t)(s))
#endif /* __mips64__ && __MIPSEL__ */
/* Program Header */
@ -518,7 +514,7 @@ typedef struct {
#define DT_INIT 12 /* address of initialization func. */
#define DT_FINI 13 /* address of termination function */
#define DT_SONAME 14 /* string table offset of shared obj */
#define DT_RPATH 15 /* string table offset of library
#define DT_RPATH 15 /* string table offset of library \
search path */
#define DT_SYMBOLIC 16 /* start sym search in shared obj. */
#define DT_REL 17 /* address of rel. tbl. w addends */
@ -670,14 +666,14 @@ struct elfcore_procinfo {
*/
#if defined(_KERNEL) || defined(_DYN_LOADER)
#define ELF32_NO_ADDR ((uint32_t) ~0) /* Indicates addr. not yet filled in */
# define ELF32_NO_ADDR ((uint32_t)~0) /* Indicates addr. not yet filled in */
typedef struct {
Elf32_Sword au_id; /* 32-bit id */
Elf32_Word au_v; /* 32-bit value */
} Aux32Info;
#define ELF64_NO_ADDR ((uint64_t) ~0)/* Indicates addr. not yet filled in */
# define ELF64_NO_ADDR ((uint64_t)~0) /* Indicates addr. not yet filled in */
typedef struct {
Elf64_Shalf au_id; /* 32-bit id */
@ -712,69 +708,69 @@ struct elf_args {
#endif
#if !defined(ELFSIZE) && defined(ARCH_ELFSIZE)
#define ELFSIZE ARCH_ELFSIZE
# define ELFSIZE ARCH_ELFSIZE
#endif
#if defined(ELFSIZE)
#define CONCAT(x,y) __CONCAT(x,y)
#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
# define CONCAT(x, y) __CONCAT(x, y)
# define ELFNAME(x) CONCAT(elf, CONCAT(ELFSIZE, CONCAT(_, x)))
# define ELFDEFNNAME(x) CONCAT(ELF, CONCAT(ELFSIZE, CONCAT(_, x)))
#endif
#if defined(ELFSIZE) && (ELFSIZE == 32)
#define Elf_Ehdr Elf32_Ehdr
#define Elf_Phdr Elf32_Phdr
#define Elf_Shdr Elf32_Shdr
#define Elf_Sym Elf32_Sym
#define Elf_Rel Elf32_Rel
#define Elf_RelA Elf32_Rela
#define Elf_Dyn Elf32_Dyn
#define Elf_Half Elf32_Half
#define Elf_Word Elf32_Word
#define Elf_Sword Elf32_Sword
#define Elf_Addr Elf32_Addr
#define Elf_Off Elf32_Off
#define Elf_Nhdr Elf32_Nhdr
#define Elf_Note Elf32_Note
# define Elf_Ehdr Elf32_Ehdr
# define Elf_Phdr Elf32_Phdr
# define Elf_Shdr Elf32_Shdr
# define Elf_Sym Elf32_Sym
# define Elf_Rel Elf32_Rel
# define Elf_RelA Elf32_Rela
# define Elf_Dyn Elf32_Dyn
# define Elf_Half Elf32_Half
# define Elf_Word Elf32_Word
# define Elf_Sword Elf32_Sword
# define Elf_Addr Elf32_Addr
# define Elf_Off Elf32_Off
# define Elf_Nhdr Elf32_Nhdr
# define Elf_Note Elf32_Note
#define ELF_R_SYM ELF32_R_SYM
#define ELF_R_TYPE ELF32_R_TYPE
#define ELF_R_INFO ELF32_R_INFO
#define ELFCLASS ELFCLASS32
# define ELF_R_SYM ELF32_R_SYM
# define ELF_R_TYPE ELF32_R_TYPE
# define ELF_R_INFO ELF32_R_INFO
# define ELFCLASS ELFCLASS32
#define ELF_ST_BIND ELF32_ST_BIND
#define ELF_ST_TYPE ELF32_ST_TYPE
#define ELF_ST_INFO ELF32_ST_INFO
# define ELF_ST_BIND ELF32_ST_BIND
# define ELF_ST_TYPE ELF32_ST_TYPE
# define ELF_ST_INFO ELF32_ST_INFO
#define ELF_NO_ADDR ELF32_NO_ADDR
#define AuxInfo Aux32Info
# define ELF_NO_ADDR ELF32_NO_ADDR
# define AuxInfo Aux32Info
#elif defined(ELFSIZE) && (ELFSIZE == 64)
#define Elf_Ehdr Elf64_Ehdr
#define Elf_Phdr Elf64_Phdr
#define Elf_Shdr Elf64_Shdr
#define Elf_Sym Elf64_Sym
#define Elf_Rel Elf64_Rel
#define Elf_RelA Elf64_Rela
#define Elf_Dyn Elf64_Dyn
#define Elf_Half Elf64_Half
#define Elf_Word Elf64_Word
#define Elf_Sword Elf64_Sword
#define Elf_Addr Elf64_Addr
#define Elf_Off Elf64_Off
#define Elf_Nhdr Elf64_Nhdr
#define Elf_Note Elf64_Note
# define Elf_Ehdr Elf64_Ehdr
# define Elf_Phdr Elf64_Phdr
# define Elf_Shdr Elf64_Shdr
# define Elf_Sym Elf64_Sym
# define Elf_Rel Elf64_Rel
# define Elf_RelA Elf64_Rela
# define Elf_Dyn Elf64_Dyn
# define Elf_Half Elf64_Half
# define Elf_Word Elf64_Word
# define Elf_Sword Elf64_Sword
# define Elf_Addr Elf64_Addr
# define Elf_Off Elf64_Off
# define Elf_Nhdr Elf64_Nhdr
# define Elf_Note Elf64_Note
#define ELF_R_SYM ELF64_R_SYM
#define ELF_R_TYPE ELF64_R_TYPE
#define ELF_R_INFO ELF64_R_INFO
#define ELFCLASS ELFCLASS64
# define ELF_R_SYM ELF64_R_SYM
# define ELF_R_TYPE ELF64_R_TYPE
# define ELF_R_INFO ELF64_R_INFO
# define ELFCLASS ELFCLASS64
#define ELF_ST_BIND ELF64_ST_BIND
#define ELF_ST_TYPE ELF64_ST_TYPE
#define ELF_ST_INFO ELF64_ST_INFO
# define ELF_ST_BIND ELF64_ST_BIND
# define ELF_ST_TYPE ELF64_ST_TYPE
# define ELF_ST_INFO ELF64_ST_INFO
#define ELF_NO_ADDR ELF64_NO_ADDR
#define AuxInfo Aux64Info
# define ELF_NO_ADDR ELF64_NO_ADDR
# define AuxInfo Aux64Info
#endif
#define ELF_TARG_VER 1 /* The ver for which this code is intended */