1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1999 Global Technology Associates, Inc.
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: stable/11/usr.sbin/kgzip/elfhdr.c 330449 2018-03-05 07:26:05Z eadler $
29 */
30
31#include <sys/endian.h>
32#include <stddef.h>
33#include "elfhdr.h"
34
35#define KGZ_FIX_NSIZE	0	/* Run-time fixup */
36
37/*
38 * Relocatable header template.
39 */
40const struct kgz_elfhdr elfhdr = {
41    /* ELF header */
42    {
43	{
44	    ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, 	/* e_ident */
45	    ELFCLASS32, ELFDATA2LSB, EV_CURRENT, 0,
46	    'F', 'r', 'e', 'e', 'B', 'S', 'D', 0
47	},
48	htole16(ET_EXEC),				/* e_type */
49	htole16(EM_386),				/* e_machine */
50	htole32(EV_CURRENT),				/* e_version */
51	0,						/* e_entry */
52	0,						/* e_phoff */
53	htole32(offsetof(struct kgz_elfhdr, sh)),	/* e_shoff */
54	0,						/* e_flags */
55	htole16(sizeof(Elf32_Ehdr)),			/* e_ehsize */
56	0,						/* e_phentsize */
57	0,						/* e_phnum */
58	htole16(sizeof(Elf32_Shdr)),			/* e_shentsize */
59	htole16(KGZ_SHNUM),				/* e_shnum */
60	htole16(KGZ_SH_SHSTRTAB)			/* e_shstrndx */
61    },
62    /* Section header */
63    {
64	{
65	    0,						/* sh_name */
66	    htole32(SHT_NULL),				/* sh_type */
67	    0,						/* sh_flags */
68	    0,						/* sh_addr */
69	    0,						/* sh_offset */
70	    0,						/* sh_size */
71	    htole32(SHN_UNDEF),				/* sh_link */
72	    0,						/* sh_info */
73	    0,						/* sh_addralign */
74	    0						/* sh_entsize */
75	},
76	{
77	    htole32(offsetof(struct kgz_shstrtab, symtab)), /* sh_name */
78	    htole32(SHT_SYMTAB),			/* sh_type */
79	    0,						/* sh_flags */
80	    0,						/* sh_addr */
81	    htole32(offsetof(struct kgz_elfhdr, st)),	/* sh_offset */
82	    htole32(sizeof(Elf32_Sym) * KGZ_STNUM),	/* sh_size */
83	    htole32(KGZ_SH_STRTAB),			/* sh_link */
84	    htole32(1),					/* sh_info */
85	    htole32(4),					/* sh_addralign */
86	    htole32(sizeof(Elf32_Sym))			/* sh_entsize */
87	},
88	{
89	    htole32(offsetof(struct kgz_shstrtab, shstrtab)), /* sh_name */
90	    htole32(SHT_STRTAB),			/* sh_type */
91	    0,						/* sh_flags */
92	    0,						/* sh_addr */
93	    htole32(offsetof(struct kgz_elfhdr, shstrtab)), /* sh_offset */
94	    htole32(sizeof(struct kgz_shstrtab)),	/* sh_size */
95	    htole32(SHN_UNDEF),				/* sh_link */
96	    0,						/* sh_info */
97	    htole32(1),					/* sh_addralign */
98	    0						/* sh_entsize */
99	},
100	{
101	    htole32(offsetof(struct kgz_shstrtab, strtab)), /* sh_name */
102	    htole32(SHT_STRTAB),			/* sh_type */
103	    0,						/* sh_flags */
104	    0,						/* sh_addr */
105	    htole32(offsetof(struct kgz_elfhdr, strtab)), /* sh_offset */
106	    htole32(sizeof(struct kgz_strtab)),		/* sh_size */
107	    htole32(SHN_UNDEF),				/* sh_link */
108	    0,						/* sh_info */
109	    htole32(1),					/* sh_addralign */
110	    0						/* sh_entsize */
111	},
112	{
113	    htole32(offsetof(struct kgz_shstrtab, data)), /* sh_name */
114	    htole32(SHT_PROGBITS),			/* sh_type */
115	    htole32(SHF_ALLOC | SHF_WRITE),		/* sh_flags */
116	    0,						/* sh_addr */
117	    htole32(sizeof(struct kgz_elfhdr)),		/* sh_offset */
118	    htole32(sizeof(struct kgz_hdr) + KGZ_FIX_NSIZE), /* sh_size */
119	    htole32(SHN_UNDEF),				/* sh_link */
120	    0,						/* sh_info */
121	    htole32(4),					/* sh_addralign */
122	    0						/* sh_entsize */
123	}
124    },
125    /* Symbol table */
126    {
127	{
128	    0,						/* st_name */
129	    0,						/* st_value */
130	    0,						/* st_size */
131	    0,						/* st_info */
132	    0,						/* st_other */
133	    htole16(SHN_UNDEF)				/* st_shndx */
134	},
135	{
136	    htole32(offsetof(struct kgz_strtab, kgz)),	/* st_name */
137	    0,						/* st_value */
138	    htole32(sizeof(struct kgz_hdr)),		/* st_size */
139	    ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),	/* st_info */
140	    0,						/* st_other */
141	    htole16(KGZ_SH_DATA)			/* st_shndx */
142	},
143	{
144	    htole32(offsetof(struct kgz_strtab, kgz_ndata)), /* st_name */
145	    htole32(sizeof(struct kgz_hdr)),		/* st_value */
146	    htole32(KGZ_FIX_NSIZE),			/* st_size */
147	    ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT),	/* st_info */
148	    0,						/* st_other */
149	    htole16(KGZ_SH_DATA)			/* st_shndx */
150	}
151    },
152    /* Section header string table */
153    {
154	KGZ_SHSTR_ZERO, 				/* zero */
155	KGZ_SHSTR_SYMTAB,				/* symtab */
156	KGZ_SHSTR_SHSTRTAB,				/* shstrtab */
157	KGZ_SHSTR_STRTAB,				/* strtab */
158	KGZ_SHSTR_DATA					/* data */
159    },
160    /* String table */
161    {
162	KGZ_STR_ZERO,					/* zero */
163	KGZ_STR_KGZ,					/* kgz */
164	KGZ_STR_KGZ_NDATA				/* kgz_ndata */
165    }
166};
167