2 * Copyright (c) 2006-2008 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
44 * Update the internal data structures associated with an ELF object.
45 * Returns the size in bytes the ELF object would occupy in its file
48 * After a successful call to this function, the following structures
51 * - The ELF header is updated.
52 * - All sections are sorted in order of ascending addresses and their
53 * section header table entries updated. An error is signalled
54 * if an overlap was detected among sections.
55 * - All data descriptors associated with a section are sorted in order
56 * of ascending addresses. Overlaps, if detected, are signalled as
57 * errors. Other sanity checks for alignments, section types etc. are
60 * After a resync_elf() successfully returns, the ELF descriptor is
61 * ready for being handed over to _libelf_write_elf().
67 * XXX: how do we handle 'flags'.
71 * Compute the extents of a section, by looking at the data
72 * descriptors associated with it. The function returns zero if an
73 * error was detected. `*rc' holds the maximum file extent seen so
77 _libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t *rc)
84 uint64_t sh_align, sh_entsize, sh_offset, sh_size;
85 uint64_t scn_size, scn_alignment;
88 * We need to recompute library private data structures if one
89 * or more of the following is true:
90 * - The underlying Shdr structure has been marked `dirty'. Significant
91 * fields include: `sh_offset', `sh_type', `sh_size', `sh_addralign'.
92 * - The Elf_Data structures part of this section have been marked
93 * `dirty'. Affected members include `d_align', `d_offset', `d_type',
95 * - The section as a whole is `dirty', e.g., it has been allocated
96 * using elf_newscn(), or if a new Elf_Data structure was added using
99 * Each of these conditions would result in the ELF_F_DIRTY bit being
100 * set on the section descriptor's `s_flags' field.
105 if (ec == ELFCLASS32) {
106 sh_type = s->s_shdr.s_shdr32.sh_type;
107 sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
108 sh_entsize = (uint64_t) s->s_shdr.s_shdr32.sh_entsize;
109 sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
110 sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size;
112 sh_type = s->s_shdr.s_shdr64.sh_type;
113 sh_align = s->s_shdr.s_shdr64.sh_addralign;
114 sh_entsize = s->s_shdr.s_shdr64.sh_entsize;
115 sh_offset = s->s_shdr.s_shdr64.sh_offset;
116 sh_size = s->s_shdr.s_shdr64.sh_size;
119 if (sh_type == SHT_NULL || sh_type == SHT_NOBITS)
122 if ((s->s_flags & ELF_F_DIRTY) == 0) {
123 if ((size_t) *rc < sh_offset + sh_size)
124 *rc = sh_offset + sh_size;
128 elftype = _libelf_xlate_shtype(sh_type);
129 if (elftype > ELF_T_LAST) {
130 LIBELF_SET_ERROR(SECTION, 0);
135 * Compute the extent of the data descriptors associated with
140 sh_align = _libelf_falign(elftype, ec);
142 /* Compute the section alignment. */
143 STAILQ_FOREACH(d, &s->s_data, d_next) {
144 if (d->d_type != elftype) {
145 LIBELF_SET_ERROR(DATA, 0);
148 if (d->d_version != e->e_version) {
149 LIBELF_SET_ERROR(VERSION, 0);
152 if ((d_align = d->d_align) % sh_align) {
153 LIBELF_SET_ERROR(LAYOUT, 0);
156 if (d_align == 0 || (d_align & (d_align - 1))) {
157 LIBELF_SET_ERROR(DATA, 0);
160 if (d_align > scn_alignment)
161 scn_alignment = d_align;
166 STAILQ_FOREACH_SAFE(d, &s->s_data, d_next, td) {
167 if (e->e_flags & ELF_F_LAYOUT) {
168 if ((uint64_t) d->d_off + d->d_size > scn_size)
169 scn_size = d->d_off + d->d_size;
171 scn_size = roundup2(scn_size, scn_alignment);
173 scn_size += d->d_size;
178 * If the application is requesting full control over the layout
179 * of the section, check its values for sanity.
181 if (e->e_flags & ELF_F_LAYOUT) {
182 if (scn_alignment > sh_align || sh_offset % sh_align ||
183 sh_size < scn_size) {
184 LIBELF_SET_ERROR(LAYOUT, 0);
189 * Otherwise compute the values in the section header.
192 if (scn_alignment > sh_align)
193 sh_align = scn_alignment;
196 * If the section entry size is zero, try and fill in an
197 * appropriate entry size. Per the elf(5) manual page
198 * sections without fixed-size entries should have their
199 * 'sh_entsize' field set to zero.
201 if (sh_entsize == 0 &&
202 (sh_entsize = _libelf_fsize(elftype, ec, e->e_version,
207 sh_offset = roundup(*rc, sh_align);
209 if (ec == ELFCLASS32) {
210 s->s_shdr.s_shdr32.sh_addralign = (uint32_t) sh_align;
211 s->s_shdr.s_shdr32.sh_entsize = (uint32_t) sh_entsize;
212 s->s_shdr.s_shdr32.sh_offset = (uint32_t) sh_offset;
213 s->s_shdr.s_shdr32.sh_size = (uint32_t) sh_size;
215 s->s_shdr.s_shdr64.sh_addralign = sh_align;
216 s->s_shdr.s_shdr64.sh_entsize = sh_entsize;
217 s->s_shdr.s_shdr64.sh_offset = sh_offset;
218 s->s_shdr.s_shdr64.sh_size = sh_size;
222 if ((size_t) *rc < sh_offset + sh_size)
223 *rc = sh_offset + sh_size;
226 s->s_offset = sh_offset;
232 * Insert a section in ascending order in the list
236 _libelf_insert_section(Elf *e, Elf_Scn *s)
239 uint64_t smax, smin, tmax, tmin;
242 smax = smin + s->s_size;
245 STAILQ_FOREACH(t, &e->e_u.e_elf.e_scn, s_next) {
247 tmax = tmin + t->s_size;
251 * 't' lies entirely before 's': ...| t |...| s |...
255 } else if (smax <= tmin)
257 * 's' lies entirely before 't', and after 'prevt':
258 * ...| prevt |...| s |...| t |...
261 else { /* 's' and 't' overlap. */
262 LIBELF_SET_ERROR(LAYOUT, 0);
268 STAILQ_INSERT_AFTER(&e->e_u.e_elf.e_scn, prevt, s, s_next);
270 STAILQ_INSERT_HEAD(&e->e_u.e_elf.e_scn, s, s_next);
275 _libelf_resync_sections(Elf *e, off_t rc)
279 size_t sh_type, shdr_start, shdr_end;
285 * Make a pass through sections, computing the extent of each
286 * section. Order in increasing order of addresses.
290 STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next)
291 if (_libelf_compute_section_extents(e, s, &nrc) == 0)
294 STAILQ_FOREACH_SAFE(s, &e->e_u.e_elf.e_scn, s_next, ts) {
295 if (ec == ELFCLASS32)
296 sh_type = s->s_shdr.s_shdr32.sh_type;
298 sh_type = s->s_shdr.s_shdr64.sh_type;
300 if (sh_type == SHT_NOBITS || sh_type == SHT_NULL)
303 if (s->s_offset < (uint64_t) rc) {
304 if (s->s_offset + s->s_size < (uint64_t) rc) {
306 * Try insert this section in the
307 * correct place in the list,
308 * detecting overlaps if any.
310 STAILQ_REMOVE(&e->e_u.e_elf.e_scn, s, _Elf_Scn,
312 if (_libelf_insert_section(e, s) == 0)
315 LIBELF_SET_ERROR(LAYOUT, 0);
319 rc = s->s_offset + s->s_size;
323 * If the application is controlling file layout, check for an
324 * overlap between this section's extents and the SHDR table.
326 if (e->e_flags & ELF_F_LAYOUT) {
328 if (e->e_class == ELFCLASS32)
329 shdr_start = e->e_u.e_elf.e_ehdr.e_ehdr32->e_shoff;
331 shdr_start = e->e_u.e_elf.e_ehdr.e_ehdr64->e_shoff;
333 shdr_end = shdr_start + _libelf_fsize(ELF_T_SHDR, e->e_class,
334 e->e_version, e->e_u.e_elf.e_nscn);
336 STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next) {
337 if (s->s_offset >= shdr_end ||
338 s->s_offset + s->s_size <= shdr_start)
340 LIBELF_SET_ERROR(LAYOUT, 0);
351 _libelf_resync_elf(Elf *e)
353 int ec, eh_class, eh_type;
354 unsigned int eh_byteorder, eh_version;
357 off_t rc, phoff, shoff;
366 assert(ec == ELFCLASS32 || ec == ELFCLASS64);
371 if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
377 if (ec == ELFCLASS32) {
378 eh_byteorder = eh32->e_ident[EI_DATA];
379 eh_class = eh32->e_ident[EI_CLASS];
380 phoff = (uint64_t) eh32->e_phoff;
381 shoff = (uint64_t) eh32->e_shoff;
382 eh_type = eh32->e_type;
383 eh_version = eh32->e_version;
385 eh_byteorder = eh64->e_ident[EI_DATA];
386 eh_class = eh64->e_ident[EI_CLASS];
387 phoff = eh64->e_phoff;
388 shoff = eh64->e_shoff;
389 eh_type = eh64->e_type;
390 eh_version = eh64->e_version;
393 if (eh_version == EV_NONE)
394 eh_version = EV_CURRENT;
396 if (eh_version != e->e_version) { /* always EV_CURRENT */
397 LIBELF_SET_ERROR(VERSION, 0);
401 if (eh_class != e->e_class) {
402 LIBELF_SET_ERROR(CLASS, 0);
406 if (e->e_cmd != ELF_C_WRITE && eh_byteorder != e->e_byteorder) {
407 LIBELF_SET_ERROR(HEADER, 0);
411 shnum = e->e_u.e_elf.e_nscn;
412 phnum = e->e_u.e_elf.e_nphdr;
414 e->e_byteorder = eh_byteorder;
416 #define INITIALIZE_EHDR(E,EC,V) do { \
417 (E)->e_ident[EI_MAG0] = ELFMAG0; \
418 (E)->e_ident[EI_MAG1] = ELFMAG1; \
419 (E)->e_ident[EI_MAG2] = ELFMAG2; \
420 (E)->e_ident[EI_MAG3] = ELFMAG3; \
421 (E)->e_ident[EI_CLASS] = (EC); \
422 (E)->e_ident[EI_VERSION] = (V); \
423 (E)->e_ehsize = _libelf_fsize(ELF_T_EHDR, (EC), (V), \
425 (E)->e_phentsize = (phnum == 0) ? 0 : _libelf_fsize( \
426 ELF_T_PHDR, (EC), (V), (size_t) 1); \
427 (E)->e_shentsize = _libelf_fsize(ELF_T_SHDR, (EC), (V), \
431 if (ec == ELFCLASS32)
432 INITIALIZE_EHDR(eh32, ec, eh_version);
434 INITIALIZE_EHDR(eh64, ec, eh_version);
436 (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY);
438 rc += _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1);
441 * Compute the layout the program header table, if one is
442 * present. The program header table needs to be aligned to a
443 * `natural' boundary.
446 fsz = _libelf_fsize(ELF_T_PHDR, ec, eh_version, phnum);
447 align = _libelf_falign(ELF_T_PHDR, ec);
449 if (e->e_flags & ELF_F_LAYOUT) {
451 * Check offsets for sanity.
454 LIBELF_SET_ERROR(HEADER, 0);
459 LIBELF_SET_ERROR(LAYOUT, 0);
464 phoff = roundup(rc, align);
471 * Compute the layout of the sections associated with the
475 if (e->e_cmd != ELF_C_WRITE &&
476 (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
477 _libelf_load_scn(e, ehdr) == 0)
480 if ((rc = _libelf_resync_sections(e, rc)) < 0)
484 * Compute the space taken up by the section header table, if
485 * one is needed. If ELF_F_LAYOUT is asserted, the
486 * application may have placed the section header table in
487 * between existing sections, so the net size of the file need
488 * not increase due to the presence of the section header
492 fsz = _libelf_fsize(ELF_T_SHDR, ec, eh_version, (size_t) 1);
493 align = _libelf_falign(ELF_T_SHDR, ec);
495 if (e->e_flags & ELF_F_LAYOUT) {
497 LIBELF_SET_ERROR(LAYOUT, 0);
501 shoff = roundup(rc, align);
503 if (shoff + fsz * shnum > (size_t) rc)
504 rc = shoff + fsz * shnum;
509 * Set the fields of the Executable Header that could potentially use
510 * extended numbering.
512 _libelf_setphnum(e, ehdr, ec, phnum);
513 _libelf_setshnum(e, ehdr, ec, shnum);
516 * Update the `e_phoff' and `e_shoff' fields if the library is
519 if ((e->e_flags & ELF_F_LAYOUT) == 0) {
520 if (ec == ELFCLASS32) {
521 eh32->e_phoff = (uint32_t) phoff;
522 eh32->e_shoff = (uint32_t) shoff;
524 eh64->e_phoff = (uint64_t) phoff;
525 eh64->e_shoff = (uint64_t) shoff;
533 * Write out the contents of a section.
537 _libelf_write_scn(Elf *e, char *nf, Elf_Scn *s, off_t rc)
540 size_t fsz, msz, nobjects;
542 uint64_t sh_off, sh_size;
546 if ((ec = e->e_class) == ELFCLASS32) {
547 sh_type = s->s_shdr.s_shdr32.sh_type;
548 sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size;
550 sh_type = s->s_shdr.s_shdr64.sh_type;
551 sh_size = s->s_shdr.s_shdr64.sh_size;
555 * Ignore sections that do not allocate space in the file.
557 if (sh_type == SHT_NOBITS || sh_type == SHT_NULL || sh_size == 0)
560 elftype = _libelf_xlate_shtype(sh_type);
561 assert(elftype >= ELF_T_FIRST && elftype <= ELF_T_LAST);
563 msz = _libelf_msize(elftype, ec, e->e_version);
565 sh_off = s->s_offset;
566 assert(sh_off % _libelf_falign(elftype, ec) == 0);
569 * If the section has a `rawdata' descriptor, and the section
570 * contents have not been modified, use its contents directly.
571 * The `s_rawoff' member contains the offset into the original
572 * file, while `s_offset' contains its new location in the
576 if (STAILQ_EMPTY(&s->s_data)) {
578 if ((d = elf_rawdata(s, NULL)) == NULL)
581 STAILQ_FOREACH(d, &s->s_rawdata, d_next) {
582 if ((uint64_t) rc < sh_off + d->d_off)
583 (void) memset(nf + rc,
584 LIBELF_PRIVATE(fillchar), sh_off +
586 rc = sh_off + d->d_off;
588 assert(d->d_buf != NULL);
589 assert(d->d_type == ELF_T_BYTE);
590 assert(d->d_version == e->e_version);
592 (void) memcpy(nf + rc,
593 e->e_rawfile + s->s_rawoff + d->d_off, d->d_size);
602 * Iterate over the set of data descriptors for this section.
603 * The prior call to _libelf_resync_elf() would have setup the
604 * descriptors for this step.
607 dst.d_version = e->e_version;
609 STAILQ_FOREACH(d, &s->s_data, d_next) {
611 if ((uint64_t) rc < sh_off + d->d_off)
612 (void) memset(nf + rc,
613 LIBELF_PRIVATE(fillchar), sh_off + d->d_off - rc);
615 rc = sh_off + d->d_off;
617 assert(d->d_buf != NULL);
618 assert(d->d_type == (Elf_Type) elftype);
619 assert(d->d_version == e->e_version);
620 assert(d->d_size % msz == 0);
622 nobjects = d->d_size / msz;
624 fsz = _libelf_fsize(elftype, ec, e->e_version, nobjects);
629 if (_libelf_xlate(&dst, d, e->e_byteorder, ec, ELF_TOFILE) ==
640 * Write out the file image.
642 * The original file could have been mapped in with an ELF_C_RDWR
643 * command and the application could have added new content or
644 * re-arranged its sections before calling elf_update(). Consequently
645 * its not safe to work `in place' on the original file. So we
646 * malloc() the required space for the updated ELF object and build
647 * the object there and write it out to the underlying file at the
648 * end. Note that the application may have opened the underlying file
649 * in ELF_C_RDWR and only retrieved/modified a few sections. We take
650 * care to avoid translating file sections unnecessarily.
652 * Gaps in the coverage of the file by the file's sections will be
653 * filled with the fill character set by elf_fill(3).
657 _libelf_write_elf(Elf *e, off_t newsize)
661 size_t fsz, msz, phnum, shnum;
662 uint64_t phoff, shoff;
670 assert(e->e_kind == ELF_K_ELF);
671 assert(e->e_cmd != ELF_C_READ);
672 assert(e->e_fd >= 0);
674 if ((newfile = malloc((size_t) newsize)) == NULL) {
675 LIBELF_SET_ERROR(RESOURCE, errno);
681 ehdr = _libelf_ehdr(e, ec, 0);
682 assert(ehdr != NULL);
684 phnum = e->e_u.e_elf.e_nphdr;
686 if (ec == ELFCLASS32) {
687 eh32 = (Elf32_Ehdr *) ehdr;
689 phoff = (uint64_t) eh32->e_phoff;
690 shnum = eh32->e_shnum;
691 shoff = (uint64_t) eh32->e_shoff;
693 eh64 = (Elf64_Ehdr *) ehdr;
695 phoff = eh64->e_phoff;
696 shnum = eh64->e_shnum;
697 shoff = eh64->e_shoff;
700 fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
701 msz = _libelf_msize(ELF_T_EHDR, ec, e->e_version);
703 (void) memset(&dst, 0, sizeof(dst));
704 (void) memset(&src, 0, sizeof(src));
708 src.d_type = ELF_T_EHDR;
709 src.d_version = dst.d_version = e->e_version;
713 dst.d_buf = newfile + rc;
716 if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) ==
723 * Write the program header table if present.
726 if (phnum != 0 && phoff != 0) {
727 assert((unsigned) rc <= phoff);
729 fsz = _libelf_fsize(ELF_T_PHDR, ec, e->e_version, phnum);
731 assert(phoff % _libelf_falign(ELF_T_PHDR, ec) == 0);
734 src.d_buf = _libelf_getphdr(e, ec);
735 src.d_version = dst.d_version = e->e_version;
736 src.d_type = ELF_T_PHDR;
737 src.d_size = phnum * _libelf_msize(ELF_T_PHDR, ec,
742 if ((uint64_t) rc < phoff)
743 (void) memset(newfile + rc,
744 LIBELF_PRIVATE(fillchar), phoff - rc);
746 dst.d_buf = newfile + rc;
748 if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) ==
756 * Write out individual sections.
759 STAILQ_FOREACH(scn, &e->e_u.e_elf.e_scn, s_next)
760 if ((rc = _libelf_write_scn(e, newfile, scn, rc)) < 0)
764 * Write out the section header table, if required. Note that
765 * if flag ELF_F_LAYOUT has been set the section header table
766 * could reside in between byte ranges mapped by section
769 if (shnum != 0 && shoff != 0) {
770 if ((uint64_t) rc < shoff)
771 (void) memset(newfile + rc,
772 LIBELF_PRIVATE(fillchar), shoff - rc);
777 assert(rc % _libelf_falign(ELF_T_SHDR, ec) == 0);
779 src.d_type = ELF_T_SHDR;
780 src.d_size = _libelf_msize(ELF_T_SHDR, ec, e->e_version);
781 src.d_version = dst.d_version = e->e_version;
783 fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
785 STAILQ_FOREACH(scn, &e->e_u.e_elf.e_scn, s_next) {
786 if (ec == ELFCLASS32)
787 src.d_buf = &scn->s_shdr.s_shdr32;
789 src.d_buf = &scn->s_shdr.s_shdr64;
792 dst.d_buf = newfile + rc + scn->s_ndx * fsz;
794 if (_libelf_xlate(&dst, &src, e->e_byteorder, ec,
799 rc += e->e_u.e_elf.e_nscn * fsz;
804 assert(rc == newsize);
807 * Write out the constructed contents and remap the file in
811 if (e->e_rawfile && munmap(e->e_rawfile, e->e_rawsize) < 0) {
812 LIBELF_SET_ERROR(IO, errno);
816 if (write(e->e_fd, newfile, (size_t) newsize) != newsize ||
817 lseek(e->e_fd, (off_t) 0, SEEK_SET) < 0) {
818 LIBELF_SET_ERROR(IO, errno);
822 if (e->e_cmd != ELF_C_WRITE) {
823 if ((e->e_rawfile = mmap(NULL, (size_t) newsize, PROT_READ,
824 MAP_PRIVATE, e->e_fd, (off_t) 0)) == MAP_FAILED) {
825 LIBELF_SET_ERROR(IO, errno);
828 e->e_rawsize = newsize;
832 * Reset flags, remove existing section descriptors and
833 * {E,P}HDR pointers so that a subsequent elf_get{e,p}hdr()
834 * and elf_getscn() will function correctly.
837 e->e_flags &= ~ELF_F_DIRTY;
839 STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, tscn)
840 _libelf_release_scn(scn);
842 if (ec == ELFCLASS32) {
843 free(e->e_u.e_elf.e_ehdr.e_ehdr32);
844 if (e->e_u.e_elf.e_phdr.e_phdr32)
845 free(e->e_u.e_elf.e_phdr.e_phdr32);
847 e->e_u.e_elf.e_ehdr.e_ehdr32 = NULL;
848 e->e_u.e_elf.e_phdr.e_phdr32 = NULL;
850 free(e->e_u.e_elf.e_ehdr.e_ehdr64);
851 if (e->e_u.e_elf.e_phdr.e_phdr64)
852 free(e->e_u.e_elf.e_phdr.e_phdr64);
854 e->e_u.e_elf.e_ehdr.e_ehdr64 = NULL;
855 e->e_u.e_elf.e_phdr.e_phdr64 = NULL;
869 elf_update(Elf *e, Elf_Cmd c)
876 if (e == NULL || e->e_kind != ELF_K_ELF ||
877 (c != ELF_C_NULL && c != ELF_C_WRITE)) {
878 LIBELF_SET_ERROR(ARGUMENT, 0);
882 if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
883 LIBELF_SET_ERROR(CLASS, 0);
887 if (e->e_version == EV_NONE)
888 e->e_version = EV_CURRENT;
890 if (c == ELF_C_WRITE && e->e_cmd == ELF_C_READ) {
891 LIBELF_SET_ERROR(MODE, 0);
895 if ((rc = _libelf_resync_elf(e)) < 0)
901 if (e->e_cmd == ELF_C_READ) {
903 * This descriptor was opened in read-only mode or by
907 LIBELF_SET_ERROR(MODE, 0);
909 LIBELF_SET_ERROR(ARGUMENT, 0);
914 LIBELF_SET_ERROR(SEQUENCE, 0);
918 return (_libelf_write_elf(e, rc));