Export dlfcn.h to nanoapps

Exports the CHRE version of dlfcn.h to nanoapps while making it use
c-style so it can be utilized similar to the libc version of the header.

This is required to support backwards compatbility in nanoapps.

Bug: 151847750
Test: Compile CHRE and nanoapps with backwards compatibility enabled.
Change-Id: Ic436a6bf6b4a873eaafa3ffe352361da81ebc840
diff --git a/build/arch/armv8a.mk b/build/arch/armv8a.mk
index 9167b04..42b8748 100644
--- a/build/arch/armv8a.mk
+++ b/build/arch/armv8a.mk
@@ -16,11 +16,6 @@
 TARGET_SO_LDFLAGS += $(LDFLAGS)
 TARGET_SO_EARLY_LIBS += $(LDLIBS)
 
-TARGET_CFLAGS += -I$(AOC_TOP_DIR)/AOC/libs/bionic_interface/include
-TARGET_CFLAGS += -I$(AOC_TOP_DIR)/AOC/libs/common/basic/include
-TARGET_CFLAGS += -I$(AOC_TOP_DIR)/AOC/libs/common/libc/include
-TARGET_CFLAGS += -I$(AOC_TOP_DIR)/external/libcxx/include
-
 # TODO: Fix ar_client so the following two can be removed.
 TARGET_CFLAGS += -Wno-strict-prototypes
 TARGET_CFLAGS += -Wno-missing-prototypes
diff --git a/build/variant/google_armv8a_aoc.mk b/build/variant/google_armv8a_aoc.mk
index b6d84eb..cc131a9 100644
--- a/build/variant/google_armv8a_aoc.mk
+++ b/build/variant/google_armv8a_aoc.mk
@@ -36,6 +36,13 @@
 TARGET_CFLAGS = -DCHRE_MESSAGE_TO_HOST_MAX_SIZE=4000
 TARGET_CFLAGS += $(AOC_CFLAGS)
 TARGET_CFLAGS += $(FREERTOS_CFLAGS)
+TARGET_CFLAGS += -I$(AOC_TOP_DIR)/AOC/libs/bionic_interface/include
+TARGET_CFLAGS += -I$(AOC_TOP_DIR)/AOC/libs/common/basic/include
+TARGET_CFLAGS += -I$(AOC_TOP_DIR)/AOC/libs/common/libc/include
+TARGET_CFLAGS += -I$(AOC_TOP_DIR)/external/libcxx/include
+
+# Used to expose libc headers to nanoapps that aren't supported on the given platform
+TARGET_CFLAGS += -I$(CHRE_PREFIX)/platform/freertos/include/chre/platform/freertos/libc
 
 # add platform specific flags
 ifeq ($(AOC_PLATFORM),linux)
@@ -67,9 +74,6 @@
 
 ifneq ($(IS_NANOAPP_BUILD),)
 include $(CHRE_PREFIX)/build/nanoapp/google_aoc.mk
-
-# TODO: Remove once dynamic loading is implemented.
-TARGET_CFLAGS += -DCHRE_NANOAPP_DISABLE_BACKCOMPAT
 endif
 
 include $(CHRE_PREFIX)/build/arch/armv8a.mk
diff --git a/platform/freertos/dlfcn.cc b/platform/freertos/dlfcn.cc
index c5cbf44..afc6d85 100644
--- a/platform/freertos/dlfcn.cc
+++ b/platform/freertos/dlfcn.cc
@@ -14,24 +14,22 @@
  * limitations under the License.
  */
 
-#include "chre/platform/freertos/dlfcn.h"
+#include <dlfcn.h>
 
 #include "chre/platform/assert.h"
 #include "chre/platform/freertos/nanoapp_loader.h"
 #include "chre/platform/memory.h"
 #include "chre/util/unique_ptr.h"
 
-namespace chre {
-
 void *dlopenbuf(void *elfBinary) {
-  return NanoappLoader::create(elfBinary);
+  return chre::NanoappLoader::create(elfBinary);
 }
 
 void *dlsym(void *handle, const char *symbol) {
   LOGV("Attempting to find %s", symbol);
 
   void *resolvedSymbol = nullptr;
-  auto *loader = reinterpret_cast<NanoappLoader *>(handle);
+  auto *loader = reinterpret_cast<chre::NanoappLoader *>(handle);
   if (loader != nullptr) {
     resolvedSymbol = loader->findSymbolByName(symbol);
     LOGV("Found symbol at %p", resolvedSymbol);
@@ -41,7 +39,8 @@
 
 int dlclose(void *handle) {
   int rv = -1;
-  UniquePtr<NanoappLoader> loader = static_cast<NanoappLoader *>(handle);
+  chre::UniquePtr<chre::NanoappLoader> loader =
+      static_cast<chre::NanoappLoader *>(handle);
 
   if (!loader.isNull()) {
     loader->close();
@@ -54,5 +53,3 @@
 const char *dlerror() {
   return "Shared library load failed";
 }
-
-}  // namespace chre
diff --git a/platform/freertos/include/chre/platform/freertos/dlfcn.h b/platform/freertos/include/chre/platform/freertos/libc/dlfcn.h
similarity index 84%
rename from platform/freertos/include/chre/platform/freertos/dlfcn.h
rename to platform/freertos/include/chre/platform/freertos/libc/dlfcn.h
index 8cd3a18..3a948dd 100644
--- a/platform/freertos/include/chre/platform/freertos/dlfcn.h
+++ b/platform/freertos/include/chre/platform/freertos/libc/dlfcn.h
@@ -17,9 +17,14 @@
 #ifndef CHRE_PLATFORM_FREERTOS_DLFCN_H_
 #define CHRE_PLATFORM_FREERTOS_DLFCN_H_
 
+//! This file is intended to be used when a platform doesn't provide a dlfcn.h
+//! implementation so that dynamic loading support can be provided to nanoapps.
+
 #include <cstdlib>
 
-namespace chre {
+//! Indicates that the dlsym call is attempting to lookup the provided symbol
+//! in another library (CHRE).
+#define RTLD_NEXT ((void *)-1L)
 
 /**
  * This function parses, verifies, and loads a buffer containing an ELF
@@ -49,6 +54,4 @@
  */
 int dlclose(void *handle);
 
-}  // namespace chre
-
 #endif  // CHRE_PLATFORM_FREERTOS_DLFCN_H_
diff --git a/platform/freertos/platform_nanoapp.cc b/platform/freertos/platform_nanoapp.cc
index d8ac3ec..0e99200 100644
--- a/platform/freertos/platform_nanoapp.cc
+++ b/platform/freertos/platform_nanoapp.cc
@@ -15,12 +15,12 @@
  */
 
 #include "chre/platform/platform_nanoapp.h"
-#include "chre/platform/freertos/dlfcn.h"
 #include "chre/platform/freertos/dram_util.h"
 #include "chre/platform/freertos/memory.h"
 #include "chre/platform/freertos/nanoapp_loader.h"
 #include "chre/platform/shared/nanoapp_dso_util.h"
 
+#include <dlfcn.h>
 #include <cinttypes>
 
 #include "chre/platform/assert.h"