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