Source

gimli / elf-read.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
 * Copyright (c) 2009 Message Systems, Inc. All rights reserved
 * For licensing information, see:
 * https://bitbucket.org/wez/gimli/src/tip/LICENSE
 */
#ifndef __MACH__
#include "impl.h"

/* on-disk data types */
typedef uint32_t elf32_addr_t;
typedef uint32_t elf32_off_t;
typedef uint16_t elf32_half_t;
typedef uint32_t elf32_word_t;
typedef int32_t  elf32_sword_t;

typedef uint64_t elf64_addr_t;
typedef uint64_t elf64_off_t;
typedef uint16_t elf64_half_t;
typedef uint32_t elf64_word_t;
typedef int32_t  elf64_sword_t;
typedef uint64_t elf64_xword_t;
typedef int64_t  elf64_sxword_t;

struct elf32_ehdr {
  elf32_half_t e_type, e_machine;
  elf32_word_t e_version;
  elf32_addr_t e_entry;
  elf32_off_t  e_phoff, e_shoff;
  elf32_word_t e_flags;
  elf32_half_t e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx;
};

struct elf64_ehdr {
  elf64_half_t e_type, e_machine;
  elf64_word_t e_version;
  elf64_addr_t e_entry;
  elf64_off_t  e_phoff, e_shoff;
  elf64_word_t e_flags;
  elf64_half_t e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx;
};

struct elf32_shdr {
  elf32_word_t sh_name, sh_type, sh_flags;
  elf32_addr_t sh_addr;
  elf32_off_t  sh_offset;
  elf32_word_t sh_size, sh_link, sh_info, sh_addralign, sh_entsize;
};
struct elf64_shdr {
  elf64_word_t  sh_name, sh_type;
  elf64_xword_t sh_flags;
  elf64_addr_t  sh_addr;
  elf64_off_t   sh_offset;
  elf64_xword_t sh_size;
  elf64_word_t  sh_link, sh_info;
  elf64_xword_t sh_addralign, sh_entsize;
};

struct elf32_sym {
  elf32_word_t st_name;
  elf32_addr_t st_value;
  elf32_word_t st_size;
  uint8_t st_info, st_other;
  elf32_half_t st_shndx;
};

struct elf64_sym {
  elf64_word_t st_name;
  uint8_t st_info, st_other;
  elf64_half_t st_shndx;
  elf64_addr_t st_value;
  elf64_xword_t st_size;
};

struct elf32_phdr {
  elf32_word_t p_type;
  elf32_off_t p_offset;
  elf32_addr_t p_vaddr, p_paddr;
  elf32_word_t p_filesz, p_memsz, p_flags, p_align;
};

struct elf64_phdr {
  elf64_word_t p_type, p_flags;
  elf64_off_t p_offset;
  elf64_addr_t p_vaddr, p_paddr;
  elf64_xword_t p_filesz, p_memsz, p_align;
};




static uint8_t native_elf_abi =
#ifdef __linux__
  GIMLI_ELFOSABI_LINUX
#elif defined(sun)
  GIMLI_ELFOSABI_SOLARIS
#elif defined(__FreeBSD__)
  GIMLI_ELFOSABI_FREEBSD
#else
# error unsupported OS
#endif
  ;

static uint16_t native_machine =
#ifdef __x86_64__
  GIMLI_EM_X86_64
#elif defined(__sparcv9)
  GIMLI_EM_SPARCV9
#elif defined(__sparc__)
  GIMLI_EM_SPARC32PLUS
#else
  GIMLI_EM_386
#endif
  ;

static struct gimli_elf_shdr *gimli_get_section_by_index(
  struct gimli_elf_ehdr *elf, int section)
{
  struct gimli_elf_shdr *s;

  STAILQ_FOREACH(s, &elf->sections, shdrs) {
    if (s->section_no == section) {
      return s;
    }
  }
  return NULL;
}

static struct gimli_elf_shdr *gimli_get_elf_section_by_name(struct gimli_elf_ehdr *elf,
  const char *name)
{
  struct gimli_elf_shdr *s = NULL;

  STAILQ_FOREACH(s, &elf->sections, shdrs) {
    if (!strcmp(s->name, name)) {
      return s;
    }
  }
  return NULL;
}

static const char *gimli_get_section_data(struct gimli_elf_ehdr *elf, int section)
{
  struct gimli_elf_shdr *s;
  int i;

  s = gimli_get_section_by_index(elf, section);

  if (!s->data) {
    s->data = malloc(s->sh_size);
    if (!s->data) return NULL;
    if (lseek(s->elf->fd, s->sh_offset, SEEK_SET) != s->sh_offset) {
      fprintf(stderr, "ELF: failed to seek: %s\n", strerror(errno));
      free(s->data);
      s->data = NULL;
      return NULL;
    }
    if (read(s->elf->fd, s->data, s->sh_size) != s->sh_size) {
      fprintf(stderr, "ELF: failed to read: %s\n", strerror(errno));
      free(s->data);
      s->data = NULL;
      return NULL;
    }
  }
  return s->data;
}

struct gimli_section_data *gimli_get_section_by_name(
  gimli_object_file_t elf, const char *name)
{
  struct gimli_section_data *data;
  struct gimli_elf_shdr *shdr;

  if (gimli_hash_find(elf->gobject->sections, name, (void**)&data)) {
    return data;
  }
  shdr = gimli_get_elf_section_by_name(elf, name);
  if (!shdr) return NULL;
  data = calloc(1, sizeof(*data));
  data->data = (uint8_t*)gimli_get_section_data(elf, shdr->section_no);
  data->size = shdr->sh_size;
  data->offset = shdr->sh_offset;
  data->addr = shdr->sh_addr;
  data->name = strdup(name);
  data->container = elf;
  gimli_hash_insert(elf->gobject->sections, name, data);
  return data;
}


static const char *gimli_elf_get_string(struct gimli_elf_ehdr *elf,
  int section, uint64_t off)
{
  const char *d = gimli_get_section_data(elf, section);
  if (d) {
    return d + off;
  }
  return NULL;
}

void gimli_object_file_destroy(struct gimli_elf_ehdr *elf)
{
  struct gimli_elf_shdr *s;

  while (STAILQ_FIRST(&elf->sections)) {
    s = STAILQ_FIRST(&elf->sections);
    STAILQ_REMOVE_HEAD(&elf->sections, shdrs);

    free(s->data);
    free(s);
  }

  if (elf->fd >= 0) {
    close(elf->fd);
  }
  free(elf->objname);
  free(elf);
}

struct gimli_elf_ehdr *gimli_elf_open(const char *filename)
{
  struct gimli_elf_ehdr *elf = calloc(1, sizeof(*elf));
  unsigned char ident[16];
  int i;
  struct gimli_elf_shdr *s;

  elf->fd = open(filename, O_RDONLY);
  if (elf->fd == -1) {
    free(elf);
    return 0;
  }

  STAILQ_INIT(&elf->sections);

  read(elf->fd, ident, sizeof(ident));

  if (memcmp(ident, GIMLI_EI_ELF_MAGIC, 4)) {
closeout:
    close(elf->fd);
    free(elf);
    return 0;
  }

  elf->objname = strdup(filename);

  if (ident[GIMLI_EI_VERSION] != GIMLI_EV_CURRENT) {
    fprintf(stderr, "ELF: %s: unsupported ELF version %d\n", filename,
      ident[GIMLI_EI_VERSION]);
    goto closeout;
  }
  if (ident[GIMLI_EI_OSABI] && ident[GIMLI_EI_OSABI] != native_elf_abi) {
    fprintf(stderr, "ELF: %s: unsupported OS ABI %d (expected %d)\n",
      filename, ident[GIMLI_EI_OSABI], native_elf_abi);
    goto closeout;
  }

  /* we only support reading natively encoded ELF objects */
#ifdef __sparc__
  if (ident[GIMLI_EI_DATA] != GIMLI_ELFDATA2MSB) {
    fprintf(stderr, "ELF: %s: expected MSB format on this system\n",
      filename);
    goto closeout;
  }
#else
  if (ident[GIMLI_EI_DATA] != GIMLI_ELFDATA2LSB) {
    fprintf(stderr, "ELF: %s: expected LSB format on this system\n",
      filename);
    goto closeout;
  }
#endif

  elf->ei_class = ident[GIMLI_EI_CLASS];

  if (elf->ei_class == GIMLI_ELFCLASS32) {
    struct elf32_ehdr hdr;

    if (read(elf->fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
      fprintf(stderr, "ELF: %s: error reading EHDR: %s\n",
        filename, strerror(errno));
      goto closeout;
    }
    elf->e_type = hdr.e_type;
    elf->e_machine = hdr.e_machine;
    elf->e_version = hdr.e_version;
    elf->e_entry = hdr.e_entry;
    elf->e_phoff = hdr.e_phoff;
    elf->e_shoff = hdr.e_shoff;
    elf->e_flags = hdr.e_flags;
    elf->e_ehsize = hdr.e_ehsize;
    elf->e_phentsize = hdr.e_phentsize;
    elf->e_phnum = hdr.e_phnum;
    elf->e_shentsize = hdr.e_shentsize;
    elf->e_shnum = hdr.e_shnum;
    elf->e_shstrndx = hdr.e_shstrndx;
  } else {
    struct elf64_ehdr hdr;

    if (read(elf->fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
      fprintf(stderr, "ELF: %s: error reading EHDR: %s\n",
          filename, strerror(errno));
      goto closeout;
    }
    elf->e_type = hdr.e_type;
    elf->e_machine = hdr.e_machine;
    elf->e_version = hdr.e_version;
    elf->e_entry = hdr.e_entry;
    elf->e_phoff = hdr.e_phoff;
    elf->e_shoff = hdr.e_shoff;
    elf->e_flags = hdr.e_flags;
    elf->e_ehsize = hdr.e_ehsize;
    elf->e_phentsize = hdr.e_phentsize;
    elf->e_phnum = hdr.e_phnum;
    elf->e_shentsize = hdr.e_shentsize;
    elf->e_shnum = hdr.e_shnum;
    elf->e_shstrndx = hdr.e_shstrndx;
  }

  if (elf->e_machine != native_machine
#ifdef __sparc__ /* too many variants for simple compares... */
      && elf->e_machine != GIMLI_EM_SPARC
#endif
    ) {
    fprintf(stderr, "ELF: %s: expected e_machine=%d, found %d\n",
      filename, native_machine, elf->e_machine);
    goto closeout;
  }

  if (elf->e_version != GIMLI_EV_CURRENT) {
    fprintf(stderr, "ELF: %s: unsupported ELF version %" PRId64 "\n", filename,
      elf->e_version);
    goto closeout;
  }

  /* run through the section headers, pulling them in */
  for (i = 0; i < elf->e_shnum; i++) {
    s = calloc(1, sizeof(*s));
    s->section_no = i;
    s->elf = elf;
    off_t target = elf->e_shoff + (i * elf->e_shentsize);

    if (lseek(elf->fd, target, SEEK_SET) != target) {
      fprintf(stderr,
        "ELF: %s: failed to seek for section header %d: offset %" PRId64 ": %s\n",
        filename, i, (int64_t)target, strerror(errno));
      return 0;
    }
    if (elf->ei_class == GIMLI_ELFCLASS32) {
      struct elf32_shdr hdr;

      if (read(elf->fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
        fprintf(stderr,
          "ELF: %s: failed to read section header %d: %s\n",
            filename, i, strerror(errno));
        return 0;
      }

      s->sh_name = hdr.sh_name;
      s->sh_type = hdr.sh_type;
      s->sh_flags = hdr.sh_flags;
      s->sh_addr = hdr.sh_addr;
      s->sh_offset = hdr.sh_offset;
      s->sh_size = hdr.sh_size;
      s->sh_link = hdr.sh_link;
      s->sh_info = hdr.sh_info;
      s->sh_addralign = hdr.sh_addralign;
      s->sh_entsize = hdr.sh_entsize;

    } else {
      struct elf64_shdr hdr;

      if (read(elf->fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
        fprintf(stderr,
          "ELF: %s: failed to read section header %d: %s\n",
            filename, i, strerror(errno));
        return 0;
      }

      s->sh_name = hdr.sh_name;
      s->sh_type = hdr.sh_type;
      s->sh_flags = hdr.sh_flags;
      s->sh_addr = hdr.sh_addr;
      s->sh_offset = hdr.sh_offset;
      s->sh_size = hdr.sh_size;
      s->sh_link = hdr.sh_link;
      s->sh_info = hdr.sh_info;
      s->sh_addralign = hdr.sh_addralign;
      s->sh_entsize = hdr.sh_entsize;
    }

    STAILQ_INSERT_TAIL(&elf->sections, s, shdrs);
    if (STAILQ_FIRST(&elf->sections) == s) {
      /* let's fixup the e_shstrndx here.  If it has the value
       * SHN_XINDEX, then the true value is stashed in the sh_link
       * field of the 0th section (that's us) */
      if (elf->e_shstrndx == GIMLI_SHN_XINDEX) {
        elf->e_shstrndx = s->sh_link;
      }
    }
  }
//  printf("e_shstrndx is %d\n", elf->e_shstrndx);
  /* now make a pass through the sections to find out their names */
  STAILQ_FOREACH(s, &elf->sections, shdrs) {
    s->name = (char*)gimli_elf_get_string(elf, elf->e_shstrndx, s->sh_name);
//    printf("Section %d has name [%d] %s\n", s->section_no, s->sh_name, s->name);
  }

  /* now we need to locate the LOAD Program Header, and from that
   * we can deduce the base_address */
  for (i = 0; i < elf->e_phnum; i++) {
    struct elf64_phdr hdr;

    lseek(elf->fd, elf->e_phoff + (i * elf->e_phentsize), SEEK_SET);

    if (elf->ei_class == GIMLI_ELFCLASS32) {
      struct elf32_phdr hdr32;

      if (read(elf->fd, &hdr32, sizeof(hdr32)) != sizeof(hdr32)) {
        fprintf(stderr, "ELF: %s: error reading EHDR: %s\n",
            filename, strerror(errno));
        return 0;
      }
      hdr.p_type = hdr32.p_type;
      hdr.p_flags = hdr32.p_flags;
      hdr.p_offset = hdr32.p_offset;
      hdr.p_vaddr = hdr32.p_vaddr;
      hdr.p_paddr = hdr32.p_paddr;
      hdr.p_filesz = hdr32.p_filesz;
      hdr.p_memsz = hdr32.p_align;

    } else {
      if (read(elf->fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
        fprintf(stderr, "ELF: %s: error reading EHDR: %s\n",
            filename, strerror(errno));
        return 0;
      }
    }

    if (hdr.p_type == GIMLI_PT_LOAD) {
      elf->vaddr = hdr.p_vaddr;
      break;
    }
  }

  return elf;
}

int gimli_elf_enum_symbols(struct gimli_elf_ehdr *elf,
  gimli_elf_sym_iter_func func, void *arg)
{
  struct gimli_elf_shdr *s;
  int matches = 0;
  const char *symtab = NULL;
  const char *end = NULL;

  /* find the symbol table */
  STAILQ_FOREACH(s, &elf->sections, shdrs) {
    if (s->sh_type == GIMLI_SHT_SYMTAB || s->sh_type == GIMLI_SHT_DYNSYM) {
      symtab = gimli_get_section_data(elf, s->section_no);
      if (symtab == NULL) {
        continue;
      }
      end = symtab + s->sh_size;

      for (; symtab < end; symtab += s->sh_entsize) {
        struct gimli_elf_symbol sym;

        memset(&sym, 0, sizeof(sym));
        if (elf->ei_class == GIMLI_ELFCLASS32) {
          struct elf32_sym s;

          memcpy(&s, symtab, sizeof(s));
          sym.st_name = s.st_name;
          sym.st_value = s.st_value;
          sym.st_size = s.st_size;
          sym.st_info = s.st_info;
          sym.st_other = s.st_other;
          sym.st_shndx = s.st_shndx;
        } else {
          struct elf64_sym s;

          memcpy(&s, symtab, sizeof(s));
          sym.st_name = s.st_name;
          sym.st_value = s.st_value;
          sym.st_size = s.st_size;
          sym.st_info = s.st_info;
          sym.st_other = s.st_other;
          sym.st_shndx = s.st_shndx;
        }
        if (sym.st_shndx == GIMLI_SHN_UNDEF) {
          continue;
        }
        if (sym.st_shndx == GIMLI_SHN_XINDEX) {
          printf("Got an xindex here\n");
        }

        sym.name = (char*)gimli_elf_get_string(elf,
                s->sh_link, sym.st_name);

        if (sym.name == NULL) {
          continue;
        }

        if (func(elf, &sym, arg)) {
          matches++;
        }
      }
    }
  }
  return matches;
}

#endif
/* vim:ts=2:sw=2:et:
 */