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