1 /*-
2 * Copyright (c) 2006-2011 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/param.h>
28 #include <sys/stat.h>
29
30 #include <assert.h>
31 #include <errno.h>
32 #include <gelf.h>
33 #include <libelf.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "_libelf.h"
39
40 #if ELFTC_HAVE_MMAP
41 #include <sys/mman.h>
42 #endif
43
44 ELFTC_VCSID("$Id: elf_update.c 3632 2018-10-10 21:12:43Z jkoshy $");
45
46 /*
47 * Layout strategy:
48 *
49 * - Case 1: ELF_F_LAYOUT is asserted
50 * In this case the application has full control over where the
51 * section header table, program header table, and section data
52 * will reside. The library only perform error checks.
53 *
54 * - Case 2: ELF_F_LAYOUT is not asserted
55 *
56 * The library will do the object layout using the following
57 * ordering:
58 * - The executable header is placed first, are required by the
59 * ELF specification.
60 * - The program header table is placed immediately following the
61 * executable header.
62 * - Section data, if any, is placed after the program header
63 * table, aligned appropriately.
64 * - The section header table, if needed, is placed last.
65 *
66 * There are two sub-cases to be taken care of:
67 *
68 * - Case 2a: e->e_cmd == ELF_C_READ or ELF_C_RDWR
69 *
70 * In this sub-case, the underlying ELF object may already have
71 * content in it, which the application may have modified. The
72 * library will retrieve content from the existing object as
73 * needed.
74 *
75 * - Case 2b: e->e_cmd == ELF_C_WRITE
76 *
77 * The ELF object is being created afresh in this sub-case;
78 * there is no pre-existing content in the underlying ELF
79 * object.
80 */
81
82 /*
83 * The types of extents in an ELF object.
84 */
85 enum elf_extent {
86 ELF_EXTENT_EHDR,
87 ELF_EXTENT_PHDR,
88 ELF_EXTENT_SECTION,
89 ELF_EXTENT_SHDR
90 };
91
92 /*
93 * A extent descriptor, used when laying out an ELF object.
94 */
95 struct _Elf_Extent {
96 SLIST_ENTRY(_Elf_Extent) ex_next;
97 uint64_t ex_start; /* Start of the region. */
98 uint64_t ex_size; /* The size of the region. */
99 enum elf_extent ex_type; /* Type of region. */
100 void *ex_desc; /* Associated descriptor. */
101 };
102
103 SLIST_HEAD(_Elf_Extent_List, _Elf_Extent);
104
105 /*
106 * Compute the extents of a section, by looking at the data
107 * descriptors associated with it. The function returns 1
108 * if successful, or zero if an error was detected.
109 */
110 static int
_libelf_compute_section_extents(Elf * e,Elf_Scn * s,off_t rc)111 _libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t rc)
112 {
113 Elf_Data *d;
114 size_t fsz, msz;
115 int ec, elftype;
116 uint32_t sh_type;
117 uint64_t d_align;
118 Elf32_Shdr *shdr32;
119 Elf64_Shdr *shdr64;
120 struct _Libelf_Data *ld;
121 uint64_t scn_size, scn_alignment;
122 uint64_t sh_align, sh_entsize, sh_offset, sh_size;
123
124 ec = e->e_class;
125
126 shdr32 = &s->s_shdr.s_shdr32;
127 shdr64 = &s->s_shdr.s_shdr64;
128 if (ec == ELFCLASS32) {
129 sh_type = shdr32->sh_type;
130 sh_align = (uint64_t) shdr32->sh_addralign;
131 sh_entsize = (uint64_t) shdr32->sh_entsize;
132 sh_offset = (uint64_t) shdr32->sh_offset;
133 sh_size = (uint64_t) shdr32->sh_size;
134 } else {
135 sh_type = shdr64->sh_type;
136 sh_align = shdr64->sh_addralign;
137 sh_entsize = shdr64->sh_entsize;
138 sh_offset = shdr64->sh_offset;
139 sh_size = shdr64->sh_size;
140 }
141
142 assert(sh_type != SHT_NULL && sh_type != SHT_NOBITS);
143
144 elftype = _libelf_xlate_shtype(sh_type);
145 if (elftype > ELF_T_LAST) {
146 LIBELF_SET_ERROR(SECTION, 0);
147 return (0);
148 }
149
150 if (sh_align == 0)
151 sh_align = _libelf_falign(elftype, ec);
152
153 /*
154 * Compute the section's size and alignment using the data
155 * descriptors associated with the section.
156 */
157 if (STAILQ_EMPTY(&s->s_data)) {
158 /*
159 * The section's content (if any) has not been read in
160 * yet. If section is not dirty marked dirty, we can
161 * reuse the values in the 'sh_size' and 'sh_offset'
162 * fields of the section header.
163 */
164 if ((s->s_flags & ELF_F_DIRTY) == 0) {
165 /*
166 * If the library is doing the layout, then we
167 * compute the new start offset for the
168 * section based on the current offset and the
169 * section's alignment needs.
170 *
171 * If the application is doing the layout, we
172 * can use the value in the 'sh_offset' field
173 * in the section header directly.
174 */
175 if (e->e_flags & ELF_F_LAYOUT)
176 goto updatedescriptor;
177 else
178 goto computeoffset;
179 }
180
181 /*
182 * Otherwise, we need to bring in the section's data
183 * from the underlying ELF object.
184 */
185 if (e->e_cmd != ELF_C_WRITE && elf_getdata(s, NULL) == NULL)
186 return (0);
187 }
188
189 /*
190 * Loop through the section's data descriptors.
191 */
192 scn_size = 0L;
193 scn_alignment = 0;
194 STAILQ_FOREACH(ld, &s->s_data, d_next) {
195
196 d = &ld->d_data;
197
198 /*
199 * The data buffer's type is known.
200 */
201 if (d->d_type >= ELF_T_NUM) {
202 LIBELF_SET_ERROR(DATA, 0);
203 return (0);
204 }
205
206 /*
207 * The data buffer's version is supported.
208 */
209 if (d->d_version != e->e_version) {
210 LIBELF_SET_ERROR(VERSION, 0);
211 return (0);
212 }
213
214 /*
215 * The buffer's alignment is non-zero and a power of
216 * two.
217 */
218 if ((d_align = d->d_align) == 0 ||
219 (d_align & (d_align - 1))) {
220 LIBELF_SET_ERROR(DATA, 0);
221 return (0);
222 }
223
224 /*
225 * The buffer's size should be a multiple of the
226 * memory size of the underlying type.
227 */
228 msz = _libelf_msize(d->d_type, ec, e->e_version);
229 if (d->d_size % msz) {
230 LIBELF_SET_ERROR(DATA, 0);
231 return (0);
232 }
233
234 /*
235 * If the application is controlling layout, then the
236 * d_offset field should be compatible with the
237 * buffer's specified alignment.
238 */
239 if ((e->e_flags & ELF_F_LAYOUT) &&
240 (d->d_off & (d_align - 1))) {
241 LIBELF_SET_ERROR(LAYOUT, 0);
242 return (0);
243 }
244
245 /*
246 * Compute the section's size.
247 */
248 if (e->e_flags & ELF_F_LAYOUT) {
249 if ((uint64_t) d->d_off + d->d_size > scn_size)
250 scn_size = d->d_off + d->d_size;
251 } else {
252 scn_size = roundup2(scn_size, d->d_align);
253 d->d_off = scn_size;
254 fsz = _libelf_fsize(d->d_type, ec, d->d_version,
255 (size_t) d->d_size / msz);
256 scn_size += fsz;
257 }
258
259 /*
260 * The section's alignment is the maximum alignment
261 * needed for its data buffers.
262 */
263 if (d_align > scn_alignment)
264 scn_alignment = d_align;
265 }
266
267
268 /*
269 * If the application is requesting full control over the
270 * layout of the section, check the section's specified size,
271 * offsets and alignment for sanity.
272 */
273 if (e->e_flags & ELF_F_LAYOUT) {
274 if (scn_alignment > sh_align ||
275 sh_offset % sh_align ||
276 sh_size < scn_size ||
277 sh_offset % _libelf_falign(elftype, ec)) {
278 LIBELF_SET_ERROR(LAYOUT, 0);
279 return (0);
280 }
281 goto updatedescriptor;
282 }
283
284 /*
285 * Otherwise, compute the values in the section header.
286 *
287 * The section alignment is the maximum alignment for any of
288 * its contained data descriptors.
289 */
290 if (scn_alignment > sh_align)
291 sh_align = scn_alignment;
292
293 /*
294 * If the section entry size is zero, try and fill in an
295 * appropriate entry size. Per the elf(5) manual page
296 * sections without fixed-size entries should have their
297 * 'sh_entsize' field set to zero.
298 */
299 if (sh_entsize == 0 &&
300 (sh_entsize = _libelf_fsize(elftype, ec, e->e_version,
301 (size_t) 1)) == 1)
302 sh_entsize = 0;
303
304 sh_size = scn_size;
305
306 computeoffset:
307 /*
308 * Compute the new offset for the section based on
309 * the section's alignment needs.
310 */
311 sh_offset = roundup((uint64_t) rc, sh_align);
312
313 /*
314 * Update the section header.
315 */
316 if (ec == ELFCLASS32) {
317 shdr32->sh_addralign = (uint32_t) sh_align;
318 shdr32->sh_entsize = (uint32_t) sh_entsize;
319 shdr32->sh_offset = (uint32_t) sh_offset;
320 shdr32->sh_size = (uint32_t) sh_size;
321 } else {
322 shdr64->sh_addralign = sh_align;
323 shdr64->sh_entsize = sh_entsize;
324 shdr64->sh_offset = sh_offset;
325 shdr64->sh_size = sh_size;
326 }
327
328 updatedescriptor:
329 /*
330 * Update the section descriptor.
331 */
332 s->s_size = sh_size;
333 s->s_offset = sh_offset;
334
335 return (1);
336 }
337
338 /*
339 * Free a list of extent descriptors.
340 */
341
342 static void
_libelf_release_extents(struct _Elf_Extent_List * extents)343 _libelf_release_extents(struct _Elf_Extent_List *extents)
344 {
345 struct _Elf_Extent *ex;
346
347 while ((ex = SLIST_FIRST(extents)) != NULL) {
348 SLIST_REMOVE_HEAD(extents, ex_next);
349 free(ex);
350 }
351 }
352
353 /*
354 * Check if an extent 's' defined by [start..start+size) is free.
355 * This routine assumes that the given extent list is sorted in order
356 * of ascending extent offsets.
357 */
358
359 static int
_libelf_extent_is_unused(struct _Elf_Extent_List * extents,const uint64_t start,const uint64_t size,struct _Elf_Extent ** prevt)360 _libelf_extent_is_unused(struct _Elf_Extent_List *extents,
361 const uint64_t start, const uint64_t size, struct _Elf_Extent **prevt)
362 {
363 uint64_t tmax, tmin;
364 struct _Elf_Extent *t, *pt;
365 const uint64_t smax = start + size;
366
367 /* First, look for overlaps with existing extents. */
368 pt = NULL;
369 SLIST_FOREACH(t, extents, ex_next) {
370 tmin = t->ex_start;
371 tmax = tmin + t->ex_size;
372
373 if (tmax <= start) {
374 /*
375 * 't' lies entirely before 's': ...| t |...| s |...
376 */
377 pt = t;
378 continue;
379 } else if (smax <= tmin) {
380 /*
381 * 's' lies entirely before 't', and after 'pt':
382 * ...| pt |...| s |...| t |...
383 */
384 assert(pt == NULL ||
385 pt->ex_start + pt->ex_size <= start);
386 break;
387 } else
388 /* 's' and 't' overlap. */
389 return (0);
390 }
391
392 if (prevt)
393 *prevt = pt;
394 return (1);
395 }
396
397 /*
398 * Insert an extent into the list of extents.
399 */
400
401 static int
_libelf_insert_extent(struct _Elf_Extent_List * extents,int type,uint64_t start,uint64_t size,void * desc)402 _libelf_insert_extent(struct _Elf_Extent_List *extents, int type,
403 uint64_t start, uint64_t size, void *desc)
404 {
405 struct _Elf_Extent *ex, *prevt;
406
407 assert(type >= ELF_EXTENT_EHDR && type <= ELF_EXTENT_SHDR);
408
409 prevt = NULL;
410
411 /*
412 * If the requested range overlaps with an existing extent,
413 * signal an error.
414 */
415 if (!_libelf_extent_is_unused(extents, start, size, &prevt)) {
416 LIBELF_SET_ERROR(LAYOUT, 0);
417 return (0);
418 }
419
420 /* Allocate and fill in a new extent descriptor. */
421 if ((ex = malloc(sizeof(struct _Elf_Extent))) == NULL) {
422 LIBELF_SET_ERROR(RESOURCE, errno);
423 return (0);
424 }
425 ex->ex_start = start;
426 ex->ex_size = size;
427 ex->ex_desc = desc;
428 ex->ex_type = type;
429
430 /* Insert the region descriptor into the list. */
431 if (prevt)
432 SLIST_INSERT_AFTER(prevt, ex, ex_next);
433 else
434 SLIST_INSERT_HEAD(extents, ex, ex_next);
435 return (1);
436 }
437
438 /*
439 * Recompute section layout.
440 */
441
442 static off_t
_libelf_resync_sections(Elf * e,off_t rc,struct _Elf_Extent_List * extents)443 _libelf_resync_sections(Elf *e, off_t rc, struct _Elf_Extent_List *extents)
444 {
445 int ec;
446 Elf_Scn *s;
447 size_t sh_type;
448
449 ec = e->e_class;
450
451 /*
452 * Make a pass through sections, computing the extent of each
453 * section.
454 */
455 RB_FOREACH(s, scntree, &e->e_u.e_elf.e_scn) {
456 if (ec == ELFCLASS32)
457 sh_type = s->s_shdr.s_shdr32.sh_type;
458 else
459 sh_type = s->s_shdr.s_shdr64.sh_type;
460
461 if (sh_type == SHT_NOBITS || sh_type == SHT_NULL)
462 continue;
463
464 if (_libelf_compute_section_extents(e, s, rc) == 0)
465 return ((off_t) -1);
466
467 if (s->s_size == 0)
468 continue;
469
470 if (!_libelf_insert_extent(extents, ELF_EXTENT_SECTION,
471 s->s_offset, s->s_size, s))
472 return ((off_t) -1);
473
474 if ((size_t) rc < s->s_offset + s->s_size)
475 rc = (off_t) (s->s_offset + s->s_size);
476 }
477
478 return (rc);
479 }
480
481 /*
482 * Recompute the layout of the ELF object and update the internal data
483 * structures associated with the ELF descriptor.
484 *
485 * Returns the size in bytes the ELF object would occupy in its file
486 * representation.
487 *
488 * After a successful call to this function, the following structures
489 * are updated:
490 *
491 * - The ELF header is updated.
492 * - All extents in the ELF object are sorted in order of ascending
493 * addresses. Sections have their section header table entries
494 * updated. An error is signalled if an overlap was detected among
495 * extents.
496 * - Data descriptors associated with sections are checked for valid
497 * types, offsets and alignment.
498 *
499 * After a resync_elf() successfully returns, the ELF descriptor is
500 * ready for being handed over to _libelf_write_elf().
501 */
502
503 static off_t
_libelf_resync_elf(Elf * e,struct _Elf_Extent_List * extents)504 _libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents)
505 {
506 int ec, eh_class;
507 unsigned int eh_byteorder, eh_version;
508 size_t align, fsz;
509 size_t phnum, shnum;
510 off_t rc, phoff, shoff;
511 void *ehdr, *phdr;
512 Elf32_Ehdr *eh32;
513 Elf64_Ehdr *eh64;
514
515 rc = 0;
516
517 ec = e->e_class;
518
519 assert(ec == ELFCLASS32 || ec == ELFCLASS64);
520
521 /*
522 * Prepare the EHDR.
523 */
524 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
525 return ((off_t) -1);
526
527 eh32 = ehdr;
528 eh64 = ehdr;
529
530 if (ec == ELFCLASS32) {
531 eh_byteorder = eh32->e_ident[EI_DATA];
532 eh_class = eh32->e_ident[EI_CLASS];
533 phoff = (off_t) eh32->e_phoff;
534 shoff = (off_t) eh32->e_shoff;
535 eh_version = eh32->e_version;
536 } else {
537 eh_byteorder = eh64->e_ident[EI_DATA];
538 eh_class = eh64->e_ident[EI_CLASS];
539 phoff = (off_t) eh64->e_phoff;
540 shoff = (off_t) eh64->e_shoff;
541 eh_version = eh64->e_version;
542 }
543
544 if (phoff < 0 || shoff < 0) {
545 LIBELF_SET_ERROR(HEADER, 0);
546 return ((off_t) -1);
547 }
548
549 if (eh_version == EV_NONE)
550 eh_version = EV_CURRENT;
551
552 if (eh_version != e->e_version) { /* always EV_CURRENT */
553 LIBELF_SET_ERROR(VERSION, 0);
554 return ((off_t) -1);
555 }
556
557 if (eh_class != e->e_class) {
558 LIBELF_SET_ERROR(CLASS, 0);
559 return ((off_t) -1);
560 }
561
562 if (e->e_cmd != ELF_C_WRITE && eh_byteorder != e->e_byteorder) {
563 LIBELF_SET_ERROR(HEADER, 0);
564 return ((off_t) -1);
565 }
566
567 shnum = e->e_u.e_elf.e_nscn;
568 phnum = e->e_u.e_elf.e_nphdr;
569
570 e->e_byteorder = eh_byteorder;
571
572 #define INITIALIZE_EHDR(E,EC,V) do { \
573 unsigned int _version = (unsigned int) (V); \
574 (E)->e_ident[EI_MAG0] = ELFMAG0; \
575 (E)->e_ident[EI_MAG1] = ELFMAG1; \
576 (E)->e_ident[EI_MAG2] = ELFMAG2; \
577 (E)->e_ident[EI_MAG3] = ELFMAG3; \
578 (E)->e_ident[EI_CLASS] = (unsigned char) (EC); \
579 (E)->e_ident[EI_VERSION] = (_version & 0xFFU); \
580 (E)->e_ehsize = (uint16_t) _libelf_fsize(ELF_T_EHDR, \
581 (EC), _version, (size_t) 1); \
582 (E)->e_phentsize = (uint16_t) ((phnum == 0) ? 0 : \
583 _libelf_fsize(ELF_T_PHDR, (EC), _version, \
584 (size_t) 1)); \
585 (E)->e_shentsize = (uint16_t) _libelf_fsize(ELF_T_SHDR, \
586 (EC), _version, (size_t) 1); \
587 } while (0)
588
589 if (ec == ELFCLASS32)
590 INITIALIZE_EHDR(eh32, ec, eh_version);
591 else
592 INITIALIZE_EHDR(eh64, ec, eh_version);
593
594 (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY);
595
596 rc += (off_t) _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1);
597
598 if (!_libelf_insert_extent(extents, ELF_EXTENT_EHDR, 0, (uint64_t) rc,
599 ehdr))
600 return ((off_t) -1);
601
602 /*
603 * Compute the layout the program header table, if one is
604 * present. The program header table needs to be aligned to a
605 * `natural' boundary.
606 */
607 if (phnum) {
608 fsz = _libelf_fsize(ELF_T_PHDR, ec, eh_version, phnum);
609 align = _libelf_falign(ELF_T_PHDR, ec);
610
611 if (e->e_flags & ELF_F_LAYOUT) {
612 /*
613 * Check offsets for sanity.
614 */
615 if (rc > phoff) {
616 LIBELF_SET_ERROR(LAYOUT, 0);
617 return ((off_t) -1);
618 }
619
620 if (phoff % (off_t) align) {
621 LIBELF_SET_ERROR(LAYOUT, 0);
622 return ((off_t) -1);
623 }
624
625 } else
626 phoff = roundup(rc, (off_t) align);
627
628 rc = phoff + (off_t) fsz;
629
630 phdr = _libelf_getphdr(e, ec);
631
632 if (!_libelf_insert_extent(extents, ELF_EXTENT_PHDR,
633 (uint64_t) phoff, fsz, phdr))
634 return ((off_t) -1);
635 } else
636 phoff = 0;
637
638 /*
639 * Compute the layout of the sections associated with the
640 * file.
641 */
642
643 if (e->e_cmd != ELF_C_WRITE &&
644 (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
645 _libelf_load_section_headers(e, ehdr) == 0)
646 return ((off_t) -1);
647
648 if ((rc = _libelf_resync_sections(e, rc, extents)) < 0)
649 return ((off_t) -1);
650
651 /*
652 * Compute the space taken up by the section header table, if
653 * one is needed.
654 *
655 * If ELF_F_LAYOUT has been asserted, the application may have
656 * placed the section header table in between existing
657 * sections, so the net size of the file need not increase due
658 * to the presence of the section header table.
659 *
660 * If the library is responsible for laying out the object,
661 * the section header table is placed after section data.
662 */
663 if (shnum) {
664 fsz = _libelf_fsize(ELF_T_SHDR, ec, eh_version, shnum);
665 align = _libelf_falign(ELF_T_SHDR, ec);
666
667 if (e->e_flags & ELF_F_LAYOUT) {
668 if (shoff % (off_t) align) {
669 LIBELF_SET_ERROR(LAYOUT, 0);
670 return ((off_t) -1);
671 }
672 } else
673 shoff = roundup(rc, (off_t) align);
674
675 if (shoff + (off_t) fsz > rc)
676 rc = shoff + (off_t) fsz;
677
678 if (!_libelf_insert_extent(extents, ELF_EXTENT_SHDR,
679 (uint64_t) shoff, fsz, NULL))
680 return ((off_t) -1);
681 } else
682 shoff = 0;
683
684 /*
685 * Set the fields of the Executable Header that could potentially use
686 * extended numbering.
687 */
688 _libelf_setphnum(e, ehdr, ec, phnum);
689 _libelf_setshnum(e, ehdr, ec, shnum);
690
691 /*
692 * Update the `e_phoff' and `e_shoff' fields if the library is
693 * doing the layout.
694 */
695 if ((e->e_flags & ELF_F_LAYOUT) == 0) {
696 if (ec == ELFCLASS32) {
697 eh32->e_phoff = (uint32_t) phoff;
698 eh32->e_shoff = (uint32_t) shoff;
699 } else {
700 eh64->e_phoff = (uint64_t) phoff;
701 eh64->e_shoff = (uint64_t) shoff;
702 }
703 }
704
705 return (rc);
706 }
707
708 /*
709 * Write out the contents of an ELF section.
710 */
711
712 static off_t
_libelf_write_scn(Elf * e,unsigned char * nf,struct _Elf_Extent * ex)713 _libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
714 {
715 off_t rc;
716 int ec, em;
717 Elf_Scn *s;
718 int elftype;
719 Elf_Data *d, dst;
720 uint32_t sh_type;
721 struct _Libelf_Data *ld;
722 uint64_t sh_off, sh_size;
723 size_t fsz, msz, nobjects;
724
725 assert(ex->ex_type == ELF_EXTENT_SECTION);
726
727 s = ex->ex_desc;
728 rc = (off_t) ex->ex_start;
729
730 if ((ec = e->e_class) == ELFCLASS32) {
731 sh_type = s->s_shdr.s_shdr32.sh_type;
732 sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size;
733 } else {
734 sh_type = s->s_shdr.s_shdr64.sh_type;
735 sh_size = s->s_shdr.s_shdr64.sh_size;
736 }
737
738 /*
739 * Ignore sections that do not allocate space in the file.
740 */
741 if (sh_type == SHT_NOBITS || sh_type == SHT_NULL || sh_size == 0)
742 return (rc);
743
744 elftype = _libelf_xlate_shtype(sh_type);
745 assert(elftype >= ELF_T_FIRST && elftype <= ELF_T_LAST);
746
747 sh_off = s->s_offset;
748 assert(sh_off % _libelf_falign(elftype, ec) == 0);
749
750 em = _libelf_elfmachine(e);
751 #if 0
752 assert(em >= EM_NONE && em < EM__LAST__);
753 #endif
754
755 /*
756 * If the section has a `rawdata' descriptor, and the section
757 * contents have not been modified, use its contents directly.
758 * The `s_rawoff' member contains the offset into the original
759 * file, while `s_offset' contains its new location in the
760 * destination.
761 */
762
763 if (STAILQ_EMPTY(&s->s_data)) {
764
765 if ((d = elf_rawdata(s, NULL)) == NULL)
766 return ((off_t) -1);
767
768 STAILQ_FOREACH(ld, &s->s_rawdata, d_next) {
769
770 d = &ld->d_data;
771
772 if ((uint64_t) rc < sh_off + d->d_off)
773 (void) memset(nf + rc,
774 LIBELF_PRIVATE(fillchar),
775 (size_t) (sh_off + d->d_off -
776 (uint64_t) rc));
777 rc = (off_t) (sh_off + d->d_off);
778
779 assert(d->d_buf != NULL);
780 assert(d->d_type == ELF_T_BYTE);
781 assert(d->d_version == e->e_version);
782
783 (void) memcpy(nf + rc,
784 e->e_rawfile + s->s_rawoff + d->d_off,
785 (size_t) d->d_size);
786
787 rc += (off_t) d->d_size;
788 }
789
790 return (rc);
791 }
792
793 /*
794 * Iterate over the set of data descriptors for this section.
795 * The prior call to _libelf_resync_elf() would have setup the
796 * descriptors for this step.
797 */
798
799 dst.d_version = e->e_version;
800
801 STAILQ_FOREACH(ld, &s->s_data, d_next) {
802
803 d = &ld->d_data;
804
805 msz = _libelf_msize(d->d_type, ec, e->e_version);
806
807 if ((uint64_t) rc < sh_off + d->d_off)
808 (void) memset(nf + rc,
809 LIBELF_PRIVATE(fillchar),
810 (size_t) (sh_off + d->d_off - (uint64_t) rc));
811
812 rc = (off_t) (sh_off + d->d_off);
813
814 assert(d->d_buf != NULL);
815 assert(d->d_version == e->e_version);
816 assert(msz != 0);
817 assert(d->d_size % msz == 0);
818
819 nobjects = (size_t) (d->d_size / msz);
820
821 fsz = _libelf_fsize(d->d_type, ec, e->e_version, nobjects);
822
823 dst.d_buf = nf + rc;
824 dst.d_size = fsz;
825
826 if (_libelf_xlate(&dst, d, e->e_byteorder, ec, em, ELF_TOFILE)
827 == NULL)
828 return ((off_t) -1);
829
830 rc += (off_t) fsz;
831 }
832
833 return (rc);
834 }
835
836 /*
837 * Write out an ELF Executable Header.
838 */
839
840 static off_t
_libelf_write_ehdr(Elf * e,unsigned char * nf,struct _Elf_Extent * ex)841 _libelf_write_ehdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
842 {
843 int ec, em;
844 void *ehdr;
845 size_t fsz, msz;
846 Elf_Data dst, src;
847
848 assert(ex->ex_type == ELF_EXTENT_EHDR);
849 assert(ex->ex_start == 0); /* Ehdr always comes first. */
850
851 ec = e->e_class;
852
853 ehdr = _libelf_ehdr(e, ec, 0);
854 assert(ehdr != NULL);
855
856 fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
857 msz = _libelf_msize(ELF_T_EHDR, ec, e->e_version);
858
859 em = _libelf_elfmachine(e);
860
861 (void) memset(&dst, 0, sizeof(dst));
862 (void) memset(&src, 0, sizeof(src));
863
864 src.d_buf = ehdr;
865 src.d_size = msz;
866 src.d_type = ELF_T_EHDR;
867 src.d_version = dst.d_version = e->e_version;
868
869 dst.d_buf = nf;
870 dst.d_size = fsz;
871
872 if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, em, ELF_TOFILE) ==
873 NULL)
874 return ((off_t) -1);
875
876 return ((off_t) fsz);
877 }
878
879 /*
880 * Write out an ELF program header table.
881 */
882
883 static off_t
_libelf_write_phdr(Elf * e,unsigned char * nf,struct _Elf_Extent * ex)884 _libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
885 {
886 int ec, em;
887 void *ehdr;
888 Elf32_Ehdr *eh32;
889 Elf64_Ehdr *eh64;
890 Elf_Data dst, src;
891 size_t fsz, phnum;
892 uint64_t phoff;
893
894 assert(ex->ex_type == ELF_EXTENT_PHDR);
895
896 ec = e->e_class;
897 ehdr = _libelf_ehdr(e, ec, 0);
898 phnum = e->e_u.e_elf.e_nphdr;
899
900 assert(phnum > 0);
901
902 if (ec == ELFCLASS32) {
903 eh32 = (Elf32_Ehdr *) ehdr;
904 phoff = (uint64_t) eh32->e_phoff;
905 } else {
906 eh64 = (Elf64_Ehdr *) ehdr;
907 phoff = eh64->e_phoff;
908 }
909
910 em = _libelf_elfmachine(e);
911
912 assert(phoff > 0);
913 assert(ex->ex_start == phoff);
914 assert(phoff % _libelf_falign(ELF_T_PHDR, ec) == 0);
915
916 (void) memset(&dst, 0, sizeof(dst));
917 (void) memset(&src, 0, sizeof(src));
918
919 fsz = _libelf_fsize(ELF_T_PHDR, ec, e->e_version, phnum);
920 assert(fsz > 0);
921
922 src.d_buf = _libelf_getphdr(e, ec);
923 src.d_version = dst.d_version = e->e_version;
924 src.d_type = ELF_T_PHDR;
925 src.d_size = phnum * _libelf_msize(ELF_T_PHDR, ec,
926 e->e_version);
927
928 dst.d_size = fsz;
929 dst.d_buf = nf + ex->ex_start;
930
931 if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, em, ELF_TOFILE) ==
932 NULL)
933 return ((off_t) -1);
934
935 return ((off_t) (phoff + fsz));
936 }
937
938 /*
939 * Write out an ELF section header table.
940 */
941
942 static off_t
_libelf_write_shdr(Elf * e,unsigned char * nf,struct _Elf_Extent * ex)943 _libelf_write_shdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
944 {
945 int ec, em;
946 void *ehdr;
947 Elf_Scn *scn;
948 uint64_t shoff;
949 Elf32_Ehdr *eh32;
950 Elf64_Ehdr *eh64;
951 size_t fsz, nscn;
952 Elf_Data dst, src;
953
954 assert(ex->ex_type == ELF_EXTENT_SHDR);
955
956 ec = e->e_class;
957 ehdr = _libelf_ehdr(e, ec, 0);
958 nscn = e->e_u.e_elf.e_nscn;
959
960 if (ec == ELFCLASS32) {
961 eh32 = (Elf32_Ehdr *) ehdr;
962 shoff = (uint64_t) eh32->e_shoff;
963 } else {
964 eh64 = (Elf64_Ehdr *) ehdr;
965 shoff = eh64->e_shoff;
966 }
967
968 em = _libelf_elfmachine(e);
969
970 assert(nscn > 0);
971 assert(shoff % _libelf_falign(ELF_T_SHDR, ec) == 0);
972 assert(ex->ex_start == shoff);
973
974 (void) memset(&dst, 0, sizeof(dst));
975 (void) memset(&src, 0, sizeof(src));
976
977 src.d_type = ELF_T_SHDR;
978 src.d_size = _libelf_msize(ELF_T_SHDR, ec, e->e_version);
979 src.d_version = dst.d_version = e->e_version;
980
981 fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
982
983 RB_FOREACH(scn, scntree, &e->e_u.e_elf.e_scn) {
984 if (ec == ELFCLASS32)
985 src.d_buf = &scn->s_shdr.s_shdr32;
986 else
987 src.d_buf = &scn->s_shdr.s_shdr64;
988
989 dst.d_size = fsz;
990 dst.d_buf = nf + ex->ex_start + scn->s_ndx * fsz;
991
992 if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, em,
993 ELF_TOFILE) == NULL)
994 return ((off_t) -1);
995 }
996
997 return ((off_t) (ex->ex_start + nscn * fsz));
998 }
999
1000 /*
1001 * Write out the file image.
1002 *
1003 * The original file could have been mapped in with an ELF_C_RDWR
1004 * command and the application could have added new content or
1005 * re-arranged its sections before calling elf_update(). Consequently
1006 * its not safe to work `in place' on the original file. So we
1007 * malloc() the required space for the updated ELF object and build
1008 * the object there and write it out to the underlying file at the
1009 * end. Note that the application may have opened the underlying file
1010 * in ELF_C_RDWR and only retrieved/modified a few sections. We take
1011 * care to avoid translating file sections unnecessarily.
1012 *
1013 * Gaps in the coverage of the file by the file's sections will be
1014 * filled with the fill character set by elf_fill(3).
1015 */
1016
1017 static off_t
_libelf_write_elf(Elf * e,off_t newsize,struct _Elf_Extent_List * extents)1018 _libelf_write_elf(Elf *e, off_t newsize, struct _Elf_Extent_List *extents)
1019 {
1020 off_t nrc, rc;
1021 Elf_Scn *scn, *tscn;
1022 struct _Elf_Extent *ex;
1023 unsigned char *newfile;
1024
1025 assert(e->e_kind == ELF_K_ELF);
1026 assert(e->e_cmd == ELF_C_RDWR || e->e_cmd == ELF_C_WRITE);
1027 assert(e->e_fd >= 0);
1028
1029 if ((newfile = malloc((size_t) newsize)) == NULL) {
1030 LIBELF_SET_ERROR(RESOURCE, errno);
1031 return ((off_t) -1);
1032 }
1033
1034 nrc = rc = 0;
1035 SLIST_FOREACH(ex, extents, ex_next) {
1036
1037 /* Fill inter-extent gaps. */
1038 if (ex->ex_start > (size_t) rc)
1039 (void) memset(newfile + rc, LIBELF_PRIVATE(fillchar),
1040 (size_t) (ex->ex_start - (uint64_t) rc));
1041
1042 switch (ex->ex_type) {
1043 case ELF_EXTENT_EHDR:
1044 if ((nrc = _libelf_write_ehdr(e, newfile, ex)) < 0)
1045 goto error;
1046 break;
1047
1048 case ELF_EXTENT_PHDR:
1049 if ((nrc = _libelf_write_phdr(e, newfile, ex)) < 0)
1050 goto error;
1051 break;
1052
1053 case ELF_EXTENT_SECTION:
1054 if ((nrc = _libelf_write_scn(e, newfile, ex)) < 0)
1055 goto error;
1056 break;
1057
1058 case ELF_EXTENT_SHDR:
1059 if ((nrc = _libelf_write_shdr(e, newfile, ex)) < 0)
1060 goto error;
1061 break;
1062
1063 default:
1064 assert(0);
1065 break;
1066 }
1067
1068 assert(ex->ex_start + ex->ex_size == (size_t) nrc);
1069 assert(rc < nrc);
1070
1071 rc = nrc;
1072 }
1073
1074 assert(rc == newsize);
1075
1076 /*
1077 * For regular files, throw away existing file content and
1078 * unmap any existing mappings.
1079 */
1080 if ((e->e_flags & LIBELF_F_SPECIAL_FILE) == 0) {
1081 if (ftruncate(e->e_fd, (off_t) 0) < 0 ||
1082 lseek(e->e_fd, (off_t) 0, SEEK_SET)) {
1083 LIBELF_SET_ERROR(IO, errno);
1084 goto error;
1085 }
1086 #if ELFTC_HAVE_MMAP
1087 if (e->e_flags & LIBELF_F_RAWFILE_MMAP) {
1088 assert(e->e_rawfile != NULL);
1089 assert(e->e_cmd == ELF_C_RDWR);
1090 if (munmap(e->e_rawfile, e->e_rawsize) < 0) {
1091 LIBELF_SET_ERROR(IO, errno);
1092 goto error;
1093 }
1094 }
1095 #endif
1096 }
1097
1098 /*
1099 * Write out the new contents.
1100 */
1101 if (write(e->e_fd, newfile, (size_t) newsize) != newsize) {
1102 LIBELF_SET_ERROR(IO, errno);
1103 goto error;
1104 }
1105
1106 /*
1107 * For files opened in ELF_C_RDWR mode, set up the new 'raw'
1108 * contents.
1109 */
1110 if (e->e_cmd == ELF_C_RDWR) {
1111 assert(e->e_rawfile != NULL);
1112 assert((e->e_flags & LIBELF_F_RAWFILE_MALLOC) ||
1113 (e->e_flags & LIBELF_F_RAWFILE_MMAP));
1114 if (e->e_flags & LIBELF_F_RAWFILE_MALLOC) {
1115 free(e->e_rawfile);
1116 e->e_rawfile = newfile;
1117 newfile = NULL;
1118 }
1119 #if ELFTC_HAVE_MMAP
1120 else if (e->e_flags & LIBELF_F_RAWFILE_MMAP) {
1121 if ((e->e_rawfile = mmap(NULL, (size_t) newsize,
1122 PROT_READ, MAP_PRIVATE, e->e_fd, (off_t) 0)) ==
1123 MAP_FAILED) {
1124 LIBELF_SET_ERROR(IO, errno);
1125 goto error;
1126 }
1127 }
1128 #endif /* ELFTC_HAVE_MMAP */
1129
1130 /* Record the new size of the file. */
1131 e->e_rawsize = (size_t) newsize;
1132 } else {
1133 /* File opened in ELF_C_WRITE mode. */
1134 assert(e->e_rawfile == NULL);
1135 }
1136
1137 /*
1138 * Reset flags, remove existing section descriptors and
1139 * {E,P}HDR pointers so that a subsequent elf_get{e,p}hdr()
1140 * and elf_getscn() will function correctly.
1141 */
1142
1143 e->e_flags &= ~ELF_F_DIRTY;
1144
1145 RB_FOREACH_SAFE(scn, scntree, &e->e_u.e_elf.e_scn, tscn)
1146 _libelf_release_scn(scn);
1147
1148 if (e->e_class == ELFCLASS32) {
1149 free(e->e_u.e_elf.e_ehdr.e_ehdr32);
1150 if (e->e_u.e_elf.e_phdr.e_phdr32)
1151 free(e->e_u.e_elf.e_phdr.e_phdr32);
1152
1153 e->e_u.e_elf.e_ehdr.e_ehdr32 = NULL;
1154 e->e_u.e_elf.e_phdr.e_phdr32 = NULL;
1155 } else {
1156 free(e->e_u.e_elf.e_ehdr.e_ehdr64);
1157 if (e->e_u.e_elf.e_phdr.e_phdr64)
1158 free(e->e_u.e_elf.e_phdr.e_phdr64);
1159
1160 e->e_u.e_elf.e_ehdr.e_ehdr64 = NULL;
1161 e->e_u.e_elf.e_phdr.e_phdr64 = NULL;
1162 }
1163
1164 /* Free the temporary buffer. */
1165 if (newfile)
1166 free(newfile);
1167
1168 return (rc);
1169
1170 error:
1171 free(newfile);
1172
1173 return ((off_t) -1);
1174 }
1175
1176 /*
1177 * Update an ELF object.
1178 */
1179
1180 off_t
elf_update(Elf * e,Elf_Cmd c)1181 elf_update(Elf *e, Elf_Cmd c)
1182 {
1183 int ec;
1184 off_t rc;
1185 struct _Elf_Extent_List extents;
1186
1187 rc = (off_t) -1;
1188
1189 if (e == NULL || e->e_kind != ELF_K_ELF ||
1190 (c != ELF_C_NULL && c != ELF_C_WRITE)) {
1191 LIBELF_SET_ERROR(ARGUMENT, 0);
1192 return (rc);
1193 }
1194
1195 if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
1196 LIBELF_SET_ERROR(CLASS, 0);
1197 return (rc);
1198 }
1199
1200 if (e->e_version == EV_NONE)
1201 e->e_version = EV_CURRENT;
1202
1203 if (c == ELF_C_WRITE && e->e_cmd == ELF_C_READ) {
1204 LIBELF_SET_ERROR(MODE, 0);
1205 return (rc);
1206 }
1207
1208 SLIST_INIT(&extents);
1209
1210 if ((rc = _libelf_resync_elf(e, &extents)) < 0)
1211 goto done;
1212
1213 if (c == ELF_C_NULL)
1214 goto done;
1215
1216 if (e->e_fd < 0) {
1217 rc = (off_t) -1;
1218 LIBELF_SET_ERROR(SEQUENCE, 0);
1219 goto done;
1220 }
1221
1222 rc = _libelf_write_elf(e, rc, &extents);
1223
1224 done:
1225 _libelf_release_extents(&extents);
1226 e->e_flags &= ~LIBELF_F_SHDRS_LOADED;
1227 return (rc);
1228 }
1229