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