From c1aa0b46b59531d4a2bf842146b361ee6b264bdc Mon Sep 17 00:00:00 2001 From: Damjan Jovanovic Date: Thu, 2 Aug 2012 10:36:16 -0600 Subject: [PATCH] add support for FreeBSD --- classpath/java-lang.cpp | 2 ++ classpath/java-nio.cpp | 2 ++ makefile | 14 ++++++++++++++ src/binaryToObject/elf.cpp | 17 +++++++++++------ src/binaryToObject/tools.cpp | 5 ++++- src/binaryToObject/tools.h | 4 +++- src/classpath-openjdk.cpp | 2 ++ src/environment.h | 4 +++- src/posix.cpp | 11 +++++++++++ src/target-fields.h | 3 ++- src/x86.h | 6 ++++++ 11 files changed, 60 insertions(+), 10 deletions(-) diff --git a/classpath/java-lang.cpp b/classpath/java-lang.cpp index 82376799c1..865f3c3fb4 100644 --- a/classpath/java-lang.cpp +++ b/classpath/java-lang.cpp @@ -578,6 +578,8 @@ Java_java_lang_System_getProperty(JNIEnv* e, jclass, jstring name, } else if (strcmp(chars, "os.name") == 0) { #ifdef __APPLE__ r = e->NewStringUTF("Mac OS X"); +#elif defined __FreeBSD__ + r = e->NewStringUTF("FreeBSD"); #else r = e->NewStringUTF("Linux"); #endif diff --git a/classpath/java-nio.cpp b/classpath/java-nio.cpp index a9a7088078..d5e067e1e4 100644 --- a/classpath/java-nio.cpp +++ b/classpath/java-nio.cpp @@ -31,6 +31,8 @@ # include # include # include +# include +# include # include # include # include diff --git a/makefile b/makefile index 6120005fba..2a91344f2e 100755 --- a/makefile +++ b/makefile @@ -334,6 +334,16 @@ ifeq ($(platform),qnx) rdynamic = -Wl,--export-dynamic endif +ifeq ($(platform),freebsd) +# There is no -ldl on FreeBSD + build-lflags = $(common-lflags) -lz -lpthread + lflags = $(common-lflags) -lpthread +# include/freebsd instead of include/linux + build-cflags = $(common-cflags) -fPIC -fvisibility=hidden \ + "-I$(JAVA_HOME)/include/freebsd" -I$(src) -pthread + cflags = $(build-cflags) +endif + ifeq ($(platform),darwin) ifeq (${OSX_SDK_SYSROOT},) OSX_SDK_SYSROOT = 10.4u @@ -855,6 +865,10 @@ ifeq ($(target-platform),darwin) cflags += -DAVIAN_TARGET_PLATFORM=AVIAN_PLATFORM_DARWIN endif +ifeq ($(target-platform),freebsd) + cflags += -DAVIAN_TARGET_PLATFORM=AVIAN_PLATFORM_FREEBSD +endif + class-name = $(patsubst $(1)/%.class,%,$(2)) class-names = $(foreach x,$(2),$(call class-name,$(1),$(x))) diff --git a/src/binaryToObject/elf.cpp b/src/binaryToObject/elf.cpp index b4e0025b02..f50e4b6eba 100644 --- a/src/binaryToObject/elf.cpp +++ b/src/binaryToObject/elf.cpp @@ -197,8 +197,8 @@ public: const unsigned machine; - ElfPlatform(PlatformInfo::Architecture arch): - Platform(PlatformInfo(PlatformInfo::Linux, arch)), + ElfPlatform(PlatformInfo::OperatingSystem os, PlatformInfo::Architecture arch): + Platform(PlatformInfo(os, arch)), machine(getElfPlatform(arch)) {} class FileWriter { @@ -372,10 +372,15 @@ public: } }; -ElfPlatform elfx86Platform(PlatformInfo::x86); -ElfPlatform elfArmPlatform(PlatformInfo::Arm); -ElfPlatform elfPowerPCPlatform(PlatformInfo::PowerPC); -ElfPlatform elfx86_64Platform(PlatformInfo::x86_64); +ElfPlatform elfLinuxX86Platform(PlatformInfo::Linux, PlatformInfo::x86); +ElfPlatform elfLinuxArmPlatform(PlatformInfo::Linux, PlatformInfo::Arm); +ElfPlatform elfLinuxPowerPCPlatform(PlatformInfo::Linux, PlatformInfo::PowerPC); +ElfPlatform elfLinuxX86_64Platform(PlatformInfo::Linux, PlatformInfo::x86_64); + +ElfPlatform elfFreeBSDx86Platform(PlatformInfo::FreeBSD, PlatformInfo::x86); +ElfPlatform elfFreeBSDArmPlatform(PlatformInfo::FreeBSD, PlatformInfo::Arm); +ElfPlatform elfFreeBSDPowerPCPlatform(PlatformInfo::FreeBSD, PlatformInfo::PowerPC); +ElfPlatform elfFreeBSDx86_64Platform(PlatformInfo::FreeBSD, PlatformInfo::x86_64); } // namespace diff --git a/src/binaryToObject/tools.cpp b/src/binaryToObject/tools.cpp index 3181474b94..89961a74c5 100644 --- a/src/binaryToObject/tools.cpp +++ b/src/binaryToObject/tools.cpp @@ -92,6 +92,8 @@ PlatformInfo::OperatingSystem PlatformInfo::osFromString(const char* os) { return Windows; } else if(strcmp(os, "darwin") == 0) { return Darwin; + } else if(strcmp(os, "freebsd") == 0) { + return FreeBSD; } else { return UnknownOS; } @@ -122,4 +124,5 @@ Platform* Platform::getPlatform(PlatformInfo info) { } // namespace tools -} // namespace avian \ No newline at end of file +} // namespace avian + diff --git a/src/binaryToObject/tools.h b/src/binaryToObject/tools.h index c95d22ae89..dde9140678 100644 --- a/src/binaryToObject/tools.h +++ b/src/binaryToObject/tools.h @@ -135,6 +135,7 @@ public: Linux = AVIAN_PLATFORM_LINUX, Windows = AVIAN_PLATFORM_WINDOWS, Darwin = AVIAN_PLATFORM_DARWIN, + FreeBSD = AVIAN_PLATFORM_FREEBSD, UnknownOS = AVIAN_PLATFORM_UNKNOWN }; @@ -193,4 +194,5 @@ public: } // namespace avian -#endif \ No newline at end of file +#endif + diff --git a/src/classpath-openjdk.cpp b/src/classpath-openjdk.cpp index bbdb4a547e..febbf3b947 100644 --- a/src/classpath-openjdk.cpp +++ b/src/classpath-openjdk.cpp @@ -3002,6 +3002,8 @@ jvmInitProperties(Thread* t, uintptr_t* arguments) local::setProperty(t, method, *properties, "path.separator", ":"); # ifdef __APPLE__ local::setProperty(t, method, *properties, "os.name", "Mac OS X"); +# elif defined __FreeBSD__ + local::setProperty(t, method, *properties, "os.name", "FreeBSD"); # else // not __APPLE__ local::setProperty(t, method, *properties, "os.name", "Linux"); # endif // not __APPLE__ diff --git a/src/environment.h b/src/environment.h index 045eb2642d..2d66407109 100644 --- a/src/environment.h +++ b/src/environment.h @@ -23,6 +23,7 @@ #define AVIAN_PLATFORM_LINUX 1 #define AVIAN_PLATFORM_WINDOWS 2 #define AVIAN_PLATFORM_DARWIN 3 +#define AVIAN_PLATFORM_FREEBSD 4 #define AVIAN_ARCH_UNKNOWN 0 #define AVIAN_ARCH_X86 (1 << 8) @@ -30,4 +31,5 @@ #define AVIAN_ARCH_ARM (3 << 8) #define AVIAN_ARCH_POWERPC (4 << 8) -#endif \ No newline at end of file +#endif + diff --git a/src/posix.cpp b/src/posix.cpp index 6b2184011b..afa4c0ca38 100644 --- a/src/posix.cpp +++ b/src/posix.cpp @@ -17,6 +17,9 @@ # include "sys/ucontext.h" # undef assert #else +# if defined __FreeBSD__ +# include "limits.h" +# endif # include "ucontext.h" #endif @@ -797,6 +800,13 @@ class MySystem: public System { } virtual FileType stat(const char* name, unsigned* length) { +#ifdef __FreeBSD__ + // Now the hack below causes the error "Dereferencing type-punned + // pointer will break strict aliasing rules", so another workaround + // is needed... + struct stat ss; + struct stat* s = &ss; +#else // Ugly Hack Alert: It seems that the Apple iOS Simulator's stat // implementation writes beyond the end of the struct stat we pass // it, which can clobber unrelated parts of the stack. Perhaps @@ -806,6 +816,7 @@ class MySystem: public System { // made up and seems to work. void* array[ceiling(sizeof(struct stat), sizeof(void*)) + 8]; struct stat* s = reinterpret_cast(array); +#endif int r = ::stat(name, s); if (r == 0) { diff --git a/src/target-fields.h b/src/target-fields.h index 3481d4516e..a02aee0f6a 100644 --- a/src/target-fields.h +++ b/src/target-fields.h @@ -60,4 +60,5 @@ # error #endif -#endif \ No newline at end of file +#endif + diff --git a/src/x86.h b/src/x86.h index 4d978b2a0c..42cf083d77 100644 --- a/src/x86.h +++ b/src/x86.h @@ -70,6 +70,12 @@ # define THREAD_REGISTER(context) (context->uc_mcontext.cpu.ebx) # define LINK_REGISTER(context) (context->uc_mcontext.cpu.ecx) # define FRAME_REGISTER(context) (context->uc_mcontext.cpu.ebp) +# elif (defined __FreeBSD__) +# define IP_REGISTER(context) (context->uc_mcontext.mc_eip) +# define STACK_REGISTER(context) (context->uc_mcontext.mc_esp) +# define THREAD_REGISTER(context) (context->uc_mcontext.mc_ebx) +# define LINK_REGISTER(context) (context->uc_mcontext.mc_ecx) +# define FRAME_REGISTER(context) (context->uc_mcontext.mc_ebp) # else # define IP_REGISTER(context) (context->uc_mcontext.gregs[REG_EIP]) # define STACK_REGISTER(context) (context->uc_mcontext.gregs[REG_ESP])