xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c (revision 8881d206f4e68b564c2c5f50fc717086fc3e827a)
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 
12 #include <elf.h>
13 #include <link.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include "InstrProfiling.h"
18 #include "InstrProfilingInternal.h"
19 
20 #if defined(__FreeBSD__) && !defined(ElfW)
21 /*
22  * FreeBSD's elf.h and link.h headers do not define the ElfW(type) macro yet.
23  * If this is added to all supported FreeBSD versions in the future, this
24  * compatibility macro can be removed.
25  */
26 #define ElfW(type) __ElfN(type)
27 #endif
28 
29 #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON)
30 #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON)
31 #define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON)
32 #define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON)
33 #define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON)
34 #define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON)
35 #define PROF_ORDERFILE_START INSTR_PROF_SECT_START(INSTR_PROF_ORDERFILE_COMMON)
36 #define PROF_VNODES_START INSTR_PROF_SECT_START(INSTR_PROF_VNODES_COMMON)
37 #define PROF_VNODES_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNODES_COMMON)
38 
39 /* Declare section start and stop symbols for various sections
40  * generated by compiler instrumentation.
41  */
42 extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY
43     COMPILER_RT_WEAK;
44 extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY
45     COMPILER_RT_WEAK;
46 extern uint64_t PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
47 extern uint64_t PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
48 extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
49 extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
50 extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
51 extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
52 extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
53 
54 COMPILER_RT_VISIBILITY const __llvm_profile_data *
55 __llvm_profile_begin_data(void) {
56   return &PROF_DATA_START;
57 }
58 COMPILER_RT_VISIBILITY const __llvm_profile_data *
59 __llvm_profile_end_data(void) {
60   return &PROF_DATA_STOP;
61 }
62 COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) {
63   return &PROF_NAME_START;
64 }
65 COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) {
66   return &PROF_NAME_STOP;
67 }
68 COMPILER_RT_VISIBILITY uint64_t *__llvm_profile_begin_counters(void) {
69   return &PROF_CNTS_START;
70 }
71 COMPILER_RT_VISIBILITY uint64_t *__llvm_profile_end_counters(void) {
72   return &PROF_CNTS_STOP;
73 }
74 COMPILER_RT_VISIBILITY uint32_t *__llvm_profile_begin_orderfile(void) {
75   return &PROF_ORDERFILE_START;
76 }
77 
78 COMPILER_RT_VISIBILITY ValueProfNode *
79 __llvm_profile_begin_vnodes(void) {
80   return &PROF_VNODES_START;
81 }
82 COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) {
83   return &PROF_VNODES_STOP;
84 }
85 COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START;
86 COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP;
87 
88 #ifdef NT_GNU_BUILD_ID
89 static size_t RoundUp(size_t size, size_t align) {
90   return (size + align - 1) & ~(align - 1);
91 }
92 
93 /*
94  * Write binary id length and then its data, because binary id does not
95  * have a fixed length.
96  */
97 static int WriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen,
98                             const uint8_t *BinaryIdData) {
99   ProfDataIOVec BinaryIdIOVec[] = {
100       {&BinaryIdLen, sizeof(uint64_t), 1, 0},
101       {BinaryIdData, sizeof(uint8_t), BinaryIdLen, 0}};
102   if (Writer->Write(Writer, BinaryIdIOVec,
103                     sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec)))
104     return -1;
105 
106   /* Successfully wrote binary id, report success. */
107   return 0;
108 }
109 
110 /*
111  * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID
112  * that contains build id. If build id exists, write binary id.
113  *
114  * Each note in notes section starts with a struct which includes
115  * n_namesz, n_descsz, and n_type members. It is followed by the name
116  * (whose length is defined in n_namesz) and then by the descriptor
117  * (whose length is defined in n_descsz).
118  *
119  * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned
120  * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.
121  */
122 static int WriteBinaryIdForNote(ProfDataWriter *Writer,
123                                 const ElfW(Nhdr) * Note) {
124   int BinaryIdSize = 0;
125 
126   const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));
127   if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&
128       memcmp(NoteName, "GNU\0", 4) == 0) {
129 
130     uint64_t BinaryIdLen = Note->n_descsz;
131     const uint8_t *BinaryIdData =
132         (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));
133     if (Writer != NULL &&
134         WriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData) == -1)
135       return -1;
136 
137     BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen;
138   }
139 
140   return BinaryIdSize;
141 }
142 
143 /*
144  * Helper function that iterates through notes section and find build ids.
145  * If writer is given, write binary ids into profiles.
146  * If an error happens while writing, return -1.
147  */
148 static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,
149                           const ElfW(Nhdr) * NotesEnd) {
150   int TotalBinaryIdsSize = 0;
151   while (Note < NotesEnd) {
152     int Result = WriteBinaryIdForNote(Writer, Note);
153     if (Result == -1)
154       return -1;
155     TotalBinaryIdsSize += Result;
156 
157     /* Calculate the offset of the next note in notes section. */
158     size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +
159                         RoundUp(Note->n_descsz, 4);
160     Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);
161   }
162 
163   return TotalBinaryIdsSize;
164 }
165 
166 /*
167  * Write binary ids into profiles if writer is given.
168  * Return the total size of binary ids.
169  * If an error happens while writing, return -1.
170  */
171 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
172   extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));
173   const ElfW(Ehdr) *ElfHeader = &__ehdr_start;
174   const ElfW(Phdr) *ProgramHeader =
175       (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);
176 
177   uint32_t I;
178   /* Iterate through entries in the program header. */
179   for (I = 0; I < ElfHeader->e_phnum; I++) {
180     /* Look for the notes section in program header entries. */
181     if (ProgramHeader[I].p_type != PT_NOTE)
182       continue;
183 
184     const ElfW(Nhdr) *Note =
185         (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_offset);
186     const ElfW(Nhdr) *NotesEnd =
187         (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_filesz);
188     return WriteBinaryIds(Writer, Note, NotesEnd);
189   }
190 
191   return 0;
192 }
193 #else /* !NT_GNU_BUILD_ID */
194 /*
195  * Fallback implementation for targets that don't support the GNU
196  * extensions NT_GNU_BUILD_ID and __ehdr_start.
197  */
198 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
199   return 0;
200 }
201 #endif
202 
203 #endif
204