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