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