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 206e75b2fbSDimitry Andric #if defined(__FreeBSD__) && !defined(ElfW) 216e75b2fbSDimitry Andric /* 226e75b2fbSDimitry Andric * FreeBSD's elf.h and link.h headers do not define the ElfW(type) macro yet. 236e75b2fbSDimitry Andric * If this is added to all supported FreeBSD versions in the future, this 246e75b2fbSDimitry Andric * compatibility macro can be removed. 256e75b2fbSDimitry Andric */ 266e75b2fbSDimitry Andric #define ElfW(type) __ElfN(type) 276e75b2fbSDimitry Andric #endif 286e75b2fbSDimitry 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; 46*04eeddc0SDimitry Andric extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 47*04eeddc0SDimitry Andric extern char 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 } 68*04eeddc0SDimitry Andric COMPILER_RT_VISIBILITY char *__llvm_profile_begin_counters(void) { 690b57cec5SDimitry Andric return &PROF_CNTS_START; 700b57cec5SDimitry Andric } 71*04eeddc0SDimitry Andric COMPILER_RT_VISIBILITY char *__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 886e75b2fbSDimitry 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 */ 9769ade1e0SDimitry Andric static int WriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen, 98349cc55cSDimitry Andric const uint8_t *BinaryIdData, 99349cc55cSDimitry Andric uint64_t BinaryIdPadding) { 100fe6060f1SDimitry Andric ProfDataIOVec BinaryIdIOVec[] = { 101fe6060f1SDimitry Andric {&BinaryIdLen, sizeof(uint64_t), 1, 0}, 102349cc55cSDimitry Andric {BinaryIdData, sizeof(uint8_t), BinaryIdLen, 0}, 103349cc55cSDimitry Andric {NULL, sizeof(uint8_t), BinaryIdPadding, 1}, 104349cc55cSDimitry Andric }; 105fe6060f1SDimitry Andric if (Writer->Write(Writer, BinaryIdIOVec, 106fe6060f1SDimitry Andric sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec))) 107fe6060f1SDimitry Andric return -1; 108fe6060f1SDimitry Andric 109fe6060f1SDimitry Andric /* Successfully wrote binary id, report success. */ 110fe6060f1SDimitry Andric return 0; 111fe6060f1SDimitry Andric } 112fe6060f1SDimitry Andric 113fe6060f1SDimitry Andric /* 114fe6060f1SDimitry Andric * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID 115fe6060f1SDimitry Andric * that contains build id. If build id exists, write binary id. 116fe6060f1SDimitry Andric * 117fe6060f1SDimitry Andric * Each note in notes section starts with a struct which includes 118fe6060f1SDimitry Andric * n_namesz, n_descsz, and n_type members. It is followed by the name 119fe6060f1SDimitry Andric * (whose length is defined in n_namesz) and then by the descriptor 120fe6060f1SDimitry Andric * (whose length is defined in n_descsz). 121fe6060f1SDimitry Andric * 122fe6060f1SDimitry Andric * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned 123fe6060f1SDimitry Andric * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes. 124fe6060f1SDimitry Andric */ 12569ade1e0SDimitry Andric static int WriteBinaryIdForNote(ProfDataWriter *Writer, 12669ade1e0SDimitry Andric const ElfW(Nhdr) * Note) { 127fe6060f1SDimitry Andric int BinaryIdSize = 0; 128fe6060f1SDimitry Andric const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr)); 129fe6060f1SDimitry Andric if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 && 130fe6060f1SDimitry Andric memcmp(NoteName, "GNU\0", 4) == 0) { 131fe6060f1SDimitry Andric uint64_t BinaryIdLen = Note->n_descsz; 132fe6060f1SDimitry Andric const uint8_t *BinaryIdData = 133fe6060f1SDimitry Andric (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4)); 134349cc55cSDimitry Andric uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen); 135349cc55cSDimitry Andric if (Writer != NULL && WriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData, 136349cc55cSDimitry Andric BinaryIdPadding) == -1) 137fe6060f1SDimitry Andric return -1; 138fe6060f1SDimitry Andric 139349cc55cSDimitry Andric BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding; 140fe6060f1SDimitry Andric } 141fe6060f1SDimitry Andric 142fe6060f1SDimitry Andric return BinaryIdSize; 143fe6060f1SDimitry Andric } 144fe6060f1SDimitry Andric 145fe6060f1SDimitry Andric /* 146fe6060f1SDimitry Andric * Helper function that iterates through notes section and find build ids. 147fe6060f1SDimitry Andric * If writer is given, write binary ids into profiles. 148fe6060f1SDimitry Andric * If an error happens while writing, return -1. 149fe6060f1SDimitry Andric */ 15069ade1e0SDimitry Andric static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note, 151fe6060f1SDimitry Andric const ElfW(Nhdr) * NotesEnd) { 152*04eeddc0SDimitry Andric int BinaryIdsSize = 0; 153fe6060f1SDimitry Andric while (Note < NotesEnd) { 154*04eeddc0SDimitry Andric int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note); 155*04eeddc0SDimitry Andric if (OneBinaryIdSize == -1) 156fe6060f1SDimitry Andric return -1; 157*04eeddc0SDimitry Andric BinaryIdsSize += OneBinaryIdSize; 158fe6060f1SDimitry Andric 159fe6060f1SDimitry Andric /* Calculate the offset of the next note in notes section. */ 160fe6060f1SDimitry Andric size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) + 161fe6060f1SDimitry Andric RoundUp(Note->n_descsz, 4); 162fe6060f1SDimitry Andric Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset); 163fe6060f1SDimitry Andric } 164fe6060f1SDimitry Andric 165*04eeddc0SDimitry Andric return BinaryIdsSize; 166fe6060f1SDimitry Andric } 167fe6060f1SDimitry Andric 168fe6060f1SDimitry Andric /* 169fe6060f1SDimitry Andric * Write binary ids into profiles if writer is given. 170fe6060f1SDimitry Andric * Return the total size of binary ids. 171fe6060f1SDimitry Andric * If an error happens while writing, return -1. 172fe6060f1SDimitry Andric */ 173fe6060f1SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) { 174fe6060f1SDimitry Andric extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden"))); 175fe6060f1SDimitry Andric const ElfW(Ehdr) *ElfHeader = &__ehdr_start; 176fe6060f1SDimitry Andric const ElfW(Phdr) *ProgramHeader = 177fe6060f1SDimitry Andric (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff); 178fe6060f1SDimitry Andric 179*04eeddc0SDimitry Andric int TotalBinaryIdsSize = 0; 180fe6060f1SDimitry Andric uint32_t I; 181fe6060f1SDimitry Andric /* Iterate through entries in the program header. */ 182fe6060f1SDimitry Andric for (I = 0; I < ElfHeader->e_phnum; I++) { 183*04eeddc0SDimitry Andric /* Look for the notes segment in program header entries. */ 184fe6060f1SDimitry Andric if (ProgramHeader[I].p_type != PT_NOTE) 185fe6060f1SDimitry Andric continue; 186fe6060f1SDimitry Andric 187*04eeddc0SDimitry Andric /* There can be multiple notes segment, and examine each of them. */ 188*04eeddc0SDimitry Andric const ElfW(Nhdr) * Note; 189*04eeddc0SDimitry Andric const ElfW(Nhdr) * NotesEnd; 190*04eeddc0SDimitry Andric /* 191*04eeddc0SDimitry Andric * When examining notes in file, use p_offset, which is the offset within 192*04eeddc0SDimitry Andric * the elf file, to find the start of notes. 193*04eeddc0SDimitry Andric */ 194*04eeddc0SDimitry Andric if (ProgramHeader[I].p_memsz == 0 || 195*04eeddc0SDimitry Andric ProgramHeader[I].p_memsz == ProgramHeader[I].p_filesz) { 196*04eeddc0SDimitry Andric Note = (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + 197*04eeddc0SDimitry Andric ProgramHeader[I].p_offset); 198*04eeddc0SDimitry Andric NotesEnd = (const ElfW(Nhdr) *)((const char *)(Note) + 199*04eeddc0SDimitry Andric ProgramHeader[I].p_filesz); 200*04eeddc0SDimitry Andric } else { 201*04eeddc0SDimitry Andric /* 202*04eeddc0SDimitry Andric * When examining notes in memory, use p_vaddr, which is the address of 203*04eeddc0SDimitry Andric * section after loaded to memory, to find the start of notes. 204*04eeddc0SDimitry Andric */ 205*04eeddc0SDimitry Andric Note = 206*04eeddc0SDimitry Andric (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_vaddr); 207*04eeddc0SDimitry Andric NotesEnd = 208*04eeddc0SDimitry Andric (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz); 209fe6060f1SDimitry Andric } 210fe6060f1SDimitry Andric 211*04eeddc0SDimitry Andric int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd); 212*04eeddc0SDimitry Andric if (TotalBinaryIdsSize == -1) 213*04eeddc0SDimitry Andric return -1; 214*04eeddc0SDimitry Andric 215*04eeddc0SDimitry Andric TotalBinaryIdsSize += BinaryIdsSize; 216*04eeddc0SDimitry Andric } 217*04eeddc0SDimitry Andric 218*04eeddc0SDimitry Andric return TotalBinaryIdsSize; 219fe6060f1SDimitry Andric } 2206e75b2fbSDimitry Andric #else /* !NT_GNU_BUILD_ID */ 2216e75b2fbSDimitry Andric /* 2226e75b2fbSDimitry Andric * Fallback implementation for targets that don't support the GNU 2236e75b2fbSDimitry Andric * extensions NT_GNU_BUILD_ID and __ehdr_start. 2246e75b2fbSDimitry Andric */ 2256e75b2fbSDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) { 2266e75b2fbSDimitry Andric return 0; 2276e75b2fbSDimitry Andric } 2286e75b2fbSDimitry Andric #endif 229fe6060f1SDimitry Andric 2300b57cec5SDimitry Andric #endif 231