1 /*===- InstrProfilingPlatformLinux.c - Profile data Linux platform ------===*\
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
7 \*===----------------------------------------------------------------------===*/
9 #if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
10 (defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) || \
11 defined(_AIX) || defined(__wasm__) || defined(__HAIKU__)
13 #if !defined(_AIX) && !defined(__wasm__)
20 #include "InstrProfiling.h"
21 #include "InstrProfilingInternal.h"
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 #define PROF_COVINIT_START INSTR_PROF_SECT_START(INSTR_PROF_COVINIT_COMMON)
39 #define PROF_COVINIT_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_COVINIT_COMMON)
41 /* Declare section start and stop symbols for various sections
42 * generated by compiler instrumentation.
44 extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY
46 extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY
48 extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
49 extern char PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
50 extern VTableProfData PROF_VTABLE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
51 extern VTableProfData PROF_VTABLE_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
52 extern char PROF_VNAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
53 extern char PROF_VNAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
54 extern char PROF_BITS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
55 extern char PROF_BITS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
56 extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
57 extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
58 extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
59 extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
60 extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK
;
61 extern __llvm_gcov_init_func_struct PROF_COVINIT_START COMPILER_RT_VISIBILITY
63 extern __llvm_gcov_init_func_struct PROF_COVINIT_STOP COMPILER_RT_VISIBILITY
66 COMPILER_RT_VISIBILITY
const __llvm_profile_data
*
67 __llvm_profile_begin_data(void) {
68 return &PROF_DATA_START
;
70 COMPILER_RT_VISIBILITY
const __llvm_profile_data
*
71 __llvm_profile_end_data(void) {
72 return &PROF_DATA_STOP
;
74 COMPILER_RT_VISIBILITY
const char *__llvm_profile_begin_names(void) {
75 return &PROF_NAME_START
;
77 COMPILER_RT_VISIBILITY
const char *__llvm_profile_end_names(void) {
78 return &PROF_NAME_STOP
;
80 COMPILER_RT_VISIBILITY
const char *__llvm_profile_begin_vtabnames(void) {
81 return &PROF_VNAME_START
;
83 COMPILER_RT_VISIBILITY
const char *__llvm_profile_end_vtabnames(void) {
84 return &PROF_VNAME_STOP
;
86 COMPILER_RT_VISIBILITY
const VTableProfData
*
87 __llvm_profile_begin_vtables(void) {
88 return &PROF_VTABLE_START
;
90 COMPILER_RT_VISIBILITY
const VTableProfData
*__llvm_profile_end_vtables(void) {
91 return &PROF_VTABLE_STOP
;
93 COMPILER_RT_VISIBILITY
char *__llvm_profile_begin_counters(void) {
94 return &PROF_CNTS_START
;
96 COMPILER_RT_VISIBILITY
char *__llvm_profile_end_counters(void) {
97 return &PROF_CNTS_STOP
;
99 COMPILER_RT_VISIBILITY
char *__llvm_profile_begin_bitmap(void) {
100 return &PROF_BITS_START
;
102 COMPILER_RT_VISIBILITY
char *__llvm_profile_end_bitmap(void) {
103 return &PROF_BITS_STOP
;
105 COMPILER_RT_VISIBILITY
uint32_t *__llvm_profile_begin_orderfile(void) {
106 return &PROF_ORDERFILE_START
;
109 COMPILER_RT_VISIBILITY ValueProfNode
*
110 __llvm_profile_begin_vnodes(void) {
111 return &PROF_VNODES_START
;
113 COMPILER_RT_VISIBILITY ValueProfNode
*__llvm_profile_end_vnodes(void) {
114 return &PROF_VNODES_STOP
;
116 COMPILER_RT_VISIBILITY ValueProfNode
*CurrentVNode
= &PROF_VNODES_START
;
117 COMPILER_RT_VISIBILITY ValueProfNode
*EndVNode
= &PROF_VNODES_STOP
;
119 COMPILER_RT_VISIBILITY
const __llvm_gcov_init_func_struct
*
120 __llvm_profile_begin_covinit() {
121 return &PROF_COVINIT_START
;
124 COMPILER_RT_VISIBILITY
const __llvm_gcov_init_func_struct
*
125 __llvm_profile_end_covinit() {
126 return &PROF_COVINIT_STOP
;
129 #ifdef NT_GNU_BUILD_ID
130 static size_t RoundUp(size_t size
, size_t align
) {
131 return (size
+ align
- 1) & ~(align
- 1);
135 * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID
136 * that contains build id. If build id exists, write binary id.
138 * Each note in notes section starts with a struct which includes
139 * n_namesz, n_descsz, and n_type members. It is followed by the name
140 * (whose length is defined in n_namesz) and then by the descriptor
141 * (whose length is defined in n_descsz).
143 * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned
144 * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.
146 static int WriteBinaryIdForNote(ProfDataWriter
*Writer
,
147 const ElfW(Nhdr
) * Note
) {
148 int BinaryIdSize
= 0;
149 const char *NoteName
= (const char *)Note
+ sizeof(ElfW(Nhdr
));
150 if (Note
->n_type
== NT_GNU_BUILD_ID
&& Note
->n_namesz
== 4 &&
151 memcmp(NoteName
, "GNU\0", 4) == 0) {
152 uint64_t BinaryIdLen
= Note
->n_descsz
;
153 const uint8_t *BinaryIdData
=
154 (const uint8_t *)(NoteName
+ RoundUp(Note
->n_namesz
, 4));
155 uint8_t BinaryIdPadding
= __llvm_profile_get_num_padding_bytes(BinaryIdLen
);
156 if (Writer
!= NULL
&&
157 lprofWriteOneBinaryId(Writer
, BinaryIdLen
, BinaryIdData
,
158 BinaryIdPadding
) == -1)
161 BinaryIdSize
= sizeof(BinaryIdLen
) + BinaryIdLen
+ BinaryIdPadding
;
168 * Helper function that iterates through notes section and find build ids.
169 * If writer is given, write binary ids into profiles.
170 * If an error happens while writing, return -1.
172 static int WriteBinaryIds(ProfDataWriter
*Writer
, const ElfW(Nhdr
) * Note
,
173 const ElfW(Nhdr
) * NotesEnd
) {
174 int BinaryIdsSize
= 0;
175 while (Note
< NotesEnd
) {
176 int OneBinaryIdSize
= WriteBinaryIdForNote(Writer
, Note
);
177 if (OneBinaryIdSize
== -1)
179 BinaryIdsSize
+= OneBinaryIdSize
;
181 /* Calculate the offset of the next note in notes section. */
182 size_t NoteOffset
= sizeof(ElfW(Nhdr
)) + RoundUp(Note
->n_namesz
, 4) +
183 RoundUp(Note
->n_descsz
, 4);
184 Note
= (const ElfW(Nhdr
) *)((const char *)(Note
) + NoteOffset
);
187 return BinaryIdsSize
;
191 * Write binary ids into profiles if writer is given.
192 * Return the total size of binary ids.
193 * If an error happens while writing, return -1.
195 COMPILER_RT_VISIBILITY
int __llvm_write_binary_ids(ProfDataWriter
*Writer
) {
196 extern const ElfW(Ehdr
) __ehdr_start
__attribute__((visibility("hidden")));
197 const ElfW(Ehdr
) *ElfHeader
= &__ehdr_start
;
198 const ElfW(Phdr
) *ProgramHeader
=
199 (const ElfW(Phdr
) *)((uintptr_t)ElfHeader
+ ElfHeader
->e_phoff
);
201 int TotalBinaryIdsSize
= 0;
203 /* Iterate through entries in the program header. */
204 for (I
= 0; I
< ElfHeader
->e_phnum
; I
++) {
205 /* Look for the notes segment in program header entries. */
206 if (ProgramHeader
[I
].p_type
!= PT_NOTE
)
209 /* There can be multiple notes segment, and examine each of them. */
210 const ElfW(Nhdr
) * Note
;
211 const ElfW(Nhdr
) * NotesEnd
;
213 * When examining notes in file, use p_offset, which is the offset within
214 * the elf file, to find the start of notes.
216 if (ProgramHeader
[I
].p_memsz
== 0 ||
217 ProgramHeader
[I
].p_memsz
== ProgramHeader
[I
].p_filesz
) {
218 Note
= (const ElfW(Nhdr
) *)((uintptr_t)ElfHeader
+
219 ProgramHeader
[I
].p_offset
);
220 NotesEnd
= (const ElfW(Nhdr
) *)((const char *)(Note
) +
221 ProgramHeader
[I
].p_filesz
);
224 * When examining notes in memory, use p_vaddr, which is the address of
225 * section after loaded to memory, to find the start of notes.
228 (const ElfW(Nhdr
) *)((uintptr_t)ElfHeader
+ ProgramHeader
[I
].p_vaddr
);
230 (const ElfW(Nhdr
) *)((const char *)(Note
) + ProgramHeader
[I
].p_memsz
);
233 int BinaryIdsSize
= WriteBinaryIds(Writer
, Note
, NotesEnd
);
234 if (TotalBinaryIdsSize
== -1)
237 TotalBinaryIdsSize
+= BinaryIdsSize
;
240 return TotalBinaryIdsSize
;
242 #elif !defined(_AIX) /* !NT_GNU_BUILD_ID */
244 * Fallback implementation for targets that don't support the GNU
245 * extensions NT_GNU_BUILD_ID and __ehdr_start.
247 COMPILER_RT_VISIBILITY
int __llvm_write_binary_ids(ProfDataWriter
*Writer
) {