1 /*
2  *  openbsd ELF definitions
3  *
4  *  Copyright (c) 2013 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 /* this flag is uneffective under linux too, should be deleted */
26 #ifndef MAP_DENYWRITE
27 #define MAP_DENYWRITE 0
28 #endif
29 
30 /* should probably go in elf.h */
31 #ifndef ELIBBAD
32 #define ELIBBAD 80
33 #endif
34 
35 #ifndef ELF_PLATFORM
36 #define ELF_PLATFORM (NULL)
37 #endif
38 
39 #ifndef ELF_HWCAP
40 #define ELF_HWCAP 0
41 #endif
42 
43 #ifdef TARGET_ABI32
44 #undef ELF_CLASS
45 #define ELF_CLASS ELFCLASS32
46 #undef bswaptls
47 #define bswaptls(ptr) bswap32s(ptr)
48 #endif
49 
50 /* max code+data+bss space allocated to elf interpreter */
51 #define INTERP_MAP_SIZE (32 * 1024 * 1024)
52 
53 /* max code+data+bss+brk space allocated to ET_DYN executables */
54 #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
55 
56 /* Necessary parameters */
57 #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
58 #define TARGET_ELF_PAGESTART(_v) ((_v) & \
59         ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE - 1))
60 #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE - 1))
61 
62 #define DLINFO_ITEMS 12
63 
64 static abi_ulong target_create_elf_tables(abi_ulong p, int argc, int envc,
65                                           abi_ulong stringp,
66                                           struct elfhdr *exec,
67                                           abi_ulong load_addr,
68                                           abi_ulong load_bias,
69                                           abi_ulong interp_load_addr,
70                                           struct image_info *info)
71 {
72         abi_ulong sp;
73         int size;
74         abi_ulong u_platform;
75         const char *k_platform;
76         const int n = sizeof(elf_addr_t);
77 
78         sp = p;
79         u_platform = 0;
80         k_platform = ELF_PLATFORM;
81         if (k_platform) {
82             size_t len = strlen(k_platform) + 1;
83             sp -= (len + n - 1) & ~(n - 1);
84             u_platform = sp;
85             /* FIXME - check return value of memcpy_to_target() for failure */
86             memcpy_to_target(sp, k_platform, len);
87         }
88         /*
89          * Force 16 byte _final_ alignment here for generality.
90          */
91         sp = sp & ~(abi_ulong)15;
92         size = (DLINFO_ITEMS + 1) * 2;
93         if (k_platform) {
94             size += 2;
95         }
96 #ifdef DLINFO_ARCH_ITEMS
97         size += DLINFO_ARCH_ITEMS * 2;
98 #endif
99         size += envc + argc + 2;
100         size += 1;                        /* argc itself */
101         size *= n;
102         if (size & 15) {
103             sp -= 16 - (size & 15);
104         }
105 
106         /*
107          * OpenBSD defines elf_addr_t as Elf32_Off / Elf64_Off
108          */
109 #define NEW_AUX_ENT(id, val) do {               \
110             sp -= n; put_user_ual(val, sp);     \
111             sp -= n; put_user_ual(id, sp);      \
112           } while (0)
113 
114         NEW_AUX_ENT(AT_NULL, 0);
115 
116         /* There must be exactly DLINFO_ITEMS entries here.  */
117         NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
118         NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof(struct elf_phdr)));
119         NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
120         NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
121         NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
122         NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
123         NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
124         NEW_AUX_ENT(AT_UID, (abi_ulong)getuid());
125         NEW_AUX_ENT(AT_EUID, (abi_ulong)geteuid());
126         NEW_AUX_ENT(AT_GID, (abi_ulong)getgid());
127         NEW_AUX_ENT(AT_EGID, (abi_ulong)getegid());
128         NEW_AUX_ENT(AT_HWCAP, (abi_ulong)ELF_HWCAP);
129         NEW_AUX_ENT(AT_CLKTCK, (abi_ulong)sysconf(_SC_CLK_TCK));
130         if (k_platform) {
131             NEW_AUX_ENT(AT_PLATFORM, u_platform);
132         }
133 #ifdef ARCH_DLINFO
134         /*
135          * ARCH_DLINFO must come last so platform specific code can enforce
136          * special alignment requirements on the AUXV if necessary (eg. PPC).
137          */
138         ARCH_DLINFO;
139 #endif
140 #undef NEW_AUX_ENT
141 
142         sp = loader_build_argptr(envc, argc, sp, stringp);
143         return sp;
144 }
145 
146 #endif /* _TARGET_OS_ELF_H_ */
147