1*5ac3bc71Schristos /* $NetBSD: elf_next.c,v 1.5 2024/03/03 17:37:33 christos Exp $ */
2e81373b4Schristos
39dd9d0cfSchristos /*-
49dd9d0cfSchristos * Copyright (c) 2006,2008 Joseph Koshy
59dd9d0cfSchristos * All rights reserved.
69dd9d0cfSchristos *
79dd9d0cfSchristos * Redistribution and use in source and binary forms, with or without
89dd9d0cfSchristos * modification, are permitted provided that the following conditions
99dd9d0cfSchristos * are met:
109dd9d0cfSchristos * 1. Redistributions of source code must retain the above copyright
119dd9d0cfSchristos * notice, this list of conditions and the following disclaimer.
129dd9d0cfSchristos * 2. Redistributions in binary form must reproduce the above copyright
139dd9d0cfSchristos * notice, this list of conditions and the following disclaimer in the
149dd9d0cfSchristos * documentation and/or other materials provided with the distribution.
159dd9d0cfSchristos *
169dd9d0cfSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179dd9d0cfSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189dd9d0cfSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199dd9d0cfSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209dd9d0cfSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219dd9d0cfSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229dd9d0cfSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239dd9d0cfSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249dd9d0cfSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259dd9d0cfSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269dd9d0cfSchristos * SUCH DAMAGE.
279dd9d0cfSchristos */
289dd9d0cfSchristos
29e81373b4Schristos #if HAVE_NBTOOL_CONFIG_H
30e81373b4Schristos # include "nbtool_config.h"
31e81373b4Schristos #endif
32e81373b4Schristos
339dd9d0cfSchristos #include <sys/cdefs.h>
349dd9d0cfSchristos
359dd9d0cfSchristos #include <ar.h>
369dd9d0cfSchristos #include <assert.h>
379dd9d0cfSchristos #include <libelf.h>
389dd9d0cfSchristos
399dd9d0cfSchristos #include "_libelf.h"
409dd9d0cfSchristos
41*5ac3bc71Schristos __RCSID("$NetBSD: elf_next.c,v 1.5 2024/03/03 17:37:33 christos Exp $");
429dd9d0cfSchristos
439dd9d0cfSchristos Elf_Cmd
elf_next(Elf * e)449dd9d0cfSchristos elf_next(Elf *e)
459dd9d0cfSchristos {
469dd9d0cfSchristos off_t next;
479dd9d0cfSchristos Elf *parent;
489dd9d0cfSchristos
499dd9d0cfSchristos if (e == NULL)
509dd9d0cfSchristos return (ELF_C_NULL);
519dd9d0cfSchristos
529dd9d0cfSchristos if ((parent = e->e_parent) == NULL) {
539dd9d0cfSchristos LIBELF_SET_ERROR(ARGUMENT, 0);
549dd9d0cfSchristos return (ELF_C_NULL);
559dd9d0cfSchristos }
569dd9d0cfSchristos
579dd9d0cfSchristos assert(parent->e_kind == ELF_K_AR);
589dd9d0cfSchristos assert(parent->e_cmd == ELF_C_READ);
599dd9d0cfSchristos assert(e->e_rawfile > parent->e_rawfile);
609dd9d0cfSchristos
6142bd3019Schristos next = e->e_rawfile - parent->e_rawfile + (off_t) e->e_rawsize;
629dd9d0cfSchristos next = (next + 1) & ~1; /* round up to an even boundary */
639dd9d0cfSchristos
6442bd3019Schristos /*
6542bd3019Schristos * Setup the 'e_next' field of the archive descriptor for the
6642bd3019Schristos * next call to 'elf_begin()'.
6742bd3019Schristos */
689dd9d0cfSchristos parent->e_u.e_ar.e_next = (next >= (off_t) parent->e_rawsize) ?
699dd9d0cfSchristos (off_t) 0 : next;
709dd9d0cfSchristos
71*5ac3bc71Schristos /*
72*5ac3bc71Schristos * Return an error if the 'e_next' field falls outside the current
73*5ac3bc71Schristos * file.
74*5ac3bc71Schristos *
75*5ac3bc71Schristos * This check is performed after updating the parent descriptor's
76*5ac3bc71Schristos * 'e_next' field so that the next call to elf_begin(3) will terminate
77*5ac3bc71Schristos * traversal of a too-small archive even if client code forgets to
78*5ac3bc71Schristos * check the return value from elf_next(3).
79*5ac3bc71Schristos */
80*5ac3bc71Schristos if (next > (off_t) parent->e_rawsize) {
81*5ac3bc71Schristos LIBELF_SET_ERROR(ARGUMENT, 0);
82*5ac3bc71Schristos return (ELF_C_NULL);
83*5ac3bc71Schristos }
84*5ac3bc71Schristos
859dd9d0cfSchristos return (ELF_C_READ);
869dd9d0cfSchristos }
87