#include "gold.h"
#include <cerrno>
#include <cstring>
#include "safe-ctype.h"
#include "elfcpp.h"
#include "stringpool.h"
#include "fileread.h"
#include "output.h"
#include "binary.h"
namespace gold
{
Binary_to_elf::Binary_to_elf(elfcpp::EM machine, int size, bool big_endian,
const std::string& filename)
: elf_machine_(machine), size_(size), big_endian_(big_endian),
filename_(filename), data_(NULL), filesize_(0)
{
}
Binary_to_elf::~Binary_to_elf()
{
if (this->data_ != NULL)
delete[] this->data_;
}
bool
Binary_to_elf::convert(const Task* task)
{
if (this->size_ == 32)
{
if (!this->big_endian_)
{
#ifdef HAVE_TARGET_32_LITTLE
return this->sized_convert<32, false>(task);
#else
gold_unreachable();
#endif
}
else
{
#ifdef HAVE_TARGET_32_BIG
return this->sized_convert<32, true>(task);
#else
gold_unreachable();
#endif
}
}
else if (this->size_ == 64)
{
if (!this->big_endian_)
{
#ifdef HAVE_TARGET_64_LITTLE
return this->sized_convert<64, false>(task);
#else
gold_unreachable();
#endif
}
else
{
#ifdef HAVE_TARGET_64_BIG
return this->sized_convert<64, true>(task);
#else
gold_unreachable();
#endif
}
}
else
gold_unreachable();
}
template<int size, bool big_endian>
bool
Binary_to_elf::sized_convert(const Task* task)
{
File_read f;
if (!f.open(task, this->filename_))
{
gold_error(_("cannot open %s: %s:"), this->filename_.c_str(),
strerror(errno));
return false;
}
section_size_type filesize = convert_to_section_size_type(f.filesize());
const unsigned char* fileview = f.get_view(0, 0, filesize, false, false);
unsigned int align;
if (size == 32)
align = 4;
else if (size == 64)
align = 8;
else
gold_unreachable();
section_size_type aligned_filesize = align_address(filesize, align);
std::string mangled_name = this->filename_;
for (std::string::iterator p = mangled_name.begin();
p != mangled_name.end();
++p)
if (!ISALNUM(*p))
*p = '_';
mangled_name = "_binary_" + mangled_name;
std::string start_symbol_name = mangled_name + "_start";
std::string end_symbol_name = mangled_name + "_end";
std::string size_symbol_name = mangled_name + "_size";
Stringpool strtab;
strtab.add(start_symbol_name.c_str(), false, NULL);
strtab.add(end_symbol_name.c_str(), false, NULL);
strtab.add(size_symbol_name.c_str(), false, NULL);
strtab.set_string_offsets();
Stringpool shstrtab;
shstrtab.add(".data", false, NULL);
shstrtab.add(".symtab", false, NULL);
shstrtab.add(".strtab", false, NULL);
shstrtab.add(".shstrtab", false, NULL);
shstrtab.set_string_offsets();
const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
size_t output_size = (elfcpp::Elf_sizes<size>::ehdr_size
+ 5 * elfcpp::Elf_sizes<size>::shdr_size);
size_t data_offset = output_size;
output_size += aligned_filesize;
size_t symtab_offset = output_size;
output_size += 4 * sym_size;
size_t strtab_offset = output_size;
output_size += strtab.get_strtab_size();
size_t shstrtab_offset = output_size;
output_size += shstrtab.get_strtab_size();
unsigned char* buffer = new unsigned char[output_size];
unsigned char* pout = buffer;
this->write_file_header<size, big_endian>(&pout);
this->write_section_header<size, big_endian>("", &shstrtab, elfcpp::SHT_NULL,
0, 0, 0, 0, 0,
0, 0, &pout);
this->write_section_header<size, big_endian>(".data", &shstrtab,
elfcpp::SHT_PROGBITS,
(elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE),
data_offset,
filesize, 0, 0,
1, 0, &pout);
this->write_section_header<size, big_endian>(".symtab", &shstrtab,
elfcpp::SHT_SYMTAB,
0, symtab_offset, 4 * sym_size,
3, 1, align, sym_size, &pout);
this->write_section_header<size, big_endian>(".strtab", &shstrtab,
elfcpp::SHT_STRTAB,
0, strtab_offset,
strtab.get_strtab_size(),
0, 0, 1, 0, &pout);
this->write_section_header<size, big_endian>(".shstrtab", &shstrtab,
elfcpp::SHT_STRTAB,
0, shstrtab_offset,
shstrtab.get_strtab_size(),
0, 0, 1, 0, &pout);
memcpy(pout, fileview, filesize);
pout += filesize;
memset(pout, 0, aligned_filesize - filesize);
pout += aligned_filesize - filesize;
this->write_symbol<size, big_endian>("", &strtab, 0, 0, &pout);
this->write_symbol<size, big_endian>(start_symbol_name, &strtab, 0, 1,
&pout);
this->write_symbol<size, big_endian>(end_symbol_name, &strtab, filesize, 1,
&pout);
this->write_symbol<size, big_endian>(size_symbol_name, &strtab, filesize,
elfcpp::SHN_ABS, &pout);
strtab.write_to_buffer(pout, strtab.get_strtab_size());
pout += strtab.get_strtab_size();
shstrtab.write_to_buffer(pout, shstrtab.get_strtab_size());
pout += shstrtab.get_strtab_size();
gold_assert(static_cast<size_t>(pout - buffer) == output_size);
this->data_ = buffer;
this->filesize_ = output_size;
f.unlock(task);
return true;
}
template<int size, bool big_endian>
void
Binary_to_elf::write_file_header(unsigned char** ppout)
{
elfcpp::Ehdr_write<size, big_endian> oehdr(*ppout);
unsigned char e_ident[elfcpp::EI_NIDENT];
memset(e_ident, 0, elfcpp::EI_NIDENT);
e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
if (size == 32)
e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
else if (size == 64)
e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
else
gold_unreachable();
e_ident[elfcpp::EI_DATA] = (big_endian
? elfcpp::ELFDATA2MSB
: elfcpp::ELFDATA2LSB);
e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
oehdr.put_e_ident(e_ident);
oehdr.put_e_type(elfcpp::ET_REL);
oehdr.put_e_machine(this->elf_machine_);
oehdr.put_e_version(elfcpp::EV_CURRENT);
oehdr.put_e_entry(0);
oehdr.put_e_phoff(0);
oehdr.put_e_shoff(elfcpp::Elf_sizes<size>::ehdr_size);
oehdr.put_e_flags(0);
oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
oehdr.put_e_phentsize(0);
oehdr.put_e_phnum(0);
oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
oehdr.put_e_shnum(5);
oehdr.put_e_shstrndx(4);
*ppout += elfcpp::Elf_sizes<size>::ehdr_size;
}
template<int size, bool big_endian>
void
Binary_to_elf::write_section_header(
const char* name,
const Stringpool* shstrtab,
elfcpp::SHT type,
unsigned int flags,
section_size_type offset,
section_size_type section_size,
unsigned int link,
unsigned int info,
unsigned int addralign,
unsigned int entsize,
unsigned char** ppout)
{
elfcpp::Shdr_write<size, big_endian> oshdr(*ppout);
oshdr.put_sh_name(*name == '\0' ? 0 : shstrtab->get_offset(name));
oshdr.put_sh_type(type);
oshdr.put_sh_flags(flags);
oshdr.put_sh_addr(0);
oshdr.put_sh_offset(offset);
oshdr.put_sh_size(section_size);
oshdr.put_sh_link(link);
oshdr.put_sh_info(info);
oshdr.put_sh_addralign(addralign);
oshdr.put_sh_entsize(entsize);
*ppout += elfcpp::Elf_sizes<size>::shdr_size;
}
template<int size, bool big_endian>
void
Binary_to_elf::write_symbol(
const std::string& name,
const Stringpool* strtab,
section_size_type value,
unsigned int shndx,
unsigned char** ppout)
{
unsigned char* pout = *ppout;
elfcpp::Sym_write<size, big_endian> osym(pout);
osym.put_st_name(name.empty() ? 0 : strtab->get_offset(name.c_str()));
osym.put_st_value(value);
osym.put_st_size(0);
osym.put_st_info(name.empty() ? elfcpp::STB_LOCAL : elfcpp::STB_GLOBAL,
elfcpp::STT_NOTYPE);
osym.put_st_other(elfcpp::STV_DEFAULT, 0);
osym.put_st_shndx(shndx);
*ppout += elfcpp::Elf_sizes<size>::sym_size;
}
}