Skip to content

Commit

Permalink
add elfRebuilder to rebuild so file
Browse files Browse the repository at this point in the history
  • Loading branch information
F8LEFT committed Jun 5, 2017
1 parent 90bd7c2 commit c557fac
Show file tree
Hide file tree
Showing 6 changed files with 1,011 additions and 19 deletions.
24 changes: 18 additions & 6 deletions ElfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
#define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \
MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \
MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE))
#define DL_ERR printf
ElfReader::ElfReader(const char* name, int fd)
: name_(name), fd_(fd),
phdr_num_(0), phdr_mmap_(NULL), phdr_table_(NULL), phdr_size_(0),
Expand All @@ -107,6 +106,9 @@ ElfReader::~ElfReader() {
if (phdr_mmap_ != NULL) {
delete [](uint8_t*)phdr_mmap_;
}
if(load_start_ != nullptr) {
delete [](uint8_t*)load_start_;
}
}

bool ElfReader::Load() {
Expand Down Expand Up @@ -206,6 +208,16 @@ bool ElfReader::ReadProgramHeader() {

phdr_mmap_ = mmap_result;
phdr_table_ = reinterpret_cast<Elf_Phdr*>(reinterpret_cast<char*>(mmap_result));

if(dump_so_file_) {
auto phdr = phdr_table_;
for(auto i = 0; i < phdr_num_; i++) {
phdr->p_filesz = phdr->p_memsz; // expend filesize to memsiz
phdr->p_paddr = phdr->p_vaddr;
phdr->p_offset = phdr->p_vaddr; // elf has been loaded.
phdr++;
}
}
return true;
}

Expand Down Expand Up @@ -292,6 +304,8 @@ bool ElfReader::LoadSegments() {
continue;
}

// TODO for dumped file, I need to fix phdr first

// Segment addresses in memory.
Elf_Addr seg_start = phdr->p_vaddr;
Elf_Addr seg_end = seg_start + phdr->p_memsz;
Expand Down Expand Up @@ -490,7 +504,6 @@ phdr_table_protect_gnu_relro(const Elf_Phdr* phdr_table,
/*PROT_READ*/0);
}

#ifdef ANDROID_ARM_LINKER

# ifndef PT_ARM_EXIDX
# define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */
Expand Down Expand Up @@ -524,14 +537,13 @@ phdr_table_get_arm_exidx(const Elf_Phdr* phdr_table,
continue;

*arm_exidx = (Elf_Addr*)(load_bias + phdr->p_vaddr);
*arm_exidx_count = (unsigned)(phdr->p_memsz / 8);
*arm_exidx_count = (unsigned)(phdr->p_memsz / sizeof(Elf_Addr));
return 0;
}
*arm_exidx = NULL;
*arm_exidx_count = 0;
return -1;
}
#endif /* ANDROID_ARM_LINKER */

/* Return the address and size of the ELF file's .dynamic section in memory,
* or NULL if missing.
Expand Down Expand Up @@ -565,7 +577,7 @@ phdr_table_get_dynamic_section(const Elf_Phdr* phdr_table,

*dynamic = reinterpret_cast<Elf_Dyn*>(load_bias + phdr->p_vaddr);
if (dynamic_count) {
*dynamic_count = (unsigned)(phdr->p_memsz / 8);
*dynamic_count = (unsigned)(phdr->p_memsz / sizeof(Elf_Dyn));
}
if (dynamic_flags) {
*dynamic_flags = phdr->p_flags;
Expand Down Expand Up @@ -631,7 +643,7 @@ bool ElfReader::CheckPhdr(Elf_Addr loaded) {
return false;
}

bool ElfReader::LoadFileData(void *addr, size_t len, off_t offset) {
bool ElfReader::LoadFileData(void *addr, size_t len, int offset) {
lseek(fd_, offset, SEEK_SET);
auto rc = read(fd_, addr, len);

Expand Down
18 changes: 11 additions & 7 deletions ElfReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ typedef Elf64_Dyn Elf_Dyn;
typedef Elf64_Word Elf_Word;
#endif

// TODO fix phdr(file offset)
// TODO rebuild shdr
#define DL_ERR printf

class ElfReader {
public:
ElfReader(const char* name, int fd);
Expand All @@ -51,6 +51,7 @@ class ElfReader {
Elf_Addr load_bias() { return load_bias_; }
const Elf_Phdr* loaded_phdr() { return loaded_phdr_; }

const Elf_Ehdr* record_ehdr() { return &header_; }
private:
bool ReadElfHeader();
bool VerifyElfHeader();
Expand All @@ -59,7 +60,7 @@ class ElfReader {
bool LoadSegments();
bool FindPhdr();
bool CheckPhdr(Elf_Addr);
bool LoadFileData(void* addr, size_t len, off_t offset);
bool LoadFileData(void* addr, size_t len, int offset);

const char* name_;
int fd_;
Expand All @@ -80,6 +81,12 @@ class ElfReader {

// Loaded phdr.
const Elf_Phdr* loaded_phdr_;

// feature
public:
void setDumpSoFile(bool b) { dump_so_file_ = b; }
private:
bool dump_so_file_ = false;
};


Expand All @@ -106,14 +113,11 @@ phdr_table_protect_gnu_relro(const Elf_Phdr* phdr_table,
Elf_Addr load_bias);


#ifdef ANDROID_ARM_LINKER
int
phdr_table_get_arm_exidx(const Elf_Phdr* phdr_table,
int phdr_table_get_arm_exidx(const Elf_Phdr* phdr_table,
int phdr_count,
Elf_Addr load_bias,
Elf_Addr** arm_exidx,
unsigned* arm_exidix_count);
#endif

void
phdr_table_get_dynamic_section(const Elf_Phdr* phdr_table,
Expand Down
Loading

0 comments on commit c557fac

Please sign in to comment.