1 --- a/llvm-3.1/tools/clang/lib/Driver/Tools.cpp 2012-09-17 17:55:54.000000000 -0700 2 +++ b/llvm-3.1/tools/clang/lib/Driver/Tools.cpp 2012-09-17 19:43:56.194269627 -0700 3 @@ -654,6 +654,7 @@ 4 bool KernelOrKext) const { 5 const Driver &D = getToolChain().getDriver(); 6 llvm::Triple Triple = getToolChain().getTriple(); 7 + const bool isAndroid = Triple.getEnvironment() == llvm::Triple::ANDROID; 8 9 // Select the ABI to use. 10 // 11 @@ -756,7 +757,7 @@ 12 CmdArgs.push_back("-mno-global-merge"); 13 } 14 15 - if (Args.hasArg(options::OPT_mehabi)) { 16 + if (isAndroid || Args.hasArg(options::OPT_mehabi)) { 17 CmdArgs.push_back("-backend-option"); 18 CmdArgs.push_back("-arm-enable-ehabi"); 19 20 @@ -764,7 +765,7 @@ 21 CmdArgs.push_back("-arm-enable-ehabi-descriptors"); 22 } 23 24 - if (Args.hasArg(options::OPT_mignore_has_ras)) { 25 + if (isAndroid || Args.hasArg(options::OPT_mignore_has_ras)) { 26 CmdArgs.push_back("-backend-option"); 27 CmdArgs.push_back("-arm-ignore-has-ras"); 28 } 29 @@ -950,6 +951,8 @@ 30 31 void Clang::AddX86TargetArgs(const ArgList &Args, 32 ArgStringList &CmdArgs) const { 33 + const bool isAndroid = getToolChain().getTriple().getEnvironment() == 34 + llvm::Triple::ANDROID; 35 if (!Args.hasFlag(options::OPT_mred_zone, 36 options::OPT_mno_red_zone, 37 true) || 38 @@ -1026,6 +1029,11 @@ 39 // attributes here. 40 llvm::StringMap<unsigned> PrevFeature; 41 std::vector<const char*> Features; 42 + if (isAndroid) { 43 + // Add -msse3 44 + PrevFeature["sse3"] = Features.size() + 1; 45 + Features.push_back("+sse3"); 46 + } 47 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group), 48 ie = Args.filtered_end(); it != ie; ++it) { 49 StringRef Name = (*it)->getOption().getName(); 50 @@ -1337,6 +1345,8 @@ 51 bool KernelOrKext = Args.hasArg(options::OPT_mkernel, 52 options::OPT_fapple_kext); 53 const Driver &D = getToolChain().getDriver(); 54 + llvm::Triple Triple = getToolChain().getTriple(); 55 + const bool isAndroid = Triple.getEnvironment() == llvm::Triple::ANDROID; 56 ArgStringList CmdArgs; 57 58 assert(Inputs.size() == 1 && "Unable to handle multiple inputs."); 59 @@ -1517,15 +1527,42 @@ 60 options::OPT_fpic, options::OPT_fno_pic, 61 options::OPT_fPIE, options::OPT_fno_PIE, 62 options::OPT_fpie, options::OPT_fno_pie); 63 + int DefaultPICLevel = 0; 64 + bool DefaultStackRealign = false; 65 + if (isAndroid) { 66 + // add Android-specific default 67 + switch(getToolChain().getTriple().getArch()) { 68 + default: 69 + break; 70 + 71 + case llvm::Triple::arm: 72 + case llvm::Triple::thumb: 73 + case llvm::Triple::mips: 74 + case llvm::Triple::mipsel: 75 + case llvm::Triple::mips64: 76 + case llvm::Triple::mips64el: 77 + DefaultPICLevel = 1; // "-fpic" 78 + break; 79 + 80 + case llvm::Triple::x86: 81 + case llvm::Triple::x86_64: 82 + DefaultPICLevel = 2; // "-fPIC" 83 + DefaultStackRealign = true; 84 + break; 85 + } 86 + } 87 bool PICDisabled = false; 88 bool PICEnabled = false; 89 bool PICForPIE = false; 90 - if (LastPICArg) { 91 - PICForPIE = (LastPICArg->getOption().matches(options::OPT_fPIE) || 92 - LastPICArg->getOption().matches(options::OPT_fpie)); 93 + if (LastPICArg || DefaultPICLevel) { 94 + PICForPIE = (LastPICArg && 95 + (LastPICArg->getOption().matches(options::OPT_fPIE) || 96 + LastPICArg->getOption().matches(options::OPT_fpie))); 97 PICEnabled = (PICForPIE || 98 - LastPICArg->getOption().matches(options::OPT_fPIC) || 99 - LastPICArg->getOption().matches(options::OPT_fpic)); 100 + DefaultPICLevel || 101 + (LastPICArg && 102 + (LastPICArg->getOption().matches(options::OPT_fPIC) || 103 + LastPICArg->getOption().matches(options::OPT_fpic)))); 104 PICDisabled = !PICEnabled; 105 } 106 // Note that these flags are trump-cards. Regardless of the order w.r.t. the 107 @@ -1556,14 +1593,15 @@ 108 109 // Infer the __PIC__ and __PIE__ values. 110 if (ModelStr == "pic" && PICForPIE) { 111 + assert(LastPICArg && "-fPIE or -fpie should exist"); 112 CmdArgs.push_back("-pie-level"); 113 - CmdArgs.push_back((LastPICArg && 114 - LastPICArg->getOption().matches(options::OPT_fPIE)) ? 115 - "2" : "1"); 116 + CmdArgs.push_back(LastPICArg->getOption().matches(options::OPT_fPIE) ? "2":"1"); 117 } else if (ModelStr == "pic" || ModelStr == "dynamic-no-pic") { 118 + bool isPIC = DefaultPICLevel == 2 || 119 + (LastPICArg && 120 + LastPICArg->getOption().matches(options::OPT_fPIC)); 121 CmdArgs.push_back("-pic-level"); 122 - CmdArgs.push_back(((ModelStr != "dynamic-no-pic" && LastPICArg && 123 - LastPICArg->getOption().matches(options::OPT_fPIC)) || 124 + CmdArgs.push_back(((ModelStr != "dynamic-no-pic" && isPIC) || 125 getToolChain().getTriple().isOSDarwin()) ? "2" : "1"); 126 } 127 128 @@ -2170,13 +2208,13 @@ 129 130 // Translate -mstackrealign 131 if (Args.hasFlag(options::OPT_mstackrealign, options::OPT_mno_stackrealign, 132 - false)) { 133 + DefaultStackRealign)) { 134 CmdArgs.push_back("-backend-option"); 135 CmdArgs.push_back("-force-align-stack"); 136 } 137 if (!Args.hasFlag(options::OPT_mno_stackrealign, options::OPT_mstackrealign, 138 false)) { 139 - CmdArgs.push_back(Args.MakeArgString("-mstackrealign")); 140 + CmdArgs.push_back("-mstackrealign"); 141 } 142 143 if (Args.hasArg(options::OPT_mstack_alignment)) { 144 @@ -5059,6 +5097,8 @@ 145 const InputInfoList &Inputs, 146 const ArgList &Args, 147 const char *LinkingOutput) const { 148 + const bool isAndroid = getToolChain().getTriple().getEnvironment() == 149 + llvm::Triple::ANDROID; 150 ArgStringList CmdArgs; 151 152 // Add --32/--64 to make sure we get the format we want. 153 @@ -5109,11 +5149,12 @@ 154 options::OPT_fpic, options::OPT_fno_pic, 155 options::OPT_fPIE, options::OPT_fno_PIE, 156 options::OPT_fpie, options::OPT_fno_pie); 157 - if (LastPICArg && 158 - (LastPICArg->getOption().matches(options::OPT_fPIC) || 159 - LastPICArg->getOption().matches(options::OPT_fpic) || 160 - LastPICArg->getOption().matches(options::OPT_fPIE) || 161 - LastPICArg->getOption().matches(options::OPT_fpie))) { 162 + if (isAndroid || 163 + (LastPICArg && 164 + (LastPICArg->getOption().matches(options::OPT_fPIC) || 165 + LastPICArg->getOption().matches(options::OPT_fpic) || 166 + LastPICArg->getOption().matches(options::OPT_fPIE) || 167 + LastPICArg->getOption().matches(options::OPT_fpie)))) { 168 CmdArgs.push_back("-KPIC"); 169 } 170 } 171 @@ -5141,8 +5182,8 @@ 172 173 static void AddLibgcc(llvm::Triple Triple, const Driver &D, 174 ArgStringList &CmdArgs, const ArgList &Args) { 175 - bool isAndroid = Triple.getEnvironment() == llvm::Triple::ANDROID; 176 - bool StaticLibgcc = Args.hasArg(options::OPT_static) || 177 + const bool isAndroid = Triple.getEnvironment() == llvm::Triple::ANDROID; 178 + const bool StaticLibgcc = Args.hasArg(options::OPT_static) || 179 Args.hasArg(options::OPT_static_libgcc); 180 if (!D.CCCIsCXX) 181 CmdArgs.push_back("-lgcc"); 182