From 47e879dfa735f81472a51b1bd69cc4eb9159db39 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Tue, 13 Jul 2010 23:31:40 +0000 Subject: [PATCH] Driver/Darwin: Pass -pie/-no_pie to the linker when -fpie/-fno-pie and friends are explicitly given. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@108297 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Driver/ArgList.h | 2 ++ include/clang/Driver/Options.td | 2 ++ lib/Driver/ArgList.cpp | 19 +++++++++++++++++++ lib/Driver/Tools.cpp | 11 +++++++++-- test/Driver/darwin-ld.c | 12 ++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/include/clang/Driver/ArgList.h b/include/clang/Driver/ArgList.h index 16396daa60..257b653f57 100644 --- a/include/clang/Driver/ArgList.h +++ b/include/clang/Driver/ArgList.h @@ -179,6 +179,8 @@ namespace driver { Arg *getLastArg(OptSpecifier Id) const; Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const; Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const; + Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2, + OptSpecifier Id3) const; /// getArgString - Return the input argument string at \arg Index. virtual const char *getArgString(unsigned Index) const = 0; diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index 96ec12215b..cdd05ca1f6 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -230,6 +230,7 @@ def exported__symbols__list : Separate<"-exported_symbols_list">; def e : JoinedOrSeparate<"-e">; def fPIC : Flag<"-fPIC">, Group; def fPIE : Flag<"-fPIE">, Group; +def fno_PIE : Flag<"-fno-PIE">, Group; def faccess_control : Flag<"-faccess-control">, Group; def fapple_kext : Flag<"-fapple-kext">, Group; def fasm : Flag<"-fasm">, Group; @@ -356,6 +357,7 @@ def fpascal_strings : Flag<"-fpascal-strings">, Group; def fpch_preprocess : Flag<"-fpch-preprocess">, Group; def fpic : Flag<"-fpic">, Group; def fpie : Flag<"-fpie">, Group; +def fno_pie : Flag<"-fno-pie">, Group; def fprofile_arcs : Flag<"-fprofile-arcs">, Group; def fprofile_generate : Flag<"-fprofile-generate">, Group; def framework : Separate<"-framework">, Flags<[LinkerInput]>; diff --git a/lib/Driver/ArgList.cpp b/lib/Driver/ArgList.cpp index 95fef89b85..9101523767 100644 --- a/lib/Driver/ArgList.cpp +++ b/lib/Driver/ArgList.cpp @@ -95,6 +95,25 @@ Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, return Res; } +Arg *ArgList::getLastArg(OptSpecifier Id0, OptSpecifier Id1, + OptSpecifier Id2, OptSpecifier Id3) const { + Arg *Res = 0; + for (const_reverse_iterator it = rbegin(), ie = rend(); it != ie; ++it) { + if ((*it)->getOption().matches(Id0) || + (*it)->getOption().matches(Id1) || + (*it)->getOption().matches(Id2) || + (*it)->getOption().matches(Id3)) { + Res = *it; + break; + } + } + + if (Res) + Res->claim(); + + return Res; +} + bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const { if (Arg *A = getLastArg(Pos, Neg)) return A->getOption().matches(Pos); diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 9e03a18ae0..e6465ac72c 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -2356,8 +2356,15 @@ void darwin::Link::AddLinkArgs(const ArgList &Args, Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined); Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused); - if (Args.hasArg(options::OPT_fpie)) - CmdArgs.push_back("-pie"); + if (const Arg *A = Args.getLastArg(options::OPT_fpie, options::OPT_fPIE, + options::OPT_fno_pie, + options::OPT_fno_PIE)) { + if (A->getOption().matches(options::OPT_fpie) || + A->getOption().matches(options::OPT_fPIE)) + CmdArgs.push_back("-pie"); + else + CmdArgs.push_back("-no_pie"); + } Args.AddLastArg(CmdArgs, options::OPT_prebind); Args.AddLastArg(CmdArgs, options::OPT_noprebind); diff --git a/test/Driver/darwin-ld.c b/test/Driver/darwin-ld.c index 0a89895676..448446885a 100644 --- a/test/Driver/darwin-ld.c +++ b/test/Driver/darwin-ld.c @@ -70,3 +70,15 @@ // LINK_IPHONE_3_1: ld" // LINK_IPHONE_3_1-NOT: -lbundle1.o // LINK_IPHONE_3_1: -lSystem + +// RUN: %clang -ccc-host-triple i386-apple-darwin9 -### -fpie %t.o 2> %t.log +// RUN: FileCheck -check-prefix=LINK_EXPLICIT_PIE %s < %t.log +// +// LINK_EXPLICIT_PIE: ld" +// LINK_EXPLICIT_PIE: "-pie" + +// RUN: %clang -ccc-host-triple i386-apple-darwin9 -### -fno-pie %t.o 2> %t.log +// RUN: FileCheck -check-prefix=LINK_EXPLICIT_NO_PIE %s < %t.log +// +// LINK_EXPLICIT_NO_PIE: ld" +// LINK_EXPLICIT_NO_PIE: "-no_pie" -- 2.40.0