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 
20 #ifndef TARGET_OS_ELF_H
21 #define TARGET_OS_ELF_H
22 
23 #include "target_arch_elf.h"
24 #include "elf.h"
25 #include "user/tswap-target.h"
26 
27 #define bsd_get_ncpu() 1 /* until we pull in bsd-proc.[hc] */
28 
29 /* this flag is uneffective under linux too, should be deleted */
30 #ifndef MAP_DENYWRITE
31 #define MAP_DENYWRITE 0
32 #endif
33 
34 /* should probably go in elf.h */
35 #ifndef ELIBBAD
36 #define ELIBBAD 80
37 #endif
38 
39 #ifndef ELF_PLATFORM
40 #define ELF_PLATFORM (NULL)
41 #endif
42 
43 /* XXX Look at the other conflicting AT_* values. */
44 #define FREEBSD_AT_NCPUS     19
45 #define FREEBSD_AT_HWCAP     25
46 #define FREEBSD_AT_HWCAP2    26
47 
48 #ifdef TARGET_ABI32
49 #undef ELF_CLASS
50 #define ELF_CLASS ELFCLASS32
51 #undef bswaptls
52 #define bswaptls(ptr) bswap32s(ptr)
53 #endif
54 
55 /* max code+data+bss space allocated to elf interpreter */
56 #define INTERP_MAP_SIZE (32 * 1024 * 1024)
57 
58 /* max code+data+bss+brk space allocated to ET_DYN executables */
59 #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
60 
61 /* Necessary parameters */
62 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
63 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
64         ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE - 1))
65 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE - 1))
66 
67 #define DLINFO_ITEMS 14
68 
69 static abi_ulong target_create_elf_tables(abi_ulong p, int argc, int envc,
70                                           abi_ulong stringp,
71                                           struct elfhdr *exec,
72                                           abi_ulong load_addr,
73                                           abi_ulong load_bias,
74                                           abi_ulong interp_load_addr,
75                                           struct image_info *info)
76 {
77         abi_ulong features, sp;
78         int size;
79         const int n = sizeof(elf_addr_t);
80 
81         target_auxents_sz = 0;
82         sp = p;
83         /*
84          * Force 16 byte _final_ alignment here for generality.
85          */
86         sp = sp & ~(abi_ulong)15;
87         size = (DLINFO_ITEMS + 1) * 2;
88         size += envc + argc + 2;
89         size += 1;                      /* argc itself */
90         size *= n;
91         if (size & 15) {
92             sp -= 16 - (size & 15);
93         }
94 
95         /*
96          * FreeBSD defines elf_addr_t as Elf32_Off / Elf64_Off
97          */
98 #define NEW_AUX_ENT(id, val) do {               \
99             sp -= n; put_user_ual(val, sp);     \
100             sp -= n; put_user_ual(id, sp);      \
101             target_auxents_sz += 2 * n;         \
102           } while (0)
103 
104         NEW_AUX_ENT(AT_NULL, 0);
105 
106         /* There must be exactly DLINFO_ITEMS entries here.  */
107         NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
108         NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof(struct elf_phdr)));
109         NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
110         NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
111         NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
112         NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
113         NEW_AUX_ENT(FREEBSD_AT_NCPUS, (abi_ulong)bsd_get_ncpu());
114         NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
115         features = ELF_HWCAP;
116         NEW_AUX_ENT(FREEBSD_AT_HWCAP, features);
117 #ifdef ELF_HWCAP2
118         features = ELF_HWCAP2;
119         NEW_AUX_ENT(FREEBSD_AT_HWCAP2, features);
120 #endif
121         NEW_AUX_ENT(AT_UID, (abi_ulong)getuid());
122         NEW_AUX_ENT(AT_EUID, (abi_ulong)geteuid());
123         NEW_AUX_ENT(AT_GID, (abi_ulong)getgid());
124         NEW_AUX_ENT(AT_EGID, (abi_ulong)getegid());
125         target_auxents = sp; /* Note where the aux entries are in the target */
126 #ifdef ARCH_DLINFO
127         /*
128          * ARCH_DLINFO must come last so platform specific code can enforce
129          * special alignment requirements on the AUXV if necessary (eg. PPC).
130          */
131         ARCH_DLINFO;
132 #endif
133 #undef NEW_AUX_ENT
134 
135         sp = loader_build_argptr(envc, argc, sp, stringp);
136         return sp;
137 }
138 
139 #endif /* TARGET_OS_ELF_H */
140