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