1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stddef.h>
18 #include <stdint.h>
19 
20 #include "elf/relocation.h"
21 
22 // Following are naive implementations of functions in <string.h> that are not
23 // provided by the toolchain for RISC-V targets. They are simply for getting
24 // compilation passed and not necessarily optimzied.
25 extern "C" {
26 
memcmp(const void * s1,const void * s2,size_t count)27 int memcmp(const void* s1, const void* s2, size_t count) {
28   const uint8_t* left = static_cast<const uint8_t*>(s1);
29   const uint8_t* right = static_cast<const uint8_t*>(s2);
30   for (size_t i = 0; i < count; i++) {
31     if (left[i] == right[i]) {
32       continue;
33     }
34     return left[i] < right[i] ? -1 : 1;
35   }
36   return 0;
37 }
38 
memmove(void * dest,void const * src,size_t count)39 void* memmove(void* dest, void const* src, size_t count) {
40   uint8_t* _dest = static_cast<uint8_t*>(dest);
41   const uint8_t* _src = static_cast<const uint8_t*>(src);
42   if (dest < src) {
43     for (size_t i = 0; i < count; i++) {
44       _dest[i] = _src[i];
45     }
46   } else {
47     for (size_t i = count; i > 0; i--) {
48       _dest[i - 1] = _src[i - 1];
49     }
50   }
51   return dest;
52 }
53 
strlen(const char * str)54 size_t strlen(const char* str) {
55   size_t i = 0;
56   for (; str[i] != 0; i++) {
57   }
58   return i;
59 }
60 
memset(void * ptr,int value,size_t num)61 void* memset(void* ptr, int value, size_t num) {
62   uint8_t* start = static_cast<uint8_t*>(ptr);
63   for (size_t i = 0; i < num; i++) {
64     start[i] = value;
65   }
66   return ptr;
67 }
68 
memcpy(void * dest,const void * src,size_t num)69 void* memcpy(void* dest, const void* src, size_t num) {
70   return memmove(dest, src, num);
71 }
72 
ApplyRelocationHangIfFail(uintptr_t program_base,uintptr_t dynamic_section)73 void ApplyRelocationHangIfFail(uintptr_t program_base, uintptr_t dynamic_section) {
74   if (!ApplyRelocation(program_base, dynamic_section)) {
75     while (true) {
76     };
77   }
78 }
79 
80 }
81