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