17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*7257d1b4Sraf /* Copyright (c) 1988 AT&T */
28*7257d1b4Sraf /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include "decl.h"
337c478bd9Sstevel@tonic-gate #include "msg.h"
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate * This module is compiled twice, the second time having
377c478bd9Sstevel@tonic-gate * -D_ELF64 defined. The following set of macros, along
387c478bd9Sstevel@tonic-gate * with machelf.h, represent the differences between the
397c478bd9Sstevel@tonic-gate * two compilations. Be careful *not* to add any class-
407c478bd9Sstevel@tonic-gate * dependent code (anything that has elf32 or elf64 in the
417c478bd9Sstevel@tonic-gate * name) to this code without hiding it behind a switch-
427c478bd9Sstevel@tonic-gate * able macro like these.
437c478bd9Sstevel@tonic-gate */
447c478bd9Sstevel@tonic-gate #if defined(_ELF64)
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #define ELFCLASS ELFCLASS64
477c478bd9Sstevel@tonic-gate #define _elf_ehdr_init _elf64_ehdr_init
487c478bd9Sstevel@tonic-gate #define elf_newehdr elf64_newehdr
497c478bd9Sstevel@tonic-gate #define getehdr elf64_getehdr
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate #else /* else ELF32 */
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #define ELFCLASS ELFCLASS32
547c478bd9Sstevel@tonic-gate #define _elf_ehdr_init _elf32_ehdr_init
55*7257d1b4Sraf #define elf_newehdr elf32_newehdr
567c478bd9Sstevel@tonic-gate #define getehdr elf32_getehdr
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate #endif /* ELF64 */
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate Ehdr *
elf_newehdr(Elf * elf)627c478bd9Sstevel@tonic-gate elf_newehdr(Elf * elf)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate Ehdr *eh;
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate if (elf == 0)
677c478bd9Sstevel@tonic-gate return (0);
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate * If reading file, return its hdr
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate ELFWLOCK(elf)
747c478bd9Sstevel@tonic-gate if (elf->ed_myflags & EDF_READ) {
757c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
767c478bd9Sstevel@tonic-gate if ((eh = (Ehdr *)getehdr(elf)) != 0) {
777c478bd9Sstevel@tonic-gate ELFWLOCK(elf)
787c478bd9Sstevel@tonic-gate elf->ed_ehflags |= ELF_F_DIRTY;
797c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate return (eh);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate * Writing file
867c478bd9Sstevel@tonic-gate */
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate if (elf->ed_class == ELFCLASSNONE)
897c478bd9Sstevel@tonic-gate elf->ed_class = ELFCLASS;
907c478bd9Sstevel@tonic-gate else if (elf->ed_class != ELFCLASS) {
917c478bd9Sstevel@tonic-gate _elf_seterr(EREQ_CLASS, 0);
927c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
937c478bd9Sstevel@tonic-gate return (0);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate ELFUNLOCK(elf);
967c478bd9Sstevel@tonic-gate if ((eh = (Ehdr *)getehdr(elf)) != 0) { /* this cooks if necessary */
977c478bd9Sstevel@tonic-gate ELFWLOCK(elf)
987c478bd9Sstevel@tonic-gate elf->ed_ehflags |= ELF_F_DIRTY;
997c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
1007c478bd9Sstevel@tonic-gate return (eh);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate ELFWLOCK(elf)
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate if ((eh = (Ehdr *)malloc(sizeof (Ehdr))) == 0) {
1057c478bd9Sstevel@tonic-gate _elf_seterr(EMEM_EHDR, errno);
1067c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
1077c478bd9Sstevel@tonic-gate return (0);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate *eh = _elf_ehdr_init;
1107c478bd9Sstevel@tonic-gate elf->ed_myflags |= EDF_EHALLOC;
1117c478bd9Sstevel@tonic-gate elf->ed_ehflags |= ELF_F_DIRTY;
1127c478bd9Sstevel@tonic-gate elf->ed_ehdr = eh;
1137c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
1147c478bd9Sstevel@tonic-gate return (eh);
1157c478bd9Sstevel@tonic-gate }
116