Skip to content

Commit

Permalink
[BOLT] Enable intToStr for MacOS
Browse files Browse the repository at this point in the history
Summary: Enable intToStr et al. in the runtime library for MacOS.

(cherry picked from FBD25745358)
  • Loading branch information
Alexander Shaposhnikov authored and maksfb committed Jan 21, 2021
1 parent faaefff commit d6e60c5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 55 deletions.
3 changes: 2 additions & 1 deletion bolt/runtime/CMakeLists.txt
Expand Up @@ -40,6 +40,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*")
-target x86_64-apple-darwin19.6.0
-ffreestanding
-fno-exceptions
-fno-rtti)
-fno-rtti
-fno-stack-protector)
install(TARGETS bolt_rt_instr_osx DESTINATION lib)
endif()
113 changes: 59 additions & 54 deletions bolt/runtime/common.h
Expand Up @@ -16,6 +16,10 @@ typedef __SIZE_TYPE__ size_t;
typedef __SSIZE_TYPE__ ssize_t;

typedef unsigned long long uint64_t;
typedef unsigned uint32_t;

typedef long long int64_t;
typedef int int32_t;

#endif

Expand Down Expand Up @@ -66,6 +70,8 @@ typedef unsigned long long uint64_t;
// Anonymous namespace covering everything but our library entry point
namespace {

constexpr uint32_t BufSize = 10240;

#define _STRINGIFY(x) #x
#define STRINGIFY(x) _STRINGIFY(x)

Expand Down Expand Up @@ -121,12 +127,64 @@ uint64_t __munmap(void *addr, uint64_t size) {
return ret;
}

// Helper functions for writing strings to the .fdata file. We intentionally
// avoid using libc names (lowercase memset) to make it clear it is our impl.

/// Write number Num using Base to the buffer in OutBuf, returns a pointer to
/// the end of the string.
char *intToStr(char *OutBuf, uint64_t Num, uint32_t Base) {
const char *Chars = "0123456789abcdef";
char Buf[21];
char *Ptr = Buf;
while (Num) {
*Ptr++ = *(Chars + (Num % Base));
Num /= Base;
}
if (Ptr == Buf) {
*OutBuf++ = '0';
return OutBuf;
}
while (Ptr != Buf) {
*OutBuf++ = *--Ptr;
}
return OutBuf;
}

/// Copy Str to OutBuf, returns a pointer to the end of the copied string
char *strCopy(char *OutBuf, const char *Str, int32_t Size = BufSize) {
while (*Str) {
*OutBuf++ = *Str++;
if (--Size <= 0)
return OutBuf;
}
return OutBuf;
}

void memSet(char *Buf, char C, uint32_t Size) {
for (int I = 0; I < Size; ++I)
*Buf++ = C;
}

void *memCpy(void *Dest, const void *Src, size_t Len) {
char *d = static_cast<char *>(Dest);
const char *s = static_cast<const char *>(Src);
while (Len--)
*d++ = *s++;
return Dest;
}

uint32_t strLen(const char *Str) {
uint32_t Size = 0;
while (*Str++)
++Size;
return Size;
}

#if !defined(__APPLE__)
// We use a stack-allocated buffer for string manipulation in many pieces of
// this code, including the code that prints each line of the fdata file. This
// buffer needs to accomodate large function names, but shouldn't be arbitrarily
// large (dynamically allocated) for simplicity of our memory space usage.
constexpr uint32_t BufSize = 10240;

// Declare some syscall wrappers we use throughout this code to avoid linking
// against system libc.
Expand Down Expand Up @@ -258,59 +316,6 @@ uint64_t __exit(uint64_t code) {
return ret;
}

// Helper functions for writing strings to the .fdata file. We intentionally
// avoid using libc names (lowercase memset) to make it clear it is our impl.

/// Write number Num using Base to the buffer in OutBuf, returns a pointer to
/// the end of the string.
char *intToStr(char *OutBuf, uint64_t Num, uint32_t Base) {
const char *Chars = "0123456789abcdef";
char Buf[21];
char *Ptr = Buf;
while (Num) {
*Ptr++ = *(Chars + (Num % Base));
Num /= Base;
}
if (Ptr == Buf) {
*OutBuf++ = '0';
return OutBuf;
}
while (Ptr != Buf) {
*OutBuf++ = *--Ptr;
}
return OutBuf;
}

/// Copy Str to OutBuf, returns a pointer to the end of the copied string
char *strCopy(char *OutBuf, const char *Str, int32_t Size = BufSize) {
while (*Str) {
*OutBuf++ = *Str++;
if (--Size <= 0)
return OutBuf;
}
return OutBuf;
}

void memSet(char *Buf, char C, uint32_t Size) {
for (int I = 0; I < Size; ++I)
*Buf++ = C;
}

void *memCpy(void *Dest, const void *Src, size_t Len) {
char *d = static_cast<char *>(Dest);
const char *s = static_cast<const char *>(Src);
while (Len--)
*d++ = *s++;
return Dest;
}

uint32_t strLen(const char *Str) {
uint32_t Size = 0;
while (*Str++)
++Size;
return Size;
}

void reportError(const char *Msg, uint64_t Size) {
__write(2, Msg, Size);
__exit(1);
Expand Down

0 comments on commit d6e60c5

Please sign in to comment.