240 lines · c
1/*===- InstrProfilingPlatformLinux.c - Profile data Linux platform ------===*\2|*3|* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4|* See https://llvm.org/LICENSE.txt for license information.5|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6|*7\*===----------------------------------------------------------------------===*/8 9#if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \10 (defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) || \11 defined(_AIX) || defined(__wasm__) || defined(__HAIKU__)12 13#if !defined(_AIX) && !defined(__wasm__)14#include <elf.h>15#include <link.h>16#endif17#include <stdlib.h>18#include <string.h>19 20#include "InstrProfiling.h"21#include "InstrProfilingInternal.h"22 23#define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON)24#define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON)25#define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON)26#define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON)27#define PROF_VNAME_START INSTR_PROF_SECT_START(INSTR_PROF_VNAME_COMMON)28#define PROF_VNAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNAME_COMMON)29#define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON)30#define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON)31#define PROF_VTABLE_START INSTR_PROF_SECT_START(INSTR_PROF_VTAB_COMMON)32#define PROF_VTABLE_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VTAB_COMMON)33#define PROF_BITS_START INSTR_PROF_SECT_START(INSTR_PROF_BITS_COMMON)34#define PROF_BITS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_BITS_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)37#define PROF_COVINIT_START INSTR_PROF_SECT_START(INSTR_PROF_COVINIT_COMMON)38#define PROF_COVINIT_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_COVINIT_COMMON)39 40/* Declare section start and stop symbols for various sections41 * generated by compiler instrumentation.42 */43extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY44 COMPILER_RT_WEAK;45extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY46 COMPILER_RT_WEAK;47extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;48extern char PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;49extern VTableProfData PROF_VTABLE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;50extern VTableProfData PROF_VTABLE_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;51extern char PROF_VNAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;52extern char PROF_VNAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;53extern char PROF_BITS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;54extern char PROF_BITS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;55extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;56extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;57extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;58extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;59extern __llvm_gcov_init_func_struct PROF_COVINIT_START COMPILER_RT_VISIBILITY60 COMPILER_RT_WEAK;61extern __llvm_gcov_init_func_struct PROF_COVINIT_STOP COMPILER_RT_VISIBILITY62 COMPILER_RT_WEAK;63 64COMPILER_RT_VISIBILITY const __llvm_profile_data *65__llvm_profile_begin_data(void) {66 return &PROF_DATA_START;67}68COMPILER_RT_VISIBILITY const __llvm_profile_data *69__llvm_profile_end_data(void) {70 return &PROF_DATA_STOP;71}72COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) {73 return &PROF_NAME_START;74}75COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) {76 return &PROF_NAME_STOP;77}78COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_vtabnames(void) {79 return &PROF_VNAME_START;80}81COMPILER_RT_VISIBILITY const char *__llvm_profile_end_vtabnames(void) {82 return &PROF_VNAME_STOP;83}84COMPILER_RT_VISIBILITY const VTableProfData *85__llvm_profile_begin_vtables(void) {86 return &PROF_VTABLE_START;87}88COMPILER_RT_VISIBILITY const VTableProfData *__llvm_profile_end_vtables(void) {89 return &PROF_VTABLE_STOP;90}91COMPILER_RT_VISIBILITY char *__llvm_profile_begin_counters(void) {92 return &PROF_CNTS_START;93}94COMPILER_RT_VISIBILITY char *__llvm_profile_end_counters(void) {95 return &PROF_CNTS_STOP;96}97COMPILER_RT_VISIBILITY char *__llvm_profile_begin_bitmap(void) {98 return &PROF_BITS_START;99}100COMPILER_RT_VISIBILITY char *__llvm_profile_end_bitmap(void) {101 return &PROF_BITS_STOP;102}103 104COMPILER_RT_VISIBILITY ValueProfNode *105__llvm_profile_begin_vnodes(void) {106 return &PROF_VNODES_START;107}108COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) {109 return &PROF_VNODES_STOP;110}111COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START;112COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP;113 114COMPILER_RT_VISIBILITY const __llvm_gcov_init_func_struct *115__llvm_profile_begin_covinit() {116 return &PROF_COVINIT_START;117}118 119COMPILER_RT_VISIBILITY const __llvm_gcov_init_func_struct *120__llvm_profile_end_covinit() {121 return &PROF_COVINIT_STOP;122}123 124#ifdef NT_GNU_BUILD_ID125static size_t RoundUp(size_t size, size_t align) {126 return (size + align - 1) & ~(align - 1);127}128 129/*130 * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID131 * that contains build id. If build id exists, write binary id.132 *133 * Each note in notes section starts with a struct which includes134 * n_namesz, n_descsz, and n_type members. It is followed by the name135 * (whose length is defined in n_namesz) and then by the descriptor136 * (whose length is defined in n_descsz).137 *138 * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned139 * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.140 */141static int WriteBinaryIdForNote(ProfDataWriter *Writer,142 const ElfW(Nhdr) * Note) {143 int BinaryIdSize = 0;144 const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));145 if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&146 memcmp(NoteName, "GNU\0", 4) == 0) {147 uint64_t BinaryIdLen = Note->n_descsz;148 const uint8_t *BinaryIdData =149 (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));150 uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen);151 if (Writer != NULL &&152 lprofWriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData,153 BinaryIdPadding) == -1)154 return -1;155 156 BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding;157 }158 159 return BinaryIdSize;160}161 162/*163 * Helper function that iterates through notes section and find build ids.164 * If writer is given, write binary ids into profiles.165 * If an error happens while writing, return -1.166 */167static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,168 const ElfW(Nhdr) * NotesEnd) {169 int BinaryIdsSize = 0;170 while (Note < NotesEnd) {171 int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note);172 if (OneBinaryIdSize == -1)173 return -1;174 BinaryIdsSize += OneBinaryIdSize;175 176 /* Calculate the offset of the next note in notes section. */177 size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +178 RoundUp(Note->n_descsz, 4);179 Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);180 }181 182 return BinaryIdsSize;183}184 185/*186 * Write binary ids into profiles if writer is given.187 * Return the total size of binary ids.188 * If an error happens while writing, return -1.189 */190COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {191 extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));192 extern ElfW(Dyn) _DYNAMIC[] __attribute__((weak, visibility("hidden")));193 194 const ElfW(Ehdr) *ElfHeader = &__ehdr_start;195 const ElfW(Phdr) *ProgramHeader =196 (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);197 198 /* Compute the added base address in case of position-independent code. */199 uintptr_t Base = 0;200 for (uint32_t I = 0; I < ElfHeader->e_phnum; I++) {201 if (ProgramHeader[I].p_type == PT_PHDR)202 Base = (uintptr_t)ProgramHeader - ProgramHeader[I].p_vaddr;203 if (ProgramHeader[I].p_type == PT_DYNAMIC && _DYNAMIC)204 Base = (uintptr_t)_DYNAMIC - ProgramHeader[I].p_vaddr;205 }206 207 int TotalBinaryIdsSize = 0;208 /* Iterate through entries in the program header. */209 for (uint32_t I = 0; I < ElfHeader->e_phnum; I++) {210 /* Look for the notes segment in program header entries. */211 if (ProgramHeader[I].p_type != PT_NOTE)212 continue;213 214 /* There can be multiple notes segment, and examine each of them. */215 const ElfW(Nhdr) *Note =216 (const ElfW(Nhdr) *)(Base + ProgramHeader[I].p_vaddr);217 const ElfW(Nhdr) *NotesEnd =218 (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz);219 220 int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd);221 if (TotalBinaryIdsSize == -1)222 return -1;223 224 TotalBinaryIdsSize += BinaryIdsSize;225 }226 227 return TotalBinaryIdsSize;228}229#elif !defined(_AIX) /* !NT_GNU_BUILD_ID */230/*231 * Fallback implementation for targets that don't support the GNU232 * extensions NT_GNU_BUILD_ID and __ehdr_start.233 */234COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {235 return 0;236}237#endif238 239#endif240