xref: /openbsd-src/lib/libelf/elf_next.c (revision e219834f801e78888637194a4612bc3229c35e9c)
1a1b5ec25Sjsg /*-
2a1b5ec25Sjsg  * Copyright (c) 2006,2008 Joseph Koshy
3a1b5ec25Sjsg  * All rights reserved.
4a1b5ec25Sjsg  *
5a1b5ec25Sjsg  * Redistribution and use in source and binary forms, with or without
6a1b5ec25Sjsg  * modification, are permitted provided that the following conditions
7a1b5ec25Sjsg  * are met:
8a1b5ec25Sjsg  * 1. Redistributions of source code must retain the above copyright
9a1b5ec25Sjsg  *    notice, this list of conditions and the following disclaimer.
10a1b5ec25Sjsg  * 2. Redistributions in binary form must reproduce the above copyright
11a1b5ec25Sjsg  *    notice, this list of conditions and the following disclaimer in the
12a1b5ec25Sjsg  *    documentation and/or other materials provided with the distribution.
13a1b5ec25Sjsg  *
14a1b5ec25Sjsg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a1b5ec25Sjsg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a1b5ec25Sjsg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a1b5ec25Sjsg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a1b5ec25Sjsg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a1b5ec25Sjsg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a1b5ec25Sjsg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a1b5ec25Sjsg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a1b5ec25Sjsg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a1b5ec25Sjsg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a1b5ec25Sjsg  * SUCH DAMAGE.
25a1b5ec25Sjsg  */
26a1b5ec25Sjsg 
27a1b5ec25Sjsg #include <ar.h>
28a1b5ec25Sjsg #include <assert.h>
29a1b5ec25Sjsg #include <libelf.h>
30a1b5ec25Sjsg 
31a1b5ec25Sjsg #include "_libelf.h"
32a1b5ec25Sjsg 
33*e219834fSjsg ELFTC_VCSID("$Id: elf_next.c,v 1.2 2019/03/19 02:31:35 jsg Exp $");
34a1b5ec25Sjsg 
35a1b5ec25Sjsg Elf_Cmd
elf_next(Elf * e)36a1b5ec25Sjsg elf_next(Elf *e)
37a1b5ec25Sjsg {
38a1b5ec25Sjsg 	off_t next;
39a1b5ec25Sjsg 	Elf *parent;
40a1b5ec25Sjsg 
41a1b5ec25Sjsg 	if (e == NULL)
42a1b5ec25Sjsg 		return (ELF_C_NULL);
43a1b5ec25Sjsg 
44a1b5ec25Sjsg 	 if ((parent = e->e_parent) == NULL) {
45a1b5ec25Sjsg 		 LIBELF_SET_ERROR(ARGUMENT, 0);
46a1b5ec25Sjsg 		 return (ELF_C_NULL);
47a1b5ec25Sjsg 	 }
48a1b5ec25Sjsg 
49a1b5ec25Sjsg 	assert(parent->e_kind == ELF_K_AR);
50a1b5ec25Sjsg 	assert(parent->e_cmd == ELF_C_READ);
51a1b5ec25Sjsg 	assert(e->e_rawfile > parent->e_rawfile);
52a1b5ec25Sjsg 
53a1b5ec25Sjsg 	next = e->e_rawfile - parent->e_rawfile + (off_t) e->e_rawsize;
54a1b5ec25Sjsg 	next = (next + 1) & ~1;	/* round up to an even boundary */
55a1b5ec25Sjsg 
56a1b5ec25Sjsg 	/*
57a1b5ec25Sjsg 	 * Setup the 'e_next' field of the archive descriptor for the
58a1b5ec25Sjsg 	 * next call to 'elf_begin()'.
59a1b5ec25Sjsg 	 */
60a1b5ec25Sjsg 	parent->e_u.e_ar.e_next = (next >= (off_t) parent->e_rawsize) ?
61a1b5ec25Sjsg 	    (off_t) 0 : next;
62a1b5ec25Sjsg 
63*e219834fSjsg 	/*
64*e219834fSjsg 	 * Return an error if the 'e_next' field falls outside the current
65*e219834fSjsg 	 * file.
66*e219834fSjsg 	 *
67*e219834fSjsg 	 * This check is performed after updating the parent descriptor's
68*e219834fSjsg 	 * 'e_next' field so that the next call to elf_begin(3) will terminate
69*e219834fSjsg 	 * traversal of a too-small archive even if client code forgets to
70*e219834fSjsg 	 * check the return value from elf_next(3).
71*e219834fSjsg 	 */
72*e219834fSjsg 	if (next > (off_t) parent->e_rawsize) {
73*e219834fSjsg 		LIBELF_SET_ERROR(ARGUMENT, 0);
74*e219834fSjsg 		return (ELF_C_NULL);
75*e219834fSjsg 	}
76*e219834fSjsg 
77a1b5ec25Sjsg 	return (ELF_C_READ);
78a1b5ec25Sjsg }
79