Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v6.17 360 lines 7.7 kB view raw
1#include "dso.h" 2#include "symbol.h" 3#include "symsrc.h" 4 5#include <errno.h> 6#include <unistd.h> 7#include <fcntl.h> 8#include <string.h> 9#include <stdlib.h> 10#include <byteswap.h> 11#include <sys/stat.h> 12#include <linux/zalloc.h> 13#include <internal/lib.h> 14 15static bool check_need_swap(int file_endian) 16{ 17 const int data = 1; 18 u8 *check = (u8 *)&data; 19 int host_endian; 20 21 if (check[0] == 1) 22 host_endian = ELFDATA2LSB; 23 else 24 host_endian = ELFDATA2MSB; 25 26 return host_endian != file_endian; 27} 28 29#define NOTE_ALIGN(sz) (((sz) + 3) & ~3) 30 31#define NT_GNU_BUILD_ID 3 32 33static int read_build_id(void *note_data, size_t note_len, struct build_id *bid, 34 bool need_swap) 35{ 36 size_t size = sizeof(bid->data); 37 struct { 38 u32 n_namesz; 39 u32 n_descsz; 40 u32 n_type; 41 } *nhdr; 42 void *ptr; 43 44 ptr = note_data; 45 while (ptr < (note_data + note_len)) { 46 const char *name; 47 size_t namesz, descsz; 48 49 nhdr = ptr; 50 if (need_swap) { 51 nhdr->n_namesz = bswap_32(nhdr->n_namesz); 52 nhdr->n_descsz = bswap_32(nhdr->n_descsz); 53 nhdr->n_type = bswap_32(nhdr->n_type); 54 } 55 56 namesz = NOTE_ALIGN(nhdr->n_namesz); 57 descsz = NOTE_ALIGN(nhdr->n_descsz); 58 59 ptr += sizeof(*nhdr); 60 name = ptr; 61 ptr += namesz; 62 if (nhdr->n_type == NT_GNU_BUILD_ID && 63 nhdr->n_namesz == sizeof("GNU")) { 64 if (memcmp(name, "GNU", sizeof("GNU")) == 0) { 65 size_t sz = min(size, descsz); 66 memcpy(bid->data, ptr, sz); 67 memset(bid->data + sz, 0, size - sz); 68 bid->size = sz; 69 return 0; 70 } 71 } 72 ptr += descsz; 73 } 74 75 return -1; 76} 77 78int filename__read_debuglink(const char *filename __maybe_unused, 79 char *debuglink __maybe_unused, 80 size_t size __maybe_unused) 81{ 82 return -1; 83} 84 85/* 86 * Just try PT_NOTE header otherwise fails 87 */ 88int filename__read_build_id(const char *filename, struct build_id *bid, bool block) 89{ 90 int fd, ret = -1; 91 bool need_swap = false, elf32; 92 union { 93 struct { 94 Elf32_Ehdr ehdr32; 95 Elf32_Phdr *phdr32; 96 }; 97 struct { 98 Elf64_Ehdr ehdr64; 99 Elf64_Phdr *phdr64; 100 }; 101 } hdrs; 102 void *phdr, *buf = NULL; 103 ssize_t phdr_size, ehdr_size, buf_size = 0; 104 105 fd = open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK)); 106 if (fd < 0) 107 return -1; 108 109 if (read(fd, hdrs.ehdr32.e_ident, EI_NIDENT) != EI_NIDENT) 110 goto out; 111 112 if (memcmp(hdrs.ehdr32.e_ident, ELFMAG, SELFMAG) || 113 hdrs.ehdr32.e_ident[EI_VERSION] != EV_CURRENT) 114 goto out; 115 116 need_swap = check_need_swap(hdrs.ehdr32.e_ident[EI_DATA]); 117 elf32 = hdrs.ehdr32.e_ident[EI_CLASS] == ELFCLASS32; 118 ehdr_size = (elf32 ? sizeof(hdrs.ehdr32) : sizeof(hdrs.ehdr64)) - EI_NIDENT; 119 120 if (read(fd, 121 (elf32 ? (void *)&hdrs.ehdr32 : (void *)&hdrs.ehdr64) + EI_NIDENT, 122 ehdr_size) != ehdr_size) 123 goto out; 124 125 if (need_swap) { 126 if (elf32) { 127 hdrs.ehdr32.e_phoff = bswap_32(hdrs.ehdr32.e_phoff); 128 hdrs.ehdr32.e_phentsize = bswap_16(hdrs.ehdr32.e_phentsize); 129 hdrs.ehdr32.e_phnum = bswap_16(hdrs.ehdr32.e_phnum); 130 } else { 131 hdrs.ehdr64.e_phoff = bswap_64(hdrs.ehdr64.e_phoff); 132 hdrs.ehdr64.e_phentsize = bswap_16(hdrs.ehdr64.e_phentsize); 133 hdrs.ehdr64.e_phnum = bswap_16(hdrs.ehdr64.e_phnum); 134 } 135 } 136 if ((elf32 && hdrs.ehdr32.e_phentsize != sizeof(Elf32_Phdr)) || 137 (!elf32 && hdrs.ehdr64.e_phentsize != sizeof(Elf64_Phdr))) 138 goto out; 139 140 phdr_size = elf32 ? sizeof(Elf32_Phdr) * hdrs.ehdr32.e_phnum 141 : sizeof(Elf64_Phdr) * hdrs.ehdr64.e_phnum; 142 phdr = malloc(phdr_size); 143 if (phdr == NULL) 144 goto out; 145 146 lseek(fd, elf32 ? hdrs.ehdr32.e_phoff : hdrs.ehdr64.e_phoff, SEEK_SET); 147 if (read(fd, phdr, phdr_size) != phdr_size) 148 goto out_free; 149 150 if (elf32) 151 hdrs.phdr32 = phdr; 152 else 153 hdrs.phdr64 = phdr; 154 155 for (int i = 0; i < (elf32 ? hdrs.ehdr32.e_phnum : hdrs.ehdr64.e_phnum); i++) { 156 ssize_t p_filesz; 157 158 if (need_swap) { 159 if (elf32) { 160 hdrs.phdr32[i].p_type = bswap_32(hdrs.phdr32[i].p_type); 161 hdrs.phdr32[i].p_offset = bswap_32(hdrs.phdr32[i].p_offset); 162 hdrs.phdr32[i].p_filesz = bswap_32(hdrs.phdr32[i].p_offset); 163 } else { 164 hdrs.phdr64[i].p_type = bswap_32(hdrs.phdr64[i].p_type); 165 hdrs.phdr64[i].p_offset = bswap_64(hdrs.phdr64[i].p_offset); 166 hdrs.phdr64[i].p_filesz = bswap_64(hdrs.phdr64[i].p_filesz); 167 } 168 } 169 if ((elf32 ? hdrs.phdr32[i].p_type : hdrs.phdr64[i].p_type) != PT_NOTE) 170 continue; 171 172 p_filesz = elf32 ? hdrs.phdr32[i].p_filesz : hdrs.phdr64[i].p_filesz; 173 if (p_filesz > buf_size) { 174 void *tmp; 175 176 buf_size = p_filesz; 177 tmp = realloc(buf, buf_size); 178 if (tmp == NULL) 179 goto out_free; 180 buf = tmp; 181 } 182 lseek(fd, elf32 ? hdrs.phdr32[i].p_offset : hdrs.phdr64[i].p_offset, SEEK_SET); 183 if (read(fd, buf, p_filesz) != p_filesz) 184 goto out_free; 185 186 ret = read_build_id(buf, p_filesz, bid, need_swap); 187 if (ret == 0) { 188 ret = bid->size; 189 break; 190 } 191 } 192out_free: 193 free(buf); 194 free(phdr); 195out: 196 close(fd); 197 return ret; 198} 199 200int sysfs__read_build_id(const char *filename, struct build_id *bid) 201{ 202 int fd; 203 int ret = -1; 204 struct stat stbuf; 205 size_t buf_size; 206 void *buf; 207 208 fd = open(filename, O_RDONLY); 209 if (fd < 0) 210 return -1; 211 212 if (fstat(fd, &stbuf) < 0) 213 goto out; 214 215 buf_size = stbuf.st_size; 216 buf = malloc(buf_size); 217 if (buf == NULL) 218 goto out; 219 220 if (read(fd, buf, buf_size) != (ssize_t) buf_size) 221 goto out_free; 222 223 ret = read_build_id(buf, buf_size, bid, false); 224out_free: 225 free(buf); 226out: 227 close(fd); 228 return ret; 229} 230 231int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name, 232 enum dso_binary_type type) 233{ 234 int fd = open(name, O_RDONLY); 235 if (fd < 0) 236 goto out_errno; 237 238 ss->name = strdup(name); 239 if (!ss->name) 240 goto out_close; 241 242 ss->fd = fd; 243 ss->type = type; 244 245 return 0; 246out_close: 247 close(fd); 248out_errno: 249 RC_CHK_ACCESS(dso)->load_errno = errno; 250 return -1; 251} 252 253bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused) 254{ 255 /* Assume all sym sources could be a runtime image. */ 256 return true; 257} 258 259bool symsrc__has_symtab(struct symsrc *ss __maybe_unused) 260{ 261 return false; 262} 263 264void symsrc__destroy(struct symsrc *ss) 265{ 266 zfree(&ss->name); 267 close(ss->fd); 268} 269 270int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused, 271 struct symsrc *ss __maybe_unused) 272{ 273 return 0; 274} 275 276static int fd__is_64_bit(int fd) 277{ 278 u8 e_ident[EI_NIDENT]; 279 280 if (lseek(fd, 0, SEEK_SET)) 281 return -1; 282 283 if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident)) 284 return -1; 285 286 if (memcmp(e_ident, ELFMAG, SELFMAG) || 287 e_ident[EI_VERSION] != EV_CURRENT) 288 return -1; 289 290 return e_ident[EI_CLASS] == ELFCLASS64; 291} 292 293enum dso_type dso__type_fd(int fd) 294{ 295 Elf64_Ehdr ehdr; 296 int ret; 297 298 ret = fd__is_64_bit(fd); 299 if (ret < 0) 300 return DSO__TYPE_UNKNOWN; 301 302 if (ret) 303 return DSO__TYPE_64BIT; 304 305 if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) 306 return DSO__TYPE_UNKNOWN; 307 308 if (ehdr.e_machine == EM_X86_64) 309 return DSO__TYPE_X32BIT; 310 311 return DSO__TYPE_32BIT; 312} 313 314int dso__load_sym(struct dso *dso, struct map *map __maybe_unused, 315 struct symsrc *ss, 316 struct symsrc *runtime_ss __maybe_unused, 317 int kmodule __maybe_unused) 318{ 319 struct build_id bid = { .size = 0, }; 320 int ret; 321 322 ret = fd__is_64_bit(ss->fd); 323 if (ret >= 0) 324 RC_CHK_ACCESS(dso)->is_64_bit = ret; 325 326 if (filename__read_build_id(ss->name, &bid, /*block=*/true) > 0) 327 dso__set_build_id(dso, &bid); 328 return 0; 329} 330 331int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused, 332 mapfn_t mapfn __maybe_unused, void *data __maybe_unused, 333 bool *is_64_bit __maybe_unused) 334{ 335 return -1; 336} 337 338int kcore_extract__create(struct kcore_extract *kce __maybe_unused) 339{ 340 return -1; 341} 342 343void kcore_extract__delete(struct kcore_extract *kce __maybe_unused) 344{ 345} 346 347int kcore_copy(const char *from_dir __maybe_unused, 348 const char *to_dir __maybe_unused) 349{ 350 return -1; 351} 352 353void symbol__elf_init(void) 354{ 355} 356 357bool filename__has_section(const char *filename __maybe_unused, const char *sec __maybe_unused) 358{ 359 return false; 360}