diff --git a/src/Makefile b/src/Makefile index 29b92e36..2317b8a7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -162,7 +162,7 @@ endif # automatically format some C++ source code files 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 += snprintf.cpp CLANG_FORMAT_FILES += stdcxx.cpp stdcxx.h diff --git a/src/linker.cpp b/src/linker.cpp index 23b632f7..b314517f 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -25,17 +25,12 @@ */ - #include "conf.h" #include "linker.h" -static unsigned hex(unsigned char c) -{ - return (c & 0xf) + (c > '9' ? 9 : 0); -} +static unsigned hex(unsigned char c) { return (c & 0xf) + (c > '9' ? 9 : 0); } -static bool update_capacity(unsigned size, unsigned *capacity) -{ +static bool update_capacity(unsigned size, unsigned *capacity) { if (size < *capacity) return false; if (*capacity == 0) @@ -45,8 +40,7 @@ static bool update_capacity(unsigned size, unsigned *capacity) return true; } -static void __acc_cdecl_va internal_error(const char *format, ...) -{ +static void __acc_cdecl_va internal_error(const char *format, ...) { static char buf[1024]; va_list ap; @@ -57,79 +51,62 @@ static void __acc_cdecl_va internal_error(const char *format, ...) throwInternalError(buf); } - /************************************************************************* // Section **************************************************************************/ -ElfLinker::Section::Section(const char *n, const void *i, unsigned s, unsigned a) : - name(NULL), output(NULL), size(s), offset(0), p2align(a), next(NULL) -{ +ElfLinker::Section::Section(const char *n, const void *i, unsigned s, unsigned a) + : name(NULL), output(NULL), size(s), offset(0), p2align(a), next(NULL) { name = strdup(n); assert(name != NULL); input = malloc(s + 1); assert(input != NULL); if (s != 0) memcpy(input, i, s); - ((char *)input)[s] = 0; + ((char *) input)[s] = 0; } -ElfLinker::Section::~Section() -{ +ElfLinker::Section::~Section() { free(name); free(input); } - /************************************************************************* // Symbol **************************************************************************/ -ElfLinker::Symbol::Symbol(const char *n, Section *s, upx_uint64_t o) : - name(NULL), section(s), offset(o) -{ +ElfLinker::Symbol::Symbol(const char *n, Section *s, upx_uint64_t o) + : name(NULL), section(s), offset(o) { name = strdup(n); assert(name != NULL); assert(section != NULL); } -ElfLinker::Symbol::~Symbol() -{ - free(name); -} - +ElfLinker::Symbol::~Symbol() { free(name); } /************************************************************************* // Relocation **************************************************************************/ -ElfLinker::Relocation::Relocation(const Section *s, unsigned o, const char *t, - const Symbol *v, upx_uint64_t a) : - section(s), offset(o), type(t), value(v), add(a) -{ +ElfLinker::Relocation::Relocation(const Section *s, unsigned o, const char *t, const Symbol *v, + upx_uint64_t a) + : section(s), offset(o), type(t), value(v), add(a) { assert(section != NULL); } - /************************************************************************* // ElfLinker **************************************************************************/ -ElfLinker::ElfLinker() : - bele(&N_BELE_RTP::le_policy), - input(NULL), output(NULL), head(NULL), tail(NULL), - sections(NULL), symbols(NULL), relocations(NULL), - nsections(0), nsections_capacity(0), - nsymbols(0), nsymbols_capacity(0), - nrelocations(0), nrelocations_capacity(0), - reloc_done(false) -{ -} +ElfLinker::ElfLinker() + : bele(&N_BELE_RTP::le_policy), input(NULL), output(NULL), head(NULL), tail(NULL), + sections(NULL), symbols(NULL), relocations(NULL), nsections(0), nsections_capacity(0), + nsymbols(0), nsymbols_capacity(0), nrelocations(0), nrelocations_capacity(0), + reloc_done(false) {} -ElfLinker::~ElfLinker() -{ - delete [] input; - delete [] output; +ElfLinker::~ElfLinker() { + delete[] input; + delete[] output; unsigned ic; for (ic = 0; ic < nsections; ic++) @@ -143,24 +120,19 @@ ElfLinker::~ElfLinker() free(relocations); } -void ElfLinker::init(const void *pdata_v, int plen) -{ +void ElfLinker::init(const void *pdata_v, int plen) { const upx_byte *pdata = (const upx_byte *) pdata_v; - if (plen >= 16 && memcmp(pdata, "UPX#", 4) == 0) - { + if (plen >= 16 && memcmp(pdata, "UPX#", 4) == 0) { // decompress pre-compressed stub-loader int method; unsigned u_len, c_len; - if (pdata[4]) - { + if (pdata[4]) { method = pdata[4]; u_len = get_le16(pdata + 5); c_len = get_le16(pdata + 7); pdata += 9; assert(9 + c_len == (unsigned) plen); - } - else - { + } else { method = pdata[5]; u_len = get_le32(pdata + 6); c_len = get_le32(pdata + 10); @@ -176,9 +148,7 @@ void ElfLinker::init(const void *pdata_v, int plen) throwOutOfMemoryException(); if (r != UPX_E_OK || new_len != u_len) throwBadLoader(); - } - else - { + } else { inputlen = plen; input = new upx_byte[inputlen + 1]; if (inputlen) @@ -189,7 +159,9 @@ void ElfLinker::init(const void *pdata_v, int plen) output = new upx_byte[inputlen ? inputlen : 0x4000]; outputlen = 0; - if ((int)strlen("Sections:\n" "SYMBOL TABLE:\n" "RELOCATION RECORDS FOR ") < inputlen) { + if ((int) strlen("Sections:\n" + "SYMBOL TABLE:\n" + "RELOCATION RECORDS FOR ") < inputlen) { int pos = find(input, inputlen, "Sections:\n", 10); assert(pos != -1); char *psections = (char *) input + pos; @@ -202,53 +174,45 @@ void ElfLinker::init(const void *pdata_v, int plen) preprocessSections(psections, psymbols); preprocessSymbols(psymbols, prelocs); - preprocessRelocations(prelocs, (char*) input + inputlen); + preprocessRelocations(prelocs, (char *) input + inputlen); addLoader("*UND*"); } } -void ElfLinker::preprocessSections(char *start, char *end) -{ +void ElfLinker::preprocessSections(char *start, char *end) { char *nextl; - for (nsections = 0; start < end; start = 1+ nextl) - { + for (nsections = 0; start < end; start = 1 + nextl) { nextl = strchr(start, '\n'); assert(nextl != NULL); - *nextl = '\0'; // a record is a line + *nextl = '\0'; // a record is a line unsigned offset, size, align; char name[1024]; - if (sscanf(start, "%*d %1023s %x %*d %*d %x 2**%d", - name, &size, &offset, &align) == 4) - { + if (sscanf(start, "%*d %1023s %x %*d %*d %x 2**%d", name, &size, &offset, &align) == 4) { char *n = strstr(start, name); n[strlen(name)] = 0; addSection(n, input + offset, size, align); - //printf("section %s preprocessed\n", n); + // printf("section %s preprocessed\n", n); } } addSection("*ABS*", NULL, 0, 0); addSection("*UND*", NULL, 0, 0); } -void ElfLinker::preprocessSymbols(char *start, char *end) -{ +void ElfLinker::preprocessSymbols(char *start, char *end) { char *nextl; - for (nsymbols = 0; start < end; start = 1+ nextl) - { + for (nsymbols = 0; start < end; start = 1 + nextl) { nextl = strchr(start, '\n'); assert(nextl != NULL); - *nextl = '\0'; // a record is a line + *nextl = '\0'; // a record is a line unsigned value, offset; char section[1024]; char symbol[1024]; - if (sscanf(start, "%x g *ABS* %x %1023s", - &value, &offset, symbol) == 3) - { + if (sscanf(start, "%x g *ABS* %x %1023s", &value, &offset, symbol) == 3) { char *s = strstr(start, symbol); s[strlen(symbol)] = 0; addSymbol(s, "*ABS*", value); @@ -263,25 +227,23 @@ void ElfLinker::preprocessSymbols(char *start, char *end) #endif &offset, section, symbol) == 3) { - char *s = strstr(start, symbol); - s[strlen(symbol)] = 0; - if (strcmp(section, "*UND*") == 0) - offset = 0xdeaddead; - assert(strcmp(section, "*ABS*") != 0); - addSymbol(s, section, offset); - } + char *s = strstr(start, symbol); + s[strlen(symbol)] = 0; + if (strcmp(section, "*UND*") == 0) + offset = 0xdeaddead; + assert(strcmp(section, "*ABS*") != 0); + addSymbol(s, section, offset); + } } } -void ElfLinker::preprocessRelocations(char *start, char *end) -{ +void ElfLinker::preprocessRelocations(char *start, char *end) { Section *section = NULL; char *nextl; - for (nrelocations = 0; start < end; start = 1+ nextl) - { + for (nrelocations = 0; start < end; start = 1 + nextl) { nextl = strchr(start, '\n'); assert(nextl != NULL); - *nextl = '\0'; // a record is a line + *nextl = '\0'; // a record is a line { char sect[1024]; @@ -293,9 +255,7 @@ void ElfLinker::preprocessRelocations(char *start, char *end) char type[100]; char symbol[1024]; - if (sscanf(start, "%x %99s %1023s", - &offset, type, symbol) == 3) - { + if (sscanf(start, "%x %99s %1023s", &offset, type, symbol) == 3) { char *t = strstr(start, type); t[strlen(type)] = 0; @@ -303,16 +263,15 @@ void ElfLinker::preprocessRelocations(char *start, char *end) char *p = strstr(symbol, "+0x"); if (p == NULL) p = strstr(symbol, "-0x"); - if (p) - { + if (p) { char sign = *p; - *p = 0; // terminate the symbol name + *p = 0; // terminate the symbol name p += 3; assert(strlen(p) == 8 || strlen(p) == 16); #if (ACC_CC_MSC && (_MSC_VER < 1800)) unsigned a = 0, b = 0; if (sscanf(p, "%08x%08x", &a, &b) == 2) - add = ((upx_uint64_t)a << 32) | b; + add = ((upx_uint64_t) a << 32) | b; else add = a; #else @@ -326,14 +285,14 @@ void ElfLinker::preprocessRelocations(char *start, char *end) if (section) { addRelocation(section->name, offset, t, symbol, add); - //printf("relocation %s %s %x %llu preprocessed\n", section->name, symbol, offset, (unsigned long long) add); + // printf("relocation %s %s %x %llu preprocessed\n", section->name, symbol, offset, + // (unsigned long long) add); } } } } -ElfLinker::Section *ElfLinker::findSection(const char *name, bool fatal) const -{ +ElfLinker::Section *ElfLinker::findSection(const char *name, bool fatal) const { for (unsigned ic = 0; ic < nsections; ic++) if (strcmp(sections[ic]->name, name) == 0) return sections[ic]; @@ -342,8 +301,7 @@ ElfLinker::Section *ElfLinker::findSection(const char *name, bool fatal) const return NULL; } -ElfLinker::Symbol *ElfLinker::findSymbol(const char *name, bool fatal) const -{ +ElfLinker::Symbol *ElfLinker::findSymbol(const char *name, bool fatal) const { for (unsigned ic = 0; ic < nsymbols; ic++) if (strcmp(symbols[ic]->name, name) == 0) return symbols[ic]; @@ -352,13 +310,16 @@ ElfLinker::Symbol *ElfLinker::findSymbol(const char *name, bool fatal) const return NULL; } -ElfLinker::Section *ElfLinker::addSection(const char *sname, const void *sdata, int slen, unsigned p2align) -{ - //printf("addSection: %s len=%d align=%d\n", sname, slen, p2align); +ElfLinker::Section *ElfLinker::addSection(const char *sname, const void *sdata, int slen, + unsigned p2align) { + // printf("addSection: %s len=%d align=%d\n", sname, slen, p2align); if (update_capacity(nsections, &nsections_capacity)) - sections = static_cast
(realloc(sections, nsections_capacity * sizeof(Section *))); + sections = + static_cast
(realloc(sections, nsections_capacity * sizeof(Section *))); assert(sections); - assert(sname); assert(sname[0]); assert(sname[strlen(sname)-1] != ':'); + assert(sname); + assert(sname[0]); + assert(sname[strlen(sname) - 1] != ':'); assert(findSection(sname, false) == NULL); Section *sec = new Section(sname, sdata, slen, p2align); sections[nsections++] = sec; @@ -366,28 +327,27 @@ ElfLinker::Section *ElfLinker::addSection(const char *sname, const void *sdata, } ElfLinker::Symbol *ElfLinker::addSymbol(const char *name, const char *section, - upx_uint64_t offset) -{ - //printf("addSymbol: %s %s 0x%x\n", name, section, offset); + upx_uint64_t offset) { + // printf("addSymbol: %s %s 0x%x\n", name, section, offset); if (update_capacity(nsymbols, &nsymbols_capacity)) symbols = static_cast(realloc(symbols, nsymbols_capacity * sizeof(Symbol *))); assert(symbols != NULL); - assert(name); assert(name[0]); assert(name[strlen(name)-1] != ':'); + assert(name); + assert(name[0]); + assert(name[strlen(name) - 1] != ':'); assert(findSymbol(name, false) == NULL); Symbol *sym = new Symbol(name, findSection(section), offset); symbols[nsymbols++] = sym; return sym; } -ElfLinker::Relocation *ElfLinker::addRelocation(const char *section, unsigned off, - const char *type, const char *symbol, - upx_uint64_t add) -{ +ElfLinker::Relocation *ElfLinker::addRelocation(const char *section, unsigned off, const char *type, + const char *symbol, upx_uint64_t add) { if (update_capacity(nrelocations, &nrelocations_capacity)) - relocations = static_cast(realloc(relocations, (nrelocations_capacity) * sizeof(Relocation *))); + relocations = static_cast( + realloc(relocations, (nrelocations_capacity) * sizeof(Relocation *))); assert(relocations != NULL); - Relocation *rel = new Relocation(findSection(section), off, - type, findSymbol(symbol), add); + Relocation *rel = new Relocation(findSection(section), off, type, findSymbol(symbol), add); relocations[nrelocations++] = rel; return rel; } @@ -400,19 +360,16 @@ void ElfLinker::setLoaderAlignOffset(int phase) } #endif -int ElfLinker::addLoader(const char *sname) -{ +int ElfLinker::addLoader(const char *sname) { assert(sname != NULL); if (!sname[0]) return outputlen; char *begin = strdup(sname); char *end = begin + strlen(begin); - for (char *sect = begin; sect < end; ) - { + for (char *sect = begin; sect < end;) { for (char *tokend = sect; *tokend; tokend++) - if (*tokend == ' ' || *tokend == ',') - { + if (*tokend == ' ' || *tokend == ',') { *tokend = 0; break; } @@ -425,39 +382,34 @@ int ElfLinker::addLoader(const char *sname) if (m) { l %= hex(sect[1]); } - if (l) - { + if (l) { if (sect[3] == 'D') alignData(l); else alignCode(l); tail->size += l; } - } - else - { + } else { Section *section = findSection(sect); if (section->p2align) { assert(tail); assert(tail != section); // .p2align must be < 32 unsigned const v = ~0u << section->p2align; - if (unsigned const l = ~v & (0u-(unsigned)(tail->offset + tail->size))) { + if (unsigned const l = ~v & (0u - (unsigned) (tail->offset + tail->size))) { alignCode(l); tail->size += l; } } memcpy(output + outputlen, section->input, section->size); section->output = output + outputlen; - //printf("section added: 0x%04x %3d %s\n", outputlen, section->size, section->name); + // printf("section added: 0x%04x %3d %s\n", outputlen, section->size, section->name); outputlen += section->size; - if (head) - { + if (head) { tail->next = section; section->offset = tail->offset + tail->size; - } - else + } else head = section; tail = section; } @@ -467,80 +419,68 @@ int ElfLinker::addLoader(const char *sname) return outputlen; } -void ElfLinker::addLoader(const char *s, va_list ap) -{ - while (s != NULL) - { +void ElfLinker::addLoader(const char *s, va_list ap) { + while (s != NULL) { addLoader(s); s = va_arg(ap, const char *); } } -void __acc_cdecl_va ElfLinker::addLoaderVA(const char *s, ...) -{ +void __acc_cdecl_va ElfLinker::addLoaderVA(const char *s, ...) { va_list ap; va_start(ap, s); addLoader(s, ap); va_end(ap); } -int ElfLinker::getSection(const char *sname, int *slen) const -{ +int ElfLinker::getSection(const char *sname, int *slen) const { const Section *section = findSection(sname); if (slen) *slen = section->size; return (int) (section->output - output); } -int ElfLinker::getSectionSize(const char *sname) const -{ +int ElfLinker::getSectionSize(const char *sname) const { const Section *section = findSection(sname); return section->size; } -upx_byte *ElfLinker::getLoader(int *llen) const -{ +upx_byte *ElfLinker::getLoader(int *llen) const { if (llen) *llen = outputlen; return output; } -void ElfLinker::relocate() -{ +void ElfLinker::relocate() { assert(!reloc_done); reloc_done = true; - for (unsigned ic = 0; ic < nrelocations; ic++) - { + for (unsigned ic = 0; ic < nrelocations; ic++) { const Relocation *rel = relocations[ic]; upx_uint64_t value = 0; if (rel->section->output == NULL) continue; - if (strcmp(rel->value->section->name, "*ABS*") == 0) - { + if (strcmp(rel->value->section->name, "*ABS*") == 0) { value = rel->value->offset; - } - else if (strcmp(rel->value->section->name, "*UND*") == 0 && - rel->value->offset == 0xdeaddead) + } else if (strcmp(rel->value->section->name, "*UND*") == 0 && + rel->value->offset == 0xdeaddead) internal_error("undefined symbol '%s' referenced\n", rel->value->name); else if (rel->value->section->output == NULL) - internal_error("can not apply reloc '%s:%x' without section '%s'\n", - rel->section->name, rel->offset, - rel->value->section->name); - else - { - value = rel->value->section->offset + - rel->value->offset + rel->add; + internal_error("can not apply reloc '%s:%x' without section '%s'\n", rel->section->name, + rel->offset, rel->value->section->name); + else { + value = rel->value->section->offset + rel->value->offset + rel->add; } upx_byte *location = rel->section->output + rel->offset; - //printf("%-28s %-28s %-10s 0x%16llx 0x%16llx\n", rel->section->name, rel->value->name, rel->type, (long long) value, (long long) value - rel->section->offset - rel->offset); - //printf(" %llx %d %llx %d %llx : %d\n", (long long) value, rel->value->section->offset, rel->value->offset, rel->offset, (long long) rel->add, *location); + // printf("%-28s %-28s %-10s 0x%16llx 0x%16llx\n", rel->section->name, rel->value->name, + // rel->type, (long long) value, (long long) value - rel->section->offset - rel->offset); + // printf(" %llx %d %llx %d %llx : %d\n", (long long) value, rel->value->section->offset, + // rel->value->offset, rel->offset, (long long) rel->add, *location); relocate1(rel, location, value, rel->type); } } -void ElfLinker::defineSymbol(const char *name, upx_uint64_t value) -{ +void ElfLinker::defineSymbol(const char *name, upx_uint64_t value) { Symbol *symbol = findSymbol(name); if (strcmp(symbol->section->name, "*ABS*") == 0) internal_error("defineSymbol: symbol '%s' is *ABS*\n", name); @@ -548,77 +488,62 @@ void ElfLinker::defineSymbol(const char *name, upx_uint64_t value) symbol->offset = value; else if (strcmp(symbol->section->name, name) == 0) // for sections { - for (Section *section = symbol->section; section; section = section->next) - { + for (Section *section = symbol->section; section; section = section->next) { assert(section->offset < value); section->offset = value; value += section->size; } - } - else + } else internal_error("defineSymbol: symbol '%s' already defined\n", name); } // debugging support -void ElfLinker::dumpSymbol(const Symbol *symbol, unsigned flags, FILE *fp) const -{ +void ElfLinker::dumpSymbol(const Symbol *symbol, unsigned flags, FILE *fp) const { if ((flags & 1) && symbol->section->output == NULL) return; - char d0[16+1], d1[16+1]; + char d0[16 + 1], d1[16 + 1]; upx_snprintf(d0, sizeof(d0), "%016llx", (upx_uint64_t) symbol->offset); upx_snprintf(d1, sizeof(d1), "%016llx", (upx_uint64_t) symbol->section->offset); - fprintf(fp, "%-28s 0x%-16s | %-28s 0x%-16s\n", - symbol->name, d0, symbol->section->name, d1); + fprintf(fp, "%-28s 0x%-16s | %-28s 0x%-16s\n", symbol->name, d0, symbol->section->name, d1); } -void ElfLinker::dumpSymbols(unsigned flags, FILE *fp) const -{ +void ElfLinker::dumpSymbols(unsigned flags, FILE *fp) const { if (fp == NULL) fp = stdout; - if ((flags & 2) == 0) - { + if ((flags & 2) == 0) { // default: dump symbols in used section order - for (const Section *section = head; section; section = section->next) - { - char d1[16+1]; + for (const Section *section = head; section; section = section->next) { + char d1[16 + 1]; upx_snprintf(d1, sizeof(d1), "%016llx", (upx_uint64_t) section->offset); fprintf(fp, "%-42s%-28s 0x%-16s\n", "", section->name, d1); - for (unsigned ic = 0; ic < nsymbols; ic++) - { + for (unsigned ic = 0; ic < nsymbols; ic++) { const Symbol *symbol = symbols[ic]; if (symbol->section == section && strcmp(symbol->name, section->name) != 0) dumpSymbol(symbol, flags, fp); } } - } - else - { + } else { // dump all symbols for (unsigned ic = 0; ic < nsymbols; ic++) dumpSymbol(symbols[ic], flags, fp); } } -upx_uint64_t ElfLinker::getSymbolOffset(const char *name) const -{ +upx_uint64_t ElfLinker::getSymbolOffset(const char *name) const { const Symbol *symbol = findSymbol(name); if (symbol->section->output == NULL) return 0xdeaddead; return symbol->section->offset + symbol->offset; } -void ElfLinker::alignWithByte(unsigned len, unsigned char b) -{ +void ElfLinker::alignWithByte(unsigned len, unsigned char b) { memset(output + outputlen, b, len); outputlen += len; } -void ElfLinker::relocate1(const Relocation *rel, upx_byte *, - upx_uint64_t, const char *) -{ +void ElfLinker::relocate1(const Relocation *rel, upx_byte *, upx_uint64_t, const char *) { internal_error("unknown relocation type '%s\n", rel->type); } - /************************************************************************* // ElfLinker arch subclasses // FIXME: add more displacment overflow checks @@ -637,33 +562,28 @@ static void check8(const Relocation *rel, const upx_byte *location, int v, int d } #endif - -void ElfLinkerAMD64::relocate1(const Relocation *rel, upx_byte *location, - upx_uint64_t value, const char *type) -{ +void ElfLinkerAMD64::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, + const char *type) { if (strncmp(type, "R_X86_64_", 9)) return super::relocate1(rel, location, value, type); type += 9; bool range_check = false; - if (strncmp(type, "PC", 2) == 0) - { + if (strncmp(type, "PC", 2) == 0) { value -= rel->section->offset + rel->offset; type += 2; range_check = true; } - if (strcmp(type, "8") == 0) - { + if (strcmp(type, "8") == 0) { int displ = (signed char) *location + (int) value; if (range_check && (displ < -128 || displ > 127)) - internal_error("target out of range (%d) in reloc %s:%x\n", - displ, rel->section->name, rel->offset); + internal_error("target out of range (%d) in reloc %s:%x\n", displ, rel->section->name, + rel->offset); *location += value; - } - else if (strcmp(type, "16") == 0) + } else if (strcmp(type, "16") == 0) set_le16(location, get_le16(location) + value); - else if (strncmp(type, "32", 2) == 0) // for "32" and "32S" + else if (strncmp(type, "32", 2) == 0) // for "32" and "32S" set_le32(location, get_le32(location) + value); else if (strcmp(type, "64") == 0) set_le64(location, get_le64(location) + value); @@ -671,95 +591,66 @@ void ElfLinkerAMD64::relocate1(const Relocation *rel, upx_byte *location, super::relocate1(rel, location, value, type); } - -void ElfLinkerArmBE::relocate1(const Relocation *rel, upx_byte *location, - upx_uint64_t value, const char *type) -{ - if (strcmp(type, "R_ARM_PC24") == 0) - { +void ElfLinkerArmBE::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, + const char *type) { + if (strcmp(type, "R_ARM_PC24") == 0) { value -= rel->section->offset + rel->offset; - set_be24(1+ location, get_be24(1+ location) + value / 4); - } - else if (strcmp(type, "R_ARM_ABS32") == 0) - { + set_be24(1 + location, get_be24(1 + location) + value / 4); + } else if (strcmp(type, "R_ARM_ABS32") == 0) { set_be32(location, get_be32(location) + value); - } - else if (strcmp(type, "R_ARM_THM_CALL") == 0 - || strcmp(type, "R_ARM_THM_XPC22") == 0 - || strcmp(type, "R_ARM_THM_PC22") == 0 - ) - { + } else if (strcmp(type, "R_ARM_THM_CALL") == 0 || strcmp(type, "R_ARM_THM_XPC22") == 0 || + strcmp(type, "R_ARM_THM_PC22") == 0) { value -= rel->section->offset + rel->offset; value += ((get_be16(location) & 0x7ff) << 12); value += (get_be16(location + 2) & 0x7ff) << 1; set_be16(location, 0xf000 + ((value >> 12) & 0x7ff)); - set_be16(location + 2, 0xf800 + ((value >> 1) & 0x7ff)); + set_be16(location + 2, 0xf800 + ((value >> 1) & 0x7ff)); //(b, 0xF000 + ((v - 1) / 2) * 0x10000); - //set_be32(location, get_be32(location) + value / 4); - } - else if (0==strcmp("R_ARM_ABS8", type)) { + // set_be32(location, get_be32(location) + value / 4); + } else if (0 == strcmp("R_ARM_ABS8", type)) { *location += value; - } - else + } else super::relocate1(rel, location, value, type); } - -void ElfLinkerArmLE::relocate1(const Relocation *rel, upx_byte *location, - upx_uint64_t value, const char *type) -{ - if (strcmp(type, "R_ARM_PC24") == 0) - { +void ElfLinkerArmLE::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, + const char *type) { + if (strcmp(type, "R_ARM_PC24") == 0) { value -= rel->section->offset + rel->offset; set_le24(location, get_le24(location) + value / 4); - } - else if (strcmp(type, "R_ARM_ABS32") == 0) - { + } else if (strcmp(type, "R_ARM_ABS32") == 0) { set_le32(location, get_le32(location) + value); - } - else if (strcmp(type, "R_ARM_THM_CALL") == 0 - || strcmp(type, "R_ARM_THM_XPC22") == 0 - || strcmp(type, "R_ARM_THM_PC22") == 0 - ) - { + } else if (strcmp(type, "R_ARM_THM_CALL") == 0 || strcmp(type, "R_ARM_THM_XPC22") == 0 || + strcmp(type, "R_ARM_THM_PC22") == 0) { value -= rel->section->offset + rel->offset; value += ((get_le16(location) & 0x7ff) << 12); value += (get_le16(location + 2) & 0x7ff) << 1; set_le16(location, 0xf000 + ((value >> 12) & 0x7ff)); - set_le16(location + 2, 0xf800 + ((value >> 1) & 0x7ff)); + set_le16(location + 2, 0xf800 + ((value >> 1) & 0x7ff)); //(b, 0xF000 + ((v - 1) / 2) * 0x10000); - //set_le32(location, get_le32(location) + value / 4); - } - else if (0==strcmp("R_ARM_ABS8", type)) { + // set_le32(location, get_le32(location) + value / 4); + } else if (0 == strcmp("R_ARM_ABS8", type)) { *location += value; - } - else + } else super::relocate1(rel, location, value, type); } -void ElfLinkerArm64LE::relocate1(const Relocation *rel, upx_byte *location, - upx_uint64_t value, const char *type) -{ - if (strcmp(type, "R_AARCH64_CALL26") == 0) - { +void ElfLinkerArm64LE::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, + const char *type) { + if (strcmp(type, "R_AARCH64_CALL26") == 0) { value -= rel->section->offset + rel->offset; - set_le24(location, get_le24(location) + value / 4); // FIXME set_le26 - } - else if (strcmp(type, "R_AARCH64_ABS32") == 0) - { + set_le24(location, get_le24(location) + value / 4); // FIXME set_le26 + } else if (strcmp(type, "R_AARCH64_ABS32") == 0) { set_le32(location, get_le32(location) + value); - } - else + } else super::relocate1(rel, location, value, type); } - -void ElfLinkerM68k::alignCode(unsigned len) -{ +void ElfLinkerM68k::alignCode(unsigned len) { assert((len & 1) == 0); assert((outputlen & 1) == 0); for (unsigned i = 0; i < len; i += 2) @@ -767,50 +658,41 @@ void ElfLinkerM68k::alignCode(unsigned len) outputlen += len; } -void ElfLinkerM68k::relocate1(const Relocation *rel, upx_byte *location, - upx_uint64_t value, const char *type) -{ +void ElfLinkerM68k::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, + const char *type) { if (strncmp(type, "R_68K_", 6)) return super::relocate1(rel, location, value, type); type += 6; - if (strncmp(type, "PC", 2) == 0) - { + if (strncmp(type, "PC", 2) == 0) { value -= rel->section->offset + rel->offset; type += 2; } if (strcmp(type, "8") == 0) { *location += value; - } - else if (strcmp(type, "16") == 0) { + } else if (strcmp(type, "16") == 0) { set_be16(location, get_be16(location) + value); - } - else if (strcmp(type, "32") == 0) { + } else if (strcmp(type, "32") == 0) { set_be32(location, get_be32(location) + value); - } - else + } else super::relocate1(rel, location, value, type); } - -void ElfLinkerMipsBE::relocate1(const Relocation *rel, upx_byte *location, - upx_uint64_t value, const char *type) -{ -#define MIPS_HI(a) (((a) >> 16) + (((a) & 0x8000) >> 15)) -#define MIPS_LO(a) ((a) & 0xffff) -#define MIPS_PC16(a) ((a) >> 2) -#define MIPS_PC26(a) (((a) & 0x0fffffff) >> 2) +void ElfLinkerMipsBE::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, + const char *type) { +#define MIPS_HI(a) (((a) >> 16) + (((a) &0x8000) >> 15)) +#define MIPS_LO(a) ((a) &0xffff) +#define MIPS_PC16(a) ((a) >> 2) +#define MIPS_PC26(a) (((a) &0x0fffffff) >> 2) if (strcmp(type, "R_MIPS_HI16") == 0) - set_be16(2+location, get_be16(2+location) + MIPS_HI(value)); + set_be16(2 + location, get_be16(2 + location) + MIPS_HI(value)); else if (strcmp(type, "R_MIPS_LO16") == 0) - set_be16(2+location, get_be16(2+location) + MIPS_LO(value)); - else if (strcmp(type, "R_MIPS_PC16") == 0) - { + set_be16(2 + location, get_be16(2 + location) + MIPS_LO(value)); + else if (strcmp(type, "R_MIPS_PC16") == 0) { value -= rel->section->offset + rel->offset; - set_be16(2+location, get_be16(2+location) + MIPS_PC16(value)); - } - else if (strcmp(type, "R_MIPS_26") == 0) + set_be16(2 + location, get_be16(2 + location) + MIPS_PC16(value)); + } else if (strcmp(type, "R_MIPS_26") == 0) set_be32(location, get_be32(location) + MIPS_PC26(value)); else if (strcmp(type, "R_MIPS_32") == 0) set_be32(location, get_be32(location) + value); @@ -823,25 +705,21 @@ void ElfLinkerMipsBE::relocate1(const Relocation *rel, upx_byte *location, #undef MIPS_PC26 } - -void ElfLinkerMipsLE::relocate1(const Relocation *rel, upx_byte *location, - upx_uint64_t value, const char *type) -{ -#define MIPS_HI(a) (((a) >> 16) + (((a) & 0x8000) >> 15)) -#define MIPS_LO(a) ((a) & 0xffff) -#define MIPS_PC16(a) ((a) >> 2) -#define MIPS_PC26(a) (((a) & 0x0fffffff) >> 2) +void ElfLinkerMipsLE::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, + const char *type) { +#define MIPS_HI(a) (((a) >> 16) + (((a) &0x8000) >> 15)) +#define MIPS_LO(a) ((a) &0xffff) +#define MIPS_PC16(a) ((a) >> 2) +#define MIPS_PC26(a) (((a) &0x0fffffff) >> 2) if (strcmp(type, "R_MIPS_HI16") == 0) set_le16(location, get_le16(location) + MIPS_HI(value)); else if (strcmp(type, "R_MIPS_LO16") == 0) set_le16(location, get_le16(location) + MIPS_LO(value)); - else if (strcmp(type, "R_MIPS_PC16") == 0) - { + else if (strcmp(type, "R_MIPS_PC16") == 0) { value -= rel->section->offset + rel->offset; set_le16(location, get_le16(location) + MIPS_PC16(value)); - } - else if (strcmp(type, "R_MIPS_26") == 0) + } else if (strcmp(type, "R_MIPS_26") == 0) set_le32(location, get_le32(location) + MIPS_PC26(value)); else if (strcmp(type, "R_MIPS_32") == 0) set_le32(location, get_le32(location) + value); @@ -854,22 +732,18 @@ void ElfLinkerMipsLE::relocate1(const Relocation *rel, upx_byte *location, #undef MIPS_PC26 } - -void ElfLinkerPpc32::relocate1(const Relocation *rel, upx_byte *location, - upx_uint64_t value, const char *type) -{ +void ElfLinkerPpc32::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, + const char *type) { if (strncmp(type, "R_PPC_", 6)) return super::relocate1(rel, location, value, type); type += 6; - if (strcmp(type, "ADDR32") == 0) - { + if (strcmp(type, "ADDR32") == 0) { set_be32(location, get_be32(location) + value); return; } - if (strncmp(type, "REL", 3) == 0) - { + if (strncmp(type, "REL", 3) == 0) { value -= rel->section->offset + rel->offset; type += 3; } @@ -878,33 +752,27 @@ void ElfLinkerPpc32::relocate1(const Relocation *rel, upx_byte *location, // Note that original (*location).displ is ignored. if (strcmp(type, "24") == 0) { - if (3& value) + if (3 & value) internal_error("unaligned word diplacement"); // FIXME: displacment overflow? - set_be32(location, (0xfc000003 & get_be32(location)) + - (0x03fffffc & value)); - } - else if (strcmp(type, "14") == 0) { - if (3& value) + set_be32(location, (0xfc000003 & get_be32(location)) + (0x03fffffc & value)); + } else if (strcmp(type, "14") == 0) { + if (3 & value) internal_error("unaligned word diplacement"); // FIXME: displacment overflow? - set_be32(location, (0xffff0003 & get_be32(location)) + - (0x0000fffc & value)); - } - else + set_be32(location, (0xffff0003 & get_be32(location)) + (0x0000fffc & value)); + } else super::relocate1(rel, location, value, type); } -void ElfLinkerPpc64le::relocate1(const Relocation *rel, upx_byte *location, - upx_uint64_t value, const char *type) -{ +void ElfLinkerPpc64le::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, + const char *type) { if (strncmp(type, "R_PPC64_REL", 11)) return super::relocate1(rel, location, value, type); type += 11; bool range_check = false; - if (strncmp(type, "PC", 2) == 0) - { + if (strncmp(type, "PC", 2) == 0) { type += 2; range_check = true; } @@ -912,21 +780,19 @@ void ElfLinkerPpc64le::relocate1(const Relocation *rel, upx_byte *location, /* value will hold relative displacement */ value -= rel->section->offset + rel->offset; - if (strcmp(type, "8") == 0) - { + if (strcmp(type, "8") == 0) { int displ = (signed char) *location + (int) value; if (range_check && (displ < -128 || displ > 127)) - internal_error("target out of range (%d) in reloc %s:%x\n", - displ, rel->section->name, rel->offset); + internal_error("target out of range (%d) in reloc %s:%x\n", displ, rel->section->name, + rel->offset); *location += value; - } - else if (strncmp(type, "14", 2) == 0) // for "32" and "32S" + } else if (strncmp(type, "14", 2) == 0) // for "32" and "32S" set_le16(location, get_le16(location) + value); else if (strcmp(type, "16") == 0) set_le16(location, get_le16(location) + value); - else if (strncmp(type, "24", 2) == 0) // for "32" and "32S" + else if (strncmp(type, "24", 2) == 0) // for "32" and "32S" set_le24(location, get_le24(location) + value); - else if (strncmp(type, "32", 2) == 0) // for "32" and "32S" + else if (strncmp(type, "32", 2) == 0) // for "32" and "32S" set_le32(location, get_le32(location) + value); else if (strcmp(type, "64") == 0) set_le64(location, get_le64(location) + value); @@ -934,31 +800,26 @@ void ElfLinkerPpc64le::relocate1(const Relocation *rel, upx_byte *location, super::relocate1(rel, location, value, type); } - -void ElfLinkerX86::relocate1(const Relocation *rel, upx_byte *location, - upx_uint64_t value, const char *type) -{ +void ElfLinkerX86::relocate1(const Relocation *rel, upx_byte *location, upx_uint64_t value, + const char *type) { if (strncmp(type, "R_386_", 6)) return super::relocate1(rel, location, value, type); type += 6; bool range_check = false; - if (strncmp(type, "PC", 2) == 0) - { + if (strncmp(type, "PC", 2) == 0) { value -= rel->section->offset + rel->offset; type += 2; range_check = true; } - if (strcmp(type, "8") == 0) - { + if (strcmp(type, "8") == 0) { int displ = (signed char) *location + (int) value; if (range_check && (displ < -128 || displ > 127)) - internal_error("target out of range (%d,%d,%d) in reloc %s:%x\n", - displ, *location, value, rel->section->name, rel->offset); + internal_error("target out of range (%d,%d,%d) in reloc %s:%x\n", displ, *location, + value, rel->section->name, rel->offset); *location += value; - } - else if (strcmp(type, "16") == 0) + } else if (strcmp(type, "16") == 0) set_le16(location, get_le16(location) + value); else if (strcmp(type, "32") == 0) set_le32(location, get_le32(location) + value); diff --git a/src/linker.h b/src/linker.h index 59e5081a..c83798af 100644 --- a/src/linker.h +++ b/src/linker.h @@ -25,52 +25,50 @@ */ - #ifndef __UPX_LINKER_H #define __UPX_LINKER_H 1 - /************************************************************************* // ElfLinker **************************************************************************/ -class ElfLinker : private noncopyable -{ +class ElfLinker : private noncopyable { friend class Packer; + public: const N_BELE_RTP::AbstractPolicy *bele; // target endianness protected: - struct Section; - struct Symbol; - struct Relocation; + struct Section; + struct Symbol; + struct Relocation; - upx_byte *input; - int inputlen; - upx_byte *output; - int outputlen; + upx_byte *input; + int inputlen; + upx_byte *output; + int outputlen; - Section *head; - Section *tail; + Section *head; + Section *tail; - Section **sections; - Symbol **symbols; - Relocation **relocations; + Section **sections; + Symbol **symbols; + Relocation **relocations; - unsigned nsections; - unsigned nsections_capacity; - unsigned nsymbols; - unsigned nsymbols_capacity; - unsigned nrelocations; - unsigned nrelocations_capacity; + unsigned nsections; + unsigned nsections_capacity; + unsigned nsymbols; + unsigned nsymbols_capacity; + unsigned nrelocations; + unsigned nrelocations_capacity; - bool reloc_done; + bool reloc_done; protected: void preprocessSections(char *start, char *end); void preprocessSymbols(char *start, char *end); void preprocessRelocations(char *start, char *end); - Section *findSection(const char *name, bool fatal=true) const; - Symbol *findSymbol(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 *addSymbol(const char *name, const char *section, upx_uint64_t offset); Relocation *addRelocation(const char *section, unsigned off, const char *type, @@ -81,7 +79,7 @@ public: virtual ~ElfLinker(); virtual void init(const void *pdata, int plen); - //virtual void setLoaderAlignOffset(int phase); + // virtual void setLoaderAlignOffset(int phase); virtual int addLoader(const char *sname); void addLoader(const char *s, va_list ap); #if 1 && (ACC_CC_CLANG || (ACC_CC_GNUC >= 0x040100)) @@ -90,14 +88,14 @@ public: void __acc_cdecl_va addLoaderVA(const char *s, ...); #endif 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 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 upx_uint64_t getSymbolOffset(const char *) 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); virtual void alignCode(unsigned len) { alignWithByte(len, 0); } @@ -105,36 +103,32 @@ public: protected: virtual void relocate(); - virtual void relocate1(const Relocation *, upx_byte *location, - upx_uint64_t value, const char *type); + virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + const char *type); // target endianness abstraction - unsigned get_te16(const void *p) const { return bele->get16(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); } - void set_te16(void *p, unsigned v) const { bele->set16(p, v); } - void set_te32(void *p, unsigned v) const { bele->set32(p, v); } + unsigned get_te16(const void *p) const { return bele->get16(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); } + 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_te64(void *p, upx_uint64_t v) const { bele->set64(p, v); } }; - -struct ElfLinker::Section : private noncopyable -{ +struct ElfLinker::Section : private noncopyable { char *name; void *input; upx_byte *output; unsigned size; upx_uint64_t offset; - unsigned p2align; // log2 + unsigned p2align; // log2 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(); }; - -struct ElfLinker::Symbol : private noncopyable -{ +struct ElfLinker::Symbol : private noncopyable { char *name; Section *section; upx_uint64_t offset; @@ -143,124 +137,115 @@ struct ElfLinker::Symbol : private noncopyable ~Symbol(); }; - -struct ElfLinker::Relocation : private noncopyable -{ +struct ElfLinker::Relocation : private noncopyable { const Section *section; unsigned offset; const char *type; 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, - const Symbol *v, upx_uint64_t a); + Relocation(const Section *s, unsigned o, const char *t, const Symbol *v, upx_uint64_t a); }; - /************************************************************************* // ElfLinker arch subclasses **************************************************************************/ -class ElfLinkerAMD64 : public ElfLinker -{ +class ElfLinkerAMD64 : public ElfLinker { typedef ElfLinker super; + protected: virtual void alignCode(unsigned len) { alignWithByte(len, 0x90); } - virtual void relocate1(const Relocation *, upx_byte *location, - upx_uint64_t value, const char *type); + virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + const char *type); }; - -class ElfLinkerArmBE : public ElfLinker -{ +class ElfLinkerArmBE : public ElfLinker { typedef ElfLinker super; + public: ElfLinkerArmBE() { bele = &N_BELE_RTP::be_policy; } + protected: - virtual void relocate1(const Relocation *, upx_byte *location, - upx_uint64_t value, const char *type); + virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + const char *type); }; - -class ElfLinkerArmLE : public ElfLinker -{ +class ElfLinkerArmLE : public ElfLinker { typedef ElfLinker super; + protected: - virtual void relocate1(const Relocation *, upx_byte *location, - upx_uint64_t value, const char *type); + virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + const char *type); }; -class ElfLinkerArm64LE : public ElfLinker -{ +class ElfLinkerArm64LE : public ElfLinker { typedef ElfLinker super; + protected: - virtual void relocate1(const Relocation *, upx_byte *location, - upx_uint64_t value, const char *type); + virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + const char *type); }; - -class ElfLinkerM68k : public ElfLinker -{ +class ElfLinkerM68k : public ElfLinker { typedef ElfLinker super; + public: ElfLinkerM68k() { bele = &N_BELE_RTP::be_policy; } + protected: virtual void alignCode(unsigned len); - virtual void relocate1(const Relocation *, upx_byte *location, - upx_uint64_t value, const char *type); + virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + const char *type); }; - -class ElfLinkerMipsBE : public ElfLinker -{ +class ElfLinkerMipsBE : public ElfLinker { typedef ElfLinker super; + public: ElfLinkerMipsBE() { bele = &N_BELE_RTP::be_policy; } + protected: - virtual void relocate1(const Relocation *, upx_byte *location, - upx_uint64_t value, const char *type); + virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + const char *type); }; - -class ElfLinkerMipsLE : public ElfLinker -{ +class ElfLinkerMipsLE : public ElfLinker { typedef ElfLinker super; + protected: - virtual void relocate1(const Relocation *, upx_byte *location, - upx_uint64_t value, const char *type); + virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + const char *type); }; - -class ElfLinkerPpc32 : public ElfLinker -{ +class ElfLinkerPpc32 : public ElfLinker { typedef ElfLinker super; + public: ElfLinkerPpc32() { bele = &N_BELE_RTP::be_policy; } + protected: - virtual void relocate1(const Relocation *, upx_byte *location, - upx_uint64_t value, const char *type); + virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + const char *type); }; -class ElfLinkerPpc64le : public ElfLinker -{ +class ElfLinkerPpc64le : public ElfLinker { typedef ElfLinker super; + protected: - virtual void relocate1(const Relocation *, upx_byte *location, - upx_uint64_t value, const char *type); + virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + const char *type); }; - -class ElfLinkerX86 : public ElfLinker -{ +class ElfLinkerX86 : public ElfLinker { typedef ElfLinker super; + protected: virtual void alignCode(unsigned len) { alignWithByte(len, 0x90); } - virtual void relocate1(const Relocation *, upx_byte *location, - upx_uint64_t value, const char *type); + virtual void relocate1(const Relocation *, upx_byte *location, upx_uint64_t value, + const char *type); }; - - #endif /* already included */ /* vim:set ts=4 sw=4 et: */