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