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