1253103Simp/*
2253103Simp * Copyright (c) 2013 M. Warner Losh. All Rights Reserved.
3178172Simp *
4178172Simp * Redistribution and use in source and binary forms, with or without
5178172Simp * modification, are permitted provided that the following conditions
6178172Simp * are met:
7178172Simp * 1. Redistributions of source code must retain the above copyright
8178172Simp *    notice, this list of conditions and the following disclaimer.
9178172Simp * 2. Redistributions in binary form must reproduce the above copyright
10178172Simp *    notice, this list of conditions and the following disclaimer in the
11178172Simp *    documentation and/or other materials provided with the distribution.
12178172Simp *
13253103Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14253103Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15253103Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16253103Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17253103Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18178172Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19178172Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20178172Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21178172Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22178172Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23178172Simp * SUCH DAMAGE.
24178172Simp *
25178172Simp * $FreeBSD$
26253103Simp */
27253103Simp
28253103Simp/*-
29253103Simp * Copyright (c) 2013 The NetBSD Foundation, Inc.
30253103Simp * All rights reserved.
31178172Simp *
32253103Simp * Redistribution and use in source and binary forms, with or without
33253103Simp * modification, are permitted provided that the following conditions
34253103Simp * are met:
35253103Simp * 1. Redistributions of source code must retain the above copyright
36253103Simp *    notice, this list of conditions and the following disclaimer.
37253103Simp * 2. Redistributions in binary form must reproduce the above copyright
38253103Simp *    notice, this list of conditions and the following disclaimer in the
39253103Simp *    documentation and/or other materials provided with the distribution.
40253103Simp *
41253103Simp * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
42253103Simp * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
43253103Simp * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44253103Simp * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
45253103Simp * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46253103Simp * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47253103Simp * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48253103Simp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49253103Simp * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50253103Simp * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51253103Simp * POSSIBILITY OF SUCH DAMAGE.
52253103Simp *
53253103Simp * See below starting with the line with $NetBSD...$ for code this applies to.
54178172Simp */
55178172Simp
56253103Simp#ifndef	__MIPS_ELF_H
57253103Simp#define	__MIPS_ELF_H
58178172Simp
59253103Simp/* FreeBSD specific bits - derived from FreeBSD specific files and changes to old elf.h */
60178172Simp
61253103Simp/*
62253103Simp * Define __ELF_WORD_SIZE based on the ABI, if not defined yet. This sets
63253103Simp * the proper defaults when we're not trying to do 32-bit on 64-bit systems.
64253103Simp * We include both 32 and 64 bit versions so we can support multiple ABIs.
65253103Simp */
66186191Simp#ifndef __ELF_WORD_SIZE
67202031Simp#if defined(__mips_n64)
68253103Simp#define __ELF_WORD_SIZE 64
69202031Simp#else
70253103Simp#define __ELF_WORD_SIZE 32
71186191Simp#endif
72202031Simp#endif
73253103Simp#include <sys/elf32.h>
74253103Simp#include <sys/elf64.h>
75178172Simp#include <sys/elf_generic.h>
76178172Simp
77253103Simp#define ELF_ARCH	EM_MIPS
78253103Simp#define ELF_ARCH32	EM_MIPS
79178172Simp
80285052Semaste#define	ELF_MACHINE_OK(x)	((x) == ELF_ARCH)
81285052Semaste
82178172Simp/* Define "machine" characteristics */
83186191Simp#if __ELF_WORD_SIZE == 32
84178172Simp#define	ELF_TARG_CLASS	ELFCLASS32
85186191Simp#else
86186191Simp#define	ELF_TARG_CLASS	ELFCLASS64
87186191Simp#endif
88178172Simp#ifdef __MIPSEB__
89178172Simp#define	ELF_TARG_DATA	ELFDATA2MSB
90178172Simp#else
91178172Simp#define ELF_TARG_DATA	ELFDATA2LSB
92178172Simp#endif
93178172Simp#define	ELF_TARG_MACH	EM_MIPS
94178172Simp#define	ELF_TARG_VER	1
95178172Simp
96178172Simp/*
97178172Simp * Auxiliary vector entries for passing information to the interpreter.
98178172Simp *
99178172Simp * The i386 supplement to the SVR4 ABI specification names this "auxv_t",
100178172Simp * but POSIX lays claim to all symbols ending with "_t".
101178172Simp */
102178172Simptypedef struct {	/* Auxiliary vector entry on initial stack */
103178172Simp	int	a_type;			/* Entry type. */
104178172Simp	union {
105186191Simp		int	a_val;		/* Integer value. */
106178172Simp		void	*a_ptr;		/* Address. */
107178172Simp		void	(*a_fcn)(void); /* Function pointer (not used). */
108178172Simp	} a_un;
109178172Simp} Elf32_Auxinfo;
110178172Simp
111186191Simptypedef struct {	/* Auxiliary vector entry on initial stack */
112186191Simp	long	a_type;			/* Entry type. */
113186191Simp	union {
114186191Simp		long	a_val;		/* Integer value. */
115186191Simp		void	*a_ptr;		/* Address. */
116186191Simp		void	(*a_fcn)(void); /* Function pointer (not used). */
117186191Simp	} a_un;
118186191Simp} Elf64_Auxinfo;
119186191Simp
120178172Simp__ElfType(Auxinfo);
121178172Simp
122178172Simp/* Values for a_type. */
123178172Simp#define	AT_NULL		0	/* Terminates the vector. */
124178172Simp#define	AT_IGNORE	1	/* Ignored entry. */
125178172Simp#define	AT_EXECFD	2	/* File descriptor of program to load. */
126178172Simp#define	AT_PHDR		3	/* Program header of program already loaded. */
127178172Simp#define	AT_PHENT	4	/* Size of each program header entry. */
128178172Simp#define	AT_PHNUM	5	/* Number of program header entries. */
129178172Simp#define	AT_PAGESZ	6	/* Page size in bytes. */
130178172Simp#define	AT_BASE		7	/* Interpreter's base address. */
131178172Simp#define	AT_FLAGS	8	/* Flags (unused for i386). */
132178172Simp#define	AT_ENTRY	9	/* Where interpreter should transfer control. */
133178172Simp#define	AT_NOTELF	10	/* Program is not ELF ?? */
134178172Simp#define	AT_UID		11	/* Real uid. */
135178172Simp#define	AT_EUID		12	/* Effective uid. */
136178172Simp#define	AT_GID		13	/* Real gid. */
137178172Simp#define	AT_EGID		14	/* Effective gid. */
138189926Skib#define	AT_EXECPATH	15	/* Path to the executable. */
139211412Skib#define	AT_CANARY	16	/* Canary for SSP */
140211412Skib#define	AT_CANARYLEN	17	/* Length of the canary. */
141211412Skib#define	AT_OSRELDATE	18	/* OSRELDATE. */
142211412Skib#define	AT_NCPUS	19	/* Number of CPUs. */
143211412Skib#define	AT_PAGESIZES	20	/* Pagesizes. */
144211412Skib#define	AT_PAGESIZESLEN	21	/* Number of pagesizes. */
145237430Skib#define	AT_TIMEKEEP	22	/* Pointer to timehands. */
146217097Skib#define	AT_STACKPROT	23	/* Initial stack protection. */
147178172Simp
148217097Skib#define	AT_COUNT	24	/* Count of defined aux entry types. */
149178172Simp
150197933Skib#define	ET_DYN_LOAD_ADDR 0x0120000
151197933Skib
152202908Sgonzo/*
153202908Sgonzo * Constant to mark start of symtab/strtab saved by trampoline
154202908Sgonzo */
155202908Sgonzo#define	SYMTAB_MAGIC	0x64656267
156202908Sgonzo
157253103Simp/* from NetBSD's sys/mips/include/elf_machdep.h $NetBSD: elf_machdep.h,v 1.18 2013/05/23 21:39:49 christos Exp $ */
158253103Simp
159253103Simp/* mips relocs. */
160253103Simp
161253103Simp#define	R_MIPS_NONE		0
162253103Simp#define	R_MIPS_16		1
163253103Simp#define	R_MIPS_32		2
164253103Simp#define	R_MIPS_REL32		3
165253103Simp#define	R_MIPS_REL		R_MIPS_REL32
166253103Simp#define	R_MIPS_26		4
167253103Simp#define	R_MIPS_HI16		5	/* high 16 bits of symbol value */
168253103Simp#define	R_MIPS_LO16		6	/* low 16 bits of symbol value */
169253103Simp#define	R_MIPS_GPREL16		7	/* GP-relative reference  */
170253103Simp#define	R_MIPS_LITERAL		8	/* Reference to literal section  */
171253103Simp#define	R_MIPS_GOT16		9	/* Reference to global offset table */
172253103Simp#define	R_MIPS_GOT		R_MIPS_GOT16
173253103Simp#define	R_MIPS_PC16		10	/* 16 bit PC relative reference */
174253103Simp#define	R_MIPS_CALL16 		11	/* 16 bit call thru glbl offset tbl */
175253103Simp#define	R_MIPS_CALL		R_MIPS_CALL16
176253103Simp#define	R_MIPS_GPREL32		12
177253103Simp
178253103Simp/* 13, 14, 15 are not defined at this point. */
179253103Simp#define	R_MIPS_UNUSED1		13
180253103Simp#define	R_MIPS_UNUSED2		14
181253103Simp#define	R_MIPS_UNUSED3		15
182253103Simp
183253103Simp/*
184253103Simp * The remaining relocs are apparently part of the 64-bit Irix ELF ABI.
185253103Simp */
186253103Simp#define	R_MIPS_SHIFT5		16
187253103Simp#define	R_MIPS_SHIFT6		17
188253103Simp
189253103Simp#define	R_MIPS_64		18
190253103Simp#define	R_MIPS_GOT_DISP		19
191253103Simp#define	R_MIPS_GOT_PAGE		20
192253103Simp#define	R_MIPS_GOT_OFST		21
193253103Simp#define	R_MIPS_GOT_HI16		22
194253103Simp#define	R_MIPS_GOT_LO16		23
195253103Simp#define	R_MIPS_SUB		24
196253103Simp#define	R_MIPS_INSERT_A		25
197253103Simp#define	R_MIPS_INSERT_B		26
198253103Simp#define	R_MIPS_DELETE		27
199253103Simp#define	R_MIPS_HIGHER		28
200253103Simp#define	R_MIPS_HIGHEST		29
201253103Simp#define	R_MIPS_CALL_HI16	30
202253103Simp#define	R_MIPS_CALL_LO16	31
203253103Simp#define	R_MIPS_SCN_DISP		32
204253103Simp#define	R_MIPS_REL16		33
205253103Simp#define	R_MIPS_ADD_IMMEDIATE	34
206253103Simp#define	R_MIPS_PJUMP		35
207253103Simp#define	R_MIPS_RELGOT		36
208253103Simp#define	R_MIPS_JALR		37
209253103Simp/* TLS relocations */
210253103Simp
211253103Simp#define	R_MIPS_TLS_DTPMOD32	38	/* Module number 32 bit */
212253103Simp#define	R_MIPS_TLS_DTPREL32	39	/* Module-relative offset 32 bit */
213253103Simp#define	R_MIPS_TLS_DTPMOD64	40	/* Module number 64 bit */
214253103Simp#define	R_MIPS_TLS_DTPREL64	41	/* Module-relative offset 64 bit */
215253103Simp#define	R_MIPS_TLS_GD		42	/* 16 bit GOT offset for GD */
216253103Simp#define	R_MIPS_TLS_LDM		43	/* 16 bit GOT offset for LDM */
217253103Simp#define	R_MIPS_TLS_DTPREL_HI16	44	/* Module-relative offset, high 16 bits */
218253103Simp#define	R_MIPS_TLS_DTPREL_LO16	45	/* Module-relative offset, low 16 bits */
219253103Simp#define	R_MIPS_TLS_GOTTPREL	46	/* 16 bit GOT offset for IE */
220253103Simp#define	R_MIPS_TLS_TPREL32	47	/* TP-relative offset, 32 bit */
221253103Simp#define	R_MIPS_TLS_TPREL64	48	/* TP-relative offset, 64 bit */
222253103Simp#define	R_MIPS_TLS_TPREL_HI16	49	/* TP-relative offset, high 16 bits */
223253103Simp#define	R_MIPS_TLS_TPREL_LO16	50	/* TP-relative offset, low 16 bits */
224253103Simp
225253103Simp#define	R_MIPS_max		51
226253103Simp
227253103Simp#define	R_TYPE(name)		__CONCAT(R_MIPS_,name)
228253103Simp
229253103Simp#define	R_MIPS16_min		100
230253103Simp#define	R_MIPS16_26		100
231253103Simp#define	R_MIPS16_GPREL		101
232253103Simp#define	R_MIPS16_GOT16		102
233253103Simp#define	R_MIPS16_CALL16		103
234253103Simp#define	R_MIPS16_HI16		104
235253103Simp#define	R_MIPS16_LO16		105
236253103Simp#define	R_MIPS16_max		106
237253103Simp
238253103Simp#define	R_MIPS_COPY		126
239253103Simp#define	R_MIPS_JUMP_SLOT	127
240253103Simp
241253103Simp/*
242253103Simp * ELF Flags
243253103Simp */
244253103Simp
245253103Simp#define	EF_MIPS_ARCH_1		0x00000000	/* -mips1 code */
246253103Simp#define	EF_MIPS_ARCH_2		0x10000000	/* -mips2 code */
247253103Simp#define	EF_MIPS_ARCH_3		0x20000000	/* -mips3 code */
248253103Simp#define	EF_MIPS_ARCH_4		0x30000000	/* -mips4 code */
249253103Simp#define	EF_MIPS_ARCH_5		0x40000000	/* -mips5 code */
250253103Simp#define	EF_MIPS_ARCH_32		0x50000000	/* -mips32 code */
251253103Simp#define	EF_MIPS_ARCH_64		0x60000000	/* -mips64 code */
252253103Simp#define	EF_MIPS_ARCH_32R2	0x70000000	/* -mips32r2 code */
253253103Simp#define	EF_MIPS_ARCH_64R2	0x80000000	/* -mips64r2 code */
254253103Simp
255253103Simp#define	EF_MIPS_ABI		0x0000f000
256253103Simp#define	EF_MIPS_ABI_O32		0x00001000
257253103Simp#define	EF_MIPS_ABI_O64		0x00002000
258253103Simp#define	EF_MIPS_ABI_EABI32	0x00003000
259253103Simp#define	EF_MIPS_ABI_EABI64	0x00004000
260253103Simp
261253103Simp#endif /* __MIPS_ELF_H */
262