File: linker.h

package info (click to toggle)
upx-ucl 3.91-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,448 kB
  • ctags: 12,267
  • sloc: ansic: 70,113; cpp: 40,297; asm: 22,239; python: 1,119; makefile: 1,041; sh: 16
file content (254 lines) | stat: -rw-r--r-- 7,634 bytes parent folder | download | duplicates (2)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* linker.h --

   This file is part of the UPX executable compressor.

   Copyright (C) 1996-2013 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 1996-2013 Laszlo Molnar
   All Rights Reserved.

   UPX and the UCL library are free software; you can redistribute them
   and/or modify them 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; see the file COPYING.
   If not, write to the Free Software Foundation, Inc.,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   Markus F.X.J. Oberhumer              Laszlo Molnar
   <markus@oberhumer.com>               <ml1050@users.sourceforge.net>
 */


#ifndef __UPX_LINKER_H
#define __UPX_LINKER_H 1


/*************************************************************************
// ElfLinker
**************************************************************************/

class ElfLinker : private noncopyable
{
    friend class Packer;
public:
    const N_BELE_RTP::AbstractPolicy *bele; // target endianness
protected:
    struct      Section;
    struct      Symbol;
    struct      Relocation;

    upx_byte    *input;
    int         inputlen;
    upx_byte    *output;
    int         outputlen;

    Section     *head;
    Section     *tail;

    Section     **sections;
    Symbol      **symbols;
    Relocation  **relocations;

    unsigned    nsections;
    unsigned    nsections_capacity;
    unsigned    nsymbols;
    unsigned    nsymbols_capacity;
    unsigned    nrelocations;
    unsigned    nrelocations_capacity;

    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;

    Symbol *addSymbol(const char *name, const char *section, upx_uint64_t offset);
    Relocation *addRelocation(const char *section, unsigned off, const char *type,
                              const char *symbol, upx_uint64_t add);

public:
    ElfLinker();
    virtual ~ElfLinker();

    virtual void init(const void *pdata, int plen);
    //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))
    void __acc_cdecl_va addLoaderVA(const char *s, ...) __attribute__((__sentinel__));
#else
    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 getSectionSize(const char *sname) 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;

    void alignWithByte(unsigned len, unsigned char b);
    virtual void alignCode(unsigned len) { alignWithByte(len, 0); }
    virtual void alignData(unsigned len) { alignWithByte(len, 0); }

protected:
    virtual void relocate();
    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); }
    void set_te64(void *p, upx_uint64_t v) const { bele->set64(p, v); }
};


struct ElfLinker::Section : private noncopyable
{
    char *name;
    void *input;
    upx_byte *output;
    unsigned size;
    upx_uint64_t offset;
    unsigned p2align;   // log2
    Section *next;

    Section(const char *n, const void *i, unsigned s, unsigned a=0);
    ~Section();
};


struct ElfLinker::Symbol : private noncopyable
{
    char *name;
    Section *section;
    upx_uint64_t offset;

    Symbol(const char *n, Section *s, upx_uint64_t o);
    ~Symbol();
};


struct ElfLinker::Relocation : private noncopyable
{
    const Section *section;
    unsigned offset;
    const char *type;
    const Symbol *value;
    upx_uint64_t add;           // used in .rela relocations

    Relocation(const Section *s, unsigned o, const char *t,
               const Symbol *v, upx_uint64_t a);
};


/*************************************************************************
// ElfLinker arch subclasses
**************************************************************************/

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);
};


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);
};


class ElfLinkerArmLE : public ElfLinker
{
    typedef ElfLinker super;
protected:
    virtual void relocate1(const Relocation *, upx_byte *location,
                           upx_uint64_t value, const char *type);
};


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);
};


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);
};


class ElfLinkerMipsLE : public ElfLinker
{
    typedef ElfLinker super;
protected:
    virtual void relocate1(const Relocation *, upx_byte *location,
                           upx_uint64_t value, const char *type);
};


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);
};


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);
};



#endif /* already included */


/*
vi:ts=4:et
*/