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