diff options
author | Chandler Carruth <chandlerc@gmail.com> | 2012-04-08 16:40:35 +0000 |
---|---|---|
committer | Chandler Carruth <chandlerc@gmail.com> | 2012-04-08 16:40:35 +0000 |
commit | 5e219cf7f896873c1c1e64b9e87a7dade99debba (patch) | |
tree | 0f59d5fc2946acc858cb49188b692649fc3ea42e /lib/Driver/Tools.cpp | |
parent | 95794225e9f80d4b45d4767e0b4b43f818c585da (diff) |
Teach Clang about PIE compilations. This is the first step of PR12380.
First, this patch cleans up the parsing of the PIC and PIE family of
options in the driver. The existing logic failed to claim arguments all
over the place resulting in kludges that marked the options as unused.
Instead actually walk all of the arguments and claim them properly.
We now treat -f{,no-}{pic,PIC,pie,PIE} as a single set, accepting the
last one on the commandline. Previously there were lots of ordering bugs
that could creep in due to the nature of the parsing. Let me know if
folks would like weird things such as "-fPIE -fno-pic" to turn on PIE,
but disable full PIC. This doesn't make any sense to me, but we could in
theory support it.
Options that seem to have intentional "trump" status (-static, -mkernel,
etc) continue to do so and are commented as such.
Next, a -pie-level flag is threaded into the frontend, rigged to
a language option, and handled preprocessor, setting up the appropriate
defines. We'll now have the correct defines when compiling with -fpie.
The one place outside of the preprocessor that was inspecting the PIC
level (as opposed to the relocation model, which is set and handled
separately, yay!) is in the GNU ObjC runtime. I changed it to exactly
preserve existing behavior. If folks want to change its behavior in the
face of PIE, they can do that in a separate patch.
Essentially the only functionality changed here is the preprocessor
defines and bug-fixes to the argument management.
Tests have been updated and extended to test all of this a bit more
thoroughly.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154291 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Driver/Tools.cpp')
-rw-r--r-- | lib/Driver/Tools.cpp | 66 |
1 files changed, 48 insertions, 18 deletions
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index fd064d5bb5..14842f36e7 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -1480,19 +1480,43 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // would do to enable flag_pic. // // FIXME: Centralize this code. - bool PICEnabled = (Args.hasArg(options::OPT_fPIC) || - Args.hasArg(options::OPT_fpic) || - Args.hasArg(options::OPT_fPIE) || - Args.hasArg(options::OPT_fpie)); - bool PICDisabled = (Args.hasArg(options::OPT_mkernel) || - Args.hasArg(options::OPT_static) || - Args.hasArg(options::OPT_fno_PIC) || - Args.hasArg(options::OPT_fno_pic) || - Args.hasArg(options::OPT_fno_PIE) || - Args.hasArg(options::OPT_fno_pie)); + Arg *LastPICArg = 0; + for (ArgList::const_iterator I = Args.begin(), E = Args.end(); I != E; ++I) { + if ((*I)->getOption().matches(options::OPT_fPIC) || + (*I)->getOption().matches(options::OPT_fno_PIC) || + (*I)->getOption().matches(options::OPT_fpic) || + (*I)->getOption().matches(options::OPT_fno_pic) || + (*I)->getOption().matches(options::OPT_fPIE) || + (*I)->getOption().matches(options::OPT_fno_PIE) || + (*I)->getOption().matches(options::OPT_fpie) || + (*I)->getOption().matches(options::OPT_fno_pie)) { + LastPICArg = *I; + (*I)->claim(); + } + } + bool PICDisabled = false; + bool PICEnabled = false; + bool PICForPIE = false; + if (LastPICArg) { + PICForPIE = (LastPICArg->getOption().matches(options::OPT_fPIE) || + LastPICArg->getOption().matches(options::OPT_fpie)); + PICEnabled = (PICForPIE || + LastPICArg->getOption().matches(options::OPT_fPIC) || + LastPICArg->getOption().matches(options::OPT_fpic)); + PICDisabled = !PICEnabled; + } + // Note that these flags are trump-cards. Regardless of the order w.r.t. the + // PIC or PIE options above, if these show up, PIC is disabled. + if (Args.hasArg(options::OPT_mkernel)) + PICDisabled = true; + if (Args.hasArg(options::OPT_static)) + PICDisabled = true; + bool DynamicNoPIC = Args.hasArg(options::OPT_mdynamic_no_pic); + + // Select the relocation model. const char *Model = getToolChain().GetForcedPicModel(); if (!Model) { - if (Args.hasArg(options::OPT_mdynamic_no_pic)) + if (DynamicNoPIC) Model = "dynamic-no-pic"; else if (PICDisabled) Model = "static"; @@ -1501,19 +1525,25 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, else Model = getToolChain().GetDefaultRelocationModel(); } - if (StringRef(Model) != "pic") { + StringRef ModelStr = Model ? Model : ""; + if (Model && ModelStr != "pic") { CmdArgs.push_back("-mrelocation-model"); CmdArgs.push_back(Model); } - // Infer the __PIC__ value. - // - // FIXME: This isn't quite right on Darwin, which always sets - // __PIC__=2. - if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) { + // Infer the __PIC__ and __PIE__ values. + if (ModelStr == "pic" && PICForPIE) { + CmdArgs.push_back("-pie-level"); + CmdArgs.push_back((LastPICArg && + LastPICArg->getOption().matches(options::OPT_fPIE)) ? + "2" : "1"); + } else if (ModelStr == "pic" || ModelStr == "dynamic-no-pic") { CmdArgs.push_back("-pic-level"); - CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1"); + CmdArgs.push_back(((ModelStr != "dynamic-no-pic" && LastPICArg && + LastPICArg->getOption().matches(options::OPT_fPIC)) || + getToolChain().getTriple().isOSDarwin()) ? "2" : "1"); } + if (!Args.hasFlag(options::OPT_fmerge_all_constants, options::OPT_fno_merge_all_constants)) CmdArgs.push_back("-fno-merge-all-constants"); |