Path: blob/main/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c
35233 views
/*===- InstrProfilingPlatformLinux.c - Profile data Linux platform ------===*\1|*2|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3|* See https://llvm.org/LICENSE.txt for license information.4|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5|*6\*===----------------------------------------------------------------------===*/78#if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \9(defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) || \10defined(_AIX)1112#if !defined(_AIX)13#include <elf.h>14#include <link.h>15#endif16#include <stdlib.h>17#include <string.h>1819#include "InstrProfiling.h"20#include "InstrProfilingInternal.h"2122#define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON)23#define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON)24#define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON)25#define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON)26#define PROF_VNAME_START INSTR_PROF_SECT_START(INSTR_PROF_VNAME_COMMON)27#define PROF_VNAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNAME_COMMON)28#define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON)29#define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON)30#define PROF_VTABLE_START INSTR_PROF_SECT_START(INSTR_PROF_VTAB_COMMON)31#define PROF_VTABLE_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VTAB_COMMON)32#define PROF_BITS_START INSTR_PROF_SECT_START(INSTR_PROF_BITS_COMMON)33#define PROF_BITS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_BITS_COMMON)34#define PROF_ORDERFILE_START INSTR_PROF_SECT_START(INSTR_PROF_ORDERFILE_COMMON)35#define PROF_VNODES_START INSTR_PROF_SECT_START(INSTR_PROF_VNODES_COMMON)36#define PROF_VNODES_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNODES_COMMON)3738/* Declare section start and stop symbols for various sections39* generated by compiler instrumentation.40*/41extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY42COMPILER_RT_WEAK;43extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY44COMPILER_RT_WEAK;45extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;46extern char PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;47extern VTableProfData PROF_VTABLE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;48extern VTableProfData PROF_VTABLE_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;49extern char PROF_VNAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;50extern char PROF_VNAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;51extern char PROF_BITS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;52extern char PROF_BITS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;53extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;54extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;55extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;56extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;57extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;5859COMPILER_RT_VISIBILITY const __llvm_profile_data *60__llvm_profile_begin_data(void) {61return &PROF_DATA_START;62}63COMPILER_RT_VISIBILITY const __llvm_profile_data *64__llvm_profile_end_data(void) {65return &PROF_DATA_STOP;66}67COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) {68return &PROF_NAME_START;69}70COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) {71return &PROF_NAME_STOP;72}73COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_vtabnames(void) {74return &PROF_VNAME_START;75}76COMPILER_RT_VISIBILITY const char *__llvm_profile_end_vtabnames(void) {77return &PROF_VNAME_STOP;78}79COMPILER_RT_VISIBILITY const VTableProfData *80__llvm_profile_begin_vtables(void) {81return &PROF_VTABLE_START;82}83COMPILER_RT_VISIBILITY const VTableProfData *__llvm_profile_end_vtables(void) {84return &PROF_VTABLE_STOP;85}86COMPILER_RT_VISIBILITY char *__llvm_profile_begin_counters(void) {87return &PROF_CNTS_START;88}89COMPILER_RT_VISIBILITY char *__llvm_profile_end_counters(void) {90return &PROF_CNTS_STOP;91}92COMPILER_RT_VISIBILITY char *__llvm_profile_begin_bitmap(void) {93return &PROF_BITS_START;94}95COMPILER_RT_VISIBILITY char *__llvm_profile_end_bitmap(void) {96return &PROF_BITS_STOP;97}98COMPILER_RT_VISIBILITY uint32_t *__llvm_profile_begin_orderfile(void) {99return &PROF_ORDERFILE_START;100}101102COMPILER_RT_VISIBILITY ValueProfNode *103__llvm_profile_begin_vnodes(void) {104return &PROF_VNODES_START;105}106COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) {107return &PROF_VNODES_STOP;108}109COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START;110COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP;111112#ifdef NT_GNU_BUILD_ID113static size_t RoundUp(size_t size, size_t align) {114return (size + align - 1) & ~(align - 1);115}116117/*118* Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID119* that contains build id. If build id exists, write binary id.120*121* Each note in notes section starts with a struct which includes122* n_namesz, n_descsz, and n_type members. It is followed by the name123* (whose length is defined in n_namesz) and then by the descriptor124* (whose length is defined in n_descsz).125*126* Note sections like .note.ABI-tag and .note.gnu.build-id are aligned127* to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.128*/129static int WriteBinaryIdForNote(ProfDataWriter *Writer,130const ElfW(Nhdr) * Note) {131int BinaryIdSize = 0;132const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));133if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&134memcmp(NoteName, "GNU\0", 4) == 0) {135uint64_t BinaryIdLen = Note->n_descsz;136const uint8_t *BinaryIdData =137(const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));138uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen);139if (Writer != NULL &&140lprofWriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData,141BinaryIdPadding) == -1)142return -1;143144BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding;145}146147return BinaryIdSize;148}149150/*151* Helper function that iterates through notes section and find build ids.152* If writer is given, write binary ids into profiles.153* If an error happens while writing, return -1.154*/155static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,156const ElfW(Nhdr) * NotesEnd) {157int BinaryIdsSize = 0;158while (Note < NotesEnd) {159int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note);160if (OneBinaryIdSize == -1)161return -1;162BinaryIdsSize += OneBinaryIdSize;163164/* Calculate the offset of the next note in notes section. */165size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +166RoundUp(Note->n_descsz, 4);167Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);168}169170return BinaryIdsSize;171}172173/*174* Write binary ids into profiles if writer is given.175* Return the total size of binary ids.176* If an error happens while writing, return -1.177*/178COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {179extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));180const ElfW(Ehdr) *ElfHeader = &__ehdr_start;181const ElfW(Phdr) *ProgramHeader =182(const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);183184int TotalBinaryIdsSize = 0;185uint32_t I;186/* Iterate through entries in the program header. */187for (I = 0; I < ElfHeader->e_phnum; I++) {188/* Look for the notes segment in program header entries. */189if (ProgramHeader[I].p_type != PT_NOTE)190continue;191192/* There can be multiple notes segment, and examine each of them. */193const ElfW(Nhdr) * Note;194const ElfW(Nhdr) * NotesEnd;195/*196* When examining notes in file, use p_offset, which is the offset within197* the elf file, to find the start of notes.198*/199if (ProgramHeader[I].p_memsz == 0 ||200ProgramHeader[I].p_memsz == ProgramHeader[I].p_filesz) {201Note = (const ElfW(Nhdr) *)((uintptr_t)ElfHeader +202ProgramHeader[I].p_offset);203NotesEnd = (const ElfW(Nhdr) *)((const char *)(Note) +204ProgramHeader[I].p_filesz);205} else {206/*207* When examining notes in memory, use p_vaddr, which is the address of208* section after loaded to memory, to find the start of notes.209*/210Note =211(const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_vaddr);212NotesEnd =213(const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz);214}215216int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd);217if (TotalBinaryIdsSize == -1)218return -1;219220TotalBinaryIdsSize += BinaryIdsSize;221}222223return TotalBinaryIdsSize;224}225#elif !defined(_AIX) /* !NT_GNU_BUILD_ID */226/*227* Fallback implementation for targets that don't support the GNU228* extensions NT_GNU_BUILD_ID and __ehdr_start.229*/230COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {231return 0;232}233#endif234235#endif236237238