xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric /*===- InstrProfilingPlatformLinux.c - Profile data Linux platform ------===*\
20b57cec5SDimitry Andric |*
30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric |*
70b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
1081ad6265SDimitry Andric     (defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) || \
1181ad6265SDimitry Andric     defined(_AIX)
120b57cec5SDimitry Andric 
1381ad6265SDimitry Andric #if !defined(_AIX)
14fe6060f1SDimitry Andric #include <elf.h>
15fe6060f1SDimitry Andric #include <link.h>
1681ad6265SDimitry Andric #endif
170b57cec5SDimitry Andric #include <stdlib.h>
18fe6060f1SDimitry Andric #include <string.h>
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric #include "InstrProfiling.h"
21fe6060f1SDimitry Andric #include "InstrProfilingInternal.h"
220b57cec5SDimitry Andric 
236e75b2fbSDimitry Andric #if defined(__FreeBSD__) && !defined(ElfW)
246e75b2fbSDimitry Andric /*
256e75b2fbSDimitry Andric  * FreeBSD's elf.h and link.h headers do not define the ElfW(type) macro yet.
266e75b2fbSDimitry Andric  * If this is added to all supported FreeBSD versions in the future, this
276e75b2fbSDimitry Andric  * compatibility macro can be removed.
286e75b2fbSDimitry Andric  */
296e75b2fbSDimitry Andric #define ElfW(type) __ElfN(type)
306e75b2fbSDimitry Andric #endif
316e75b2fbSDimitry Andric 
320b57cec5SDimitry Andric #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON)
330b57cec5SDimitry Andric #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON)
340b57cec5SDimitry Andric #define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON)
350b57cec5SDimitry Andric #define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON)
360b57cec5SDimitry Andric #define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON)
370b57cec5SDimitry Andric #define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON)
380b57cec5SDimitry Andric #define PROF_ORDERFILE_START INSTR_PROF_SECT_START(INSTR_PROF_ORDERFILE_COMMON)
390b57cec5SDimitry Andric #define PROF_VNODES_START INSTR_PROF_SECT_START(INSTR_PROF_VNODES_COMMON)
400b57cec5SDimitry Andric #define PROF_VNODES_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNODES_COMMON)
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric /* Declare section start and stop symbols for various sections
430b57cec5SDimitry Andric  * generated by compiler instrumentation.
440b57cec5SDimitry Andric  */
45fe6060f1SDimitry Andric extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY
46fe6060f1SDimitry Andric     COMPILER_RT_WEAK;
47fe6060f1SDimitry Andric extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY
48fe6060f1SDimitry Andric     COMPILER_RT_WEAK;
4904eeddc0SDimitry Andric extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
5004eeddc0SDimitry Andric extern char PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
51fe6060f1SDimitry Andric extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
52fe6060f1SDimitry Andric extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
53fe6060f1SDimitry Andric extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
54fe6060f1SDimitry Andric extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
55fe6060f1SDimitry Andric extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const __llvm_profile_data *
580b57cec5SDimitry Andric __llvm_profile_begin_data(void) {
590b57cec5SDimitry Andric   return &PROF_DATA_START;
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const __llvm_profile_data *
620b57cec5SDimitry Andric __llvm_profile_end_data(void) {
630b57cec5SDimitry Andric   return &PROF_DATA_STOP;
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) {
660b57cec5SDimitry Andric   return &PROF_NAME_START;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) {
690b57cec5SDimitry Andric   return &PROF_NAME_STOP;
700b57cec5SDimitry Andric }
7104eeddc0SDimitry Andric COMPILER_RT_VISIBILITY char *__llvm_profile_begin_counters(void) {
720b57cec5SDimitry Andric   return &PROF_CNTS_START;
730b57cec5SDimitry Andric }
7404eeddc0SDimitry Andric COMPILER_RT_VISIBILITY char *__llvm_profile_end_counters(void) {
750b57cec5SDimitry Andric   return &PROF_CNTS_STOP;
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint32_t *__llvm_profile_begin_orderfile(void) {
780b57cec5SDimitry Andric   return &PROF_ORDERFILE_START;
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *
820b57cec5SDimitry Andric __llvm_profile_begin_vnodes(void) {
830b57cec5SDimitry Andric   return &PROF_VNODES_START;
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) {
860b57cec5SDimitry Andric   return &PROF_VNODES_STOP;
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START;
890b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP;
900b57cec5SDimitry Andric 
916e75b2fbSDimitry Andric #ifdef NT_GNU_BUILD_ID
92fe6060f1SDimitry Andric static size_t RoundUp(size_t size, size_t align) {
93fe6060f1SDimitry Andric   return (size + align - 1) & ~(align - 1);
94fe6060f1SDimitry Andric }
95fe6060f1SDimitry Andric 
96fe6060f1SDimitry Andric /*
97fe6060f1SDimitry Andric  * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID
98fe6060f1SDimitry Andric  * that contains build id. If build id exists, write binary id.
99fe6060f1SDimitry Andric  *
100fe6060f1SDimitry Andric  * Each note in notes section starts with a struct which includes
101fe6060f1SDimitry Andric  * n_namesz, n_descsz, and n_type members. It is followed by the name
102fe6060f1SDimitry Andric  * (whose length is defined in n_namesz) and then by the descriptor
103fe6060f1SDimitry Andric  * (whose length is defined in n_descsz).
104fe6060f1SDimitry Andric  *
105fe6060f1SDimitry Andric  * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned
106fe6060f1SDimitry Andric  * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.
107fe6060f1SDimitry Andric  */
10869ade1e0SDimitry Andric static int WriteBinaryIdForNote(ProfDataWriter *Writer,
10969ade1e0SDimitry Andric                                 const ElfW(Nhdr) * Note) {
110fe6060f1SDimitry Andric   int BinaryIdSize = 0;
111fe6060f1SDimitry Andric   const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));
112fe6060f1SDimitry Andric   if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&
113fe6060f1SDimitry Andric       memcmp(NoteName, "GNU\0", 4) == 0) {
114fe6060f1SDimitry Andric     uint64_t BinaryIdLen = Note->n_descsz;
115fe6060f1SDimitry Andric     const uint8_t *BinaryIdData =
116fe6060f1SDimitry Andric         (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));
117349cc55cSDimitry Andric     uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen);
118*06c3fb27SDimitry Andric     if (Writer != NULL &&
119*06c3fb27SDimitry Andric         lprofWriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData,
120349cc55cSDimitry Andric                               BinaryIdPadding) == -1)
121fe6060f1SDimitry Andric       return -1;
122fe6060f1SDimitry Andric 
123349cc55cSDimitry Andric     BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding;
124fe6060f1SDimitry Andric   }
125fe6060f1SDimitry Andric 
126fe6060f1SDimitry Andric   return BinaryIdSize;
127fe6060f1SDimitry Andric }
128fe6060f1SDimitry Andric 
129fe6060f1SDimitry Andric /*
130fe6060f1SDimitry Andric  * Helper function that iterates through notes section and find build ids.
131fe6060f1SDimitry Andric  * If writer is given, write binary ids into profiles.
132fe6060f1SDimitry Andric  * If an error happens while writing, return -1.
133fe6060f1SDimitry Andric  */
13469ade1e0SDimitry Andric static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,
135fe6060f1SDimitry Andric                           const ElfW(Nhdr) * NotesEnd) {
13604eeddc0SDimitry Andric   int BinaryIdsSize = 0;
137fe6060f1SDimitry Andric   while (Note < NotesEnd) {
13804eeddc0SDimitry Andric     int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note);
13904eeddc0SDimitry Andric     if (OneBinaryIdSize == -1)
140fe6060f1SDimitry Andric       return -1;
14104eeddc0SDimitry Andric     BinaryIdsSize += OneBinaryIdSize;
142fe6060f1SDimitry Andric 
143fe6060f1SDimitry Andric     /* Calculate the offset of the next note in notes section. */
144fe6060f1SDimitry Andric     size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +
145fe6060f1SDimitry Andric                         RoundUp(Note->n_descsz, 4);
146fe6060f1SDimitry Andric     Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);
147fe6060f1SDimitry Andric   }
148fe6060f1SDimitry Andric 
14904eeddc0SDimitry Andric   return BinaryIdsSize;
150fe6060f1SDimitry Andric }
151fe6060f1SDimitry Andric 
152fe6060f1SDimitry Andric /*
153fe6060f1SDimitry Andric  * Write binary ids into profiles if writer is given.
154fe6060f1SDimitry Andric  * Return the total size of binary ids.
155fe6060f1SDimitry Andric  * If an error happens while writing, return -1.
156fe6060f1SDimitry Andric  */
157fe6060f1SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
158fe6060f1SDimitry Andric   extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));
159fe6060f1SDimitry Andric   const ElfW(Ehdr) *ElfHeader = &__ehdr_start;
160fe6060f1SDimitry Andric   const ElfW(Phdr) *ProgramHeader =
161fe6060f1SDimitry Andric       (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);
162fe6060f1SDimitry Andric 
16304eeddc0SDimitry Andric   int TotalBinaryIdsSize = 0;
164fe6060f1SDimitry Andric   uint32_t I;
165fe6060f1SDimitry Andric   /* Iterate through entries in the program header. */
166fe6060f1SDimitry Andric   for (I = 0; I < ElfHeader->e_phnum; I++) {
16704eeddc0SDimitry Andric     /* Look for the notes segment in program header entries. */
168fe6060f1SDimitry Andric     if (ProgramHeader[I].p_type != PT_NOTE)
169fe6060f1SDimitry Andric       continue;
170fe6060f1SDimitry Andric 
17104eeddc0SDimitry Andric     /* There can be multiple notes segment, and examine each of them. */
17204eeddc0SDimitry Andric     const ElfW(Nhdr) * Note;
17304eeddc0SDimitry Andric     const ElfW(Nhdr) * NotesEnd;
17404eeddc0SDimitry Andric     /*
17504eeddc0SDimitry Andric      * When examining notes in file, use p_offset, which is the offset within
17604eeddc0SDimitry Andric      * the elf file, to find the start of notes.
17704eeddc0SDimitry Andric      */
17804eeddc0SDimitry Andric     if (ProgramHeader[I].p_memsz == 0 ||
17904eeddc0SDimitry Andric         ProgramHeader[I].p_memsz == ProgramHeader[I].p_filesz) {
18004eeddc0SDimitry Andric       Note = (const ElfW(Nhdr) *)((uintptr_t)ElfHeader +
18104eeddc0SDimitry Andric                                   ProgramHeader[I].p_offset);
18204eeddc0SDimitry Andric       NotesEnd = (const ElfW(Nhdr) *)((const char *)(Note) +
18304eeddc0SDimitry Andric                                       ProgramHeader[I].p_filesz);
18404eeddc0SDimitry Andric     } else {
18504eeddc0SDimitry Andric       /*
18604eeddc0SDimitry Andric        * When examining notes in memory, use p_vaddr, which is the address of
18704eeddc0SDimitry Andric        * section after loaded to memory, to find the start of notes.
18804eeddc0SDimitry Andric        */
18904eeddc0SDimitry Andric       Note =
19004eeddc0SDimitry Andric           (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_vaddr);
19104eeddc0SDimitry Andric       NotesEnd =
19204eeddc0SDimitry Andric           (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz);
193fe6060f1SDimitry Andric     }
194fe6060f1SDimitry Andric 
19504eeddc0SDimitry Andric     int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd);
19604eeddc0SDimitry Andric     if (TotalBinaryIdsSize == -1)
19704eeddc0SDimitry Andric       return -1;
19804eeddc0SDimitry Andric 
19904eeddc0SDimitry Andric     TotalBinaryIdsSize += BinaryIdsSize;
20004eeddc0SDimitry Andric   }
20104eeddc0SDimitry Andric 
20204eeddc0SDimitry Andric   return TotalBinaryIdsSize;
203fe6060f1SDimitry Andric }
204*06c3fb27SDimitry Andric #elif !defined(_AIX) /* !NT_GNU_BUILD_ID */
2056e75b2fbSDimitry Andric /*
2066e75b2fbSDimitry Andric  * Fallback implementation for targets that don't support the GNU
2076e75b2fbSDimitry Andric  * extensions NT_GNU_BUILD_ID and __ehdr_start.
2086e75b2fbSDimitry Andric  */
2096e75b2fbSDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
2106e75b2fbSDimitry Andric   return 0;
2116e75b2fbSDimitry Andric }
2126e75b2fbSDimitry Andric #endif
213fe6060f1SDimitry Andric 
2140b57cec5SDimitry Andric #endif
215