Skip to content

Commit

Permalink
[BOLT] Add runtime functions required by freestanding environment
Browse files Browse the repository at this point in the history
Compiler can generate calls to some functions implicitly, even under
constraints of freestanding environment. Make sure these functions are
available in our runtime objects.

Fixes test failures on some systems after https://reviews.llvm.org/D128960.

Reviewed By: yota9

Differential Revision: https://reviews.llvm.org/D129168
  • Loading branch information
maksfb committed Jul 6, 2022
1 parent 8a66867 commit ea2182f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
58 changes: 44 additions & 14 deletions bolt/runtime/common.h
Expand Up @@ -76,6 +76,49 @@ typedef int int32_t;
"pop %%rbx\n" \
"pop %%rax\n"

// Functions that are required by freestanding environment. Compiler may
// generate calls to these implicitly.
extern "C" {
void *memcpy(void *Dest, const void *Src, size_t Len) {
uint8_t *d = static_cast<uint8_t *>(Dest);
const uint8_t *s = static_cast<const uint8_t *>(Src);
while (Len--)
*d++ = *s++;
return Dest;
}

void *memmove(void *Dest, const void *Src, size_t Len) {
uint8_t *d = static_cast<uint8_t *>(Dest);
const uint8_t *s = static_cast<const uint8_t *>(Src);
if (d < s) {
while (Len--)
*d++ = *s++;
} else {
s += Len - 1;
d += Len - 1;
while (Len--)
*d-- = *s--;
}

return Dest;
}

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

int memcmp(const void *s1, const void *s2, size_t n) {
const uint8_t *c1 = static_cast<const uint8_t *>(s1);
const uint8_t *c2 = static_cast<const uint8_t *>(s2);
for (; n--; c1++, c2++) {
if (*c1 != *c2)
return *c1 < *c2 ? -1 : 1;
}
return 0;
}
} // extern "C"

// Anonymous namespace covering everything but our library entry point
namespace {

Expand Down Expand Up @@ -187,7 +230,7 @@ uint64_t __exit(uint64_t code) {
}

// 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.
// avoid using libc names 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.
Expand Down Expand Up @@ -231,19 +274,6 @@ int strnCmp(const char *Str1, const char *Str2, size_t Num) {
return *(unsigned char *)Str1 - *(unsigned char *)Str2;
}

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++)
Expand Down
4 changes: 2 additions & 2 deletions bolt/runtime/hugify.cpp
Expand Up @@ -59,7 +59,7 @@ static void hugify_for_old_kernel(uint8_t *from, uint8_t *to) {
#endif

// Copy the hot code to a temproary location.
memCpy(mem, from, size);
memcpy(mem, from, size);

// Maps out the existing hot code.
if (__mmap(reinterpret_cast<uint64_t>(from), size,
Expand All @@ -77,7 +77,7 @@ static void hugify_for_old_kernel(uint8_t *from, uint8_t *to) {
}

// Copy the hot code back.
memCpy(from, mem, size);
memcpy(from, mem, size);

// Change permission back to read-only, ignore failure
__mprotect(from, size, PROT_READ | PROT_EXEC);
Expand Down
6 changes: 3 additions & 3 deletions bolt/runtime/instr.cpp
Expand Up @@ -224,15 +224,15 @@ BumpPtrAllocator GlobalAlloc;
void *operator new(size_t Sz, BumpPtrAllocator &A) { return A.allocate(Sz); }
void *operator new(size_t Sz, BumpPtrAllocator &A, char C) {
auto *Ptr = reinterpret_cast<char *>(A.allocate(Sz));
memSet(Ptr, C, Sz);
memset(Ptr, C, Sz);
return Ptr;
}
void *operator new[](size_t Sz, BumpPtrAllocator &A) {
return A.allocate(Sz);
}
void *operator new[](size_t Sz, BumpPtrAllocator &A, char C) {
auto *Ptr = reinterpret_cast<char *>(A.allocate(Sz));
memSet(Ptr, C, Sz);
memset(Ptr, C, Sz);
return Ptr;
}
// Only called during exception unwinding (useless). We must manually dealloc.
Expand Down Expand Up @@ -1438,7 +1438,7 @@ int openProfile() {
/// Where 0xdeadbeef is this function address and PROCESSNAME your binary file
/// name.
extern "C" void __bolt_instr_clear_counters() {
memSet(reinterpret_cast<char *>(__bolt_instr_locations), 0,
memset(reinterpret_cast<char *>(__bolt_instr_locations), 0,
__bolt_num_counters * 8);
for (int I = 0; I < __bolt_instr_num_ind_calls; ++I)
GlobalIndCallCounters[I].resetCounters();
Expand Down

0 comments on commit ea2182f

Please sign in to comment.