From e916ab98b6d7d179b8e55230c35c1d3223c9196e Mon Sep 17 00:00:00 2001 From: Zuzana Baranova <xbaranov@fi.muni.cz> Date: Thu, 20 Jun 2019 08:33:15 +0000 Subject: [PATCH] CC: Move addSection() & ld_args() into cc/link, add include guard to .hpp. --- divine/cc/link.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++ divine/cc/link.hpp | 5 +++ tools/divcc.cpp | 57 ++----------------------------- 3 files changed, 91 insertions(+), 55 deletions(-) create mode 100644 divine/cc/link.cpp diff --git a/divine/cc/link.cpp b/divine/cc/link.cpp new file mode 100644 index 000000000..816f99e15 --- /dev/null +++ b/divine/cc/link.cpp @@ -0,0 +1,84 @@ +// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- + +/* + * (c) 2017 Jan Horáček <me@apophis.cz> + * (c) 2017-2019 Zuzana Baranová <xbaranov@fi.muni.cz> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <divine/cc/codegen.hpp> +#include <divine/cc/driver.hpp> +#include <divine/cc/link.hpp> + +DIVINE_RELAX_WARNINGS +#include "lld/Common/Driver.h" +DIVINE_UNRELAX_WARNINGS + +#include <brick-proc> + +namespace divine::cc +{ + void addSection( std::string filepath, std::string sectionName, const std::string §ionData ) + { + using namespace brick::fs; + + TempDir workdir( ".divine.addSection.XXXXXX", AutoDelete::Yes, + UseSystemTemp::Yes ); + auto secpath = joinPath( workdir, "sec" ); + std::ofstream secf( secpath, std::ios_base::out | std::ios_base::binary ); + secf << sectionData; + secf.close(); + + auto r = brick::proc::spawnAndWait( brick::proc::CaptureStderr, "objcopy", + "--remove-section", sectionName, // objcopy can't override section + "--add-section", sectionName + "=" + secpath, + "--set-section-flags", sectionName + "=noload,readonly", + filepath ); + if ( !r ) + throw cc::CompileError( "could not add section " + sectionName + " to " + filepath + + ", objcopy exited with " + to_string( r ) ); + } + + std::vector< std::string > ld_args( cc::ParsedOpts& po, PairedFiles& objFiles ) + { + std::vector< std::string > args; + + for ( auto op : po.opts ) + args.push_back( op ); + for ( auto path : po.libSearchPath ) + args.push_back( "-L" + path ); + if ( po.outputFile != "" ) + { + args.push_back( "-o" ); + args.push_back( po.outputFile ); + } + + for ( auto file : objFiles ) + { + if ( file.first == "lib" ) + { + args.push_back( "-l" + file.second ); + continue; + } + + if ( cc::is_object_type( file.first ) ) + args.push_back( file.first ); + else + args.push_back( file.second ); + } + args.insert( args.begin(), "divcc" ); + + return args; + } +} diff --git a/divine/cc/link.hpp b/divine/cc/link.hpp index 4cbbaedac..ef31ef99c 100644 --- a/divine/cc/link.hpp +++ b/divine/cc/link.hpp @@ -1,4 +1,5 @@ // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- +#pragma once /* * (c) 2017 Jan Horáček <me@apophis.cz> @@ -20,6 +21,7 @@ #include <memory> #include <divine/cc/cc1.hpp> #include <divine/cc/filetype.hpp> +#include <divine/cc/options.hpp> #include <divine/vm/xg-code.hpp> DIVINE_RELAX_WARNINGS @@ -60,6 +62,9 @@ namespace divine::cc } } + void addSection( std::string filepath, std::string sectionName, const std::string §ionData ); + std::vector< std::string > ld_args( cc::ParsedOpts& po, PairedFiles& objFiles ); + template < typename Driver, bool link_dios > std::unique_ptr< llvm::Module > link_bitcode( PairedFiles& files, cc::CC1& clang, std::vector< std::string > libSearchPath ) diff --git a/tools/divcc.cpp b/tools/divcc.cpp index 639150eb2..d34eab90b 100644 --- a/tools/divcc.cpp +++ b/tools/divcc.cpp @@ -46,28 +46,8 @@ using namespace brick::types; using PairedFiles = std::vector< std::pair< std::string, std::string > >; using FileType = cc::FileType; - -void addSection( std::string filepath, std::string sectionName, const std::string §ionData ) { using namespace brick::fs; - - TempDir workdir( ".divine.addSection.XXXXXX", AutoDelete::Yes, - UseSystemTemp::Yes ); - auto secpath = joinPath( workdir, "sec" ); - std::ofstream secf( secpath, std::ios_base::out | std::ios_base::binary ); - secf << sectionData; - secf.close(); - - auto r = brick::proc::spawnAndWait( brick::proc::CaptureStderr, "objcopy", - "--remove-section", sectionName, // objcopy can't override section - "--add-section", sectionName + "=" + secpath, - "--set-section-flags", sectionName + "=noload,readonly", - filepath ); - if ( !r ) - throw cc::CompileError( "could not add section " + sectionName + " to " + filepath - + ", objcopy exited with " + to_string( r ) ); -} - int compile( cc::ParsedOpts& po, cc::CC1& clang, PairedFiles& objFiles ) { for ( auto file : objFiles ) @@ -82,38 +62,6 @@ int compile( cc::ParsedOpts& po, cc::CC1& clang, PairedFiles& objFiles ) return 0; } -std::vector< std::string > ld_args( cc::ParsedOpts& po, PairedFiles& objFiles ) -{ - std::vector< std::string > args; - - for ( auto op : po.opts ) - args.push_back( op ); - for ( auto path : po.libSearchPath ) - args.push_back( "-L" + path ); - if ( po.outputFile != "" ) - { - args.push_back( "-o" ); - args.push_back( po.outputFile ); - } - - for ( auto file : objFiles ) - { - if ( file.first == "lib" ) - { - args.push_back( "-l" + file.second ); - continue; - } - - if ( cc::is_object_type( file.first ) ) - args.push_back( file.first ); - else - args.push_back( file.second ); - } - args.insert( args.begin(), "divcc" ); - - return args; -} - int compile_and_link( cc::ParsedOpts& po, cc::CC1& clang, PairedFiles& objFiles, bool cxx = false ) { auto diosCC = std::make_unique< rt::DiosCC >( clang.context() ); @@ -121,8 +69,6 @@ int compile_and_link( cc::ParsedOpts& po, cc::CC1& clang, PairedFiles& objFiles, compile( po, clang, objFiles ); - std::vector< std::string > args = ld_args( po, objFiles ); - using namespace brick::fs; TempDir tmpdir( ".divcc.XXXXXX", AutoDelete::Yes, UseSystemTemp::Yes ); auto hostlib = tmpdir.path + "/libdios-host.a", @@ -141,6 +87,7 @@ int compile_and_link( cc::ParsedOpts& po, cc::CC1& clang, PairedFiles& objFiles, writeFile( hostlib, rt::dios_host() ); args.push_back( hostlib ); + std::vector< std::string > args = cc::ld_args( po, objFiles ); ld_args_c.reserve( args.size() ); for ( size_t i = 0; i < args.size(); ++i ) ld_args_c.push_back( args[i].c_str() ); @@ -168,7 +115,7 @@ int compile_and_link( cc::ParsedOpts& po, cc::CC1& clang, PairedFiles& objFiles, std::unique_ptr< llvm::Module > mod = cc::link_bitcode< rt::DiosCC, true >( objFiles, clang, po.libSearchPath ); std::string file_out = po.outputFile != "" ? po.outputFile : "a.out"; - addSection( file_out, cc::llvm_section_name, clang.serializeModule( *mod ) ); + cc::addSection( file_out, cc::llvm_section_name, clang.serializeModule( *mod ) ); for ( auto file : objFiles ) { -- GitLab