diff options
Diffstat (limited to 'includes')
-rw-r--r-- | includes/binary-elf.h | 98 | ||||
-rw-r--r-- | includes/binary.h | 226 | ||||
-rw-r--r-- | includes/binwriter-elf.h | 32 | ||||
-rw-r--r-- | includes/binwriter.h | 44 |
4 files changed, 400 insertions, 0 deletions
diff --git a/includes/binary-elf.h b/includes/binary-elf.h new file mode 100644 index 0000000..f1ea14e --- /dev/null +++ b/includes/binary-elf.h @@ -0,0 +1,98 @@ +/* + * 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-elf.h,v 1.1 2005-12-02 11:28:26 pixel Exp $ */ + +#ifndef __BINARY_ELF_H__ +#define __BINARY_ELF_H__ + +#ifdef _MSC_VER +//do packing +#endif + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef signed char s8; +typedef signed short s16; +typedef signed int s32; + +#define PROGBITS 1 +#define NOBITS 8 +#define REL 9 +#define NOTYPE 0 +#define SECTION 3 +#define PT_LOAD 1 +#define PF_X 1 +#define PF_W 2 +#define PF_R 4 + +struct elf_header_t { + union { + u8 raw[16]; + struct e_ident_t { + u8 ei_magic[4]; + u8 ei_class; + u8 ei_data; + u8 ei_version; + } cook; + } e_ident; + u16 e_type; + u16 e_machine; + u32 e_version; + u32 e_entry; + u32 e_phoff; + u32 e_shoff; + u32 e_flags; + u16 e_ehsize; + u16 e_phentsize; + u16 e_phnum; + u16 e_shentsize; + u16 e_shnum; + u16 e_shstrndx; +} PACKED; + +struct elf_pheader_t { + u32 type; /* == 1, PT_LOAD (that is, this section will get loaded */ + u32 offset; /* offset in file, on a 4096 bytes boundary */ + u32 vaddr; /* virtual address where this section is loaded */ + u32 paddr; /* physical address where this section is loaded */ + u32 filesz; /* size of that section in the file */ + u32 memsz; /* size of that section in memory (rest is zero filled) */ + u32 flags; /* PF_X | PF_W | PF_R, that is executable, writable, readable */ + u32 align; /* == 0x1000 that is 4096 bytes */ +} PACKED; + + +struct elf_section_t { + u32 sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size; + u32 sh_link, sh_info, sh_addralign, sh_entsize; +} PACKED; + +struct elf_symbol_t { + u32 st_name, st_value, st_size; + u8 st_info, st_other; + u16 st_shndx; +} PACKED; + +struct elf_reloc_t { + u32 r_offset, r_info; +} PACKED; + +#endif diff --git a/includes/binary.h b/includes/binary.h new file mode 100644 index 0000000..576f7e8 --- /dev/null +++ b/includes/binary.h @@ -0,0 +1,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 diff --git a/includes/binwriter-elf.h b/includes/binwriter-elf.h new file mode 100644 index 0000000..e3f0bae --- /dev/null +++ b/includes/binwriter-elf.h @@ -0,0 +1,32 @@ +/* + * 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: binwriter-elf.h,v 1.1 2005-12-02 11:28:27 pixel Exp $ */ + +#ifndef __BINWRITER_ELF_H__ +#define __BINWRITER_ELF_H__ + +#include <binwriter.h> + +class BinWriter_elf : public BinWriter { + public: + virtual void dump(Handle *); +}; + +#endif diff --git a/includes/binwriter.h b/includes/binwriter.h new file mode 100644 index 0000000..1e73ac0 --- /dev/null +++ b/includes/binwriter.h @@ -0,0 +1,44 @@ +/* + * 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: binwriter.h,v 1.1 2005-12-02 11:28:27 pixel Exp $ */ + +#ifndef __BINWRITER_H__ +#define __BINWRITER_H__ + +#include <vector> +#include <Exceptions.h> +#include <Handle.h> +#include <binary.h> + +struct SectionOut { + char * data; + Uint32 base, real_size, whole_size; +}; + +class BinWriter : public Base { + public: + void merge(); + virtual void dump(Handle *) = 0; + protected: + Uint32 epc; + std::vector<struct SectionOut> sections; +}; + +#endif |