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    defined(_AIX)
12
13#if !defined(_AIX)
14#include <elf.h>
15#include <link.h>
16#endif
17#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_CNTS_START INSTR_PROF_SECT_START(INSTR_PROF_CNTS_COMMON)
28#define PROF_CNTS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_CNTS_COMMON)
29#define PROF_BITS_START INSTR_PROF_SECT_START(INSTR_PROF_BITS_COMMON)
30#define PROF_BITS_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_BITS_COMMON)
31#define PROF_ORDERFILE_START INSTR_PROF_SECT_START(INSTR_PROF_ORDERFILE_COMMON)
32#define PROF_VNODES_START INSTR_PROF_SECT_START(INSTR_PROF_VNODES_COMMON)
33#define PROF_VNODES_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_VNODES_COMMON)
34
35/* Declare section start and stop symbols for various sections
36 * generated by compiler instrumentation.
37 */
38extern __llvm_profile_data PROF_DATA_START COMPILER_RT_VISIBILITY
39    COMPILER_RT_WEAK;
40extern __llvm_profile_data PROF_DATA_STOP COMPILER_RT_VISIBILITY
41    COMPILER_RT_WEAK;
42extern char PROF_CNTS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
43extern char PROF_CNTS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
44extern char PROF_BITS_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
45extern char PROF_BITS_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
46extern uint32_t PROF_ORDERFILE_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
47extern char PROF_NAME_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
48extern char PROF_NAME_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
49extern ValueProfNode PROF_VNODES_START COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
50extern ValueProfNode PROF_VNODES_STOP COMPILER_RT_VISIBILITY COMPILER_RT_WEAK;
51
52COMPILER_RT_VISIBILITY const __llvm_profile_data *
53__llvm_profile_begin_data(void) {
54  return &PROF_DATA_START;
55}
56COMPILER_RT_VISIBILITY const __llvm_profile_data *
57__llvm_profile_end_data(void) {
58  return &PROF_DATA_STOP;
59}
60COMPILER_RT_VISIBILITY const char *__llvm_profile_begin_names(void) {
61  return &PROF_NAME_START;
62}
63COMPILER_RT_VISIBILITY const char *__llvm_profile_end_names(void) {
64  return &PROF_NAME_STOP;
65}
66COMPILER_RT_VISIBILITY char *__llvm_profile_begin_counters(void) {
67  return &PROF_CNTS_START;
68}
69COMPILER_RT_VISIBILITY char *__llvm_profile_end_counters(void) {
70  return &PROF_CNTS_STOP;
71}
72COMPILER_RT_VISIBILITY char *__llvm_profile_begin_bitmap(void) {
73  return &PROF_BITS_START;
74}
75COMPILER_RT_VISIBILITY char *__llvm_profile_end_bitmap(void) {
76  return &PROF_BITS_STOP;
77}
78COMPILER_RT_VISIBILITY uint32_t *__llvm_profile_begin_orderfile(void) {
79  return &PROF_ORDERFILE_START;
80}
81
82COMPILER_RT_VISIBILITY ValueProfNode *
83__llvm_profile_begin_vnodes(void) {
84  return &PROF_VNODES_START;
85}
86COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) {
87  return &PROF_VNODES_STOP;
88}
89COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START;
90COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP;
91
92#ifdef NT_GNU_BUILD_ID
93static size_t RoundUp(size_t size, size_t align) {
94  return (size + align - 1) & ~(align - 1);
95}
96
97/*
98 * Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID
99 * that contains build id. If build id exists, write binary id.
100 *
101 * Each note in notes section starts with a struct which includes
102 * n_namesz, n_descsz, and n_type members. It is followed by the name
103 * (whose length is defined in n_namesz) and then by the descriptor
104 * (whose length is defined in n_descsz).
105 *
106 * Note sections like .note.ABI-tag and .note.gnu.build-id are aligned
107 * to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.
108 */
109static int WriteBinaryIdForNote(ProfDataWriter *Writer,
110                                const ElfW(Nhdr) * Note) {
111  int BinaryIdSize = 0;
112  const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));
113  if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&
114      memcmp(NoteName, "GNU\0", 4) == 0) {
115    uint64_t BinaryIdLen = Note->n_descsz;
116    const uint8_t *BinaryIdData =
117        (const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));
118    uint8_t BinaryIdPadding = __llvm_profile_get_num_padding_bytes(BinaryIdLen);
119    if (Writer != NULL &&
120        lprofWriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData,
121                              BinaryIdPadding) == -1)
122      return -1;
123
124    BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen + BinaryIdPadding;
125  }
126
127  return BinaryIdSize;
128}
129
130/*
131 * Helper function that iterates through notes section and find build ids.
132 * If writer is given, write binary ids into profiles.
133 * If an error happens while writing, return -1.
134 */
135static int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,
136                          const ElfW(Nhdr) * NotesEnd) {
137  int BinaryIdsSize = 0;
138  while (Note < NotesEnd) {
139    int OneBinaryIdSize = WriteBinaryIdForNote(Writer, Note);
140    if (OneBinaryIdSize == -1)
141      return -1;
142    BinaryIdsSize += OneBinaryIdSize;
143
144    /* Calculate the offset of the next note in notes section. */
145    size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +
146                        RoundUp(Note->n_descsz, 4);
147    Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);
148  }
149
150  return BinaryIdsSize;
151}
152
153/*
154 * Write binary ids into profiles if writer is given.
155 * Return the total size of binary ids.
156 * If an error happens while writing, return -1.
157 */
158COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
159  extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));
160  const ElfW(Ehdr) *ElfHeader = &__ehdr_start;
161  const ElfW(Phdr) *ProgramHeader =
162      (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);
163
164  int TotalBinaryIdsSize = 0;
165  uint32_t I;
166  /* Iterate through entries in the program header. */
167  for (I = 0; I < ElfHeader->e_phnum; I++) {
168    /* Look for the notes segment in program header entries. */
169    if (ProgramHeader[I].p_type != PT_NOTE)
170      continue;
171
172    /* There can be multiple notes segment, and examine each of them. */
173    const ElfW(Nhdr) * Note;
174    const ElfW(Nhdr) * NotesEnd;
175    /*
176     * When examining notes in file, use p_offset, which is the offset within
177     * the elf file, to find the start of notes.
178     */
179    if (ProgramHeader[I].p_memsz == 0 ||
180        ProgramHeader[I].p_memsz == ProgramHeader[I].p_filesz) {
181      Note = (const ElfW(Nhdr) *)((uintptr_t)ElfHeader +
182                                  ProgramHeader[I].p_offset);
183      NotesEnd = (const ElfW(Nhdr) *)((const char *)(Note) +
184                                      ProgramHeader[I].p_filesz);
185    } else {
186      /*
187       * When examining notes in memory, use p_vaddr, which is the address of
188       * section after loaded to memory, to find the start of notes.
189       */
190      Note =
191          (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_vaddr);
192      NotesEnd =
193          (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_memsz);
194    }
195
196    int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd);
197    if (TotalBinaryIdsSize == -1)
198      return -1;
199
200    TotalBinaryIdsSize += BinaryIdsSize;
201  }
202
203  return TotalBinaryIdsSize;
204}
205#elif !defined(_AIX) /* !NT_GNU_BUILD_ID */
206/*
207 * Fallback implementation for targets that don't support the GNU
208 * extensions NT_GNU_BUILD_ID and __ehdr_start.
209 */
210COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
211  return 0;
212}
213#endif
214
215#endif
216