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.
This commit is contained in:
Stefano Sanfilippo 2018-09-16 14:09:18 +02:00
parent ab596da379
commit 3b5a9be6b9
5 changed files with 13 additions and 5 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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<std::string> object_filenames;

View File

@ -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);

View File

@ -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