xref: /llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c (revision 667e1fadcf4376ce41f5cae7cabab9f5ccc77b15)
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) || defined(__wasm__) || defined(__HAIKU__)
12 
13 #if !defined(_AIX) && !defined(__wasm__)
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 #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON)
24 #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON)
25 #define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON)
26 #define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON)
27 #define PROF_VNAME_START INSTR_PROF_SECT_START(INSTR_PROF_VNAME_COMMON)
28 #define PROF_VNAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNAME_COMMON)
29 #define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON)
30 #define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON)
31 #define PROF_VTABLE_START INSTR_PROF_SECT_START(INSTR_PROF_VTAB_COMMON)
32 #define PROF_VTABLE_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VTAB_COMMON)
33 #define PROF_BITS_START INSTR_PROF_SECT_START(INSTR_PROF_BITS_COMMON)
34 #define PROF_BITS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_BITS_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 #define PROF_COVINIT_START INSTR_PROF_SECT_START(INSTR_PROF_COVINIT_COMMON)
39 #define PROF_COVINIT_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_COVINIT_COMMON)
40 
41 /* Declare section start and stop symbols for various sections
42  * generated by compiler instrumentation.
43  */
44 extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY
45     COMPILER_RT_WEAK;
46 extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY
47     COMPILER_RT_WEAK;
48 extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
49 extern char PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
50 extern VTableProfData PROF_VTABLE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
51 extern VTableProfData PROF_VTABLE_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
52 extern char PROF_VNAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
53 extern char PROF_VNAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
54 extern char PROF_BITS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
55 extern char PROF_BITS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
56 extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
57 extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
58 extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
59 extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
60 extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
61 extern __llvm_gcov_init_func_struct PROF_COVINIT_START COMPILER_RT_VISIBILITY
62     COMPILER_RT_WEAK;
63 extern __llvm_gcov_init_func_struct PROF_COVINIT_STOP COMPILER_RT_VISIBILITY
64     COMPILER_RT_WEAK;
65 
66 COMPILER_RT_VISIBILITY const __llvm_profile_data *
67 __llvm_profile_begin_data(void) {
68   return &PROF_DATA_START;
69 }
70 COMPILER_RT_VISIBILITY const __llvm_profile_data *
71 __llvm_profile_end_data(void) {
72   return &PROF_DATA_STOP;
73 }
74 COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) {
75   return &PROF_NAME_START;
76 }
77 COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) {
78   return &PROF_NAME_STOP;
79 }
80 COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_vtabnames(void) {
81   return &PROF_VNAME_START;
82 }
83 COMPILER_RT_VISIBILITY const char *__llvm_profile_end_vtabnames(void) {
84   return &PROF_VNAME_STOP;
85 }
86 COMPILER_RT_VISIBILITY const VTableProfData *
87 __llvm_profile_begin_vtables(void) {
88   return &PROF_VTABLE_START;
89 }
90 COMPILER_RT_VISIBILITY const VTableProfData *__llvm_profile_end_vtables(void) {
91   return &PROF_VTABLE_STOP;
92 }
93 COMPILER_RT_VISIBILITY char *__llvm_profile_begin_counters(void) {
94   return &PROF_CNTS_START;
95 }
96 COMPILER_RT_VISIBILITY char *__llvm_profile_end_counters(void) {
97   return &PROF_CNTS_STOP;
98 }
99 COMPILER_RT_VISIBILITY char *__llvm_profile_begin_bitmap(void) {
100   return &PROF_BITS_START;
101 }
102 COMPILER_RT_VISIBILITY char *__llvm_profile_end_bitmap(void) {
103   return &PROF_BITS_STOP;
104 }
105 COMPILER_RT_VISIBILITY uint32_t *__llvm_profile_begin_orderfile(void) {
106   return &PROF_ORDERFILE_START;
107 }
108 
109 COMPILER_RT_VISIBILITY ValueProfNode *
110 __llvm_profile_begin_vnodes(void) {
111   return &PROF_VNODES_START;
112 }
113 COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) {
114   return &PROF_VNODES_STOP;
115 }
116 COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START;
117 COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP;
118 
119 COMPILER_RT_VISIBILITY const __llvm_gcov_init_func_struct *
120 __llvm_profile_begin_covinit() {
121   return &PROF_COVINIT_START;
122 }
123 
124 COMPILER_RT_VISIBILITY const __llvm_gcov_init_func_struct *
125 __llvm_profile_end_covinit() {
126   return &PROF_COVINIT_STOP;
127 }
128 
129 #ifdef NT_GNU_BUILD_ID
130 static size_t RoundUp(size_t size, size_t align) {
131   return (size + align - 1) & ~(align - 1);
132 }
133 
134 /*
135  * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID
136  * that contains build id. If build id exists, write binary id.
137  *
138  * Each note in notes section starts with a struct which includes
139  * n_namesz, n_descsz, and n_type members. It is followed by the name
140  * (whose length is defined in n_namesz) and then by the descriptor
141  * (whose length is defined in n_descsz).
142  *
143  * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned
144  * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.
145  */
146 static int WriteBinaryIdForNote(ProfDataWriter *Writer,
147                                 const ElfW(Nhdr) * Note) {
148   int BinaryIdSize = 0;
149   const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));
150   if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&
151       memcmp(NoteName, "GNU\0", 4) == 0) {
152     uint64_t BinaryIdLen = Note->n_descsz;
153     const uint8_t *BinaryIdData =
154         (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));
155     uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen);
156     if (Writer != NULL &&
157         lprofWriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData,
158                               BinaryIdPadding) == -1)
159       return -1;
160 
161     BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding;
162   }
163 
164   return BinaryIdSize;
165 }
166 
167 /*
168  * Helper function that iterates through notes section and find build ids.
169  * If writer is given, write binary ids into profiles.
170  * If an error happens while writing, return -1.
171  */
172 static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,
173                           const ElfW(Nhdr) * NotesEnd) {
174   int BinaryIdsSize = 0;
175   while (Note < NotesEnd) {
176     int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note);
177     if (OneBinaryIdSize == -1)
178       return -1;
179     BinaryIdsSize += OneBinaryIdSize;
180 
181     /* Calculate the offset of the next note in notes section. */
182     size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +
183                         RoundUp(Note->n_descsz, 4);
184     Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);
185   }
186 
187   return BinaryIdsSize;
188 }
189 
190 /*
191  * Write binary ids into profiles if writer is given.
192  * Return the total size of binary ids.
193  * If an error happens while writing, return -1.
194  */
195 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
196   extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));
197   extern ElfW(Dyn) _DYNAMIC[] __attribute__((weak, visibility("hidden")));
198 
199   const ElfW(Ehdr) *ElfHeader = &__ehdr_start;
200   const ElfW(Phdr) *ProgramHeader =
201       (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);
202 
203   /* Compute the added base address in case of position-independent code. */
204   uintptr_t Base = 0;
205   for (uint32_t I = 0; I < ElfHeader->e_phnum; I++) {
206     if (ProgramHeader[I].p_type == PT_PHDR)
207       Base = (uintptr_t)ProgramHeader - ProgramHeader[I].p_vaddr;
208     if (ProgramHeader[I].p_type == PT_DYNAMIC && _DYNAMIC)
209       Base = (uintptr_t)_DYNAMIC - ProgramHeader[I].p_vaddr;
210   }
211 
212   int TotalBinaryIdsSize = 0;
213   /* Iterate through entries in the program header. */
214   for (uint32_t I = 0; I < ElfHeader->e_phnum; I++) {
215     /* Look for the notes segment in program header entries. */
216     if (ProgramHeader[I].p_type != PT_NOTE)
217       continue;
218 
219     /* There can be multiple notes segment, and examine each of them. */
220     const ElfW(Nhdr) *Note =
221         (const ElfW(Nhdr) *)(Base + ProgramHeader[I].p_vaddr);
222     const ElfW(Nhdr) *NotesEnd =
223         (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz);
224 
225     int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd);
226     if (TotalBinaryIdsSize == -1)
227       return -1;
228 
229     TotalBinaryIdsSize += BinaryIdsSize;
230   }
231 
232   return TotalBinaryIdsSize;
233 }
234 #elif !defined(_AIX) /* !NT_GNU_BUILD_ID */
235 /*
236  * Fallback implementation for targets that don't support the GNU
237  * extensions NT_GNU_BUILD_ID and __ehdr_start.
238  */
239 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
240   return 0;
241 }
242 #endif
243 
244 #endif
245