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__) || \ 10*81ad6265SDimitry Andric (defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) || \ 11*81ad6265SDimitry Andric defined(_AIX) 120b57cec5SDimitry Andric 13*81ad6265SDimitry Andric #if !defined(_AIX) 14fe6060f1SDimitry Andric #include <elf.h> 15fe6060f1SDimitry Andric #include <link.h> 16*81ad6265SDimitry 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 * Write binary id length and then its data, because binary id does not 98fe6060f1SDimitry Andric * have a fixed length. 99fe6060f1SDimitry Andric */ 10069ade1e0SDimitry Andric static int WriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen, 101349cc55cSDimitry Andric const uint8_t *BinaryIdData, 102349cc55cSDimitry Andric uint64_t BinaryIdPadding) { 103fe6060f1SDimitry Andric ProfDataIOVec BinaryIdIOVec[] = { 104fe6060f1SDimitry Andric {&BinaryIdLen, sizeof(uint64_t), 1, 0}, 105349cc55cSDimitry Andric {BinaryIdData, sizeof(uint8_t), BinaryIdLen, 0}, 106349cc55cSDimitry Andric {NULL, sizeof(uint8_t), BinaryIdPadding, 1}, 107349cc55cSDimitry Andric }; 108fe6060f1SDimitry Andric if (Writer->Write(Writer, BinaryIdIOVec, 109fe6060f1SDimitry Andric sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec))) 110fe6060f1SDimitry Andric return -1; 111fe6060f1SDimitry Andric 112fe6060f1SDimitry Andric /* Successfully wrote binary id, report success. */ 113fe6060f1SDimitry Andric return 0; 114fe6060f1SDimitry Andric } 115fe6060f1SDimitry Andric 116fe6060f1SDimitry Andric /* 117fe6060f1SDimitry Andric * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID 118fe6060f1SDimitry Andric * that contains build id. If build id exists, write binary id. 119fe6060f1SDimitry Andric * 120fe6060f1SDimitry Andric * Each note in notes section starts with a struct which includes 121fe6060f1SDimitry Andric * n_namesz, n_descsz, and n_type members. It is followed by the name 122fe6060f1SDimitry Andric * (whose length is defined in n_namesz) and then by the descriptor 123fe6060f1SDimitry Andric * (whose length is defined in n_descsz). 124fe6060f1SDimitry Andric * 125fe6060f1SDimitry Andric * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned 126fe6060f1SDimitry Andric * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes. 127fe6060f1SDimitry Andric */ 12869ade1e0SDimitry Andric static int WriteBinaryIdForNote(ProfDataWriter *Writer, 12969ade1e0SDimitry Andric const ElfW(Nhdr) * Note) { 130fe6060f1SDimitry Andric int BinaryIdSize = 0; 131fe6060f1SDimitry Andric const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr)); 132fe6060f1SDimitry Andric if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 && 133fe6060f1SDimitry Andric memcmp(NoteName, "GNU\0", 4) == 0) { 134fe6060f1SDimitry Andric uint64_t BinaryIdLen = Note->n_descsz; 135fe6060f1SDimitry Andric const uint8_t *BinaryIdData = 136fe6060f1SDimitry Andric (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4)); 137349cc55cSDimitry Andric uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen); 138349cc55cSDimitry Andric if (Writer != NULL && WriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData, 139349cc55cSDimitry Andric BinaryIdPadding) == -1) 140fe6060f1SDimitry Andric return -1; 141fe6060f1SDimitry Andric 142349cc55cSDimitry Andric BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding; 143fe6060f1SDimitry Andric } 144fe6060f1SDimitry Andric 145fe6060f1SDimitry Andric return BinaryIdSize; 146fe6060f1SDimitry Andric } 147fe6060f1SDimitry Andric 148fe6060f1SDimitry Andric /* 149fe6060f1SDimitry Andric * Helper function that iterates through notes section and find build ids. 150fe6060f1SDimitry Andric * If writer is given, write binary ids into profiles. 151fe6060f1SDimitry Andric * If an error happens while writing, return -1. 152fe6060f1SDimitry Andric */ 15369ade1e0SDimitry Andric static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note, 154fe6060f1SDimitry Andric const ElfW(Nhdr) * NotesEnd) { 15504eeddc0SDimitry Andric int BinaryIdsSize = 0; 156fe6060f1SDimitry Andric while (Note < NotesEnd) { 15704eeddc0SDimitry Andric int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note); 15804eeddc0SDimitry Andric if (OneBinaryIdSize == -1) 159fe6060f1SDimitry Andric return -1; 16004eeddc0SDimitry Andric BinaryIdsSize += OneBinaryIdSize; 161fe6060f1SDimitry Andric 162fe6060f1SDimitry Andric /* Calculate the offset of the next note in notes section. */ 163fe6060f1SDimitry Andric size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) + 164fe6060f1SDimitry Andric RoundUp(Note->n_descsz, 4); 165fe6060f1SDimitry Andric Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset); 166fe6060f1SDimitry Andric } 167fe6060f1SDimitry Andric 16804eeddc0SDimitry Andric return BinaryIdsSize; 169fe6060f1SDimitry Andric } 170fe6060f1SDimitry Andric 171fe6060f1SDimitry Andric /* 172fe6060f1SDimitry Andric * Write binary ids into profiles if writer is given. 173fe6060f1SDimitry Andric * Return the total size of binary ids. 174fe6060f1SDimitry Andric * If an error happens while writing, return -1. 175fe6060f1SDimitry Andric */ 176fe6060f1SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) { 177fe6060f1SDimitry Andric extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden"))); 178fe6060f1SDimitry Andric const ElfW(Ehdr) *ElfHeader = &__ehdr_start; 179fe6060f1SDimitry Andric const ElfW(Phdr) *ProgramHeader = 180fe6060f1SDimitry Andric (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff); 181fe6060f1SDimitry Andric 18204eeddc0SDimitry Andric int TotalBinaryIdsSize = 0; 183fe6060f1SDimitry Andric uint32_t I; 184fe6060f1SDimitry Andric /* Iterate through entries in the program header. */ 185fe6060f1SDimitry Andric for (I = 0; I < ElfHeader->e_phnum; I++) { 18604eeddc0SDimitry Andric /* Look for the notes segment in program header entries. */ 187fe6060f1SDimitry Andric if (ProgramHeader[I].p_type != PT_NOTE) 188fe6060f1SDimitry Andric continue; 189fe6060f1SDimitry Andric 19004eeddc0SDimitry Andric /* There can be multiple notes segment, and examine each of them. */ 19104eeddc0SDimitry Andric const ElfW(Nhdr) * Note; 19204eeddc0SDimitry Andric const ElfW(Nhdr) * NotesEnd; 19304eeddc0SDimitry Andric /* 19404eeddc0SDimitry Andric * When examining notes in file, use p_offset, which is the offset within 19504eeddc0SDimitry Andric * the elf file, to find the start of notes. 19604eeddc0SDimitry Andric */ 19704eeddc0SDimitry Andric if (ProgramHeader[I].p_memsz == 0 || 19804eeddc0SDimitry Andric ProgramHeader[I].p_memsz == ProgramHeader[I].p_filesz) { 19904eeddc0SDimitry Andric Note = (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + 20004eeddc0SDimitry Andric ProgramHeader[I].p_offset); 20104eeddc0SDimitry Andric NotesEnd = (const ElfW(Nhdr) *)((const char *)(Note) + 20204eeddc0SDimitry Andric ProgramHeader[I].p_filesz); 20304eeddc0SDimitry Andric } else { 20404eeddc0SDimitry Andric /* 20504eeddc0SDimitry Andric * When examining notes in memory, use p_vaddr, which is the address of 20604eeddc0SDimitry Andric * section after loaded to memory, to find the start of notes. 20704eeddc0SDimitry Andric */ 20804eeddc0SDimitry Andric Note = 20904eeddc0SDimitry Andric (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_vaddr); 21004eeddc0SDimitry Andric NotesEnd = 21104eeddc0SDimitry Andric (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz); 212fe6060f1SDimitry Andric } 213fe6060f1SDimitry Andric 21404eeddc0SDimitry Andric int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd); 21504eeddc0SDimitry Andric if (TotalBinaryIdsSize == -1) 21604eeddc0SDimitry Andric return -1; 21704eeddc0SDimitry Andric 21804eeddc0SDimitry Andric TotalBinaryIdsSize += BinaryIdsSize; 21904eeddc0SDimitry Andric } 22004eeddc0SDimitry Andric 22104eeddc0SDimitry Andric return TotalBinaryIdsSize; 222fe6060f1SDimitry Andric } 2236e75b2fbSDimitry Andric #else /* !NT_GNU_BUILD_ID */ 2246e75b2fbSDimitry Andric /* 2256e75b2fbSDimitry Andric * Fallback implementation for targets that don't support the GNU 2266e75b2fbSDimitry Andric * extensions NT_GNU_BUILD_ID and __ehdr_start. 2276e75b2fbSDimitry Andric */ 2286e75b2fbSDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) { 2296e75b2fbSDimitry Andric return 0; 2306e75b2fbSDimitry Andric } 2316e75b2fbSDimitry Andric #endif 232fe6060f1SDimitry Andric 233*81ad6265SDimitry Andric #if defined(_AIX) 234*81ad6265SDimitry Andric // Empty stubs to allow linking object files using the registration-based scheme 235*81ad6265SDimitry Andric COMPILER_RT_VISIBILITY 236*81ad6265SDimitry Andric void __llvm_profile_register_function(void *Data_) {} 237*81ad6265SDimitry Andric 238*81ad6265SDimitry Andric COMPILER_RT_VISIBILITY 239*81ad6265SDimitry Andric void __llvm_profile_register_names_function(void *NamesStart, 240*81ad6265SDimitry Andric uint64_t NamesSize) {} 241*81ad6265SDimitry Andric 242*81ad6265SDimitry Andric // The __start_SECNAME and __stop_SECNAME symbols (for SECNAME \in 243*81ad6265SDimitry Andric // {"__llvm_prf_cnts", "__llvm_prf_data", "__llvm_prf_name", "__llvm_prf_vnds"}) 244*81ad6265SDimitry Andric // are always live when linking on AIX, regardless if the .o's being linked 245*81ad6265SDimitry Andric // reference symbols from the profile library (for example when no files were 246*81ad6265SDimitry Andric // compiled with -fprofile-generate). That's because these symbols are kept 247*81ad6265SDimitry Andric // alive through references in constructor functions that are always live in the 248*81ad6265SDimitry Andric // default linking model on AIX (-bcdtors:all). The __start_SECNAME and 249*81ad6265SDimitry Andric // __stop_SECNAME symbols are only resolved by the linker when the SECNAME 250*81ad6265SDimitry Andric // section exists. So for the scenario where the user objects have no such 251*81ad6265SDimitry Andric // section (i.e. when they are compiled with -fno-profile-generate), we always 252*81ad6265SDimitry Andric // define these zero length variables in each of the above 4 sections. 253*81ad6265SDimitry Andric COMPILER_RT_VISIBILITY int dummy_cnts[0] COMPILER_RT_SECTION( 254*81ad6265SDimitry Andric COMPILER_RT_SEG INSTR_PROF_CNTS_SECT_NAME); 255*81ad6265SDimitry Andric COMPILER_RT_VISIBILITY int dummy_data[0] COMPILER_RT_SECTION( 256*81ad6265SDimitry Andric COMPILER_RT_SEG INSTR_PROF_DATA_SECT_NAME); 257*81ad6265SDimitry Andric COMPILER_RT_VISIBILITY const int dummy_name[0] COMPILER_RT_SECTION( 258*81ad6265SDimitry Andric COMPILER_RT_SEG INSTR_PROF_NAME_SECT_NAME); 259*81ad6265SDimitry Andric COMPILER_RT_VISIBILITY int dummy_vnds[0] COMPILER_RT_SECTION( 260*81ad6265SDimitry Andric COMPILER_RT_SEG INSTR_PROF_VNODES_SECT_NAME); 261*81ad6265SDimitry Andric 262*81ad6265SDimitry Andric // Create a fake reference to avoid GC'ing of the dummy variables by the linker. 263*81ad6265SDimitry Andric // Ideally, we create a ".ref" of each variable inside the function 264*81ad6265SDimitry Andric // __llvm_profile_begin_counters(), but there's no source level construct 265*81ad6265SDimitry Andric // that allows us to generate that. 266*81ad6265SDimitry Andric __attribute__((destructor)) void keep() { 267*81ad6265SDimitry Andric int volatile use = &dummy_cnts < &dummy_data && &dummy_name < &dummy_vnds; 268*81ad6265SDimitry Andric (void)use; 269*81ad6265SDimitry Andric } 270*81ad6265SDimitry Andric #endif 271*81ad6265SDimitry Andric 2720b57cec5SDimitry Andric #endif 273