summaryrefslogtreecommitdiff
path: root/src/libelfu/modelops/fromFile.c
blob: a79a66660db0427cdd318ca12abcdbff314d48d4 (plain)
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
/* This file is part of centaur.
 *
 * centaur is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License 2 as
 * published by the Free Software Foundation.

 * centaur is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <libelfu/libelfu.h>


static void parseSymtab(ElfuElf *me, ElfuScn *ms, ElfuScn**origScnArr)
{
  ElfuSym *sym;
  size_t i;

  assert(ms);
  assert(ms->databuf);
  assert(origScnArr);

  /* Parse symbols from their elfclass-specific format */
  if (me->elfclass == ELFCLASS32) {
    for (i = 1; (i + 1) * sizeof(Elf32_Sym) <= ms->shdr.sh_size; i++) {
      Elf32_Sym *cursym = ((Elf32_Sym*)ms->databuf) + i;
      ElfuSym *newsym = malloc(sizeof(*sym));
      assert(newsym);

      newsym->name = cursym->st_name;
      newsym->value = cursym->st_value;
      newsym->size = cursym->st_size;
      newsym->bind = ELF32_ST_BIND(cursym->st_info);
      newsym->type = ELF32_ST_TYPE(cursym->st_info);
      newsym->other = cursym->st_other;
      newsym->shndx = cursym->st_shndx;

      CIRCLEQ_INSERT_TAIL(&ms->symtab.syms, newsym, elem);
    }
  } else if (me->elfclass == ELFCLASS64) {
    for (i = 1; (i + 1) * sizeof(Elf64_Sym) <= ms->shdr.sh_size; i++) {
      Elf64_Sym *cursym = ((Elf64_Sym*)ms->databuf) + i;
      ElfuSym *newsym = malloc(sizeof(*sym));
      assert(newsym);

      newsym->name = cursym->st_name;
      newsym->value = cursym->st_value;
      newsym->size = cursym->st_size;
      newsym->bind = ELF64_ST_BIND(cursym->st_info);
      newsym->type = ELF64_ST_TYPE(cursym->st_info);
      newsym->other = cursym->st_other;
      newsym->shndx = cursym->st_shndx;

      CIRCLEQ_INSERT_TAIL(&ms->symtab.syms, newsym, elem);
    }
  } else {
    /* Unknown elfclass */
    assert(0);
  }

  /* For each section, find the section it points to if any. */
  CIRCLEQ_FOREACH(sym, &ms->symtab.syms, elem) {
    switch (sym->shndx) {
      case SHN_UNDEF:
      case SHN_ABS:
      case SHN_COMMON:
        sym->scnptr = NULL;
        break;
      default:
        sym->scnptr = origScnArr[sym->shndx - 1];
        break;
    }
  }
}


static void parseReltab(ElfuElf *me, ElfuScn *ms)
{
  size_t i;

  assert(ms);
  assert(ms->databuf);

  if (me->elfclass == ELFCLASS32) {
    for (i = 0; (i + 1) * sizeof(Elf32_Rel) <= ms->shdr.sh_size; i++) {
      Elf32_Rel *currel = &(((Elf32_Rel*)ms->databuf)[i]);
      ElfuRel *rel;

      rel = malloc(sizeof(*rel));
      assert(rel);

      rel->offset = currel->r_offset;
      rel->sym = ELF32_R_SYM(currel->r_info);
      rel->type = ELF32_R_TYPE(currel->r_info);
      rel->addendUsed = 0;
      rel->addend = 0;

      CIRCLEQ_INSERT_TAIL(&ms->reltab.rels, rel, elem);
    }
  } else if (me->elfclass == ELFCLASS64) {
    for (i = 0; (i + 1) * sizeof(Elf64_Rel) <= ms->shdr.sh_size; i++) {
      Elf64_Rel *currel = &(((Elf64_Rel*)ms->databuf)[i]);
      ElfuRel *rel;

      rel = malloc(sizeof(*rel));
      assert(rel);

      rel->offset = currel->r_offset;
      rel->sym = ELF64_R_SYM(currel->r_info);
      rel->type = ELF64_R_TYPE(currel->r_info);
      rel->addendUsed = 0;
      rel->addend = 0;

      CIRCLEQ_INSERT_TAIL(&ms->reltab.rels, rel, elem);
    }
  } else {
    /* Unknown elfclass */
    assert(0);
  }
}


static void parseRelatab(ElfuElf *me, ElfuScn *ms)
{
  size_t i;

  assert(ms);
  assert(ms->databuf);

  if (me->elfclass == ELFCLASS32) {
    for (i = 0; (i + 1) * sizeof(Elf32_Rela) <= ms->shdr.sh_size; i++) {
      Elf32_Rela *currel = &(((Elf32_Rela*)ms->databuf)[i]);
      ElfuRel *rel;

      rel = malloc(sizeof(*rel));
      assert(rel);

      rel->offset = currel->r_offset;
      rel->sym = ELF32_R_SYM(currel->r_info);
      rel->type = ELF32_R_TYPE(currel->r_info);
      rel->addendUsed = 1;
      rel->addend = currel->r_addend;

      CIRCLEQ_INSERT_TAIL(&ms->reltab.rels, rel, elem);
    }
  } else if (me->elfclass == ELFCLASS64) {
    for (i = 0; (i + 1) * sizeof(Elf64_Rela) <= ms->shdr.sh_size; i++) {
      Elf64_Rela *currel = &(((Elf64_Rela*)ms->databuf)[i]);
      ElfuRel *rel;

      rel = malloc(sizeof(*rel));
      assert(rel);

      rel->offset = currel->r_offset;
      rel->sym = ELF64_R_SYM(currel->r_info);
      rel->type = ELF64_R_TYPE(currel->r_info);
      rel->addendUsed = 1;
      rel->addend = currel->r_addend;

      CIRCLEQ_INSERT_TAIL(&ms->reltab.rels, rel, elem);
    }
  } else {
    /* Unknown elfclass */
    assert(0);
  }
}


static int cmpScnOffs(const void *ms1, const void *ms2)
{
  ElfuScn *s1 = *(ElfuScn**)ms1;
  ElfuScn *s2 = *(ElfuScn**)ms2;

  assert(ms1);
  assert(ms2);

  s1 = *(ElfuScn**)ms1;
  s2 = *(ElfuScn**)ms2;

  assert(s1);
  assert(s2);


  if (s1->shdr.sh_offset < s2->shdr.sh_offset) {
    return -1;
  } else if (s1->shdr.sh_offset == s2->shdr.sh_offset) {
    return 0;
  } else /* if (s1->shdr.sh_offset > s2->shdr.sh_offset) */ {
    return 1;
  }
}



static ElfuPhdr* parentPhdr(ElfuElf *me, ElfuScn *ms)
{
  ElfuPhdr *mp;

  assert(me);
  assert(ms);

  CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
    if (mp->phdr.p_type != PT_LOAD) {
      continue;
    }

    if (PHDR_CONTAINS_SCN_IN_MEMORY(&mp->phdr, &ms->shdr)) {
      return mp;
    }
  }

  return NULL;
}


static ElfuPhdr* modelFromPhdr(GElf_Phdr *phdr)
{
  ElfuPhdr *mp;

  assert(phdr);

  mp = elfu_mPhdrAlloc();
  if (!mp) {
    return NULL;
  }

  mp->phdr = *phdr;

  return mp;
}


static ElfuScn* modelFromSection(Elf_Scn *scn)
{
  ElfuScn *ms;

  assert(scn);

  ms = elfu_mScnAlloc();
  if (!ms) {
    goto ERROR;
  }

  /* Copy SHDR */
  assert(gelf_getshdr(scn, &ms->shdr) == &ms->shdr);

  /* Copy each data part in source segment */
  if (SCNFILESIZE(&ms->shdr) > 0) {
    Elf_Data *data;

    ms->databuf = malloc(ms->shdr.sh_size);
    if (!ms->databuf) {
      ELFU_WARN("modelFromSection: malloc() failed for data buffer (%x bytes).\n", (unsigned)ms->shdr.sh_size);
      goto ERROR;
    }

    /* A non-empty section should contain at least one data block. */
    data = elf_rawdata(scn, NULL);
    assert(data);

    while (data) {
      if (data->d_off + data->d_size > ms->shdr.sh_size) {
        ELFU_WARN("modelFromSection: libelf delivered a bogus data blob. Skipping\n");
      } else {
        memcpy((char*)ms->databuf + data->d_off, data->d_buf, data->d_size);
      }

      data = elf_rawdata(scn, data);
    }
  }

  return ms;

  ERROR:
  if (ms) {
    free(ms);
  }
  return NULL;
}




ElfuElf* elfu_mFromElf(Elf *e)
{
  ElfuElf *me = NULL;
  size_t shstrndx;
  size_t i, numPhdr, numShdr;
  ElfuScn **secArray = NULL;

  assert(e);
  if (elfu_eCheck(e)) {
    goto ERROR;
  }

  me = elfu_mElfAlloc();
  if (!me) {
    goto ERROR;
  }

  me->elfclass = gelf_getclass(e);
  assert(me->elfclass != ELFCLASSNONE);
  assert(gelf_getehdr(e, &me->ehdr) == &me->ehdr);


  /* Get the section string table index */
  if (elf_getshdrstrndx(e, &shstrndx) != 0) {
    shstrndx = 0;
  }


  /* Load segments */
  assert(!elf_getphdrnum(e, &numPhdr));
  for (i = 0; i < numPhdr; i++) {
    GElf_Phdr phdr;
    ElfuPhdr *mp;

    assert(gelf_getphdr(e, i, &phdr) == &phdr);

    mp = modelFromPhdr(&phdr);
    if (!mp) {
      goto ERROR;
    }

    CIRCLEQ_INSERT_TAIL(&me->phdrList, mp, elem);
  }

  if (numPhdr > 0) {
    ElfuPhdr *mp;

    /* Find PHDR -> PHDR dependencies (needs sorted sections) */
    CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
      ElfuPhdr *mp2;

      if (mp->phdr.p_type != PT_LOAD) {
        continue;
      }

      CIRCLEQ_FOREACH(mp2, &me->phdrList, elem) {
        if (mp2 == mp) {
          continue;
        }

        if (mp->phdr.p_vaddr <= mp2->phdr.p_vaddr
            && OFFS_END(mp2->phdr.p_vaddr, mp2->phdr.p_memsz) <= OFFS_END(mp->phdr.p_vaddr, mp->phdr.p_memsz)) {
          /* Remove from the main list so only LOADs remain there */
          CIRCLEQ_REMOVE(&me->phdrList, mp2, elem);
          CIRCLEQ_INSERT_TAIL(&mp->childPhdrList, mp2, elemChildPhdr);
        }
      }
    }
  }


  /* Load sections */
  assert(!elf_getshdrnum(e, &numShdr));
  if (numShdr > 1) {
    secArray = malloc((numShdr - 1) * sizeof(*secArray));
    if (!secArray) {
      ELFU_WARN("elfu_mFromElf: malloc() failed for secArray.\n");
      goto ERROR;
    }

    for (i = 1; i < numShdr; i++) {
      Elf_Scn *scn;
      ElfuScn *ms;

      scn = elf_getscn(e, i);
      assert(scn);

      ms = modelFromSection(scn);
      if (!ms) {
        goto ERROR;
      }

      secArray[i-1] =  ms;

      if (i == shstrndx) {
        me->shstrtab = ms;
      }
    }


    /* Find sh_link and sh_info dependencies (needs sections in original order) */
    for (i = 0; i < numShdr - 1; i++) {
      ElfuScn *ms = secArray[i];

      switch (ms->shdr.sh_type) {
        case SHT_REL:
        case SHT_RELA:
          if (ms->shdr.sh_info > 0) {
            ms->infoptr = secArray[ms->shdr.sh_info - 1];
          }
        case SHT_DYNAMIC:
        case SHT_HASH:
        case SHT_SYMTAB:
        case SHT_DYNSYM:
        case SHT_GNU_versym:
        case SHT_GNU_verdef:
        case SHT_GNU_verneed:
          if (ms->shdr.sh_link > 0) {
            ms->linkptr = secArray[ms->shdr.sh_link - 1];
          }
      }
    }


    /* Parse symtabs (needs sections in original order) */
    for (i = 0; i < numShdr - 1; i++) {
      ElfuScn *ms = secArray[i];

      switch (ms->shdr.sh_type) {
        case SHT_SYMTAB:
          me->symtab = ms;
        case SHT_DYNSYM:
          parseSymtab(me, ms, secArray);
          break;
      }
    }


    /* Parse relocations (needs sections in original order) */
    for (i = 0; i < numShdr - 1; i++) {
      ElfuScn *ms = secArray[i];

      switch (ms->shdr.sh_type) {
        case SHT_REL:
          parseReltab(me, ms);
          break;
        case SHT_RELA:
          parseRelatab(me, ms);
          break;
      }
    }


    /* Sort sections by file offset */
    qsort(secArray, numShdr - 1, sizeof(*secArray), cmpScnOffs);


    /* Find PHDR -> Section dependencies (needs sorted sections) */
    for (i = 0; i < numShdr - 1; i++) {
      ElfuScn *ms = secArray[i];

      ElfuPhdr *parent = parentPhdr(me, ms);

      if (parent) {
        GElf_Off shaddr = parent->phdr.p_vaddr +
                         (ms->shdr.sh_offset - parent->phdr.p_offset);

        if (ms->shdr.sh_addr == 0) {
          ms->shdr.sh_addr = shaddr;
        } else {
          if (ms->shdr.sh_type != SHT_NOBITS) {
            assert(ms->shdr.sh_addr == shaddr);
          } else if (ms->shdr.sh_addr > shaddr) {
            parent->phdr.p_filesz = MAX(parent->phdr.p_filesz,
                                        ms->shdr.sh_addr - parent->phdr.p_vaddr);
          }
        }

        CIRCLEQ_INSERT_TAIL(&parent->childScnList, ms, elemChildScn);
      } else {
        CIRCLEQ_INSERT_TAIL(&me->orphanScnList, ms, elemChildScn);
      }
    }
  }

  if (secArray) {
    free(secArray);
  }
  return me;


  ERROR:
  if (secArray) {
    free(secArray);
  }
  if (me) {
    elfu_mElfDestroy(me);
  }

  ELFU_WARN("elfu_mFromElf: Failed to load file.\n");
  return NULL;
}