xref: /freebsd/usr.sbin/btxld/elfh.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998 Robert Nordier
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/types.h>
32 #include <sys/endian.h>
33 
34 #include <stddef.h>
35 #include "elfh.h"
36 
37 #define SET_ME	0xeeeeeeee    /* filled in by btxld */
38 
39 /*
40  * ELF header template.
41  */
42 const struct elfh elfhdr = {
43     {
44 	{
45 	    ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,     /* e_ident */
46 	    ELFCLASS32, ELFDATA2LSB, EV_CURRENT, 0,
47 	    'F', 'r', 'e', 'e', 'B', 'S', 'D', 0
48 	},
49 	htole16(ET_EXEC),			    /* e_type */
50 	htole16(EM_386),			    /* e_machine */
51 	htole32(EV_CURRENT),			    /* e_version */
52 	htole32(SET_ME),			    /* e_entry */
53 	htole32(offsetof(struct elfh, p)),	    /* e_phoff */
54 	htole32(offsetof(struct elfh, sh)),	    /* e_shoff */
55 	0,					    /* e_flags */
56 	htole16(sizeof(elfhdr.e)),		    /* e_ehsize */
57 	htole16(sizeof(elfhdr.p[0])),		    /* e_phentsize */
58 	htole16(sizeof(elfhdr.p) / sizeof(elfhdr.p[0])), /* e_phnum */
59 	htole16(sizeof(elfhdr.sh[0])),		    /* e_shentsize */
60 	htole16(sizeof(elfhdr.sh) / sizeof(elfhdr.sh[0])), /* e_shnum */
61 	htole16(1)				    /* e_shstrndx */
62     },
63     {
64 	{
65 	    htole32(PT_LOAD),			    /* p_type */
66 	    htole32(sizeof(elfhdr)),		    /* p_offset */
67 	    htole32(SET_ME),			    /* p_vaddr */
68 	    htole32(SET_ME),			    /* p_paddr */
69 	    htole32(SET_ME),			    /* p_filesz */
70 	    htole32(SET_ME),			    /* p_memsz */
71 	    htole32(PF_R | PF_X),		    /* p_flags */
72 	    htole32(0x1000)			    /* p_align */
73 	},
74 	{
75 	    htole32(PT_LOAD),			    /* p_type */
76 	    htole32(SET_ME),			    /* p_offset */
77 	    htole32(SET_ME),			    /* p_vaddr */
78 	    htole32(SET_ME),			    /* p_paddr */
79 	    htole32(SET_ME),			    /* p_filesz */
80 	    htole32(SET_ME),			    /* p_memsz */
81 	    htole32(PF_R | PF_W),		    /* p_flags */
82 	    htole32(0x1000)			    /* p_align */
83 	}
84     },
85     {
86 	{
87 	    0, htole32(SHT_NULL), 0, 0, 0, 0, htole32(SHN_UNDEF), 0, 0, 0
88 	},
89 	{
90 	    htole32(1),				    /* sh_name */
91 	    htole32(SHT_STRTAB), 		    /* sh_type */
92 	    0,					    /* sh_flags */
93 	    0,					    /* sh_addr */
94 	    htole32(offsetof(struct elfh, shstrtab)), /* sh_offset */
95 	    htole32(sizeof(elfhdr.shstrtab)),	    /* sh_size */
96 	    htole32(SHN_UNDEF),			    /* sh_link */
97 	    0,					    /* sh_info */
98 	    htole32(1),				    /* sh_addralign */
99 	    0					    /* sh_entsize */
100 	},
101 	{
102 	    htole32(0xb),			    /* sh_name */
103 	    htole32(SHT_PROGBITS),		    /* sh_type */
104 	    htole32(SHF_EXECINSTR | SHF_ALLOC),	    /* sh_flags */
105 	    htole32(SET_ME),			    /* sh_addr */
106 	    htole32(SET_ME),			    /* sh_offset */
107 	    htole32(SET_ME),			    /* sh_size */
108 	    htole32(SHN_UNDEF),			    /* sh_link */
109 	    0,					    /* sh_info */
110 	    htole32(4),				    /* sh_addralign */
111 	    0					    /* sh_entsize */
112 	},
113 	{
114 	    htole32(0x11),			    /* sh_name */
115 	    htole32(SHT_PROGBITS),		    /* sh_type */
116 	    htole32(SHF_ALLOC | SHF_WRITE),	    /* sh_flags */
117 	    htole32(SET_ME),			    /* sh_addr */
118 	    htole32(SET_ME),			    /* sh_offset */
119 	    htole32(SET_ME),			    /* sh_size */
120 	    htole32(SHN_UNDEF),			    /* sh_link */
121 	    0,					    /* sh_info */
122 	    htole32(4),				    /* sh_addralign */
123 	    0					    /* sh_entsize */
124 	}
125     },
126     "\0.shstrtab\0.text\0.data" 		    /* shstrtab */
127 };
128