Exemplo n.º 1
0
void Convert(std::string pe_file_name, std::string xky_file_name)
{
	//Creamos el fichero PE
	PEFile pe_file(pe_file_name);
	
	//Testear que exista una seccion ".module"
	if(!pe_file.GetSectionHeaderByName(MODULE_SECTION_NAME))
		throw std::string(".module section doesn't exists");

	//Creamos el fichero linkado
	XFile xky_file(CalculateXkyFileSize(&pe_file, XKY_SECTION_ALIGNMENT, XKY_HEADER_ALIGNMENT));

	//Volcamos la cabecera
	DumpHeader(&xky_file, &pe_file, XKY_HEADER_ALIGNMENT);

	//Volcamos por este orden:
	//	.import
	//	.data
	//	.code
	//	.export
	DumpSection(&xky_file, &pe_file, IMPORT_SECTION_NAME, XKY_SECTION_ALIGNMENT);
	DumpSection(&xky_file, &pe_file, DATA_SECTION_NAME,   XKY_SECTION_ALIGNMENT);
	DumpSection(&xky_file, &pe_file, CODE_SECTION_NAME,   XKY_SECTION_ALIGNMENT);
	DumpSection(&xky_file, &pe_file, EXPORT_SECTION_NAME, XKY_SECTION_ALIGNMENT);

	//Volcamos las relocs
//	xky_module_header->relocs_section.offset = xky_file_write_pointer;
//	xky_module_header->relocs_section.size = xky_file_size - xky_file_write_pointer;
	DumpRelocs(&xky_file, &pe_file, XKY_SECTION_ALIGNMENT);

	//Volcamos a disco
	if(!xky_file.FlushToDisk(xky_file_name))
		throw std::string("Cant open dump disk file: ") + xky_file_name;
}
Exemplo n.º 2
0
static int
DoDump(const std::string& in_filename,
       yasm::SourceManager& source_mgr,
       yasm::Diagnostic& diags)
{
    yasm::FileManager file_mgr;

    // open the input file or STDIN (for filename of "-")
    if (in_filename == "-")
    {
        source_mgr.createMainFileIDForMemBuffer(llvm::MemoryBuffer::getSTDIN());
    }
    else
    {
        const yasm::FileEntry* in = file_mgr.getFile(in_filename);
        if (!in)
        {
            diags.Report(yasm::SourceLocation(), yasm::diag::err_file_open)
                << in_filename;
        }
        source_mgr.createMainFileID(in, yasm::SourceLocation());
    }

    const llvm::MemoryBuffer* in_file =
        source_mgr.getBuffer(source_mgr.getMainFileID());
    yasm::SourceLocation sloc =
        source_mgr.getLocForStartOfFile(source_mgr.getMainFileID());

    std::auto_ptr<yasm::ObjectFormatModule> objfmt_module(0);
    std::string arch_keyword, machine;

    if (!objfmt_keyword.empty())
    {
        objfmt_keyword = llvm::LowercaseString(objfmt_keyword);
        if (!yasm::isModule<yasm::ObjectFormatModule>(objfmt_keyword))
        {
            diags.Report(sloc, yasm::diag::err_unrecognized_object_format)
                << objfmt_keyword;
            return EXIT_FAILURE;
        }

        // Object format forced by user
        objfmt_module =
            yasm::LoadModule<yasm::ObjectFormatModule>(objfmt_keyword);

        if (objfmt_module.get() == 0)
        {
            diags.Report(sloc, yasm::diag::fatal_module_load)
                << "object format" << objfmt_keyword;
            return EXIT_FAILURE;
        }

        if (!objfmt_module->Taste(*in_file, &arch_keyword, &machine))
        {
            diags.Report(sloc, yasm::diag::err_unrecognized_object_file)
                << objfmt_module->getKeyword();
            return EXIT_FAILURE;
        }
    }
    else
    {
        // Need to loop through available object formats, and taste each one
        yasm::ModuleNames list = yasm::getModules<yasm::ObjectFormatModule>();
        yasm::ModuleNames::iterator i=list.begin(), end=list.end();
        for (; i != end; ++i)
        {
            objfmt_module = yasm::LoadModule<yasm::ObjectFormatModule>(*i);
            if (objfmt_module->Taste(*in_file, &arch_keyword, &machine))
                break;
        }
        if (i == end)
        {
            diags.Report(sloc, yasm::diag::err_unrecognized_file_format);
            return EXIT_FAILURE;
        }
    }

    std::auto_ptr<yasm::ArchModule> arch_module =
        yasm::LoadModule<yasm::ArchModule>(arch_keyword);
    if (arch_module.get() == 0)
    {
        diags.Report(sloc, yasm::diag::fatal_module_load)
            << "architecture" << arch_keyword;
        return EXIT_FAILURE;
    }

    std::auto_ptr<yasm::Arch> arch = arch_module->Create();
    if (!arch->setMachine(machine))
    {
        diags.Report(sloc, yasm::diag::fatal_module_combo)
            << "machine" << machine
            << "architecture" << arch_module->getKeyword();
        return EXIT_FAILURE;
    }

    yasm::Object object("", in_filename, arch.get());

    if (!objfmt_module->isOkObject(object))
    {
        diags.Report(sloc, yasm::diag::fatal_objfmt_machine_mismatch)
            << objfmt_module->getKeyword()
            << arch_module->getKeyword()
            << arch->getMachine();
        return EXIT_FAILURE;
    }

    std::auto_ptr<yasm::ObjectFormat> objfmt = objfmt_module->Create(object);
    if (!objfmt->Read(source_mgr, diags))
        return EXIT_FAILURE;

    llvm::outs() << in_filename << ":     file format "
                 << objfmt_module->getKeyword() << "\n\n";

    if (show_section_headers)
        DumpSectionHeaders(object);
    if (show_symbols)
        DumpSymbols(object);
    if (show_relocs)
        DumpRelocs(object);
    if (show_contents)
        DumpContents(object);
    return EXIT_SUCCESS;
}