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-exception
6 |*
7 \*===----------------------------------------------------------------------===*/
8 
9 #if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
10     (defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__)
11 
12 #include <elf.h>
13 #include <link.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include "InstrProfiling.h"
18 #include "InstrProfilingInternal.h"
19 
20 #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON)
21 #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON)
22 #define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_COMMON)
23 #define PROF_NAME_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_NAME_COMMON)
24 #define PROF_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON)
25 #define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON)
26 #define PROF_ORDERFILE_START INSTR_PROF_SECT_START(INSTR_PROF_ORDERFILE_COMMON)
27 #define PROF_VNODES_START INSTR_PROF_SECT_START(INSTR_PROF_VNODES_COMMON)
28 #define PROF_VNODES_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNODES_COMMON)
29 
30 /* Declare section start and stop symbols for various sections
31  * generated by compiler instrumentation.
32  */
33 extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY
34     COMPILER_RT_WEAK;
35 extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY
36     COMPILER_RT_WEAK;
37 extern uint64_t PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
38 extern uint64_t PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
39 extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
40 extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
41 extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
42 extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
43 extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
44 
45 COMPILER_RT_VISIBILITY const __llvm_profile_data *
46 __llvm_profile_begin_data(void) {
47   return &PROF_DATA_START;
48 }
49 COMPILER_RT_VISIBILITY const __llvm_profile_data *
50 __llvm_profile_end_data(void) {
51   return &PROF_DATA_STOP;
52 }
53 COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) {
54   return &PROF_NAME_START;
55 }
56 COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) {
57   return &PROF_NAME_STOP;
58 }
59 COMPILER_RT_VISIBILITY uint64_t *__llvm_profile_begin_counters(void) {
60   return &PROF_CNTS_START;
61 }
62 COMPILER_RT_VISIBILITY uint64_t *__llvm_profile_end_counters(void) {
63   return &PROF_CNTS_STOP;
64 }
65 COMPILER_RT_VISIBILITY uint32_t *__llvm_profile_begin_orderfile(void) {
66   return &PROF_ORDERFILE_START;
67 }
68 
69 COMPILER_RT_VISIBILITY ValueProfNode *
70 __llvm_profile_begin_vnodes(void) {
71   return &PROF_VNODES_START;
72 }
73 COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) {
74   return &PROF_VNODES_STOP;
75 }
76 COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START;
77 COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP;
78 
79 static size_t RoundUp(size_t size, size_t align) {
80   return (size + align - 1) & ~(align - 1);
81 }
82 
83 /*
84  * Write binary id length and then its data, because binary id does not
85  * have a fixed length.
86  */
87 int WriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen,
88                      const uint8_t *BinaryIdData) {
89   ProfDataIOVec BinaryIdIOVec[] = {
90       {&BinaryIdLen, sizeof(uint64_t), 1, 0},
91       {BinaryIdData, sizeof(uint8_t), BinaryIdLen, 0}};
92   if (Writer->Write(Writer, BinaryIdIOVec,
93                     sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec)))
94     return -1;
95 
96   /* Successfully wrote binary id, report success. */
97   return 0;
98 }
99 
100 /*
101  * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID
102  * that contains build id. If build id exists, write binary id.
103  *
104  * Each note in notes section starts with a struct which includes
105  * n_namesz, n_descsz, and n_type members. It is followed by the name
106  * (whose length is defined in n_namesz) and then by the descriptor
107  * (whose length is defined in n_descsz).
108  *
109  * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned
110  * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.
111  */
112 int WriteBinaryIdForNote(ProfDataWriter *Writer, const ElfW(Nhdr) * Note) {
113   int BinaryIdSize = 0;
114 
115   const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));
116   if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&
117       memcmp(NoteName, "GNU\0", 4) == 0) {
118 
119     uint64_t BinaryIdLen = Note->n_descsz;
120     const uint8_t *BinaryIdData =
121         (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));
122     if (Writer != NULL &&
123         WriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData) == -1)
124       return -1;
125 
126     BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen;
127   }
128 
129   return BinaryIdSize;
130 }
131 
132 /*
133  * Helper function that iterates through notes section and find build ids.
134  * If writer is given, write binary ids into profiles.
135  * If an error happens while writing, return -1.
136  */
137 int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,
138                    const ElfW(Nhdr) * NotesEnd) {
139   int TotalBinaryIdsSize = 0;
140   while (Note < NotesEnd) {
141     int Result = WriteBinaryIdForNote(Writer, Note);
142     if (Result == -1)
143       return -1;
144     TotalBinaryIdsSize += Result;
145 
146     /* Calculate the offset of the next note in notes section. */
147     size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +
148                         RoundUp(Note->n_descsz, 4);
149     Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);
150   }
151 
152   return TotalBinaryIdsSize;
153 }
154 
155 /*
156  * Write binary ids into profiles if writer is given.
157  * Return the total size of binary ids.
158  * If an error happens while writing, return -1.
159  */
160 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
161   extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));
162   const ElfW(Ehdr) *ElfHeader = &__ehdr_start;
163   const ElfW(Phdr) *ProgramHeader =
164       (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);
165 
166   uint32_t I;
167   /* Iterate through entries in the program header. */
168   for (I = 0; I < ElfHeader->e_phnum; I++) {
169     /* Look for the notes section in program header entries. */
170     if (ProgramHeader[I].p_type != PT_NOTE)
171       continue;
172 
173     const ElfW(Nhdr) *Note =
174         (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_offset);
175     const ElfW(Nhdr) *NotesEnd =
176         (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_filesz);
177     return WriteBinaryIds(Writer, Note, NotesEnd);
178   }
179 
180   return 0;
181 }
182 
183 #endif
184