xref: /llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformOther.c (revision d084bc291a21895fa2ecc74e2d1c9d1818ba4fd7)
1 /*===- InstrProfilingPlatformOther.c - Profile data default platform ------===*\
2 |*
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 |* See https://llvm.org/LICENSE.txt for license information.
5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 |*
7 \*===----------------------------------------------------------------------===*/
8 
9 #if !defined(__APPLE__) && !defined(__linux__) && !defined(__FreeBSD__) &&     \
10     !defined(__Fuchsia__) && !(defined(__sun__) && defined(__svr4__)) &&       \
11     !defined(__NetBSD__) && !defined(_WIN32) && !defined(_AIX) &&              \
12     !defined(__wasm__) && !defined(__HAIKU__)
13 
14 #include <stdlib.h>
15 #include <stdio.h>
16 
17 #include "InstrProfiling.h"
18 #include "InstrProfilingInternal.h"
19 
20 static const __llvm_profile_data *DataFirst = NULL;
21 static const __llvm_profile_data *DataLast = NULL;
22 static const VTableProfData *VTableProfDataFirst = NULL;
23 static const VTableProfData *VTableProfDataLast = NULL;
24 static const char *NamesFirst = NULL;
25 static const char *NamesLast = NULL;
26 static const char *VNamesFirst = NULL;
27 static const char *VNamesLast = NULL;
28 static char *CountersFirst = NULL;
29 static char *CountersLast = NULL;
30 static uint32_t *OrderFileFirst = NULL;
31 
32 static const void *getMinAddr(const void *A1, const void *A2) {
33   return A1 < A2 ? A1 : A2;
34 }
35 
36 static const void *getMaxAddr(const void *A1, const void *A2) {
37   return A1 > A2 ? A1 : A2;
38 }
39 
40 /*!
41  * \brief Register an instrumented function.
42  *
43  * Calls to this are emitted by clang with -fprofile-instr-generate.  Such
44  * calls are only required (and only emitted) on targets where we haven't
45  * implemented linker magic to find the bounds of the sections.
46  */
47 COMPILER_RT_VISIBILITY
48 void __llvm_profile_register_function(void *Data_) {
49   /* TODO: Only emit this function if we can't use linker magic. */
50   const __llvm_profile_data *Data = (__llvm_profile_data *)Data_;
51   if (!DataFirst) {
52     DataFirst = Data;
53     DataLast = Data + 1;
54     CountersFirst = (char *)((uintptr_t)Data_ + Data->CounterPtr);
55     CountersLast =
56         CountersFirst + Data->NumCounters * __llvm_profile_counter_entry_size();
57     return;
58   }
59 
60   DataFirst = (const __llvm_profile_data *)getMinAddr(DataFirst, Data);
61   CountersFirst = (char *)getMinAddr(
62       CountersFirst, (char *)((uintptr_t)Data_ + Data->CounterPtr));
63 
64   DataLast = (const __llvm_profile_data *)getMaxAddr(DataLast, Data + 1);
65   CountersLast = (char *)getMaxAddr(
66       CountersLast,
67       (char *)((uintptr_t)Data_ + Data->CounterPtr) +
68           Data->NumCounters * __llvm_profile_counter_entry_size());
69 }
70 
71 COMPILER_RT_VISIBILITY
72 void __llvm_profile_register_names_function(void *NamesStart,
73                                             uint64_t NamesSize) {
74   if (!NamesFirst) {
75     NamesFirst = (const char *)NamesStart;
76     NamesLast = (const char *)NamesStart + NamesSize;
77     return;
78   }
79   NamesFirst = (const char *)getMinAddr(NamesFirst, NamesStart);
80   NamesLast =
81       (const char *)getMaxAddr(NamesLast, (const char *)NamesStart + NamesSize);
82 }
83 
84 COMPILER_RT_VISIBILITY
85 const __llvm_profile_data *__llvm_profile_begin_data(void) { return DataFirst; }
86 COMPILER_RT_VISIBILITY
87 const __llvm_profile_data *__llvm_profile_end_data(void) { return DataLast; }
88 COMPILER_RT_VISIBILITY const VTableProfData *
89 __llvm_profile_begin_vtables(void) {
90   return VTableProfDataFirst;
91 }
92 COMPILER_RT_VISIBILITY const VTableProfData *__llvm_profile_end_vtables(void) {
93   return VTableProfDataLast;
94 }
95 COMPILER_RT_VISIBILITY
96 const char *__llvm_profile_begin_names(void) { return NamesFirst; }
97 COMPILER_RT_VISIBILITY
98 const char *__llvm_profile_end_names(void) { return NamesLast; }
99 COMPILER_RT_VISIBILITY
100 const char *__llvm_profile_begin_vtabnames(void) { return VNamesFirst; }
101 COMPILER_RT_VISIBILITY
102 const char *__llvm_profile_end_vtabnames(void) { return VNamesLast; }
103 COMPILER_RT_VISIBILITY
104 char *__llvm_profile_begin_counters(void) { return CountersFirst; }
105 COMPILER_RT_VISIBILITY
106 char *__llvm_profile_end_counters(void) { return CountersLast; }
107 COMPILER_RT_VISIBILITY
108 char *__llvm_profile_begin_bitmap(void) { return BitmapFirst; }
109 COMPILER_RT_VISIBILITY
110 char *__llvm_profile_end_bitmap(void) { return BitmapLast; }
111 /* TODO: correctly set up OrderFileFirst. */
112 COMPILER_RT_VISIBILITY
113 uint32_t *__llvm_profile_begin_orderfile(void) { return OrderFileFirst; }
114 
115 COMPILER_RT_VISIBILITY
116 ValueProfNode *__llvm_profile_begin_vnodes(void) {
117   return 0;
118 }
119 COMPILER_RT_VISIBILITY
120 ValueProfNode *__llvm_profile_end_vnodes(void) { return 0; }
121 
122 COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = 0;
123 COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = 0;
124 
125 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
126   return 0;
127 }
128 
129 #endif
130