forked from OSchip/llvm-project
[BOLT] Add runtime functions required by freestanding environment
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
This commit is contained in:
parent
8a668671a0
commit
ea2182fedd
|
@ -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 {
|
||||
|
||||
|
@ -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.
|
||||
|
@ -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++)
|
||||
|
|
|
@ -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,
|
||||
|
@ -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);
|
||||
|
|
|
@ -224,7 +224,7 @@ 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) {
|
||||
|
@ -232,7 +232,7 @@ void *operator new[](size_t Sz, BumpPtrAllocator &A) {
|
|||
}
|
||||
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.
|
||||
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue