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__) || \ 100b57cec5SDimitry Andric (defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) 110b57cec5SDimitry Andric 12fe6060f1SDimitry Andric #include <elf.h> 13fe6060f1SDimitry Andric #include <link.h> 140b57cec5SDimitry Andric #include <stdlib.h> 15fe6060f1SDimitry Andric #include <string.h> 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "InstrProfiling.h" 18fe6060f1SDimitry Andric #include "InstrProfilingInternal.h" 190b57cec5SDimitry Andric 20*6e75b2fbSDimitry Andric #if defined(__FreeBSD__) && !defined(ElfW) 21*6e75b2fbSDimitry Andric /* 22*6e75b2fbSDimitry Andric * FreeBSD's elf.h and link.h headers do not define the ElfW(type) macro yet. 23*6e75b2fbSDimitry Andric * If this is added to all supported FreeBSD versions in the future, this 24*6e75b2fbSDimitry Andric * compatibility macro can be removed. 25*6e75b2fbSDimitry Andric */ 26*6e75b2fbSDimitry Andric #define ElfW(type) __ElfN(type) 27*6e75b2fbSDimitry Andric #endif 28*6e75b2fbSDimitry Andric 290b57cec5SDimitry Andric #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON) 300b57cec5SDimitry Andric #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON) 310b57cec5SDimitry Andric #define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON) 320b57cec5SDimitry Andric #define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON) 330b57cec5SDimitry Andric #define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON) 340b57cec5SDimitry Andric #define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON) 350b57cec5SDimitry Andric #define PROF_ORDERFILE_START INSTR_PROF_SECT_START(INSTR_PROF_ORDERFILE_COMMON) 360b57cec5SDimitry Andric #define PROF_VNODES_START INSTR_PROF_SECT_START(INSTR_PROF_VNODES_COMMON) 370b57cec5SDimitry Andric #define PROF_VNODES_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNODES_COMMON) 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric /* Declare section start and stop symbols for various sections 400b57cec5SDimitry Andric * generated by compiler instrumentation. 410b57cec5SDimitry Andric */ 42fe6060f1SDimitry Andric extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY 43fe6060f1SDimitry Andric COMPILER_RT_WEAK; 44fe6060f1SDimitry Andric extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY 45fe6060f1SDimitry Andric COMPILER_RT_WEAK; 46fe6060f1SDimitry Andric extern uint64_t PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 47fe6060f1SDimitry Andric extern uint64_t PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 48fe6060f1SDimitry Andric extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 49fe6060f1SDimitry Andric extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 50fe6060f1SDimitry Andric extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 51fe6060f1SDimitry Andric extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 52fe6060f1SDimitry Andric extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const __llvm_profile_data * 550b57cec5SDimitry Andric __llvm_profile_begin_data(void) { 560b57cec5SDimitry Andric return &PROF_DATA_START; 570b57cec5SDimitry Andric } 580b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const __llvm_profile_data * 590b57cec5SDimitry Andric __llvm_profile_end_data(void) { 600b57cec5SDimitry Andric return &PROF_DATA_STOP; 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) { 630b57cec5SDimitry Andric return &PROF_NAME_START; 640b57cec5SDimitry Andric } 650b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) { 660b57cec5SDimitry Andric return &PROF_NAME_STOP; 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint64_t *__llvm_profile_begin_counters(void) { 690b57cec5SDimitry Andric return &PROF_CNTS_START; 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint64_t *__llvm_profile_end_counters(void) { 720b57cec5SDimitry Andric return &PROF_CNTS_STOP; 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint32_t *__llvm_profile_begin_orderfile(void) { 750b57cec5SDimitry Andric return &PROF_ORDERFILE_START; 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode * 790b57cec5SDimitry Andric __llvm_profile_begin_vnodes(void) { 800b57cec5SDimitry Andric return &PROF_VNODES_START; 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) { 830b57cec5SDimitry Andric return &PROF_VNODES_STOP; 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START; 860b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP; 870b57cec5SDimitry Andric 88*6e75b2fbSDimitry Andric #ifdef NT_GNU_BUILD_ID 89fe6060f1SDimitry Andric static size_t RoundUp(size_t size, size_t align) { 90fe6060f1SDimitry Andric return (size + align - 1) & ~(align - 1); 91fe6060f1SDimitry Andric } 92fe6060f1SDimitry Andric 93fe6060f1SDimitry Andric /* 94fe6060f1SDimitry Andric * Write binary id length and then its data, because binary id does not 95fe6060f1SDimitry Andric * have a fixed length. 96fe6060f1SDimitry Andric */ 97fe6060f1SDimitry Andric int WriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen, 98fe6060f1SDimitry Andric const uint8_t *BinaryIdData) { 99fe6060f1SDimitry Andric ProfDataIOVec BinaryIdIOVec[] = { 100fe6060f1SDimitry Andric {&BinaryIdLen, sizeof(uint64_t), 1, 0}, 101fe6060f1SDimitry Andric {BinaryIdData, sizeof(uint8_t), BinaryIdLen, 0}}; 102fe6060f1SDimitry Andric if (Writer->Write(Writer, BinaryIdIOVec, 103fe6060f1SDimitry Andric sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec))) 104fe6060f1SDimitry Andric return -1; 105fe6060f1SDimitry Andric 106fe6060f1SDimitry Andric /* Successfully wrote binary id, report success. */ 107fe6060f1SDimitry Andric return 0; 108fe6060f1SDimitry Andric } 109fe6060f1SDimitry Andric 110fe6060f1SDimitry Andric /* 111fe6060f1SDimitry Andric * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID 112fe6060f1SDimitry Andric * that contains build id. If build id exists, write binary id. 113fe6060f1SDimitry Andric * 114fe6060f1SDimitry Andric * Each note in notes section starts with a struct which includes 115fe6060f1SDimitry Andric * n_namesz, n_descsz, and n_type members. It is followed by the name 116fe6060f1SDimitry Andric * (whose length is defined in n_namesz) and then by the descriptor 117fe6060f1SDimitry Andric * (whose length is defined in n_descsz). 118fe6060f1SDimitry Andric * 119fe6060f1SDimitry Andric * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned 120fe6060f1SDimitry Andric * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes. 121fe6060f1SDimitry Andric */ 122fe6060f1SDimitry Andric int WriteBinaryIdForNote(ProfDataWriter *Writer, const ElfW(Nhdr) * Note) { 123fe6060f1SDimitry Andric int BinaryIdSize = 0; 124fe6060f1SDimitry Andric 125fe6060f1SDimitry Andric const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr)); 126fe6060f1SDimitry Andric if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 && 127fe6060f1SDimitry Andric memcmp(NoteName, "GNU\0", 4) == 0) { 128fe6060f1SDimitry Andric 129fe6060f1SDimitry Andric uint64_t BinaryIdLen = Note->n_descsz; 130fe6060f1SDimitry Andric const uint8_t *BinaryIdData = 131fe6060f1SDimitry Andric (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4)); 132fe6060f1SDimitry Andric if (Writer != NULL && 133fe6060f1SDimitry Andric WriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData) == -1) 134fe6060f1SDimitry Andric return -1; 135fe6060f1SDimitry Andric 136fe6060f1SDimitry Andric BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen; 137fe6060f1SDimitry Andric } 138fe6060f1SDimitry Andric 139fe6060f1SDimitry Andric return BinaryIdSize; 140fe6060f1SDimitry Andric } 141fe6060f1SDimitry Andric 142fe6060f1SDimitry Andric /* 143fe6060f1SDimitry Andric * Helper function that iterates through notes section and find build ids. 144fe6060f1SDimitry Andric * If writer is given, write binary ids into profiles. 145fe6060f1SDimitry Andric * If an error happens while writing, return -1. 146fe6060f1SDimitry Andric */ 147fe6060f1SDimitry Andric int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note, 148fe6060f1SDimitry Andric const ElfW(Nhdr) * NotesEnd) { 149fe6060f1SDimitry Andric int TotalBinaryIdsSize = 0; 150fe6060f1SDimitry Andric while (Note < NotesEnd) { 151fe6060f1SDimitry Andric int Result = WriteBinaryIdForNote(Writer, Note); 152fe6060f1SDimitry Andric if (Result == -1) 153fe6060f1SDimitry Andric return -1; 154fe6060f1SDimitry Andric TotalBinaryIdsSize += Result; 155fe6060f1SDimitry Andric 156fe6060f1SDimitry Andric /* Calculate the offset of the next note in notes section. */ 157fe6060f1SDimitry Andric size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) + 158fe6060f1SDimitry Andric RoundUp(Note->n_descsz, 4); 159fe6060f1SDimitry Andric Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset); 160fe6060f1SDimitry Andric } 161fe6060f1SDimitry Andric 162fe6060f1SDimitry Andric return TotalBinaryIdsSize; 163fe6060f1SDimitry Andric } 164fe6060f1SDimitry Andric 165fe6060f1SDimitry Andric /* 166fe6060f1SDimitry Andric * Write binary ids into profiles if writer is given. 167fe6060f1SDimitry Andric * Return the total size of binary ids. 168fe6060f1SDimitry Andric * If an error happens while writing, return -1. 169fe6060f1SDimitry Andric */ 170fe6060f1SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) { 171fe6060f1SDimitry Andric extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden"))); 172fe6060f1SDimitry Andric const ElfW(Ehdr) *ElfHeader = &__ehdr_start; 173fe6060f1SDimitry Andric const ElfW(Phdr) *ProgramHeader = 174fe6060f1SDimitry Andric (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff); 175fe6060f1SDimitry Andric 176fe6060f1SDimitry Andric uint32_t I; 177fe6060f1SDimitry Andric /* Iterate through entries in the program header. */ 178fe6060f1SDimitry Andric for (I = 0; I < ElfHeader->e_phnum; I++) { 179fe6060f1SDimitry Andric /* Look for the notes section in program header entries. */ 180fe6060f1SDimitry Andric if (ProgramHeader[I].p_type != PT_NOTE) 181fe6060f1SDimitry Andric continue; 182fe6060f1SDimitry Andric 183fe6060f1SDimitry Andric const ElfW(Nhdr) *Note = 184fe6060f1SDimitry Andric (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_offset); 185fe6060f1SDimitry Andric const ElfW(Nhdr) *NotesEnd = 186fe6060f1SDimitry Andric (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_filesz); 187fe6060f1SDimitry Andric return WriteBinaryIds(Writer, Note, NotesEnd); 188fe6060f1SDimitry Andric } 189fe6060f1SDimitry Andric 190fe6060f1SDimitry Andric return 0; 191fe6060f1SDimitry Andric } 192*6e75b2fbSDimitry Andric #else /* !NT_GNU_BUILD_ID */ 193*6e75b2fbSDimitry Andric /* 194*6e75b2fbSDimitry Andric * Fallback implementation for targets that don't support the GNU 195*6e75b2fbSDimitry Andric * extensions NT_GNU_BUILD_ID and __ehdr_start. 196*6e75b2fbSDimitry Andric */ 197*6e75b2fbSDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) { 198*6e75b2fbSDimitry Andric return 0; 199*6e75b2fbSDimitry Andric } 200*6e75b2fbSDimitry Andric #endif 201fe6060f1SDimitry Andric 2020b57cec5SDimitry Andric #endif 203