diff --git a/.gitignore b/.gitignore
index 6c34e37..9129d4c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,3 +33,6 @@ tools/extra
 # Sphinx build products
 docs/_build
 docs/analyzer/_build
+
+# random files
+*.debug
diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td
index 9d4fdad..062b729 100644
--- a/include/clang/Driver/Options.td
+++ b/include/clang/Driver/Options.td
@@ -212,7 +212,8 @@ def _HASH_HASH_HASH : Flag<["-"], "###">, Flags<[DriverOption, CoreOption]>,
 def _DASH_DASH : Option<["--"], "", KIND_REMAINING_ARGS>,
     Flags<[DriverOption, CoreOption]>;
 def A : JoinedOrSeparate<["-"], "A">, Flags<[RenderJoined]>;
-def B : JoinedOrSeparate<["-"], "B">;
+def B : JoinedOrSeparate<["-"], "B">, Flags<[RenderJoined]>,
+  HelpText<"Add search paths for compiler executables (ld,as)">;
 def CC : Flag<["-"], "CC">, Flags<[CC1Option]>;
 def C : Flag<["-"], "C">, Flags<[CC1Option]>;
 def D : JoinedOrSeparate<["-"], "D">, Group<CompileOnly_Group>, Flags<[CC1Option]>;
diff --git a/include/clang/Serialization/ASTBitCodes.h b/include/clang/Serialization/ASTBitCodes.h
index 7ae1977..ce3a228 100644
--- a/include/clang/Serialization/ASTBitCodes.h
+++ b/include/clang/Serialization/ASTBitCodes.h
@@ -83,21 +83,21 @@ namespace clang {
 
     /// \brief A type index; the type ID with the qualifier bits removed.
     class TypeIdx {
-      uint32_t Idx;
+      TypeID Idx;
     public:
       TypeIdx() : Idx(0) { }
-      explicit TypeIdx(uint32_t index) : Idx(index) { }
+      explicit TypeIdx(TypeID index) : Idx(index) { }
 
-      uint32_t getIndex() const { return Idx; }
+      TypeID getIndex() const { return Idx; }
       TypeID asTypeID(unsigned FastQuals) const {
-        if (Idx == uint32_t(-1))
+        if (Idx == TypeID(-1))
           return TypeID(-1);
         
         return (Idx << Qualifiers::FastWidth) | FastQuals;
       }
       static TypeIdx fromTypeID(TypeID ID) {
         if (ID == TypeID(-1))
-          return TypeIdx(-1);
+          return TypeIdx(TypeID(-1));
         
         return TypeIdx(ID >> Qualifiers::FastWidth);
       }
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index bccdae9..ae86f71 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -5165,6 +5165,7 @@ static char getObjCEncodingForPrimitiveKind(const ASTContext *C,
       llvm_unreachable("invalid builtin type for @encode");
     }
     llvm_unreachable("invalid BuiltinType::Kind value");
+    return '\0';	// unreachable, just to suppress warning
 }
 
 static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) {
diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp
index b46f69d..81af930 100644
--- a/lib/Driver/ToolChains.cpp
+++ b/lib/Driver/ToolChains.cpp
@@ -404,8 +404,20 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
     AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
   } else {
     assert(isTargetMacOS() && "unexpected non MacOS platform");
+
+    // darwin8 also has symbols defined in libSystemStubs.a
+    // does darwin9 need this too?
+    if (isMacosxVersionLT(10, 5))
+      CmdArgs.push_back("-lSystemStubs");
+    // use runtime functions from compiler-rt first
+    // does darwin9 need this too?  probably.  x86?
+    if (isMacosxVersionLT(10, 6))
+      if (getTriple().getArch() == llvm::Triple::ppc)
+        AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ppc.a");
+
     // The dynamic runtime library was merged with libSystem for 10.6 and
     // beyond; only 10.4 and 10.5 need an additional runtime library.
+    // These include the system libunwind.
     if (isMacosxVersionLT(10, 5))
       CmdArgs.push_back("-lgcc_s.10.4");
     else if (isMacosxVersionLT(10, 6))
@@ -602,6 +614,11 @@ void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
   switch (Type) {
   case ToolChain::CST_Libcxx:
     CmdArgs.push_back("-lc++");
+    // darwin8: now libc++ is statically linked to libsupc++.a
+    // instead of libc++abi.dylib, which appeared in darwin11+
+    if (DarwinVersion[0] < 11) {
+      CmdArgs.push_back("-lsupc++");
+    }
     break;
 
   case ToolChain::CST_Libstdcxx: {
@@ -1000,6 +1017,7 @@ bool MachO::SupportsProfiling() const {
 void Darwin::addMinVersionArgs(const llvm::opt::ArgList &Args,
                                llvm::opt::ArgStringList &CmdArgs) const {
   VersionTuple TargetVersion = getTargetVersion();
+  const std::string mmvs1(TargetVersion.getAsString());
 
   // If we had an explicit -mios-simulator-version-min argument, honor that,
   // otherwise use the traditional deployment targets. We can't just check the
@@ -1008,16 +1026,26 @@ void Darwin::addMinVersionArgs(const llvm::opt::ArgList &Args,
   //
   // FIXME: We may be able to remove this, once we can verify no one depends on
   // it.
-  if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ))
+  if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ)) {
     CmdArgs.push_back("-ios_simulator_version_min");
-  else if (isTargetIOSBased())
+    CmdArgs.push_back(Args.MakeArgString(mmvs1));
+  } else if (isTargetIOSBased()) {
     CmdArgs.push_back("-iphoneos_version_min");
-  else {
+    CmdArgs.push_back(Args.MakeArgString(mmvs1));
+  } else {
     assert(isTargetMacOS() && "unexpected target");
     CmdArgs.push_back("-macosx_version_min");
+    const unsigned vmin = *TargetVersion.getMinor();
+    if (vmin <= 4) {
+    const VersionTuple
+      MmTargetVersion(TargetVersion.getMajor(), vmin);
+      // for darwin8 ld: omit subminor number, so we get 10.4 instead of 10.4.0
+      const std::string mmvs2(MmTargetVersion.getAsString());
+      CmdArgs.push_back(Args.MakeArgString(mmvs2));
+    } else {
+      CmdArgs.push_back(Args.MakeArgString(mmvs1));
+    }
   }
-
-  CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString()));
 }
 
 void Darwin::addStartObjectFileArgs(const llvm::opt::ArgList &Args,
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index 198e82e..212ce67 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -5451,6 +5451,9 @@ void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
   // Derived from asm spec.
   AddMachOArch(Args, CmdArgs);
 
+  // We probably want to allow use of newer assemblers.
+  // Args.AddAllArgs(CmdArgs, options::OPT_B);
+
   // Use -force_cpusubtype_ALL on x86 by default.
   if (getToolChain().getArch() == llvm::Triple::x86 ||
       getToolChain().getArch() == llvm::Triple::x86_64 ||
@@ -5489,6 +5492,14 @@ void darwin::MachOTool::AddMachOArch(const ArgList &Args,
 
   // Derived from darwin_arch spec.
   CmdArgs.push_back("-arch");
+#if 1
+// suggested patch from http://llvm.org/bugs/show_bug.cgi?id=3830
+  if (ArchName == "powerpc")
+    CmdArgs.push_back("ppc");
+  else if (ArchName == "powerpc64")
+    CmdArgs.push_back("ppc64");
+  else
+#endif
   CmdArgs.push_back(Args.MakeArgString(ArchName));
 
   // FIXME: Is this needed anymore?
@@ -5715,6 +5726,8 @@ void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
   // we follow suite for ease of comparison.
   AddLinkArgs(C, Args, CmdArgs, Inputs);
 
+  // if 'gcc/g++' are used to drive the linker, but not if ld is called directly
+  // Args.AddAllArgs(CmdArgs, options::OPT_B);
   Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
   Args.AddAllArgs(CmdArgs, options::OPT_s);
   Args.AddAllArgs(CmdArgs, options::OPT_t);
diff --git a/test/Driver/target-triple-deployment.c b/test/Driver/target-triple-deployment.c
index 4f5de59..6389373 100644
--- a/test/Driver/target-triple-deployment.c
+++ b/test/Driver/target-triple-deployment.c
@@ -14,7 +14,7 @@
 
 // CHECK: {{ld(.exe)?"}}
 // CHECK: -macosx_version_min
-// CHECK: 10.4.0
+// CHECK: 10.4
 // CHECK: {{ld(.exe)?"}}
 // CHECK: -macosx_version_min
 // CHECK: 10.5.0
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index fc8703a..7ba2a15 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -6881,7 +6881,8 @@ void clang::setThreadBackgroundPriority() {
   if (getenv("LIBCLANG_BGPRIO_DISABLE"))
     return;
 
-#ifdef USE_DARWIN_THREADS
+// constants are not defined on darwin8, expected in <sys/resource.h>
+#if defined(USE_DARWIN_THREADS) && defined(PRIO_DARWIN_THREAD) && defined(PRIO_DARWIN_BG)
   setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG);
 #endif
 }
