summaryrefslogtreecommitdiff
path: root/includes/binary.h
blob: 576f7e8577809318e22e1c1a448b816bff1e16f9 (plain)
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
/*
 *  PSX-Tools Bundle Pack
 *  Copyright (C) 2002-2005 Nicolas "Pixel" Noble
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it 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; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* $Id: binary.h,v 1.1 2005-12-02 11:28:26 pixel Exp $ */
#ifndef __BINARY_H__
#define __BINARY_H__

#define LOCAL 0
#define GLOBAL 1
#define WEAK 2

enum elf_mips_reloc_type {
    R_MIPS_NONE = 0,
    R_MIPS_16,
    R_MIPS_32,
    R_MIPS_REL32,
    R_MIPS_26,
    R_MIPS_HI16,
    R_MIPS_LO16,
    R_MIPS_GPREL16,
    R_MIPS_LITERAL,
    R_MIPS_GOT16,
    R_MIPS_PC16,
    R_MIPS_CALL16,
    R_MIPS_GPREL32,
    elf_mips_reloc_type_size
};

#include <vector>

#include <Exceptions.h>
#include <BString.h>
#include <Handle.h>

class Section;
class Export : public Base {
  public:
      Export(Section *, const String &, Uint32 offset, int bind);
      ~Export();
    static Export * GetFirst(const Section * parent = 0);
    Export * GetNext(const Section * parent = 0) const;
    static Export * GetLast(const Section * parent = 0);
    Export * GetPrev(const Section * parent = 0) const;
    String GetName() const;
    Uint32 get_offset() const;
    bool is_absolute() const;
    bool is_relative() const;
    Uint32 get_absolute_offset() const;
    static Export * find_export(const String &, int bind = -1);
    Section * GetParent() const;
  private:
    Section * parent;
    String name;
    Uint32 offset;
    int bind;
    Export * next, * prev;
    static Export * head;
    static Export * tail;
};
class Import : public Base {
  public:
      Import(Section *, const String &, Uint32 offset, int reloc);
      ~Import();
    static Import * GetFirst(const Section * parent = 0);
    Import * GetNext(const Section * parent = 0) const;
    static Import * GetLast(const Section * parent = 0);
    Import * GetPrev(const Section * parent = 0) const;
    String GetName() const;
    Uint32 get_offset() const;
    bool is_absolute() const;
    bool is_relative() const;
    static void fix_relocs() throw (GeneralException);
    Section * GetParent() const;
  private:
    Section * parent;
    String name;
    Uint32 offset;
    int reloc;
    Import * next, * prev;
    static Import * head;
    static Import * tail;
};
class Internal : public Base {
  public:
      Internal(Section *, Section *, Uint32 offset, int reloc);
      ~Internal();
    static Internal * GetFirst(const Section * parent = 0);
    Internal * GetNext(const Section * parent = 0) const;
    static Internal * GetLast(const Section * parent = 0);
    Internal * GetPrev(const Section * parent = 0) const;
    Section * getDest() const;
    Uint32 get_offset() const;
    bool is_absolute() const;
    bool is_relative() const;
    static void fix_relocs() throw (GeneralException);
    Section * GetParent() const;
  private:
    Section * parent;
    Section * dest;
    Uint32 offset;
    int reloc;
    Internal * next, * prev;
    static Internal * head;
    static Internal * tail;
};

class Binary;
class Section : public Base {
  public:
      Section(Binary * parent, Uint32 size, Uint32 offset, bool bss = false) throw (GeneralException);
      ~Section();
    char * get_bytes() const;
    Uint32 get_size() const;
    Uint32 get_offset() const;
    Uint32 get_end_offset() const;
    bool is_in(Uint32) const;
    bool is_bss() const;
    bool is_absolute() const;
    bool is_relative() const;
    Export * add_export(const String & name, Uint32 offset, int bind);
    Import * add_import(const String & name, Uint32 offset, int reloc);
    Internal * add_internal(Section *, Uint32 offset, int reloc);
    static Section * GetFirst(const Binary * parent = 0);
    Section * GetNext(const Binary * parent = 0) const;
    static Section * GetLast(const Binary * parent = 0);
    Section * GetPrev(const Binary * parent = 0) const;
    Export * GetFirstExport() const;
    Export * GetNextExport(const Export *) const;
    Export * GetLastExport() const;
    Export * GetPrevExport(const Export *) const;
    Import * GetFirstImport() const;
    Import * GetNextImport(const Import *) const;
    Import * GetLastImport() const;
    Import * GetPrevImport(const Import *) const;
    Internal * GetFirstInternal() const;
    Internal * GetNextInternal(const Internal *) const;
    Internal * GetLastInternal() const;
    Internal * GetPrevInternal(const Internal *) const;
    static bool solve_constraints();
    void fix_reloc(Uint32 from, Uint32 to, int reloc);
    Binary * GetParent() const;
  private:
    Export * find_export(const String &);
    void solve_constraints_r(Section *);
    Binary * parent;
    Uint32 size, offset;
    char * bytes;
    Section * next, * prev;
    static Section * head;
    static Section * tail;
};

class Binary : public Base {
  public:
    Section * add_section(Uint32 size, Uint32 offset);
    Section * add_bss(Uint32 size, Uint32 offset);
    Section * GetFirstSection() const;
    Section * GetNextSection(const Section *) const;
    Section * GetLastSection() const;
    Section * GetPrevSection(const Section *) const;
    Export * add_export(const String & name, Uint32 offset, int bind) throw (GeneralException);
    Uint32 getEntryPoint() const;
    String GetName() const;
    void loadSymbolMap(Handle * map);
    void create_jhook(Uint32 from, Uint32 to, int size = 0);
    void create_jhook(Uint32 from, const String & to, int size = 0);
    void create_jhook(const String & from, Uint32 to, int size = 0);
    void create_jhook(const String & from, const String & to, int size = 0);
    void create_jalhook(Uint32 from, Uint32 to, int size = 0);
    void create_jalhook(Uint32 from, const String & to, int size = 0);
    void create_jalhook(const String & from, Uint32 to, int size = 0);
    void create_jalhook(const String & from, const String & to, int size = 0);
  protected:
      Binary(const String &);
      void setEntryPoint(Uint32);
  private:
    Section * create_hook(Uint32, int);
    String name;
    Binary * next, * prev;
    static Binary * head;
    static Binary * tail;
    Uint32 EntryPoint;
};

class Binary_elf : public Binary {
  public:
      Binary_elf(Handle * elf) throw (GeneralException);
};

#ifdef s_addr
#undef s_addr
#endif

struct psyq_header_t {
    struct psyq_details_t {
        Uint8 id[8];
        Uint32 text, data, pc0, gp0, t_addr, t_size;
        Uint32 d_addr, d_size, b_addr, b_size, s_addr, s_size;
        Uint32 sp, fp, gp, ra, s0;
    } details;
    Uint8 psyqhead[0x800];
};

class Binary_psexe : public Binary {
  public:
      Binary_psexe(Handle * psexe) throw (GeneralException);
    static struct psyq_header_t header;
};

#endif