1 /*
2  *  freebsd ELF definitions
3  *
4  *  Copyright (c) 2013-15 Stacey D. Son
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef _TARGET_OS_ELF_H_
20 #define _TARGET_OS_ELF_H_
21 
22 #include "target_arch_elf.h"
23 #include "elf.h"
24 
25 #define bsd_get_ncpu() 1 /* until we pull in bsd-proc.[hc] */
26 
27 /* this flag is uneffective under linux too, should be deleted */
28 #ifndef MAP_DENYWRITE
29 #define MAP_DENYWRITE 0
30 #endif
31 
32 /* should probably go in elf.h */
33 #ifndef ELIBBAD
34 #define ELIBBAD 80
35 #endif
36 
37 #ifndef ELF_PLATFORM
38 #define ELF_PLATFORM (NULL)
39 #endif
40 
41 /* XXX Look at the other conflicting AT_* values. */
42 #define FREEBSD_AT_NCPUS     19
43 #define FREEBSD_AT_HWCAP     25
44 #define FREEBSD_AT_HWCAP2    26
45 
46 #ifdef TARGET_ABI32
47 #undef ELF_CLASS
48 #define ELF_CLASS ELFCLASS32
49 #undef bswaptls
50 #define bswaptls(ptr) bswap32s(ptr)
51 #endif
52 
53 /* max code+data+bss space allocated to elf interpreter */
54 #define INTERP_MAP_SIZE (32 * 1024 * 1024)
55 
56 /* max code+data+bss+brk space allocated to ET_DYN executables */
57 #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
58 
59 /* Necessary parameters */
60 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
61 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
62         ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE - 1))
63 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE - 1))
64 
65 #define DLINFO_ITEMS 14
66 
67 static abi_ulong target_create_elf_tables(abi_ulong p, int argc, int envc,
68                                           abi_ulong stringp,
69                                           struct elfhdr *exec,
70                                           abi_ulong load_addr,
71                                           abi_ulong load_bias,
72                                           abi_ulong interp_load_addr,
73                                           struct image_info *info)
74 {
75         abi_ulong features, sp;
76         int size;
77         const int n = sizeof(elf_addr_t);
78 
79         target_auxents_sz = 0;
80         sp = p;
81         /*
82          * Force 16 byte _final_ alignment here for generality.
83          */
84         sp = sp & ~(abi_ulong)15;
85         size = (DLINFO_ITEMS + 1) * 2;
86         size += envc + argc + 2;
87         size += 1;                      /* argc itself */
88         size *= n;
89         if (size & 15) {
90             sp -= 16 - (size & 15);
91         }
92 
93         /*
94          * FreeBSD defines elf_addr_t as Elf32_Off / Elf64_Off
95          */
96 #define NEW_AUX_ENT(id, val) do {               \
97             sp -= n; put_user_ual(val, sp);     \
98             sp -= n; put_user_ual(id, sp);      \
99             target_auxents_sz += 2 * n;         \
100           } while (0)
101 
102         NEW_AUX_ENT(AT_NULL, 0);
103 
104         /* There must be exactly DLINFO_ITEMS entries here.  */
105         NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
106         NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof(struct elf_phdr)));
107         NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
108         NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
109         NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
110         NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
111         NEW_AUX_ENT(FREEBSD_AT_NCPUS, (abi_ulong)bsd_get_ncpu());
112         NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
113         features = ELF_HWCAP;
114         NEW_AUX_ENT(FREEBSD_AT_HWCAP, features);
115 #ifdef ELF_HWCAP2
116         features = ELF_HWCAP2;
117         NEW_AUX_ENT(FREEBSD_AT_HWCAP2, features);
118 #endif
119         NEW_AUX_ENT(AT_UID, (abi_ulong)getuid());
120         NEW_AUX_ENT(AT_EUID, (abi_ulong)geteuid());
121         NEW_AUX_ENT(AT_GID, (abi_ulong)getgid());
122         NEW_AUX_ENT(AT_EGID, (abi_ulong)getegid());
123         target_auxents = sp; /* Note where the aux entries are in the target */
124 #ifdef ARCH_DLINFO
125         /*
126          * ARCH_DLINFO must come last so platform specific code can enforce
127          * special alignment requirements on the AUXV if necessary (eg. PPC).
128          */
129         ARCH_DLINFO;
130 #endif
131 #undef NEW_AUX_ENT
132 
133         sp = loader_build_argptr(envc, argc, sp, stringp);
134         return sp;
135 }
136 
137 #endif /* _TARGET_OS_ELF_H_ */
138