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