summaryrefslogtreecommitdiff
path: root/src/modelops/reladd.c
blob: 936c0bd34c3cd9da8337e171720a0834ca74c693 (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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <libelfu/libelfu.h>


static int appendData(ElfuScn *ms, void *buf, size_t len)
{
  void *newbuf;

  assert(ms);
  assert(ms->shdr.sh_type != SHT_NOBITS);
  assert(ms->data.d_buf);

  newbuf = realloc(ms->data.d_buf, ms->shdr.sh_size + len);
  if (!newbuf) {
    ELFU_WARN("appendData: malloc() failed for newbuf.\n");
    return 1;
  }

  ms->data.d_buf = newbuf;
  memcpy(newbuf + ms->shdr.sh_size, buf, len);
  ms->shdr.sh_size += len;
  ms->data.d_size += len;
  assert(ms->shdr.sh_size == ms->data.d_size);

  return 0;
}


static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
{
  ElfuScn *newscn = NULL;
  GElf_Addr injAddr;
  GElf_Off injOffset;
  ElfuPhdr *injPhdr;

  if (oldscn->shdr.sh_flags & SHF_ALLOC) {
    newscn = elfu_mCloneScn(oldscn);
    if (!newscn) {
      return NULL;
    }

    if (newscn->shdr.sh_type == SHT_NOBITS) {
      /* Expand this to SHT_PROGBITS, then insert as such. */

      assert(!newscn->data.d_buf);

      newscn->data.d_buf = malloc(newscn->shdr.sh_size);
      if (!newscn->data.d_buf) {
        goto ERROR;
      }
      newscn->data.d_size = newscn->shdr.sh_size;
      newscn->shdr.sh_type = SHT_PROGBITS;
    }

    injAddr = elfu_mLayoutGetSpaceInPhdr(me,
                                         newscn->shdr.sh_size,
                                         newscn->shdr.sh_addralign,
                                         newscn->shdr.sh_flags & SHF_WRITE,
                                         newscn->shdr.sh_flags & SHF_EXECINSTR,
                                         &injPhdr);

    if (!injPhdr) {
      ELFU_WARN("insertSection: Could not find a place to insert section.\n");
      goto ERROR;
    }

    ELFU_INFO("Inserting %s at address 0x%jx...\n",
              elfu_mScnName(mrel, oldscn),
              injAddr);

    injOffset = injAddr - injPhdr->phdr.p_vaddr + injPhdr->phdr.p_offset;

    newscn->shdr.sh_addr = injAddr;
    newscn->shdr.sh_offset = injOffset;

    if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
        || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
      CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
    } else {
      ElfuScn *ms;
      CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
        if (injOffset < ms->shdr.sh_offset) {
          CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
          break;
        }
      }
    }


    /* Inject name */
    if (me->shstrtab) {
      char *newname;
      size_t newnamelen;

      newnamelen = strlen("reladd") + 1;
      if (elfu_mScnName(mrel, oldscn)) {
        newnamelen += strlen(elfu_mScnName(mrel, oldscn));
      }

      newname = malloc(newnamelen);
      strcpy(newname, "reladd");
      strcat(newname, elfu_mScnName(mrel, oldscn));

      if (!newname) {
        ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
        newscn->shdr.sh_name = 0;
      } else {
        size_t offset = me->shstrtab->shdr.sh_size;

        if (!appendData(me->shstrtab, newname, newnamelen)) {
          newscn->shdr.sh_name = offset;
        }

        free(newname);
      }
    }

    return newscn;
  } else {
      ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %jd).\n",
                elfu_mScnName(mrel, oldscn),
                oldscn->shdr.sh_type,
                oldscn->shdr.sh_flags);
      goto ERROR;
  }

  ERROR:
  if (newscn) {
    // TODO: Destroy newscn
  }
  return NULL;
}


static void* subScnAdd1(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
{
  (void)aux2;
  ElfuElf *me = (ElfuElf*)aux1;

  ElfuScn *newscn;

  switch(ms->shdr.sh_type) {
    case SHT_PROGBITS: /* 1 */
    case SHT_NOBITS: /* 8 */
      /* Ignore empty sections */
      if (ms->shdr.sh_size == 0) {
        break;
      }

      /* Find a place where it belongs and shove it in. */
      newscn = insertSection(me, mrel, ms);
      if (!newscn) {
        ELFU_WARN("mReladd: Could not insert section %s (type %d), skipping.\n",
                  elfu_mScnName(mrel, ms),
                  ms->shdr.sh_type);
      }
      break;
  }

  return NULL;
}


static void* subScnAdd2(ElfuElf *mrel, ElfuScn *ms, void *aux1, void *aux2)
{
  (void)aux2;
  ElfuElf *me = (ElfuElf*)aux1;
  (void)me;

  switch(ms->shdr.sh_type) {
    case SHT_NULL: /* 0 */
    case SHT_PROGBITS: /* 1 */
    case SHT_STRTAB: /* 3 */
    case SHT_NOBITS: /* 8 */
      break;


    case SHT_REL: /* 9 */
      /* Relocate. */
      elfu_mRelocate32(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
      break;

    case SHT_RELA: /* 4 */
      // TODO: Needs a parser
      //elfu_mRelocate(elfu_mScnByOldscn(me, ms->infoptr), ms);

    case SHT_SYMTAB: /* 2 */
      /* Merge with the existing table. Take care of string tables also. */

    /* The next section types either do not occur in .o files, or are
     * not strictly necessary to process here. */
    case SHT_NOTE: /* 7 */
    case SHT_HASH: /* 5 */
    case SHT_DYNAMIC: /* 6 */
    case SHT_SHLIB: /* 10 */
    case SHT_DYNSYM: /* 11 */
    case SHT_INIT_ARRAY: /* 14 */
    case SHT_FINI_ARRAY: /* 15 */
    case SHT_PREINIT_ARRAY: /* 16 */
    case SHT_GROUP: /* 17 */
    case SHT_SYMTAB_SHNDX: /* 18 */
    case SHT_NUM: /* 19 */
    default:
      ELFU_WARN("mReladd: Skipping section %s (type %d).\n",
                elfu_mScnName(mrel, ms),
                ms->shdr.sh_type);
  }

  return NULL;
}



static void insertSymClone(ElfuElf *me, const ElfuScn *oldmsst, const ElfuSym *oldsym)
{
  GElf_Xword newsize;
  void *newbuf;
  ElfuScn *newscn = NULL;
  ElfuSym *newsym;
  char *oldsymname;

  assert(me);
  assert(oldmsst);
  assert(oldsym);

  /* If the old symbol pointed to a section, find its clone in the target */
  if (oldsym->scnptr) {
    newscn = elfu_mScnByOldscn(me, oldsym->scnptr);

    /* If we didn't copy the section referenced, we won't
     * copy this symbol either */
    if (!newscn) {
      return;
    }
  }

  // TODO: Allocate symtab if none present
  assert(me->symtab);

  /* Allocate memory for the cloned symbol */
  newsym = malloc(sizeof(*newsym));
  if (!newsym) {
    ELFU_WARN("insertSymClone: malloc() failed for newsym.\n");
    goto ERROR;
  }

  oldsymname = ELFU_SYMSTR(oldmsst, oldsym->name);

  /* Expand .strtab, append symbol name, link newsym to it */
  newsize = me->symtab->linkptr->shdr.sh_size + strlen(oldsymname) + 1;
  newbuf = realloc(me->symtab->linkptr->data.d_buf, newsize);
  if (!newbuf) {
    ELFU_WARN("insertSymClone: realloc() failed for strtab.\n");
    goto ERROR;
  }

  me->symtab->linkptr->data.d_buf = newbuf;

  newsym->name = me->symtab->linkptr->shdr.sh_size;

  strcpy(newbuf + newsym->name, oldsymname);

  me->symtab->linkptr->data.d_size = newsize;
  me->symtab->linkptr->shdr.sh_size = newsize;


  /* Copy all other fields */
  newsym->scnptr = newscn;
  newsym->shndx = oldsym->shndx; /* If scnptr == NULL, this becomes relevant */
  newsym->bind = oldsym->bind;
  newsym->other = oldsym->other;
  newsym->size = oldsym->size;
  newsym->type = oldsym->type;
  newsym->value = oldsym->value;

  /* In executables, symbol addresses need to be in memory */
  if (newscn) {
    newsym->value += newscn->shdr.sh_addr;
  }

  /* Insert symbol */
  CIRCLEQ_INSERT_TAIL(&me->symtab->symtab.syms, newsym, elem);

  return;

  ERROR:
  if (newsym) {
    free(newsym);
  }
}

static void mergeSymtab(ElfuElf *me, const ElfuElf *mrel)
{
  ElfuSym *sym;

  assert(me);
  assert(mrel);

  CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
    insertSymClone(me, mrel->symtab, sym);
  }
}



void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
{
  assert(me);
  assert(mrel);

  /* For each section in object file, guess how to insert it */
  elfu_mScnForall((ElfuElf*)mrel, subScnAdd1, me, NULL);

  mergeSymtab(me, mrel);

  /* Do relocations and other stuff */
  elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL);

  /* Re-layout to accommodate new contents */
  elfu_mLayoutAuto(me);
}