|
9 | 9 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \ |
10 | 10 | (defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) |
11 | 11 |
|
| 12 | +#include <elf.h> |
| 13 | +#include <link.h> |
12 | 14 | #include <stdlib.h> |
| 15 | +#include <string.h> |
13 | 16 |
|
14 | 17 | #include "InstrProfiling.h" |
| 18 | +#include "InstrProfilingInternal.h" |
15 | 19 |
|
16 | 20 | #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON) |
17 | 21 | #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON) |
@@ -72,4 +76,109 @@ COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) { |
72 | 76 | COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START; |
73 | 77 | COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP; |
74 | 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 COMPILER_RT_WEAK int |
| 161 | +__llvm_write_binary_ids(ProfDataWriter *Writer) { |
| 162 | + extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden"))); |
| 163 | + const ElfW(Ehdr) *ElfHeader = &__ehdr_start; |
| 164 | + const ElfW(Phdr) *ProgramHeader = |
| 165 | + (const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff); |
| 166 | + |
| 167 | + uint32_t I; |
| 168 | + /* Iterate through entries in the program header. */ |
| 169 | + for (I = 0; I < ElfHeader->e_phnum; I++) { |
| 170 | + /* Look for the notes section in program header entries. */ |
| 171 | + if (ProgramHeader[I].p_type != PT_NOTE) |
| 172 | + continue; |
| 173 | + |
| 174 | + const ElfW(Nhdr) *Note = |
| 175 | + (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_offset); |
| 176 | + const ElfW(Nhdr) *NotesEnd = |
| 177 | + (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_filesz); |
| 178 | + return WriteBinaryIds(Writer, Note, NotesEnd); |
| 179 | + } |
| 180 | + |
| 181 | + return 0; |
| 182 | +} |
| 183 | + |
75 | 184 | #endif |
0 commit comments