1
0
mirror of https://github.com/upx/upx synced 2025-09-28 19:06:07 +08:00

upx-clang-format -i linker.cpp linker.h

This commit is contained in:
Markus F.X.J. Oberhumer 2016-11-20 14:20:36 +01:00
parent 8d61e037f9
commit 4953e77161
3 changed files with 291 additions and 445 deletions

View File

@ -162,7 +162,7 @@ endif
# automatically format some C++ source code files # automatically format some C++ source code files
ifeq ($(shell uname),Linux) ifeq ($(shell uname),Linux)
CLANG_FORMAT_FILES += packhead.cpp CLANG_FORMAT_FILES += linker.cpp linker.h packhead.cpp
CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h CLANG_FORMAT_FILES += s_djgpp2.cpp s_object.cpp s_vcsa.cpp s_win32.cpp screen.h
CLANG_FORMAT_FILES += snprintf.cpp CLANG_FORMAT_FILES += snprintf.cpp
CLANG_FORMAT_FILES += stdcxx.cpp stdcxx.h CLANG_FORMAT_FILES += stdcxx.cpp stdcxx.h

File diff suppressed because it is too large Load Diff

View File

@ -25,52 +25,50 @@
<markus@oberhumer.com> <ezerotven+github@gmail.com> <markus@oberhumer.com> <ezerotven+github@gmail.com>
*/ */
#ifndef __UPX_LINKER_H #ifndef __UPX_LINKER_H
#define __UPX_LINKER_H 1 #define __UPX_LINKER_H 1
/************************************************************************* /*************************************************************************
// ElfLinker // ElfLinker
**************************************************************************/ **************************************************************************/
class ElfLinker : private noncopyable class ElfLinker : private noncopyable {
{
friend class Packer; friend class Packer;
public: public:
const N_BELE_RTP::AbstractPolicy *bele; // target endianness const N_BELE_RTP::AbstractPolicy *bele; // target endianness
protected: protected:
struct Section; struct Section;
struct Symbol; struct Symbol;
struct Relocation; struct Relocation;
upx_byte *input; upx_byte *input;
int inputlen; int inputlen;
upx_byte *output; upx_byte *output;
int outputlen; int outputlen;
Section *head; Section *head;
Section *tail; Section *tail;
Section **sections; Section **sections;
Symbol **symbols; Symbol **symbols;
Relocation **relocations; Relocation **relocations;
unsigned nsections; unsigned nsections;
unsigned nsections_capacity; unsigned nsections_capacity;
unsigned nsymbols; unsigned nsymbols;
unsigned nsymbols_capacity; unsigned nsymbols_capacity;
unsigned nrelocations; unsigned nrelocations;
unsigned nrelocations_capacity; unsigned nrelocations_capacity;
bool reloc_done; bool reloc_done;
protected: protected:
void preprocessSections(char *start, char *end); void preprocessSections(char *start, char *end);
void preprocessSymbols(char *start, char *end); void preprocessSymbols(char *start, char *end);
void preprocessRelocations(char *start, char *end); void preprocessRelocations(char *start, char *end);
Section *findSection(const char *name, bool fatal=true) const; Section *findSection(const char *name, bool fatal = true) const;
Symbol *findSymbol(const char *name, bool fatal=true) const; Symbol *findSymbol(const char *name, bool fatal = true) const;
Symbol *addSymbol(const char *name, const char *section, upx_uint64_t offset); Symbol *addSymbol(const char *name, const char *section, upx_uint64_t offset);
Relocation *addRelocation(const char *section, unsigned off, const char *type, Relocation *addRelocation(const char *section, unsigned off, const char *type,
@ -81,7 +79,7 @@ public:
virtual ~ElfLinker(); virtual ~ElfLinker();
virtual void init(const void *pdata, int plen); virtual void init(const void *pdata, int plen);
//virtual void setLoaderAlignOffset(int phase); // virtual void setLoaderAlignOffset(int phase);
virtual int addLoader(const char *sname); virtual int addLoader(const char *sname);
void addLoader(const char *s, va_list ap); void addLoader(const char *s, va_list ap);
#if 1 && (ACC_CC_CLANG || (ACC_CC_GNUC >= 0x040100)) #if 1 && (ACC_CC_CLANG || (ACC_CC_GNUC >= 0x040100))
@ -90,14 +88,14 @@ public:
void __acc_cdecl_va addLoaderVA(const char *s, ...); void __acc_cdecl_va addLoaderVA(const char *s, ...);
#endif #endif
virtual Section *addSection(const char *sname, const void *sdata, int slen, unsigned p2align); virtual Section *addSection(const char *sname, const void *sdata, int slen, unsigned p2align);
virtual int getSection(const char *sname, int *slen=NULL) const; virtual int getSection(const char *sname, int *slen = NULL) const;
virtual int getSectionSize(const char *sname) const; virtual int getSectionSize(const char *sname) const;
virtual upx_byte *getLoader(int *llen=NULL) const; virtual upx_byte *getLoader(int *llen = NULL) const;
virtual void defineSymbol(const char *name, upx_uint64_t value); virtual void defineSymbol(const char *name, upx_uint64_t value);
virtual upx_uint64_t getSymbolOffset(const char *) const; virtual upx_uint64_t getSymbolOffset(const char *) const;
virtual void dumpSymbol(const Symbol *, unsigned flags, FILE *fp) const; virtual void dumpSymbol(const Symbol *, unsigned flags, FILE *fp) const;
virtual void dumpSymbols(unsigned flags=0, FILE *fp=NULL) const; virtual void dumpSymbols(unsigned flags = 0, FILE *fp = NULL) const;
void alignWithByte(unsigned len, unsigned char b); void alignWithByte(unsigned len, unsigned char b);
virtual void alignCode(unsigned len) { alignWithByte(len, 0); } virtual void alignCode(unsigned len) { alignWithByte(len, 0); }
@ -105,36 +103,32 @@ public:
protected: protected:
virtual void relocate(); virtual void relocate();
virtual void relocate1(const Relocation *, upx_byte *location, virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value,
upx_uint64_t value, const char *type); const char *type);
// target endianness abstraction // target endianness abstraction
unsigned get_te16(const void *p) const { return bele->get16(p); } unsigned get_te16(const void *p) const { return bele->get16(p); }
unsigned get_te32(const void *p) const { return bele->get32(p); } unsigned get_te32(const void *p) const { return bele->get32(p); }
upx_uint64_t get_te64(const void *p) const { return bele->get64(p); } upx_uint64_t get_te64(const void *p) const { return bele->get64(p); }
void set_te16(void *p, unsigned v) const { bele->set16(p, v); } void set_te16(void *p, unsigned v) const { bele->set16(p, v); }
void set_te32(void *p, unsigned v) const { bele->set32(p, v); } void set_te32(void *p, unsigned v) const { bele->set32(p, v); }
void set_te64(void *p, upx_uint64_t v) const { bele->set64(p, v); } void set_te64(void *p, upx_uint64_t v) const { bele->set64(p, v); }
}; };
struct ElfLinker::Section : private noncopyable {
struct ElfLinker::Section : private noncopyable
{
char *name; char *name;
void *input; void *input;
upx_byte *output; upx_byte *output;
unsigned size; unsigned size;
upx_uint64_t offset; upx_uint64_t offset;
unsigned p2align; // log2 unsigned p2align; // log2
Section *next; Section *next;
Section(const char *n, const void *i, unsigned s, unsigned a=0); Section(const char *n, const void *i, unsigned s, unsigned a = 0);
~Section(); ~Section();
}; };
struct ElfLinker::Symbol : private noncopyable {
struct ElfLinker::Symbol : private noncopyable
{
char *name; char *name;
Section *section; Section *section;
upx_uint64_t offset; upx_uint64_t offset;
@ -143,124 +137,115 @@ struct ElfLinker::Symbol : private noncopyable
~Symbol(); ~Symbol();
}; };
struct ElfLinker::Relocation : private noncopyable {
struct ElfLinker::Relocation : private noncopyable
{
const Section *section; const Section *section;
unsigned offset; unsigned offset;
const char *type; const char *type;
const Symbol *value; const Symbol *value;
upx_uint64_t add; // used in .rela relocations upx_uint64_t add; // used in .rela relocations
Relocation(const Section *s, unsigned o, const char *t, Relocation(const Section *s, unsigned o, const char *t, const Symbol *v, upx_uint64_t a);
const Symbol *v, upx_uint64_t a);
}; };
/************************************************************************* /*************************************************************************
// ElfLinker arch subclasses // ElfLinker arch subclasses
**************************************************************************/ **************************************************************************/
class ElfLinkerAMD64 : public ElfLinker class ElfLinkerAMD64 : public ElfLinker {
{
typedef ElfLinker super; typedef ElfLinker super;
protected: protected:
virtual void alignCode(unsigned len) { alignWithByte(len, 0x90); } virtual void alignCode(unsigned len) { alignWithByte(len, 0x90); }
virtual void relocate1(const Relocation *, upx_byte *location, virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value,
upx_uint64_t value, const char *type); const char *type);
}; };
class ElfLinkerArmBE : public ElfLinker {
class ElfLinkerArmBE : public ElfLinker
{
typedef ElfLinker super; typedef ElfLinker super;
public: public:
ElfLinkerArmBE() { bele = &N_BELE_RTP::be_policy; } ElfLinkerArmBE() { bele = &N_BELE_RTP::be_policy; }
protected: protected:
virtual void relocate1(const Relocation *, upx_byte *location, virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value,
upx_uint64_t value, const char *type); const char *type);
}; };
class ElfLinkerArmLE : public ElfLinker {
class ElfLinkerArmLE : public ElfLinker
{
typedef ElfLinker super; typedef ElfLinker super;
protected: protected:
virtual void relocate1(const Relocation *, upx_byte *location, virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value,
upx_uint64_t value, const char *type); const char *type);
}; };
class ElfLinkerArm64LE : public ElfLinker class ElfLinkerArm64LE : public ElfLinker {
{
typedef ElfLinker super; typedef ElfLinker super;
protected: protected:
virtual void relocate1(const Relocation *, upx_byte *location, virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value,
upx_uint64_t value, const char *type); const char *type);
}; };
class ElfLinkerM68k : public ElfLinker {
class ElfLinkerM68k : public ElfLinker
{
typedef ElfLinker super; typedef ElfLinker super;
public: public:
ElfLinkerM68k() { bele = &N_BELE_RTP::be_policy; } ElfLinkerM68k() { bele = &N_BELE_RTP::be_policy; }
protected: protected:
virtual void alignCode(unsigned len); virtual void alignCode(unsigned len);
virtual void relocate1(const Relocation *, upx_byte *location, virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value,
upx_uint64_t value, const char *type); const char *type);
}; };
class ElfLinkerMipsBE : public ElfLinker {
class ElfLinkerMipsBE : public ElfLinker
{
typedef ElfLinker super; typedef ElfLinker super;
public: public:
ElfLinkerMipsBE() { bele = &N_BELE_RTP::be_policy; } ElfLinkerMipsBE() { bele = &N_BELE_RTP::be_policy; }
protected: protected:
virtual void relocate1(const Relocation *, upx_byte *location, virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value,
upx_uint64_t value, const char *type); const char *type);
}; };
class ElfLinkerMipsLE : public ElfLinker {
class ElfLinkerMipsLE : public ElfLinker
{
typedef ElfLinker super; typedef ElfLinker super;
protected: protected:
virtual void relocate1(const Relocation *, upx_byte *location, virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value,
upx_uint64_t value, const char *type); const char *type);
}; };
class ElfLinkerPpc32 : public ElfLinker {
class ElfLinkerPpc32 : public ElfLinker
{
typedef ElfLinker super; typedef ElfLinker super;
public: public:
ElfLinkerPpc32() { bele = &N_BELE_RTP::be_policy; } ElfLinkerPpc32() { bele = &N_BELE_RTP::be_policy; }
protected: protected:
virtual void relocate1(const Relocation *, upx_byte *location, virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value,
upx_uint64_t value, const char *type); const char *type);
}; };
class ElfLinkerPpc64le : public ElfLinker class ElfLinkerPpc64le : public ElfLinker {
{
typedef ElfLinker super; typedef ElfLinker super;
protected: protected:
virtual void relocate1(const Relocation *, upx_byte *location, virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value,
upx_uint64_t value, const char *type); const char *type);
}; };
class ElfLinkerX86 : public ElfLinker {
class ElfLinkerX86 : public ElfLinker
{
typedef ElfLinker super; typedef ElfLinker super;
protected: protected:
virtual void alignCode(unsigned len) { alignWithByte(len, 0x90); } virtual void alignCode(unsigned len) { alignWithByte(len, 0x90); }
virtual void relocate1(const Relocation *, upx_byte *location, virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value,
upx_uint64_t value, const char *type); const char *type);
}; };
#endif /* already included */ #endif /* already included */
/* vim:set ts=4 sw=4 et: */ /* vim:set ts=4 sw=4 et: */