From 3b5a9be6b95f33f04244e6df26905578a9081d82 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Sun, 16 Sep 2018 14:09:18 +0200 Subject: [PATCH] Emit position independent code by default, to enable support for shared libraries and PIE. PIE is now default on Ubuntu LTS. PIC can be disabled with --no-pic. --- src/asmgen.cpp | 4 ++-- src/asmgen.h | 2 +- src/main.cpp | 3 ++- src/options.cpp | 5 +++++ src/options.h | 4 +++- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/asmgen.cpp b/src/asmgen.cpp index 8ec6742..7b4a2a2 100644 --- a/src/asmgen.cpp +++ b/src/asmgen.cpp @@ -32,7 +32,7 @@ void registerTargets() { } llvm::TargetMachine* getTargetMachine(const std::string& triple, const std::string& cpu, - const std::string& features) { + const std::string& features, bool emit_pic) { std::string error; auto target = llvm::TargetRegistry::lookupTarget(triple, error); @@ -42,7 +42,7 @@ llvm::TargetMachine* getTargetMachine(const std::string& triple, const std::stri } llvm::TargetOptions opt; - auto reloc_model = llvm::Reloc::Model::Static; + auto reloc_model = emit_pic ? llvm::Reloc::Model::PIC_ : llvm::Reloc::Model::Static; return target->createTargetMachine(triple, cpu, features, opt, reloc_model); } diff --git a/src/asmgen.h b/src/asmgen.h index 7c39123..4d40def 100644 --- a/src/asmgen.h +++ b/src/asmgen.h @@ -15,7 +15,7 @@ namespace monicelli { void registerTargets(); llvm::TargetMachine* getTargetMachine(const std::string& triple, const std::string& cpu, - const std::string& features); + const std::string& features, bool emit_pic); void writeAssembly(const std::string& to_filename, llvm::Module* module, llvm::TargetMachine* target_machine); diff --git a/src/main.cpp b/src/main.cpp index 7c502ac..12ff167 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,7 +30,8 @@ int main(int argc, char** argv) { registerTargets(); auto triple = llvm::sys::getDefaultTargetTriple(); - auto target_machine = getTargetMachine(triple, options.getCPU(), options.getCPUFeatures()); + auto target_machine = + getTargetMachine(triple, options.getCPU(), options.getCPUFeatures(), options.shouldEmitPIC()); #ifdef MONICELLI_ENABLE_LINKER std::vector object_filenames; diff --git a/src/options.cpp b/src/options.cpp index 394cd35..07a954c 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -37,6 +37,10 @@ ProgramOptions ProgramOptions::fromCommandLine(int argc, char** argv) { options.skip_compile_ = true; continue; } + if (strcmp(argv[i], "--no-pic") == 0) { + options.emit_pic_ = false; + continue; + } #ifdef MONICELLI_ENABLE_LINKER if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--only-compile") == 0) { options.compile_only_ = true; @@ -88,6 +92,7 @@ void ProgramOptions::printHelp(const char* program_name) { " --print-ir, -s : Print the IR of the code.\n" " --cpu, -m model : Set the CPU model to this (default: generic).\n" " --cpu-features, -f feat : Enable these CPU features (default: none).\n" + " --no-pic : Disable position independent code.\n" " --help, -h : Print this message.\n" "\n"; exit(0); diff --git a/src/options.h b/src/options.h index add7d92..4420724 100644 --- a/src/options.h +++ b/src/options.h @@ -34,13 +34,14 @@ public: const std::string& getCPU() const { return cpu_; } const std::string& getCPUFeatures() const { return cpu_features_; } + bool shouldEmitPIC() const { return emit_pic_; } private: static void printHelp(const char* program_name); ProgramOptions() : print_ir_(false), print_ast_(false), trace_lexer_(false), compile_only_(false), - skip_compile_(false), cpu_("generic") {} + skip_compile_(false), cpu_("generic"), emit_pic_(true) {} bool print_ir_; bool print_ast_; @@ -51,6 +52,7 @@ private: std::string output_filename_; std::string cpu_; std::string cpu_features_; + bool emit_pic_; }; } // namespace monicelli