]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libelf/elf_update.c
This commit was generated by cvs2svn to compensate for changes in r172314,
[FreeBSD/FreeBSD.git] / lib / libelf / elf_update.c
1 /*-
2  * Copyright (c) 2006 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/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/mman.h>
31 #include <sys/param.h>
32
33 #include <assert.h>
34 #include <errno.h>
35 #include <gelf.h>
36 #include <libelf.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "_libelf.h"
42
43 /*
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
46  * representation.
47  *
48  * After a successful call to this function, the following structures
49  * are updated:
50  *
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
58  *   made.
59  *
60  * After a resync_elf() successfully returns, the ELF descriptor is
61  * ready for being handed over to _libelf_write_elf().
62  *
63  * File alignments:
64  * PHDR - Addr
65  * SHDR - Addr
66  *
67  * XXX: how do we handle 'flags'.
68  */
69
70 /*
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
74  * far.
75  */
76 static int
77 _libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t *rc)
78 {
79         int ec;
80         Elf_Data *d, *td;
81         unsigned int elftype;
82         uint32_t sh_type;
83         uint64_t d_align;
84         uint64_t sh_align, sh_entsize, sh_offset, sh_size;
85         uint64_t scn_size, scn_alignment;
86
87         /*
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',
94          *   and `d_size'.
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
97          *   elf_newdata().
98          *
99          * Each of these conditions would result in the ELF_F_DIRTY bit being
100          * set on the section descriptor's `s_flags' field.
101          */
102
103         ec = e->e_class;
104
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;
111         } else {
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;
117         }
118
119         if (sh_type == SHT_NULL || sh_type == SHT_NOBITS)
120                 return (1);
121
122         if ((s->s_flags & ELF_F_DIRTY) == 0) {
123                 if ((size_t) *rc < sh_offset + sh_size)
124                         *rc = sh_offset + sh_size;
125                 return (1);
126         }
127
128         elftype = _libelf_xlate_shtype(sh_type);
129         if (elftype > ELF_T_LAST) {
130                 LIBELF_SET_ERROR(SECTION, 0);
131                 return (0);
132         }
133
134         /*
135          * Compute the extent of the data descriptors associated with
136          * this section.
137          */
138         scn_alignment = 0;
139         if (sh_align == 0)
140                 sh_align = _libelf_falign(elftype, ec);
141
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);
146                         return (0);
147                 }
148                 if (d->d_version != e->e_version) {
149                         LIBELF_SET_ERROR(VERSION, 0);
150                         return (0);
151                 }
152                 if ((d_align = d->d_align) % sh_align) {
153                         LIBELF_SET_ERROR(LAYOUT, 0);
154                         return (0);
155                 }
156                 if (d_align == 0 || (d_align & (d_align - 1))) {
157                         LIBELF_SET_ERROR(DATA, 0);
158                         return (0);
159                 }
160                 if (d_align > scn_alignment)
161                         scn_alignment = d_align;
162         }
163
164         scn_size = 0L;
165
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;
170                 } else {
171                         scn_size = roundup2(scn_size, scn_alignment);
172                         d->d_off = scn_size;
173                         scn_size += d->d_size;
174                 }
175         }
176
177         /*
178          * If the application is requesting full control over the layout
179          * of the section, check its values for sanity.
180          */
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);
185                         return (0);
186                 }
187         } else {
188                 /*
189                  * Otherwise compute the values in the section header.
190                  */
191
192                 if (scn_alignment > sh_align)
193                         sh_align = scn_alignment;
194
195                 /*
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.
200                  */
201                 if (sh_entsize == 0 &&
202                     (sh_entsize = _libelf_fsize(elftype, ec, e->e_version,
203                     (size_t) 1)) == 1)
204                         sh_entsize = 0;
205
206                 sh_size = scn_size;
207                 sh_offset = roundup(*rc, sh_align);
208
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;
214                 } else {
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;
219                 }
220         }
221
222         if ((size_t) *rc < sh_offset + sh_size)
223                 *rc = sh_offset + sh_size;
224
225         s->s_size = sh_size;
226         s->s_offset = sh_offset;
227         return (1);
228 }
229
230
231 /*
232  * Insert a section in ascending order in the list
233  */
234
235 static int
236 _libelf_insert_section(Elf *e, Elf_Scn *s)
237 {
238         Elf_Scn *t, *prevt;
239         uint64_t smax, smin, tmax, tmin;
240
241         smin = s->s_offset;
242         smax = smin + s->s_size;
243
244         prevt = NULL;
245         STAILQ_FOREACH(t, &e->e_u.e_elf.e_scn, s_next) {
246                 tmin = t->s_offset;
247                 tmax = tmin + t->s_size;
248
249                 /* check if there is an overlap */
250                 if (tmax < smin) {
251                         prevt = t;
252                         continue;
253                 } else if (smax < tmin)
254                         break;
255                 else {
256                         LIBELF_SET_ERROR(LAYOUT, 0);
257                         return (0);
258                 }
259         }
260
261         if (prevt)
262                 STAILQ_INSERT_AFTER(&e->e_u.e_elf.e_scn, prevt, s, s_next);
263         else
264                 STAILQ_INSERT_HEAD(&e->e_u.e_elf.e_scn, s, s_next);
265         return (1);
266 }
267
268 static off_t
269 _libelf_resync_sections(Elf *e, off_t rc)
270 {
271         int ec;
272         off_t nrc;
273         size_t sh_type, shdr_start, shdr_end;
274         Elf_Scn *s, *ts;
275
276         ec = e->e_class;
277
278         /*
279          * Make a pass through sections, computing the extent of each
280          * section. Order in increasing order of addresses.
281          */
282
283         nrc = rc;
284         STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next)
285                 if (_libelf_compute_section_extents(e, s, &nrc) == 0)
286                         return ((off_t) -1);
287
288         STAILQ_FOREACH_SAFE(s, &e->e_u.e_elf.e_scn, s_next, ts) {
289                 if (ec == ELFCLASS32)
290                         sh_type = s->s_shdr.s_shdr32.sh_type;
291                 else
292                         sh_type = s->s_shdr.s_shdr64.sh_type;
293
294                 /* XXX Do we need the 'size' field of an SHT_NOBITS section */
295                 if (sh_type == SHT_NOBITS || sh_type == SHT_NULL)
296                         continue;
297
298                 if (s->s_offset < (uint64_t) rc) {
299                         if (s->s_offset + s->s_size < (uint64_t) rc) {
300                                 /*
301                                  * Try insert this section in the
302                                  * correct place in the list,
303                                  * detecting overlaps if any.
304                                  */
305                                 STAILQ_REMOVE(&e->e_u.e_elf.e_scn, s, _Elf_Scn,
306                                     s_next);
307                                 if (_libelf_insert_section(e, s) == 0)
308                                         return ((off_t) -1);
309                         } else {
310                                 LIBELF_SET_ERROR(LAYOUT, 0);
311                                 return ((off_t) -1);
312                         }
313                 } else
314                         rc = s->s_offset + s->s_size;
315         }
316
317         /*
318          * If the application is controlling file layout, check for an
319          * overlap between this section's extents and the SHDR table.
320          */
321         if (e->e_flags & ELF_F_LAYOUT) {
322
323                 if (e->e_class == ELFCLASS32)
324                         shdr_start = e->e_u.e_elf.e_ehdr.e_ehdr32->e_shoff;
325                 else
326                         shdr_start = e->e_u.e_elf.e_ehdr.e_ehdr64->e_shoff;
327
328                 shdr_end = shdr_start + _libelf_fsize(ELF_T_SHDR, e->e_class,
329                     e->e_version, e->e_u.e_elf.e_nscn);
330
331                 STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next) {
332                         if (s->s_offset >= shdr_end ||
333                             s->s_offset + s->s_size <= shdr_start)
334                                 continue;
335                         LIBELF_SET_ERROR(LAYOUT, 0);
336                         return ((off_t) -1);
337                 }
338         }
339
340         assert(nrc == rc);
341
342         return (rc);
343 }
344
345 static off_t
346 _libelf_resync_elf(Elf *e)
347 {
348         int ec, eh_class, eh_type;
349         unsigned int eh_byteorder, eh_version;
350         size_t align, fsz;
351         size_t phnum, shnum;
352         off_t rc, phoff, shoff;
353         void *ehdr;
354         Elf32_Ehdr *eh32;
355         Elf64_Ehdr *eh64;
356
357         rc = 0;
358
359         ec = e->e_class;
360
361         assert(ec == ELFCLASS32 || ec == ELFCLASS64);
362
363         /*
364          * Prepare the EHDR.
365          */
366         if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
367                 return ((off_t) -1);
368
369         eh32 = ehdr;
370         eh64 = ehdr;
371
372         if (ec == ELFCLASS32) {
373                 eh_byteorder = eh32->e_ident[EI_DATA];
374                 eh_class     = eh32->e_ident[EI_CLASS];
375                 phoff        = (uint64_t) eh32->e_phoff;
376                 shoff        = (uint64_t) eh32->e_shoff;
377                 eh_type      = eh32->e_type;
378                 eh_version   = eh32->e_version;
379         } else {
380                 eh_byteorder = eh64->e_ident[EI_DATA];
381                 eh_class     = eh64->e_ident[EI_CLASS];
382                 phoff        = eh64->e_phoff;
383                 shoff        = eh64->e_shoff;
384                 eh_type      = eh64->e_type;
385                 eh_version   = eh64->e_version;
386         }
387
388         if (eh_version == EV_NONE)
389                 eh_version = EV_CURRENT;
390
391         if (eh_version != e->e_version) {       /* always EV_CURRENT */
392                 LIBELF_SET_ERROR(VERSION, 0);
393                 return ((off_t) -1);
394         }
395
396         if (eh_class != e->e_class) {
397                 LIBELF_SET_ERROR(CLASS, 0);
398                 return ((off_t) -1);
399         }
400
401         if (e->e_cmd != ELF_C_WRITE && eh_byteorder != e->e_byteorder) {
402                 LIBELF_SET_ERROR(HEADER, 0);
403                 return ((off_t) -1);
404         }
405
406         shnum = e->e_u.e_elf.e_nscn;
407         phnum = e->e_u.e_elf.e_nphdr;
408
409         e->e_byteorder = eh_byteorder;
410
411 #define INITIALIZE_EHDR(E,EC,V) do {                                    \
412                 (E)->e_ident[EI_MAG0] = ELFMAG0;                        \
413                 (E)->e_ident[EI_MAG1] = ELFMAG1;                        \
414                 (E)->e_ident[EI_MAG2] = ELFMAG2;                        \
415                 (E)->e_ident[EI_MAG3] = ELFMAG3;                        \
416                 (E)->e_ident[EI_CLASS] = (EC);                          \
417                 (E)->e_ident[EI_VERSION] = (V);                         \
418                 (E)->e_ehsize = _libelf_fsize(ELF_T_EHDR, (EC), (V),    \
419                     (size_t) 1);                                        \
420                 (E)->e_phentsize = _libelf_fsize(ELF_T_PHDR, (EC), (V), \
421                     (size_t) 1);                                        \
422                 (E)->e_shentsize = _libelf_fsize(ELF_T_SHDR, (EC), (V), \
423                     (size_t) 1);                                        \
424         } while (0)
425
426         if (ec == ELFCLASS32)
427                 INITIALIZE_EHDR(eh32, ec, eh_version);
428         else
429                 INITIALIZE_EHDR(eh64, ec, eh_version);
430
431         (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY);
432
433         rc += _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1);
434
435         /*
436          * Compute the layout the program header table, if one is
437          * present.  The program header table needs to be aligned to a
438          * `natural' boundary.
439          */
440         if (phnum) {
441                 fsz = _libelf_fsize(ELF_T_PHDR, ec, eh_version, phnum);
442                 align = _libelf_falign(ELF_T_PHDR, ec);
443
444                 if (e->e_flags & ELF_F_LAYOUT) {
445                         /*
446                          * Check offsets for sanity.
447                          */
448                         if (rc > phoff) {
449                                 LIBELF_SET_ERROR(HEADER, 0);
450                                 return ((off_t) -1);
451                         }
452
453                         if (phoff % align) {
454                                 LIBELF_SET_ERROR(LAYOUT, 0);
455                                 return ((off_t) -1);
456                         }
457
458                 } else
459                         phoff = roundup(rc, align);
460
461                 rc = phoff + fsz;
462         } else
463                 phoff = 0;
464
465         /*
466          * Compute the layout of the sections associated with the
467          * file.
468          */
469
470         if ((rc = _libelf_resync_sections(e, rc)) < 0)
471                 return ((off_t) -1);
472
473         /*
474          * Compute the space taken up by the section header table, if
475          * one is needed.  If ELF_F_LAYOUT is asserted, the
476          * application may have placed the section header table in
477          * between existing sections, so the net size of the file need
478          * not increase due to the presence of the section header
479          * table.
480          */
481         if (shnum) {
482                 fsz = _libelf_fsize(ELF_T_SHDR, ec, eh_version, (size_t) 1);
483                 align = _libelf_falign(ELF_T_SHDR, ec);
484
485                 if (e->e_flags & ELF_F_LAYOUT) {
486                         if (shoff % align) {
487                                 LIBELF_SET_ERROR(LAYOUT, 0);
488                                 return ((off_t) -1);
489                         }
490                 } else
491                         shoff = roundup(rc, align);
492
493                 if (shoff + fsz * shnum > (size_t) rc)
494                         rc = shoff + fsz * shnum;
495         } else
496                 shoff = 0;
497
498         /*
499          * Set the fields of the Executable Header that could potentially use
500          * extended numbering.
501          */
502         _libelf_setphnum(e, ehdr, ec, phnum);
503         _libelf_setshnum(e, ehdr, ec, shnum);
504
505         /*
506          * Update the `e_phoff' and `e_shoff' fields if the library is
507          * doing the layout.
508          */
509         if ((e->e_flags & ELF_F_LAYOUT) == 0) {
510                 if (ec == ELFCLASS32) {
511                         eh32->e_phoff = (uint32_t) phoff;
512                         eh32->e_shoff = (uint32_t) shoff;
513                 } else {
514                         eh64->e_phoff = (uint64_t) phoff;
515                         eh64->e_shoff = (uint64_t) shoff;
516                 }
517         }
518
519         return (rc);
520 }
521
522 /*
523  * Write out the contents of a section.
524  */
525
526 static off_t
527 _libelf_write_scn(Elf *e, char *nf, Elf_Scn *s, off_t rc)
528 {
529         int ec;
530         size_t fsz, msz, nobjects;
531         uint32_t sh_type;
532         uint64_t sh_off;
533         int elftype;
534         Elf_Data *d, dst;
535
536         if ((ec = e->e_class) == ELFCLASS32)
537                 sh_type = s->s_shdr.s_shdr32.sh_type;
538         else
539                 sh_type = s->s_shdr.s_shdr64.sh_type;
540
541         /*
542          * Ignore sections that do not allocate space in the file.
543          */
544         if (sh_type == SHT_NOBITS || sh_type == SHT_NULL)
545                 return (rc);
546
547
548         elftype = _libelf_xlate_shtype(sh_type);
549         assert(elftype >= ELF_T_FIRST && elftype <= ELF_T_LAST);
550
551         msz = _libelf_msize(elftype, ec, e->e_version);
552
553         sh_off = s->s_offset;
554         assert(sh_off % _libelf_falign(elftype, ec) == 0);
555
556         /*
557          * If the section has a `rawdata' descriptor, and the section
558          * contents have not been modified, use its contents directly.
559          * The `s_rawoff' member contains the offset into the original
560          * file, while `s_offset' contains its new location in the
561          * destination.
562          */
563
564         if (STAILQ_EMPTY(&s->s_data)) {
565
566                 if ((d = elf_rawdata(s, NULL)) == NULL)
567                         return ((off_t) -1);
568
569                 STAILQ_FOREACH(d, &s->s_rawdata, d_next) {
570                         if ((uint64_t) rc < sh_off + d->d_off)
571                                 (void) memset(nf + rc,
572                                     LIBELF_PRIVATE(fillchar), sh_off +
573                                     d->d_off - rc);
574                         rc = sh_off + d->d_off;
575
576                         assert(d->d_buf != NULL);
577                         assert(d->d_type == ELF_T_BYTE);
578                         assert(d->d_version == e->e_version);
579
580                         (void) memcpy(nf + rc,
581                             e->e_rawfile + s->s_rawoff + d->d_off, d->d_size);
582
583                         rc += d->d_size;
584                 }
585
586                 return (rc);
587         }
588
589         /*
590          * Iterate over the set of data descriptors for this section.
591          * The prior call to _libelf_resync_elf() would have setup the
592          * descriptors for this step.
593          */
594
595         dst.d_version = e->e_version;
596
597         STAILQ_FOREACH(d, &s->s_data, d_next) {
598
599                 if ((uint64_t) rc < sh_off + d->d_off)
600                         (void) memset(nf + rc,
601                             LIBELF_PRIVATE(fillchar), sh_off + d->d_off - rc);
602
603                 rc = sh_off + d->d_off;
604
605                 assert(d->d_buf != NULL);
606                 assert(d->d_type == (Elf_Type) elftype);
607                 assert(d->d_version == e->e_version);
608                 assert(d->d_size % msz == 0);
609
610                 nobjects = d->d_size / msz;
611
612                 fsz = _libelf_fsize(elftype, ec, e->e_version, nobjects);
613
614                 dst.d_buf    = nf + rc;
615                 dst.d_size   = fsz;
616
617                 if (_libelf_xlate(&dst, d, e->e_byteorder, ec, ELF_TOFILE) ==
618                     NULL)
619                         return ((off_t) -1);
620
621                 rc += fsz;
622         }
623
624         return ((off_t) rc);
625 }
626
627 /*
628  * Write out the file image.
629  *
630  * The original file could have been mapped in with an ELF_C_RDWR
631  * command and the application could have added new content or
632  * re-arranged its sections before calling elf_update().  Consequently
633  * its not safe to work `in place' on the original file.  So we
634  * malloc() the required space for the updated ELF object and build
635  * the object there and write it out to the underlying file at the
636  * end.  Note that the application may have opened the underlying file
637  * in ELF_C_RDWR and only retrieved/modified a few sections.  We take
638  * care to avoid translating file sections unnecessarily.
639  *
640  * Gaps in the coverage of the file by the file's sections will be
641  * filled with the fill character set by elf_fill(3).
642  */
643
644 static off_t
645 _libelf_write_elf(Elf *e, off_t newsize)
646 {
647         int ec;
648         off_t maxrc, rc;
649         size_t fsz, msz, phnum, shnum;
650         uint64_t phoff, shoff;
651         void *ehdr;
652         char *newfile;
653         Elf_Data dst, src;
654         Elf_Scn *scn, *tscn;
655         Elf32_Ehdr *eh32;
656         Elf64_Ehdr *eh64;
657
658         assert(e->e_kind == ELF_K_ELF);
659         assert(e->e_cmd != ELF_C_READ);
660         assert(e->e_fd >= 0);
661
662         if ((newfile = malloc((size_t) newsize)) == NULL) {
663                 LIBELF_SET_ERROR(RESOURCE, errno);
664                 return ((off_t) -1);
665         }
666
667         ec = e->e_class;
668
669         ehdr = _libelf_ehdr(e, ec, 0);
670         assert(ehdr != NULL);
671
672         phnum = e->e_u.e_elf.e_nphdr;
673
674         if (ec == ELFCLASS32) {
675                 eh32 = (Elf32_Ehdr *) ehdr;
676
677                 phoff = (uint64_t) eh32->e_phoff;
678                 shnum = eh32->e_shnum;
679                 shoff = (uint64_t) eh32->e_shoff;
680         } else {
681                 eh64 = (Elf64_Ehdr *) ehdr;
682
683                 phoff = eh64->e_phoff;
684                 shnum = eh64->e_shnum;
685                 shoff = eh64->e_shoff;
686         }
687
688         fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
689         msz = _libelf_msize(ELF_T_EHDR, ec, e->e_version);
690
691         (void) memset(&dst, 0, sizeof(dst));
692         (void) memset(&src, 0, sizeof(src));
693
694         src.d_buf     = ehdr;
695         src.d_size    = msz;
696         src.d_type    = ELF_T_EHDR;
697         src.d_version = dst.d_version = e->e_version;
698
699         rc = 0;
700
701         dst.d_buf     = newfile + rc;
702         dst.d_size    = fsz;
703
704         if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) ==
705             NULL)
706                 goto error;
707
708         rc += fsz;
709
710         /*
711          * Write the program header table if present.
712          */
713
714         if (phnum != 0 && phoff != 0) {
715                 assert((unsigned) rc <= phoff);
716
717                 fsz = _libelf_fsize(ELF_T_PHDR, ec, e->e_version, phnum);
718
719                 assert(phoff % _libelf_falign(ELF_T_PHDR, ec) == 0);
720                 assert(fsz > 0);
721
722                 src.d_version = dst.d_version = e->e_version;
723                 src.d_type = ELF_T_PHDR;
724
725                 if (ec == ELFCLASS32)
726                         src.d_buf = e->e_u.e_elf.e_phdr.e_phdr32;
727                 else
728                         src.d_buf = e->e_u.e_elf.e_phdr.e_phdr64;
729
730                 src.d_size = phnum * _libelf_msize(ELF_T_PHDR, ec,
731                     e->e_version);
732
733                 dst.d_size = fsz;
734
735                 if ((uint64_t) rc < phoff)
736                         (void) memset(newfile + rc,
737                             LIBELF_PRIVATE(fillchar), phoff - rc);
738
739                 dst.d_buf = newfile + rc;
740
741                 if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) ==
742                     NULL)
743                         goto error;
744
745                 rc = phoff + fsz;
746         }
747
748         /*
749          * Write out individual sections.
750          */
751
752         STAILQ_FOREACH(scn, &e->e_u.e_elf.e_scn, s_next)
753                 if ((rc = _libelf_write_scn(e, newfile, scn, rc)) < 0)
754                         goto error;
755
756         /*
757          * Write out the section header table, if required.  Note that
758          * if flag ELF_F_LAYOUT has been set the section header table
759          * could reside in between byte ranges mapped by section
760          * descriptors.
761          */
762         if (shnum != 0 && shoff != 0) {
763                 if ((uint64_t) rc < shoff)
764                         (void) memset(newfile + rc,
765                             LIBELF_PRIVATE(fillchar), shoff - rc);
766
767                 maxrc = rc;
768                 rc = shoff;
769
770                 assert(rc % _libelf_falign(ELF_T_SHDR, ec) == 0);
771
772                 src.d_type = ELF_T_SHDR;
773                 src.d_size = _libelf_msize(ELF_T_SHDR, ec, e->e_version);
774                 src.d_version = dst.d_version = e->e_version;
775
776                 fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
777
778                 STAILQ_FOREACH(scn, &e->e_u.e_elf.e_scn, s_next) {
779                         if (ec == ELFCLASS32)
780                                 src.d_buf = &scn->s_shdr.s_shdr32;
781                         else
782                                 src.d_buf = &scn->s_shdr.s_shdr64;
783
784                         dst.d_size = fsz;
785                         dst.d_buf = newfile + rc;
786
787                         if (_libelf_xlate(&dst, &src, e->e_byteorder, ec,
788                                 ELF_TOFILE) != &dst)
789                                 goto error;
790
791                         rc += fsz;
792                 }
793
794                 if (maxrc > rc)
795                         rc = maxrc;
796         }
797
798         assert(rc == newsize);
799
800         /*
801          * Write out the constructed contents and remap the file in
802          * read-only.
803          */
804
805         if (e->e_rawfile && munmap(e->e_rawfile, e->e_rawsize) < 0) {
806                 LIBELF_SET_ERROR(IO, errno);
807                 goto error;
808         }
809
810         if (write(e->e_fd, newfile, (size_t) newsize) != newsize ||
811             lseek(e->e_fd, (off_t) 0, SEEK_SET) < 0) {
812                 LIBELF_SET_ERROR(IO, errno);
813                 goto error;
814         }
815
816         if (e->e_cmd != ELF_C_WRITE) {
817                 if ((e->e_rawfile = mmap(NULL, (size_t) newsize, PROT_READ,
818                     MAP_PRIVATE, e->e_fd, (off_t) 0)) == MAP_FAILED) {
819                         LIBELF_SET_ERROR(IO, errno);
820                         goto error;
821                 }
822                 e->e_rawsize = newsize;
823         }
824
825         /*
826          * Reset flags, remove existing section descriptors and
827          * {E,P}HDR pointers so that a subsequent elf_get{e,p}hdr()
828          * and elf_getscn() will function correctly.
829          */
830
831         e->e_flags &= ~ELF_F_DIRTY;
832
833         STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, tscn)
834                 _libelf_release_scn(scn);
835
836         if (ec == ELFCLASS32) {
837                 free(e->e_u.e_elf.e_ehdr.e_ehdr32);
838                 if (e->e_u.e_elf.e_phdr.e_phdr32)
839                         free(e->e_u.e_elf.e_phdr.e_phdr32);
840
841                 e->e_u.e_elf.e_ehdr.e_ehdr32 = NULL;
842                 e->e_u.e_elf.e_phdr.e_phdr32 = NULL;
843         } else {
844                 free(e->e_u.e_elf.e_ehdr.e_ehdr64);
845                 if (e->e_u.e_elf.e_phdr.e_phdr64)
846                         free(e->e_u.e_elf.e_phdr.e_phdr64);
847
848                 e->e_u.e_elf.e_ehdr.e_ehdr64 = NULL;
849                 e->e_u.e_elf.e_phdr.e_phdr64 = NULL;
850         }
851
852         return (rc);
853
854  error:
855         if (newfile)
856                 free(newfile);
857         return ((off_t) -1);
858 }
859
860 off_t
861 elf_update(Elf *e, Elf_Cmd c)
862 {
863         int ec;
864         off_t rc;
865
866         rc = (off_t) -1;
867
868         if (e == NULL || e->e_kind != ELF_K_ELF ||
869             (c != ELF_C_NULL && c != ELF_C_WRITE)) {
870                 LIBELF_SET_ERROR(ARGUMENT, 0);
871                 return (rc);
872         }
873
874         if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
875                 LIBELF_SET_ERROR(CLASS, 0);
876                 return (rc);
877         }
878
879         if (e->e_version == EV_NONE)
880                 e->e_version = EV_CURRENT;
881
882         if (c == ELF_C_WRITE && e->e_cmd == ELF_C_READ) {
883                 LIBELF_SET_ERROR(MODE, 0);
884                 return (rc);
885         }
886
887         if ((rc = _libelf_resync_elf(e)) < 0)
888                 return (rc);
889
890         if (c == ELF_C_NULL)
891                 return (rc);
892
893         if (e->e_cmd == ELF_C_READ) {
894                 /*
895                  * This descriptor was opened in read-only mode or by
896                  * elf_memory().
897                  */
898                 if (e->e_fd)
899                         LIBELF_SET_ERROR(MODE, 0);
900                 else
901                         LIBELF_SET_ERROR(ARGUMENT, 0);
902                 return ((off_t) -1);
903         }
904
905         if (e->e_fd < 0) {
906                 LIBELF_SET_ERROR(SEQUENCE, 0);
907                 return ((off_t) -1);
908         }
909
910         return (_libelf_write_elf(e, rc));
911 }