+static void $bzero(void *data, size_t size) {
+ char *bytes(reinterpret_cast<char *>(data));
+ for (size_t i(0); i != size; ++i)
+ bytes[i] = 0;
+}
+
+static int $strcmp(const char *lhs, const char *rhs) {
+ while (*lhs == *rhs) {
+ if (*lhs == '\0')
+ return 0;
+ ++lhs, ++rhs;
+ } return *lhs < *rhs ? -1 : 1;
+}
+
+static void $strlcpy(char *dst, const char *src, size_t size) {
+ if (src == NULL)
+ src = "(null)";
+ if (size == 0)
+ return;
+ size_t i(0);
+ while (i != size - 1) {
+ char value(src[i]);
+ if (value == '\0')
+ break;
+ dst[i++] = value;
+ } dst[i] = '\0';
+}
+
+__attribute__((__unused__))
+static void $snprintfp(char *dst, size_t size, const void *pointer) {
+ uintptr_t value(reinterpret_cast<uintptr_t>(pointer));
+ char buffer[32];
+ char *end(buffer + sizeof(buffer));
+ *--end = '\0';
+ if (value == 0)
+ *--end = '0';
+ else do {
+ unsigned digit(value & 0xf);
+ value >>= 4;
+ *--end = (digit < 10 ? '0' : 'a' - 10) + digit;
+ } while (value != 0);
+ *--end = 'x';
+ *--end = '0';
+ $strlcpy(dst, end, size);
+}
+
+#ifdef __LP64__
+typedef struct mach_header_64 mach_header_xx;
+typedef struct nlist_64 nlist_xx;
+typedef struct segment_command_64 segment_command_xx;
+
+static const uint32_t LC_SEGMENT_XX = LC_SEGMENT_64;
+static const uint32_t MH_MAGIC_XX = MH_MAGIC_64;
+#else
+typedef struct mach_header mach_header_xx;
+typedef struct nlist nlist_xx;
+typedef struct segment_command segment_command_xx;
+
+static const uint32_t LC_SEGMENT_XX = LC_SEGMENT;
+static const uint32_t MH_MAGIC_XX = MH_MAGIC;
+#endif
+
+#define forlc(command, mach, lc, type) \
+ if (const struct load_command *load_commands = reinterpret_cast<const struct load_command *>(mach + 1)) \
+ if (const struct load_command *lcp = load_commands) \
+ for (uint32_t i(0); i != mach->ncmds; ++i, lcp = reinterpret_cast<const struct load_command *>(reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize)) \
+ if ( \
+ lcp->cmdsize % sizeof(long) != 0 || lcp->cmdsize <= 0 || \
+ reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize > reinterpret_cast<const uint8_t *>(load_commands) + mach->sizeofcmds \
+ ) \
+ break; \
+ else if (lcp->cmd != lc) \
+ continue; \
+ else if (lcp->cmdsize < sizeof(type)) \
+ break; \
+ else if (const type *command = reinterpret_cast<const type *>(lcp))
+
+static const mach_header_xx *Library(struct dyld_all_image_infos *infos, const char *name) {
+ for (uint32_t i(0); i != infos->infoArrayCount; ++i) {
+ const dyld_image_info &info(infos->infoArray[i]);
+ const mach_header_xx *mach(reinterpret_cast<const mach_header_xx *>(info.imageLoadAddress));
+ if (mach->magic != MH_MAGIC_XX)
+ continue;
+
+ const char *path(info.imageFilePath);
+ forlc (dylib, mach, LC_ID_DYLIB, dylib_command)
+ path = reinterpret_cast<const char *>(dylib) + dylib->dylib.name.offset;
+ if ($strcmp(path, name) != 0)
+ continue;
+
+ return mach;
+ }
+
+ return NULL;
+}
+
+static void *Symbol(const mach_header_xx *mach, const char *name) {
+ const struct symtab_command *stp(NULL);
+ forlc (command, mach, LC_SYMTAB, struct symtab_command)
+ stp = command;
+ if (stp == NULL)
+ return NULL;
+
+ size_t slide(_not(size_t));
+ const nlist_xx *symbols(NULL);
+ const char *strings(NULL);
+
+ forlc (segment, mach, LC_SEGMENT_XX, segment_command_xx) {
+ if (segment->fileoff == 0)
+ slide = reinterpret_cast<size_t>(mach) - segment->vmaddr;
+ if (stp->symoff >= segment->fileoff && stp->symoff < segment->fileoff + segment->filesize)
+ symbols = reinterpret_cast<const nlist_xx *>(stp->symoff - segment->fileoff + segment->vmaddr + slide);
+ if (stp->stroff >= segment->fileoff && stp->stroff < segment->fileoff + segment->filesize)
+ strings = reinterpret_cast<const char *>(stp->stroff - segment->fileoff + segment->vmaddr + slide);
+ }
+
+ if (slide == _not(size_t) || symbols == NULL || strings == NULL)
+ return NULL;
+
+ for (size_t i(0); i != stp->nsyms; ++i) {
+ const nlist_xx *symbol(&symbols[i]);
+ if (symbol->n_un.n_strx == 0 || (symbol->n_type & N_STAB) != 0)
+ continue;
+
+ const char *nambuf(strings + symbol->n_un.n_strx);
+ if ($strcmp(name, nambuf) != 0)
+ continue;
+
+ uintptr_t value(symbol->n_value);
+ if (value == 0)
+ continue;
+
+#ifdef __arm__
+ if ((symbol->n_desc & N_ARM_THUMB_DEF) != 0)
+ value |= 0x00000001;
+#endif
+
+ value += slide;
+ return reinterpret_cast<void *>(value);
+ }
+
+ return NULL;
+}
+