summaryrefslogtreecommitdiff
path: root/src/libelfu/modelops/layout.c
blob: f8a30ed4e2d66b4a40eebbfd45fb70e77abeeb84 (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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <libelfu/libelfu.h>



static GElf_Word shiftStuffAtAfterOffset(ElfuElf *me,
                                         GElf_Off offset,
                                         GElf_Word size)
{
  ElfuPhdr *mp;
  ElfuScn *ms;
  /* Force a minimum alignment, just to be sure. */
  GElf_Word align = 64;

  /* Find maximum alignment size by which we have to shift.
   * Assumes alignment sizes are always 2^x. */
  CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
    if (mp->phdr.p_offset >= offset) {
      if (mp->phdr.p_align > align) {
        align = mp->phdr.p_align;
      }
    }
  }

  CIRCLEQ_FOREACH(ms, &me->orphanScnList, elemChildScn) {
    if (ms->shdr.sh_offset >= offset) {
      if (ms->shdr.sh_addralign > align) {
        align = ms->shdr.sh_addralign;
      }
    }
  }

  size = ROUNDUP(size, align);

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

    if (mp->phdr.p_offset >= offset) {
      mp->phdr.p_offset += size;

      elfu_mPhdrUpdateChildOffsets(mp);
    }
  }

  CIRCLEQ_FOREACH(ms, &me->orphanScnList, elemChildScn) {
    if (ms->shdr.sh_offset >= offset) {
      ms->shdr.sh_offset += size;
    }
  }

  if (me->ehdr.e_phoff >= offset) {
    me->ehdr.e_phoff += size;
  }

  if (me->ehdr.e_shoff >= offset) {
    me->ehdr.e_shoff += size;
  }

  return size;
}





static ElfuPhdr* appendPhdr(ElfuElf *me)
{
  ElfuPhdr *lowestAddr;
  ElfuPhdr *highestAddr;
  ElfuPhdr *lowestOffs;
  ElfuPhdr *highestOffsEnd;
  ElfuPhdr *phdrmp;
  ElfuPhdr *newmp;

  ELFU_DEBUG("Appending new PHDR\n");

  /* See if we have enough space for more PHDRs. If not, expand
   * the PHDR they are in. */
  phdrmp = elfu_mPhdrByOffset(me, me->ehdr.e_phoff);
  if (!phdrmp) {
    /* No LOAD maps PHDRs into memory. Let re-layouter move them. */
  } else {
    GElf_Off phdr_maxsz = OFFS_END(phdrmp->phdr.p_offset, phdrmp->phdr.p_filesz);
    ElfuScn *firstms = CIRCLEQ_FIRST(&phdrmp->childScnList);

    /* How much can the PHDR table expand within its LOAD segment? */
    if (firstms) {
      phdr_maxsz = MIN(firstms->shdr.sh_offset, phdr_maxsz);
    }
    phdr_maxsz -= me->ehdr.e_phoff;

    /* If we don't have enough space, try to make some by expanding
     * the LOAD segment we are in. There is no other way.
     * Also, we can only expand if it is the first LOAD PHDR. */
    elfu_mPhdrLoadLowestHighest(me, &lowestAddr, &highestAddr,
                                &lowestOffs, &highestOffsEnd);
    if (phdr_maxsz < (me->ehdr.e_phnum + 1) * me->ehdr.e_phentsize
        && phdrmp == lowestAddr
        && phdrmp == lowestOffs) {
      ElfuPhdr *mp;
      ElfuScn *ms;
      GElf_Word size = ROUNDUP(me->ehdr.e_phentsize, phdrmp->phdr.p_align);

      /* Move our sections */
      CIRCLEQ_FOREACH(ms, &phdrmp->childScnList, elemChildScn) {
        if (ms->shdr.sh_offset >= me->ehdr.e_phoff) {
          ms->shdr.sh_offset += size;
        }
      }

      /* Move our PHDRs */
      CIRCLEQ_FOREACH(mp, &phdrmp->childPhdrList, elemChildPhdr) {
        if (mp->phdr.p_offset > me->ehdr.e_phoff) {
          mp->phdr.p_offset += size;
        } else {
          mp->phdr.p_vaddr -= size;
          mp->phdr.p_paddr -= size;
        }

        if (mp->phdr.p_type == PT_PHDR) {
          mp->phdr.p_filesz += me->ehdr.e_phentsize;
          mp->phdr.p_memsz += me->ehdr.e_phentsize;
        }
      }

      /* Move other PHDRs and sections */
      assert(size <= shiftStuffAtAfterOffset(me, me->ehdr.e_phoff + 1, size));

      /* Remap ourselves */
      phdrmp->phdr.p_vaddr -= size;
      phdrmp->phdr.p_paddr -= size;
      phdrmp->phdr.p_filesz += size;
      phdrmp->phdr.p_memsz += size;
    }
  }

  newmp = elfu_mPhdrAlloc();
  assert(newmp);
  CIRCLEQ_INSERT_TAIL(&me->phdrList, newmp, elem);

  return newmp;
}





/* Finds a suitable PHDR to insert a hole into and expands it
 * if necessary.
 * Returns memory address the hole will be mapped to, or 0 if
 * the operation failed. */
GElf_Addr elfu_mLayoutGetSpaceInPhdr(ElfuElf *me, GElf_Word size,
                                     GElf_Word align, int w, int x,
                                     ElfuPhdr **injPhdr)
{
  ElfuPhdr *lowestAddr;
  ElfuPhdr *highestAddr;
  ElfuPhdr *lowestOffs;
  ElfuPhdr *highestOffsEnd;
  ElfuPhdr *mp;

  assert(!(w && x));

  /* Treat read-only data as executable.
   * That's what the GNU toolchain does on x86. */
  if (!w && !x) {
    x = 1;
  }

  /* Find first and last LOAD PHDRs. */
  elfu_mPhdrLoadLowestHighest(me, &lowestAddr, &highestAddr,
                              &lowestOffs, &highestOffsEnd);

  if (((w && (highestAddr->phdr.p_flags & PF_W))
      || (x && (highestAddr->phdr.p_flags & PF_X)))
      /* Merging only works if the LOAD is the last both in file and mem */
      && highestAddr == highestOffsEnd) {
    /* Need to append. */
    GElf_Off injOffset = OFFS_END(highestAddr->phdr.p_offset, highestAddr->phdr.p_filesz);
    GElf_Word injSpace = 0;
    GElf_Word nobitsize = highestAddr->phdr.p_memsz - highestAddr->phdr.p_filesz;

    /* Expand NOBITS if any */
    if (nobitsize > 0) {
      GElf_Off endOff = OFFS_END(highestAddr->phdr.p_offset, highestAddr->phdr.p_filesz);
      GElf_Off endAddr = OFFS_END(highestAddr->phdr.p_vaddr, highestAddr->phdr.p_filesz);
      ElfuScn *ms;

      ELFU_INFO("Expanding NOBITS at address 0x%x...\n", (unsigned)endAddr);

      CIRCLEQ_FOREACH(ms, &highestAddr->childScnList, elemChildScn) {
        if (ms->shdr.sh_offset == endOff) {
          assert(ms->shdr.sh_type == SHT_NOBITS);
          assert(ms->shdr.sh_size == nobitsize);
          ms->databuf = malloc(ms->shdr.sh_size);
          memset(ms->databuf, '\0', ms->shdr.sh_size);
          if (!ms->databuf) {
            ELFU_WARN("mExpandNobits: Could not allocate %u bytes for NOBITS expansion. Data may be inconsistent.\n",
                      (unsigned)ms->shdr.sh_size);
            assert(0);
            goto ERROR;
          }

          ms->shdr.sh_type = SHT_PROGBITS;
          ms->shdr.sh_addr = endAddr;
        }
      }

      injSpace += shiftStuffAtAfterOffset(me, endOff, nobitsize);
      injSpace -= nobitsize;
      injOffset += nobitsize;
      highestAddr->phdr.p_filesz += nobitsize;
      assert(highestAddr->phdr.p_filesz == highestAddr->phdr.p_memsz);
    }

    /* Calculate how much space we need, taking alignment into account */
    size += ROUNDUP(injOffset, align) - injOffset;

    /* If there is not enough space left, create even more. */
    if (injSpace < size) {
      injSpace += shiftStuffAtAfterOffset(me, injOffset, size - injSpace);
    }
    assert(injSpace >= size);

    /* Remap ourselves */
    highestAddr->phdr.p_filesz += size;
    highestAddr->phdr.p_memsz += size;

    injOffset = ROUNDUP(injOffset, align);

    if (injPhdr) {
      *injPhdr = highestAddr;
    }
    return highestAddr->phdr.p_vaddr + (injOffset - highestAddr->phdr.p_offset);
  } else if (((w && (lowestAddr->phdr.p_flags & PF_W))
              || (x && (lowestAddr->phdr.p_flags & PF_X)))
             && /* Enough space to expand downwards? */
             (lowestAddr->phdr.p_vaddr >= ((2 * lowestAddr->phdr.p_align)
                                           + ROUNDUP(size, lowestAddr->phdr.p_align)))
             /* Merging only works if the LOAD is the first both in file and mem */
             && lowestAddr == lowestOffs) {
    /* Need to prepend or split up the PHDR. */
    GElf_Off injOffset = OFFS_END(lowestAddr->phdr.p_offset,
                                  lowestAddr->phdr.p_filesz);
    ElfuScn *ms;

    /* Round up size to take PHDR alignment into account.
     * We assume that this is a multiple of the alignment asked for. */
    assert(lowestAddr->phdr.p_align >= align);
    size = ROUNDUP(size, lowestAddr->phdr.p_align);

    /* Find first section. We assume there is at least one. */
    assert(!CIRCLEQ_EMPTY(&lowestAddr->childScnList));
    injOffset = CIRCLEQ_FIRST(&lowestAddr->childScnList)->shdr.sh_offset;

    /* Move our sections */
    CIRCLEQ_FOREACH(ms, &lowestAddr->childScnList, elemChildScn) {
      if (ms->shdr.sh_offset >= injOffset) {
        ms->shdr.sh_offset += size;
      }
    }

    /* Move our PHDRs */
    CIRCLEQ_FOREACH(mp, &lowestAddr->childPhdrList, elemChildPhdr) {
      if (mp->phdr.p_offset >= injOffset) {
        mp->phdr.p_offset += size;
      } else {
        mp->phdr.p_vaddr -= size;
        mp->phdr.p_paddr -= size;
      }
    }

    /* Move other PHDRs and sections */
    assert(size <= shiftStuffAtAfterOffset(me, injOffset + 1, size));

    /* Remap ourselves */
    lowestAddr->phdr.p_vaddr -= size;
    lowestAddr->phdr.p_paddr -= size;
    lowestAddr->phdr.p_filesz += size;
    lowestAddr->phdr.p_memsz += size;

    injOffset = ROUNDUP(injOffset, align);

    if (injPhdr) {
      *injPhdr = lowestAddr;
    }
    return lowestAddr->phdr.p_vaddr + (injOffset - lowestAddr->phdr.p_offset);
  } else {
    ElfuPhdr *newmp;
    GElf_Off injOffset;
    GElf_Addr injAddr;

    /* Add a new LOAD PHDR. */
    newmp = appendPhdr(me);
    if (!newmp) {
      goto ERROR;
    }

    /* ELF spec: We need (p_offset % p_align) == (p_vaddr % p_align) */
    injOffset = OFFS_END(highestOffsEnd->phdr.p_offset, highestOffsEnd->phdr.p_filesz);
    injOffset = ROUNDUP(injOffset, align);
    injAddr = OFFS_END(highestAddr->phdr.p_vaddr, highestAddr->phdr.p_memsz);
    injAddr = ROUNDUP(injAddr, highestAddr->phdr.p_align);
    injAddr += injOffset % highestAddr->phdr.p_align;

    newmp->phdr.p_align = highestAddr->phdr.p_align;
    newmp->phdr.p_filesz = size;
    newmp->phdr.p_memsz = size;
    newmp->phdr.p_flags = PF_R | (x ? PF_X : 0) | (w ? PF_W : 0);
    newmp->phdr.p_type = PT_LOAD;
    newmp->phdr.p_offset = injOffset;
    newmp->phdr.p_vaddr = injAddr;
    newmp->phdr.p_paddr = newmp->phdr.p_vaddr;

    *injPhdr = newmp;

    return injAddr;
  }




  ERROR:
  if (injPhdr) {
    *injPhdr = NULL;
  }
  return 0;
}





int elfu_mLayoutAuto(ElfuElf *me)
{
  ElfuPhdr *lowestAddr;
  ElfuPhdr *highestAddr;
  ElfuPhdr *lowestOffs;
  ElfuPhdr *highestOffsEnd;
  ElfuPhdr *mp;
  ElfuScn *ms;
  ElfuPhdr **phdrArr;
  GElf_Off lastend = 0;

  assert(me);

  /* Find first and last LOAD PHDRs. */
  elfu_mPhdrLoadLowestHighest(me, &lowestAddr, &highestAddr,
                              &lowestOffs, &highestOffsEnd);

  phdrArr = malloc(elfu_mPhdrCount(me) * sizeof(*phdrArr));
  if (!phdrArr) {
    ELFU_WARN("elfu_mLayoutAuto: malloc failed for phdrArr.\n");
    return 1;
  }


  lastend = OFFS_END(highestOffsEnd->phdr.p_offset, highestOffsEnd->phdr.p_filesz);


  /* If PHDRs are not mapped into memory, place them after LOAD segments. */
  mp = elfu_mPhdrByOffset(me, me->ehdr.e_phoff);
  if (!mp) {
    lastend = ROUNDUP(lastend, 8);
    me->ehdr.e_phoff = lastend;
    lastend += me->ehdr.e_phnum * me->ehdr.e_phentsize;
  } else {
    /* Update size of PHDR PHDR */
    ElfuPhdr *phdrmp;

    CIRCLEQ_FOREACH(phdrmp, &mp->childPhdrList, elemChildPhdr) {
      if (phdrmp->phdr.p_type == PT_PHDR) {
        phdrmp->phdr.p_filesz = elfu_mPhdrCount(me) * me->ehdr.e_phentsize;
        phdrmp->phdr.p_memsz = elfu_mPhdrCount(me) * me->ehdr.e_phentsize;
      }
    }
  }


  /* Place orphaned sections afterwards, maintaining alignment */
  CIRCLEQ_FOREACH(ms, &me->orphanScnList, elemChildScn) {
    lastend = ROUNDUP(lastend, ms->shdr.sh_addralign);

    ms->shdr.sh_offset = lastend;

    lastend = OFFS_END(ms->shdr.sh_offset, SCNFILESIZE(&ms->shdr));
  }


  /* Move SHDRs to end */
  lastend = ROUNDUP(lastend, 8);
  me->ehdr.e_shoff = lastend;


  return 0;
}