Merge "Avoid permitted_packages related build failures in unbundled builds"
diff --git a/cmd/extract_linker/main.go b/cmd/extract_linker/main.go
index 2dcb894..f1f7bc7 100644
--- a/cmd/extract_linker/main.go
+++ b/cmd/extract_linker/main.go
@@ -85,7 +85,7 @@
 
 		fmt.Fprintf(asm, ".globl %s\n%s:\n\n", symName, symName)
 
-		fmt.Fprintf(script, "  %s %d : {\n", sectionName, baseLoadAddr+prog.Vaddr)
+		fmt.Fprintf(script, "  %s 0x%x : {\n", sectionName, baseLoadAddr+prog.Vaddr)
 		fmt.Fprintf(script, "    KEEP(*(%s));\n", sectionName)
 		fmt.Fprintln(script, "  }")
 
@@ -106,8 +106,10 @@
 		load += 1
 	}
 
+	fmt.Fprintln(asm, `.section .note.android.embedded_linker,"a",%note`)
+
 	fmt.Fprintln(script, "}")
-	fmt.Fprintln(script, "INSERT BEFORE .note.android.ident;")
+	fmt.Fprintln(script, "INSERT BEFORE .note.android.embedded_linker;")
 
 	if asmPath != "" {
 		if err := ioutil.WriteFile(asmPath, asm.Bytes(), 0777); err != nil {
diff --git a/dexpreopt/class_loader_context.go b/dexpreopt/class_loader_context.go
index 94b9adf..245af2c 100644
--- a/dexpreopt/class_loader_context.go
+++ b/dexpreopt/class_loader_context.go
@@ -286,11 +286,19 @@
 	}
 	subcontexts := nestedClcMap[AnySdkVersion]
 
-	// If the library with this name is already present as one of the unconditional top-level
-	// components, do not re-add it.
+	// Check if the library with this name is already present in unconditional top-level CLC.
 	for _, clc := range clcMap[sdkVer] {
-		if clc.Name == lib {
+		if clc.Name != lib {
+			// Ok, a different library.
+		} else if clc.Host == hostPath && clc.Device == devicePath {
+			// Ok, the same library with the same paths. Don't re-add it, but don't raise an error
+			// either, as the same library may be reachable via different transitional dependencies.
 			return nil
+		} else {
+			// Fail, as someone is trying to add the same library with different paths. This likely
+			// indicates an error somewhere else, like trying to add a stub library.
+			return fmt.Errorf("a <uses-library> named %q is already in class loader context,"+
+				"but the library paths are different:\t\n", lib)
 		}
 	}
 
diff --git a/java/app.go b/java/app.go
index 4456b22..4e967ad 100755
--- a/java/app.go
+++ b/java/app.go
@@ -1264,6 +1264,15 @@
 
 		dep := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(m))
 
+		// Skip stub libraries. A dependency on the implementation library has been added earlier,
+		// so it will be added to CLC, but the stub shouldn't be. Stub libraries can be distingushed
+		// from implementation libraries by their name, which is different as it has a suffix.
+		if comp, ok := m.(SdkLibraryComponentDependency); ok {
+			if impl := comp.OptionalSdkLibraryImplementation(); impl != nil && *impl != dep {
+				return
+			}
+		}
+
 		if lib, ok := m.(UsesLibraryDependency); ok {
 			libName := dep
 			if ulib, ok := m.(ProvidesUsesLib); ok && ulib.ProvidesUsesLib() != nil {
diff --git a/java/java.go b/java/java.go
index bd059b5..83e9fe1 100644
--- a/java/java.go
+++ b/java/java.go
@@ -1786,22 +1786,16 @@
 		return
 	}
 
-	// Find out if the dependency is either an SDK library or an ordinary library that is disguised
-	// as an SDK library by the means of `provides_uses_lib` property. If yes, the library is itself
-	// a <uses-library> and should be added as a node in the CLC tree, and its CLC should be added
-	// as subtree of that node. Otherwise the library is not a <uses_library> and should not be
-	// added to CLC, but the transitive <uses-library> dependencies from its CLC should be added to
-	// the current CLC.
-	var implicitSdkLib *string
-	comp, isComp := depModule.(SdkLibraryComponentDependency)
-	if isComp {
-		implicitSdkLib = comp.OptionalImplicitSdkLibrary()
-		// OptionalImplicitSdkLibrary() may be nil so need to fall through to ProvidesUsesLib().
-	}
-	if implicitSdkLib == nil {
-		if ulib, ok := depModule.(ProvidesUsesLib); ok {
-			implicitSdkLib = ulib.ProvidesUsesLib()
-		}
+	depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule))
+
+	var sdkLib *string
+	if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() {
+		// A shared SDK library. This should be added as a top-level CLC element.
+		sdkLib = &depName
+	} else if ulib, ok := depModule.(ProvidesUsesLib); ok {
+		// A non-SDK library disguised as an SDK library by the means of `provides_uses_lib`
+		// property. This should be handled in the same way as a shared SDK library.
+		sdkLib = ulib.ProvidesUsesLib()
 	}
 
 	depTag := ctx.OtherModuleDependencyTag(depModule)
@@ -1811,7 +1805,7 @@
 		// Propagate <uses-library> through static library dependencies, unless it is a component
 		// library (such as stubs). Component libraries have a dependency on their SDK library,
 		// which should not be pulled just because of a static component library.
-		if implicitSdkLib != nil {
+		if sdkLib != nil {
 			return
 		}
 	} else {
@@ -1819,11 +1813,14 @@
 		return
 	}
 
-	if implicitSdkLib != nil {
-		clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *implicitSdkLib,
+	// If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree,
+	// and its CLC should be added as subtree of that node. Otherwise the library is not a
+	// <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies
+	// from its CLC should be added to the current CLC.
+	if sdkLib != nil {
+		clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib,
 			dep.DexJarBuildPath(), dep.DexJarInstallPath(), dep.ClassLoaderContexts())
 	} else {
-		depName := ctx.OtherModuleName(depModule)
 		clcMap.AddContextMap(dep.ClassLoaderContexts(), depName)
 	}
 }
diff --git a/rust/benchmark.go b/rust/benchmark.go
index b89f5cd..0e84243 100644
--- a/rust/benchmark.go
+++ b/rust/benchmark.go
@@ -101,6 +101,7 @@
 func (benchmark *benchmarkDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
 	deps = benchmark.binaryDecorator.compilerDeps(ctx, deps)
 
+	deps.Rustlibs = append(deps.Rustlibs, "libtest")
 	deps.Rustlibs = append(deps.Rustlibs, "libcriterion")
 
 	return deps
diff --git a/rust/config/global.go b/rust/config/global.go
index 43b49d1..1b56237 100644
--- a/rust/config/global.go
+++ b/rust/config/global.go
@@ -29,7 +29,6 @@
 	DefaultEdition     = "2018"
 	Stdlibs            = []string{
 		"libstd",
-		"libtest",
 	}
 
 	// Mapping between Soong internal arch types and std::env constants.
diff --git a/rust/test.go b/rust/test.go
index 6caa7b1..e95b47c 100644
--- a/rust/test.go
+++ b/rust/test.go
@@ -169,3 +169,11 @@
 func (test *testDecorator) stdLinkage(ctx *depsContext) RustLinkage {
 	return RlibLinkage
 }
+
+func (test *testDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
+	deps = test.binaryDecorator.compilerDeps(ctx, deps)
+
+	deps.Rustlibs = append(deps.Rustlibs, "libtest")
+
+	return deps
+}
diff --git a/rust/testing.go b/rust/testing.go
index 72f87e1..94cdd9d 100644
--- a/rust/testing.go
+++ b/rust/testing.go
@@ -170,12 +170,10 @@
 			name: "libtest",
 			crate_name: "test",
 			srcs: ["foo.rs"],
-			no_stdlibs: true,
 			host_supported: true,
 			vendor_available: true,
 			vendor_ramdisk_available: true,
 			native_coverage: false,
-			sysroot: true,
 			apex_available: ["//apex_available:platform", "//apex_available:anyapex"],
 			min_sdk_version: "29",
 		}
diff --git a/rust/vendor_snapshot_test.go b/rust/vendor_snapshot_test.go
index 815f80e..60ddb65 100644
--- a/rust/vendor_snapshot_test.go
+++ b/rust/vendor_snapshot_test.go
@@ -569,7 +569,6 @@
 				],
 				rlibs: [
 					"libstd",
-					"libtest",
 					"librust_vendor_available",
 				],
 				binaries: [
@@ -597,7 +596,6 @@
 				],
 				rlibs: [
 					"libstd",
-					"libtest",
 					"librust_vendor_available",
 				],
 				binaries: [
@@ -665,22 +663,6 @@
 	}
 
 	vendor_snapshot_rlib {
-		name: "libtest",
-		version: "30",
-		target_arch: "arm64",
-		vendor: true,
-		sysroot: true,
-		arch: {
-			arm64: {
-				src: "libtest.rlib",
-			},
-			arm: {
-				src: "libtest.rlib",
-			},
-		},
-	}
-
-	vendor_snapshot_rlib {
 		name: "librust_vendor_available",
 		version: "30",
 		target_arch: "arm64",
@@ -917,7 +899,6 @@
 		"vendor/lib64.so":                               nil,
 		"vendor/liblog.so":                              nil,
 		"vendor/libstd.rlib":                            nil,
-		"vendor/libtest.rlib":                           nil,
 		"vendor/librust_vendor_available.rlib":          nil,
 		"vendor/crtbegin_so.o":                          nil,
 		"vendor/crtend_so.o":                            nil,
@@ -962,7 +943,7 @@
 	}
 
 	libclientAndroidMkRlibs := ctx.ModuleForTests("libclient", sharedVariant).Module().(*Module).Properties.AndroidMkRlibs
-	if g, w := libclientAndroidMkRlibs, []string{"librust_vendor_available.vendor_rlib.30.arm64.rlib-std", "libstd.vendor_rlib.30.arm64", "libtest.vendor_rlib.30.arm64"}; !reflect.DeepEqual(g, w) {
+	if g, w := libclientAndroidMkRlibs, []string{"librust_vendor_available.vendor_rlib.30.arm64.rlib-std", "libstd.vendor_rlib.30.arm64"}; !reflect.DeepEqual(g, w) {
 		t.Errorf("wanted libclient libclientAndroidMkRlibs %q, got %q", w, g)
 	}
 
@@ -977,7 +958,7 @@
 	}
 
 	libclientRustAndroidMkRlibs := ctx.ModuleForTests("libclient_rust", rlibVariant).Module().(*Module).Properties.AndroidMkRlibs
-	if g, w := libclientRustAndroidMkRlibs, []string{"librust_vendor_available.vendor_rlib.30.arm64.rlib-std", "libstd.vendor_rlib.30.arm64", "libtest.vendor_rlib.30.arm64"}; !reflect.DeepEqual(g, w) {
+	if g, w := libclientRustAndroidMkRlibs, []string{"librust_vendor_available.vendor_rlib.30.arm64.rlib-std", "libstd.vendor_rlib.30.arm64"}; !reflect.DeepEqual(g, w) {
 		t.Errorf("wanted libclient libclientAndroidMkRlibs %q, got %q", w, g)
 	}