Skip to content

Goto-cc extensions to build (and link) recent Linux kernels #2030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 131 additions & 119 deletions src/goto-cc/compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Date: June 2006

#include "compile.h"

#include <cstring>
#include <fstream>
#include <sstream>
#include <iostream>
Expand Down Expand Up @@ -135,56 +136,114 @@ bool compilet::doit()
warnings_before;
}

/// puts input file names into a list and does preprocessing for libraries.
/// \return false on success, true on error.
bool compilet::add_input_file(const std::string &file_name)
enum class file_typet
{
FAILED_TO_OPEN_FILE,
UNKNOWN,
SOURCE_FILE,
NORMAL_ARCHIVE,
THIN_ARCHIVE,
GOTO_BINARY,
ELF_OBJECT
};

static file_typet detect_file_type(const std::string &file_name)
{
// first of all, try to open the file
{
std::ifstream in(file_name);
if(!in)
{
warning() << "failed to open file `" << file_name << "'" << eom;
return warning_is_fatal; // generously ignore unless -Werror
}
}
std::ifstream in(file_name);
if(!in)
return file_typet::FAILED_TO_OPEN_FILE;

const std::string::size_type r = file_name.rfind('.');

size_t r=file_name.rfind('.', file_name.length()-1);
const std::string ext =
r == std::string::npos ? "" : file_name.substr(r + 1, file_name.length());

if(r==std::string::npos)
if(
ext == "c" || ext == "cc" || ext == "cp" || ext == "cpp" || ext == "CPP" ||
ext == "c++" || ext == "C" || ext == "i" || ext == "ii" || ext == "class" ||
ext == "jar" || ext == "jsil")
{
// a file without extension; will ignore
warning() << "input file `" << file_name
<< "' has no extension, not considered" << eom;
return warning_is_fatal;
return file_typet::SOURCE_FILE;
}

std::string ext = file_name.substr(r+1, file_name.length());

if(ext=="c" ||
ext=="cc" ||
ext=="cp" ||
ext=="cpp" ||
ext=="CPP" ||
ext=="c++" ||
ext=="C" ||
ext=="i" ||
ext=="ii" ||
ext=="class" ||
ext=="jar" ||
ext=="jsil")
char hdr[8];
in.get(hdr, 8);
if((ext == "a" || ext == "o") && strncmp(hdr, "!<thin>", 8) == 0)
return file_typet::THIN_ARCHIVE;

if(ext == "a")
return file_typet::NORMAL_ARCHIVE;

if(is_goto_binary(file_name))
return file_typet::GOTO_BINARY;

if(hdr[0] == 0x7f && memcmp(hdr + 1, "ELF", 3) == 0)
return file_typet::ELF_OBJECT;

return file_typet::UNKNOWN;
}

/// puts input file names into a list and does preprocessing for libraries.
/// \return false on success, true on error.
bool compilet::add_input_file(const std::string &file_name)
{
switch(detect_file_type(file_name))
{
case file_typet::FAILED_TO_OPEN_FILE:
warning() << "failed to open file `" << file_name
<< "': " << std::strerror(errno) << eom;
return warning_is_fatal; // generously ignore unless -Werror

case file_typet::UNKNOWN:
// unknown extension, not a goto binary, will silently ignore
debug() << "unknown file type in `" << file_name << "'" << eom;
return false;

case file_typet::ELF_OBJECT:
// ELF file without goto-cc section, silently ignore
debug() << "ELF object without goto-cc section: `" << file_name << "'"
<< eom;
return false;

case file_typet::SOURCE_FILE:
source_files.push_back(file_name);
return false;

case file_typet::NORMAL_ARCHIVE:
return add_files_from_archive(file_name, false);

case file_typet::THIN_ARCHIVE:
return add_files_from_archive(file_name, true);

case file_typet::GOTO_BINARY:
object_files.push_back(file_name);
return false;
}
else if(ext=="a")
{
#ifdef _WIN32
char td[MAX_PATH+1];
#else
char td[] = "goto-cc.XXXXXX";
#endif

std::string tstr=get_temporary_directory(td);
UNREACHABLE;
}

/// extracts goto binaries from AR archive and add them as input files.
/// \return false on success, true on error.
bool compilet::add_files_from_archive(
const std::string &file_name,
bool thin_archive)
{
#ifdef _WIN32
char td[MAX_PATH + 1];
#else
char td[] = "goto-cc.XXXXXX";
#endif

std::stringstream cmd;
FILE *stream;

std::string tstr = working_directory;

if(!thin_archive)
{
tstr = get_temporary_directory(td);

if(tstr=="")
{
Expand All @@ -193,7 +252,6 @@ bool compilet::add_input_file(const std::string &file_name)
}

tmp_dirs.push_back(tstr);
std::stringstream cmd("");
if(chdir(tmp_dirs.back().c_str())!=0)
{
error() << "Cannot switch to temporary directory" << eom;
Expand All @@ -203,76 +261,47 @@ bool compilet::add_input_file(const std::string &file_name)
// unpack now
cmd << "ar x " << concat_dir_file(working_directory, file_name);

FILE *stream;

stream=popen(cmd.str().c_str(), "r");
pclose(stream);

cmd.clear();
cmd.str("");
}

// add the files from "ar t"
#ifdef _WIN32
if(file_name[0]!='/' && file_name[1]!=':') // NOLINT(readability/braces)
#else
if(file_name[0]!='/') // NOLINT(readability/braces)
#endif
{
cmd << "ar t " <<
#ifdef _WIN32
working_directory << "\\" << file_name;
#else
working_directory << "/" << file_name;
#endif
}
else
{
cmd << "ar t " << file_name;
}
// add the files from "ar t"
cmd << "ar t " << concat_dir_file(working_directory, file_name);

stream=popen(cmd.str().c_str(), "r");
stream = popen(cmd.str().c_str(), "r");

if(stream!=nullptr)
if(stream != nullptr)
{
std::string line;
int ch; // fgetc returns an int, not char
while((ch = fgetc(stream)) != EOF)
{
std::string line;
int ch; // fgetc returns an int, not char
while((ch=fgetc(stream))!=EOF)
if(ch != '\n')
{
if(ch!='\n')
{
line+=static_cast<char>(ch);
}
else
{
std::string t;
#ifdef _WIN32
t = tmp_dirs.back() + '\\' + line;
#else
t = tmp_dirs.back() + '/' + line;
#endif

if(is_goto_binary(t))
object_files.push_back(t);

line = "";
}
line += static_cast<char>(ch);
}
else
{
std::string t = concat_dir_file(tstr, line);

pclose(stream);
}
if(is_goto_binary(t))
object_files.push_back(t);
else
debug() << "Object file is not a goto binary: " << line << eom;

cmd.str("");
line = "";
}
}

if(chdir(working_directory.c_str())!=0)
error() << "Could not change back to working directory" << eom;
}
else if(is_goto_binary(file_name))
object_files.push_back(file_name);
else
{
// unknown extension, not a goto binary, will silently ignore
pclose(stream);
}

if(!thin_archive && chdir(working_directory.c_str()) != 0)
error() << "Could not change back to working directory" << eom;

return false;
}

Expand Down Expand Up @@ -302,41 +331,24 @@ bool compilet::find_library(const std::string &name)
{
std::string libname=tmp+name+".so";

if(is_goto_binary(libname))
return !add_input_file(libname);
else if(is_elf_file(libname))
switch(detect_file_type(libname))
{
case file_typet::GOTO_BINARY:
return !add_input_file(libname);

case file_typet::ELF_OBJECT:
warning() << "Warning: Cannot read ELF library " << libname << eom;
return warning_is_fatal;

default:
break;
}
}
}

return false;
}

/// checking if we can load an object file
/// \par parameters: file name
/// \return true if the given file name exists and is an ELF file, false
/// otherwise
bool compilet::is_elf_file(const std::string &file_name)
{
std::fstream in;

in.open(file_name, std::ios::in);
if(in.is_open())
{
char buf[4];
for(std::size_t i=0; i<4; i++)
buf[i]=static_cast<char>(in.get());
if(buf[0]==0x7f && buf[1]=='E' &&
buf[2]=='L' && buf[3]=='F')
return true;
}

return false;
}

/// parses object files and links them
/// \return true on error, false otherwise
bool compilet::link()
Expand Down
2 changes: 1 addition & 1 deletion src/goto-cc/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class compilet:public language_uit

bool add_input_file(const std::string &);
bool find_library(const std::string &);
bool is_elf_file(const std::string &);
bool add_files_from_archive(const std::string &file_name, bool thin_archive);

bool parse(const std::string &filename);
bool parse_stdin();
Expand Down
10 changes: 7 additions & 3 deletions src/goto-cc/gcc_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ Author: CM Wintersteiger, 2006

#include <util/prefix.h>

/// parses the command line options into a cmdlinet
/// \par parameters: argument count, argument strings
/// \return none
// clang-format off
// non-gcc options
const char *goto_cc_options_with_separated_argument[]=
{
Expand Down Expand Up @@ -163,6 +161,8 @@ const char *gcc_options_without_argument[]=
"-print-multi-directory",
"-print-multi-lib",
"-print-search-dirs",
"-print-sysroot",
"-print-sysroot-headers-suffix",
"-Q",
"-Qn",
"-Qy",
Expand Down Expand Up @@ -211,7 +211,11 @@ const char *gcc_options_without_argument[]=
"-fast", // Apple only
nullptr
};
// clang-format on

/// parses the command line options into a cmdlinet
/// \par parameters: argument count, argument strings
/// \return none
bool gcc_cmdlinet::parse(int argc, const char **argv)
{
assert(argc>0);
Expand Down
16 changes: 14 additions & 2 deletions src/goto-cc/gcc_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,24 @@ int gcc_modet::doit()
return EX_OK; // Exit!
}

if(cmdline.isset("dumpversion"))
if(
cmdline.isset("dumpmachine") || cmdline.isset("dumpspecs") ||
cmdline.isset("dumpversion") || cmdline.isset("print-sysroot") ||
cmdline.isset("print-sysroot-headers-suffix"))
{
if(produce_hybrid_binary)
return run_gcc(compiler);

std::cout << "3.4.4\n";
// GCC will only print one of these, even when multiple arguments are
// passed, so we do the same
if(cmdline.isset("dumpmachine"))
std::cout << config.this_architecture() << '\n';
else if(cmdline.isset("dumpversion"))
std::cout << "3.4.4\n";

// we don't have any meaningful output for the other options, and GCC
// doesn't necessarily produce non-empty output either

return EX_OK;
}

Expand Down
Loading