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 230b57cec5SDimitry Andric #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON) 240b57cec5SDimitry Andric #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON) 250b57cec5SDimitry Andric #define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON) 260b57cec5SDimitry Andric #define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON) 27*0fca6ea1SDimitry Andric #define PROF_VNAME_START INSTR_PROF_SECT_START(INSTR_PROF_VNAME_COMMON) 28*0fca6ea1SDimitry Andric #define PROF_VNAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNAME_COMMON) 290b57cec5SDimitry Andric #define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON) 300b57cec5SDimitry Andric #define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON) 31*0fca6ea1SDimitry Andric #define PROF_VTABLE_START INSTR_PROF_SECT_START(INSTR_PROF_VTAB_COMMON) 32*0fca6ea1SDimitry Andric #define PROF_VTABLE_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VTAB_COMMON) 335f757f3fSDimitry Andric #define PROF_BITS_START INSTR_PROF_SECT_START(INSTR_PROF_BITS_COMMON) 345f757f3fSDimitry Andric #define PROF_BITS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_BITS_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; 4604eeddc0SDimitry Andric extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 4704eeddc0SDimitry Andric extern char PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 48*0fca6ea1SDimitry Andric extern VTableProfData PROF_VTABLE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 49*0fca6ea1SDimitry Andric extern VTableProfData PROF_VTABLE_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 50*0fca6ea1SDimitry Andric extern char PROF_VNAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 51*0fca6ea1SDimitry Andric extern char PROF_VNAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 525f757f3fSDimitry Andric extern char PROF_BITS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 535f757f3fSDimitry Andric extern char PROF_BITS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 54fe6060f1SDimitry Andric extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 55fe6060f1SDimitry Andric extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 56fe6060f1SDimitry Andric extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 57fe6060f1SDimitry Andric extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 58fe6060f1SDimitry Andric extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK; 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const __llvm_profile_data * 610b57cec5SDimitry Andric __llvm_profile_begin_data(void) { 620b57cec5SDimitry Andric return &PROF_DATA_START; 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const __llvm_profile_data * 650b57cec5SDimitry Andric __llvm_profile_end_data(void) { 660b57cec5SDimitry Andric return &PROF_DATA_STOP; 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) { 690b57cec5SDimitry Andric return &PROF_NAME_START; 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) { 720b57cec5SDimitry Andric return &PROF_NAME_STOP; 730b57cec5SDimitry Andric } 74*0fca6ea1SDimitry Andric COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_vtabnames(void) { 75*0fca6ea1SDimitry Andric return &PROF_VNAME_START; 76*0fca6ea1SDimitry Andric } 77*0fca6ea1SDimitry Andric COMPILER_RT_VISIBILITY const char *__llvm_profile_end_vtabnames(void) { 78*0fca6ea1SDimitry Andric return &PROF_VNAME_STOP; 79*0fca6ea1SDimitry Andric } 80*0fca6ea1SDimitry Andric COMPILER_RT_VISIBILITY const VTableProfData * 81*0fca6ea1SDimitry Andric __llvm_profile_begin_vtables(void) { 82*0fca6ea1SDimitry Andric return &PROF_VTABLE_START; 83*0fca6ea1SDimitry Andric } 84*0fca6ea1SDimitry Andric COMPILER_RT_VISIBILITY const VTableProfData *__llvm_profile_end_vtables(void) { 85*0fca6ea1SDimitry Andric return &PROF_VTABLE_STOP; 86*0fca6ea1SDimitry Andric } 8704eeddc0SDimitry Andric COMPILER_RT_VISIBILITY char *__llvm_profile_begin_counters(void) { 880b57cec5SDimitry Andric return &PROF_CNTS_START; 890b57cec5SDimitry Andric } 9004eeddc0SDimitry Andric COMPILER_RT_VISIBILITY char *__llvm_profile_end_counters(void) { 910b57cec5SDimitry Andric return &PROF_CNTS_STOP; 920b57cec5SDimitry Andric } 935f757f3fSDimitry Andric COMPILER_RT_VISIBILITY char *__llvm_profile_begin_bitmap(void) { 945f757f3fSDimitry Andric return &PROF_BITS_START; 955f757f3fSDimitry Andric } 965f757f3fSDimitry Andric COMPILER_RT_VISIBILITY char *__llvm_profile_end_bitmap(void) { 975f757f3fSDimitry Andric return &PROF_BITS_STOP; 985f757f3fSDimitry Andric } 990b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint32_t *__llvm_profile_begin_orderfile(void) { 1000b57cec5SDimitry Andric return &PROF_ORDERFILE_START; 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode * 1040b57cec5SDimitry Andric __llvm_profile_begin_vnodes(void) { 1050b57cec5SDimitry Andric return &PROF_VNODES_START; 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) { 1080b57cec5SDimitry Andric return &PROF_VNODES_STOP; 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START; 1110b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP; 1120b57cec5SDimitry Andric 1136e75b2fbSDimitry Andric #ifdef NT_GNU_BUILD_ID 114fe6060f1SDimitry Andric static size_t RoundUp(size_t size, size_t align) { 115fe6060f1SDimitry Andric return (size + align - 1) & ~(align - 1); 116fe6060f1SDimitry Andric } 117fe6060f1SDimitry Andric 118fe6060f1SDimitry Andric /* 119fe6060f1SDimitry Andric * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID 120fe6060f1SDimitry Andric * that contains build id. If build id exists, write binary id. 121fe6060f1SDimitry Andric * 122fe6060f1SDimitry Andric * Each note in notes section starts with a struct which includes 123fe6060f1SDimitry Andric * n_namesz, n_descsz, and n_type members. It is followed by the name 124fe6060f1SDimitry Andric * (whose length is defined in n_namesz) and then by the descriptor 125fe6060f1SDimitry Andric * (whose length is defined in n_descsz). 126fe6060f1SDimitry Andric * 127fe6060f1SDimitry Andric * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned 128fe6060f1SDimitry Andric * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes. 129fe6060f1SDimitry Andric */ 13069ade1e0SDimitry Andric static int WriteBinaryIdForNote(ProfDataWriter *Writer, 13169ade1e0SDimitry Andric const ElfW(Nhdr) * Note) { 132fe6060f1SDimitry Andric int BinaryIdSize = 0; 133fe6060f1SDimitry Andric const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr)); 134fe6060f1SDimitry Andric if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 && 135fe6060f1SDimitry Andric memcmp(NoteName, "GNU\0", 4) == 0) { 136fe6060f1SDimitry Andric uint64_t BinaryIdLen = Note->n_descsz; 137fe6060f1SDimitry Andric const uint8_t *BinaryIdData = 138fe6060f1SDimitry Andric (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4)); 139349cc55cSDimitry Andric uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen); 14006c3fb27SDimitry Andric if (Writer != NULL && 14106c3fb27SDimitry Andric lprofWriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData, 142349cc55cSDimitry Andric BinaryIdPadding) == -1) 143fe6060f1SDimitry Andric return -1; 144fe6060f1SDimitry Andric 145349cc55cSDimitry Andric BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding; 146fe6060f1SDimitry Andric } 147fe6060f1SDimitry Andric 148fe6060f1SDimitry Andric return BinaryIdSize; 149fe6060f1SDimitry Andric } 150fe6060f1SDimitry Andric 151fe6060f1SDimitry Andric /* 152fe6060f1SDimitry Andric * Helper function that iterates through notes section and find build ids. 153fe6060f1SDimitry Andric * If writer is given, write binary ids into profiles. 154fe6060f1SDimitry Andric * If an error happens while writing, return -1. 155fe6060f1SDimitry Andric */ 15669ade1e0SDimitry Andric static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note, 157fe6060f1SDimitry Andric const ElfW(Nhdr) * NotesEnd) { 15804eeddc0SDimitry Andric int BinaryIdsSize = 0; 159fe6060f1SDimitry Andric while (Note < NotesEnd) { 16004eeddc0SDimitry Andric int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note); 16104eeddc0SDimitry Andric if (OneBinaryIdSize == -1) 162fe6060f1SDimitry Andric return -1; 16304eeddc0SDimitry Andric BinaryIdsSize += OneBinaryIdSize; 164fe6060f1SDimitry Andric 165fe6060f1SDimitry Andric /* Calculate the offset of the next note in notes section. */ 166fe6060f1SDimitry Andric size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) + 167fe6060f1SDimitry Andric RoundUp(Note->n_descsz, 4); 168fe6060f1SDimitry Andric Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset); 169fe6060f1SDimitry Andric } 170fe6060f1SDimitry Andric 17104eeddc0SDimitry Andric return BinaryIdsSize; 172fe6060f1SDimitry Andric } 173fe6060f1SDimitry Andric 174fe6060f1SDimitry Andric /* 175fe6060f1SDimitry Andric * Write binary ids into profiles if writer is given. 176fe6060f1SDimitry Andric * Return the total size of binary ids. 177fe6060f1SDimitry Andric * If an error happens while writing, return -1. 178fe6060f1SDimitry Andric */ 179fe6060f1SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) { 180fe6060f1SDimitry Andric extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden"))); 181fe6060f1SDimitry Andric const ElfW(Ehdr) *ElfHeader = &__ehdr_start; 182fe6060f1SDimitry Andric const ElfW(Phdr) *ProgramHeader = 183fe6060f1SDimitry Andric (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff); 184fe6060f1SDimitry Andric 18504eeddc0SDimitry Andric int TotalBinaryIdsSize = 0; 186fe6060f1SDimitry Andric uint32_t I; 187fe6060f1SDimitry Andric /* Iterate through entries in the program header. */ 188fe6060f1SDimitry Andric for (I = 0; I < ElfHeader->e_phnum; I++) { 18904eeddc0SDimitry Andric /* Look for the notes segment in program header entries. */ 190fe6060f1SDimitry Andric if (ProgramHeader[I].p_type != PT_NOTE) 191fe6060f1SDimitry Andric continue; 192fe6060f1SDimitry Andric 19304eeddc0SDimitry Andric /* There can be multiple notes segment, and examine each of them. */ 19404eeddc0SDimitry Andric const ElfW(Nhdr) * Note; 19504eeddc0SDimitry Andric const ElfW(Nhdr) * NotesEnd; 19604eeddc0SDimitry Andric /* 19704eeddc0SDimitry Andric * When examining notes in file, use p_offset, which is the offset within 19804eeddc0SDimitry Andric * the elf file, to find the start of notes. 19904eeddc0SDimitry Andric */ 20004eeddc0SDimitry Andric if (ProgramHeader[I].p_memsz == 0 || 20104eeddc0SDimitry Andric ProgramHeader[I].p_memsz == ProgramHeader[I].p_filesz) { 20204eeddc0SDimitry Andric Note = (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + 20304eeddc0SDimitry Andric ProgramHeader[I].p_offset); 20404eeddc0SDimitry Andric NotesEnd = (const ElfW(Nhdr) *)((const char *)(Note) + 20504eeddc0SDimitry Andric ProgramHeader[I].p_filesz); 20604eeddc0SDimitry Andric } else { 20704eeddc0SDimitry Andric /* 20804eeddc0SDimitry Andric * When examining notes in memory, use p_vaddr, which is the address of 20904eeddc0SDimitry Andric * section after loaded to memory, to find the start of notes. 21004eeddc0SDimitry Andric */ 21104eeddc0SDimitry Andric Note = 21204eeddc0SDimitry Andric (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_vaddr); 21304eeddc0SDimitry Andric NotesEnd = 21404eeddc0SDimitry Andric (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz); 215fe6060f1SDimitry Andric } 216fe6060f1SDimitry Andric 21704eeddc0SDimitry Andric int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd); 21804eeddc0SDimitry Andric if (TotalBinaryIdsSize == -1) 21904eeddc0SDimitry Andric return -1; 22004eeddc0SDimitry Andric 22104eeddc0SDimitry Andric TotalBinaryIdsSize += BinaryIdsSize; 22204eeddc0SDimitry Andric } 22304eeddc0SDimitry Andric 22404eeddc0SDimitry Andric return TotalBinaryIdsSize; 225fe6060f1SDimitry Andric } 22606c3fb27SDimitry Andric #elif !defined(_AIX) /* !NT_GNU_BUILD_ID */ 2276e75b2fbSDimitry Andric /* 2286e75b2fbSDimitry Andric * Fallback implementation for targets that don't support the GNU 2296e75b2fbSDimitry Andric * extensions NT_GNU_BUILD_ID and __ehdr_start. 2306e75b2fbSDimitry Andric */ 2316e75b2fbSDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) { 2326e75b2fbSDimitry Andric return 0; 2336e75b2fbSDimitry Andric } 2346e75b2fbSDimitry Andric #endif 235fe6060f1SDimitry Andric 2360b57cec5SDimitry Andric #endif 237