xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c (revision d9a42747950146bf03cda7f6e25d219253f8a57a)
1 /*===- InstrProfilingPlatformLinux.c - Profile data Linux 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(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
10     (defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) || \
11     defined(_AIX)
12 
13 #if !defined(_AIX)
14 #include <elf.h>
15 #include <link.h>
16 #endif
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #include "InstrProfiling.h"
21 #include "InstrProfilingInternal.h"
22 
23 #if defined(__FreeBSD__) && !defined(ElfW)
24 /*
25  * FreeBSD's elf.h and link.h headers do not define the ElfW(type) macro yet.
26  * If this is added to all supported FreeBSD versions in the future, this
27  * compatibility macro can be removed.
28  */
29 #define ElfW(type) __ElfN(type)
30 #endif
31 
32 #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON)
33 #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON)
34 #define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON)
35 #define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON)
36 #define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON)
37 #define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON)
38 #define PROF_ORDERFILE_START INSTR_PROF_SECT_START(INSTR_PROF_ORDERFILE_COMMON)
39 #define PROF_VNODES_START INSTR_PROF_SECT_START(INSTR_PROF_VNODES_COMMON)
40 #define PROF_VNODES_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNODES_COMMON)
41 
42 /* Declare section start and stop symbols for various sections
43  * generated by compiler instrumentation.
44  */
45 extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY
46     COMPILER_RT_WEAK;
47 extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY
48     COMPILER_RT_WEAK;
49 extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
50 extern char PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
51 extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
52 extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
53 extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
54 extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
55 extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
56 
57 COMPILER_RT_VISIBILITY const __llvm_profile_data *
58 __llvm_profile_begin_data(void) {
59   return &PROF_DATA_START;
60 }
61 COMPILER_RT_VISIBILITY const __llvm_profile_data *
62 __llvm_profile_end_data(void) {
63   return &PROF_DATA_STOP;
64 }
65 COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) {
66   return &PROF_NAME_START;
67 }
68 COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) {
69   return &PROF_NAME_STOP;
70 }
71 COMPILER_RT_VISIBILITY char *__llvm_profile_begin_counters(void) {
72   return &PROF_CNTS_START;
73 }
74 COMPILER_RT_VISIBILITY char *__llvm_profile_end_counters(void) {
75   return &PROF_CNTS_STOP;
76 }
77 COMPILER_RT_VISIBILITY uint32_t *__llvm_profile_begin_orderfile(void) {
78   return &PROF_ORDERFILE_START;
79 }
80 
81 COMPILER_RT_VISIBILITY ValueProfNode *
82 __llvm_profile_begin_vnodes(void) {
83   return &PROF_VNODES_START;
84 }
85 COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) {
86   return &PROF_VNODES_STOP;
87 }
88 COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START;
89 COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP;
90 
91 #ifdef NT_GNU_BUILD_ID
92 static size_t RoundUp(size_t size, size_t align) {
93   return (size + align - 1) & ~(align - 1);
94 }
95 
96 /*
97  * Write binary id length and then its data, because binary id does not
98  * have a fixed length.
99  */
100 static int WriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen,
101                             const uint8_t *BinaryIdData,
102                             uint64_t BinaryIdPadding) {
103   ProfDataIOVec BinaryIdIOVec[] = {
104       {&BinaryIdLen, sizeof(uint64_t), 1, 0},
105       {BinaryIdData, sizeof(uint8_t), BinaryIdLen, 0},
106       {NULL, sizeof(uint8_t), BinaryIdPadding, 1},
107   };
108   if (Writer->Write(Writer, BinaryIdIOVec,
109                     sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec)))
110     return -1;
111 
112   /* Successfully wrote binary id, report success. */
113   return 0;
114 }
115 
116 /*
117  * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID
118  * that contains build id. If build id exists, write binary id.
119  *
120  * Each note in notes section starts with a struct which includes
121  * n_namesz, n_descsz, and n_type members. It is followed by the name
122  * (whose length is defined in n_namesz) and then by the descriptor
123  * (whose length is defined in n_descsz).
124  *
125  * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned
126  * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.
127  */
128 static int WriteBinaryIdForNote(ProfDataWriter *Writer,
129                                 const ElfW(Nhdr) * Note) {
130   int BinaryIdSize = 0;
131   const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));
132   if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&
133       memcmp(NoteName, "GNU\0", 4) == 0) {
134     uint64_t BinaryIdLen = Note->n_descsz;
135     const uint8_t *BinaryIdData =
136         (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));
137     uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen);
138     if (Writer != NULL && WriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData,
139                                            BinaryIdPadding) == -1)
140       return -1;
141 
142     BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding;
143   }
144 
145   return BinaryIdSize;
146 }
147 
148 /*
149  * Helper function that iterates through notes section and find build ids.
150  * If writer is given, write binary ids into profiles.
151  * If an error happens while writing, return -1.
152  */
153 static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,
154                           const ElfW(Nhdr) * NotesEnd) {
155   int BinaryIdsSize = 0;
156   while (Note < NotesEnd) {
157     int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note);
158     if (OneBinaryIdSize == -1)
159       return -1;
160     BinaryIdsSize += OneBinaryIdSize;
161 
162     /* Calculate the offset of the next note in notes section. */
163     size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +
164                         RoundUp(Note->n_descsz, 4);
165     Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);
166   }
167 
168   return BinaryIdsSize;
169 }
170 
171 /*
172  * Write binary ids into profiles if writer is given.
173  * Return the total size of binary ids.
174  * If an error happens while writing, return -1.
175  */
176 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
177   extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));
178   const ElfW(Ehdr) *ElfHeader = &__ehdr_start;
179   const ElfW(Phdr) *ProgramHeader =
180       (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);
181 
182   int TotalBinaryIdsSize = 0;
183   uint32_t I;
184   /* Iterate through entries in the program header. */
185   for (I = 0; I < ElfHeader->e_phnum; I++) {
186     /* Look for the notes segment in program header entries. */
187     if (ProgramHeader[I].p_type != PT_NOTE)
188       continue;
189 
190     /* There can be multiple notes segment, and examine each of them. */
191     const ElfW(Nhdr) * Note;
192     const ElfW(Nhdr) * NotesEnd;
193     /*
194      * When examining notes in file, use p_offset, which is the offset within
195      * the elf file, to find the start of notes.
196      */
197     if (ProgramHeader[I].p_memsz == 0 ||
198         ProgramHeader[I].p_memsz == ProgramHeader[I].p_filesz) {
199       Note = (const ElfW(Nhdr) *)((uintptr_t)ElfHeader +
200                                   ProgramHeader[I].p_offset);
201       NotesEnd = (const ElfW(Nhdr) *)((const char *)(Note) +
202                                       ProgramHeader[I].p_filesz);
203     } else {
204       /*
205        * When examining notes in memory, use p_vaddr, which is the address of
206        * section after loaded to memory, to find the start of notes.
207        */
208       Note =
209           (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_vaddr);
210       NotesEnd =
211           (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz);
212     }
213 
214     int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd);
215     if (TotalBinaryIdsSize == -1)
216       return -1;
217 
218     TotalBinaryIdsSize += BinaryIdsSize;
219   }
220 
221   return TotalBinaryIdsSize;
222 }
223 #else /* !NT_GNU_BUILD_ID */
224 /*
225  * Fallback implementation for targets that don't support the GNU
226  * extensions NT_GNU_BUILD_ID and __ehdr_start.
227  */
228 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
229   return 0;
230 }
231 #endif
232 
233 #if defined(_AIX)
234 // Empty stubs to allow linking object files using the registration-based scheme
235 COMPILER_RT_VISIBILITY
236 void __llvm_profile_register_function(void *Data_) {}
237 
238 COMPILER_RT_VISIBILITY
239 void __llvm_profile_register_names_function(void *NamesStart,
240                                             uint64_t NamesSize) {}
241 
242 // The __start_SECNAME and __stop_SECNAME symbols (for SECNAME \in
243 // {"__llvm_prf_cnts", "__llvm_prf_data", "__llvm_prf_name", "__llvm_prf_vnds"})
244 // are always live when linking on AIX, regardless if the .o's being linked
245 // reference symbols from the profile library (for example when no files were
246 // compiled with -fprofile-generate). That's because these symbols are kept
247 // alive through references in constructor functions that are always live in the
248 // default linking model on AIX (-bcdtors:all). The __start_SECNAME and
249 // __stop_SECNAME symbols are only resolved by the linker when the SECNAME
250 // section exists. So for the scenario where the user objects have no such
251 // section (i.e. when they are compiled with -fno-profile-generate), we always
252 // define these zero length variables in each of the above 4 sections.
253 COMPILER_RT_VISIBILITY int dummy_cnts[0] COMPILER_RT_SECTION(
254     COMPILER_RT_SEG INSTR_PROF_CNTS_SECT_NAME);
255 COMPILER_RT_VISIBILITY int dummy_data[0] COMPILER_RT_SECTION(
256     COMPILER_RT_SEG INSTR_PROF_DATA_SECT_NAME);
257 COMPILER_RT_VISIBILITY const int dummy_name[0] COMPILER_RT_SECTION(
258     COMPILER_RT_SEG INSTR_PROF_NAME_SECT_NAME);
259 COMPILER_RT_VISIBILITY int dummy_vnds[0] COMPILER_RT_SECTION(
260     COMPILER_RT_SEG INSTR_PROF_VNODES_SECT_NAME);
261 
262 // Create a fake reference to avoid GC'ing of the dummy variables by the linker.
263 // Ideally, we create a ".ref" of each variable inside the function
264 // __llvm_profile_begin_counters(), but there's no source level construct
265 // that allows us to generate that.
266 __attribute__((destructor)) void keep() {
267   int volatile use = &dummy_cnts < &dummy_data && &dummy_name < &dummy_vnds;
268   (void)use;
269 }
270 #endif
271 
272 #endif
273