xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
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 char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
47 extern char 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 char *__llvm_profile_begin_counters(void) {
69   return &PROF_CNTS_START;
70 }
71 COMPILER_RT_VISIBILITY char *__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                             uint64_t BinaryIdPadding) {
100   ProfDataIOVec BinaryIdIOVec[] = {
101       {&BinaryIdLen, sizeof(uint64_t), 1, 0},
102       {BinaryIdData, sizeof(uint8_t), BinaryIdLen, 0},
103       {NULL, sizeof(uint8_t), BinaryIdPadding, 1},
104   };
105   if (Writer->Write(Writer, BinaryIdIOVec,
106                     sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec)))
107     return -1;
108 
109   /* Successfully wrote binary id, report success. */
110   return 0;
111 }
112 
113 /*
114  * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID
115  * that contains build id. If build id exists, write binary id.
116  *
117  * Each note in notes section starts with a struct which includes
118  * n_namesz, n_descsz, and n_type members. It is followed by the name
119  * (whose length is defined in n_namesz) and then by the descriptor
120  * (whose length is defined in n_descsz).
121  *
122  * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned
123  * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.
124  */
125 static int WriteBinaryIdForNote(ProfDataWriter *Writer,
126                                 const ElfW(Nhdr) * Note) {
127   int BinaryIdSize = 0;
128   const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));
129   if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&
130       memcmp(NoteName, "GNU\0", 4) == 0) {
131     uint64_t BinaryIdLen = Note->n_descsz;
132     const uint8_t *BinaryIdData =
133         (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));
134     uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen);
135     if (Writer != NULL && WriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData,
136                                            BinaryIdPadding) == -1)
137       return -1;
138 
139     BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding;
140   }
141 
142   return BinaryIdSize;
143 }
144 
145 /*
146  * Helper function that iterates through notes section and find build ids.
147  * If writer is given, write binary ids into profiles.
148  * If an error happens while writing, return -1.
149  */
150 static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,
151                           const ElfW(Nhdr) * NotesEnd) {
152   int BinaryIdsSize = 0;
153   while (Note < NotesEnd) {
154     int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note);
155     if (OneBinaryIdSize == -1)
156       return -1;
157     BinaryIdsSize += OneBinaryIdSize;
158 
159     /* Calculate the offset of the next note in notes section. */
160     size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +
161                         RoundUp(Note->n_descsz, 4);
162     Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);
163   }
164 
165   return BinaryIdsSize;
166 }
167 
168 /*
169  * Write binary ids into profiles if writer is given.
170  * Return the total size of binary ids.
171  * If an error happens while writing, return -1.
172  */
173 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
174   extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));
175   const ElfW(Ehdr) *ElfHeader = &__ehdr_start;
176   const ElfW(Phdr) *ProgramHeader =
177       (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);
178 
179   int TotalBinaryIdsSize = 0;
180   uint32_t I;
181   /* Iterate through entries in the program header. */
182   for (I = 0; I < ElfHeader->e_phnum; I++) {
183     /* Look for the notes segment in program header entries. */
184     if (ProgramHeader[I].p_type != PT_NOTE)
185       continue;
186 
187     /* There can be multiple notes segment, and examine each of them. */
188     const ElfW(Nhdr) * Note;
189     const ElfW(Nhdr) * NotesEnd;
190     /*
191      * When examining notes in file, use p_offset, which is the offset within
192      * the elf file, to find the start of notes.
193      */
194     if (ProgramHeader[I].p_memsz == 0 ||
195         ProgramHeader[I].p_memsz == ProgramHeader[I].p_filesz) {
196       Note = (const ElfW(Nhdr) *)((uintptr_t)ElfHeader +
197                                   ProgramHeader[I].p_offset);
198       NotesEnd = (const ElfW(Nhdr) *)((const char *)(Note) +
199                                       ProgramHeader[I].p_filesz);
200     } else {
201       /*
202        * When examining notes in memory, use p_vaddr, which is the address of
203        * section after loaded to memory, to find the start of notes.
204        */
205       Note =
206           (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_vaddr);
207       NotesEnd =
208           (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz);
209     }
210 
211     int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd);
212     if (TotalBinaryIdsSize == -1)
213       return -1;
214 
215     TotalBinaryIdsSize += BinaryIdsSize;
216   }
217 
218   return TotalBinaryIdsSize;
219 }
220 #else /* !NT_GNU_BUILD_ID */
221 /*
222  * Fallback implementation for targets that don't support the GNU
223  * extensions NT_GNU_BUILD_ID and __ehdr_start.
224  */
225 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
226   return 0;
227 }
228 #endif
229 
230 #endif
231