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