Linker configuration for recovery
am: 09cbb08096

Change-Id: Iffffa168e744891783ecbdcc4e241df66cff73bc
diff --git a/Android.bp b/Android.bp
index d76e26d..bf980cd 100644
--- a/Android.bp
+++ b/Android.bp
@@ -26,6 +26,7 @@
         "liblog",
     ],
     host_supported: true,
+    recovery_available: true,
 }
 
 cc_defaults {
@@ -36,6 +37,7 @@
         "-Wunused",
     ],
     test_suites: ["general-tests"],
+    recovery_available: false,
 }
 
 cc_library_static {
@@ -100,7 +102,6 @@
     static_libs: [
         "linkerconfig_modules",
     ],
-    host_supported: true,
 }
 
 cc_test {
diff --git a/contents/configuration/recovery.cc b/contents/configuration/recovery.cc
new file mode 100644
index 0000000..730dc7a
--- /dev/null
+++ b/contents/configuration/recovery.cc
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "linkerconfig/legacy.h"
+#include "linkerconfig/sectionbuilder.h"
+
+using android::linkerconfig::contents::LinkerConfigType;
+using android::linkerconfig::modules::DirToSection;
+using android::linkerconfig::modules::Section;
+
+namespace {
+const std::vector<DirToSection> kDirToSection = {
+    {"/system/bin", "recovery"},
+};
+}  // namespace
+
+namespace android {
+namespace linkerconfig {
+namespace contents {
+android::linkerconfig::modules::Configuration CreateRecoveryConfiguration() {
+  std::vector<Section> sections;
+  Context current_context;
+  current_context.SetCurrentLinkerConfigType(LinkerConfigType::Recovery);
+
+  sections.emplace_back(BuildRecoverySection(current_context));
+
+  return android::linkerconfig::modules::Configuration(std::move(sections),
+                                                       kDirToSection);
+}
+}  // namespace contents
+}  // namespace linkerconfig
+}  // namespace android
\ No newline at end of file
diff --git a/contents/include/linkerconfig/namespacebuilder.h b/contents/include/linkerconfig/namespacebuilder.h
index f582063..73c8699 100644
--- a/contents/include/linkerconfig/namespacebuilder.h
+++ b/contents/include/linkerconfig/namespacebuilder.h
@@ -39,6 +39,7 @@
 NamespaceBuilder BuildPostInstallNamespace;
 NamespaceBuilder BuildNeuralNetworksNamespace;
 NamespaceBuilder BuildRuntimeNamespace;
+NamespaceBuilder BuildRecoveryDefaultNamespace;
 }  // namespace contents
 }  // namespace linkerconfig
 }  // namespace android
diff --git a/contents/include/linkerconfig/recovery.h b/contents/include/linkerconfig/recovery.h
new file mode 100644
index 0000000..636b6ca
--- /dev/null
+++ b/contents/include/linkerconfig/recovery.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "linkerconfig/configuration.h"
+
+namespace android {
+namespace linkerconfig {
+namespace contents {
+android::linkerconfig::modules::Configuration CreateRecoveryConfiguration();
+}  // namespace contents
+}  // namespace linkerconfig
+}  // namespace android
\ No newline at end of file
diff --git a/contents/include/linkerconfig/sectionbuilder.h b/contents/include/linkerconfig/sectionbuilder.h
index 0c08358..e410186 100644
--- a/contents/include/linkerconfig/sectionbuilder.h
+++ b/contents/include/linkerconfig/sectionbuilder.h
@@ -29,6 +29,7 @@
 SectionBuilder BuildUnrestrictedSection;
 SectionBuilder BuildLegacySection;
 SectionBuilder BuildPostInstallSection;
+SectionBuilder BuildRecoverySection;
 }  // namespace contents
 }  // namespace linkerconfig
 }  // namespace android
\ No newline at end of file
diff --git a/contents/namespace/recoverydefault.cc b/contents/namespace/recoverydefault.cc
new file mode 100644
index 0000000..59841fb
--- /dev/null
+++ b/contents/namespace/recoverydefault.cc
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "linkerconfig/environment.h"
+#include "linkerconfig/namespace.h"
+#include "linkerconfig/namespacebuilder.h"
+
+using android::linkerconfig::modules::AsanPath;
+using android::linkerconfig::modules::Namespace;
+
+namespace android {
+namespace linkerconfig {
+namespace contents {
+Namespace BuildRecoveryDefaultNamespace([[maybe_unused]] const Context& ctx) {
+  Namespace ns("default");
+
+  ns.AddSearchPath("/system/${LIB}", AsanPath::NONE);
+
+  return ns;
+}
+}  // namespace contents
+}  // namespace linkerconfig
+}  // namespace android
\ No newline at end of file
diff --git a/contents/section/recovery.cc b/contents/section/recovery.cc
new file mode 100644
index 0000000..b55d22e
--- /dev/null
+++ b/contents/section/recovery.cc
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "linkerconfig/sectionbuilder.h"
+
+#include "linkerconfig/namespacebuilder.h"
+#include "linkerconfig/section.h"
+
+using android::linkerconfig::modules::Namespace;
+using android::linkerconfig::modules::Section;
+
+namespace android {
+namespace linkerconfig {
+namespace contents {
+Section BuildRecoverySection(Context& ctx) {
+  std::vector<Namespace> namespaces;
+  namespaces.emplace_back(BuildRecoveryDefaultNamespace(ctx));
+  Section section("recovery", std::move(namespaces));
+
+  return section;
+}
+}  // namespace contents
+}  // namespace linkerconfig
+}  // namespace android
diff --git a/contents/tests/configuration/recovery_test.cc b/contents/tests/configuration/recovery_test.cc
new file mode 100644
index 0000000..a6890e8
--- /dev/null
+++ b/contents/tests/configuration/recovery_test.cc
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "linkerconfig/recovery.h"
+#include "configurationtest.h"
+#include "linkerconfig/configwriter.h"
+#include "mockenv.h"
+
+TEST(linkerconfig_configuration_fulltest, recovery_test) {
+  MockGenericVariables();
+  auto legacy_config =
+      android::linkerconfig::contents::CreateRecoveryConfiguration();
+  android::linkerconfig::modules::ConfigWriter config_writer;
+
+  legacy_config.WriteConfig(config_writer);
+
+  VerifyConfiguration(config_writer.ToString());
+}
\ No newline at end of file
diff --git a/main.cc b/main.cc
index a721485..c965c1b 100644
--- a/main.cc
+++ b/main.cc
@@ -25,6 +25,7 @@
 #include "linkerconfig/environment.h"
 #include "linkerconfig/legacy.h"
 #include "linkerconfig/log.h"
+#include "linkerconfig/recovery.h"
 #include "linkerconfig/variableloader.h"
 #include "linkerconfig/variables.h"
 
@@ -50,6 +51,7 @@
                " --root <root dir>"
                " --vndk <vndk version>"
 #endif
+               " [--recovery]"
                " [--help]"
             << std::endl;
   exit(status);
@@ -84,12 +86,14 @@
 }
 
 android::linkerconfig::modules::Configuration GetConfiguration() {
+  if (android::linkerconfig::modules::IsRecoveryMode()) {
+    return android::linkerconfig::contents::CreateRecoveryConfiguration();
+  }
+
   if (android::linkerconfig::modules::IsLegacyDevice()) {
     return android::linkerconfig::contents::CreateLegacyConfiguration();
   }
 
-  // TODO : Use recovery if needed
-
   // Use base configuration in default
   return android::linkerconfig::contents::CreateBaseConfiguration();
 }
diff --git a/modules/environment.cc b/modules/environment.cc
index db9c501..54e2ff3 100644
--- a/modules/environment.cc
+++ b/modules/environment.cc
@@ -15,6 +15,9 @@
  */
 
 #include "linkerconfig/environment.h"
+
+#include <unistd.h>
+
 #include "linkerconfig/variables.h"
 
 namespace android {
@@ -37,6 +40,10 @@
 std::string GetVendorVndkVersion() {
   return Variables::GetValue("ro.vndk.version").value_or("");
 }
+
+bool IsRecoveryMode() {
+  return access("/system/bin/recovery", F_OK) == 0;
+}
 }  // namespace modules
 }  // namespace linkerconfig
 }  // namespace android
\ No newline at end of file
diff --git a/modules/include/linkerconfig/environment.h b/modules/include/linkerconfig/environment.h
index 44eecae..fb6e9f9 100644
--- a/modules/include/linkerconfig/environment.h
+++ b/modules/include/linkerconfig/environment.h
@@ -23,6 +23,7 @@
 bool IsVndkLiteDevice();
 bool IsVndkInSystemNamespace();
 std::string GetVendorVndkVersion();
+bool IsRecoveryMode();
 }  // namespace modules
 }  // namespace linkerconfig
 }  // namespace android
\ No newline at end of file