17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  *  GRUB  --  GRand Unified Bootloader
37c478bd9Sstevel@tonic-gate  *  Copyright (C) 2001,2002  Free Software Foundation, Inc.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  *  This program is free software; you can redistribute it and/or modify
67c478bd9Sstevel@tonic-gate  *  it under the terms of the GNU General Public License as published by
77c478bd9Sstevel@tonic-gate  *  the Free Software Foundation; either version 2 of the License, or
87c478bd9Sstevel@tonic-gate  *  (at your option) any later version.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  *  This program is distributed in the hope that it will be useful,
117c478bd9Sstevel@tonic-gate  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
127c478bd9Sstevel@tonic-gate  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
137c478bd9Sstevel@tonic-gate  *  GNU General Public License for more details.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  *  You should have received a copy of the GNU General Public License
167c478bd9Sstevel@tonic-gate  *  along with this program; if not, write to the Free Software
177c478bd9Sstevel@tonic-gate  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
187c478bd9Sstevel@tonic-gate  */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate /* 32-bit data types */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate typedef unsigned long Elf32_Addr;
237c478bd9Sstevel@tonic-gate typedef unsigned short Elf32_Half;
247c478bd9Sstevel@tonic-gate typedef unsigned long Elf32_Off;
257c478bd9Sstevel@tonic-gate typedef signed long Elf32_Sword;
267c478bd9Sstevel@tonic-gate typedef unsigned long Elf32_Word;
277c478bd9Sstevel@tonic-gate /* "unsigned char" already exists */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /* ELF header */
307c478bd9Sstevel@tonic-gate typedef struct
317c478bd9Sstevel@tonic-gate {
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #define EI_NIDENT 16
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate   /* first four characters are defined below */
367c478bd9Sstevel@tonic-gate #define EI_MAG0		0
377c478bd9Sstevel@tonic-gate #define ELFMAG0		0x7f
387c478bd9Sstevel@tonic-gate #define EI_MAG1		1
397c478bd9Sstevel@tonic-gate #define ELFMAG1		'E'
407c478bd9Sstevel@tonic-gate #define EI_MAG2		2
417c478bd9Sstevel@tonic-gate #define ELFMAG2		'L'
427c478bd9Sstevel@tonic-gate #define EI_MAG3		3
437c478bd9Sstevel@tonic-gate #define ELFMAG3		'F'
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define EI_CLASS	4	/* data sizes */
467c478bd9Sstevel@tonic-gate #define ELFCLASS32	1	/* i386 -- up to 32-bit data sizes present */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define EI_DATA		5	/* data type and ordering */
497c478bd9Sstevel@tonic-gate #define ELFDATA2LSB	1	/* i386 -- LSB 2's complement */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define EI_VERSION	6	/* version number.  "e_version" must be the same */
527c478bd9Sstevel@tonic-gate #define EV_CURRENT      1	/* current version number */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define EI_OSABI	7	/* operating system/ABI indication */
557c478bd9Sstevel@tonic-gate #define ELFOSABI_FREEBSD	9
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define EI_ABIVERSION	8	/* ABI version */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define EI_PAD		9	/* from here in is just padding */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define EI_BRAND	8	/* start of OS branding (This is
627c478bd9Sstevel@tonic-gate 				   obviously illegal against the ELF
637c478bd9Sstevel@tonic-gate 				   standard.) */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate   unsigned char e_ident[EI_NIDENT];	/* basic identification block */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define ET_EXEC		2	/* we only care about executable types */
687c478bd9Sstevel@tonic-gate   Elf32_Half e_type;		/* file types */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define EM_386		3	/* i386 -- obviously use this one */
717c478bd9Sstevel@tonic-gate   Elf32_Half e_machine;	/* machine types */
727c478bd9Sstevel@tonic-gate   Elf32_Word e_version;	/* use same as "EI_VERSION" above */
737c478bd9Sstevel@tonic-gate   Elf32_Addr e_entry;		/* entry point of the program */
747c478bd9Sstevel@tonic-gate   Elf32_Off e_phoff;		/* program header table file offset */
757c478bd9Sstevel@tonic-gate   Elf32_Off e_shoff;		/* section header table file offset */
767c478bd9Sstevel@tonic-gate   Elf32_Word e_flags;		/* flags */
777c478bd9Sstevel@tonic-gate   Elf32_Half e_ehsize;		/* elf header size in bytes */
787c478bd9Sstevel@tonic-gate   Elf32_Half e_phentsize;	/* program header entry size */
797c478bd9Sstevel@tonic-gate   Elf32_Half e_phnum;		/* number of entries in program header */
807c478bd9Sstevel@tonic-gate   Elf32_Half e_shentsize;	/* section header entry size */
817c478bd9Sstevel@tonic-gate   Elf32_Half e_shnum;		/* number of entries in section header */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define SHN_UNDEF       0
847c478bd9Sstevel@tonic-gate #define SHN_LORESERVE   0xff00
857c478bd9Sstevel@tonic-gate #define SHN_LOPROC      0xff00
867c478bd9Sstevel@tonic-gate #define SHN_HIPROC      0xff1f
877c478bd9Sstevel@tonic-gate #define SHN_ABS         0xfff1
887c478bd9Sstevel@tonic-gate #define SHN_COMMON      0xfff2
897c478bd9Sstevel@tonic-gate #define SHN_HIRESERVE   0xffff
907c478bd9Sstevel@tonic-gate   Elf32_Half e_shstrndx;	/* section header table index */
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate Elf32_Ehdr;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define BOOTABLE_I386_ELF(h) \
967c478bd9Sstevel@tonic-gate  ((h.e_ident[EI_MAG0] == ELFMAG0) & (h.e_ident[EI_MAG1] == ELFMAG1) \
977c478bd9Sstevel@tonic-gate   & (h.e_ident[EI_MAG2] == ELFMAG2) & (h.e_ident[EI_MAG3] == ELFMAG3) \
987c478bd9Sstevel@tonic-gate   & (h.e_ident[EI_CLASS] == ELFCLASS32) & (h.e_ident[EI_DATA] == ELFDATA2LSB) \
997c478bd9Sstevel@tonic-gate   & (h.e_ident[EI_VERSION] == EV_CURRENT) & (h.e_type == ET_EXEC) \
1007c478bd9Sstevel@tonic-gate   & (h.e_machine == EM_386) & (h.e_version == EV_CURRENT))
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /* section table - ? */
1037c478bd9Sstevel@tonic-gate typedef struct
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate   Elf32_Word	sh_name;		/* Section name (string tbl index) */
1067c478bd9Sstevel@tonic-gate   Elf32_Word	sh_type;		/* Section type */
1077c478bd9Sstevel@tonic-gate   Elf32_Word	sh_flags;		/* Section flags */
1087c478bd9Sstevel@tonic-gate   Elf32_Addr	sh_addr;		/* Section virtual addr at execution */
1097c478bd9Sstevel@tonic-gate   Elf32_Off	sh_offset;		/* Section file offset */
1107c478bd9Sstevel@tonic-gate   Elf32_Word	sh_size;		/* Section size in bytes */
1117c478bd9Sstevel@tonic-gate   Elf32_Word	sh_link;		/* Link to another section */
1127c478bd9Sstevel@tonic-gate   Elf32_Word	sh_info;		/* Additional section information */
1137c478bd9Sstevel@tonic-gate   Elf32_Word	sh_addralign;		/* Section alignment */
1147c478bd9Sstevel@tonic-gate   Elf32_Word	sh_entsize;		/* Entry size if section holds table */
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate Elf32_Shdr;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /* symbol table - page 4-25, figure 4-15 */
1197c478bd9Sstevel@tonic-gate typedef struct
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate   Elf32_Word st_name;
1227c478bd9Sstevel@tonic-gate   Elf32_Addr st_value;
1237c478bd9Sstevel@tonic-gate   Elf32_Word st_size;
1247c478bd9Sstevel@tonic-gate   unsigned char st_info;
1257c478bd9Sstevel@tonic-gate   unsigned char st_other;
1267c478bd9Sstevel@tonic-gate   Elf32_Half st_shndx;
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate Elf32_Sym;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /* symbol type and binding attributes - page 4-26 */
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #define ELF32_ST_BIND(i)    ((i) >> 4)
1337c478bd9Sstevel@tonic-gate #define ELF32_ST_TYPE(i)    ((i) & 0xf)
1347c478bd9Sstevel@tonic-gate #define ELF32_ST_INFO(b,t)  (((b)<<4)+((t)&0xf))
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* symbol binding - page 4-26, figure 4-16 */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define STB_LOCAL    0
1397c478bd9Sstevel@tonic-gate #define STB_GLOBAL   1
1407c478bd9Sstevel@tonic-gate #define STB_WEAK     2
1417c478bd9Sstevel@tonic-gate #define STB_LOPROC  13
1427c478bd9Sstevel@tonic-gate #define STB_HIPROC  15
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /* symbol types - page 4-28, figure 4-17 */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #define STT_NOTYPE   0
1477c478bd9Sstevel@tonic-gate #define STT_OBJECT   1
1487c478bd9Sstevel@tonic-gate #define STT_FUNC     2
1497c478bd9Sstevel@tonic-gate #define STT_SECTION  3
1507c478bd9Sstevel@tonic-gate #define STT_FILE     4
1517c478bd9Sstevel@tonic-gate #define STT_LOPROC  13
1527c478bd9Sstevel@tonic-gate #define STT_HIPROC  15
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /* Macros to split/combine relocation type and symbol page 4-32 */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #define ELF32_R_SYM(__i)	((__i)>>8)
1587c478bd9Sstevel@tonic-gate #define ELF32_R_TYPE(__i)	((unsigned char) (__i))
1597c478bd9Sstevel@tonic-gate #define ELF32_R_INFO(__s, __t)	(((__s)<<8) + (unsigned char) (__t))
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /* program header - page 5-2, figure 5-1 */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate typedef struct
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate   Elf32_Word p_type;
1677c478bd9Sstevel@tonic-gate   Elf32_Off p_offset;
1687c478bd9Sstevel@tonic-gate   Elf32_Addr p_vaddr;
1697c478bd9Sstevel@tonic-gate   Elf32_Addr p_paddr;
1707c478bd9Sstevel@tonic-gate   Elf32_Word p_filesz;
1717c478bd9Sstevel@tonic-gate   Elf32_Word p_memsz;
1727c478bd9Sstevel@tonic-gate   Elf32_Word p_flags;
1737c478bd9Sstevel@tonic-gate   Elf32_Word p_align;
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate Elf32_Phdr;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /* segment types - page 5-3, figure 5-2 */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate #define PT_NULL		0
1807c478bd9Sstevel@tonic-gate #define PT_LOAD		1
1817c478bd9Sstevel@tonic-gate #define PT_DYNAMIC	2
1827c478bd9Sstevel@tonic-gate #define PT_INTERP	3
1837c478bd9Sstevel@tonic-gate #define PT_NOTE		4
1847c478bd9Sstevel@tonic-gate #define PT_SHLIB	5
1857c478bd9Sstevel@tonic-gate #define PT_PHDR		6
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate #define PT_LOPROC	0x70000000
1887c478bd9Sstevel@tonic-gate #define PT_HIPROC	0x7fffffff
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /* segment permissions - page 5-6 */
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate #define PF_X		0x1
1937c478bd9Sstevel@tonic-gate #define PF_W		0x2
1947c478bd9Sstevel@tonic-gate #define PF_R		0x4
1957c478bd9Sstevel@tonic-gate #define PF_MASKPROC	0xf0000000
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /* dynamic structure - page 5-15, figure 5-9 */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate typedef struct
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate   Elf32_Sword d_tag;
2037c478bd9Sstevel@tonic-gate   union
2047c478bd9Sstevel@tonic-gate   {
2057c478bd9Sstevel@tonic-gate     Elf32_Word d_val;
2067c478bd9Sstevel@tonic-gate     Elf32_Addr d_ptr;
2077c478bd9Sstevel@tonic-gate   }
2087c478bd9Sstevel@tonic-gate   d_un;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate Elf32_Dyn;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /* Dynamic array tags - page 5-16, figure 5-10.  */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate #define DT_NULL		0
2157c478bd9Sstevel@tonic-gate #define DT_NEEDED	1
2167c478bd9Sstevel@tonic-gate #define DT_PLTRELSZ	2
2177c478bd9Sstevel@tonic-gate #define DT_PLTGOT	3
2187c478bd9Sstevel@tonic-gate #define DT_HASH		4
2197c478bd9Sstevel@tonic-gate #define DT_STRTAB	5
2207c478bd9Sstevel@tonic-gate #define DT_SYMTAB	6
2217c478bd9Sstevel@tonic-gate #define DT_RELA		7
2227c478bd9Sstevel@tonic-gate #define DT_RELASZ	8
2237c478bd9Sstevel@tonic-gate #define DT_RELAENT      9
2247c478bd9Sstevel@tonic-gate #define DT_STRSZ	10
2257c478bd9Sstevel@tonic-gate #define DT_SYMENT	11
2267c478bd9Sstevel@tonic-gate #define DT_INIT		12
2277c478bd9Sstevel@tonic-gate #define DT_FINI		13
2287c478bd9Sstevel@tonic-gate #define DT_SONAME	14
2297c478bd9Sstevel@tonic-gate #define DT_RPATH	15
2307c478bd9Sstevel@tonic-gate #define DT_SYMBOLIC	16
2317c478bd9Sstevel@tonic-gate #define DT_REL		17
2327c478bd9Sstevel@tonic-gate #define DT_RELSZ	18
2337c478bd9Sstevel@tonic-gate #define DT_RELENT	19
2347c478bd9Sstevel@tonic-gate #define DT_PLTREL	20
2357c478bd9Sstevel@tonic-gate #define DT_DEBUG	21
2367c478bd9Sstevel@tonic-gate #define DT_TEXTREL	22
2377c478bd9Sstevel@tonic-gate #define DT_JMPREL	23
238