2 * Copyright (c) 2007-2010,2012 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/queue.h>
37 ELFTC_VCSID("$Id: segments.c 3615 2018-05-17 04:12:24Z kaiwang27 $");
39 static void insert_to_inseg_list(struct segment *seg, struct section *sec);
42 * elfcopy's segment handling is relatively simpler and less powerful than
43 * libbfd. Program headers are modified or copied from input to output objects,
44 * but never re-generated. As a result, if the input object has incorrect
45 * program headers, the output object's program headers will remain incorrect
46 * or become even worse.
50 * Check whether a section is "loadable". If so, add it to the
51 * corresponding segment list(s) and return 1.
54 add_to_inseg_list(struct elfcopy *ecp, struct section *s)
63 * Segment is a different view of an ELF object. One segment can
64 * contain one or more sections, and one section can be included
65 * in one or more segments, or not included in any segment at all.
66 * We call those sections which can be found in one or more segments
67 * "loadable" sections, and call the rest "unloadable" sections.
68 * We keep track of "loadable" sections in their containing
69 * segment(s)' v_sec queue. These information are later used to
70 * recalculate the extents of segments, when sections are removed,
74 STAILQ_FOREACH(seg, &ecp->v_seg, seg_list) {
75 if (s->off < seg->off || (s->vma < seg->vaddr && !s->pseudo))
77 if (s->off + s->sz > seg->off + seg->fsz &&
78 s->type != SHT_NOBITS)
80 if (s->vma + s->sz > seg->vaddr + seg->msz)
82 if (seg->type == PT_TLS && ((s->flags & SHF_TLS) == 0))
85 insert_to_inseg_list(seg, s);
86 if (seg->type == PT_LOAD)
88 else if (seg->type == PT_TLS)
91 s->vma = seg->vaddr + (s->off - seg->off);
93 s->lma = seg->paddr + (s->off - seg->off);
103 adjust_addr(struct elfcopy *ecp)
105 struct section *s, *s0;
107 struct sec_action *sac;
108 uint64_t dl, vma, lma, start, end;
112 * Apply VMA and global LMA changes in the first iteration.
114 TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
116 /* Only adjust loadable section's address. */
120 /* Apply global VMA adjustment. */
121 if (ecp->change_addr != 0)
122 s->vma += ecp->change_addr;
124 /* Apply global LMA adjustment. */
125 if (ecp->change_addr != 0 && s->seg != NULL &&
127 s->lma += ecp->change_addr;
131 * Apply sections VMA change in the second iteration.
133 TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
139 * Check if there is a VMA change request for this
142 sac = lookup_sec_act(ecp, s->name, 0);
148 if (sac->vma_adjust != 0)
149 vma += sac->vma_adjust;
154 * No need to make segment adjustment if the section doesn't
155 * belong to any segment.
157 if (s->seg == NULL) {
163 * Check if the VMA change is viable.
165 * 1. Check if the new VMA is properly aligned accroding to
168 * 2. Compute the new extent of segment that contains this
169 * section, make sure it doesn't overlap with other
173 printf("VMA for section %s: %#jx\n", s->name, vma);
176 if (vma % s->align != 0)
177 errx(EXIT_FAILURE, "The VMA %#jx for "
178 "section %s is not aligned to %ju",
179 (uintmax_t) vma, s->name, (uintmax_t) s->align);
182 /* Move section to lower address. */
183 if (vma < s->vma - s->seg->vaddr)
184 errx(EXIT_FAILURE, "Not enough space to move "
185 "section %s VMA to %#jx", s->name,
187 start = vma - (s->vma - s->seg->vaddr);
188 if (s == s->seg->v_sec[s->seg->nsec - 1])
189 end = start + s->seg->msz;
191 end = s->seg->vaddr + s->seg->msz;
193 /* Move section to upper address. */
194 if (s == s->seg->v_sec[0])
197 start = s->seg->vaddr;
198 end = vma + (s->seg->vaddr + s->seg->msz - s->vma);
200 errx(EXIT_FAILURE, "Not enough space to move "
201 "section %s VMA to %#jx", s->name,
206 printf("new extent for segment containing %s: (%#jx,%#jx)\n",
207 s->name, start, end);
210 STAILQ_FOREACH(seg, &ecp->v_seg, seg_list) {
211 if (seg == s->seg || seg->type != PT_LOAD)
213 if (start > seg->vaddr + seg->msz)
215 if (end < seg->vaddr)
217 errx(EXIT_FAILURE, "The extent of segment containing "
218 "section %s overlaps with segment(%#jx,%#jx)",
219 s->name, (uintmax_t) seg->vaddr,
220 (uintmax_t) (seg->vaddr + seg->msz));
224 * Update section VMA and file offset.
229 * To move a section to lower VMA, we decrease
230 * the VMA of the section and all the sections that
231 * are before it, and we increase the file offsets
232 * of all the sections that are after it.
235 for (i = 0; i < s->seg->nsec; i++) {
236 s0 = s->seg->v_sec[i];
239 printf("section %s VMA set to %#jx\n",
240 s0->name, (uintmax_t) s0->vma);
245 for (i = i + 1; i < s->seg->nsec; i++) {
246 s0 = s->seg->v_sec[i];
249 printf("section %s offset set to %#jx\n",
250 s0->name, (uintmax_t) s0->off);
255 * To move a section to upper VMA, we increase
256 * the VMA of the section and all the sections that
257 * are after it, and we increase the their file
258 * offsets too unless the section in question
259 * is the first in its containing segment.
262 for (i = 0; i < s->seg->nsec; i++)
263 if (s->seg->v_sec[i] == s)
265 if (i >= s->seg->nsec)
266 errx(EXIT_FAILURE, "Internal: section `%s' not"
267 " found in its containing segement",
269 for (; i < s->seg->nsec; i++) {
270 s0 = s->seg->v_sec[i];
273 printf("section %s VMA set to %#jx\n",
274 s0->name, (uintmax_t) s0->lma);
276 if (s != s->seg->v_sec[0]) {
279 printf("section %s offset set to %#jx\n",
280 s0->name, (uintmax_t) s0->off);
288 * Apply load address padding.
291 if (ecp->pad_to != 0) {
294 * Find the section with highest VMA.
297 STAILQ_FOREACH(seg, &ecp->v_seg, seg_list) {
298 if (seg->type != PT_LOAD)
300 for (i = seg->nsec - 1; i >= 0; i--)
301 if (seg->v_sec[i]->type != SHT_NOBITS)
309 if (s0->vma > s->vma)
317 /* No need to pad if the pad_to address is lower. */
318 if (ecp->pad_to <= s->vma + s->sz)
321 s->pad_sz = ecp->pad_to - (s->vma + s->sz);
323 printf("pad section %s VMA to address %#jx by %#jx\n", s->name,
324 (uintmax_t) ecp->pad_to, (uintmax_t) s->pad_sz);
332 * Apply sections LMA change in the third iteration.
334 TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
337 * Only loadable section that's inside a segment can have
338 * LMA adjusted. Also, if LMA of the containing segment is
339 * set to 0, it probably means we should ignore the LMA.
341 if (!s->loadable || s->seg == NULL || s->seg->paddr == 0)
345 * Check if there is a LMA change request for this
348 sac = lookup_sec_act(ecp, s->name, 0);
351 if (!sac->setlma && sac->lma_adjust == 0)
356 if (sac->lma_adjust != 0)
357 lma += sac->lma_adjust;
362 printf("LMA for section %s: %#jx\n", s->name, lma);
365 /* Check alignment. */
366 if (lma % s->align != 0)
367 errx(EXIT_FAILURE, "The LMA %#jx for "
368 "section %s is not aligned to %ju",
369 (uintmax_t) lma, s->name, (uintmax_t) s->align);
372 * Update section LMA.
377 * To move a section to lower LMA, we decrease
378 * the LMA of the section and all the sections that
382 for (i = 0; i < s->seg->nsec; i++) {
383 s0 = s->seg->v_sec[i];
386 printf("section %s LMA set to %#jx\n",
387 s0->name, (uintmax_t) s0->lma);
394 * To move a section to upper LMA, we increase
395 * the LMA of the section and all the sections that
399 for (i = 0; i < s->seg->nsec; i++)
400 if (s->seg->v_sec[i] == s)
402 if (i >= s->seg->nsec)
403 errx(EXIT_FAILURE, "Internal: section `%s' not"
404 " found in its containing segement",
406 for (; i < s->seg->nsec; i++) {
407 s0 = s->seg->v_sec[i];
410 printf("section %s LMA set to %#jx\n",
411 s0->name, (uintmax_t) s0->lma);
418 * Issue a warning if there are VMA/LMA adjust requests for
419 * some nonexistent sections.
421 if ((ecp->flags & NO_CHANGE_WARN) == 0) {
422 STAILQ_FOREACH(sac, &ecp->v_sac, sac_list) {
423 if (!sac->setvma && !sac->setlma &&
424 !sac->vma_adjust && !sac->lma_adjust)
427 TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
428 if (s->pseudo || s->name == NULL)
430 if (!strcmp(s->name, sac->name)) {
436 warnx("cannot find section `%s'", sac->name);
442 insert_to_inseg_list(struct segment *seg, struct section *sec)
448 seg->v_sec = realloc(seg->v_sec, seg->nsec * sizeof(*seg->v_sec));
449 if (seg->v_sec == NULL)
450 err(EXIT_FAILURE, "realloc failed");
453 * Sort the section in order of offset.
456 for (i = seg->nsec - 1; i > 0; i--) {
457 s = seg->v_sec[i - 1];
458 if (sec->off >= s->off) {
469 setup_phdr(struct elfcopy *ecp)
475 if (elf_getphnum(ecp->ein, &iphnum) == 0)
476 errx(EXIT_FAILURE, "elf_getphnum failed: %s",
479 ecp->ophnum = ecp->iphnum = iphnum;
483 /* If --only-keep-debug is specified, discard all program headers. */
484 if (ecp->strip == STRIP_NONDEBUG) {
489 for (i = 0; i < iphnum; i++) {
490 if (gelf_getphdr(ecp->ein, i, &iphdr) != &iphdr)
491 errx(EXIT_FAILURE, "gelf_getphdr failed: %s",
493 if ((seg = calloc(1, sizeof(*seg))) == NULL)
494 err(EXIT_FAILURE, "calloc failed");
495 seg->vaddr = iphdr.p_vaddr;
496 seg->paddr = iphdr.p_paddr;
497 seg->off = iphdr.p_offset;
498 seg->fsz = iphdr.p_filesz;
499 seg->msz = iphdr.p_memsz;
500 seg->type = iphdr.p_type;
501 STAILQ_INSERT_TAIL(&ecp->v_seg, seg, seg_list);
506 copy_phdr(struct elfcopy *ecp)
510 GElf_Phdr iphdr, ophdr;
513 STAILQ_FOREACH(seg, &ecp->v_seg, seg_list) {
514 if (seg->type == PT_PHDR) {
515 if (!TAILQ_EMPTY(&ecp->v_sec)) {
516 s = TAILQ_FIRST(&ecp->v_sec);
518 seg->vaddr = s->vma +
519 gelf_fsize(ecp->eout, ELF_T_EHDR,
521 seg->paddr = s->lma +
522 gelf_fsize(ecp->eout, ELF_T_EHDR,
526 seg->fsz = seg->msz = gelf_fsize(ecp->eout, ELF_T_PHDR,
527 ecp->ophnum, EV_CURRENT);
537 seg->fsz = seg->msz = 0;
538 for (i = 0; i < seg->nsec; i++) {
540 seg->msz = s->vma + s->sz - seg->vaddr;
541 if (s->type != SHT_NOBITS)
542 seg->fsz = s->off + s->sz - seg->off;
547 * Allocate space for program headers, note that libelf keep
548 * track of the number in internal variable, and a call to
549 * elf_update is needed to update e_phnum of ehdr.
551 if (gelf_newphdr(ecp->eout, ecp->ophnum) == NULL)
552 errx(EXIT_FAILURE, "gelf_newphdr() failed: %s",
556 * This elf_update() call is to update the e_phnum field in
557 * ehdr. It's necessary because later we will call gelf_getphdr(),
558 * which does sanity check by comparing ndx argument with e_phnum.
560 if (elf_update(ecp->eout, ELF_C_NULL) < 0)
561 errx(EXIT_FAILURE, "elf_update() failed: %s", elf_errmsg(-1));
564 * iphnum == ophnum, since we don't remove program headers even if
565 * they no longer contain sections.
568 STAILQ_FOREACH(seg, &ecp->v_seg, seg_list) {
569 if (i >= ecp->iphnum)
571 if (gelf_getphdr(ecp->ein, i, &iphdr) != &iphdr)
572 errx(EXIT_FAILURE, "gelf_getphdr failed: %s",
574 if (gelf_getphdr(ecp->eout, i, &ophdr) != &ophdr)
575 errx(EXIT_FAILURE, "gelf_getphdr failed: %s",
578 ophdr.p_type = iphdr.p_type;
579 ophdr.p_vaddr = seg->vaddr;
580 ophdr.p_paddr = seg->paddr;
581 ophdr.p_flags = iphdr.p_flags;
582 ophdr.p_align = iphdr.p_align;
583 ophdr.p_offset = seg->off;
584 ophdr.p_filesz = seg->fsz;
585 ophdr.p_memsz = seg->msz;
586 if (!gelf_update_phdr(ecp->eout, i, &ophdr))
587 errx(EXIT_FAILURE, "gelf_update_phdr failed: %s",