The Witchcraft Compiler Collection
WCC
|
#include <bfd.h>
#include <dlfcn.h>
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/procfs.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ucontext.h>
#include <unistd.h>
#include <utlist.h>
#include <ctype.h>
#include <libelf.h>
#include <gelf.h>
#include <nametotype.h>
#include <nametoalign.h>
#include <nametoentsz.h>
#include <nametolink.h>
#include <nametoinfo.h>
#include <arch.h>
#include <inttypes.h>
#include <config.h>
#include <capstone/capstone.h>
Go to the source code of this file.
Data Structures | |
struct | msec_t |
struct | mseg_t |
struct | ctx_t |
struct | symaddr |
struct | gimport_t |
Macros | |
#define | __USE_GNU |
#define | _GNU_SOURCE |
#define | DEFAULT_STRNDX_SIZE 4096 |
#define | FLAG_BSS 1 |
#define | FLAG_NOBIT 2 |
#define | FLAG_NOWRITE 4 |
#define | FLAG_TEXT 8 |
#define | ifis(x) if(!strncmp(name, x, strlen(x))) |
#define | elis(x) else if(!strncmp(name, x, strlen(x))) |
#define | MAXPADLEN 20 |
#define | EXTRA_CREATED_SECTIONS 4 |
#define | RELOC_X86_64 1 |
#define | RELOC_X86_32 2 |
#define | Elf_Ehdr Elf32_Ehdr |
#define | Elf_Shdr Elf32_Shdr |
#define | Elf_Sym Elf32_Sym |
#define | Elf_Addr Elf32_Addr |
#define | Elf_Sword Elf64_Sword |
#define | Elf_Section Elf32_Half |
#define | ELF_ST_BIND ELF32_ST_BIND |
#define | ELF_ST_TYPE ELF32_ST_TYPE |
#define | Elf_Rel Elf32_Rel |
#define | Elf_Rela Elf32_Rela |
#define | ELF_R_SYM ELF32_R_SYM |
#define | ELF_R_TYPE ELF32_R_TYPE |
#define | ELF_R_INFO ELF32_R_INFO |
#define | Elf_Phdr Elf32_Phdr |
#define | Elf_Xword Elf32_Xword |
#define | Elf_Word Elf32_Word |
#define | Elf_Off Elf32_Off |
#define | ELFCLASS ELFCLASS32 |
#define | ELFMACHINE EM_386 |
#define | CS_MODE CS_MODE_32 |
#define | RELOC_MODE RELOC_X86_32 |
#define | nullstr "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
Typedefs | |
typedef struct msec_t | msec_t |
typedef struct mseg_t | mseg_t |
typedef struct ctx_t | ctx_t |
typedef struct gimport_t | gimport_t |
Functions | |
int | craft_section (ctx_t *ctx, msec_t *m) |
unsigned int | secindex_from_name (ctx_t *ctx, const char *name) |
msec_t * | section_from_name (ctx_t *ctx, char *name) |
msec_t * | section_from_addr (ctx_t *ctx, unsigned long int addr) |
int | print_bfd_sections (ctx_t *ctx) |
msec_t * | section_from_index (ctx_t *ctx, unsigned int index) |
unsigned int | secindex_from_name_after_strip (ctx_t *ctx, const char *name) |
int | analyze_text (ctx_t *ctx, char *data, unsigned int datalen, unsigned long int addr) |
int | save_reloc (ctx_t *ctx, Elf_Rela *r, unsigned int sindex, int has_addend) |
unsigned int | protect_perms (unsigned int perms) |
void | add_symaddr (ctx_t *ctx, const char *name, int addr, char symclass) |
int | add_extra_symbols (ctx_t *ctx) |
int | rd_symbols (ctx_t *ctx) |
int | entszfromname (const char *name) |
unsigned int | max (unsigned int a, unsigned int b) |
char * | sec_name_from_index_after_strip (ctx_t *ctx, unsigned int index) |
int | link_from_name (ctx_t *ctx, const char *name) |
int | info_from_name (ctx_t *ctx, const char *name) |
int | typefromname (const char *name) |
unsigned int | alignfromname (const char *name) |
unsigned int | ptype_from_section (msec_t *ms) |
unsigned int | pflag_from_section (msec_t *ms) |
int | phdr_cmp_premerge (mseg_t *a, mseg_t *b) |
int | phdr_cmp (mseg_t *a, mseg_t *b) |
int | sort_phdrs (ctx_t *ctx) |
int | sort_phdrs_premerge (ctx_t *ctx) |
mseg_t * | alloc_phdr (msec_t *ms) |
int | create_phdrs (ctx_t *ctx) |
int | merge_phdrs (ctx_t *ctx) |
int | adjust_baseaddress (ctx_t *ctx) |
msec_t * | mk_section (void) |
char * | reloc_htype_x86_64 (int thetype) |
char * | reloc_htype_x86_32 (int thetype) |
char * | reloc_htype (int thetype) |
int | fixup_strtab_and_symtab (ctx_t *ctx) |
int | fixup_text (ctx_t *ctx) |
unsigned int | append_sym (Elf_Sym *s) |
unsigned int | append_strtab (char *str) |
void | hexdump (unsigned char *data, size_t size) |
unsigned int | open_best (ctx_t *ctx) |
int | open_target (ctx_t *ctx) |
int | copy_body (ctx_t *ctx) |
int | load_binary (ctx_t *ctx) |
int | flags_from_name (const char *name) |
int | print_msec (ctx_t *ctx) |
int | rd_sections (ctx_t *ctx) |
int | save_dynstr (ctx_t *ctx, GElf_Shdr shdr, char *binary) |
int | save_dynsym (ctx_t *ctx, GElf_Shdr shdr, char *binary) |
int | patch_symbol_index (ctx_t *ctx, Elf_Sym *s) |
int | fixup_symtab_section_index (ctx_t *ctx) |
int | append_reloc (Elf_Rela *r) |
int | save_global_import (ctx_t *ctx, char *sname, msec_t *sec, Elf_Rela *r, unsigned int sindex) |
int | check_global_import (unsigned long int addr) |
int | internal_function_store (ctx_t *ctx, unsigned long long int addr) |
int | rd_symtab (ctx_t *ctx) |
int | rm_section (ctx_t *ctx, char *name) |
int | strip_binary_reloc (ctx_t *ctx) |
unsigned int | libify (ctx_t *ctx) |
int | print_maps (void) |
ctx_t * | ctx_init (void) |
int | usage (char *name) |
int | print_version (void) |
int | desired_arch (ctx_t *ctx, char *name) |
int | ctx_getopt (ctx_t *ctx, int argc, char **argv) |
int | main (int argc, char **argv) |
Variables | |
unsigned int | maxoldsec = 0 |
unsigned int | maxnewsec = 0 |
unsigned int | deltastrtab = 0 |
char * | allowed_sections [] |
char * | blnames [] |
char * | globalsymtab = 0 |
int | globalsymtablen = 0 |
unsigned int | globalsymtableoffset = 0 |
char * | globalstrtab = 0 |
unsigned int | globalstrtablen = 0 |
unsigned int | globalstrtableoffset = 0 |
unsigned int | globalsymindex = 0 |
char * | globalreloc = 0 |
unsigned int | globalreloclen = 0 |
unsigned int | globalrelocoffset = 0 |
unsigned long int | mintext = -1 |
unsigned long int | maxtext = 0 |
unsigned long int | textvma = 0 |
unsigned long int | mindata = -1 |
unsigned long int | maxdata = 0 |
unsigned long int | datavma = 0 |
unsigned long int | orig_text = 0 |
unsigned long int | orig_sz = 0 |
struct symaddr * | symaddrs |
gimport_t ** | gimports = 0 |
unsigned int | gimportslen = 0 |
#define __USE_GNU |
Witchcraft Compiler Collection
Author: Jonathan Brossard - endra zine @gmai l.co m
The MIT License (MIT) Copyright (c) 2016 Jonathan Brossard
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define ELF_ST_BIND ELF32_ST_BIND |
#define ELF_ST_TYPE ELF32_ST_TYPE |
#define nullstr "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |
#define RELOC_MODE RELOC_X86_32 |
void add_symaddr | ( | ctx_t * | ctx, |
const char * | name, | ||
int | addr, | ||
char | symclass | ||
) |
unsigned int alignfromname | ( | const char * | name | ) |
int analyze_text | ( | ctx_t * | ctx, |
char * | data, | ||
unsigned int | datalen, | ||
unsigned long int | addr | ||
) |
unsigned int append_strtab | ( | char * | str | ) |
unsigned int append_sym | ( | Elf_Sym * | s | ) |
int check_global_import | ( | unsigned long int | addr | ) |
int create_phdrs | ( | ctx_t * | ctx | ) |
ctx_t* ctx_init | ( | void | ) |
int entszfromname | ( | const char * | name | ) |
int fixup_strtab_and_symtab | ( | ctx_t * | ctx | ) |
int flags_from_name | ( | const char * | name | ) |
void hexdump | ( | unsigned char * | data, |
size_t | size | ||
) |
int info_from_name | ( | ctx_t * | ctx, |
const char * | name | ||
) |
int internal_function_store | ( | ctx_t * | ctx, |
unsigned long long int | addr | ||
) |
unsigned int libify | ( | ctx_t * | ctx | ) |
Main routine
LOAD OPERATIONS
Load each section of binary using bfd
Print BFD sections
Read .text segment boundaries
Open target binary
Read sections from disk
Read symtab + strtab : BFD doesn't do this
Read symbols
Add extra symbols
Parse relocations
Fix section indexes in symtab
PROCESSING
Copy each section content in output file
Relocation stripping
Create Program Headers
FINAL WRITE OPERATIONS
Write strtab and symtab
Add section headers to output file
Add segment headers to output file
Add ELF Header to output file
Finalize/Close/Cleanup
int link_from_name | ( | ctx_t * | ctx, |
const char * | name | ||
) |
unsigned int max | ( | unsigned int | a, |
unsigned int | b | ||
) |
int merge_phdrs | ( | ctx_t * | ctx | ) |
unsigned int open_best | ( | ctx_t * | ctx | ) |
unsigned int pflag_from_section | ( | msec_t * | ms | ) |
int print_bfd_sections | ( | ctx_t * | ctx | ) |
unsigned int protect_perms | ( | unsigned int | perms | ) |
unsigned int ptype_from_section | ( | msec_t * | ms | ) |
int rd_sections | ( | ctx_t * | ctx | ) |
int rd_symbols | ( | ctx_t * | ctx | ) |
int rd_symtab | ( | ctx_t * | ctx | ) |
int rm_section | ( | ctx_t * | ctx, |
char * | name | ||
) |
int save_dynstr | ( | ctx_t * | ctx, |
GElf_Shdr | shdr, | ||
char * | binary | ||
) |
int save_dynsym | ( | ctx_t * | ctx, |
GElf_Shdr | shdr, | ||
char * | binary | ||
) |
char* sec_name_from_index_after_strip | ( | ctx_t * | ctx, |
unsigned int | index | ||
) |
unsigned int secindex_from_name | ( | ctx_t * | ctx, |
const char * | name | ||
) |
unsigned int secindex_from_name_after_strip | ( | ctx_t * | ctx, |
const char * | name | ||
) |
int sort_phdrs | ( | ctx_t * | ctx | ) |
int sort_phdrs_premerge | ( | ctx_t * | ctx | ) |
int strip_binary_reloc | ( | ctx_t * | ctx | ) |
int typefromname | ( | const char * | name | ) |
char* allowed_sections[] |
struct symaddr * symaddrs |