xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/elf_begin.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos /*	$NetBSD: elf_begin.c,v 1.5 2024/03/03 17:37:33 christos Exp $	*/
2e81373b4Schristos 
39dd9d0cfSchristos /*-
49dd9d0cfSchristos  * Copyright (c) 2006,2008-2011 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 
29*5ac3bc71Schristos #include <sys/cdefs.h>
30*5ac3bc71Schristos 
319dd9d0cfSchristos #include <libelf.h>
329dd9d0cfSchristos 
339dd9d0cfSchristos #include "_libelf.h"
349dd9d0cfSchristos 
35*5ac3bc71Schristos __RCSID("$NetBSD: elf_begin.c,v 1.5 2024/03/03 17:37:33 christos Exp $");
36*5ac3bc71Schristos ELFTC_VCSID("Id: elf_begin.c 3977 2022-05-01 06:45:34Z jkoshy");
379dd9d0cfSchristos 
389dd9d0cfSchristos Elf *
elf_begin(int fd,Elf_Cmd c,Elf * a)399dd9d0cfSchristos elf_begin(int fd, Elf_Cmd c, Elf *a)
409dd9d0cfSchristos {
419dd9d0cfSchristos 	Elf *e;
429dd9d0cfSchristos 
439dd9d0cfSchristos 	e = NULL;
449dd9d0cfSchristos 
459dd9d0cfSchristos 	if (LIBELF_PRIVATE(version) == EV_NONE) {
469dd9d0cfSchristos 		LIBELF_SET_ERROR(SEQUENCE, 0);
479dd9d0cfSchristos 		return (NULL);
489dd9d0cfSchristos 	}
499dd9d0cfSchristos 
509dd9d0cfSchristos 	switch (c) {
519dd9d0cfSchristos 	case ELF_C_NULL:
529dd9d0cfSchristos 		return (NULL);
539dd9d0cfSchristos 
549dd9d0cfSchristos 	case ELF_C_WRITE:
559dd9d0cfSchristos 		/*
569dd9d0cfSchristos 		 * The ELF_C_WRITE command is required to ignore the
579dd9d0cfSchristos 		 * descriptor passed in.
589dd9d0cfSchristos 		 */
599dd9d0cfSchristos 		a = NULL;
609dd9d0cfSchristos 		break;
619dd9d0cfSchristos 
629dd9d0cfSchristos 	case ELF_C_RDWR:
639dd9d0cfSchristos 		if (a != NULL) { /* not allowed for ar(1) archives. */
649dd9d0cfSchristos 			LIBELF_SET_ERROR(ARGUMENT, 0);
659dd9d0cfSchristos 			return (NULL);
669dd9d0cfSchristos 		}
679dd9d0cfSchristos 		/*FALLTHROUGH*/
689dd9d0cfSchristos 	case ELF_C_READ:
699dd9d0cfSchristos 		/*
709dd9d0cfSchristos 		 * Descriptor `a' could be for a regular ELF file, or
719dd9d0cfSchristos 		 * for an ar(1) archive.  If descriptor `a' was opened
729dd9d0cfSchristos 		 * using a valid file descriptor, we need to check if
739dd9d0cfSchristos 		 * the passed in `fd' value matches the original one.
749dd9d0cfSchristos 		 */
759dd9d0cfSchristos 		if (a &&
769dd9d0cfSchristos 		    ((a->e_fd != -1 && a->e_fd != fd) || c != a->e_cmd)) {
779dd9d0cfSchristos 			LIBELF_SET_ERROR(ARGUMENT, 0);
789dd9d0cfSchristos 			return (NULL);
799dd9d0cfSchristos 		}
809dd9d0cfSchristos 		break;
819dd9d0cfSchristos 
829dd9d0cfSchristos 	default:
839dd9d0cfSchristos 		LIBELF_SET_ERROR(ARGUMENT, 0);
849dd9d0cfSchristos 		return (NULL);
859dd9d0cfSchristos 
869dd9d0cfSchristos 	}
879dd9d0cfSchristos 
889dd9d0cfSchristos 	if (a == NULL)
899dd9d0cfSchristos 		e = _libelf_open_object(fd, c, 1);
909dd9d0cfSchristos 	else if (a->e_kind == ELF_K_AR)
919dd9d0cfSchristos 		e = _libelf_ar_open_member(a->e_fd, c, a);
929dd9d0cfSchristos 	else
939dd9d0cfSchristos 		(e = a)->e_activations++;
949dd9d0cfSchristos 
959dd9d0cfSchristos 	return (e);
969dd9d0cfSchristos }
97