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