+#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
+// Get next base address to be used for system ni image from file
+// __SYSTEM_BASE_FILE should be checked for existance before calling this function
+static uintptr_t getNextBaseAddrFromFile()
+{
+ FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
+ if (pFile == NULL) {
+ fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
+ return 0;
+ }
+
+ uintptr_t addr = 0;
+ uintptr_t size = 0;
+
+ while (fscanf(pFile, "%u %u", &addr, &size) != EOF) {
+ }
+
+ fclose(pFile);
+
+ return addr + size;
+}
+
+// Get next base address to be used for system ni image
+static uintptr_t getNextBaseAddr()
+{
+ uintptr_t baseAddr = 0;
+
+ if (!isFileExist(__SYSTEM_BASE_FILE)) {
+ // This is the starting address for all default base addresses
+ baseAddr = DEFAULT_BASE_ADDR_START;
+ } else {
+ baseAddr = getNextBaseAddrFromFile();
+
+ // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
+ uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
+ baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
+ }
+
+ return baseAddr;
+}
+
+// Save base address of system ni image to file
+static void updateBaseAddrFile(const std::string &absNiPath, uintptr_t baseAddr)
+{
+ uintptr_t niSize = getFileSize(absNiPath);
+ if (niSize == 0) {
+ fprintf(stderr, "File %s doesn't exist\n", absNiPath.c_str());
+ return;
+ }
+
+ // Write new entry to the file
+ FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
+ if (pFile == NULL) {
+ fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
+ return;
+ }
+
+ fprintf(pFile, "%u %u\n", baseAddr, niSize);
+ fclose(pFile);
+}
+
+// check if dll is listed in TPA
+static bool isTPADll(const std::string &dllPath)
+{
+ std::string absDllPath = absolutePath(dllPath);
+
+ if (__tpa.find(absDllPath) != std::string::npos) {
+ return true;
+ }
+
+ return false;
+}
+#endif
+
+// baseAddr should be checked in file before getting here