diff --git a/flight/targets/UAVObjects/uavobjectmanager.c b/flight/targets/UAVObjects/uavobjectmanager.c index 1f07e0c9c..13ba7b1ff 100644 --- a/flight/targets/UAVObjects/uavobjectmanager.c +++ b/flight/targets/UAVObjects/uavobjectmanager.c @@ -34,6 +34,10 @@ #include "openpilot.h" #include "pios_struct_helper.h" +#if (defined(__MACH__) && defined(__APPLE__)) +#include +#endif + // Constants // Private types @@ -41,9 +45,21 @@ // Macros #define SET_BITS(var, shift, value, mask) var = (var & ~(mask << shift)) | (value << shift); -/* Table of UAVO handles registered at compile time */ +// Mach-o: dummy segment to calculate ASLR offset in sim_osx +#if (defined(__MACH__) && defined(__APPLE__)) +static long _aslr_offset __attribute__((section("__DATA,_aslr"))); +#endif + +/* Table of UAVO handles */ +#if (defined(__MACH__) && defined(__APPLE__)) +/* Mach-o format */ +static struct UAVOData ** __start__uavo_handles; +static struct UAVOData ** __stop__uavo_handles; +#else +/* ELF format: automagically defined at compile time */ extern struct UAVOData * __start__uavo_handles[] __attribute__((weak)); extern struct UAVOData * __stop__uavo_handles[] __attribute__((weak)); +#endif #define UAVO_LIST_ITERATE(_item) \ for (struct UAVOData ** _uavo_slot = __start__uavo_handles; \ @@ -199,6 +215,13 @@ int32_t UAVObjInitialize() // Initialize variables memset(&stats, 0, sizeof(UAVObjStats)); + /* Initialize _uavo_handles start/stop pointers */ + #if (defined(__MACH__) && defined(__APPLE__)) + uint64_t aslr_offset = (uint64_t) & _aslr_offset - getsectbyname("__DATA","_aslr")->addr; + __start__uavo_handles = (struct UAVOData **) (getsectbyname("__DATA","_uavo_handles")->addr + aslr_offset); + __stop__uavo_handles = (struct UAVOData **) ((uint64_t)__start__uavo_handles + getsectbyname("__DATA","_uavo_handles")->size); + #endif + // Initialize the uavo handle table memset(__start__uavo_handles, 0, (uintptr_t)__stop__uavo_handles - (uintptr_t)__start__uavo_handles); diff --git a/flight/targets/UAVObjects/uavobjecttemplate.c b/flight/targets/UAVObjects/uavobjecttemplate.c index 808faf496..9af13641b 100644 --- a/flight/targets/UAVObjects/uavobjecttemplate.c +++ b/flight/targets/UAVObjects/uavobjecttemplate.c @@ -40,7 +40,11 @@ #include "$(NAMELC).h" // Private variables +#if (defined(__MACH__) && defined(__APPLE__)) +static UAVObjHandle handle __attribute__((section("__DATA,_uavo_handles"))); +#else static UAVObjHandle handle __attribute__((section("_uavo_handles"))); +#endif /** * Initialize object. diff --git a/make/templates/firmwareinfotemplate.c b/make/templates/firmwareinfotemplate.c index 6e4fbf8b3..e686ac2b4 100644 --- a/make/templates/firmwareinfotemplate.c +++ b/make/templates/firmwareinfotemplate.c @@ -55,7 +55,11 @@ struct __attribute__((packed)) fw_version_info { uint8_t pad[20]; }; +#if (defined(__MACH__) && defined(__APPLE__)) +const struct fw_version_info fw_version_blob __attribute__((used)) __attribute__((__section__("__TEXT,.fw_version_blob"))) = { +#else const struct fw_version_info fw_version_blob __attribute__((used)) __attribute__((__section__(".fw_version_blob"))) = { +#endif .magic = { 'O','p','F','w' }, .commit_hash_prefix = 0x${HASH8}, .timestamp = ${UNIXTIME},